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