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>
31 #include "gdbus-sessionbus.h"
33 /* ---------------------------------------------------------------------------------------------------- */
34 /* Utilities for bringing up and tearing down session message bus instances */
37 watch_parent (gint fd)
43 GArray *buses_to_kill_array;
46 fds[0].events = G_IO_HUP | G_IO_IN;
49 buses_to_kill_array = g_array_new (FALSE, TRUE, sizeof (guint));
56 num_events = g_poll (fds, 1, -1);
60 if (fds[0].revents == G_IO_HUP)
62 for (n = 0; n < buses_to_kill_array->len; n++)
64 pid = g_array_index (buses_to_kill_array, guint, n);
65 g_print ("cleaning up bus with pid %d\n", pid);
68 g_array_free (buses_to_kill_array, TRUE);
72 //g_debug ("data from parent");
74 memset (buf, '\0', sizeof buf);
76 bytes_read = read (fds[0].fd, buf, sizeof buf);
77 if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
80 if (sscanf (buf, "add %d\n", &pid) == 1)
82 g_array_append_val (buses_to_kill_array, pid);
84 else if (sscanf (buf, "remove %d\n", &pid) == 1)
86 for (n = 0; n < buses_to_kill_array->len; n++)
88 if (g_array_index (buses_to_kill_array, guint, n) == pid)
90 g_array_remove_index (buses_to_kill_array, n);
97 g_warning ("unknown pid %d to remove", pid);
102 g_warning ("unknown command from parent '%s'", buf);
109 static GHashTable *session_bus_address_to_pid = NULL;
110 static gint pipe_fds[2];
113 session_bus_up_with_address (const gchar *given_address)
118 gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL};
122 gchar *config_file_name;
124 GString *config_file_contents;
128 config_file_name = NULL;
132 config_file_fd = g_file_open_tmp ("g-dbus-tests-XXXXXX",
135 if (config_file_fd < 0)
137 g_warning ("Error creating temporary config file: %s", error->message);
138 g_error_free (error);
142 config_file_contents = g_string_new (NULL);
143 g_string_append (config_file_contents, "<busconfig>\n");
144 g_string_append (config_file_contents, " <type>session</type>\n");
145 g_string_append_printf (config_file_contents, " <listen>%s</listen>\n", given_address);
146 g_string_append (config_file_contents,
147 " <policy context=\"default\">\n"
148 " <!-- Allow everything to be sent -->\n"
149 " <allow send_destination=\"*\" eavesdrop=\"true\"/>\n"
150 " <!-- Allow everything to be received -->\n"
151 " <allow eavesdrop=\"true\"/>\n"
152 " <!-- Allow anyone to own anything -->\n"
153 " <allow own=\"*\"/>\n"
155 g_string_append (config_file_contents, "</busconfig>\n");
157 if (write (config_file_fd, config_file_contents->str, config_file_contents->len) != (gssize) config_file_contents->len)
159 g_warning ("Error writing %d bytes to config file: %m", (gint) config_file_contents->len);
160 g_string_free (config_file_contents, TRUE);
163 g_string_free (config_file_contents, TRUE);
165 argv[2] = g_strdup_printf ("--config-file=%s", config_file_name);
167 if (session_bus_address_to_pid == NULL)
169 /* keep a mapping from session bus address to the pid */
170 session_bus_address_to_pid = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
172 /* fork a child to clean up session buses when we are killed */
173 if (pipe (pipe_fds) != 0)
175 g_warning ("pipe() failed: %m");
176 g_assert_not_reached ();
181 g_warning ("fork() failed: %m");
182 g_assert_not_reached ();
188 watch_parent (pipe_fds[0]);
197 //atexit (cleanup_session_buses);
198 /* TODO: need to handle the cases where we crash */
202 /* check if we already have a bus running for this address */
203 if (g_hash_table_lookup (session_bus_address_to_pid, given_address) != NULL)
205 g_warning ("Already have a bus instance for the given address %s", given_address);
210 if (!g_spawn_async_with_pipes (NULL,
222 g_warning ("Error spawning dbus-daemon: %s", error->message);
223 g_error_free (error);
227 memset (buf, '\0', sizeof buf);
229 bytes_read = read (stdout_fd, buf, sizeof buf);
230 if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
234 if (bytes_read == 0 || bytes_read == sizeof buf)
236 g_warning ("Error reading address from dbus daemon, %d bytes read", (gint) bytes_read);
241 address = g_strdup (buf);
242 g_strstrip (address);
244 /* write the pid to the child so it can kill it when we die */
245 g_snprintf (buf, sizeof buf, "add %d\n", (guint) pid);
246 write (pipe_fds[1], buf, strlen (buf));
248 /* start dbus-monitor */
249 if (g_getenv ("G_DBUS_MONITOR") != NULL)
251 g_spawn_command_line_async ("dbus-monitor --session", NULL);
255 g_hash_table_insert (session_bus_address_to_pid, address, GUINT_TO_POINTER (pid));
258 if (config_file_fd > 0)
260 if (close (config_file_fd) != 0)
262 g_warning ("Error closing fd for config file %s: %m", config_file_name);
264 g_assert (config_file_name != NULL);
265 if (unlink (config_file_name) != 0)
267 g_warning ("Error unlinking config file %s: %m", config_file_name);
271 g_free (config_file_name);
276 session_bus_down_with_address (const gchar *address)
282 g_assert (address != NULL);
283 g_assert (session_bus_address_to_pid != NULL);
285 value = g_hash_table_lookup (session_bus_address_to_pid, address);
286 g_assert (value != NULL);
288 pid = GPOINTER_TO_UINT (g_hash_table_lookup (session_bus_address_to_pid, address));
292 /* write the pid to the child so it won't kill it when we die */
293 g_snprintf (buf, sizeof buf, "remove %d\n", (guint) pid);
294 write (pipe_fds[1], buf, strlen (buf));
296 g_hash_table_remove (session_bus_address_to_pid, address);
299 static gchar *temporary_address = NULL;
300 static gchar *temporary_address_used_by_bus = NULL;
303 session_bus_get_temporary_address (void)
305 if (temporary_address == NULL)
307 /* TODO: maybe use a more random name etc etc */
308 temporary_address = g_strdup_printf ("unix:path=/tmp/g-dbus-tests-pid-%d", getpid ());
311 return temporary_address;
315 session_bus_up (void)
317 if (temporary_address_used_by_bus != NULL)
319 g_warning ("There is already a session bus up");
323 temporary_address_used_by_bus = g_strdup (session_bus_up_with_address (session_bus_get_temporary_address ()));
326 return temporary_address_used_by_bus;
330 session_bus_down (void)
332 if (temporary_address_used_by_bus == NULL)
334 g_warning ("There is not a session bus up");
338 session_bus_down_with_address (temporary_address_used_by_bus);
339 g_free (temporary_address_used_by_bus);
340 temporary_address_used_by_bus = NULL;