1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
27 #include <sys/types.h>
33 #include "gdbus-sessionbus.h"
35 /* ---------------------------------------------------------------------------------------------------- */
36 /* Utilities for bringing up and tearing down session message bus instances */
39 watch_parent (gint fd)
45 GArray *buses_to_kill_array;
48 fds[0].events = G_IO_HUP | G_IO_IN;
51 buses_to_kill_array = g_array_new (FALSE, TRUE, sizeof (guint));
58 num_events = g_poll (fds, 1, -1);
62 if (fds[0].revents == G_IO_HUP)
64 for (n = 0; n < buses_to_kill_array->len; n++)
66 pid = g_array_index (buses_to_kill_array, guint, n);
67 g_print ("cleaning up bus with pid %d\n", pid);
70 g_array_free (buses_to_kill_array, TRUE);
74 //g_debug ("data from parent");
76 memset (buf, '\0', sizeof buf);
78 bytes_read = read (fds[0].fd, buf, sizeof buf);
79 if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
82 if (sscanf (buf, "add %d\n", &pid) == 1)
84 g_array_append_val (buses_to_kill_array, pid);
86 else if (sscanf (buf, "remove %d\n", &pid) == 1)
88 for (n = 0; n < buses_to_kill_array->len; n++)
90 if (g_array_index (buses_to_kill_array, guint, n) == pid)
92 g_array_remove_index (buses_to_kill_array, n);
99 g_warning ("unknown pid %d to remove", pid);
104 g_warning ("unknown command from parent '%s'", buf);
111 static GHashTable *session_bus_address_to_pid = NULL;
112 static gint pipe_fds[2];
115 session_bus_up_with_address (const gchar *given_address)
120 gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL};
124 gchar *config_file_name;
126 GString *config_file_contents;
130 config_file_name = NULL;
134 config_file_fd = g_file_open_tmp ("g-dbus-tests-XXXXXX",
137 if (config_file_fd < 0)
139 g_warning ("Error creating temporary config file: %s", error->message);
140 g_error_free (error);
144 config_file_contents = g_string_new (NULL);
145 g_string_append (config_file_contents, "<busconfig>\n");
146 g_string_append (config_file_contents, " <type>session</type>\n");
147 g_string_append_printf (config_file_contents, " <listen>%s</listen>\n", given_address);
148 g_string_append (config_file_contents,
149 " <policy context=\"default\">\n"
150 " <!-- Allow everything to be sent -->\n"
151 " <allow send_destination=\"*\" eavesdrop=\"true\"/>\n"
152 " <!-- Allow everything to be received -->\n"
153 " <allow eavesdrop=\"true\"/>\n"
154 " <!-- Allow anyone to own anything -->\n"
155 " <allow own=\"*\"/>\n"
157 g_string_append (config_file_contents, "</busconfig>\n");
159 if (write (config_file_fd, config_file_contents->str, config_file_contents->len) != (gssize) config_file_contents->len)
161 g_warning ("Error writing %d bytes to config file: %m", (gint) config_file_contents->len);
162 g_string_free (config_file_contents, TRUE);
165 g_string_free (config_file_contents, TRUE);
167 argv[2] = g_strdup_printf ("--config-file=%s", config_file_name);
169 if (session_bus_address_to_pid == NULL)
171 /* keep a mapping from session bus address to the pid */
172 session_bus_address_to_pid = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
174 /* fork a child to clean up session buses when we are killed */
175 if (pipe (pipe_fds) != 0)
177 g_warning ("pipe() failed: %m");
178 g_assert_not_reached ();
183 g_warning ("fork() failed: %m");
184 g_assert_not_reached ();
190 watch_parent (pipe_fds[0]);
199 //atexit (cleanup_session_buses);
200 /* TODO: need to handle the cases where we crash */
204 /* check if we already have a bus running for this address */
205 if (g_hash_table_lookup (session_bus_address_to_pid, given_address) != NULL)
207 g_warning ("Already have a bus instance for the given address %s", given_address);
212 if (!g_spawn_async_with_pipes (NULL,
224 g_warning ("Error spawning dbus-daemon: %s", error->message);
225 g_error_free (error);
229 memset (buf, '\0', sizeof buf);
231 bytes_read = read (stdout_fd, buf, sizeof buf);
232 if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
236 if (bytes_read == 0 || bytes_read == sizeof buf)
238 g_warning ("Error reading address from dbus daemon, %d bytes read", (gint) bytes_read);
243 address = g_strdup (buf);
244 g_strstrip (address);
246 /* write the pid to the child so it can kill it when we die */
247 g_snprintf (buf, sizeof buf, "add %d\n", (guint) pid);
248 write (pipe_fds[1], buf, strlen (buf));
250 /* start dbus-monitor */
251 if (g_getenv ("G_DBUS_MONITOR") != NULL)
253 g_spawn_command_line_async ("dbus-monitor --session", NULL);
257 g_hash_table_insert (session_bus_address_to_pid, address, GUINT_TO_POINTER (pid));
260 if (config_file_fd > 0)
262 if (close (config_file_fd) != 0)
264 g_warning ("Error closing fd for config file %s: %m", config_file_name);
266 g_assert (config_file_name != NULL);
267 if (unlink (config_file_name) != 0)
269 g_warning ("Error unlinking config file %s: %m", config_file_name);
273 g_free (config_file_name);
278 session_bus_down_with_address (const gchar *address)
284 g_assert (address != NULL);
285 g_assert (session_bus_address_to_pid != NULL);
287 value = g_hash_table_lookup (session_bus_address_to_pid, address);
288 g_assert (value != NULL);
290 pid = GPOINTER_TO_UINT (g_hash_table_lookup (session_bus_address_to_pid, address));
294 /* write the pid to the child so it won't kill it when we die */
295 g_snprintf (buf, sizeof buf, "remove %d\n", (guint) pid);
296 write (pipe_fds[1], buf, strlen (buf));
298 g_hash_table_remove (session_bus_address_to_pid, address);
301 static gchar *temporary_address = NULL;
302 static gchar *temporary_address_used_by_bus = NULL;
305 session_bus_get_temporary_address (void)
307 if (temporary_address == NULL)
310 guid = g_dbus_generate_guid ();
311 temporary_address = g_strdup_printf ("unix:abstract=/tmp/g-dbus-tests-pid-%d-uuid-%s", getpid (), guid);
315 return temporary_address;
319 session_bus_up (void)
321 if (temporary_address_used_by_bus != NULL)
323 g_warning ("There is already a session bus up");
327 temporary_address_used_by_bus = g_strdup (session_bus_up_with_address (session_bus_get_temporary_address ()));
330 return temporary_address_used_by_bus;
334 session_bus_down (void)
336 if (temporary_address_used_by_bus == NULL)
338 g_warning ("There is not a session bus up");
342 session_bus_down_with_address (temporary_address_used_by_bus);
343 g_free (temporary_address_used_by_bus);
344 temporary_address_used_by_bus = NULL;