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>
34 #include "gdbus-sessionbus.h"
36 /* ---------------------------------------------------------------------------------------------------- */
37 /* Utilities for bringing up and tearing down session message bus instances */
40 watch_parent (gint fd)
46 GArray *buses_to_kill_array;
49 fds[0].events = G_IO_HUP | G_IO_IN;
52 buses_to_kill_array = g_array_new (FALSE, TRUE, sizeof (guint));
59 num_events = g_poll (fds, 1, -1);
63 if (fds[0].revents == G_IO_HUP)
65 for (n = 0; n < buses_to_kill_array->len; n++)
67 pid = g_array_index (buses_to_kill_array, guint, n);
68 g_print ("cleaning up bus with pid %d\n", pid);
71 g_array_free (buses_to_kill_array, TRUE);
75 //g_debug ("data from parent");
77 memset (buf, '\0', sizeof buf);
79 bytes_read = read (fds[0].fd, buf, sizeof buf);
80 if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
83 if (sscanf (buf, "add %d\n", &pid) == 1)
85 g_array_append_val (buses_to_kill_array, pid);
87 else if (sscanf (buf, "remove %d\n", &pid) == 1)
89 for (n = 0; n < buses_to_kill_array->len; n++)
91 if (g_array_index (buses_to_kill_array, guint, n) == pid)
93 g_array_remove_index (buses_to_kill_array, n);
100 g_warning ("unknown pid %d to remove", pid);
105 g_warning ("unknown command from parent '%s'", buf);
112 static GHashTable *session_bus_address_to_pid = NULL;
113 static gint pipe_fds[2];
116 session_bus_up_with_address (const gchar *given_address)
121 gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL};
125 gchar *config_file_name;
127 GString *config_file_contents;
131 config_file_name = NULL;
135 config_file_fd = g_file_open_tmp ("g-dbus-tests-XXXXXX",
138 if (config_file_fd < 0)
140 g_warning ("Error creating temporary config file: %s", error->message);
141 g_error_free (error);
145 config_file_contents = g_string_new (NULL);
146 g_string_append (config_file_contents, "<busconfig>\n");
147 g_string_append (config_file_contents, " <type>session</type>\n");
148 g_string_append_printf (config_file_contents, " <listen>%s</listen>\n", given_address);
149 g_string_append (config_file_contents,
150 " <policy context=\"default\">\n"
151 " <!-- Allow everything to be sent -->\n"
152 " <allow send_destination=\"*\" eavesdrop=\"true\"/>\n"
153 " <!-- Allow everything to be received -->\n"
154 " <allow eavesdrop=\"true\"/>\n"
155 " <!-- Allow anyone to own anything -->\n"
156 " <allow own=\"*\"/>\n"
158 g_string_append (config_file_contents, "</busconfig>\n");
160 if (write (config_file_fd, config_file_contents->str, config_file_contents->len) != (gssize) config_file_contents->len)
162 g_warning ("Error writing %d bytes to config file: %m", (gint) config_file_contents->len);
163 g_string_free (config_file_contents, TRUE);
166 g_string_free (config_file_contents, TRUE);
168 argv[2] = g_strdup_printf ("--config-file=%s", config_file_name);
170 if (session_bus_address_to_pid == NULL)
172 /* keep a mapping from session bus address to the pid */
173 session_bus_address_to_pid = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
175 /* fork a child to clean up session buses when we are killed */
176 if (pipe (pipe_fds) != 0)
178 g_warning ("pipe() failed: %m");
179 g_assert_not_reached ();
184 g_warning ("fork() failed: %m");
185 g_assert_not_reached ();
191 watch_parent (pipe_fds[0]);
200 //atexit (cleanup_session_buses);
201 /* TODO: need to handle the cases where we crash */
205 /* check if we already have a bus running for this address */
206 if (g_hash_table_lookup (session_bus_address_to_pid, given_address) != NULL)
208 g_warning ("Already have a bus instance for the given address %s", given_address);
213 if (!g_spawn_async_with_pipes (NULL,
225 g_warning ("Error spawning dbus-daemon: %s", error->message);
226 g_error_free (error);
230 memset (buf, '\0', sizeof buf);
232 bytes_read = read (stdout_fd, buf, sizeof buf);
233 if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
237 if (bytes_read == 0 || bytes_read == sizeof buf)
239 g_warning ("Error reading address from dbus daemon, %d bytes read", (gint) bytes_read);
244 address = g_strdup (buf);
245 g_strstrip (address);
247 /* write the pid to the child so it can kill it when we die */
248 g_snprintf (buf, sizeof buf, "add %d\n", (guint) pid);
249 write (pipe_fds[1], buf, strlen (buf));
251 /* start dbus-monitor */
252 if (g_getenv ("G_DBUS_MONITOR") != NULL)
254 g_spawn_command_line_async ("dbus-monitor --session", NULL);
258 g_hash_table_insert (session_bus_address_to_pid, address, GUINT_TO_POINTER (pid));
261 if (config_file_fd > 0)
263 if (close (config_file_fd) != 0)
265 g_warning ("Error closing fd for config file %s: %m", config_file_name);
267 g_assert (config_file_name != NULL);
268 if (unlink (config_file_name) != 0)
270 g_warning ("Error unlinking config file %s: %m", config_file_name);
274 g_free (config_file_name);
279 session_bus_down_with_address (const gchar *address)
285 g_assert (address != NULL);
286 g_assert (session_bus_address_to_pid != NULL);
288 value = g_hash_table_lookup (session_bus_address_to_pid, address);
289 g_assert (value != NULL);
291 pid = GPOINTER_TO_UINT (g_hash_table_lookup (session_bus_address_to_pid, address));
295 /* write the pid to the child so it won't kill it when we die */
296 g_snprintf (buf, sizeof buf, "remove %d\n", (guint) pid);
297 write (pipe_fds[1], buf, strlen (buf));
299 g_hash_table_remove (session_bus_address_to_pid, address);
302 static gchar *temporary_address = NULL;
303 static gchar *temporary_address_used_by_bus = NULL;
306 session_bus_get_temporary_address (void)
308 if (temporary_address == NULL)
310 temporary_address = g_strdup_printf ("unix:path=/tmp/g-dbus-tests-pid-%d", getpid ());
313 return temporary_address;
317 session_bus_up (void)
319 if (temporary_address_used_by_bus != NULL)
321 g_warning ("There is already a session bus up");
325 temporary_address_used_by_bus = g_strdup (session_bus_up_with_address (session_bus_get_temporary_address ()));
328 return temporary_address_used_by_bus;
332 session_bus_down (void)
334 if (temporary_address_used_by_bus == NULL)
336 g_warning ("There is not a session bus up");
340 session_bus_down_with_address (temporary_address_used_by_bus);
341 g_free (temporary_address_used_by_bus);
342 temporary_address_used_by_bus = NULL;