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 */
46 #include <dbus/dbus-sysdeps-unix.h>
49 static BusContext *context;
53 /* Despite its name and its unidirectional nature, this is actually
55 static DBusSocket reload_pipe[2];
56 #define RELOAD_READ_END 0
57 #define RELOAD_WRITE_END 1
59 static void close_reload_pipe (DBusWatch **);
68 signal_handler (int sig)
70 /* Signal handlers that might set errno must save and restore the errno
71 * that the interrupted function might have been relying on. */
72 int saved_errno = errno;
79 char action[2] = { ACTION_RELOAD, '\0' };
81 _dbus_string_init_const (&str, action);
82 if ((reload_pipe[RELOAD_WRITE_END].fd > 0) &&
83 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
85 /* If we receive SIGHUP often enough to fill the pipe buffer (4096
86 * times on old Linux, 65536 on modern Linux) before it can be
87 * drained, let's just warn and ignore. The configuration will be
88 * reloaded while draining the pipe buffer, which is what we
89 * wanted. It's harmless that it will be reloaded fewer times than
90 * we asked for, since the reload is delayed anyway, so new changes
93 * We use write() because _dbus_warn uses vfprintf, which isn't
96 * This is necessarily Unix-specific, but so are POSIX signals,
98 static const char message[] =
99 "Unable to write to reload pipe - buffer full?\n";
101 if (write (STDERR_FILENO, message, strlen (message)) !=
102 (ssize_t) strlen (message))
104 /* ignore failure to write out a warning */
113 char action[2] = { ACTION_QUIT, '\0' };
114 _dbus_string_init_const (&str, action);
115 if ((reload_pipe[RELOAD_WRITE_END].fd < 0) ||
116 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
118 /* If we can't write to the socket, dying seems a more
119 * important response to SIGTERM than cleaning up sockets,
120 * so we exit. We'd use exit(), but that's not async-signal-safe,
121 * so we'll have to resort to _exit(). */
122 static const char message[] =
123 "Unable to write termination signal to pipe - buffer full?\n"
124 "Will exit instead.\n";
126 if (write (STDERR_FILENO, message, strlen (message)) !=
127 (ssize_t) strlen (message))
129 /* ignore failure to write out a warning */
137 /* can't happen unless this signal handler gets used for a wrong
138 * signal, but keep -Wswitch-default happy */
144 #endif /* DBUS_UNIX */
146 static void usage (void) _DBUS_GNUC_NORETURN;
156 " [--config-file=FILE]"
157 " [--print-address[=DESCRIPTOR]]"
158 " [--print-pid[=DESCRIPTOR]]"
160 " [--address=ADDRESS]"
168 " [--systemd-activation]"
174 static void version (void) _DBUS_GNUC_NORETURN;
179 printf ("D-Bus Message Bus Daemon %s\n"
180 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
181 "This is free software; see the source for copying conditions.\n"
182 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
183 DBUS_VERSION_STRING);
187 static void introspect (void) _DBUS_GNUC_NORETURN;
193 const char *v_STRING;
195 if (!_dbus_string_init (&xml))
198 if (!bus_driver_generate_introspect_string (&xml, TRUE, NULL))
200 _dbus_string_free (&xml);
204 v_STRING = _dbus_string_get_const_data (&xml);
205 printf ("%s\n", v_STRING);
210 _dbus_warn ("Can not introspect - Out of memory");
215 check_two_config_files (const DBusString *config_file,
216 const char *extra_arg)
218 if (_dbus_string_get_length (config_file) > 0)
220 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
221 extra_arg, _dbus_string_get_const_data (config_file));
227 check_two_addresses (const DBusString *address,
228 const char *extra_arg)
230 if (_dbus_string_get_length (address) > 0)
232 fprintf (stderr, "--%s specified but address %s already requested\n",
233 extra_arg, _dbus_string_get_const_data (address));
239 check_two_addr_descriptors (const DBusString *addr_fd,
240 const char *extra_arg)
242 if (_dbus_string_get_length (addr_fd) > 0)
244 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
245 extra_arg, _dbus_string_get_const_data (addr_fd));
251 check_two_pid_descriptors (const DBusString *pid_fd,
252 const char *extra_arg)
254 if (_dbus_string_get_length (pid_fd) > 0)
256 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
257 extra_arg, _dbus_string_get_const_data (pid_fd));
264 handle_reload_watch (DBusWatch *watch,
273 while (!_dbus_string_init (&str))
274 _dbus_wait_for_memory ();
276 if ((reload_pipe[RELOAD_READ_END].fd > 0) &&
277 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
279 _dbus_warn ("Couldn't read from reload pipe.");
280 close_reload_pipe (&watch);
284 action_str = _dbus_string_get_data (&str);
285 if (action_str != NULL)
287 action = action_str[0];
289 _dbus_string_free (&str);
291 /* this can only fail if we don't understand the config file
292 * or OOM. Either way we should just stick with the currently
295 dbus_error_init (&error);
300 if (! bus_context_reload_config (context, &error))
302 _DBUS_ASSERT_ERROR_IS_SET (&error);
303 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
304 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
305 _dbus_warn ("Unable to reload configuration: %s",
307 dbus_error_free (&error);
315 * On OSs without abstract sockets, we want to quit
316 * gracefully rather than being killed by SIGTERM,
317 * so that DBusServer gets a chance to clean up the
318 * sockets from the filesystem. fd.o #38656
320 loop = bus_context_get_loop (context);
323 _dbus_loop_quit (loop);
336 setup_reload_pipe (DBusLoop *loop)
341 dbus_error_init (&error);
343 if (!_dbus_socketpair (&reload_pipe[0], &reload_pipe[1],
346 _dbus_warn ("Unable to create reload pipe: %s",
348 dbus_error_free (&error);
352 watch = _dbus_watch_new (_dbus_socket_get_pollable (reload_pipe[RELOAD_READ_END]),
353 DBUS_WATCH_READABLE, TRUE,
354 handle_reload_watch, NULL, NULL);
358 _dbus_warn ("Unable to create reload watch: %s",
360 dbus_error_free (&error);
364 if (!_dbus_loop_add_watch (loop, watch))
366 _dbus_warn ("Unable to add reload watch to main loop: %s",
368 dbus_error_free (&error);
375 close_reload_pipe (DBusWatch **watch)
377 _dbus_loop_remove_watch (bus_context_get_loop (context), *watch);
378 _dbus_watch_invalidate (*watch);
379 _dbus_watch_unref (*watch);
382 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
383 _dbus_socket_invalidate (&reload_pipe[RELOAD_READ_END]);
385 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
386 _dbus_socket_invalidate (&reload_pipe[RELOAD_WRITE_END]);
388 #endif /* DBUS_UNIX */
391 main (int argc, char **argv)
394 DBusString config_file;
398 const char *prev_arg;
399 DBusPipe print_addr_pipe;
400 DBusPipe print_pid_pipe;
402 dbus_bool_t print_address;
403 dbus_bool_t print_pid;
404 BusContextFlags flags;
406 const char *error_str;
408 /* Redirect stdin from /dev/null since we will never need it, and
409 * redirect stdout and stderr to /dev/null if not already open.
411 * We should do this as the very first thing, to ensure that when we
412 * create other file descriptors (for example for epoll, inotify or
413 * a socket), they never get assigned as fd 0, 1 or 2. If they were,
414 * which could happen if our caller had (incorrectly) closed those
415 * standard fds, they'd get closed when we daemonize - for example,
416 * closing our listening socket would stop us listening, and closing
417 * a Linux epoll socket would cause the main loop to fail. */
418 if (!_dbus_ensure_standard_fds (DBUS_FORCE_STDIN_NULL, &error_str))
421 "dbus-daemon: fatal error setting up standard fds: %s: %s\n",
422 error_str, _dbus_strerror (errno));
427 if (!_dbus_string_init (&config_file))
430 if (!_dbus_string_init (&address))
433 if (!_dbus_string_init (&addr_fd))
436 if (!_dbus_string_init (&pid_fd))
439 print_address = FALSE;
442 flags = BUS_CONTEXT_FLAG_WRITE_PID_FILE;
448 const char *arg = argv[i];
450 if (strcmp (arg, "--help") == 0 ||
451 strcmp (arg, "-h") == 0 ||
452 strcmp (arg, "-?") == 0)
456 else if (strcmp (arg, "--version") == 0)
460 else if (strcmp (arg, "--introspect") == 0)
464 else if (strcmp (arg, "--nosyslog") == 0)
466 flags &= ~BUS_CONTEXT_FLAG_SYSLOG_ALWAYS;
467 flags |= BUS_CONTEXT_FLAG_SYSLOG_NEVER;
469 else if (strcmp (arg, "--syslog") == 0)
471 flags &= ~BUS_CONTEXT_FLAG_SYSLOG_NEVER;
472 flags |= BUS_CONTEXT_FLAG_SYSLOG_ALWAYS;
474 else if (strcmp (arg, "--syslog-only") == 0)
476 flags &= ~BUS_CONTEXT_FLAG_SYSLOG_NEVER;
477 flags |= (BUS_CONTEXT_FLAG_SYSLOG_ALWAYS|BUS_CONTEXT_FLAG_SYSLOG_ONLY);
479 else if (strcmp (arg, "--nofork") == 0)
481 flags &= ~BUS_CONTEXT_FLAG_FORK_ALWAYS;
482 flags |= BUS_CONTEXT_FLAG_FORK_NEVER;
485 else if (strcmp (arg, "--fork") == 0)
487 flags &= ~BUS_CONTEXT_FLAG_FORK_NEVER;
488 flags |= BUS_CONTEXT_FLAG_FORK_ALWAYS;
490 else if (strcmp (arg, "--systemd-activation") == 0)
492 flags |= BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION;
495 else if (strcmp (arg, "--nopidfile") == 0)
497 flags &= ~BUS_CONTEXT_FLAG_WRITE_PID_FILE;
499 else if (strcmp (arg, "--system") == 0)
501 check_two_config_files (&config_file, "system");
503 if (!_dbus_get_system_config_file (&config_file))
506 else if (strcmp (arg, "--session") == 0)
508 check_two_config_files (&config_file, "session");
510 if (!_dbus_get_session_config_file (&config_file))
513 else if (strstr (arg, "--config-file=") == arg)
517 check_two_config_files (&config_file, "config-file");
519 file = strchr (arg, '=');
522 if (!_dbus_string_append (&config_file, file))
526 strcmp (prev_arg, "--config-file") == 0)
528 check_two_config_files (&config_file, "config-file");
530 if (!_dbus_string_append (&config_file, arg))
533 else if (strcmp (arg, "--config-file") == 0)
535 /* wait for next arg */
537 else if (strstr (arg, "--address=") == arg)
541 check_two_addresses (&address, "address");
543 file = strchr (arg, '=');
546 if (!_dbus_string_append (&address, file))
550 strcmp (prev_arg, "--address") == 0)
552 check_two_addresses (&address, "address");
554 if (!_dbus_string_append (&address, arg))
557 else if (strcmp (arg, "--address") == 0)
559 /* wait for next arg */
561 else if (strstr (arg, "--print-address=") == arg)
565 check_two_addr_descriptors (&addr_fd, "print-address");
567 desc = strchr (arg, '=');
570 if (!_dbus_string_append (&addr_fd, desc))
573 print_address = TRUE;
576 strcmp (prev_arg, "--print-address") == 0)
578 check_two_addr_descriptors (&addr_fd, "print-address");
580 if (!_dbus_string_append (&addr_fd, arg))
583 print_address = TRUE;
585 else if (strcmp (arg, "--print-address") == 0)
587 print_address = TRUE; /* and we'll get the next arg if appropriate */
589 else if (strstr (arg, "--print-pid=") == arg)
593 check_two_pid_descriptors (&pid_fd, "print-pid");
595 desc = strchr (arg, '=');
598 if (!_dbus_string_append (&pid_fd, desc))
604 strcmp (prev_arg, "--print-pid") == 0)
606 check_two_pid_descriptors (&pid_fd, "print-pid");
608 if (!_dbus_string_append (&pid_fd, arg))
613 else if (strcmp (arg, "--print-pid") == 0)
615 print_pid = TRUE; /* and we'll get the next arg if appropriate */
627 if (_dbus_string_get_length (&config_file) == 0)
629 fprintf (stderr, "No configuration file specified.\n");
633 _dbus_pipe_invalidate (&print_addr_pipe);
636 _dbus_pipe_init_stdout (&print_addr_pipe);
637 if (_dbus_string_get_length (&addr_fd) > 0)
641 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
642 end != _dbus_string_get_length (&addr_fd) ||
643 val < 0 || val > _DBUS_INT_MAX)
645 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
646 _dbus_string_get_const_data (&addr_fd));
650 _dbus_pipe_init (&print_addr_pipe, val);
653 _dbus_string_free (&addr_fd);
655 _dbus_pipe_invalidate (&print_pid_pipe);
658 _dbus_pipe_init_stdout (&print_pid_pipe);
659 if (_dbus_string_get_length (&pid_fd) > 0)
663 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
664 end != _dbus_string_get_length (&pid_fd) ||
665 val < 0 || val > _DBUS_INT_MAX)
667 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
668 _dbus_string_get_const_data (&pid_fd));
672 _dbus_pipe_init (&print_pid_pipe, val);
675 _dbus_string_free (&pid_fd);
677 if (!bus_selinux_pre_init ())
679 _dbus_warn ("SELinux pre-initialization failed");
683 if (!bus_apparmor_pre_init ())
685 _dbus_warn ("AppArmor pre-initialization failed: out of memory");
689 dbus_error_init (&error);
690 context = bus_context_new (&config_file, flags,
691 &print_addr_pipe, &print_pid_pipe,
692 _dbus_string_get_length(&address) > 0 ? &address : NULL,
694 _dbus_string_free (&config_file);
695 _dbus_string_free (&address);
698 _dbus_warn ("Failed to start message bus: %s",
700 dbus_error_free (&error);
704 /* bus_context_new() closes the print_addr_pipe and
709 setup_reload_pipe (bus_context_get_loop (context));
711 /* POSIX signals are Unix-specific, and _dbus_set_signal_handler is
712 * unimplemented (and probably unimplementable) on Windows, so there's
713 * no point in trying to make the handler portable to non-Unix. */
715 _dbus_set_signal_handler (SIGTERM, signal_handler);
716 _dbus_set_signal_handler (SIGHUP, signal_handler);
717 #endif /* DBUS_UNIX */
719 dbus_set_protocol_version (DBUS_MAJOR_PROTOCOL_VERSION);
721 _dbus_verbose ("We are on D-Bus...\n");
722 _dbus_loop_run (bus_context_get_loop (context));
724 bus_context_shutdown (context);
725 bus_context_unref (context);
726 bus_selinux_shutdown ();
727 bus_apparmor_shutdown ();
728 bus_audit_shutdown ();