Always remove, invalidate and free watches before closing watched sockets
[platform/upstream/dbus.git] / bus / main.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* main.c  main() for message bus
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <config.h>
25 #include "bus.h"
26 #include "driver.h"
27 #include <dbus/dbus-internals.h>
28 #include <dbus/dbus-watch.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #ifdef HAVE_SIGNAL_H
33 #include <signal.h>
34 #endif
35 #ifdef HAVE_ERRNO_H
36 #include <errno.h>
37 #endif
38 #include "selinux.h"
39
40 static BusContext *context;
41
42 static int reload_pipe[2];
43 #define RELOAD_READ_END 0
44 #define RELOAD_WRITE_END 1
45
46 static void close_reload_pipe (DBusWatch **);
47 static void close_reload_pipe_write (void);
48
49 static void
50 signal_handler (int sig)
51 {
52
53   switch (sig)
54     {
55 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
56     case SIGIO:
57       /* explicit fall-through */
58 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX  */
59 #ifdef SIGHUP
60     case SIGHUP:
61       {
62         DBusString str;
63         _dbus_string_init_const (&str, "foo");
64         if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
65             !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
66           {
67             _dbus_warn ("Unable to write to reload pipe.\n");
68             close_reload_pipe_write ();
69           }
70       }
71       break;
72 #endif
73     }
74 }
75
76 static void
77 usage (void)
78 {
79   fprintf (stderr, DBUS_DAEMON_NAME " [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork] [--introspect] [--address=ADDRESS] [--systemd-activation]\n");
80   exit (1);
81 }
82
83 static void
84 version (void)
85 {
86   printf ("D-Bus Message Bus Daemon %s\n"
87           "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
88           "This is free software; see the source for copying conditions.\n"
89           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
90           DBUS_VERSION_STRING);
91   exit (0);
92 }
93
94 static void
95 introspect (void)
96 {
97   DBusString xml;
98   const char *v_STRING;
99
100   if (!_dbus_string_init (&xml))
101     goto oom;
102
103   if (!bus_driver_generate_introspect_string (&xml))
104     {
105       _dbus_string_free (&xml);
106       goto oom;
107     }
108
109   v_STRING = _dbus_string_get_const_data (&xml);
110   printf ("%s\n", v_STRING);
111
112   exit (0);
113
114  oom:
115   _dbus_warn ("Can not introspect - Out of memory\n");
116   exit (1);
117 }
118
119 static void
120 check_two_config_files (const DBusString *config_file,
121                         const char       *extra_arg)
122 {
123   if (_dbus_string_get_length (config_file) > 0)
124     {
125       fprintf (stderr, "--%s specified but configuration file %s already requested\n",
126                extra_arg, _dbus_string_get_const_data (config_file));
127       exit (1);
128     }
129 }
130
131 static void
132 check_two_addresses (const DBusString *address,
133                      const char       *extra_arg)
134 {
135   if (_dbus_string_get_length (address) > 0)
136     {
137       fprintf (stderr, "--%s specified but address %s already requested\n",
138                extra_arg, _dbus_string_get_const_data (address));
139       exit (1);
140     }
141 }
142
143 static void
144 check_two_addr_descriptors (const DBusString *addr_fd,
145                             const char       *extra_arg)
146 {
147   if (_dbus_string_get_length (addr_fd) > 0)
148     {
149       fprintf (stderr, "--%s specified but printing address to %s already requested\n",
150                extra_arg, _dbus_string_get_const_data (addr_fd));
151       exit (1);
152     }
153 }
154
155 static void
156 check_two_pid_descriptors (const DBusString *pid_fd,
157                            const char       *extra_arg)
158 {
159   if (_dbus_string_get_length (pid_fd) > 0)
160     {
161       fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
162                extra_arg, _dbus_string_get_const_data (pid_fd));
163       exit (1);
164     }
165 }
166
167 static dbus_bool_t
168 handle_reload_watch (DBusWatch    *watch,
169                      unsigned int  flags,
170                      void         *data)
171 {
172   DBusError error;
173   DBusString str;
174
175   while (!_dbus_string_init (&str))
176     _dbus_wait_for_memory ();
177
178   if ((reload_pipe[RELOAD_READ_END] > 0) &&
179       _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
180     {
181       _dbus_warn ("Couldn't read from reload pipe.\n");
182       close_reload_pipe (&watch);
183       return TRUE;
184     }
185   _dbus_string_free (&str);
186
187   /* this can only fail if we don't understand the config file
188    * or OOM.  Either way we should just stick with the currently
189    * loaded config.
190    */
191   dbus_error_init (&error);
192   if (! bus_context_reload_config (context, &error))
193     {
194       _DBUS_ASSERT_ERROR_IS_SET (&error);
195       _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
196                     dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
197       _dbus_warn ("Unable to reload configuration: %s\n",
198                   error.message);
199       dbus_error_free (&error);
200     }
201   return TRUE;
202 }
203
204 static dbus_bool_t
205 reload_watch_callback (DBusWatch    *watch,
206                        unsigned int  condition,
207                        void         *data)
208 {
209   return dbus_watch_handle (watch, condition);
210 }
211
212 static void
213 setup_reload_pipe (DBusLoop *loop)
214 {
215   DBusError error;
216   DBusWatch *watch;
217
218   dbus_error_init (&error);
219
220   if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
221                                TRUE, &error))
222     {
223       _dbus_warn ("Unable to create reload pipe: %s\n",
224                   error.message);
225       dbus_error_free (&error);
226       exit (1);
227     }
228
229   watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
230                            DBUS_WATCH_READABLE, TRUE,
231                            handle_reload_watch, NULL, NULL);
232
233   if (watch == NULL)
234     {
235       _dbus_warn ("Unable to create reload watch: %s\n",
236                   error.message);
237       dbus_error_free (&error);
238       exit (1);
239     }
240
241   if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
242                              NULL, NULL))
243     {
244       _dbus_warn ("Unable to add reload watch to main loop: %s\n",
245                   error.message);
246       dbus_error_free (&error);
247       exit (1);
248     }
249
250 }
251
252 static void
253 close_reload_pipe (DBusWatch **watch)
254 {
255     _dbus_loop_remove_watch (bus_context_get_loop (context),
256         *watch, reload_watch_callback, NULL);
257     _dbus_watch_invalidate (*watch);
258     _dbus_watch_unref (*watch);
259     *watch = NULL;
260
261     _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
262     reload_pipe[RELOAD_READ_END] = -1;
263
264     close_reload_pipe_write ();
265 }
266
267 /* this is the only bit that's safe to do from an async signal handler */
268 static void
269 close_reload_pipe_write (void)
270 {
271     _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
272     reload_pipe[RELOAD_WRITE_END] = -1;
273 }
274
275 int
276 main (int argc, char **argv)
277 {
278   DBusError error;
279   DBusString config_file;
280   DBusString address;
281   DBusString addr_fd;
282   DBusString pid_fd;
283   const char *prev_arg;
284   DBusPipe print_addr_pipe;
285   DBusPipe print_pid_pipe;
286   int i;
287   dbus_bool_t print_address;
288   dbus_bool_t print_pid;
289   dbus_bool_t is_session_bus;
290   int force_fork;
291   dbus_bool_t systemd_activation;
292
293   if (!_dbus_string_init (&config_file))
294     return 1;
295
296   if (!_dbus_string_init (&address))
297     return 1;
298
299   if (!_dbus_string_init (&addr_fd))
300     return 1;
301
302   if (!_dbus_string_init (&pid_fd))
303     return 1;
304
305   print_address = FALSE;
306   print_pid = FALSE;
307   is_session_bus = FALSE;
308   force_fork = FORK_FOLLOW_CONFIG_FILE;
309   systemd_activation = FALSE;
310
311   prev_arg = NULL;
312   i = 1;
313   while (i < argc)
314     {
315       const char *arg = argv[i];
316
317       if (strcmp (arg, "--help") == 0 ||
318           strcmp (arg, "-h") == 0 ||
319           strcmp (arg, "-?") == 0)
320         usage ();
321       else if (strcmp (arg, "--version") == 0)
322         version ();
323       else if (strcmp (arg, "--introspect") == 0)
324         introspect ();
325       else if (strcmp (arg, "--nofork") == 0)
326         force_fork = FORK_NEVER;
327       else if (strcmp (arg, "--fork") == 0)
328         force_fork = FORK_ALWAYS;
329       else if (strcmp (arg, "--systemd-activation") == 0)
330         systemd_activation = TRUE;
331       else if (strcmp (arg, "--system") == 0)
332         {
333           check_two_config_files (&config_file, "system");
334
335           if (!_dbus_append_system_config_file (&config_file))
336             exit (1);
337         }
338       else if (strcmp (arg, "--session") == 0)
339         {
340           check_two_config_files (&config_file, "session");
341
342           if (!_dbus_append_session_config_file (&config_file))
343             exit (1);
344         }
345       else if (strstr (arg, "--config-file=") == arg)
346         {
347           const char *file;
348
349           check_two_config_files (&config_file, "config-file");
350
351           file = strchr (arg, '=');
352           ++file;
353
354           if (!_dbus_string_append (&config_file, file))
355             exit (1);
356         }
357       else if (prev_arg &&
358                strcmp (prev_arg, "--config-file") == 0)
359         {
360           check_two_config_files (&config_file, "config-file");
361
362           if (!_dbus_string_append (&config_file, arg))
363             exit (1);
364         }
365       else if (strcmp (arg, "--config-file") == 0)
366         ; /* wait for next arg */
367       else if (strstr (arg, "--address=") == arg)
368         {
369           const char *file;
370
371           check_two_addresses (&address, "address");
372
373           file = strchr (arg, '=');
374           ++file;
375
376           if (!_dbus_string_append (&address, file))
377             exit (1);
378         }
379       else if (prev_arg &&
380                strcmp (prev_arg, "--address") == 0)
381         {
382           check_two_addresses (&address, "address");
383
384           if (!_dbus_string_append (&address, arg))
385             exit (1);
386         }
387       else if (strcmp (arg, "--address") == 0)
388         ; /* wait for next arg */
389       else if (strstr (arg, "--print-address=") == arg)
390         {
391           const char *desc;
392
393           check_two_addr_descriptors (&addr_fd, "print-address");
394
395           desc = strchr (arg, '=');
396           ++desc;
397
398           if (!_dbus_string_append (&addr_fd, desc))
399             exit (1);
400
401           print_address = TRUE;
402         }
403       else if (prev_arg &&
404                strcmp (prev_arg, "--print-address") == 0)
405         {
406           check_two_addr_descriptors (&addr_fd, "print-address");
407
408           if (!_dbus_string_append (&addr_fd, arg))
409             exit (1);
410
411           print_address = TRUE;
412         }
413       else if (strcmp (arg, "--print-address") == 0)
414         print_address = TRUE; /* and we'll get the next arg if appropriate */
415       else if (strstr (arg, "--print-pid=") == arg)
416         {
417           const char *desc;
418
419           check_two_pid_descriptors (&pid_fd, "print-pid");
420
421           desc = strchr (arg, '=');
422           ++desc;
423
424           if (!_dbus_string_append (&pid_fd, desc))
425             exit (1);
426
427           print_pid = TRUE;
428         }
429       else if (prev_arg &&
430                strcmp (prev_arg, "--print-pid") == 0)
431         {
432           check_two_pid_descriptors (&pid_fd, "print-pid");
433
434           if (!_dbus_string_append (&pid_fd, arg))
435             exit (1);
436
437           print_pid = TRUE;
438         }
439       else if (strcmp (arg, "--print-pid") == 0)
440         print_pid = TRUE; /* and we'll get the next arg if appropriate */
441       else
442         usage ();
443
444       prev_arg = arg;
445
446       ++i;
447     }
448
449   if (_dbus_string_get_length (&config_file) == 0)
450     {
451       fprintf (stderr, "No configuration file specified.\n");
452       usage ();
453     }
454
455   _dbus_pipe_invalidate (&print_addr_pipe);
456   if (print_address)
457     {
458       _dbus_pipe_init_stdout (&print_addr_pipe);
459       if (_dbus_string_get_length (&addr_fd) > 0)
460         {
461           long val;
462           int end;
463           if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
464               end != _dbus_string_get_length (&addr_fd) ||
465               val < 0 || val > _DBUS_INT_MAX)
466             {
467               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
468                        _dbus_string_get_const_data (&addr_fd));
469               exit (1);
470             }
471
472           _dbus_pipe_init (&print_addr_pipe, val);
473         }
474     }
475   _dbus_string_free (&addr_fd);
476
477   _dbus_pipe_invalidate (&print_pid_pipe);
478   if (print_pid)
479     {
480       _dbus_pipe_init_stdout (&print_pid_pipe);
481       if (_dbus_string_get_length (&pid_fd) > 0)
482         {
483           long val;
484           int end;
485           if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
486               end != _dbus_string_get_length (&pid_fd) ||
487               val < 0 || val > _DBUS_INT_MAX)
488             {
489               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
490                        _dbus_string_get_const_data (&pid_fd));
491               exit (1);
492             }
493
494           _dbus_pipe_init (&print_pid_pipe, val);
495         }
496     }
497   _dbus_string_free (&pid_fd);
498
499   if (!bus_selinux_pre_init ())
500     {
501       _dbus_warn ("SELinux pre-initialization failed\n");
502       exit (1);
503     }
504
505   dbus_error_init (&error);
506   context = bus_context_new (&config_file, force_fork,
507                              &print_addr_pipe, &print_pid_pipe,
508                              _dbus_string_get_length(&address) > 0 ? &address : NULL,
509                              systemd_activation,
510                              &error);
511   _dbus_string_free (&config_file);
512   if (context == NULL)
513     {
514       _dbus_warn ("Failed to start message bus: %s\n",
515                   error.message);
516       dbus_error_free (&error);
517       exit (1);
518     }
519
520   /* bus_context_new() closes the print_addr_pipe and
521    * print_pid_pipe
522    */
523
524   setup_reload_pipe (bus_context_get_loop (context));
525
526 #ifdef SIGHUP
527   _dbus_set_signal_handler (SIGHUP, signal_handler);
528 #endif
529 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
530   _dbus_set_signal_handler (SIGIO, signal_handler);
531 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
532
533   _dbus_verbose ("We are on D-Bus...\n");
534   _dbus_loop_run (bus_context_get_loop (context));
535
536   bus_context_shutdown (context);
537   bus_context_unref (context);
538   bus_selinux_shutdown ();
539
540   return 0;
541 }