2004-07-24 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / bus / main.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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.0
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 <dbus/dbus-internals.h>
25 #include <dbus/dbus-watch.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <errno.h>
31 #include "selinux.h"
32
33 static BusContext *context;
34
35 static int reload_pipe[2];
36 #define RELOAD_READ_END 0
37 #define RELOAD_WRITE_END 1
38
39
40 static void
41 signal_handler (int sig)
42 {
43   DBusString str;
44
45   switch (sig)
46     {
47     case SIGHUP:
48       _dbus_string_init_const (&str, "foo");
49       if (!_dbus_write (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
50         {
51           _dbus_warn ("Unable to write to reload pipe.\n");
52           exit (1);
53         }
54       break;
55
56     case SIGTERM:
57       _dbus_loop_quit (bus_context_get_loop (context));
58       break;
59     }
60 }
61
62 static void
63 usage (void)
64 {
65   fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork]\n");
66   exit (1);
67 }
68
69 static void
70 version (void)
71 {
72   printf ("D-BUS Message Bus Daemon %s\n"
73           "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
74           "This is free software; see the source for copying conditions.\n"
75           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
76           VERSION);
77   exit (0);
78 }
79
80 static void
81 check_two_config_files (const DBusString *config_file,
82                         const char       *extra_arg)
83 {
84   if (_dbus_string_get_length (config_file) > 0)
85     {
86       fprintf (stderr, "--%s specified but configuration file %s already requested\n",
87                extra_arg, _dbus_string_get_const_data (config_file));
88       exit (1);
89     }
90 }
91
92 static void
93 check_two_addr_descriptors (const DBusString *addr_fd,
94                             const char       *extra_arg)
95 {
96   if (_dbus_string_get_length (addr_fd) > 0)
97     {
98       fprintf (stderr, "--%s specified but printing address to %s already requested\n",
99                extra_arg, _dbus_string_get_const_data (addr_fd));
100       exit (1);
101     }
102 }
103
104 static void
105 check_two_pid_descriptors (const DBusString *pid_fd,
106                            const char       *extra_arg)
107 {
108   if (_dbus_string_get_length (pid_fd) > 0)
109     {
110       fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
111                extra_arg, _dbus_string_get_const_data (pid_fd));
112       exit (1);
113     }
114 }
115
116 static dbus_bool_t
117 handle_reload_watch (DBusWatch    *watch,
118                      unsigned int  flags,
119                      void         *data)
120 {
121   DBusError error;
122   DBusString str;
123   _dbus_string_init (&str);
124   if (_dbus_read (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
125     {
126       _dbus_warn ("Couldn't read from reload pipe.\n");
127       exit (1);
128     }
129   _dbus_string_free (&str);
130
131   dbus_error_init (&error);
132   if (! bus_context_reload_config (context, &error))
133     {
134       _dbus_warn ("Unable to reload configuration: %s\n",
135                   error.message);
136       dbus_error_free (&error);
137       exit (1);
138     }
139   return TRUE;
140 }
141
142 static dbus_bool_t
143 reload_watch_callback (DBusWatch    *watch,
144                        unsigned int  condition,
145                        void         *data)
146 {
147   return dbus_watch_handle (watch, condition);
148 }
149
150 static void
151 setup_reload_pipe (DBusLoop *loop)
152 {
153   DBusError error;
154   DBusWatch *watch;
155
156   dbus_error_init (&error);
157
158   if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
159                                TRUE, &error))
160     {
161       _dbus_warn ("Unable to create reload pipe: %s\n",
162                   error.message);
163       dbus_error_free (&error);
164       exit (1);
165     }
166
167   watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
168                            DBUS_WATCH_READABLE, TRUE,
169                            handle_reload_watch, NULL, NULL);
170
171   if (watch == NULL)
172     {
173       _dbus_warn ("Unable to create reload watch: %s\n",
174                   error.message);
175       dbus_error_free (&error);
176       exit (1);
177     }
178
179   if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
180                              NULL, NULL))
181     {
182       _dbus_warn ("Unable to add reload watch to main loop: %s\n",
183                   error.message);
184       dbus_error_free (&error);
185       exit (1);
186     }
187
188 }
189
190 int
191 main (int argc, char **argv)
192 {
193   DBusError error;
194   DBusString config_file;
195   DBusString addr_fd;
196   DBusString pid_fd;
197   const char *prev_arg;
198   int print_addr_fd;
199   int print_pid_fd;
200   int i;
201   dbus_bool_t print_address;
202   dbus_bool_t print_pid;
203   dbus_bool_t force_fork;
204   
205   if (!_dbus_string_init (&config_file))
206     return 1;
207
208   if (!_dbus_string_init (&addr_fd))
209     return 1;
210
211   if (!_dbus_string_init (&pid_fd))
212     return 1;
213   
214   print_address = FALSE;
215   print_pid = FALSE;
216   force_fork = FALSE;
217   
218   prev_arg = NULL;
219   i = 1;
220   while (i < argc)
221     {
222       const char *arg = argv[i];
223       
224       if (strcmp (arg, "--help") == 0 ||
225           strcmp (arg, "-h") == 0 ||
226           strcmp (arg, "-?") == 0)
227         usage ();
228       else if (strcmp (arg, "--version") == 0)
229         version ();
230       else if (strcmp (arg, "--fork") == 0)
231         force_fork = TRUE;
232       else if (strcmp (arg, "--system") == 0)
233         {
234           check_two_config_files (&config_file, "system");
235
236           if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
237             exit (1);
238         }
239       else if (strcmp (arg, "--session") == 0)
240         {
241           check_two_config_files (&config_file, "session");
242
243           if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
244             exit (1);
245         }
246       else if (strstr (arg, "--config-file=") == arg)
247         {
248           const char *file;
249
250           check_two_config_files (&config_file, "config-file");
251           
252           file = strchr (arg, '=');
253           ++file;
254
255           if (!_dbus_string_append (&config_file, file))
256             exit (1);
257         }
258       else if (prev_arg &&
259                strcmp (prev_arg, "--config-file") == 0)
260         {
261           check_two_config_files (&config_file, "config-file");
262           
263           if (!_dbus_string_append (&config_file, arg))
264             exit (1);
265         }
266       else if (strcmp (arg, "--config-file") == 0)
267         ; /* wait for next arg */
268       else if (strstr (arg, "--print-address=") == arg)
269         {
270           const char *desc;
271
272           check_two_addr_descriptors (&addr_fd, "print-address");
273           
274           desc = strchr (arg, '=');
275           ++desc;
276
277           if (!_dbus_string_append (&addr_fd, desc))
278             exit (1);
279
280           print_address = TRUE;
281         }
282       else if (prev_arg &&
283                strcmp (prev_arg, "--print-address") == 0)
284         {
285           check_two_addr_descriptors (&addr_fd, "print-address");
286           
287           if (!_dbus_string_append (&addr_fd, arg))
288             exit (1);
289
290           print_address = TRUE;
291         }
292       else if (strcmp (arg, "--print-address") == 0)
293         print_address = TRUE; /* and we'll get the next arg if appropriate */
294       else if (strstr (arg, "--print-pid=") == arg)
295         {
296           const char *desc;
297
298           check_two_pid_descriptors (&pid_fd, "print-pid");
299           
300           desc = strchr (arg, '=');
301           ++desc;
302
303           if (!_dbus_string_append (&pid_fd, desc))
304             exit (1);
305
306           print_pid = TRUE;
307         }
308       else if (prev_arg &&
309                strcmp (prev_arg, "--print-pid") == 0)
310         {
311           check_two_pid_descriptors (&pid_fd, "print-pid");
312           
313           if (!_dbus_string_append (&pid_fd, arg))
314             exit (1);
315           
316           print_pid = TRUE;
317         }
318       else if (strcmp (arg, "--print-pid") == 0)
319         print_pid = TRUE; /* and we'll get the next arg if appropriate */
320       else
321         usage ();
322       
323       prev_arg = arg;
324       
325       ++i;
326     }
327
328   if (_dbus_string_get_length (&config_file) == 0)
329     {
330       fprintf (stderr, "No configuration file specified.\n");
331       usage ();
332     }
333
334   print_addr_fd = -1;
335   if (print_address)
336     {
337       print_addr_fd = 1; /* stdout */
338       if (_dbus_string_get_length (&addr_fd) > 0)
339         {
340           long val;
341           int end;
342           if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
343               end != _dbus_string_get_length (&addr_fd) ||
344               val < 0 || val > _DBUS_INT_MAX)
345             {
346               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
347                        _dbus_string_get_const_data (&addr_fd));
348               exit (1);
349             }
350
351           print_addr_fd = val;
352         }
353     }
354
355   print_pid_fd = -1;
356   if (print_pid)
357     {
358       print_pid_fd = 1; /* stdout */
359       if (_dbus_string_get_length (&pid_fd) > 0)
360         {
361           long val;
362           int end;
363           if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
364               end != _dbus_string_get_length (&pid_fd) ||
365               val < 0 || val > _DBUS_INT_MAX)
366             {
367               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
368                        _dbus_string_get_const_data (&pid_fd));
369               exit (1);
370             }
371
372           print_pid_fd = val;
373         }
374     }
375
376   if (!bus_selinux_init ())
377     {
378       _dbus_warn ("SELinux initialization failed\n");
379       exit (1);
380     }
381
382   dbus_error_init (&error);
383   context = bus_context_new (&config_file, force_fork,
384                              print_addr_fd, print_pid_fd,
385                              &error);
386   _dbus_string_free (&config_file);
387   if (context == NULL)
388     {
389       _dbus_warn ("Failed to start message bus: %s\n",
390                   error.message);
391       dbus_error_free (&error);
392       exit (1);
393     }
394
395   setup_reload_pipe (bus_context_get_loop (context));
396  
397   _dbus_set_signal_handler (SIGHUP, signal_handler);
398   _dbus_set_signal_handler (SIGTERM, signal_handler);
399   
400   _dbus_verbose ("We are on D-Bus...\n");
401   _dbus_loop_run (bus_context_get_loop (context));
402   
403   bus_context_shutdown (context);
404   bus_context_unref (context);
405   bus_selinux_shutdown ();
406
407   return 0;
408 }