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