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