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