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