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