int main (int argc, char *argv[]) { DBusServer *server; DBusError error; DBusLoop *loop; DBusConnection *session; TestServiceData *testdata; dbus_error_init (&error); loop = _dbus_loop_new (); testdata = dbus_new (TestServiceData, 1); testdata->loop = loop; session = dbus_bus_get (DBUS_BUS_SESSION, &error); if (!session) die ("couldn't access session bus"); test_connection_setup (loop, session); dbus_bus_request_name (session, "org.freedesktop.DBus.TestSuite.PrivServer", 0, &error); if (dbus_error_is_set (&error)) die ("couldn't request name: %s", error.message); if (!dbus_connection_add_filter (session, filter_session_message, testdata, NULL)) die ("couldn't add filter"); #ifdef DBUS_CMAKE server = dbus_server_listen (TEST_LISTEN, &error); #else server = dbus_server_listen ("unix:tmpdir=/tmp", &error); #endif if (!server) die ("%s", error.message); testdata->private_addr = dbus_server_get_address (server); fprintf (stderr, "test server listening on %s\n", testdata->private_addr); dbus_server_set_new_connection_function (server, new_connection_callback, testdata, NULL); test_server_setup (loop, server); fprintf (stderr, "server running mainloop\n"); _dbus_loop_run (loop); fprintf (stderr, "server mainloop quit\n"); test_server_shutdown (loop, server); test_connection_shutdown (loop, session); dbus_connection_unref (session); _dbus_loop_unref (loop); dbus_free (testdata); return 0; }
int main (int argc, char **argv) { DBusError error; DBusConnection *connection; dbus_error_init (&error); connection = dbus_bus_get (DBUS_BUS_SESSION, &error); if (connection == NULL) { fprintf (stderr, "*** Failed to open connection to system bus: %s\n", error.message); dbus_error_free (&error); return 1; } loop = _dbus_loop_new (); if (loop == NULL) die ("No memory\n"); if (!test_connection_setup (loop, connection)) die ("No memory\n"); TestName(connection, "org.freedesktop.DBus.Test", TRUE); TestName(connection, "org.freedesktop.DBus.Test-2", TRUE); TestName(connection, "org.freedesktop.DBus.Test_2", TRUE); #if 0 TestName(connection, "Test_2", TRUE); #endif _dbus_verbose ("*** Test service name exiting\n"); return 0; }
static void setup (Fixture *f, gconstpointer data) { if (!dbus_threads_init_default ()) g_error ("OOM"); f->loop = _dbus_loop_new (); g_assert (f->loop != NULL); dbus_error_init (&f->e); f->server = dbus_server_listen ("tcp:host=127.0.0.1", &f->e); assert_no_error (&f->e); g_assert (f->server != NULL); if (!dbus_connection_allocate_data_slot (&connection_slot)) g_error ("OOM"); if (!dbus_server_allocate_data_slot (&server_slot)) g_error ("OOM"); if (!dbus_message_allocate_data_slot (&message_slot)) g_error ("OOM"); if (!dbus_pending_call_allocate_data_slot (&pending_call_slot)) g_error ("OOM"); }
static void open_destroy_shared_session_bus_connection (void) { DBusError error; DBusConnection *connection; char *session_addr_no_guid; char *comma; dbus_error_init (&error); session_addr_no_guid = strdup (getenv ("DBUS_SESSION_BUS_ADDRESS")); comma = strchr (session_addr_no_guid, ','); if (comma == NULL) die ("Couldn't find GUID in session bus address"); *comma = '\0'; connection = dbus_connection_open (session_addr_no_guid, &error); free (session_addr_no_guid); if (connection == NULL) die ("Failed to open connection to temp session bus\n"); loop = _dbus_loop_new (); if (loop == NULL) die ("No memory\n"); if (!test_connection_setup (loop, connection)) die ("No memory\n"); test_connection_shutdown (loop, connection); _dbus_loop_unref (loop); dbus_connection_unref (connection); }
static void setup (Fixture *f, gconstpointer data) { if (!dbus_threads_init_default ()) g_error ("OOM"); f->n_threads = N_THREADS; f->n_refs = N_REFS; // wine sets WINESERVERSOCKET for its child processes automatically if (g_getenv ("WINESERVERSOCKET") != NULL) { /* Our reference-counting is really slow under Wine (it involves * IPC to wineserver). Do fewer iterations: enough to demonstrate * that it works, rather than a performance test. */ f->n_threads = 10; f->n_refs = 10; } f->loop = _dbus_loop_new (); g_assert (f->loop != NULL); dbus_error_init (&f->e); f->server = dbus_server_listen ("tcp:host=127.0.0.1", &f->e); assert_no_error (&f->e); g_assert (f->server != NULL); if (!dbus_connection_allocate_data_slot (&connection_slot)) g_error ("OOM"); if (!dbus_server_allocate_data_slot (&server_slot)) g_error ("OOM"); if (!dbus_message_allocate_data_slot (&message_slot)) g_error ("OOM"); if (!dbus_pending_call_allocate_data_slot (&pending_call_slot)) g_error ("OOM"); }
static void open_shutdown_private_connection (dbus_bool_t use_guid) { DBusError error; DBusLoop *loop; DBusConnection *session; DBusMessage *msg; DBusMessage *reply; DBusConnection *privconn; char *addr; dbus_bool_t service_died; dbus_bool_t private_conn_lost; dbus_error_init (&error); service_died = FALSE; private_conn_lost = FALSE; loop = _dbus_loop_new (); session = dbus_bus_get (DBUS_BUS_SESSION, &error); if (!session) die ("couldn't access session bus\n"); dbus_connection_set_exit_on_disconnect (session, FALSE); test_connection_setup (loop, session); dbus_bus_add_match (session, PRIVSERVER_DIED_RULE, &error); if (dbus_error_is_set (&error)) die ("couldn't add match rule \"%s\": %s: %s", PRIVSERVER_DIED_RULE, error.name, error.message); if (!dbus_connection_add_filter (session, filter_session_message, &service_died, NULL)) die ("couldn't add filter to session bus\n"); msg = dbus_message_new_method_call (PRIVSERVER_SERVICE, "/", PRIVSERVER_INTERFACE, "GetPrivateAddress"); if (!(reply = dbus_connection_send_with_reply_and_block (session, msg, -1, &error))) die ("couldn't send message: %s\n", error.message); dbus_message_unref (msg); if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &addr, DBUS_TYPE_INVALID)) die ("couldn't parse message replym\n"); printf ("# got private temp address %s\n", addr); addr = strdup (addr); if (!use_guid) { char *comma = strrchr (addr, ','); if (comma) *comma = '\0'; } privconn = dbus_connection_open (addr, &error); free (addr); if (!privconn) die ("couldn't connect to server direct connection: %s\n", error.message); dbus_message_unref (reply); dbus_connection_set_exit_on_disconnect (privconn, FALSE); if (!dbus_connection_add_filter (privconn, filter_private_message, &private_conn_lost, NULL)) die ("couldn't add filter to private connection\n"); test_connection_setup (loop, privconn); msg = dbus_message_new_method_call (PRIVSERVER_SERVICE, "/", PRIVSERVER_INTERFACE, "Quit"); if (!dbus_connection_send (session, msg, NULL)) die ("couldn't send Quit message\n"); dbus_message_unref (msg); while (!service_died || !private_conn_lost) _dbus_loop_iterate (loop, TRUE); dbus_connection_remove_filter (session, filter_session_message, &service_died); dbus_bus_remove_match (session, PRIVSERVER_DIED_RULE, NULL); test_connection_shutdown (loop, session); dbus_connection_unref (session); test_connection_shutdown (loop, privconn); dbus_connection_remove_filter (privconn, filter_private_message, &private_conn_lost); dbus_connection_unref (privconn); _dbus_loop_unref (loop); }
dbusrecv_hdl_st *dbusrecv_init(char *myname, callbk_filter_func filter_func) { dbusrecv_hdl_st *p_dbusrecv_hdl = NULL; int result = 0; int retval = 0; if (NULL == myname) { printf("dbusrev_init: invalid parameters, myname couldn't be NULL!\n"); return NULL; } if (NULL == filter_func) { printf("dbusrev_init: invalid parameters, filter_func couldn't be NULL!\n"); return NULL; } p_dbusrecv_hdl = DBUS_MALLOC(sizeof(dbusrecv_hdl_st)); if (NULL == p_dbusrecv_hdl) { printf("dbusrev_init: malloc failed, p_dbusrecv_hdl!\n"); return NULL; } p_dbusrecv_hdl->dbusrecv_cfg.myname = (char *)malloc(strlen(myname) + 1); memset(p_dbusrecv_hdl->dbusrecv_cfg.myname, 0x0, strlen(myname) + 1); memcpy(p_dbusrecv_hdl->dbusrecv_cfg.myname, myname, strlen(myname)); p_dbusrecv_hdl->filter_func = filter_func; dbus_error_init (&p_dbusrecv_hdl->error); p_dbusrecv_hdl->connection = dbus_bus_get (DBUS_BUS_SYSTEM, &p_dbusrecv_hdl->error); if (NULL == p_dbusrecv_hdl->connection) { printf("%s: Failed to open connection to activating message bus: %s\n", __FILE__, p_dbusrecv_hdl->error.message); retval = -1; goto ERROR; } p_dbusrecv_hdl->dbusloop = _dbus_loop_new (); if (NULL == p_dbusrecv_hdl->dbusloop) { printf("%s: No memory, p_dbusrecv_hdl->dbusloop\n", __FILE__); retval = -1; goto ERROR; } if (!test_connection_setup (p_dbusrecv_hdl->dbusloop, p_dbusrecv_hdl->connection)) { printf("%s: No memory, test_connection_setup\n", __FILE__); retval = -1; goto ERROR; } /* match options: type interface; member; sender; destination; path; */ #if 0 memset(rules, 0x0, sizeof(rules)); sprintf(rules, "type='signal', interface='%s'", p_dbusrecv_hdl->dbusrecv_cfg.interface); dbus_bus_add_match (p_dbusrecv_hdl->connection, rules, &p_dbusrecv_hdl->error); if (dbus_error_is_set (&p_dbusrecv_hdl->error)) { printf("%s: add match(%s) failed\n", __FILE__, rules); retval = -1; goto ERROR; } memset(rules, 0x0, sizeof(rules)); sprintf(rules, "type='signal', interface='org.actiontec.Test'"); dbus_bus_add_match (p_dbusrecv_hdl->connection, rules, &p_dbusrecv_hdl->error); if (dbus_error_is_set (&p_dbusrecv_hdl->error)) { printf("%s: add match(%s) failed\n", __FILE__, rules); retval = -1; goto ERROR; } #endif if (!dbus_connection_add_filter(p_dbusrecv_hdl->connection, p_dbusrecv_hdl->filter_func, (void *)p_dbusrecv_hdl, NULL)) { printf("%s: No memory, dbus_connection_add_filter\n", __FILE__); retval = -1; goto ERROR; } p_dbusrecv_hdl->is_addfilter = 1; if (!dbus_connection_register_object_path (p_dbusrecv_hdl->connection, DBUS_DEFAULT_PATH, &dbusrecv_vtable, (void *)p_dbusrecv_hdl)) //(void*) 0xdeadbeef)) { printf("%s: No memory, dbus_connection_register_object_path\n", __FILE__); retval = -1; goto ERROR; } { void *d; if (!dbus_connection_get_object_path_data (p_dbusrecv_hdl->connection, DBUS_DEFAULT_PATH, &d)) { printf("%s: No memory, dbus_connection_get_object_path_data\n", __FILE__); retval = -1; goto ERROR; } //if (d != (void*) 0xdeadbeef) if (d != (void*) p_dbusrecv_hdl) { printf("%s: dbus_connection_get_object_path_data() doesn't seem to work right\n", __FILE__); retval = -1; goto ERROR; } } result = dbus_bus_request_name(p_dbusrecv_hdl->connection, myname, 0, &p_dbusrecv_hdl->error); if (dbus_error_is_set (&p_dbusrecv_hdl->error)) { printf("%s: Failed to acquire service: %s\n", __FILE__, p_dbusrecv_hdl->error.message); retval = -1; goto ERROR; } //_dbus_verbose("%s: service(%s) entering main loop!\n", __FILE__, myname); //_dbus_loop_run(p_dbusrecv_hdl->dbusloop); retval = 0; ERROR: if (-1 == retval) { dbus_error_free (&p_dbusrecv_hdl->error); if (p_dbusrecv_hdl->connection) { test_connection_shutdown (p_dbusrecv_hdl->dbusloop, p_dbusrecv_hdl->connection); if (p_dbusrecv_hdl->is_addfilter) dbus_connection_remove_filter (p_dbusrecv_hdl->connection, p_dbusrecv_hdl->filter_func, NULL);// dbus_connection_unref (p_dbusrecv_hdl->connection); p_dbusrecv_hdl->connection = NULL; } if (p_dbusrecv_hdl->dbusloop) { _dbus_loop_unref (p_dbusrecv_hdl->dbusloop); p_dbusrecv_hdl->dbusloop = NULL; } dbus_shutdown(); _dbus_verbose("%s: service(%s) exiting!\n", __FILE__, myname); return NULL; } return p_dbusrecv_hdl; }
dbus_bool_t bus_expire_list_test (const DBusString *test_data_dir) { DBusLoop *loop; BusExpireList *list; long tv_sec, tv_usec; long tv_sec_not_expired, tv_usec_not_expired; long tv_sec_expired, tv_usec_expired; long tv_sec_past, tv_usec_past; TestExpireItem *item; int next_interval; dbus_bool_t result = FALSE; loop = _dbus_loop_new (); _dbus_assert (loop != NULL); #define EXPIRE_AFTER 100 list = bus_expire_list_new (loop, EXPIRE_AFTER, test_expire_func, NULL); _dbus_assert (list != NULL); _dbus_get_monotonic_time (&tv_sec, &tv_usec); tv_sec_not_expired = tv_sec; tv_usec_not_expired = tv_usec; time_add_milliseconds (&tv_sec_not_expired, &tv_usec_not_expired, EXPIRE_AFTER - 1); tv_sec_expired = tv_sec; tv_usec_expired = tv_usec; time_add_milliseconds (&tv_sec_expired, &tv_usec_expired, EXPIRE_AFTER); tv_sec_past = tv_sec - 1; tv_usec_past = tv_usec; item = dbus_new0 (TestExpireItem, 1); if (item == NULL) goto oom; item->item.added_tv_sec = tv_sec; item->item.added_tv_usec = tv_usec; if (!bus_expire_list_add (list, &item->item)) _dbus_assert_not_reached ("out of memory"); next_interval = do_expiration_with_monotonic_time (list, tv_sec_not_expired, tv_usec_not_expired); _dbus_assert (item->expire_count == 0); _dbus_verbose ("next_interval = %d\n", next_interval); _dbus_assert (next_interval == 1); next_interval = do_expiration_with_monotonic_time (list, tv_sec_expired, tv_usec_expired); _dbus_assert (item->expire_count == 1); _dbus_verbose ("next_interval = %d\n", next_interval); _dbus_assert (next_interval == -1); next_interval = do_expiration_with_monotonic_time (list, tv_sec_past, tv_usec_past); _dbus_assert (item->expire_count == 1); _dbus_verbose ("next_interval = %d\n", next_interval); _dbus_assert (next_interval == 1000 + EXPIRE_AFTER); bus_expire_list_remove (list, &item->item); dbus_free (item); bus_expire_list_free (list); _dbus_loop_unref (loop); result = TRUE; oom: return result; }
int main (int argc, char **argv) { DBusError error; int result; DBusConnection *connection; dbus_error_init (&error); connection = dbus_bus_get (DBUS_BUS_STARTER, &error); if (connection == NULL) { fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", error.message); dbus_error_free (&error); return 1; } loop = _dbus_loop_new (); if (loop == NULL) die ("No memory\n"); if (!test_connection_setup (loop, connection)) die ("No memory\n"); if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) die ("No memory"); if (!dbus_connection_register_object_path (connection, echo_path, &echo_vtable, (void*) 0xdeadbeef)) die ("No memory"); { void *d; if (!dbus_connection_get_object_path_data (connection, echo_path, &d)) die ("No memory"); if (d != (void*) 0xdeadbeef) die ("dbus_connection_get_object_path_data() doesn't seem to work right\n"); } result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService", 0, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "Error %s\n", error.message); _dbus_verbose ("*** Failed to acquire service: %s\n", error.message); dbus_error_free (&error); exit (1); } _dbus_verbose ("*** Test service entering main loop\n"); _dbus_loop_run (loop); test_connection_shutdown (loop, connection); dbus_connection_remove_filter (connection, filter_func, NULL); dbus_connection_unref (connection); _dbus_loop_unref (loop); loop = NULL; dbus_shutdown (); _dbus_verbose ("*** Test service exiting\n"); return 0; }
int main (int argc, char **argv) { DBusError error; int result; DBusConnection *connection; const char *name; dbus_bool_t do_fork; if (argc != 3) { name = "org.freedesktop.DBus.TestSuiteEchoService"; do_fork = FALSE; } else { name = argv[1]; #ifndef DBUS_WIN do_fork = strcmp (argv[2], "fork") == 0; #else do_fork = FALSE; #endif } /* The bare minimum for simulating a program "daemonizing"; the intent * is to test services which move from being legacy init scripts to * activated services. * https://bugzilla.redhat.com/show_bug.cgi?id=545267 */ #ifndef DBUS_WIN if (do_fork) { pid_t pid = fork (); if (pid != 0) exit (0); sleep (1); } #endif dbus_error_init (&error); connection = dbus_bus_get (DBUS_BUS_STARTER, &error); if (connection == NULL) { fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n", error.message); dbus_error_free (&error); return 1; } loop = _dbus_loop_new (); if (loop == NULL) die ("No memory\n"); if (!test_connection_setup (loop, connection)) die ("No memory\n"); if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) die ("No memory"); if (!dbus_connection_register_object_path (connection, echo_path, &echo_vtable, (void*) 0xdeadbeef)) die ("No memory"); { void *d; if (!dbus_connection_get_object_path_data (connection, echo_path, &d)) die ("No memory"); if (d != (void*) 0xdeadbeef) die ("dbus_connection_get_object_path_data() doesn't seem to work right\n"); } result = dbus_bus_request_name (connection, name, 0, &error); if (dbus_error_is_set (&error)) { fprintf (stderr, "Error %s\n", error.message); _dbus_verbose ("*** Failed to acquire service: %s\n", error.message); dbus_error_free (&error); exit (1); } _dbus_verbose ("*** Test service entering main loop\n"); _dbus_loop_run (loop); test_connection_shutdown (loop, connection); dbus_connection_remove_filter (connection, filter_func, NULL); dbus_connection_unref (connection); _dbus_loop_unref (loop); loop = NULL; dbus_shutdown (); _dbus_verbose ("*** Test service exiting\n"); return 0; }
dbus_bool_t bus_setup_debug_client (DBusConnection *connection) { dbus_bool_t retval; if (!dbus_connection_add_filter (connection, client_disconnect_filter, NULL, NULL)) return FALSE; retval = FALSE; if (client_loop == NULL) { client_loop = _dbus_loop_new (); if (client_loop == NULL) goto out; } if (!dbus_connection_set_watch_functions (connection, add_client_watch, remove_client_watch, toggle_client_watch, connection, NULL)) goto out; if (!dbus_connection_set_timeout_functions (connection, add_client_timeout, remove_client_timeout, NULL, connection, NULL)) goto out; if (!_dbus_list_append (&clients, connection)) goto out; retval = TRUE; out: if (!retval) { dbus_connection_remove_filter (connection, client_disconnect_filter, NULL); dbus_connection_set_watch_functions (connection, NULL, NULL, NULL, NULL, NULL); dbus_connection_set_timeout_functions (connection, NULL, NULL, NULL, NULL, NULL); _dbus_list_remove_last (&clients, connection); if (clients == NULL) { _dbus_loop_unref (client_loop); client_loop = NULL; } } return retval; }