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 if (write (STDERR_FILENO, message, strlen (message)) != strlen (message))
96 /* ignore failure to write out a warning */
106 char action[2] = { ACTION_QUIT, '\0' };
107 _dbus_string_init_const (&str, action);
108 if ((reload_pipe[RELOAD_WRITE_END] < 0) ||
109 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
111 /* If we can't write to the socket, dying seems a more
112 * important response to SIGTERM than cleaning up sockets,
113 * so we exit. We'd use exit(), but that's not async-signal-safe,
114 * so we'll have to resort to _exit(). */
115 static const char message[] =
116 "Unable to write termination signal to pipe - buffer full?\n"
117 "Will exit instead.\n";
119 if (write (STDERR_FILENO, message, strlen (message)) != strlen (message))
121 /* ignore failure to write out a warning */
129 #endif /* DBUS_UNIX */
139 " [--config-file=FILE]"
140 " [--print-address[=DESCRIPTOR]]"
141 " [--print-pid[=DESCRIPTOR]]"
143 " [--address=ADDRESS]"
148 " [--systemd-activation]"
157 printf ("D-Bus Message Bus Daemon %s\n"
158 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
159 "This is free software; see the source for copying conditions.\n"
160 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
161 DBUS_VERSION_STRING);
169 const char *v_STRING;
171 if (!_dbus_string_init (&xml))
174 if (!bus_driver_generate_introspect_string (&xml))
176 _dbus_string_free (&xml);
180 v_STRING = _dbus_string_get_const_data (&xml);
181 printf ("%s\n", v_STRING);
186 _dbus_warn ("Can not introspect - Out of memory\n");
191 check_two_config_files (const DBusString *config_file,
192 const char *extra_arg)
194 if (_dbus_string_get_length (config_file) > 0)
196 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
197 extra_arg, _dbus_string_get_const_data (config_file));
203 check_two_addresses (const DBusString *address,
204 const char *extra_arg)
206 if (_dbus_string_get_length (address) > 0)
208 fprintf (stderr, "--%s specified but address %s already requested\n",
209 extra_arg, _dbus_string_get_const_data (address));
215 check_two_addr_descriptors (const DBusString *addr_fd,
216 const char *extra_arg)
218 if (_dbus_string_get_length (addr_fd) > 0)
220 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
221 extra_arg, _dbus_string_get_const_data (addr_fd));
227 check_two_pid_descriptors (const DBusString *pid_fd,
228 const char *extra_arg)
230 if (_dbus_string_get_length (pid_fd) > 0)
232 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
233 extra_arg, _dbus_string_get_const_data (pid_fd));
240 handle_reload_watch (DBusWatch *watch,
249 while (!_dbus_string_init (&str))
250 _dbus_wait_for_memory ();
252 if ((reload_pipe[RELOAD_READ_END] > 0) &&
253 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
255 _dbus_warn ("Couldn't read from reload pipe.\n");
256 close_reload_pipe (&watch);
260 action_str = _dbus_string_get_data (&str);
261 if (action_str != NULL)
263 action = action_str[0];
265 _dbus_string_free (&str);
267 /* this can only fail if we don't understand the config file
268 * or OOM. Either way we should just stick with the currently
271 dbus_error_init (&error);
276 if (! bus_context_reload_config (context, &error))
278 _DBUS_ASSERT_ERROR_IS_SET (&error);
279 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
280 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
281 _dbus_warn ("Unable to reload configuration: %s\n",
283 dbus_error_free (&error);
291 * On OSs without abstract sockets, we want to quit
292 * gracefully rather than being killed by SIGTERM,
293 * so that DBusServer gets a chance to clean up the
294 * sockets from the filesystem. fd.o #38656
296 loop = bus_context_get_loop (context);
299 _dbus_loop_quit (loop);
312 setup_reload_pipe (DBusLoop *loop)
317 dbus_error_init (&error);
319 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
322 _dbus_warn ("Unable to create reload pipe: %s\n",
324 dbus_error_free (&error);
328 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
329 DBUS_WATCH_READABLE, TRUE,
330 handle_reload_watch, NULL, NULL);
334 _dbus_warn ("Unable to create reload watch: %s\n",
336 dbus_error_free (&error);
340 if (!_dbus_loop_add_watch (loop, watch))
342 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
344 dbus_error_free (&error);
351 close_reload_pipe (DBusWatch **watch)
353 _dbus_loop_remove_watch (bus_context_get_loop (context), *watch);
354 _dbus_watch_invalidate (*watch);
355 _dbus_watch_unref (*watch);
358 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
359 reload_pipe[RELOAD_READ_END] = -1;
361 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
362 reload_pipe[RELOAD_WRITE_END] = -1;
364 #endif /* DBUS_UNIX */
367 main (int argc, char **argv)
370 DBusString config_file;
374 const char *prev_arg;
375 DBusPipe print_addr_pipe;
376 DBusPipe print_pid_pipe;
378 dbus_bool_t print_address;
379 dbus_bool_t print_pid;
380 BusContextFlags flags;
382 if (!_dbus_string_init (&config_file))
385 if (!_dbus_string_init (&address))
388 if (!_dbus_string_init (&addr_fd))
391 if (!_dbus_string_init (&pid_fd))
394 print_address = FALSE;
397 flags = BUS_CONTEXT_FLAG_WRITE_PID_FILE;
403 const char *arg = argv[i];
405 if (strcmp (arg, "--help") == 0 ||
406 strcmp (arg, "-h") == 0 ||
407 strcmp (arg, "-?") == 0)
411 else if (strcmp (arg, "--version") == 0)
415 else if (strcmp (arg, "--introspect") == 0)
420 else if (strcmp (arg, "--nofork") == 0)
422 flags &= ~BUS_CONTEXT_FLAG_FORK_ALWAYS;
423 flags |= BUS_CONTEXT_FLAG_FORK_NEVER;
425 else if (strcmp (arg, "--fork") == 0)
427 flags &= ~BUS_CONTEXT_FLAG_FORK_NEVER;
428 flags |= BUS_CONTEXT_FLAG_FORK_ALWAYS;
430 else if (strcmp (arg, "--systemd-activation") == 0)
432 flags |= BUS_CONTEXT_FLAG_SYSTEMD_ACTIVATION;
435 else if (strcmp (arg, "--nopidfile") == 0)
437 flags &= ~BUS_CONTEXT_FLAG_WRITE_PID_FILE;
439 else if (strcmp (arg, "--system") == 0)
441 check_two_config_files (&config_file, "system");
443 if (!_dbus_append_system_config_file (&config_file))
446 else if (strcmp (arg, "--session") == 0)
448 check_two_config_files (&config_file, "session");
450 if (!_dbus_append_session_config_file (&config_file))
453 else if (strstr (arg, "--config-file=") == arg)
457 check_two_config_files (&config_file, "config-file");
459 file = strchr (arg, '=');
462 if (!_dbus_string_append (&config_file, file))
466 strcmp (prev_arg, "--config-file") == 0)
468 check_two_config_files (&config_file, "config-file");
470 if (!_dbus_string_append (&config_file, arg))
473 else if (strcmp (arg, "--config-file") == 0)
475 /* wait for next arg */
477 else if (strstr (arg, "--address=") == arg)
481 check_two_addresses (&address, "address");
483 file = strchr (arg, '=');
486 if (!_dbus_string_append (&address, file))
490 strcmp (prev_arg, "--address") == 0)
492 check_two_addresses (&address, "address");
494 if (!_dbus_string_append (&address, arg))
497 else if (strcmp (arg, "--address") == 0)
499 /* wait for next arg */
501 else if (strstr (arg, "--print-address=") == arg)
505 check_two_addr_descriptors (&addr_fd, "print-address");
507 desc = strchr (arg, '=');
510 if (!_dbus_string_append (&addr_fd, desc))
513 print_address = TRUE;
516 strcmp (prev_arg, "--print-address") == 0)
518 check_two_addr_descriptors (&addr_fd, "print-address");
520 if (!_dbus_string_append (&addr_fd, arg))
523 print_address = TRUE;
525 else if (strcmp (arg, "--print-address") == 0)
527 print_address = TRUE; /* and we'll get the next arg if appropriate */
529 else if (strstr (arg, "--print-pid=") == arg)
533 check_two_pid_descriptors (&pid_fd, "print-pid");
535 desc = strchr (arg, '=');
538 if (!_dbus_string_append (&pid_fd, desc))
544 strcmp (prev_arg, "--print-pid") == 0)
546 check_two_pid_descriptors (&pid_fd, "print-pid");
548 if (!_dbus_string_append (&pid_fd, arg))
553 else if (strcmp (arg, "--print-pid") == 0)
555 print_pid = TRUE; /* and we'll get the next arg if appropriate */
567 if (_dbus_string_get_length (&config_file) == 0)
569 fprintf (stderr, "No configuration file specified.\n");
573 _dbus_pipe_invalidate (&print_addr_pipe);
576 _dbus_pipe_init_stdout (&print_addr_pipe);
577 if (_dbus_string_get_length (&addr_fd) > 0)
581 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
582 end != _dbus_string_get_length (&addr_fd) ||
583 val < 0 || val > _DBUS_INT_MAX)
585 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
586 _dbus_string_get_const_data (&addr_fd));
590 _dbus_pipe_init (&print_addr_pipe, val);
593 _dbus_string_free (&addr_fd);
595 _dbus_pipe_invalidate (&print_pid_pipe);
598 _dbus_pipe_init_stdout (&print_pid_pipe);
599 if (_dbus_string_get_length (&pid_fd) > 0)
603 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
604 end != _dbus_string_get_length (&pid_fd) ||
605 val < 0 || val > _DBUS_INT_MAX)
607 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
608 _dbus_string_get_const_data (&pid_fd));
612 _dbus_pipe_init (&print_pid_pipe, val);
615 _dbus_string_free (&pid_fd);
617 if (!bus_selinux_pre_init ())
619 _dbus_warn ("SELinux pre-initialization failed\n");
623 dbus_error_init (&error);
624 context = bus_context_new (&config_file, flags,
625 &print_addr_pipe, &print_pid_pipe,
626 _dbus_string_get_length(&address) > 0 ? &address : NULL,
628 _dbus_string_free (&config_file);
631 _dbus_warn ("Failed to start message bus: %s\n",
633 dbus_error_free (&error);
637 /* bus_context_new() closes the print_addr_pipe and
642 setup_reload_pipe (bus_context_get_loop (context));
644 /* POSIX signals are Unix-specific, and _dbus_set_signal_handler is
645 * unimplemented (and probably unimplementable) on Windows, so there's
646 * no point in trying to make the handler portable to non-Unix. */
648 _dbus_set_signal_handler (SIGTERM, signal_handler);
650 _dbus_set_signal_handler (SIGHUP, signal_handler);
652 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
653 _dbus_set_signal_handler (SIGIO, signal_handler);
654 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
655 #endif /* DBUS_UNIX */
657 _dbus_verbose ("We are on D-Bus...\n");
658 _dbus_loop_run (bus_context_get_loop (context));
660 bus_context_shutdown (context);
661 bus_context_unref (context);
662 bus_selinux_shutdown ();