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;
45 static int reload_pipe[2];
46 #define RELOAD_READ_END 0
47 #define RELOAD_WRITE_END 1
49 static void close_reload_pipe (DBusWatch **);
59 signal_handler (int sig)
63 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
65 /* explicit fall-through */
66 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
71 char action[2] = { ACTION_RELOAD, '\0' };
73 _dbus_string_init_const (&str, action);
74 if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
75 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
77 /* If we receive SIGHUP often enough to fill the pipe buffer (4096
78 * times on old Linux, 65536 on modern Linux) before it can be
79 * drained, let's just warn and ignore. The configuration will be
80 * reloaded while draining the pipe buffer, which is what we
81 * wanted. It's harmless that it will be reloaded fewer times than
82 * we asked for, since the reload is delayed anyway, so new changes
85 * We use write() because _dbus_warn uses vfprintf, which isn't
88 * This is necessarily Unix-specific, but so are POSIX signals,
90 static const char message[] =
91 "Unable to write to reload pipe - buffer full?\n";
93 write (STDERR_FILENO, message, strlen (message));
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 write (STDERR_FILENO, message, strlen (message));
122 #endif /* DBUS_UNIX */
127 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");
134 printf ("D-Bus Message Bus Daemon %s\n"
135 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
136 "This is free software; see the source for copying conditions.\n"
137 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
138 DBUS_VERSION_STRING);
146 const char *v_STRING;
148 if (!_dbus_string_init (&xml))
151 if (!bus_driver_generate_introspect_string (&xml))
153 _dbus_string_free (&xml);
157 v_STRING = _dbus_string_get_const_data (&xml);
158 printf ("%s\n", v_STRING);
163 _dbus_warn ("Can not introspect - Out of memory\n");
168 check_two_config_files (const DBusString *config_file,
169 const char *extra_arg)
171 if (_dbus_string_get_length (config_file) > 0)
173 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
174 extra_arg, _dbus_string_get_const_data (config_file));
180 check_two_addresses (const DBusString *address,
181 const char *extra_arg)
183 if (_dbus_string_get_length (address) > 0)
185 fprintf (stderr, "--%s specified but address %s already requested\n",
186 extra_arg, _dbus_string_get_const_data (address));
192 check_two_addr_descriptors (const DBusString *addr_fd,
193 const char *extra_arg)
195 if (_dbus_string_get_length (addr_fd) > 0)
197 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
198 extra_arg, _dbus_string_get_const_data (addr_fd));
204 check_two_pid_descriptors (const DBusString *pid_fd,
205 const char *extra_arg)
207 if (_dbus_string_get_length (pid_fd) > 0)
209 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
210 extra_arg, _dbus_string_get_const_data (pid_fd));
216 handle_reload_watch (DBusWatch *watch,
225 while (!_dbus_string_init (&str))
226 _dbus_wait_for_memory ();
228 if ((reload_pipe[RELOAD_READ_END] > 0) &&
229 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
231 _dbus_warn ("Couldn't read from reload pipe.\n");
232 close_reload_pipe (&watch);
236 action_str = _dbus_string_get_data (&str);
237 if (action_str != NULL)
239 action = action_str[0];
241 _dbus_string_free (&str);
243 /* this can only fail if we don't understand the config file
244 * or OOM. Either way we should just stick with the currently
247 dbus_error_init (&error);
252 if (! bus_context_reload_config (context, &error))
254 _DBUS_ASSERT_ERROR_IS_SET (&error);
255 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
256 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
257 _dbus_warn ("Unable to reload configuration: %s\n",
259 dbus_error_free (&error);
267 * On OSs without abstract sockets, we want to quit
268 * gracefully rather than being killed by SIGTERM,
269 * so that DBusServer gets a chance to clean up the
270 * sockets from the filesystem. fd.o #38656
272 loop = bus_context_get_loop (context);
275 _dbus_loop_quit (loop);
288 setup_reload_pipe (DBusLoop *loop)
293 dbus_error_init (&error);
295 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
298 _dbus_warn ("Unable to create reload pipe: %s\n",
300 dbus_error_free (&error);
304 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
305 DBUS_WATCH_READABLE, TRUE,
306 handle_reload_watch, NULL, NULL);
310 _dbus_warn ("Unable to create reload watch: %s\n",
312 dbus_error_free (&error);
316 if (!_dbus_loop_add_watch (loop, watch))
318 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
320 dbus_error_free (&error);
327 close_reload_pipe (DBusWatch **watch)
329 _dbus_loop_remove_watch (bus_context_get_loop (context), *watch);
330 _dbus_watch_invalidate (*watch);
331 _dbus_watch_unref (*watch);
334 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
335 reload_pipe[RELOAD_READ_END] = -1;
337 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
338 reload_pipe[RELOAD_WRITE_END] = -1;
342 main (int argc, char **argv)
345 DBusString config_file;
349 const char *prev_arg;
350 DBusPipe print_addr_pipe;
351 DBusPipe print_pid_pipe;
353 dbus_bool_t print_address;
354 dbus_bool_t print_pid;
356 dbus_bool_t systemd_activation;
358 if (!_dbus_string_init (&config_file))
361 if (!_dbus_string_init (&address))
364 if (!_dbus_string_init (&addr_fd))
367 if (!_dbus_string_init (&pid_fd))
370 print_address = FALSE;
372 force_fork = FORK_FOLLOW_CONFIG_FILE;
373 systemd_activation = FALSE;
379 const char *arg = argv[i];
381 if (strcmp (arg, "--help") == 0 ||
382 strcmp (arg, "-h") == 0 ||
383 strcmp (arg, "-?") == 0)
385 else if (strcmp (arg, "--version") == 0)
387 else if (strcmp (arg, "--introspect") == 0)
389 else if (strcmp (arg, "--nofork") == 0)
390 force_fork = FORK_NEVER;
391 else if (strcmp (arg, "--fork") == 0)
392 force_fork = FORK_ALWAYS;
393 else if (strcmp (arg, "--systemd-activation") == 0)
394 systemd_activation = TRUE;
395 else if (strcmp (arg, "--system") == 0)
397 check_two_config_files (&config_file, "system");
399 if (!_dbus_append_system_config_file (&config_file))
402 else if (strcmp (arg, "--session") == 0)
404 check_two_config_files (&config_file, "session");
406 if (!_dbus_append_session_config_file (&config_file))
409 else if (strstr (arg, "--config-file=") == arg)
413 check_two_config_files (&config_file, "config-file");
415 file = strchr (arg, '=');
418 if (!_dbus_string_append (&config_file, file))
422 strcmp (prev_arg, "--config-file") == 0)
424 check_two_config_files (&config_file, "config-file");
426 if (!_dbus_string_append (&config_file, arg))
429 else if (strcmp (arg, "--config-file") == 0)
430 ; /* wait for next arg */
431 else if (strstr (arg, "--address=") == arg)
435 check_two_addresses (&address, "address");
437 file = strchr (arg, '=');
440 if (!_dbus_string_append (&address, file))
444 strcmp (prev_arg, "--address") == 0)
446 check_two_addresses (&address, "address");
448 if (!_dbus_string_append (&address, arg))
451 else if (strcmp (arg, "--address") == 0)
452 ; /* wait for next arg */
453 else if (strstr (arg, "--print-address=") == arg)
457 check_two_addr_descriptors (&addr_fd, "print-address");
459 desc = strchr (arg, '=');
462 if (!_dbus_string_append (&addr_fd, desc))
465 print_address = TRUE;
468 strcmp (prev_arg, "--print-address") == 0)
470 check_two_addr_descriptors (&addr_fd, "print-address");
472 if (!_dbus_string_append (&addr_fd, arg))
475 print_address = TRUE;
477 else if (strcmp (arg, "--print-address") == 0)
478 print_address = TRUE; /* and we'll get the next arg if appropriate */
479 else if (strstr (arg, "--print-pid=") == arg)
483 check_two_pid_descriptors (&pid_fd, "print-pid");
485 desc = strchr (arg, '=');
488 if (!_dbus_string_append (&pid_fd, desc))
494 strcmp (prev_arg, "--print-pid") == 0)
496 check_two_pid_descriptors (&pid_fd, "print-pid");
498 if (!_dbus_string_append (&pid_fd, arg))
503 else if (strcmp (arg, "--print-pid") == 0)
504 print_pid = TRUE; /* and we'll get the next arg if appropriate */
513 if (_dbus_string_get_length (&config_file) == 0)
515 fprintf (stderr, "No configuration file specified.\n");
519 _dbus_pipe_invalidate (&print_addr_pipe);
522 _dbus_pipe_init_stdout (&print_addr_pipe);
523 if (_dbus_string_get_length (&addr_fd) > 0)
527 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
528 end != _dbus_string_get_length (&addr_fd) ||
529 val < 0 || val > _DBUS_INT_MAX)
531 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
532 _dbus_string_get_const_data (&addr_fd));
536 _dbus_pipe_init (&print_addr_pipe, val);
539 _dbus_string_free (&addr_fd);
541 _dbus_pipe_invalidate (&print_pid_pipe);
544 _dbus_pipe_init_stdout (&print_pid_pipe);
545 if (_dbus_string_get_length (&pid_fd) > 0)
549 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
550 end != _dbus_string_get_length (&pid_fd) ||
551 val < 0 || val > _DBUS_INT_MAX)
553 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
554 _dbus_string_get_const_data (&pid_fd));
558 _dbus_pipe_init (&print_pid_pipe, val);
561 _dbus_string_free (&pid_fd);
563 if (!bus_selinux_pre_init ())
565 _dbus_warn ("SELinux pre-initialization failed\n");
569 dbus_error_init (&error);
570 context = bus_context_new (&config_file, force_fork,
571 &print_addr_pipe, &print_pid_pipe,
572 _dbus_string_get_length(&address) > 0 ? &address : NULL,
575 _dbus_string_free (&config_file);
578 _dbus_warn ("Failed to start message bus: %s\n",
580 dbus_error_free (&error);
584 /* bus_context_new() closes the print_addr_pipe and
588 setup_reload_pipe (bus_context_get_loop (context));
591 /* POSIX signals are Unix-specific, and _dbus_set_signal_handler is
592 * unimplemented (and probably unimplementable) on Windows, so there's
593 * no point in trying to make the handler portable to non-Unix. */
595 _dbus_set_signal_handler (SIGTERM, signal_handler);
597 _dbus_set_signal_handler (SIGHUP, signal_handler);
599 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
600 _dbus_set_signal_handler (SIGIO, signal_handler);
601 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
602 #endif /* DBUS_UNIX */
604 _dbus_verbose ("We are on D-Bus...\n");
605 _dbus_loop_run (bus_context_get_loop (context));
607 bus_context_shutdown (context);
608 bus_context_unref (context);
609 bus_selinux_shutdown ();