bus signal_handler: comment why it's OK if the reload pipe gets full
[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_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #ifdef HAVE_ERRNO_H
39 #include <errno.h>
40 #endif
41 #include "selinux.h"
42
43 static BusContext *context;
44
45 static int reload_pipe[2];
46 #define RELOAD_READ_END 0
47 #define RELOAD_WRITE_END 1
48
49 static void close_reload_pipe (void);
50
51 typedef enum
52  {
53    ACTION_RELOAD = 'r',
54    ACTION_QUIT = 'q'
55  } SignalAction;
56
57 #ifdef DBUS_UNIX
58 static void
59 signal_handler (int sig)
60 {
61   switch (sig)
62     {
63 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
64     case SIGIO:
65       /* explicit fall-through */
66 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX  */
67 #ifdef SIGHUP
68     case SIGHUP:
69       {
70         DBusString str;
71         char action[2] = { ACTION_RELOAD, '\0' };
72
73         _dbus_string_init_const (&str, action);
74         if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
75             !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
76           {
77             /* If we receive SIGHUP often enough to fill the pipe buffer (4096
78              * times on old Linux, 65536 on modern Linux) before it can be
79              * drained, let's just warn and ignore. The configuration will be
80              * reloaded while draining the pipe buffer, which is what we
81              * wanted. It's harmless that it will be reloaded fewer times than
82              * we asked for, since the reload is delayed anyway, so new changes
83              * will be picked up.
84              *
85              * We use write() because _dbus_warn uses vfprintf, which isn't
86              * async-signal-safe.
87              *
88              * This is necessarily Unix-specific, but so are POSIX signals,
89              * so... */
90             static const char message[] =
91               "Unable to write to reload pipe - buffer full?\n";
92
93             write (STDERR_FILENO, message, strlen (message));
94           }
95       }
96       break;
97 #endif
98
99     case SIGTERM:
100       {
101         DBusString str;
102         char action[2] = { ACTION_QUIT, '\0' };
103         _dbus_string_init_const (&str, action);
104         if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
105             !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
106           {
107             static const char message[] =
108               "Unable to write to reload pipe - buffer full?\n";
109
110             write (STDERR_FILENO, message, strlen (message));
111           }
112       }
113       break;
114     }
115 }
116 #endif /* DBUS_UNIX */
117
118 static void
119 usage (void)
120 {
121   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");
122   exit (1);
123 }
124
125 static void
126 version (void)
127 {
128   printf ("D-Bus Message Bus Daemon %s\n"
129           "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
130           "This is free software; see the source for copying conditions.\n"
131           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
132           DBUS_VERSION_STRING);
133   exit (0);
134 }
135
136 static void
137 introspect (void)
138 {
139   DBusString xml;
140   const char *v_STRING;
141
142   if (!_dbus_string_init (&xml))
143     goto oom;
144
145   if (!bus_driver_generate_introspect_string (&xml))
146     {
147       _dbus_string_free (&xml);
148       goto oom;
149     }
150
151   v_STRING = _dbus_string_get_const_data (&xml);
152   printf ("%s\n", v_STRING);
153
154   exit (0);
155
156  oom:
157   _dbus_warn ("Can not introspect - Out of memory\n");
158   exit (1);
159 }
160
161 static void
162 check_two_config_files (const DBusString *config_file,
163                         const char       *extra_arg)
164 {
165   if (_dbus_string_get_length (config_file) > 0)
166     {
167       fprintf (stderr, "--%s specified but configuration file %s already requested\n",
168                extra_arg, _dbus_string_get_const_data (config_file));
169       exit (1);
170     }
171 }
172
173 static void
174 check_two_addresses (const DBusString *address,
175                      const char       *extra_arg)
176 {
177   if (_dbus_string_get_length (address) > 0)
178     {
179       fprintf (stderr, "--%s specified but address %s already requested\n",
180                extra_arg, _dbus_string_get_const_data (address));
181       exit (1);
182     }
183 }
184
185 static void
186 check_two_addr_descriptors (const DBusString *addr_fd,
187                             const char       *extra_arg)
188 {
189   if (_dbus_string_get_length (addr_fd) > 0)
190     {
191       fprintf (stderr, "--%s specified but printing address to %s already requested\n",
192                extra_arg, _dbus_string_get_const_data (addr_fd));
193       exit (1);
194     }
195 }
196
197 static void
198 check_two_pid_descriptors (const DBusString *pid_fd,
199                            const char       *extra_arg)
200 {
201   if (_dbus_string_get_length (pid_fd) > 0)
202     {
203       fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
204                extra_arg, _dbus_string_get_const_data (pid_fd));
205       exit (1);
206     }
207 }
208
209 static dbus_bool_t
210 handle_reload_watch (DBusWatch    *watch,
211                      unsigned int  flags,
212                      void         *data)
213 {
214   DBusError error;
215   DBusString str;
216   char *action_str;
217   char action = '\0';
218
219   while (!_dbus_string_init (&str))
220     _dbus_wait_for_memory ();
221
222   if ((reload_pipe[RELOAD_READ_END] > 0) &&
223       _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
224     {
225       _dbus_warn ("Couldn't read from reload pipe.\n");
226       close_reload_pipe ();
227       return TRUE;
228     }
229
230   action_str = _dbus_string_get_data (&str);
231   if (action_str != NULL)
232     {
233       action = action_str[0];
234     }
235   _dbus_string_free (&str);
236
237   /* this can only fail if we don't understand the config file
238    * or OOM.  Either way we should just stick with the currently
239    * loaded config.
240    */
241   dbus_error_init (&error);
242
243   switch (action)
244     {
245     case ACTION_RELOAD:
246       if (! bus_context_reload_config (context, &error))
247         {
248           _DBUS_ASSERT_ERROR_IS_SET (&error);
249           _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
250                         dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
251           _dbus_warn ("Unable to reload configuration: %s\n",
252                       error.message);
253           dbus_error_free (&error);
254         }
255       break;
256
257     case ACTION_QUIT:
258       {
259         DBusLoop *loop;
260         /*
261          * On OSs without abstract sockets, we want to quit
262          * gracefully rather than being killed by SIGTERM,
263          * so that DBusServer gets a chance to clean up the
264          * sockets from the filesystem. fd.o #38656
265          */
266         loop = bus_context_get_loop (context);
267         if (loop != NULL)
268           {
269             _dbus_loop_quit (loop);
270           }
271       }
272       break;
273
274     default:
275       break;
276     }
277
278   return TRUE;
279 }
280
281 static dbus_bool_t
282 reload_watch_callback (DBusWatch    *watch,
283                        unsigned int  condition,
284                        void         *data)
285 {
286   return dbus_watch_handle (watch, condition);
287 }
288
289 static void
290 setup_reload_pipe (DBusLoop *loop)
291 {
292   DBusError error;
293   DBusWatch *watch;
294
295   dbus_error_init (&error);
296
297   if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
298                                TRUE, &error))
299     {
300       _dbus_warn ("Unable to create reload pipe: %s\n",
301                   error.message);
302       dbus_error_free (&error);
303       exit (1);
304     }
305
306   watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
307                            DBUS_WATCH_READABLE, TRUE,
308                            handle_reload_watch, NULL, NULL);
309
310   if (watch == NULL)
311     {
312       _dbus_warn ("Unable to create reload watch: %s\n",
313                   error.message);
314       dbus_error_free (&error);
315       exit (1);
316     }
317
318   if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
319                              NULL, NULL))
320     {
321       _dbus_warn ("Unable to add reload watch to main loop: %s\n",
322                   error.message);
323       dbus_error_free (&error);
324       exit (1);
325     }
326
327 }
328
329 static void
330 close_reload_pipe (void)
331 {
332     _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
333     reload_pipe[RELOAD_READ_END] = -1;
334
335     _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
336     reload_pipe[RELOAD_WRITE_END] = -1;
337 }
338
339 int
340 main (int argc, char **argv)
341 {
342   DBusError error;
343   DBusString config_file;
344   DBusString address;
345   DBusString addr_fd;
346   DBusString pid_fd;
347   const char *prev_arg;
348   DBusPipe print_addr_pipe;
349   DBusPipe print_pid_pipe;
350   int i;
351   dbus_bool_t print_address;
352   dbus_bool_t print_pid;
353   dbus_bool_t is_session_bus;
354   int force_fork;
355   dbus_bool_t systemd_activation;
356
357   if (!_dbus_string_init (&config_file))
358     return 1;
359
360   if (!_dbus_string_init (&address))
361     return 1;
362
363   if (!_dbus_string_init (&addr_fd))
364     return 1;
365
366   if (!_dbus_string_init (&pid_fd))
367     return 1;
368
369   print_address = FALSE;
370   print_pid = FALSE;
371   is_session_bus = FALSE;
372   force_fork = FORK_FOLLOW_CONFIG_FILE;
373   systemd_activation = FALSE;
374
375   prev_arg = NULL;
376   i = 1;
377   while (i < argc)
378     {
379       const char *arg = argv[i];
380
381       if (strcmp (arg, "--help") == 0 ||
382           strcmp (arg, "-h") == 0 ||
383           strcmp (arg, "-?") == 0)
384         usage ();
385       else if (strcmp (arg, "--version") == 0)
386         version ();
387       else if (strcmp (arg, "--introspect") == 0)
388         introspect ();
389       else if (strcmp (arg, "--nofork") == 0)
390         force_fork = FORK_NEVER;
391       else if (strcmp (arg, "--fork") == 0)
392         force_fork = FORK_ALWAYS;
393       else if (strcmp (arg, "--systemd-activation") == 0)
394         systemd_activation = TRUE;
395       else if (strcmp (arg, "--system") == 0)
396         {
397           check_two_config_files (&config_file, "system");
398
399           if (!_dbus_append_system_config_file (&config_file))
400             exit (1);
401         }
402       else if (strcmp (arg, "--session") == 0)
403         {
404           check_two_config_files (&config_file, "session");
405
406           if (!_dbus_append_session_config_file (&config_file))
407             exit (1);
408         }
409       else if (strstr (arg, "--config-file=") == arg)
410         {
411           const char *file;
412
413           check_two_config_files (&config_file, "config-file");
414
415           file = strchr (arg, '=');
416           ++file;
417
418           if (!_dbus_string_append (&config_file, file))
419             exit (1);
420         }
421       else if (prev_arg &&
422                strcmp (prev_arg, "--config-file") == 0)
423         {
424           check_two_config_files (&config_file, "config-file");
425
426           if (!_dbus_string_append (&config_file, arg))
427             exit (1);
428         }
429       else if (strcmp (arg, "--config-file") == 0)
430         ; /* wait for next arg */
431       else if (strstr (arg, "--address=") == arg)
432         {
433           const char *file;
434
435           check_two_addresses (&address, "address");
436
437           file = strchr (arg, '=');
438           ++file;
439
440           if (!_dbus_string_append (&address, file))
441             exit (1);
442         }
443       else if (prev_arg &&
444                strcmp (prev_arg, "--address") == 0)
445         {
446           check_two_addresses (&address, "address");
447
448           if (!_dbus_string_append (&address, arg))
449             exit (1);
450         }
451       else if (strcmp (arg, "--address") == 0)
452         ; /* wait for next arg */
453       else if (strstr (arg, "--print-address=") == arg)
454         {
455           const char *desc;
456
457           check_two_addr_descriptors (&addr_fd, "print-address");
458
459           desc = strchr (arg, '=');
460           ++desc;
461
462           if (!_dbus_string_append (&addr_fd, desc))
463             exit (1);
464
465           print_address = TRUE;
466         }
467       else if (prev_arg &&
468                strcmp (prev_arg, "--print-address") == 0)
469         {
470           check_two_addr_descriptors (&addr_fd, "print-address");
471
472           if (!_dbus_string_append (&addr_fd, arg))
473             exit (1);
474
475           print_address = TRUE;
476         }
477       else if (strcmp (arg, "--print-address") == 0)
478         print_address = TRUE; /* and we'll get the next arg if appropriate */
479       else if (strstr (arg, "--print-pid=") == arg)
480         {
481           const char *desc;
482
483           check_two_pid_descriptors (&pid_fd, "print-pid");
484
485           desc = strchr (arg, '=');
486           ++desc;
487
488           if (!_dbus_string_append (&pid_fd, desc))
489             exit (1);
490
491           print_pid = TRUE;
492         }
493       else if (prev_arg &&
494                strcmp (prev_arg, "--print-pid") == 0)
495         {
496           check_two_pid_descriptors (&pid_fd, "print-pid");
497
498           if (!_dbus_string_append (&pid_fd, arg))
499             exit (1);
500
501           print_pid = TRUE;
502         }
503       else if (strcmp (arg, "--print-pid") == 0)
504         print_pid = TRUE; /* and we'll get the next arg if appropriate */
505       else
506         usage ();
507
508       prev_arg = arg;
509
510       ++i;
511     }
512
513   if (_dbus_string_get_length (&config_file) == 0)
514     {
515       fprintf (stderr, "No configuration file specified.\n");
516       usage ();
517     }
518
519   _dbus_pipe_invalidate (&print_addr_pipe);
520   if (print_address)
521     {
522       _dbus_pipe_init_stdout (&print_addr_pipe);
523       if (_dbus_string_get_length (&addr_fd) > 0)
524         {
525           long val;
526           int end;
527           if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
528               end != _dbus_string_get_length (&addr_fd) ||
529               val < 0 || val > _DBUS_INT_MAX)
530             {
531               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
532                        _dbus_string_get_const_data (&addr_fd));
533               exit (1);
534             }
535
536           _dbus_pipe_init (&print_addr_pipe, val);
537         }
538     }
539   _dbus_string_free (&addr_fd);
540
541   _dbus_pipe_invalidate (&print_pid_pipe);
542   if (print_pid)
543     {
544       _dbus_pipe_init_stdout (&print_pid_pipe);
545       if (_dbus_string_get_length (&pid_fd) > 0)
546         {
547           long val;
548           int end;
549           if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
550               end != _dbus_string_get_length (&pid_fd) ||
551               val < 0 || val > _DBUS_INT_MAX)
552             {
553               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
554                        _dbus_string_get_const_data (&pid_fd));
555               exit (1);
556             }
557
558           _dbus_pipe_init (&print_pid_pipe, val);
559         }
560     }
561   _dbus_string_free (&pid_fd);
562
563   if (!bus_selinux_pre_init ())
564     {
565       _dbus_warn ("SELinux pre-initialization failed\n");
566       exit (1);
567     }
568
569   dbus_error_init (&error);
570   context = bus_context_new (&config_file, force_fork,
571                              &print_addr_pipe, &print_pid_pipe,
572                              _dbus_string_get_length(&address) > 0 ? &address : NULL,
573                              systemd_activation,
574                              &error);
575   _dbus_string_free (&config_file);
576   if (context == NULL)
577     {
578       _dbus_warn ("Failed to start message bus: %s\n",
579                   error.message);
580       dbus_error_free (&error);
581       exit (1);
582     }
583
584   /* bus_context_new() closes the print_addr_pipe and
585    * print_pid_pipe
586    */
587
588   setup_reload_pipe (bus_context_get_loop (context));
589
590 #ifdef DBUS_UNIX
591   /* POSIX signals are Unix-specific, and _dbus_set_signal_handler is
592    * unimplemented (and probably unimplementable) on Windows, so there's
593    * no point in trying to make the handler portable to non-Unix. */
594
595   _dbus_set_signal_handler (SIGTERM, signal_handler);
596 #ifdef SIGHUP
597   _dbus_set_signal_handler (SIGHUP, signal_handler);
598 #endif
599 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
600   _dbus_set_signal_handler (SIGIO, signal_handler);
601 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
602 #endif /* DBUS_UNIX */
603
604   _dbus_verbose ("We are on D-Bus...\n");
605   _dbus_loop_run (bus_context_get_loop (context));
606
607   bus_context_shutdown (context);
608   bus_context_unref (context);
609   bus_selinux_shutdown ();
610
611   return 0;
612 }