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.1
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
25 #include <dbus/dbus-internals.h>
26 #include <dbus/dbus-watch.h>
34 static BusContext *context;
36 static int reload_pipe[2];
37 #define RELOAD_READ_END 0
38 #define RELOAD_WRITE_END 1
42 signal_handler (int sig)
48 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
50 /* explicit fall-through */
51 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
53 _dbus_string_init_const (&str, "foo");
54 if (!_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
56 _dbus_warn ("Unable to write to reload pipe.\n");
62 _dbus_loop_quit (bus_context_get_loop (context));
70 fprintf (stderr, DAEMON_NAME " [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork] [--introspect]\n");
77 printf ("D-Bus Message Bus Daemon %s\n"
78 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
79 "This is free software; see the source for copying conditions.\n"
80 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
91 if (!_dbus_string_init (&xml))
94 if (!bus_driver_generate_introspect_string (&xml))
96 _dbus_string_free (&xml);
100 v_STRING = _dbus_string_get_const_data (&xml);
101 printf ("%s\n", v_STRING);
106 _dbus_warn ("Can not introspect - Out of memory\n");
110 check_two_config_files (const DBusString *config_file,
111 const char *extra_arg)
113 if (_dbus_string_get_length (config_file) > 0)
115 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
116 extra_arg, _dbus_string_get_const_data (config_file));
122 check_two_addr_descriptors (const DBusString *addr_fd,
123 const char *extra_arg)
125 if (_dbus_string_get_length (addr_fd) > 0)
127 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
128 extra_arg, _dbus_string_get_const_data (addr_fd));
134 check_two_pid_descriptors (const DBusString *pid_fd,
135 const char *extra_arg)
137 if (_dbus_string_get_length (pid_fd) > 0)
139 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
140 extra_arg, _dbus_string_get_const_data (pid_fd));
146 handle_reload_watch (DBusWatch *watch,
152 _dbus_string_init (&str);
153 if (_dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
155 _dbus_warn ("Couldn't read from reload pipe.\n");
158 _dbus_string_free (&str);
160 dbus_error_init (&error);
161 if (! bus_context_reload_config (context, &error))
163 _dbus_warn ("Unable to reload configuration: %s\n",
165 dbus_error_free (&error);
172 reload_watch_callback (DBusWatch *watch,
173 unsigned int condition,
176 return dbus_watch_handle (watch, condition);
180 setup_reload_pipe (DBusLoop *loop)
185 dbus_error_init (&error);
187 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
190 _dbus_warn ("Unable to create reload pipe: %s\n",
192 dbus_error_free (&error);
196 _dbus_fd_set_close_on_exec (reload_pipe[0]);
197 _dbus_fd_set_close_on_exec (reload_pipe[1]);
199 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
200 DBUS_WATCH_READABLE, TRUE,
201 handle_reload_watch, NULL, NULL);
205 _dbus_warn ("Unable to create reload watch: %s\n",
207 dbus_error_free (&error);
211 if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
214 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
216 dbus_error_free (&error);
223 main (int argc, char **argv)
226 DBusString config_file;
229 const char *prev_arg;
233 dbus_bool_t print_address;
234 dbus_bool_t print_pid;
237 if (!_dbus_string_init (&config_file))
240 if (!_dbus_string_init (&addr_fd))
243 if (!_dbus_string_init (&pid_fd))
246 print_address = FALSE;
248 force_fork = FORK_FOLLOW_CONFIG_FILE;
254 const char *arg = argv[i];
256 if (strcmp (arg, "--help") == 0 ||
257 strcmp (arg, "-h") == 0 ||
258 strcmp (arg, "-?") == 0)
260 else if (strcmp (arg, "--version") == 0)
262 else if (strcmp (arg, "--introspect") == 0)
264 else if (strcmp (arg, "--nofork") == 0)
265 force_fork = FORK_NEVER;
266 else if (strcmp (arg, "--fork") == 0)
267 force_fork = FORK_ALWAYS;
268 else if (strcmp (arg, "--system") == 0)
270 check_two_config_files (&config_file, "system");
272 if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
275 else if (strcmp (arg, "--session") == 0)
277 check_two_config_files (&config_file, "session");
279 if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
282 else if (strstr (arg, "--config-file=") == arg)
286 check_two_config_files (&config_file, "config-file");
288 file = strchr (arg, '=');
291 if (!_dbus_string_append (&config_file, file))
295 strcmp (prev_arg, "--config-file") == 0)
297 check_two_config_files (&config_file, "config-file");
299 if (!_dbus_string_append (&config_file, arg))
302 else if (strcmp (arg, "--config-file") == 0)
303 ; /* wait for next arg */
304 else if (strstr (arg, "--print-address=") == arg)
308 check_two_addr_descriptors (&addr_fd, "print-address");
310 desc = strchr (arg, '=');
313 if (!_dbus_string_append (&addr_fd, desc))
316 print_address = TRUE;
319 strcmp (prev_arg, "--print-address") == 0)
321 check_two_addr_descriptors (&addr_fd, "print-address");
323 if (!_dbus_string_append (&addr_fd, arg))
326 print_address = TRUE;
328 else if (strcmp (arg, "--print-address") == 0)
329 print_address = TRUE; /* and we'll get the next arg if appropriate */
330 else if (strstr (arg, "--print-pid=") == arg)
334 check_two_pid_descriptors (&pid_fd, "print-pid");
336 desc = strchr (arg, '=');
339 if (!_dbus_string_append (&pid_fd, desc))
345 strcmp (prev_arg, "--print-pid") == 0)
347 check_two_pid_descriptors (&pid_fd, "print-pid");
349 if (!_dbus_string_append (&pid_fd, arg))
354 else if (strcmp (arg, "--print-pid") == 0)
355 print_pid = TRUE; /* and we'll get the next arg if appropriate */
364 if (_dbus_string_get_length (&config_file) == 0)
366 fprintf (stderr, "No configuration file specified.\n");
373 print_addr_fd = 1; /* stdout */
374 if (_dbus_string_get_length (&addr_fd) > 0)
378 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
379 end != _dbus_string_get_length (&addr_fd) ||
380 val < 0 || val > _DBUS_INT_MAX)
382 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
383 _dbus_string_get_const_data (&addr_fd));
390 _dbus_string_free (&addr_fd);
395 print_pid_fd = 1; /* stdout */
396 if (_dbus_string_get_length (&pid_fd) > 0)
400 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
401 end != _dbus_string_get_length (&pid_fd) ||
402 val < 0 || val > _DBUS_INT_MAX)
404 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
405 _dbus_string_get_const_data (&pid_fd));
412 _dbus_string_free (&pid_fd);
414 if (!bus_selinux_pre_init ())
416 _dbus_warn ("SELinux pre-initialization failed\n");
420 dbus_error_init (&error);
421 context = bus_context_new (&config_file, force_fork,
422 print_addr_fd, print_pid_fd,
424 _dbus_string_free (&config_file);
427 _dbus_warn ("Failed to start message bus: %s\n",
429 dbus_error_free (&error);
433 setup_reload_pipe (bus_context_get_loop (context));
435 _dbus_set_signal_handler (SIGHUP, signal_handler);
436 _dbus_set_signal_handler (SIGTERM, signal_handler);
437 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
438 _dbus_set_signal_handler (SIGIO, signal_handler);
439 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
441 _dbus_verbose ("We are on D-Bus...\n");
442 _dbus_loop_run (bus_context_get_loop (context));
444 bus_context_shutdown (context);
445 bus_context_unref (context);
446 bus_selinux_shutdown ();