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>
39 #include <unistd.h> /* for write() and STDERR_FILENO */
43 static BusContext *context;
47 static int reload_pipe[2];
48 #define RELOAD_READ_END 0
49 #define RELOAD_WRITE_END 1
51 static void close_reload_pipe (DBusWatch **);
60 signal_handler (int sig)
64 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
66 /* explicit fall-through */
67 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
72 char action[2] = { ACTION_RELOAD, '\0' };
74 _dbus_string_init_const (&str, action);
75 if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
76 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
78 /* If we receive SIGHUP often enough to fill the pipe buffer (4096
79 * times on old Linux, 65536 on modern Linux) before it can be
80 * drained, let's just warn and ignore. The configuration will be
81 * reloaded while draining the pipe buffer, which is what we
82 * wanted. It's harmless that it will be reloaded fewer times than
83 * we asked for, since the reload is delayed anyway, so new changes
86 * We use write() because _dbus_warn uses vfprintf, which isn't
89 * This is necessarily Unix-specific, but so are POSIX signals,
91 static const char message[] =
92 "Unable to write to reload pipe - buffer full?\n";
94 write (STDERR_FILENO, message, strlen (message));
103 char action[2] = { ACTION_QUIT, '\0' };
104 _dbus_string_init_const (&str, action);
105 if ((reload_pipe[RELOAD_WRITE_END] < 0) ||
106 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
108 /* If we can't write to the socket, dying seems a more
109 * important response to SIGTERM than cleaning up sockets,
110 * so we exit. We'd use exit(), but that's not async-signal-safe,
111 * so we'll have to resort to _exit(). */
112 static const char message[] =
113 "Unable to write termination signal to pipe - buffer full?\n"
114 "Will exit instead.\n";
116 write (STDERR_FILENO, message, strlen (message));
123 #endif /* DBUS_UNIX */
128 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");
135 printf ("D-Bus Message Bus Daemon %s\n"
136 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
137 "This is free software; see the source for copying conditions.\n"
138 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
139 DBUS_VERSION_STRING);
147 const char *v_STRING;
149 if (!_dbus_string_init (&xml))
152 if (!bus_driver_generate_introspect_string (&xml))
154 _dbus_string_free (&xml);
158 v_STRING = _dbus_string_get_const_data (&xml);
159 printf ("%s\n", v_STRING);
164 _dbus_warn ("Can not introspect - Out of memory\n");
169 check_two_config_files (const DBusString *config_file,
170 const char *extra_arg)
172 if (_dbus_string_get_length (config_file) > 0)
174 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
175 extra_arg, _dbus_string_get_const_data (config_file));
181 check_two_addresses (const DBusString *address,
182 const char *extra_arg)
184 if (_dbus_string_get_length (address) > 0)
186 fprintf (stderr, "--%s specified but address %s already requested\n",
187 extra_arg, _dbus_string_get_const_data (address));
193 check_two_addr_descriptors (const DBusString *addr_fd,
194 const char *extra_arg)
196 if (_dbus_string_get_length (addr_fd) > 0)
198 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
199 extra_arg, _dbus_string_get_const_data (addr_fd));
205 check_two_pid_descriptors (const DBusString *pid_fd,
206 const char *extra_arg)
208 if (_dbus_string_get_length (pid_fd) > 0)
210 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
211 extra_arg, _dbus_string_get_const_data (pid_fd));
218 handle_reload_watch (DBusWatch *watch,
227 while (!_dbus_string_init (&str))
228 _dbus_wait_for_memory ();
230 if ((reload_pipe[RELOAD_READ_END] > 0) &&
231 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
233 _dbus_warn ("Couldn't read from reload pipe.\n");
234 close_reload_pipe (&watch);
238 action_str = _dbus_string_get_data (&str);
239 if (action_str != NULL)
241 action = action_str[0];
243 _dbus_string_free (&str);
245 /* this can only fail if we don't understand the config file
246 * or OOM. Either way we should just stick with the currently
249 dbus_error_init (&error);
254 if (! bus_context_reload_config (context, &error))
256 _DBUS_ASSERT_ERROR_IS_SET (&error);
257 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
258 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
259 _dbus_warn ("Unable to reload configuration: %s\n",
261 dbus_error_free (&error);
269 * On OSs without abstract sockets, we want to quit
270 * gracefully rather than being killed by SIGTERM,
271 * so that DBusServer gets a chance to clean up the
272 * sockets from the filesystem. fd.o #38656
274 loop = bus_context_get_loop (context);
277 _dbus_loop_quit (loop);
290 setup_reload_pipe (DBusLoop *loop)
295 dbus_error_init (&error);
297 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
300 _dbus_warn ("Unable to create reload pipe: %s\n",
302 dbus_error_free (&error);
306 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
307 DBUS_WATCH_READABLE, TRUE,
308 handle_reload_watch, NULL, NULL);
312 _dbus_warn ("Unable to create reload watch: %s\n",
314 dbus_error_free (&error);
318 if (!_dbus_loop_add_watch (loop, watch))
320 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
322 dbus_error_free (&error);
329 close_reload_pipe (DBusWatch **watch)
331 _dbus_loop_remove_watch (bus_context_get_loop (context), *watch);
332 _dbus_watch_invalidate (*watch);
333 _dbus_watch_unref (*watch);
336 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
337 reload_pipe[RELOAD_READ_END] = -1;
339 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
340 reload_pipe[RELOAD_WRITE_END] = -1;
342 #endif /* DBUS_UNIX */
345 main (int argc, char **argv)
348 DBusString config_file;
352 const char *prev_arg;
353 DBusPipe print_addr_pipe;
354 DBusPipe print_pid_pipe;
356 dbus_bool_t print_address;
357 dbus_bool_t print_pid;
359 dbus_bool_t systemd_activation;
361 if (!_dbus_string_init (&config_file))
364 if (!_dbus_string_init (&address))
367 if (!_dbus_string_init (&addr_fd))
370 if (!_dbus_string_init (&pid_fd))
373 print_address = FALSE;
375 force_fork = FORK_FOLLOW_CONFIG_FILE;
376 systemd_activation = FALSE;
382 const char *arg = argv[i];
384 if (strcmp (arg, "--help") == 0 ||
385 strcmp (arg, "-h") == 0 ||
386 strcmp (arg, "-?") == 0)
388 else if (strcmp (arg, "--version") == 0)
390 else if (strcmp (arg, "--introspect") == 0)
392 else if (strcmp (arg, "--nofork") == 0)
393 force_fork = FORK_NEVER;
394 else if (strcmp (arg, "--fork") == 0)
395 force_fork = FORK_ALWAYS;
396 else if (strcmp (arg, "--systemd-activation") == 0)
397 systemd_activation = TRUE;
398 else if (strcmp (arg, "--system") == 0)
400 check_two_config_files (&config_file, "system");
402 if (!_dbus_append_system_config_file (&config_file))
405 else if (strcmp (arg, "--session") == 0)
407 check_two_config_files (&config_file, "session");
409 if (!_dbus_append_session_config_file (&config_file))
412 else if (strstr (arg, "--config-file=") == arg)
416 check_two_config_files (&config_file, "config-file");
418 file = strchr (arg, '=');
421 if (!_dbus_string_append (&config_file, file))
425 strcmp (prev_arg, "--config-file") == 0)
427 check_two_config_files (&config_file, "config-file");
429 if (!_dbus_string_append (&config_file, arg))
432 else if (strcmp (arg, "--config-file") == 0)
433 ; /* wait for next arg */
434 else if (strstr (arg, "--address=") == arg)
438 check_two_addresses (&address, "address");
440 file = strchr (arg, '=');
443 if (!_dbus_string_append (&address, file))
447 strcmp (prev_arg, "--address") == 0)
449 check_two_addresses (&address, "address");
451 if (!_dbus_string_append (&address, arg))
454 else if (strcmp (arg, "--address") == 0)
455 ; /* wait for next arg */
456 else if (strstr (arg, "--print-address=") == arg)
460 check_two_addr_descriptors (&addr_fd, "print-address");
462 desc = strchr (arg, '=');
465 if (!_dbus_string_append (&addr_fd, desc))
468 print_address = TRUE;
471 strcmp (prev_arg, "--print-address") == 0)
473 check_two_addr_descriptors (&addr_fd, "print-address");
475 if (!_dbus_string_append (&addr_fd, arg))
478 print_address = TRUE;
480 else if (strcmp (arg, "--print-address") == 0)
481 print_address = TRUE; /* and we'll get the next arg if appropriate */
482 else if (strstr (arg, "--print-pid=") == arg)
486 check_two_pid_descriptors (&pid_fd, "print-pid");
488 desc = strchr (arg, '=');
491 if (!_dbus_string_append (&pid_fd, desc))
497 strcmp (prev_arg, "--print-pid") == 0)
499 check_two_pid_descriptors (&pid_fd, "print-pid");
501 if (!_dbus_string_append (&pid_fd, arg))
506 else if (strcmp (arg, "--print-pid") == 0)
507 print_pid = TRUE; /* and we'll get the next arg if appropriate */
516 if (_dbus_string_get_length (&config_file) == 0)
518 fprintf (stderr, "No configuration file specified.\n");
522 _dbus_pipe_invalidate (&print_addr_pipe);
525 _dbus_pipe_init_stdout (&print_addr_pipe);
526 if (_dbus_string_get_length (&addr_fd) > 0)
530 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
531 end != _dbus_string_get_length (&addr_fd) ||
532 val < 0 || val > _DBUS_INT_MAX)
534 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
535 _dbus_string_get_const_data (&addr_fd));
539 _dbus_pipe_init (&print_addr_pipe, val);
542 _dbus_string_free (&addr_fd);
544 _dbus_pipe_invalidate (&print_pid_pipe);
547 _dbus_pipe_init_stdout (&print_pid_pipe);
548 if (_dbus_string_get_length (&pid_fd) > 0)
552 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
553 end != _dbus_string_get_length (&pid_fd) ||
554 val < 0 || val > _DBUS_INT_MAX)
556 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
557 _dbus_string_get_const_data (&pid_fd));
561 _dbus_pipe_init (&print_pid_pipe, val);
564 _dbus_string_free (&pid_fd);
566 if (!bus_selinux_pre_init ())
568 _dbus_warn ("SELinux pre-initialization failed\n");
572 dbus_error_init (&error);
573 context = bus_context_new (&config_file, force_fork,
574 &print_addr_pipe, &print_pid_pipe,
575 _dbus_string_get_length(&address) > 0 ? &address : NULL,
578 _dbus_string_free (&config_file);
581 _dbus_warn ("Failed to start message bus: %s\n",
583 dbus_error_free (&error);
587 /* bus_context_new() closes the print_addr_pipe and
592 setup_reload_pipe (bus_context_get_loop (context));
594 /* POSIX signals are Unix-specific, and _dbus_set_signal_handler is
595 * unimplemented (and probably unimplementable) on Windows, so there's
596 * no point in trying to make the handler portable to non-Unix. */
598 _dbus_set_signal_handler (SIGTERM, signal_handler);
600 _dbus_set_signal_handler (SIGHUP, signal_handler);
602 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
603 _dbus_set_signal_handler (SIGIO, signal_handler);
604 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
605 #endif /* DBUS_UNIX */
607 _dbus_verbose ("We are on D-Bus...\n");
608 _dbus_loop_run (bus_context_get_loop (context));
610 bus_context_shutdown (context);
611 bus_context_unref (context);
612 bus_selinux_shutdown ();