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