static void* no_bus_init_server (ServerData *sd) { DBusServer *server; DBusError error; dbus_error_init (&error); server = dbus_server_listen ("unix:tmpdir="DBUS_TEST_SOCKET_DIR, &error); if (server == NULL) { g_printerr ("Could not start server: %s\n", error.message); exit (1); } messages_address = dbus_server_get_address (server); dbus_server_set_new_connection_function (server, no_bus_new_connection_callback, sd, NULL); dbus_server_setup_with_g_main (server, NULL); return server; }
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; }
static void setup_connection (Fixture *f, gconstpointer data) { char *address; setup (f, data); dbus_server_set_new_connection_function (f->server, new_conn_cb, f, NULL); if (!test_server_setup (f->loop, f->server)) g_error ("failed to set up server"); address = dbus_server_get_address (f->server); g_assert (address != NULL); f->connection = dbus_connection_open_private (address, &f->e); assert_no_error (&f->e); g_assert (f->connection != NULL); dbus_free (address); if (!test_connection_setup (f->loop, f->connection)) g_error ("failed to set up connection"); while (f->server_connection == NULL) _dbus_loop_iterate (f->loop, TRUE); test_connection_shutdown (f->loop, f->connection); test_server_shutdown (f->loop, f->server); }
guint16 my_dbus_server_get_port (DBusServer* server) { DBusAddressEntry**entries; DBusError error; guint16 port = 0; char * address = dbus_server_get_address (server); int n_entries; int i; dbus_error_init (&error); if (!dbus_parse_address (address, &entries, &n_entries, &error)) { g_warning ("error parsing server address: %s", error.message); dbus_error_free (&error); free (address); exit (1); return 0; } for (i = 0; i < n_entries; i++) { if (!strcmp ("tcp", dbus_address_entry_get_method (entries[i]))) { port = atoi (dbus_address_entry_get_value (entries[i], "port")); break; } } dbus_address_entries_free (entries); free (address); return port; }
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; }
const gchar * ibus_server_get_address (IBusServer *server) { g_assert (IBUS_IS_SERVER (server)); gchar *address, *tmp; IBusServerPrivate *priv; priv = IBUS_SERVER_GET_PRIVATE (server); g_assert (priv->server != NULL); tmp = dbus_server_get_address (priv->server); address = g_strdup (tmp); dbus_free (tmp); return address; }
int main() { DBusServer* server; DBusConnection* connection; DBusError error; char* address; dbus_error_init(&error); server = dbus_server_listen("tcp:host=localhost,port=8085", &error); if(!server) { std_log(LOG_FILENAME_LINE, "Error :: %s\n%s",error.name,error.message); std_log(LOG_FILENAME_LINE, "Fail"); create_xml(1); return 1; } address = dbus_server_get_address(server); std_log(LOG_FILENAME_LINE, "Address :; %s",address); connection = dbus_connection_open_private("tcp:host=localhost,port=8085", &error); if(!CheckConnection(connection, &error)) return 1; dbus_connection_close(connection); dbus_connection_unref(connection); dbus_shutdown(); dbus_server_disconnect(server); std_log(LOG_FILENAME_LINE, "SUCCESS"); create_xml(0); return 0; }
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; }
/* * dbus_new_server * Set up a D-BUS server, integrate with the event loop * for handling file descriptor and timed events */ int sbus_new_server(TALLOC_CTX *mem_ctx, struct tevent_context *ev, const char *address, uid_t uid, gid_t gid, bool use_symlink, struct sbus_connection **_server, sbus_server_conn_init_fn init_fn, void *init_pvt_data) { struct sbus_connection *server; DBusServer *dbus_server; DBusError dbus_error; dbus_bool_t dbret; char *tmp; int ret, tmp_ret; char *filename; char *symlink_filename = NULL; const char *socket_address; struct stat stat_buf; TALLOC_CTX *tmp_ctx; *_server = NULL; tmp_ctx = talloc_new(NULL); if (!tmp_ctx) return ENOMEM; socket_address = get_socket_address(tmp_ctx, address, use_symlink); if (!socket_address) { ret = ENOMEM; goto done; } /* Set up D-BUS server */ dbus_error_init(&dbus_error); dbus_server = dbus_server_listen(socket_address, &dbus_error); if (!dbus_server) { DEBUG(SSSDBG_CRIT_FAILURE, "dbus_server_listen failed! (name=%s, message=%s)\n", dbus_error.name, dbus_error.message); if (dbus_error_is_set(&dbus_error)) dbus_error_free(&dbus_error); ret = EIO; goto done; } filename = strchr(socket_address, '/'); if (filename == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "Unexpected dbus address [%s].\n", socket_address); ret = EIO; goto done; } if (use_symlink) { symlink_filename = strchr(address, '/'); if (symlink_filename == NULL) { DEBUG(SSSDBG_CRIT_FAILURE, "Unexpected dbus address [%s].\n", address); ret = EIO; goto done; } ret = create_socket_symlink(filename, symlink_filename); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "Could not create symlink [%d]: %s\n", ret, strerror(ret)); ret = EIO; goto done; } } /* Both check_file and chmod can handle both the symlink and * the socket */ ret = check_file(filename, getuid(), getgid(), S_IFSOCK, S_IFMT, &stat_buf, true); if (ret != EOK) { DEBUG(SSSDBG_CRIT_FAILURE, "check_file failed for [%s].\n", filename); ret = EIO; goto done; } if ((stat_buf.st_mode & ~S_IFMT) != (S_IRUSR|S_IWUSR)) { ret = chmod(filename, (S_IRUSR|S_IWUSR)); if (ret != EOK) { ret = errno; DEBUG(SSSDBG_CRIT_FAILURE, "chmod failed for [%s]: [%d][%s].\n", filename, ret, sss_strerror(ret)); ret = EIO; goto done; } } if (stat_buf.st_uid != uid || stat_buf.st_gid != gid) { ret = chown(filename, uid, gid); if (ret != EOK) { ret = errno; DEBUG(SSSDBG_CRIT_FAILURE, "chown failed for [%s]: [%d][%s].\n", filename, ret, sss_strerror(ret)); ret = EIO; goto done; } } tmp = dbus_server_get_address(dbus_server); DEBUG(SSSDBG_TRACE_FUNC, "D-BUS Server listening on %s\n", tmp); free(tmp); server = talloc_zero(tmp_ctx, struct sbus_connection); if (!server) { ret = ENOMEM; goto done; } server->ev = ev; server->type = SBUS_SERVER; server->dbus.server = dbus_server; server->srv_init_fn = init_fn; server->srv_init_data = init_pvt_data; talloc_set_destructor((TALLOC_CTX *)server, sbus_server_destructor); if (use_symlink) { server->symlink = talloc_strdup(server, symlink_filename); if (!server->symlink) { ret = ENOMEM; goto done; } } /* Set up D-BUS new connection handler */ dbus_server_set_new_connection_function(server->dbus.server, sbus_server_init_new_connection, server, NULL); /* Set up DBusWatch functions */ dbret = dbus_server_set_watch_functions(server->dbus.server, sbus_add_watch, sbus_remove_watch, sbus_toggle_watch, server, NULL); if (!dbret) { DEBUG(SSSDBG_CONF_SETTINGS, "Error setting up D-BUS server watch functions\n"); ret = EIO; goto done; } /* Set up DBusTimeout functions */ dbret = dbus_server_set_timeout_functions(server->dbus.server, sbus_add_timeout, sbus_remove_timeout, sbus_toggle_timeout, server, NULL); if (!dbret) { DEBUG(SSSDBG_CONF_SETTINGS, "Error setting up D-BUS server timeout functions\n"); dbus_server_set_watch_functions(server->dbus.server, NULL, NULL, NULL, NULL, NULL); ret = EIO; goto done; } *_server = talloc_steal(mem_ctx, server); ret = EOK; done: if (ret != EOK && symlink_filename) { tmp_ret = unlink(symlink_filename); /* non-fatal failure */ if (tmp_ret != EOK) { tmp_ret = errno; DEBUG(SSSDBG_MINOR_FAILURE, "Failed to remove symbolic link '%s': %d [%s]!\n", symlink_filename, tmp_ret, sss_strerror(tmp_ret)); } } talloc_free(tmp_ctx); return ret; }
static int server_init(LassiServer *ls) { DBusError e; int r = -1; guint16 port; g_assert(ls); memset(ls, 0, sizeof(*ls)); dbus_error_init(&e); for (port = PORT_MIN; port < PORT_MAX; port++) { char *t; t = g_strdup_printf("tcp:port=%u,host=0.0.0.0", port); ls->dbus_server = dbus_server_listen(t, &e); g_free(t); if (ls->dbus_server) { ls->port = port; break; } if (!dbus_error_has_name(&e, DBUS_ERROR_ADDRESS_IN_USE)) { g_warning("Failed to create D-Bus server: %s %s", e.message, e.name); goto finish; } dbus_error_free(&e); } if (!ls->dbus_server) { g_warning("All ports blocked."); goto finish; } g_debug("Listening on port %u", port); dbus_server_setup_with_g_main(ls->dbus_server, NULL); dbus_server_set_new_connection_function(ls->dbus_server, new_connection, ls, NULL); ls->connections_by_id = g_hash_table_new(g_str_hash, g_str_equal); ls->id = g_strdup_printf(_("%s's desktop on %s"), g_get_user_name(), g_get_host_name()); if (lassi_avahi_init(&ls->avahi_info, ls) < 0) goto finish; /* The initialization of Avahi might have changed ls->id! */ ls->address = dbus_server_get_address(ls->dbus_server); ls->order = g_list_prepend(NULL, g_strdup(ls->id)); if (lassi_grab_init(&ls->grab_info, ls) < 0) goto finish; if (lassi_osd_init(&ls->osd_info) < 0) goto finish; if (lassi_tray_init(&ls->tray_info, ls) < 0) goto finish; if (lassi_clipboard_init(&ls->clipboard_info, ls) < 0) goto finish; if (lassi_prefs_init(&ls->prefs_info, ls) < 0) goto finish; r = 0; finish: dbus_error_free(&e); return r; }
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; }