- gchar *address;
- int stdout_fd;
- GError *error;
- gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL};
- GPid pid;
- gchar buf[512];
- ssize_t bytes_read;
- gchar *config_file_name;
- gint config_file_fd;
- GString *config_file_contents;
-
- address = NULL;
- error = NULL;
- config_file_name = NULL;
- config_file_fd = -1;
- argv[2] = NULL;
-
- config_file_fd = g_file_open_tmp ("g-dbus-tests-XXXXXX",
- &config_file_name,
- &error);
- if (config_file_fd < 0)
- {
- g_warning ("Error creating temporary config file: %s", error->message);
- g_error_free (error);
- goto out;
- }
-
- config_file_contents = g_string_new (NULL);
- g_string_append (config_file_contents, "<busconfig>\n");
- g_string_append (config_file_contents, " <type>session</type>\n");
- g_string_append_printf (config_file_contents, " <listen>%s</listen>\n", given_address);
- g_string_append (config_file_contents,
- " <policy context=\"default\">\n"
- " <!-- Allow everything to be sent -->\n"
- " <allow send_destination=\"*\" eavesdrop=\"true\"/>\n"
- " <!-- Allow everything to be received -->\n"
- " <allow eavesdrop=\"true\"/>\n"
- " <!-- Allow anyone to own anything -->\n"
- " <allow own=\"*\"/>\n"
- " </policy>\n");
- g_string_append (config_file_contents, "</busconfig>\n");
-
- if (write (config_file_fd, config_file_contents->str, config_file_contents->len) != (gssize) config_file_contents->len)
- {
- g_warning ("Error writing %d bytes to config file: %m", (gint) config_file_contents->len);
- g_string_free (config_file_contents, TRUE);
- goto out;
- }
- g_string_free (config_file_contents, TRUE);
-
- argv[2] = g_strdup_printf ("--config-file=%s", config_file_name);
-
- if (session_bus_address_to_pid == NULL)
- {
- /* keep a mapping from session bus address to the pid */
- session_bus_address_to_pid = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
-
- /* fork a child to clean up session buses when we are killed */
- if (pipe (pipe_fds) != 0)
- {
- g_warning ("pipe() failed: %m");
- g_assert_not_reached ();
- }
- switch (fork ())
- {
- case -1:
- g_warning ("fork() failed: %m");
- g_assert_not_reached ();
- break;
-
- case 0:
- /* child */
- close (pipe_fds[1]);
- watch_parent (pipe_fds[0]);
- break;
-
- default:
- /* parent */
- close (pipe_fds[0]);
- break;
- }
-
- //atexit (cleanup_session_buses);
- /* TODO: need to handle the cases where we crash */
- }
- else
- {
- /* check if we already have a bus running for this address */
- if (g_hash_table_lookup (session_bus_address_to_pid, given_address) != NULL)
- {
- g_warning ("Already have a bus instance for the given address %s", given_address);
- goto out;
- }
- }
-
- if (!g_spawn_async_with_pipes (NULL,
- argv,
- NULL,
- G_SPAWN_SEARCH_PATH,
- NULL,
- NULL,
- &pid,
- NULL,
- &stdout_fd,
- NULL,
- &error))
- {
- g_warning ("Error spawning dbus-daemon: %s", error->message);
- g_error_free (error);
- goto out;
- }
-
- memset (buf, '\0', sizeof buf);
- again:
- bytes_read = read (stdout_fd, buf, sizeof buf);
- if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
- goto again;
- close (stdout_fd);
-
- if (bytes_read == 0 || bytes_read == sizeof buf)
- {
- g_warning ("Error reading address from dbus daemon, %d bytes read", (gint) bytes_read);
- kill (SIGTERM, pid);
- goto out;
- }
-
- address = g_strdup (buf);
- g_strstrip (address);
-
- /* write the pid to the child so it can kill it when we die */
- g_snprintf (buf, sizeof buf, "add %d\n", (guint) pid);
- write (pipe_fds[1], buf, strlen (buf));
-
- /* start dbus-monitor */
- if (g_getenv ("G_DBUS_MONITOR") != NULL)
- {
- g_spawn_command_line_async ("dbus-monitor --session", NULL);
- usleep (500 * 1000);
- }
-
- g_hash_table_insert (session_bus_address_to_pid, address, GUINT_TO_POINTER (pid));
-
- out:
- if (config_file_fd > 0)
- {
- if (close (config_file_fd) != 0)
- {
- g_warning ("Error closing fd for config file %s: %m", config_file_name);
- }
- g_assert (config_file_name != NULL);
- if (unlink (config_file_name) != 0)
- {
- g_warning ("Error unlinking config file %s: %m", config_file_name);
- }
- }
- g_free (argv[2]);
- g_free (config_file_name);
- return address;