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