2003-04-06 Havoc Pennington <hp@pobox.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 1.2
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]]\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 int
90 main (int argc, char **argv)
91 {
92   DBusError error;
93   DBusString config_file;
94   DBusString addr_fd;
95   const char *prev_arg;
96   int print_addr_fd;
97   int i;
98   dbus_bool_t print_address;
99   
100   if (!_dbus_string_init (&config_file))
101     return 1;
102
103   if (!_dbus_string_init (&addr_fd))
104     return 1;
105   
106   print_address = FALSE;
107   
108   prev_arg = NULL;
109   i = 1;
110   while (i < argc)
111     {
112       const char *arg = argv[i];
113       
114       if (strcmp (arg, "--help") == 0 ||
115           strcmp (arg, "-h") == 0 ||
116           strcmp (arg, "-?") == 0)
117         usage ();
118       else if (strcmp (arg, "--version") == 0)
119         version ();
120       else if (strcmp (arg, "--system") == 0)
121         {
122           check_two_config_files (&config_file, "system");
123
124           if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
125             exit (1);
126         }
127       else if (strcmp (arg, "--session") == 0)
128         {
129           check_two_config_files (&config_file, "session");
130
131           if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
132             exit (1);
133         }
134       else if (strstr (arg, "--config-file=") == arg)
135         {
136           const char *file;
137
138           check_two_config_files (&config_file, "config-file");
139           
140           file = strchr (arg, '=');
141           ++file;
142
143           if (!_dbus_string_append (&config_file, file))
144             exit (1);
145         }
146       else if (prev_arg &&
147                strcmp (prev_arg, "--config-file") == 0)
148         {
149           check_two_config_files (&config_file, "config-file");
150           
151           if (!_dbus_string_append (&config_file, arg))
152             exit (1);
153         }
154       else if (strcmp (arg, "--config-file") == 0)
155         ; /* wait for next arg */
156       else if (strstr (arg, "--print-address=") == arg)
157         {
158           const char *desc;
159
160           check_two_addr_descriptors (&addr_fd, "print-address");
161           
162           desc = strchr (arg, '=');
163           ++desc;
164
165           if (!_dbus_string_append (&addr_fd, desc))
166             exit (1);
167
168           print_address = TRUE;
169         }
170       else if (prev_arg &&
171                strcmp (prev_arg, "--print-address") == 0)
172         {
173           check_two_addr_descriptors (&addr_fd, "print-address");
174           
175           if (!_dbus_string_append (&addr_fd, arg))
176             exit (1);
177
178           print_address = TRUE;
179         }
180       else if (strcmp (arg, "--print-address") == 0)
181         print_address = TRUE; /* and we'll get the next arg if appropriate */
182       else
183         usage ();
184       
185       prev_arg = arg;
186       
187       ++i;
188     }
189
190   if (_dbus_string_get_length (&config_file) == 0)
191     {
192       fprintf (stderr, "No configuration file specified.\n");
193       usage ();
194     }
195
196   print_addr_fd = -1;
197   if (print_address)
198     {
199       print_addr_fd = 1; /* stdout */
200       if (_dbus_string_get_length (&addr_fd) > 0)
201         {
202           long val;
203           int end;
204           if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
205               end != _dbus_string_get_length (&addr_fd) ||
206               val < 0 || val > _DBUS_INT_MAX)
207             {
208               fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
209                        _dbus_string_get_const_data (&addr_fd));
210               exit (1);
211             }
212
213           print_addr_fd = val;
214         }
215     }
216   
217   dbus_error_init (&error);
218   context = bus_context_new (&config_file, print_addr_fd, &error);
219   _dbus_string_free (&config_file);
220   if (context == NULL)
221     {
222       _dbus_warn ("Failed to start message bus: %s\n",
223                   error.message);
224       dbus_error_free (&error);
225       exit (1);
226     }
227   
228   /* FIXME we have to handle this properly below _dbus_set_signal_handler (SIGHUP, signal_handler); */
229   _dbus_set_signal_handler (SIGTERM, signal_handler);
230   
231   _dbus_verbose ("We are on D-Bus...\n");
232   _dbus_loop_run (bus_context_get_loop (context));
233   
234   bus_context_shutdown (context);
235   bus_context_unref (context);
236
237   /* If we exited on TERM we just exit, if we exited on
238    * HUP we restart the daemon.
239    */
240   if (got_sighup)
241     {
242       /* FIXME execv (argv) basically */
243     }
244   
245   return 0;
246 }