static void teardown (Fixture *f, gconstpointer data) { if (f->server_connection != NULL) { dbus_connection_close (f->server_connection); dbus_connection_unref (f->server_connection); } if (f->connection != NULL) { dbus_connection_close (f->connection); dbus_connection_unref (f->connection); } if (f->server != NULL) { dbus_server_disconnect (f->server); dbus_server_unref (f->server); } dbus_connection_free_data_slot (&connection_slot); dbus_server_free_data_slot (&server_slot); dbus_message_free_data_slot (&message_slot); dbus_pending_call_free_data_slot (&pending_call_slot); _dbus_loop_unref (f->loop); dbus_error_free (&f->e); }
static void no_bus_stop_server (ServerData *sd, void *server) { dbus_server_disconnect (server); dbus_server_unref (server); }
static void server_done(LassiServer *ls) { g_assert(ls); if (ls->dbus_server) { dbus_server_disconnect(ls->dbus_server); dbus_server_unref(ls->dbus_server); } server_disconnect_all(ls, FALSE); if (ls->connections_by_id) g_hash_table_destroy(ls->connections_by_id); g_free(ls->id); g_free(ls->address); lassi_list_free(ls->order); lassi_grab_done(&ls->grab_info); lassi_osd_done(&ls->osd_info); lassi_clipboard_done(&ls->clipboard_info); lassi_avahi_done(&ls->avahi_info); lassi_tray_done(&ls->tray_info); lassi_prefs_done(&ls->prefs_info); memset(ls, 0, sizeof(*ls)); }
static void serverdata_free (void *data) { ServerData *sd = data; dbus_server_unref (sd->server); _dbus_loop_unref (sd->loop); dbus_free (sd); }
static void ibus_server_destroy (IBusServer *server) { IBusServerPrivate *priv; priv = IBUS_SERVER_GET_PRIVATE (server); if (priv->server) { dbus_server_unref (priv->server); priv->server = NULL; } IBUS_OBJECT_CLASS(ibus_server_parent_class)->destroy (IBUS_OBJECT (server)); }
/** * Releases the server's address and stops listening for * new clients. If called more than once, only the first * call has an effect. Does not modify the server's * reference count. * * @param server the server. */ void dbus_server_disconnect (DBusServer *server) { _dbus_return_if_fail (server != NULL); dbus_server_ref (server); SERVER_LOCK (server); _dbus_server_disconnect_unlocked (server); SERVER_UNLOCK (server); dbus_server_unref (server); }
dbus_bool_t _dbus_server_test (void) { const char *valid_addresses[] = { "tcp:port=1234", "tcp:host=localhost,port=1234", "tcp:host=localhost,port=1234;tcp:port=5678", #ifdef DBUS_UNIX "unix:path=./boogie", "tcp:port=1234;unix:path=./boogie", #endif }; DBusServer *server; int i; for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++) { DBusError error = DBUS_ERROR_INIT; char *address; char *id; server = dbus_server_listen (valid_addresses[i], &error); if (server == NULL) { _dbus_warn ("server listen error: %s: %s\n", error.name, error.message); dbus_error_free (&error); _dbus_assert_not_reached ("Failed to listen for valid address."); } id = dbus_server_get_id (server); _dbus_assert (id != NULL); address = dbus_server_get_address (server); _dbus_assert (address != NULL); if (strstr (address, id) == NULL) { _dbus_warn ("server id '%s' is not in the server address '%s'\n", id, address); _dbus_assert_not_reached ("bad server id or address"); } dbus_free (id); dbus_free (address); dbus_server_disconnect (server); dbus_server_unref (server); } return TRUE; }
/* * watch_handler * Callback for D-BUS to handle messages on a file-descriptor */ static void sbus_watch_handler(struct tevent_context *ev, struct tevent_fd *fde, uint16_t flags, void *data) { struct sbus_watch_ctx *watch = talloc_get_type(data, struct sbus_watch_ctx); enum dbus_conn_type type; union dbus_conn_pointer dbus_p; /* conn may get freed inside a handle, save the data we need for later */ type = watch->conn->type; dbus_p = watch->conn->dbus; /* Take a reference while handling watch */ if (type == SBUS_SERVER) { dbus_server_ref(dbus_p.server); } else { dbus_connection_ref(dbus_p.conn); } /* Fire if readable */ if (flags & TEVENT_FD_READ) { if (watch->dbus_read_watch) { dbus_watch_handle(watch->dbus_read_watch, DBUS_WATCH_READABLE); } } /* Fire if writeable */ if (flags & TEVENT_FD_WRITE) { if (watch->dbus_write_watch) { dbus_watch_handle(watch->dbus_write_watch, DBUS_WATCH_WRITABLE); } } /* Release reference once done */ if (type == SBUS_SERVER) { dbus_server_unref(dbus_p.server); } else { dbus_connection_unref(dbus_p.conn); } }
/** * Releases the server's address and stops listening for * new clients. If called more than once, only the first * call has an effect. Does not modify the server's * reference count. * * @param server the server. */ void dbus_server_disconnect (DBusServer *server) { _dbus_return_if_fail (server != NULL); dbus_server_ref (server); SERVER_LOCK (server); _dbus_assert (server->vtable->disconnect != NULL); if (!server->disconnected) { /* this has to be first so recursive calls to disconnect don't happen */ server->disconnected = TRUE; (* server->vtable->disconnect) (server); } SERVER_UNLOCK (server); dbus_server_unref (server); }
gboolean hald_runner_start_runner(void) { DBusServer *server = NULL; DBusError err; GError *error = NULL; GPid pid; char *argv[] = { NULL, NULL}; char *env[] = { NULL, NULL, NULL, NULL}; const char *hald_runner_path; char *server_addr; running_processes = g_hash_table_new (g_direct_hash, g_direct_equal); dbus_error_init(&err); server = dbus_server_listen(DBUS_SERVER_ADDRESS, &err); if (server == NULL) { HAL_ERROR (("Cannot create D-BUS server for the runner")); goto error; } dbus_server_setup_with_g_main(server, NULL); dbus_server_set_new_connection_function(server, handle_connection, NULL, NULL); argv[0] = "hald-runner"; server_addr = dbus_server_get_address (server); env[0] = g_strdup_printf("HALD_RUNNER_DBUS_ADDRESS=%s", server_addr); dbus_free (server_addr); hald_runner_path = g_getenv("HALD_RUNNER_PATH"); if (hald_runner_path != NULL) { env[1] = g_strdup_printf ("PATH=%s:" PACKAGE_LIBEXEC_DIR ":" PACKAGE_SCRIPT_DIR ":" PACKAGE_BIN_DIR, hald_runner_path); } else { env[1] = g_strdup_printf ("PATH=" PACKAGE_LIBEXEC_DIR ":" PACKAGE_SCRIPT_DIR ":" PACKAGE_BIN_DIR); } /*env[2] = "DBUS_VERBOSE=1";*/ if (!g_spawn_async(NULL, argv, env, G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH, NULL, NULL, &pid, &error)) { HAL_ERROR (("Could not spawn runner : '%s'", error->message)); g_error_free (error); goto error; } g_free(env[0]); g_free(env[1]); HAL_INFO (("Runner has pid %d", pid)); g_child_watch_add(pid, runner_died, NULL); while (runner_connection == NULL) { /* Wait for the runner */ g_main_context_iteration(NULL, TRUE); } return TRUE; error: if (server != NULL) dbus_server_unref(server); return FALSE; }
EXPORT_C dbus_bool_t _dbus_server_test (void) { const char *valid_addresses[] = { "tcp:port=1234", // #ifndef __SYMBIAN32__ "unix:path=./boogie", // #endif "tcp:host=localhost,port=1234", "tcp:host=localhost,port=1234;tcp:port=5678", "tcp:port=1234;unix:path=./boogie", }; DBusServer *server; int i; for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++) { DBusError error; /* FIXME um, how are the two tests here different? */ dbus_error_init (&error); server = dbus_server_listen (valid_addresses[i], &error); if (server == NULL) { _dbus_warn ("server listen error: %s: %s\n", error.name, error.message); dbus_error_free (&error); _dbus_assert_not_reached ("Failed to listen for valid address."); } #ifdef __SYMBIAN32__ if(server!=NULL) { dbus_server_disconnect (server); dbus_server_unref (server); } #else dbus_server_disconnect (server); dbus_server_unref (server); #endif /* Try disconnecting before unreffing */ server = dbus_server_listen (valid_addresses[i], &error); if (server == NULL) { _dbus_warn ("server listen error: %s: %s\n", error.name, error.message); dbus_error_free (&error); _dbus_assert_not_reached ("Failed to listen for valid address."); } #ifdef __SYMBIAN32__ if(server!=NULL) { dbus_server_disconnect (server); dbus_server_unref (server); } #else dbus_server_disconnect (server); dbus_server_unref (server); #endif } return TRUE; }
static void test_server (Fixture *f, gconstpointer data) { Thread public_api = { f, f->server, (RefFunc) dbus_server_ref, NULL, (VoidFunc) dbus_server_unref, NULL, NULL, NULL }; Thread internal_api = { f, f->server, NULL, (VoidFunc) _dbus_server_ref_unlocked, (VoidFunc) _dbus_server_unref_unlocked, f->server, server_lock, server_unlock }; unsigned i; if (!dbus_server_set_data (f->server, server_slot, f, last_unref)) g_error ("OOM"); for (i = 0; i < f->n_threads; i++) { if ((i % 2) == 0) f->threads[i] = g_thread_new (NULL, ref_thread, &public_api); else f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api); g_assert (f->threads[i] != NULL); } wait_for_all_threads (f); for (i = 0; i < f->n_threads; i++) { if ((i % 2) == 0) f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api); else f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api); g_assert (f->threads[i] != NULL); } wait_for_all_threads (f); for (i = 0; i < f->n_threads; i++) { if ((i % 2) == 0) f->threads[i] = g_thread_new (NULL, unref_thread, &public_api); else f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api); g_assert (f->threads[i] != NULL); } wait_for_all_threads (f); /* Destroy the server. This should be the last-unref. */ g_assert (!f->last_unref); dbus_server_disconnect (f->server); dbus_server_unref (f->server); f->server = NULL; g_assert (f->last_unref); }
/* Return value is just for memory, not other failures. */ static dbus_bool_t handle_new_client_fd_and_unlock (DBusServer *server, int client_fd) { DBusConnection *connection; DBusTransport *transport; DBusNewConnectionFunction new_connection_function; DBusServerSocket* socket_server; void *new_connection_data; socket_server = (DBusServerSocket*)server; _dbus_verbose ("Creating new client connection with fd %d\n", client_fd); HAVE_LOCK_CHECK (server); if (!_dbus_set_fd_nonblocking (client_fd, NULL)) { SERVER_UNLOCK (server); return TRUE; } transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, FALSE); if (transport == NULL) { _dbus_close_socket (client_fd, NULL); SERVER_UNLOCK (server); return FALSE; } if (!_dbus_transport_set_auth_mechanisms (transport, (const char **) server->auth_mechanisms)) { _dbus_transport_unref (transport); SERVER_UNLOCK (server); return FALSE; } /* note that client_fd is now owned by the transport, and will be * closed on transport disconnection/finalization */ connection = _dbus_connection_new_for_transport (transport); _dbus_transport_unref (transport); transport = NULL; /* now under the connection lock */ if (connection == NULL) { SERVER_UNLOCK (server); return FALSE; } /* See if someone wants to handle this new connection, self-referencing * for paranoia. */ new_connection_function = server->new_connection_function; new_connection_data = server->new_connection_data; _dbus_server_ref_unlocked (server); SERVER_UNLOCK (server); if (new_connection_function) { (* new_connection_function) (server, connection, new_connection_data); } dbus_server_unref (server); /* If no one grabbed a reference, the connection will die. */ _dbus_connection_close_if_only_one_ref (connection); dbus_connection_unref (connection); return TRUE; }
/** * Creates the client-side transport for * a debug-pipe connection connected to the * given debug-pipe server name. * * @param server_name name of server to connect to * @param error address where an error can be returned. * @returns #NULL on no memory or transport */ DBusTransport* _dbus_transport_debug_pipe_new (const char *server_name, DBusError *error) { DBusTransport *client_transport; DBusTransport *server_transport; DBusConnection *connection; int client_fd, server_fd; DBusServer *server; DBusString address; _DBUS_ASSERT_ERROR_IS_CLEAR (error); if (server_pipe_hash == NULL) { dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL); return NULL; } server = _dbus_hash_table_lookup_string (server_pipe_hash, server_name); if (server == NULL || ((DBusServerDebugPipe*)server)->disconnected) { dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL); return NULL; } if (!_dbus_string_init (&address)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } if (!_dbus_string_append (&address, "debug-pipe:name=") || !_dbus_string_append (&address, server_name)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (&address); return NULL; } if (!_dbus_full_duplex_pipe (&client_fd, &server_fd, FALSE, NULL)) { _dbus_verbose ("failed to create full duplex pipe\n"); dbus_set_error (error, DBUS_ERROR_FAILED, "Could not create full-duplex pipe"); _dbus_string_free (&address); return NULL; } _dbus_fd_set_close_on_exec (client_fd); _dbus_fd_set_close_on_exec (server_fd); client_transport = _dbus_transport_new_for_socket (client_fd, NULL, &address); if (client_transport == NULL) { _dbus_close_socket (client_fd, NULL); _dbus_close_socket (server_fd, NULL); dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_string_free (&address); return NULL; } _dbus_string_free (&address); client_fd = -1; server_transport = _dbus_transport_new_for_socket (server_fd, &server->guid_hex, NULL); if (server_transport == NULL) { _dbus_transport_unref (client_transport); _dbus_close_socket (server_fd, NULL); dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } server_fd = -1; if (!_dbus_transport_set_auth_mechanisms (server_transport, (const char**) server->auth_mechanisms)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); _dbus_transport_unref (server_transport); _dbus_transport_unref (client_transport); return NULL; } connection = _dbus_connection_new_for_transport (server_transport); _dbus_transport_unref (server_transport); server_transport = NULL; if (connection == NULL) { _dbus_transport_unref (client_transport); dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return NULL; } /* See if someone wants to handle this new connection, * self-referencing for paranoia */ if (server->new_connection_function) { dbus_server_ref (server); (* server->new_connection_function) (server, connection, server->new_connection_data); dbus_server_unref (server); } /* If no one grabbed a reference, the connection will die, * and the client transport will get an immediate disconnect */ _dbus_connection_close_if_only_one_ref (connection); dbus_connection_unref (connection); return client_transport; }
int main (int argc, char**argv) { DBusServer* server; GMainLoop * loop; DBusError derror; guint16 port; GError * error = NULL; gchar * command; g_type_init (); dbus_error_init (&derror); server = dbus_server_listen("tcp:bind=*", &derror); if (dbus_error_is_set (&derror)) { g_warning ("error setting up peer-to-peer server: %s", derror.message); dbus_error_free (&derror); return 1; } dbus_server_set_new_connection_function(server, &new_connection_cb, NULL, NULL); port = my_dbus_server_get_port (server); command = g_strdup_printf ("./client %s %d", "localhost", port); if (argc > 1) { g_print ("starting \"%s\" twice:\n", command); if (!g_spawn_command_line_async (command, &error)) { g_warning ("error spawning child: %s", error->message); g_clear_error (&error); return 1; } if (!g_spawn_command_line_async (command, &error)) { g_warning ("error spawning child: %s", error->message); g_clear_error (&error); return 1; } } else { g_print ("start the clients like this: \"%s\" (replace \"localhost\" with the IP address)\n", command); } g_free (command); dbus_server_setup_with_g_main (server, NULL); loop = g_main_loop_new (NULL, FALSE); g_timeout_add (argc > 1 ? 1000 : 60 * 1000, (GSourceFunc)g_main_loop_quit, loop); g_main_loop_run (loop); g_main_loop_unref (loop); dbus_server_unref (server); return 0; }
int main (int argc, char **argv) { ServiceData svc = {0,}; DBusError dbus_error = {0,}; const char *address = NULL; #ifdef ENABLE_NLS /* initialize i18n */ bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); #endif g_type_init (); gst_init (NULL, NULL); g_set_prgname ("rhythmbox-metadata"); if (argv[1] != NULL && strcmp(argv[1], "--debug") == 0) { argv++; rb_debug_init (TRUE); } else if (argv[1] != NULL && strcmp (argv[1], "--debug-match") == 0) { rb_debug_init_match (argv[2]); argv += 2; } else { rb_debug_init (FALSE); } /* bug report modes */ if (argv[1] != NULL && strcmp(argv[1], "--load") == 0) { return test_load (argv[2]); } if (argv[1] != NULL && strcmp(argv[1], "--saveable-types") == 0) { return test_saveable_types (); } if (argv[1] != NULL && strcmp (argv[1], "--external") == 0) { argv++; svc.external = TRUE; } if (argv[1] == NULL) { address = "unix:tmpdir=/tmp"; } else { address = argv[1]; } rb_debug ("initializing metadata service; pid = %d; address = %s", getpid (), address); svc.metadata = rb_metadata_new (); /* set up D-BUS server */ svc.server = dbus_server_listen (address, &dbus_error); if (!svc.server) { rb_debug ("D-BUS server init failed: %s", dbus_error.message); return -1; } dbus_server_set_new_connection_function (svc.server, _new_connection, (gpointer) &svc, NULL); /* write the server address back to the parent process */ { char *addr; addr = dbus_server_get_address (svc.server); rb_debug ("D-BUS server listening on address %s", addr); printf ("%s\n", addr); fflush (stdout); free (addr); } /* run main loop until we get bored */ svc.loop = g_main_loop_new (NULL, TRUE); dbus_server_setup_with_g_main (svc.server, g_main_loop_get_context (svc.loop)); if (!svc.external) g_timeout_add_seconds (ATTENTION_SPAN / 2, (GSourceFunc) electromagnetic_shotgun, &svc); g_main_loop_run (svc.loop); if (svc.connection) { dbus_connection_close (svc.connection); dbus_connection_unref (svc.connection); } g_object_unref (svc.metadata); g_main_loop_unref (svc.loop); dbus_server_disconnect (svc.server); dbus_server_unref (svc.server); gst_deinit (); return 0; }