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