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 <systemd/sd-daemon.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)
75 char action[2] = { ACTION_RELOAD, '\0' };
77 _dbus_string_init_const (&str, action);
78 if ((reload_pipe[RELOAD_WRITE_END].fd > 0) &&
79 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
81 /* If we receive SIGHUP often enough to fill the pipe buffer (4096
82 * times on old Linux, 65536 on modern Linux) before it can be
83 * drained, let's just warn and ignore. The configuration will be
84 * reloaded while draining the pipe buffer, which is what we
85 * wanted. It's harmless that it will be reloaded fewer times than
86 * we asked for, since the reload is delayed anyway, so new changes
89 * We use write() because _dbus_warn uses vfprintf, which isn't
92 * This is necessarily Unix-specific, but so are POSIX signals,
94 static const char message[] =
95 "Unable to write to reload pipe - buffer full?\n";
97 if (write (STDERR_FILENO, message, strlen (message)) !=
98 (ssize_t) strlen (message))
100 /* ignore failure to write out a warning */
109 char action[2] = { ACTION_QUIT, '\0' };
110 _dbus_string_init_const (&str, action);
111 if ((reload_pipe[RELOAD_WRITE_END].fd < 0) ||
112 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
114 /* If we can't write to the socket, dying seems a more
115 * important response to SIGTERM than cleaning up sockets,
116 * so we exit. We'd use exit(), but that's not async-signal-safe,
117 * so we'll have to resort to _exit(). */
118 static const char message[] =
119 "Unable to write termination signal to pipe - buffer full?\n"
120 "Will exit instead.\n";
122 if (write (STDERR_FILENO, message, strlen (message)) !=
123 (ssize_t) strlen (message))
125 /* ignore failure to write out a warning */
133 #endif /* DBUS_UNIX */
143 " [--config-file=FILE]"
144 " [--print-address[=DESCRIPTOR]]"
145 " [--print-pid[=DESCRIPTOR]]"
147 " [--address=ADDRESS]"
152 " [--systemd-activation]"
161 printf ("D-Bus Message Bus Daemon %s\n"
162 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
163 "This is free software; see the source for copying conditions.\n"
164 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
165 DBUS_VERSION_STRING);
173 const char *v_STRING;
175 if (!_dbus_string_init (&xml))
178 if (!bus_driver_generate_introspect_string (&xml))
180 _dbus_string_free (&xml);
184 v_STRING = _dbus_string_get_const_data (&xml);
185 printf ("%s\n", v_STRING);
190 _dbus_warn ("Can not introspect - Out of memory\n");
195 check_two_config_files (const DBusString *config_file,
196 const char *extra_arg)
198 if (_dbus_string_get_length (config_file) > 0)
200 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
201 extra_arg, _dbus_string_get_const_data (config_file));
207 check_two_addresses (const DBusString *address,
208 const char *extra_arg)
210 if (_dbus_string_get_length (address) > 0)
212 fprintf (stderr, "--%s specified but address %s already requested\n",
213 extra_arg, _dbus_string_get_const_data (address));
219 check_two_addr_descriptors (const DBusString *addr_fd,
220 const char *extra_arg)
222 if (_dbus_string_get_length (addr_fd) > 0)
224 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
225 extra_arg, _dbus_string_get_const_data (addr_fd));
231 check_two_pid_descriptors (const DBusString *pid_fd,
232 const char *extra_arg)
234 if (_dbus_string_get_length (pid_fd) > 0)
236 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
237 extra_arg, _dbus_string_get_const_data (pid_fd));
244 handle_reload_watch (DBusWatch *watch,
253 while (!_dbus_string_init (&str))
254 _dbus_wait_for_memory ();
256 if ((reload_pipe[RELOAD_READ_END].fd > 0) &&
257 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
259 _dbus_warn ("Couldn't read from reload pipe.\n");
260 close_reload_pipe (&watch);
264 action_str = _dbus_string_get_data (&str);
265 if (action_str != NULL)
267 action = action_str[0];
269 _dbus_string_free (&str);
271 /* this can only fail if we don't understand the config file
272 * or OOM. Either way we should just stick with the currently
275 dbus_error_init (&error);
280 if (! bus_context_reload_config (context, &error))
282 _DBUS_ASSERT_ERROR_IS_SET (&error);
283 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
284 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
285 _dbus_warn ("Unable to reload configuration: %s\n",
287 dbus_error_free (&error);
295 * On OSs without abstract sockets, we want to quit
296 * gracefully rather than being killed by SIGTERM,
297 * so that DBusServer gets a chance to clean up the
298 * sockets from the filesystem. fd.o #38656
300 loop = bus_context_get_loop (context);
303 _dbus_loop_quit (loop);
316 setup_reload_pipe (DBusLoop *loop)
321 dbus_error_init (&error);
323 if (!_dbus_socketpair (&reload_pipe[0], &reload_pipe[1],
326 _dbus_warn ("Unable to create reload pipe: %s\n",
328 dbus_error_free (&error);
332 watch = _dbus_watch_new (_dbus_socket_get_pollable (reload_pipe[RELOAD_READ_END]),
333 DBUS_WATCH_READABLE, TRUE,
334 handle_reload_watch, NULL, NULL);
338 _dbus_warn ("Unable to create reload watch: %s\n",
340 dbus_error_free (&error);
344 if (!_dbus_loop_add_watch (loop, watch))
346 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
348 dbus_error_free (&error);
355 close_reload_pipe (DBusWatch **watch)
357 _dbus_loop_remove_watch (bus_context_get_loop (context), *watch);
358 _dbus_watch_invalidate (*watch);
359 _dbus_watch_unref (*watch);
362 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
363 _dbus_socket_invalidate (&reload_pipe[RELOAD_READ_END]);
365 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
366 _dbus_socket_invalidate (&reload_pipe[RELOAD_WRITE_END]);
368 #endif /* DBUS_UNIX */
371 main (int argc, char **argv)
374 DBusString config_file;
378 const char *prev_arg;
379 DBusPipe print_addr_pipe;
380 DBusPipe print_pid_pipe;
382 dbus_bool_t print_address;
383 dbus_bool_t print_pid;
384 BusContextFlags flags;
386 if (!_dbus_string_init (&config_file))
389 if (!_dbus_string_init (&address))
392 if (!_dbus_string_init (&addr_fd))
395 if (!_dbus_string_init (&pid_fd))
398 print_address = FALSE;
401 flags = BUS_CONTEXT_FLAG_WRITE_PID_FILE;
407 const char *arg = argv[i];
409 if (strcmp (arg, "--help") == 0 ||
410 strcmp (arg, "-h") == 0 ||
411 strcmp (arg, "-?") == 0)
415 else if (strcmp (arg, "--version") == 0)
419 else if (strcmp (arg, "--introspect") == 0)
423 else if (strcmp (arg, "--nofork") == 0)
425 flags &= ~BUS_CONTEXT_FLAG_FORK_ALWAYS;
426 flags |= BUS_CONTEXT_FLAG_FORK_NEVER;
429 else if (strcmp (arg, "--fork") == 0)
431 flags &= ~BUS_CONTEXT_FLAG_FORK_NEVER;
432 flags |= BUS_CONTEXT_FLAG_FORK_ALWAYS;
434 else if (strcmp (arg, "--systemd-activation") == 0)
436 flags |= BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION;
439 else if (strcmp (arg, "--nopidfile") == 0)
441 flags &= ~BUS_CONTEXT_FLAG_WRITE_PID_FILE;
443 else if (strcmp (arg, "--system") == 0)
445 check_two_config_files (&config_file, "system");
447 if (!_dbus_append_system_config_file (&config_file))
450 else if (strcmp (arg, "--session") == 0)
452 check_two_config_files (&config_file, "session");
454 if (!_dbus_append_session_config_file (&config_file))
457 else if (strstr (arg, "--config-file=") == arg)
461 check_two_config_files (&config_file, "config-file");
463 file = strchr (arg, '=');
466 if (!_dbus_string_append (&config_file, file))
470 strcmp (prev_arg, "--config-file") == 0)
472 check_two_config_files (&config_file, "config-file");
474 if (!_dbus_string_append (&config_file, arg))
477 else if (strcmp (arg, "--config-file") == 0)
479 /* wait for next arg */
481 else if (strstr (arg, "--address=") == arg)
485 check_two_addresses (&address, "address");
487 file = strchr (arg, '=');
490 if (!_dbus_string_append (&address, file))
494 strcmp (prev_arg, "--address") == 0)
496 check_two_addresses (&address, "address");
498 if (!_dbus_string_append (&address, arg))
501 else if (strcmp (arg, "--address") == 0)
503 /* wait for next arg */
505 else if (strstr (arg, "--print-address=") == arg)
509 check_two_addr_descriptors (&addr_fd, "print-address");
511 desc = strchr (arg, '=');
514 if (!_dbus_string_append (&addr_fd, desc))
517 print_address = TRUE;
520 strcmp (prev_arg, "--print-address") == 0)
522 check_two_addr_descriptors (&addr_fd, "print-address");
524 if (!_dbus_string_append (&addr_fd, arg))
527 print_address = TRUE;
529 else if (strcmp (arg, "--print-address") == 0)
531 print_address = TRUE; /* and we'll get the next arg if appropriate */
533 else if (strstr (arg, "--print-pid=") == arg)
537 check_two_pid_descriptors (&pid_fd, "print-pid");
539 desc = strchr (arg, '=');
542 if (!_dbus_string_append (&pid_fd, desc))
548 strcmp (prev_arg, "--print-pid") == 0)
550 check_two_pid_descriptors (&pid_fd, "print-pid");
552 if (!_dbus_string_append (&pid_fd, arg))
557 else if (strcmp (arg, "--print-pid") == 0)
559 print_pid = TRUE; /* and we'll get the next arg if appropriate */
571 if (_dbus_string_get_length (&config_file) == 0)
573 fprintf (stderr, "No configuration file specified.\n");
577 _dbus_pipe_invalidate (&print_addr_pipe);
580 _dbus_pipe_init_stdout (&print_addr_pipe);
581 if (_dbus_string_get_length (&addr_fd) > 0)
585 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
586 end != _dbus_string_get_length (&addr_fd) ||
587 val < 0 || val > _DBUS_INT_MAX)
589 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
590 _dbus_string_get_const_data (&addr_fd));
594 _dbus_pipe_init (&print_addr_pipe, val);
597 _dbus_string_free (&addr_fd);
599 _dbus_pipe_invalidate (&print_pid_pipe);
602 _dbus_pipe_init_stdout (&print_pid_pipe);
603 if (_dbus_string_get_length (&pid_fd) > 0)
607 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
608 end != _dbus_string_get_length (&pid_fd) ||
609 val < 0 || val > _DBUS_INT_MAX)
611 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
612 _dbus_string_get_const_data (&pid_fd));
616 _dbus_pipe_init (&print_pid_pipe, val);
619 _dbus_string_free (&pid_fd);
621 if (!bus_selinux_pre_init ())
623 _dbus_warn ("SELinux pre-initialization failed\n");
627 if (!bus_apparmor_pre_init ())
629 _dbus_warn ("AppArmor pre-initialization failed: out of memory\n");
633 dbus_error_init (&error);
634 context = bus_context_new (&config_file, flags,
635 &print_addr_pipe, &print_pid_pipe,
636 _dbus_string_get_length(&address) > 0 ? &address : NULL,
638 _dbus_string_free (&config_file);
641 _dbus_warn ("Failed to start message bus: %s\n",
643 dbus_error_free (&error);
647 /* bus_context_new() closes the print_addr_pipe and
652 setup_reload_pipe (bus_context_get_loop (context));
654 /* POSIX signals are Unix-specific, and _dbus_set_signal_handler is
655 * unimplemented (and probably unimplementable) on Windows, so there's
656 * no point in trying to make the handler portable to non-Unix. */
658 _dbus_set_signal_handler (SIGTERM, signal_handler);
659 _dbus_set_signal_handler (SIGHUP, signal_handler);
660 #endif /* DBUS_UNIX */
662 dbus_set_protocol_version (DBUS_MAJOR_PROTOCOL_VERSION);
665 sd_notify(0, "READY=1");
668 _dbus_verbose ("We are on D-Bus...\n");
669 _dbus_loop_run (bus_context_get_loop (context));
671 bus_context_shutdown (context);
672 bus_context_unref (context);
673 bus_selinux_shutdown ();
674 bus_apparmor_shutdown ();
675 bus_audit_shutdown ();