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