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)
68 char action[2] = { ACTION_RELOAD, '\0' };
70 _dbus_string_init_const (&str, action);
71 if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
72 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
74 /* If we receive SIGHUP often enough to fill the pipe buffer (4096
75 * times on old Linux, 65536 on modern Linux) before it can be
76 * drained, let's just warn and ignore. The configuration will be
77 * reloaded while draining the pipe buffer, which is what we
78 * wanted. It's harmless that it will be reloaded fewer times than
79 * we asked for, since the reload is delayed anyway, so new changes
82 * We use write() because _dbus_warn uses vfprintf, which isn't
85 * This is necessarily Unix-specific, but so are POSIX signals,
87 static const char message[] =
88 "Unable to write to reload pipe - buffer full?\n";
90 if (write (STDERR_FILENO, message, strlen (message)) != strlen (message))
92 /* ignore failure to write out a warning */
102 char action[2] = { ACTION_QUIT, '\0' };
103 _dbus_string_init_const (&str, action);
104 if ((reload_pipe[RELOAD_WRITE_END] < 0) ||
105 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
107 /* If we can't write to the socket, dying seems a more
108 * important response to SIGTERM than cleaning up sockets,
109 * so we exit. We'd use exit(), but that's not async-signal-safe,
110 * so we'll have to resort to _exit(). */
111 static const char message[] =
112 "Unable to write termination signal to pipe - buffer full?\n"
113 "Will exit instead.\n";
115 if (write (STDERR_FILENO, message, strlen (message)) != strlen (message))
117 /* ignore failure to write out a warning */
125 #endif /* DBUS_UNIX */
135 " [--config-file=FILE]"
136 " [--print-address[=DESCRIPTOR]]"
137 " [--print-pid[=DESCRIPTOR]]"
139 " [--address=ADDRESS]"
144 " [--systemd-activation]"
153 printf ("D-Bus Message Bus Daemon %s\n"
154 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
155 "This is free software; see the source for copying conditions.\n"
156 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
157 DBUS_VERSION_STRING);
165 const char *v_STRING;
167 if (!_dbus_string_init (&xml))
170 if (!bus_driver_generate_introspect_string (&xml))
172 _dbus_string_free (&xml);
176 v_STRING = _dbus_string_get_const_data (&xml);
177 printf ("%s\n", v_STRING);
182 _dbus_warn ("Can not introspect - Out of memory\n");
187 check_two_config_files (const DBusString *config_file,
188 const char *extra_arg)
190 if (_dbus_string_get_length (config_file) > 0)
192 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
193 extra_arg, _dbus_string_get_const_data (config_file));
199 check_two_addresses (const DBusString *address,
200 const char *extra_arg)
202 if (_dbus_string_get_length (address) > 0)
204 fprintf (stderr, "--%s specified but address %s already requested\n",
205 extra_arg, _dbus_string_get_const_data (address));
211 check_two_addr_descriptors (const DBusString *addr_fd,
212 const char *extra_arg)
214 if (_dbus_string_get_length (addr_fd) > 0)
216 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
217 extra_arg, _dbus_string_get_const_data (addr_fd));
223 check_two_pid_descriptors (const DBusString *pid_fd,
224 const char *extra_arg)
226 if (_dbus_string_get_length (pid_fd) > 0)
228 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
229 extra_arg, _dbus_string_get_const_data (pid_fd));
236 handle_reload_watch (DBusWatch *watch,
245 while (!_dbus_string_init (&str))
246 _dbus_wait_for_memory ();
248 if ((reload_pipe[RELOAD_READ_END] > 0) &&
249 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
251 _dbus_warn ("Couldn't read from reload pipe.\n");
252 close_reload_pipe (&watch);
256 action_str = _dbus_string_get_data (&str);
257 if (action_str != NULL)
259 action = action_str[0];
261 _dbus_string_free (&str);
263 /* this can only fail if we don't understand the config file
264 * or OOM. Either way we should just stick with the currently
267 dbus_error_init (&error);
272 if (! bus_context_reload_config (context, &error))
274 _DBUS_ASSERT_ERROR_IS_SET (&error);
275 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
276 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
277 _dbus_warn ("Unable to reload configuration: %s\n",
279 dbus_error_free (&error);
287 * On OSs without abstract sockets, we want to quit
288 * gracefully rather than being killed by SIGTERM,
289 * so that DBusServer gets a chance to clean up the
290 * sockets from the filesystem. fd.o #38656
292 loop = bus_context_get_loop (context);
295 _dbus_loop_quit (loop);
308 setup_reload_pipe (DBusLoop *loop)
313 dbus_error_init (&error);
315 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
318 _dbus_warn ("Unable to create reload pipe: %s\n",
320 dbus_error_free (&error);
324 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
325 DBUS_WATCH_READABLE, TRUE,
326 handle_reload_watch, NULL, NULL);
330 _dbus_warn ("Unable to create reload watch: %s\n",
332 dbus_error_free (&error);
336 if (!_dbus_loop_add_watch (loop, watch))
338 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
340 dbus_error_free (&error);
347 close_reload_pipe (DBusWatch **watch)
349 _dbus_loop_remove_watch (bus_context_get_loop (context), *watch);
350 _dbus_watch_invalidate (*watch);
351 _dbus_watch_unref (*watch);
354 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
355 reload_pipe[RELOAD_READ_END] = -1;
357 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
358 reload_pipe[RELOAD_WRITE_END] = -1;
360 #endif /* DBUS_UNIX */
363 main (int argc, char **argv)
366 DBusString config_file;
370 const char *prev_arg;
371 DBusPipe print_addr_pipe;
372 DBusPipe print_pid_pipe;
374 dbus_bool_t print_address;
375 dbus_bool_t print_pid;
376 BusContextFlags flags;
378 if (!_dbus_string_init (&config_file))
381 if (!_dbus_string_init (&address))
384 if (!_dbus_string_init (&addr_fd))
387 if (!_dbus_string_init (&pid_fd))
390 print_address = FALSE;
393 flags = BUS_CONTEXT_FLAG_WRITE_PID_FILE;
399 const char *arg = argv[i];
401 if (strcmp (arg, "--help") == 0 ||
402 strcmp (arg, "-h") == 0 ||
403 strcmp (arg, "-?") == 0)
407 else if (strcmp (arg, "--version") == 0)
411 else if (strcmp (arg, "--introspect") == 0)
415 else if (strcmp (arg, "--nofork") == 0)
417 flags &= ~BUS_CONTEXT_FLAG_FORK_ALWAYS;
418 flags |= BUS_CONTEXT_FLAG_FORK_NEVER;
421 else if (strcmp (arg, "--fork") == 0)
423 flags &= ~BUS_CONTEXT_FLAG_FORK_NEVER;
424 flags |= BUS_CONTEXT_FLAG_FORK_ALWAYS;
426 else if (strcmp (arg, "--systemd-activation") == 0)
428 flags |= BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION;
431 else if (strcmp (arg, "--nopidfile") == 0)
433 flags &= ~BUS_CONTEXT_FLAG_WRITE_PID_FILE;
435 else if (strcmp (arg, "--system") == 0)
437 check_two_config_files (&config_file, "system");
439 if (!_dbus_append_system_config_file (&config_file))
442 else if (strcmp (arg, "--session") == 0)
444 check_two_config_files (&config_file, "session");
446 if (!_dbus_append_session_config_file (&config_file))
449 else if (strstr (arg, "--config-file=") == arg)
453 check_two_config_files (&config_file, "config-file");
455 file = strchr (arg, '=');
458 if (!_dbus_string_append (&config_file, file))
462 strcmp (prev_arg, "--config-file") == 0)
464 check_two_config_files (&config_file, "config-file");
466 if (!_dbus_string_append (&config_file, arg))
469 else if (strcmp (arg, "--config-file") == 0)
471 /* wait for next arg */
473 else if (strstr (arg, "--address=") == arg)
477 check_two_addresses (&address, "address");
479 file = strchr (arg, '=');
482 if (!_dbus_string_append (&address, file))
486 strcmp (prev_arg, "--address") == 0)
488 check_two_addresses (&address, "address");
490 if (!_dbus_string_append (&address, arg))
493 else if (strcmp (arg, "--address") == 0)
495 /* wait for next arg */
497 else if (strstr (arg, "--print-address=") == arg)
501 check_two_addr_descriptors (&addr_fd, "print-address");
503 desc = strchr (arg, '=');
506 if (!_dbus_string_append (&addr_fd, desc))
509 print_address = TRUE;
512 strcmp (prev_arg, "--print-address") == 0)
514 check_two_addr_descriptors (&addr_fd, "print-address");
516 if (!_dbus_string_append (&addr_fd, arg))
519 print_address = TRUE;
521 else if (strcmp (arg, "--print-address") == 0)
523 print_address = TRUE; /* and we'll get the next arg if appropriate */
525 else if (strstr (arg, "--print-pid=") == arg)
529 check_two_pid_descriptors (&pid_fd, "print-pid");
531 desc = strchr (arg, '=');
534 if (!_dbus_string_append (&pid_fd, desc))
540 strcmp (prev_arg, "--print-pid") == 0)
542 check_two_pid_descriptors (&pid_fd, "print-pid");
544 if (!_dbus_string_append (&pid_fd, arg))
549 else if (strcmp (arg, "--print-pid") == 0)
551 print_pid = TRUE; /* and we'll get the next arg if appropriate */
563 if (_dbus_string_get_length (&config_file) == 0)
565 fprintf (stderr, "No configuration file specified.\n");
569 _dbus_pipe_invalidate (&print_addr_pipe);
572 _dbus_pipe_init_stdout (&print_addr_pipe);
573 if (_dbus_string_get_length (&addr_fd) > 0)
577 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
578 end != _dbus_string_get_length (&addr_fd) ||
579 val < 0 || val > _DBUS_INT_MAX)
581 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
582 _dbus_string_get_const_data (&addr_fd));
586 _dbus_pipe_init (&print_addr_pipe, val);
589 _dbus_string_free (&addr_fd);
591 _dbus_pipe_invalidate (&print_pid_pipe);
594 _dbus_pipe_init_stdout (&print_pid_pipe);
595 if (_dbus_string_get_length (&pid_fd) > 0)
599 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
600 end != _dbus_string_get_length (&pid_fd) ||
601 val < 0 || val > _DBUS_INT_MAX)
603 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
604 _dbus_string_get_const_data (&pid_fd));
608 _dbus_pipe_init (&print_pid_pipe, val);
611 _dbus_string_free (&pid_fd);
613 if (!bus_selinux_pre_init ())
615 _dbus_warn ("SELinux pre-initialization failed\n");
619 dbus_error_init (&error);
620 context = bus_context_new (&config_file, flags,
621 &print_addr_pipe, &print_pid_pipe,
622 _dbus_string_get_length(&address) > 0 ? &address : NULL,
624 _dbus_string_free (&config_file);
627 _dbus_warn ("Failed to start message bus: %s\n",
629 dbus_error_free (&error);
633 /* bus_context_new() closes the print_addr_pipe and
638 setup_reload_pipe (bus_context_get_loop (context));
640 /* POSIX signals are Unix-specific, and _dbus_set_signal_handler is
641 * unimplemented (and probably unimplementable) on Windows, so there's
642 * no point in trying to make the handler portable to non-Unix. */
644 _dbus_set_signal_handler (SIGTERM, signal_handler);
646 _dbus_set_signal_handler (SIGHUP, signal_handler);
648 #endif /* DBUS_UNIX */
650 #ifdef ENABLE_KDBUS_TRANSPORT
651 if (bus_context_get_systemd_activation(context) == TRUE)
653 if (strncmp(bus_context_get_address(context), "kdbus:", strlen("kdbus:")) == 0 &&
654 !strcmp(bus_context_get_type(context), "system") &&
662 _dbus_verbose ("We are on D-Bus...\n");
663 _dbus_loop_run (bus_context_get_loop (context));
665 bus_context_shutdown (context);
666 bus_context_unref (context);
667 bus_selinux_shutdown ();