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>
25 #include <dbus/dbus-watch.h>
32 static BusContext *context;
34 static int reload_pipe[2];
35 #define RELOAD_READ_END 0
36 #define RELOAD_WRITE_END 1
40 signal_handler (int sig)
47 _dbus_string_init_const (&str, "foo");
48 if (!_dbus_write (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
50 _dbus_warn ("Unable to write to reload pipe.\n");
56 _dbus_loop_quit (bus_context_get_loop (context));
64 fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork]\n");
71 printf ("D-BUS Message Bus Daemon %s\n"
72 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
73 "This is free software; see the source for copying conditions.\n"
74 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
80 check_two_config_files (const DBusString *config_file,
81 const char *extra_arg)
83 if (_dbus_string_get_length (config_file) > 0)
85 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
86 extra_arg, _dbus_string_get_const_data (config_file));
92 check_two_addr_descriptors (const DBusString *addr_fd,
93 const char *extra_arg)
95 if (_dbus_string_get_length (addr_fd) > 0)
97 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
98 extra_arg, _dbus_string_get_const_data (addr_fd));
104 check_two_pid_descriptors (const DBusString *pid_fd,
105 const char *extra_arg)
107 if (_dbus_string_get_length (pid_fd) > 0)
109 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
110 extra_arg, _dbus_string_get_const_data (pid_fd));
116 handle_reload_watch (DBusWatch *watch,
122 _dbus_string_init (&str);
123 if (_dbus_read (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
125 _dbus_warn ("Couldn't read from reload pipe.\n");
128 _dbus_string_free (&str);
130 dbus_error_init (&error);
131 if (! bus_context_reload_config (context, &error))
133 _dbus_warn ("Unable to reload configuration: %s\n",
135 dbus_error_free (&error);
142 reload_watch_callback (DBusWatch *watch,
143 unsigned int condition,
146 return dbus_watch_handle (watch, condition);
150 setup_reload_pipe (DBusLoop *loop)
155 dbus_error_init (&error);
157 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
160 _dbus_warn ("Unable to create reload pipe: %s\n",
162 dbus_error_free (&error);
166 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
167 DBUS_WATCH_READABLE, TRUE,
168 handle_reload_watch, NULL, NULL);
172 _dbus_warn ("Unable to create reload watch: %s\n",
174 dbus_error_free (&error);
178 if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
181 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
183 dbus_error_free (&error);
190 main (int argc, char **argv)
193 DBusString config_file;
196 const char *prev_arg;
200 dbus_bool_t print_address;
201 dbus_bool_t print_pid;
202 dbus_bool_t force_fork;
204 if (!_dbus_string_init (&config_file))
207 if (!_dbus_string_init (&addr_fd))
210 if (!_dbus_string_init (&pid_fd))
213 print_address = FALSE;
221 const char *arg = argv[i];
223 if (strcmp (arg, "--help") == 0 ||
224 strcmp (arg, "-h") == 0 ||
225 strcmp (arg, "-?") == 0)
227 else if (strcmp (arg, "--version") == 0)
229 else if (strcmp (arg, "--fork") == 0)
231 else if (strcmp (arg, "--system") == 0)
233 check_two_config_files (&config_file, "system");
235 if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
238 else if (strcmp (arg, "--session") == 0)
240 check_two_config_files (&config_file, "session");
242 if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
245 else if (strstr (arg, "--config-file=") == arg)
249 check_two_config_files (&config_file, "config-file");
251 file = strchr (arg, '=');
254 if (!_dbus_string_append (&config_file, file))
258 strcmp (prev_arg, "--config-file") == 0)
260 check_two_config_files (&config_file, "config-file");
262 if (!_dbus_string_append (&config_file, arg))
265 else if (strcmp (arg, "--config-file") == 0)
266 ; /* wait for next arg */
267 else if (strstr (arg, "--print-address=") == arg)
271 check_two_addr_descriptors (&addr_fd, "print-address");
273 desc = strchr (arg, '=');
276 if (!_dbus_string_append (&addr_fd, desc))
279 print_address = TRUE;
282 strcmp (prev_arg, "--print-address") == 0)
284 check_two_addr_descriptors (&addr_fd, "print-address");
286 if (!_dbus_string_append (&addr_fd, arg))
289 print_address = TRUE;
291 else if (strcmp (arg, "--print-address") == 0)
292 print_address = TRUE; /* and we'll get the next arg if appropriate */
293 else if (strstr (arg, "--print-pid=") == arg)
297 check_two_pid_descriptors (&pid_fd, "print-pid");
299 desc = strchr (arg, '=');
302 if (!_dbus_string_append (&pid_fd, desc))
308 strcmp (prev_arg, "--print-pid") == 0)
310 check_two_pid_descriptors (&pid_fd, "print-pid");
312 if (!_dbus_string_append (&pid_fd, arg))
317 else if (strcmp (arg, "--print-pid") == 0)
318 print_pid = TRUE; /* and we'll get the next arg if appropriate */
327 if (_dbus_string_get_length (&config_file) == 0)
329 fprintf (stderr, "No configuration file specified.\n");
336 print_addr_fd = 1; /* stdout */
337 if (_dbus_string_get_length (&addr_fd) > 0)
341 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
342 end != _dbus_string_get_length (&addr_fd) ||
343 val < 0 || val > _DBUS_INT_MAX)
345 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
346 _dbus_string_get_const_data (&addr_fd));
357 print_pid_fd = 1; /* stdout */
358 if (_dbus_string_get_length (&pid_fd) > 0)
362 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
363 end != _dbus_string_get_length (&pid_fd) ||
364 val < 0 || val > _DBUS_INT_MAX)
366 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
367 _dbus_string_get_const_data (&pid_fd));
375 dbus_error_init (&error);
376 context = bus_context_new (&config_file, force_fork,
377 print_addr_fd, print_pid_fd,
379 _dbus_string_free (&config_file);
382 _dbus_warn ("Failed to start message bus: %s\n",
384 dbus_error_free (&error);
388 setup_reload_pipe (bus_context_get_loop (context));
390 _dbus_set_signal_handler (SIGHUP, signal_handler);
391 _dbus_set_signal_handler (SIGTERM, signal_handler);
393 _dbus_verbose ("We are on D-Bus...\n");
394 _dbus_loop_run (bus_context_get_loop (context));
396 bus_context_shutdown (context);
397 bus_context_unref (context);