1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* main.c main() for message bus
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.0
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.
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.
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
24 #include <dbus/dbus-internals.h>
31 static BusContext *context;
34 signal_handler (int sig)
41 /* FIXME: We shouldn't be reloading the config in the
42 signal handler. We should use a pipe or something to
43 make the reload happen in the main loop. */
44 dbus_error_init (&error);
45 if (!bus_context_reload_config (context, &error))
47 _dbus_warn ("Unable to reload configuration: %s\n",
49 dbus_error_free (&error);
55 _dbus_loop_quit (bus_context_get_loop (context));
63 fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork]\n");
70 printf ("D-BUS Message Bus Daemon %s\n"
71 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
72 "This is free software; see the source for copying conditions.\n"
73 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
79 check_two_config_files (const DBusString *config_file,
80 const char *extra_arg)
82 if (_dbus_string_get_length (config_file) > 0)
84 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
85 extra_arg, _dbus_string_get_const_data (config_file));
91 check_two_addr_descriptors (const DBusString *addr_fd,
92 const char *extra_arg)
94 if (_dbus_string_get_length (addr_fd) > 0)
96 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
97 extra_arg, _dbus_string_get_const_data (addr_fd));
103 check_two_pid_descriptors (const DBusString *pid_fd,
104 const char *extra_arg)
106 if (_dbus_string_get_length (pid_fd) > 0)
108 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
109 extra_arg, _dbus_string_get_const_data (pid_fd));
115 main (int argc, char **argv)
118 DBusString config_file;
121 const char *prev_arg;
125 dbus_bool_t print_address;
126 dbus_bool_t print_pid;
127 dbus_bool_t force_fork;
129 if (!_dbus_string_init (&config_file))
132 if (!_dbus_string_init (&addr_fd))
135 if (!_dbus_string_init (&pid_fd))
138 print_address = FALSE;
146 const char *arg = argv[i];
148 if (strcmp (arg, "--help") == 0 ||
149 strcmp (arg, "-h") == 0 ||
150 strcmp (arg, "-?") == 0)
152 else if (strcmp (arg, "--version") == 0)
154 else if (strcmp (arg, "--fork") == 0)
156 else if (strcmp (arg, "--system") == 0)
158 check_two_config_files (&config_file, "system");
160 if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
163 else if (strcmp (arg, "--session") == 0)
165 check_two_config_files (&config_file, "session");
167 if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
170 else if (strstr (arg, "--config-file=") == arg)
174 check_two_config_files (&config_file, "config-file");
176 file = strchr (arg, '=');
179 if (!_dbus_string_append (&config_file, file))
183 strcmp (prev_arg, "--config-file") == 0)
185 check_two_config_files (&config_file, "config-file");
187 if (!_dbus_string_append (&config_file, arg))
190 else if (strcmp (arg, "--config-file") == 0)
191 ; /* wait for next arg */
192 else if (strstr (arg, "--print-address=") == arg)
196 check_two_addr_descriptors (&addr_fd, "print-address");
198 desc = strchr (arg, '=');
201 if (!_dbus_string_append (&addr_fd, desc))
204 print_address = TRUE;
207 strcmp (prev_arg, "--print-address") == 0)
209 check_two_addr_descriptors (&addr_fd, "print-address");
211 if (!_dbus_string_append (&addr_fd, arg))
214 print_address = TRUE;
216 else if (strcmp (arg, "--print-address") == 0)
217 print_address = TRUE; /* and we'll get the next arg if appropriate */
218 else if (strstr (arg, "--print-pid=") == arg)
222 check_two_pid_descriptors (&pid_fd, "print-pid");
224 desc = strchr (arg, '=');
227 if (!_dbus_string_append (&pid_fd, desc))
233 strcmp (prev_arg, "--print-pid") == 0)
235 check_two_pid_descriptors (&pid_fd, "print-pid");
237 if (!_dbus_string_append (&pid_fd, arg))
242 else if (strcmp (arg, "--print-pid") == 0)
243 print_pid = TRUE; /* and we'll get the next arg if appropriate */
252 if (_dbus_string_get_length (&config_file) == 0)
254 fprintf (stderr, "No configuration file specified.\n");
261 print_addr_fd = 1; /* stdout */
262 if (_dbus_string_get_length (&addr_fd) > 0)
266 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
267 end != _dbus_string_get_length (&addr_fd) ||
268 val < 0 || val > _DBUS_INT_MAX)
270 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
271 _dbus_string_get_const_data (&addr_fd));
282 print_pid_fd = 1; /* stdout */
283 if (_dbus_string_get_length (&pid_fd) > 0)
287 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
288 end != _dbus_string_get_length (&pid_fd) ||
289 val < 0 || val > _DBUS_INT_MAX)
291 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
292 _dbus_string_get_const_data (&pid_fd));
300 dbus_error_init (&error);
301 context = bus_context_new (&config_file, force_fork,
302 print_addr_fd, print_pid_fd,
304 _dbus_string_free (&config_file);
307 _dbus_warn ("Failed to start message bus: %s\n",
309 dbus_error_free (&error);
313 _dbus_set_signal_handler (SIGHUP, signal_handler);
314 _dbus_set_signal_handler (SIGTERM, signal_handler);
316 _dbus_verbose ("We are on D-Bus...\n");
317 _dbus_loop_run (bus_context_get_loop (context));
319 bus_context_shutdown (context);
320 bus_context_unref (context);