Merge branch 'dbus-1.2'
[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 #include "bus.h"
24 #include "driver.h"
25 #include <dbus/dbus-internals.h>
26 #include <dbus/dbus-watch.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <signal.h>
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #include "selinux.h"
35
36 static BusContext *context;
37
38 static int reload_pipe[2];
39 #define RELOAD_READ_END 0
40 #define RELOAD_WRITE_END 1
41
42 static void close_reload_pipe (void);
43
44 static void
45 signal_handler (int sig)
46 {
47
48   switch (sig)
49     {
50 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX 
51     case SIGIO: 
52       /* explicit fall-through */
53 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX  */
54 #ifdef SIGHUP
55     case SIGHUP:
56       {
57         DBusString str;
58         _dbus_string_init_const (&str, "foo");
59         if ((reload_pipe[RELOAD_WRITE_END] > 0) && 
60             !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
61           {
62             _dbus_warn ("Unable to write to reload pipe.\n");
63             close_reload_pipe ();
64           }
65       }
66       break;
67 #endif
68     case SIGTERM:
69       _dbus_loop_quit (bus_context_get_loop (context));
70       break;
71     }
72 }
73
74 static void
75 usage (void)
76 {
77   fprintf (stderr, DBUS_DAEMON_NAME " [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork] [--introspect]\n");
78   exit (1);
79 }
80
81 static void
82 version (void)
83 {
84   printf ("D-Bus Message Bus Daemon %s\n"
85           "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
86           "This is free software; see the source for copying conditions.\n"
87           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
88           DBUS_VERSION_STRING);
89   exit (0);
90 }
91
92 static void
93 introspect (void)
94 {
95   DBusString xml;
96   const char *v_STRING;  
97
98   if (!_dbus_string_init (&xml))
99     goto oom;
100
101   if (!bus_driver_generate_introspect_string (&xml))
102     {
103       _dbus_string_free (&xml);
104       goto oom;
105     }
106
107   v_STRING = _dbus_string_get_const_data (&xml);
108   printf ("%s\n", v_STRING); 
109
110   exit (0);
111  
112  oom:
113   _dbus_warn ("Can not introspect - Out of memory\n");
114   exit (1);
115 }
116 static void
117 check_two_config_files (const DBusString *config_file,
118                         const char       *extra_arg)
119 {
120   if (_dbus_string_get_length (config_file) > 0)
121     {
122       fprintf (stderr, "--%s specified but configuration file %s already requested\n",
123                extra_arg, _dbus_string_get_const_data (config_file));
124       exit (1);
125     }
126 }
127
128 static void
129 check_two_addr_descriptors (const DBusString *addr_fd,
130                             const char       *extra_arg)
131 {
132   if (_dbus_string_get_length (addr_fd) > 0)
133     {
134       fprintf (stderr, "--%s specified but printing address to %s already requested\n",
135                extra_arg, _dbus_string_get_const_data (addr_fd));
136       exit (1);
137     }
138 }
139
140 static void
141 check_two_pid_descriptors (const DBusString *pid_fd,
142                            const char       *extra_arg)
143 {
144   if (_dbus_string_get_length (pid_fd) > 0)
145     {
146       fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
147                extra_arg, _dbus_string_get_const_data (pid_fd));
148       exit (1);
149     }
150 }
151
152 static dbus_bool_t
153 handle_reload_watch (DBusWatch    *watch,
154                      unsigned int  flags,
155                      void         *data)
156 {
157   DBusError error;
158   DBusString str;
159   _dbus_string_init (&str);
160   if ((reload_pipe[RELOAD_READ_END] > 0) &&
161       _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
162     {
163       _dbus_warn ("Couldn't read from reload pipe.\n");
164       close_reload_pipe ();
165       return TRUE;
166     }
167   _dbus_string_free (&str);
168
169   /* this can only fail if we don't understand the config file
170    * or OOM.  Either way we should just stick with the currently
171    * loaded config.
172    */
173   dbus_error_init (&error);
174   if (! bus_context_reload_config (context, &error))
175     {
176       _DBUS_ASSERT_ERROR_IS_SET (&error);
177       _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
178                     dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
179       _dbus_warn ("Unable to reload configuration: %s\n",
180                   error.message);
181       dbus_error_free (&error);
182     }
183   return TRUE;
184 }
185
186 static dbus_bool_t
187 reload_watch_callback (DBusWatch    *watch,
188                        unsigned int  condition,
189                        void         *data)
190 {
191   return dbus_watch_handle (watch, condition);
192 }
193
194 static void
195 setup_reload_pipe (DBusLoop *loop)
196 {
197   DBusError error;
198   DBusWatch *watch;
199
200   dbus_error_init (&error);
201
202   if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
203                                TRUE, &error))
204     {
205       _dbus_warn ("Unable to create reload pipe: %s\n",
206                   error.message);
207       dbus_error_free (&error);
208       exit (1);
209     }
210
211   watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
212                            DBUS_WATCH_READABLE, TRUE,
213                            handle_reload_watch, NULL, NULL);
214
215   if (watch == NULL)
216     {
217       _dbus_warn ("Unable to create reload watch: %s\n",
218                   error.message);
219       dbus_error_free (&error);
220       exit (1);
221     }
222
223   if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
224                              NULL, NULL))
225     {
226       _dbus_warn ("Unable to add reload watch to main loop: %s\n",
227                   error.message);
228       dbus_error_free (&error);
229       exit (1);
230     }
231
232 }
233
234 static void
235 close_reload_pipe (void)
236 {
237     _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
238     reload_pipe[RELOAD_READ_END] = -1;
239
240     _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
241     reload_pipe[RELOAD_WRITE_END] = -1;
242 }
243
244 int
245 main (int argc, char **argv)
246 {
247   DBusError error;
248   DBusString config_file;
249   DBusString addr_fd;
250   DBusString pid_fd;
251   const char *prev_arg;
252   DBusPipe print_addr_pipe;
253   DBusPipe print_pid_pipe;
254   int i;
255   dbus_bool_t print_address;
256   dbus_bool_t print_pid;
257   dbus_bool_t is_session_bus;
258   int force_fork;
259
260   if (!_dbus_string_init (&config_file))
261     return 1;
262
263   if (!_dbus_string_init (&addr_fd))
264     return 1;
265
266   if (!_dbus_string_init (&pid_fd))
267     return 1;
268
269   print_address = FALSE;
270   print_pid = FALSE;
271   is_session_bus = FALSE;
272   force_fork = FORK_FOLLOW_CONFIG_FILE;
273
274   prev_arg = NULL;
275   i = 1;
276   while (i < argc)
277     {
278       const char *arg = argv[i];
279
280       if (strcmp (arg, "--help") == 0 ||
281           strcmp (arg, "-h") == 0 ||
282           strcmp (arg, "-?") == 0)
283         usage ();
284       else if (strcmp (arg, "--version") == 0)
285         version ();
286       else if (strcmp (arg, "--introspect") == 0)
287         introspect ();
288       else if (strcmp (arg, "--nofork") == 0)
289         force_fork = FORK_NEVER;
290       else if (strcmp (arg, "--fork") == 0)
291         force_fork = FORK_ALWAYS;
292       else if (strcmp (arg, "--system") == 0)
293         {
294           check_two_config_files (&config_file, "system");
295
296           if (!_dbus_append_system_config_file (&config_file))
297             exit (1);
298         }
299       else if (strcmp (arg, "--session") == 0)
300         {
301           check_two_config_files (&config_file, "session");
302
303           if (!_dbus_append_session_config_file (&config_file))
304             exit (1);
305         }
306       else if (strstr (arg, "--config-file=") == arg)
307         {
308           const char *file;
309
310           check_two_config_files (&config_file, "config-file");
311           
312           file = strchr (arg, '=');
313           ++file;
314
315           if (!_dbus_string_append (&config_file, file))
316             exit (1);
317         }
318       else if (prev_arg &&
319                strcmp (prev_arg, "--config-file") == 0)
320         {
321           check_two_config_files (&config_file, "config-file");
322           
323           if (!_dbus_string_append (&config_file, arg))
324             exit (1);
325         }
326       else if (strcmp (arg, "--config-file") == 0)
327         ; /* wait for next arg */
328       else if (strstr (arg, "--print-address=") == arg)
329         {
330           const char *desc;
331
332           check_two_addr_descriptors (&addr_fd, "print-address");
333           
334           desc = strchr (arg, '=');
335           ++desc;
336
337           if (!_dbus_string_append (&addr_fd, desc))
338             exit (1);
339
340           print_address = TRUE;
341         }
342       else if (prev_arg &&
343                strcmp (prev_arg, "--print-address") == 0)
344         {
345           check_two_addr_descriptors (&addr_fd, "print-address");
346           
347           if (!_dbus_string_append (&addr_fd, arg))
348             exit (1);
349
350           print_address = TRUE;
351         }
352       else if (strcmp (arg, "--print-address") == 0)
353         print_address = TRUE; /* and we'll get the next arg if appropriate */
354       else if (strstr (arg, "--print-pid=") == arg)
355         {
356           const char *desc;
357
358           check_two_pid_descriptors (&pid_fd, "print-pid");
359           
360           desc = strchr (arg, '=');
361           ++desc;
362
363           if (!_dbus_string_append (&pid_fd, desc))
364             exit (1);
365
366           print_pid = TRUE;
367         }
368       else if (prev_arg &&
369                strcmp (prev_arg, "--print-pid") == 0)
370         {
371           check_two_pid_descriptors (&pid_fd, "print-pid");
372           
373           if (!_dbus_string_append (&pid_fd, arg))
374             exit (1);
375           
376           print_pid = TRUE;
377         }
378       else if (strcmp (arg, "--print-pid") == 0)
379         print_pid = TRUE; /* and we'll get the next arg if appropriate */
380       else
381         usage ();
382       
383       prev_arg = arg;
384       
385       ++i;
386     }
387
388   if (_dbus_string_get_length (&config_file) == 0)
389     {
390       fprintf (stderr, "No configuration file specified.\n");
391       usage ();
392     }
393
394   _dbus_pipe_invalidate (&print_addr_pipe);
395   if (print_address)
396     {
397       _dbus_pipe_init_stdout (&print_addr_pipe);
398       if (_dbus_string_get_length (&addr_fd) > 0)
399         {
400           long val;
401           int end;
402           if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
403               end != _dbus_string_get_length (&addr_fd) ||
404               val < 0 || val > _DBUS_INT_MAX)
405             {
406               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
407                        _dbus_string_get_const_data (&addr_fd));
408               exit (1);
409             }
410
411           _dbus_pipe_init (&print_addr_pipe, val);
412         }
413     }
414   _dbus_string_free (&addr_fd);
415
416   _dbus_pipe_invalidate (&print_pid_pipe);
417   if (print_pid)
418     {
419       _dbus_pipe_init_stdout (&print_pid_pipe);
420       if (_dbus_string_get_length (&pid_fd) > 0)
421         {
422           long val;
423           int end;
424           if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
425               end != _dbus_string_get_length (&pid_fd) ||
426               val < 0 || val > _DBUS_INT_MAX)
427             {
428               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
429                        _dbus_string_get_const_data (&pid_fd));
430               exit (1);
431             }
432
433           _dbus_pipe_init (&print_pid_pipe, val);
434         }
435     }
436   _dbus_string_free (&pid_fd);
437
438   if (!bus_selinux_pre_init ())
439     {
440       _dbus_warn ("SELinux pre-initialization failed\n");
441       exit (1);
442     }
443
444   dbus_error_init (&error);
445   context = bus_context_new (&config_file, force_fork,
446                              &print_addr_pipe, &print_pid_pipe,
447                              &error);
448   _dbus_string_free (&config_file);
449   if (context == NULL)
450     {
451       _dbus_warn ("Failed to start message bus: %s\n",
452                   error.message);
453       dbus_error_free (&error);
454       exit (1);
455     }
456
457   is_session_bus = bus_context_get_type(context) != NULL
458       && strcmp(bus_context_get_type(context),"session") == 0;
459
460   if (is_session_bus)
461     _dbus_daemon_publish_session_bus_address (bus_context_get_address (context));
462
463   /* bus_context_new() closes the print_addr_pipe and
464    * print_pid_pipe
465    */
466   
467   setup_reload_pipe (bus_context_get_loop (context));
468
469 #ifdef SIGHUP
470   _dbus_set_signal_handler (SIGHUP, signal_handler);
471 #endif
472   _dbus_set_signal_handler (SIGTERM, signal_handler);
473 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX 
474   _dbus_set_signal_handler (SIGIO, signal_handler);
475 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
476   
477   _dbus_verbose ("We are on D-Bus...\n");
478   _dbus_loop_run (bus_context_get_loop (context));
479   
480   bus_context_shutdown (context);
481   bus_context_unref (context);
482   bus_selinux_shutdown ();
483
484   if (is_session_bus)
485     _dbus_daemon_unpublish_session_bus_address ();
486
487   return 0;
488 }