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