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 (DBusWatch **);
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 /* If we receive SIGHUP often enough to fill the pipe buffer (4096
67 * times on old Linux, 65536 on modern Linux) before it can be
68 * drained, let's just warn and ignore. The configuration will be
69 * reloaded while draining the pipe buffer, which is what we
70 * wanted. It's harmless that it will be reloaded fewer times than
71 * we asked for, since the reload is delayed anyway, so new changes
72 * will be picked up. */
73 _dbus_warn ("Unable to write to reload pipe.\n");
84 fprintf (stderr, DBUS_DAEMON_NAME " [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork] [--introspect] [--address=ADDRESS] [--systemd-activation]\n");
91 printf ("D-Bus Message Bus Daemon %s\n"
92 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
93 "This is free software; see the source for copying conditions.\n"
94 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
103 const char *v_STRING;
105 if (!_dbus_string_init (&xml))
108 if (!bus_driver_generate_introspect_string (&xml))
110 _dbus_string_free (&xml);
114 v_STRING = _dbus_string_get_const_data (&xml);
115 printf ("%s\n", v_STRING);
120 _dbus_warn ("Can not introspect - Out of memory\n");
125 check_two_config_files (const DBusString *config_file,
126 const char *extra_arg)
128 if (_dbus_string_get_length (config_file) > 0)
130 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
131 extra_arg, _dbus_string_get_const_data (config_file));
137 check_two_addresses (const DBusString *address,
138 const char *extra_arg)
140 if (_dbus_string_get_length (address) > 0)
142 fprintf (stderr, "--%s specified but address %s already requested\n",
143 extra_arg, _dbus_string_get_const_data (address));
149 check_two_addr_descriptors (const DBusString *addr_fd,
150 const char *extra_arg)
152 if (_dbus_string_get_length (addr_fd) > 0)
154 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
155 extra_arg, _dbus_string_get_const_data (addr_fd));
161 check_two_pid_descriptors (const DBusString *pid_fd,
162 const char *extra_arg)
164 if (_dbus_string_get_length (pid_fd) > 0)
166 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
167 extra_arg, _dbus_string_get_const_data (pid_fd));
173 handle_reload_watch (DBusWatch *watch,
180 while (!_dbus_string_init (&str))
181 _dbus_wait_for_memory ();
183 if ((reload_pipe[RELOAD_READ_END] > 0) &&
184 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
186 _dbus_warn ("Couldn't read from reload pipe.\n");
187 close_reload_pipe (&watch);
190 _dbus_string_free (&str);
192 /* this can only fail if we don't understand the config file
193 * or OOM. Either way we should just stick with the currently
196 dbus_error_init (&error);
197 if (! bus_context_reload_config (context, &error))
199 _DBUS_ASSERT_ERROR_IS_SET (&error);
200 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
201 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
202 _dbus_warn ("Unable to reload configuration: %s\n",
204 dbus_error_free (&error);
210 reload_watch_callback (DBusWatch *watch,
211 unsigned int condition,
214 return dbus_watch_handle (watch, condition);
218 setup_reload_pipe (DBusLoop *loop)
223 dbus_error_init (&error);
225 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
228 _dbus_warn ("Unable to create reload pipe: %s\n",
230 dbus_error_free (&error);
234 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
235 DBUS_WATCH_READABLE, TRUE,
236 handle_reload_watch, NULL, NULL);
240 _dbus_warn ("Unable to create reload watch: %s\n",
242 dbus_error_free (&error);
246 if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
249 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
251 dbus_error_free (&error);
258 close_reload_pipe (DBusWatch **watch)
260 _dbus_loop_remove_watch (bus_context_get_loop (context),
261 *watch, reload_watch_callback, NULL);
262 _dbus_watch_invalidate (*watch);
263 _dbus_watch_unref (*watch);
266 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
267 reload_pipe[RELOAD_READ_END] = -1;
269 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
270 reload_pipe[RELOAD_WRITE_END] = -1;
274 main (int argc, char **argv)
277 DBusString config_file;
281 const char *prev_arg;
282 DBusPipe print_addr_pipe;
283 DBusPipe print_pid_pipe;
285 dbus_bool_t print_address;
286 dbus_bool_t print_pid;
287 dbus_bool_t is_session_bus;
289 dbus_bool_t systemd_activation;
291 if (!_dbus_string_init (&config_file))
294 if (!_dbus_string_init (&address))
297 if (!_dbus_string_init (&addr_fd))
300 if (!_dbus_string_init (&pid_fd))
303 print_address = FALSE;
305 is_session_bus = FALSE;
306 force_fork = FORK_FOLLOW_CONFIG_FILE;
307 systemd_activation = FALSE;
313 const char *arg = argv[i];
315 if (strcmp (arg, "--help") == 0 ||
316 strcmp (arg, "-h") == 0 ||
317 strcmp (arg, "-?") == 0)
319 else if (strcmp (arg, "--version") == 0)
321 else if (strcmp (arg, "--introspect") == 0)
323 else if (strcmp (arg, "--nofork") == 0)
324 force_fork = FORK_NEVER;
325 else if (strcmp (arg, "--fork") == 0)
326 force_fork = FORK_ALWAYS;
327 else if (strcmp (arg, "--systemd-activation") == 0)
328 systemd_activation = TRUE;
329 else if (strcmp (arg, "--system") == 0)
331 check_two_config_files (&config_file, "system");
333 if (!_dbus_append_system_config_file (&config_file))
336 else if (strcmp (arg, "--session") == 0)
338 check_two_config_files (&config_file, "session");
340 if (!_dbus_append_session_config_file (&config_file))
343 else if (strstr (arg, "--config-file=") == arg)
347 check_two_config_files (&config_file, "config-file");
349 file = strchr (arg, '=');
352 if (!_dbus_string_append (&config_file, file))
356 strcmp (prev_arg, "--config-file") == 0)
358 check_two_config_files (&config_file, "config-file");
360 if (!_dbus_string_append (&config_file, arg))
363 else if (strcmp (arg, "--config-file") == 0)
364 ; /* wait for next arg */
365 else if (strstr (arg, "--address=") == arg)
369 check_two_addresses (&address, "address");
371 file = strchr (arg, '=');
374 if (!_dbus_string_append (&address, file))
378 strcmp (prev_arg, "--address") == 0)
380 check_two_addresses (&address, "address");
382 if (!_dbus_string_append (&address, arg))
385 else if (strcmp (arg, "--address") == 0)
386 ; /* wait for next arg */
387 else if (strstr (arg, "--print-address=") == arg)
391 check_two_addr_descriptors (&addr_fd, "print-address");
393 desc = strchr (arg, '=');
396 if (!_dbus_string_append (&addr_fd, desc))
399 print_address = TRUE;
402 strcmp (prev_arg, "--print-address") == 0)
404 check_two_addr_descriptors (&addr_fd, "print-address");
406 if (!_dbus_string_append (&addr_fd, arg))
409 print_address = TRUE;
411 else if (strcmp (arg, "--print-address") == 0)
412 print_address = TRUE; /* and we'll get the next arg if appropriate */
413 else if (strstr (arg, "--print-pid=") == arg)
417 check_two_pid_descriptors (&pid_fd, "print-pid");
419 desc = strchr (arg, '=');
422 if (!_dbus_string_append (&pid_fd, desc))
428 strcmp (prev_arg, "--print-pid") == 0)
430 check_two_pid_descriptors (&pid_fd, "print-pid");
432 if (!_dbus_string_append (&pid_fd, arg))
437 else if (strcmp (arg, "--print-pid") == 0)
438 print_pid = TRUE; /* and we'll get the next arg if appropriate */
447 if (_dbus_string_get_length (&config_file) == 0)
449 fprintf (stderr, "No configuration file specified.\n");
453 _dbus_pipe_invalidate (&print_addr_pipe);
456 _dbus_pipe_init_stdout (&print_addr_pipe);
457 if (_dbus_string_get_length (&addr_fd) > 0)
461 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
462 end != _dbus_string_get_length (&addr_fd) ||
463 val < 0 || val > _DBUS_INT_MAX)
465 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
466 _dbus_string_get_const_data (&addr_fd));
470 _dbus_pipe_init (&print_addr_pipe, val);
473 _dbus_string_free (&addr_fd);
475 _dbus_pipe_invalidate (&print_pid_pipe);
478 _dbus_pipe_init_stdout (&print_pid_pipe);
479 if (_dbus_string_get_length (&pid_fd) > 0)
483 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
484 end != _dbus_string_get_length (&pid_fd) ||
485 val < 0 || val > _DBUS_INT_MAX)
487 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
488 _dbus_string_get_const_data (&pid_fd));
492 _dbus_pipe_init (&print_pid_pipe, val);
495 _dbus_string_free (&pid_fd);
497 if (!bus_selinux_pre_init ())
499 _dbus_warn ("SELinux pre-initialization failed\n");
503 dbus_error_init (&error);
504 context = bus_context_new (&config_file, force_fork,
505 &print_addr_pipe, &print_pid_pipe,
506 _dbus_string_get_length(&address) > 0 ? &address : NULL,
509 _dbus_string_free (&config_file);
512 _dbus_warn ("Failed to start message bus: %s\n",
514 dbus_error_free (&error);
518 /* bus_context_new() closes the print_addr_pipe and
522 setup_reload_pipe (bus_context_get_loop (context));
525 _dbus_set_signal_handler (SIGHUP, signal_handler);
527 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
528 _dbus_set_signal_handler (SIGIO, signal_handler);
529 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
531 _dbus_verbose ("We are on D-Bus...\n");
532 _dbus_loop_run (bus_context_get_loop (context));
534 bus_context_shutdown (context);
535 bus_context_unref (context);
536 bus_selinux_shutdown ();