Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / tests / gdbus-sessionbus.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <signal.h>
29 #include <stdio.h>
30
31 #include "gdbus-sessionbus.h"
32
33 /* ---------------------------------------------------------------------------------------------------- */
34 /* Utilities for bringing up and tearing down session message bus instances */
35
36 static void
37 watch_parent (gint fd)
38 {
39   GPollFD fds[1];
40   gint num_events;
41   gchar buf[512];
42   guint bytes_read;
43   GArray *buses_to_kill_array;
44
45   fds[0].fd = fd;
46   fds[0].events = G_IO_HUP | G_IO_IN;
47   fds[0].revents = 0;
48
49   buses_to_kill_array = g_array_new (FALSE, TRUE, sizeof (guint));
50
51   do
52     {
53       guint pid;
54       guint n;
55
56       num_events = g_poll (fds, 1, -1);
57       if (num_events == 0)
58         continue;
59
60       if (fds[0].revents == G_IO_HUP)
61         {
62           for (n = 0; n < buses_to_kill_array->len; n++)
63             {
64               pid = g_array_index (buses_to_kill_array, guint, n);
65               g_print ("cleaning up bus with pid %d\n", pid);
66               kill (pid, SIGTERM);
67             }
68           g_array_free (buses_to_kill_array, TRUE);
69           exit (0);
70         }
71
72       //g_debug ("data from parent");
73
74       memset (buf, '\0', sizeof buf);
75     again:
76       bytes_read = read (fds[0].fd, buf, sizeof buf);
77       if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
78         goto again;
79
80       if (sscanf (buf, "add %d\n", &pid) == 1)
81         {
82           g_array_append_val (buses_to_kill_array, pid);
83         }
84       else if (sscanf (buf, "remove %d\n", &pid) == 1)
85         {
86           for (n = 0; n < buses_to_kill_array->len; n++)
87             {
88               if (g_array_index (buses_to_kill_array, guint, n) == pid)
89                 {
90                   g_array_remove_index (buses_to_kill_array, n);
91                   pid = 0;
92                   break;
93                 }
94             }
95           if (pid != 0)
96             {
97               g_warning ("unknown pid %d to remove", pid);
98             }
99         }
100       else
101         {
102           g_warning ("unknown command from parent '%s'", buf);
103         }
104     }
105   while (TRUE);
106
107 }
108
109 static GHashTable *session_bus_address_to_pid = NULL;
110 static gint pipe_fds[2];
111
112 const gchar *
113 session_bus_up_with_address (const gchar *given_address)
114 {
115   gchar *address;
116   int stdout_fd;
117   GError *error;
118   gchar *argv[] = {"dbus-daemon", "--print-address", "--config-file=foo", NULL};
119   GPid pid;
120   gchar buf[512];
121   ssize_t bytes_read;
122   gchar *config_file_name;
123   gint config_file_fd;
124   GString *config_file_contents;
125
126   address = NULL;
127   error = NULL;
128   config_file_name = NULL;
129   config_file_fd = -1;
130   argv[2] = NULL;
131
132   config_file_fd = g_file_open_tmp ("g-dbus-tests-XXXXXX",
133                                     &config_file_name,
134                                     &error);
135   if (config_file_fd < 0)
136     {
137       g_warning ("Error creating temporary config file: %s", error->message);
138       g_error_free (error);
139       goto out;
140     }
141
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"
154                           "  </policy>\n");
155   g_string_append        (config_file_contents, "</busconfig>\n");
156
157   if (write (config_file_fd, config_file_contents->str, config_file_contents->len) != (gssize) config_file_contents->len)
158     {
159       g_warning ("Error writing %d bytes to config file: %m", (gint) config_file_contents->len);
160       g_string_free (config_file_contents, TRUE);
161       goto out;
162     }
163   g_string_free (config_file_contents, TRUE);
164
165   argv[2] = g_strdup_printf ("--config-file=%s", config_file_name);
166
167   if (session_bus_address_to_pid == NULL)
168     {
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);
171
172       /* fork a child to clean up session buses when we are killed */
173       if (pipe (pipe_fds) != 0)
174         {
175           g_warning ("pipe() failed: %m");
176           g_assert_not_reached ();
177         }
178       switch (fork ())
179         {
180         case -1:
181           g_warning ("fork() failed: %m");
182           g_assert_not_reached ();
183           break;
184
185         case 0:
186           /* child */
187           close (pipe_fds[1]);
188           watch_parent (pipe_fds[0]);
189           break;
190
191         default:
192           /* parent */
193           close (pipe_fds[0]);
194           break;
195         }
196
197       //atexit (cleanup_session_buses);
198       /* TODO: need to handle the cases where we crash */
199     }
200   else
201     {
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)
204         {
205           g_warning ("Already have a bus instance for the given address %s", given_address);
206           goto out;
207         }
208     }
209
210   if (!g_spawn_async_with_pipes (NULL,
211                                  argv,
212                                  NULL,
213                                  G_SPAWN_SEARCH_PATH,
214                                  NULL,
215                                  NULL,
216                                  &pid,
217                                  NULL,
218                                  &stdout_fd,
219                                  NULL,
220                                  &error))
221     {
222       g_warning ("Error spawning dbus-daemon: %s", error->message);
223       g_error_free (error);
224       goto out;
225     }
226
227   memset (buf, '\0', sizeof buf);
228  again:
229   bytes_read = read (stdout_fd, buf, sizeof buf);
230   if (bytes_read < 0 && (errno == EAGAIN || errno == EINTR))
231     goto again;
232   close (stdout_fd);
233
234   if (bytes_read == 0 || bytes_read == sizeof buf)
235     {
236       g_warning ("Error reading address from dbus daemon, %d bytes read", (gint) bytes_read);
237       kill (SIGTERM, pid);
238       goto out;
239     }
240
241   address = g_strdup (buf);
242   g_strstrip (address);
243
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));
247
248   /* start dbus-monitor */
249   if (g_getenv ("G_DBUS_MONITOR") != NULL)
250     {
251       g_spawn_command_line_async ("dbus-monitor --session", NULL);
252       usleep (500 * 1000);
253     }
254
255   g_hash_table_insert (session_bus_address_to_pid, address, GUINT_TO_POINTER (pid));
256
257  out:
258   if (config_file_fd > 0)
259     {
260       if (close (config_file_fd) != 0)
261         {
262           g_warning ("Error closing fd for config file %s: %m", config_file_name);
263         }
264       g_assert (config_file_name != NULL);
265       if (unlink (config_file_name) != 0)
266         {
267           g_warning ("Error unlinking config file %s: %m", config_file_name);
268         }
269     }
270   g_free (argv[2]);
271   g_free (config_file_name);
272   return address;
273 }
274
275 void
276 session_bus_down_with_address (const gchar *address)
277 {
278   gpointer value;
279   GPid pid;
280   gchar buf[512];
281
282   g_assert (address != NULL);
283   g_assert (session_bus_address_to_pid != NULL);
284
285   value = g_hash_table_lookup (session_bus_address_to_pid, address);
286   g_assert (value != NULL);
287
288   pid = GPOINTER_TO_UINT (g_hash_table_lookup (session_bus_address_to_pid, address));
289
290   kill (pid, SIGTERM);
291
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));
295
296   g_hash_table_remove (session_bus_address_to_pid, address);
297 }
298
299 static gchar *temporary_address = NULL;
300 static gchar *temporary_address_used_by_bus = NULL;
301
302 const gchar *
303 session_bus_get_temporary_address (void)
304 {
305   if (temporary_address == NULL)
306     {
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 ());
309     }
310
311   return temporary_address;
312 }
313
314 const gchar *
315 session_bus_up (void)
316 {
317   if (temporary_address_used_by_bus != NULL)
318     {
319       g_warning ("There is already a session bus up");
320       goto out;
321     }
322
323   temporary_address_used_by_bus = g_strdup (session_bus_up_with_address (session_bus_get_temporary_address ()));
324
325  out:
326   return temporary_address_used_by_bus;
327 }
328
329 void
330 session_bus_down (void)
331 {
332   if (temporary_address_used_by_bus == NULL)
333     {
334       g_warning ("There is not a session bus up");
335     }
336   else
337     {
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;
341     }
342 }