2003-12-02 Richard Hult <richard@imendio.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 <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <errno.h>
30
31 static BusContext *context;
32 static dbus_bool_t got_sighup = FALSE;
33
34 static void
35 signal_handler (int sig)
36 {
37   switch (sig)
38     {
39     case SIGHUP:
40       got_sighup = TRUE;
41     case SIGTERM:
42       _dbus_loop_quit (bus_context_get_loop (context));
43       break;
44     }
45 }
46
47 static void
48 usage (void)
49 {
50   fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork]\n");
51   exit (1);
52 }
53
54 static void
55 version (void)
56 {
57   printf ("D-BUS Message Bus Daemon %s\n"
58           "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
59           "This is free software; see the source for copying conditions.\n"
60           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
61           VERSION);
62   exit (0);
63 }
64
65 static void
66 check_two_config_files (const DBusString *config_file,
67                         const char       *extra_arg)
68 {
69   if (_dbus_string_get_length (config_file) > 0)
70     {
71       fprintf (stderr, "--%s specified but configuration file %s already requested\n",
72                extra_arg, _dbus_string_get_const_data (config_file));
73       exit (1);
74     }
75 }
76
77 static void
78 check_two_addr_descriptors (const DBusString *addr_fd,
79                             const char       *extra_arg)
80 {
81   if (_dbus_string_get_length (addr_fd) > 0)
82     {
83       fprintf (stderr, "--%s specified but printing address to %s already requested\n",
84                extra_arg, _dbus_string_get_const_data (addr_fd));
85       exit (1);
86     }
87 }
88
89 static void
90 check_two_pid_descriptors (const DBusString *pid_fd,
91                            const char       *extra_arg)
92 {
93   if (_dbus_string_get_length (pid_fd) > 0)
94     {
95       fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
96                extra_arg, _dbus_string_get_const_data (pid_fd));
97       exit (1);
98     }
99 }
100
101 int
102 main (int argc, char **argv)
103 {
104   DBusError error;
105   DBusString config_file;
106   DBusString addr_fd;
107   DBusString pid_fd;
108   const char *prev_arg;
109   int print_addr_fd;
110   int print_pid_fd;
111   int i;
112   dbus_bool_t print_address;
113   dbus_bool_t print_pid;
114   dbus_bool_t force_fork;
115   
116   if (!_dbus_string_init (&config_file))
117     return 1;
118
119   if (!_dbus_string_init (&addr_fd))
120     return 1;
121
122   if (!_dbus_string_init (&pid_fd))
123     return 1;
124   
125   print_address = FALSE;
126   print_pid = FALSE;
127   force_fork = FALSE;
128   
129   prev_arg = NULL;
130   i = 1;
131   while (i < argc)
132     {
133       const char *arg = argv[i];
134       
135       if (strcmp (arg, "--help") == 0 ||
136           strcmp (arg, "-h") == 0 ||
137           strcmp (arg, "-?") == 0)
138         usage ();
139       else if (strcmp (arg, "--version") == 0)
140         version ();
141       else if (strcmp (arg, "--fork") == 0)
142         force_fork = TRUE;
143       else if (strcmp (arg, "--system") == 0)
144         {
145           check_two_config_files (&config_file, "system");
146
147           if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
148             exit (1);
149         }
150       else if (strcmp (arg, "--session") == 0)
151         {
152           check_two_config_files (&config_file, "session");
153
154           if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
155             exit (1);
156         }
157       else if (strstr (arg, "--config-file=") == arg)
158         {
159           const char *file;
160
161           check_two_config_files (&config_file, "config-file");
162           
163           file = strchr (arg, '=');
164           ++file;
165
166           if (!_dbus_string_append (&config_file, file))
167             exit (1);
168         }
169       else if (prev_arg &&
170                strcmp (prev_arg, "--config-file") == 0)
171         {
172           check_two_config_files (&config_file, "config-file");
173           
174           if (!_dbus_string_append (&config_file, arg))
175             exit (1);
176         }
177       else if (strcmp (arg, "--config-file") == 0)
178         ; /* wait for next arg */
179       else if (strstr (arg, "--print-address=") == arg)
180         {
181           const char *desc;
182
183           check_two_addr_descriptors (&addr_fd, "print-address");
184           
185           desc = strchr (arg, '=');
186           ++desc;
187
188           if (!_dbus_string_append (&addr_fd, desc))
189             exit (1);
190
191           print_address = TRUE;
192         }
193       else if (prev_arg &&
194                strcmp (prev_arg, "--print-address") == 0)
195         {
196           check_two_addr_descriptors (&addr_fd, "print-address");
197           
198           if (!_dbus_string_append (&addr_fd, arg))
199             exit (1);
200
201           print_address = TRUE;
202         }
203       else if (strcmp (arg, "--print-address") == 0)
204         print_address = TRUE; /* and we'll get the next arg if appropriate */
205       else if (strstr (arg, "--print-pid=") == arg)
206         {
207           const char *desc;
208
209           check_two_pid_descriptors (&pid_fd, "print-pid");
210           
211           desc = strchr (arg, '=');
212           ++desc;
213
214           if (!_dbus_string_append (&pid_fd, desc))
215             exit (1);
216
217           print_pid = TRUE;
218         }
219       else if (prev_arg &&
220                strcmp (prev_arg, "--print-pid") == 0)
221         {
222           check_two_pid_descriptors (&pid_fd, "print-pid");
223           
224           if (!_dbus_string_append (&pid_fd, arg))
225             exit (1);
226           
227           print_pid = TRUE;
228         }
229       else if (strcmp (arg, "--print-pid") == 0)
230         print_pid = TRUE; /* and we'll get the next arg if appropriate */
231       else
232         usage ();
233       
234       prev_arg = arg;
235       
236       ++i;
237     }
238
239   if (_dbus_string_get_length (&config_file) == 0)
240     {
241       fprintf (stderr, "No configuration file specified.\n");
242       usage ();
243     }
244
245   print_addr_fd = -1;
246   if (print_address)
247     {
248       print_addr_fd = 1; /* stdout */
249       if (_dbus_string_get_length (&addr_fd) > 0)
250         {
251           long val;
252           int end;
253           if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
254               end != _dbus_string_get_length (&addr_fd) ||
255               val < 0 || val > _DBUS_INT_MAX)
256             {
257               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
258                        _dbus_string_get_const_data (&addr_fd));
259               exit (1);
260             }
261
262           print_addr_fd = val;
263         }
264     }
265
266   print_pid_fd = -1;
267   if (print_pid)
268     {
269       print_pid_fd = 1; /* stdout */
270       if (_dbus_string_get_length (&pid_fd) > 0)
271         {
272           long val;
273           int end;
274           if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
275               end != _dbus_string_get_length (&pid_fd) ||
276               val < 0 || val > _DBUS_INT_MAX)
277             {
278               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
279                        _dbus_string_get_const_data (&pid_fd));
280               exit (1);
281             }
282
283           print_pid_fd = val;
284         }
285     }
286   
287   dbus_error_init (&error);
288   context = bus_context_new (&config_file, force_fork,
289                              print_addr_fd, print_pid_fd,
290                              &error);
291   _dbus_string_free (&config_file);
292   if (context == NULL)
293     {
294       _dbus_warn ("Failed to start message bus: %s\n",
295                   error.message);
296       dbus_error_free (&error);
297       exit (1);
298     }
299   
300   /* FIXME we have to handle this properly below _dbus_set_signal_handler (SIGHUP, signal_handler); */
301   _dbus_set_signal_handler (SIGTERM, signal_handler);
302   
303   _dbus_verbose ("We are on D-Bus...\n");
304   _dbus_loop_run (bus_context_get_loop (context));
305   
306   bus_context_shutdown (context);
307   bus_context_unref (context);
308
309   /* If we exited on TERM we just exit, if we exited on
310    * HUP we restart the daemon.
311    */
312   if (got_sighup)
313     {
314       /* FIXME execv (argv) basically */
315     }
316   
317   return 0;
318 }