1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <dbus/dbus-internals.h>
28 #include <dbus/dbus-watch.h>
40 static BusContext *context;
42 static int reload_pipe[2];
43 #define RELOAD_READ_END 0
44 #define RELOAD_WRITE_END 1
46 static void close_reload_pipe (void);
49 signal_handler (int sig)
54 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
56 /* explicit fall-through */
57 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
62 _dbus_string_init_const (&str, "foo");
63 if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
64 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
66 _dbus_warn ("Unable to write to reload pipe.\n");
74 _dbus_loop_quit (bus_context_get_loop (context));
83 fprintf (stderr, DBUS_DAEMON_NAME " [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork] [--introspect]\n");
90 printf ("D-Bus Message Bus Daemon %s\n"
91 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
92 "This is free software; see the source for copying conditions.\n"
93 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
102 const char *v_STRING;
104 if (!_dbus_string_init (&xml))
107 if (!bus_driver_generate_introspect_string (&xml))
109 _dbus_string_free (&xml);
113 v_STRING = _dbus_string_get_const_data (&xml);
114 printf ("%s\n", v_STRING);
119 _dbus_warn ("Can not introspect - Out of memory\n");
123 check_two_config_files (const DBusString *config_file,
124 const char *extra_arg)
126 if (_dbus_string_get_length (config_file) > 0)
128 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
129 extra_arg, _dbus_string_get_const_data (config_file));
135 check_two_addr_descriptors (const DBusString *addr_fd,
136 const char *extra_arg)
138 if (_dbus_string_get_length (addr_fd) > 0)
140 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
141 extra_arg, _dbus_string_get_const_data (addr_fd));
147 check_two_pid_descriptors (const DBusString *pid_fd,
148 const char *extra_arg)
150 if (_dbus_string_get_length (pid_fd) > 0)
152 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
153 extra_arg, _dbus_string_get_const_data (pid_fd));
159 handle_reload_watch (DBusWatch *watch,
166 while (!_dbus_string_init (&str))
167 _dbus_wait_for_memory ();
169 if ((reload_pipe[RELOAD_READ_END] > 0) &&
170 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
172 _dbus_warn ("Couldn't read from reload pipe.\n");
173 close_reload_pipe ();
176 _dbus_string_free (&str);
178 /* this can only fail if we don't understand the config file
179 * or OOM. Either way we should just stick with the currently
182 dbus_error_init (&error);
183 if (! bus_context_reload_config (context, &error))
185 _DBUS_ASSERT_ERROR_IS_SET (&error);
186 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
187 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
188 _dbus_warn ("Unable to reload configuration: %s\n",
190 dbus_error_free (&error);
196 reload_watch_callback (DBusWatch *watch,
197 unsigned int condition,
200 return dbus_watch_handle (watch, condition);
204 setup_reload_pipe (DBusLoop *loop)
209 dbus_error_init (&error);
211 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
214 _dbus_warn ("Unable to create reload pipe: %s\n",
216 dbus_error_free (&error);
220 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
221 DBUS_WATCH_READABLE, TRUE,
222 handle_reload_watch, NULL, NULL);
226 _dbus_warn ("Unable to create reload watch: %s\n",
228 dbus_error_free (&error);
232 if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
235 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
237 dbus_error_free (&error);
244 close_reload_pipe (void)
246 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
247 reload_pipe[RELOAD_READ_END] = -1;
249 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
250 reload_pipe[RELOAD_WRITE_END] = -1;
254 main (int argc, char **argv)
257 DBusString config_file;
260 const char *prev_arg;
261 DBusPipe print_addr_pipe;
262 DBusPipe print_pid_pipe;
264 dbus_bool_t print_address;
265 dbus_bool_t print_pid;
266 dbus_bool_t is_session_bus;
269 if (!_dbus_string_init (&config_file))
272 if (!_dbus_string_init (&addr_fd))
275 if (!_dbus_string_init (&pid_fd))
278 print_address = FALSE;
280 is_session_bus = FALSE;
281 force_fork = FORK_FOLLOW_CONFIG_FILE;
287 const char *arg = argv[i];
289 if (strcmp (arg, "--help") == 0 ||
290 strcmp (arg, "-h") == 0 ||
291 strcmp (arg, "-?") == 0)
293 else if (strcmp (arg, "--version") == 0)
295 else if (strcmp (arg, "--introspect") == 0)
297 else if (strcmp (arg, "--nofork") == 0)
298 force_fork = FORK_NEVER;
299 else if (strcmp (arg, "--fork") == 0)
300 force_fork = FORK_ALWAYS;
301 else if (strcmp (arg, "--system") == 0)
303 check_two_config_files (&config_file, "system");
305 if (!_dbus_append_system_config_file (&config_file))
308 else if (strcmp (arg, "--session") == 0)
310 check_two_config_files (&config_file, "session");
312 if (!_dbus_append_session_config_file (&config_file))
315 else if (strstr (arg, "--config-file=") == arg)
319 check_two_config_files (&config_file, "config-file");
321 file = strchr (arg, '=');
324 if (!_dbus_string_append (&config_file, file))
328 strcmp (prev_arg, "--config-file") == 0)
330 check_two_config_files (&config_file, "config-file");
332 if (!_dbus_string_append (&config_file, arg))
335 else if (strcmp (arg, "--config-file") == 0)
336 ; /* wait for next arg */
337 else if (strstr (arg, "--print-address=") == arg)
341 check_two_addr_descriptors (&addr_fd, "print-address");
343 desc = strchr (arg, '=');
346 if (!_dbus_string_append (&addr_fd, desc))
349 print_address = TRUE;
352 strcmp (prev_arg, "--print-address") == 0)
354 check_two_addr_descriptors (&addr_fd, "print-address");
356 if (!_dbus_string_append (&addr_fd, arg))
359 print_address = TRUE;
361 else if (strcmp (arg, "--print-address") == 0)
362 print_address = TRUE; /* and we'll get the next arg if appropriate */
363 else if (strstr (arg, "--print-pid=") == arg)
367 check_two_pid_descriptors (&pid_fd, "print-pid");
369 desc = strchr (arg, '=');
372 if (!_dbus_string_append (&pid_fd, desc))
378 strcmp (prev_arg, "--print-pid") == 0)
380 check_two_pid_descriptors (&pid_fd, "print-pid");
382 if (!_dbus_string_append (&pid_fd, arg))
387 else if (strcmp (arg, "--print-pid") == 0)
388 print_pid = TRUE; /* and we'll get the next arg if appropriate */
397 if (_dbus_string_get_length (&config_file) == 0)
399 fprintf (stderr, "No configuration file specified.\n");
403 _dbus_pipe_invalidate (&print_addr_pipe);
406 _dbus_pipe_init_stdout (&print_addr_pipe);
407 if (_dbus_string_get_length (&addr_fd) > 0)
411 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
412 end != _dbus_string_get_length (&addr_fd) ||
413 val < 0 || val > _DBUS_INT_MAX)
415 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
416 _dbus_string_get_const_data (&addr_fd));
420 _dbus_pipe_init (&print_addr_pipe, val);
423 _dbus_string_free (&addr_fd);
425 _dbus_pipe_invalidate (&print_pid_pipe);
428 _dbus_pipe_init_stdout (&print_pid_pipe);
429 if (_dbus_string_get_length (&pid_fd) > 0)
433 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
434 end != _dbus_string_get_length (&pid_fd) ||
435 val < 0 || val > _DBUS_INT_MAX)
437 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
438 _dbus_string_get_const_data (&pid_fd));
442 _dbus_pipe_init (&print_pid_pipe, val);
445 _dbus_string_free (&pid_fd);
447 if (!bus_selinux_pre_init ())
449 _dbus_warn ("SELinux pre-initialization failed\n");
453 dbus_error_init (&error);
454 context = bus_context_new (&config_file, force_fork,
455 &print_addr_pipe, &print_pid_pipe,
457 _dbus_string_free (&config_file);
460 _dbus_warn ("Failed to start message bus: %s\n",
462 dbus_error_free (&error);
466 is_session_bus = bus_context_get_type(context) != NULL
467 && strcmp(bus_context_get_type(context),"session") == 0;
470 _dbus_daemon_publish_session_bus_address (bus_context_get_address (context));
472 /* bus_context_new() closes the print_addr_pipe and
476 setup_reload_pipe (bus_context_get_loop (context));
479 _dbus_set_signal_handler (SIGHUP, signal_handler);
482 _dbus_set_signal_handler (SIGTERM, signal_handler);
484 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
485 _dbus_set_signal_handler (SIGIO, signal_handler);
486 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
488 _dbus_verbose ("We are on D-Bus...\n");
489 _dbus_loop_run (bus_context_get_loop (context));
491 bus_context_shutdown (context);
492 bus_context_unref (context);
493 bus_selinux_shutdown ();
496 _dbus_daemon_unpublish_session_bus_address ();