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 **);
53 signal_handler (int sig)
58 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
60 /* explicit fall-through */
61 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
66 _dbus_string_init_const (&str, "foo");
67 if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
68 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
70 /* If we receive SIGHUP often enough to fill the pipe buffer (4096
71 * times on old Linux, 65536 on modern Linux) before it can be
72 * drained, let's just warn and ignore. The configuration will be
73 * reloaded while draining the pipe buffer, which is what we
74 * wanted. It's harmless that it will be reloaded fewer times than
75 * we asked for, since the reload is delayed anyway, so new changes
78 * We use write() because _dbus_warn uses vfprintf, which isn't
81 * This is necessarily Unix-specific, but so are POSIX signals,
83 static const char message[] =
84 "Unable to write to reload pipe - buffer full?\n";
86 write (STDERR_FILENO, message, strlen (message));
93 #endif /* DBUS_UNIX */
98 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");
105 printf ("D-Bus Message Bus Daemon %s\n"
106 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
107 "This is free software; see the source for copying conditions.\n"
108 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
109 DBUS_VERSION_STRING);
117 const char *v_STRING;
119 if (!_dbus_string_init (&xml))
122 if (!bus_driver_generate_introspect_string (&xml))
124 _dbus_string_free (&xml);
128 v_STRING = _dbus_string_get_const_data (&xml);
129 printf ("%s\n", v_STRING);
134 _dbus_warn ("Can not introspect - Out of memory\n");
139 check_two_config_files (const DBusString *config_file,
140 const char *extra_arg)
142 if (_dbus_string_get_length (config_file) > 0)
144 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
145 extra_arg, _dbus_string_get_const_data (config_file));
151 check_two_addresses (const DBusString *address,
152 const char *extra_arg)
154 if (_dbus_string_get_length (address) > 0)
156 fprintf (stderr, "--%s specified but address %s already requested\n",
157 extra_arg, _dbus_string_get_const_data (address));
163 check_two_addr_descriptors (const DBusString *addr_fd,
164 const char *extra_arg)
166 if (_dbus_string_get_length (addr_fd) > 0)
168 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
169 extra_arg, _dbus_string_get_const_data (addr_fd));
175 check_two_pid_descriptors (const DBusString *pid_fd,
176 const char *extra_arg)
178 if (_dbus_string_get_length (pid_fd) > 0)
180 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
181 extra_arg, _dbus_string_get_const_data (pid_fd));
187 handle_reload_watch (DBusWatch *watch,
194 while (!_dbus_string_init (&str))
195 _dbus_wait_for_memory ();
197 if ((reload_pipe[RELOAD_READ_END] > 0) &&
198 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
200 _dbus_warn ("Couldn't read from reload pipe.\n");
201 close_reload_pipe (&watch);
204 _dbus_string_free (&str);
206 /* this can only fail if we don't understand the config file
207 * or OOM. Either way we should just stick with the currently
210 dbus_error_init (&error);
211 if (! bus_context_reload_config (context, &error))
213 _DBUS_ASSERT_ERROR_IS_SET (&error);
214 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
215 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
216 _dbus_warn ("Unable to reload configuration: %s\n",
218 dbus_error_free (&error);
224 setup_reload_pipe (DBusLoop *loop)
229 dbus_error_init (&error);
231 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
234 _dbus_warn ("Unable to create reload pipe: %s\n",
236 dbus_error_free (&error);
240 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
241 DBUS_WATCH_READABLE, TRUE,
242 handle_reload_watch, NULL, NULL);
246 _dbus_warn ("Unable to create reload watch: %s\n",
248 dbus_error_free (&error);
252 if (!_dbus_loop_add_watch (loop, watch))
254 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
256 dbus_error_free (&error);
263 close_reload_pipe (DBusWatch **watch)
265 _dbus_loop_remove_watch (bus_context_get_loop (context), *watch);
266 _dbus_watch_invalidate (*watch);
267 _dbus_watch_unref (*watch);
270 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
271 reload_pipe[RELOAD_READ_END] = -1;
273 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
274 reload_pipe[RELOAD_WRITE_END] = -1;
278 main (int argc, char **argv)
281 DBusString config_file;
285 const char *prev_arg;
286 DBusPipe print_addr_pipe;
287 DBusPipe print_pid_pipe;
289 dbus_bool_t print_address;
290 dbus_bool_t print_pid;
291 dbus_bool_t is_session_bus;
293 dbus_bool_t systemd_activation;
295 if (!_dbus_string_init (&config_file))
298 if (!_dbus_string_init (&address))
301 if (!_dbus_string_init (&addr_fd))
304 if (!_dbus_string_init (&pid_fd))
307 print_address = FALSE;
309 is_session_bus = FALSE;
310 force_fork = FORK_FOLLOW_CONFIG_FILE;
311 systemd_activation = FALSE;
317 const char *arg = argv[i];
319 if (strcmp (arg, "--help") == 0 ||
320 strcmp (arg, "-h") == 0 ||
321 strcmp (arg, "-?") == 0)
323 else if (strcmp (arg, "--version") == 0)
325 else if (strcmp (arg, "--introspect") == 0)
327 else if (strcmp (arg, "--nofork") == 0)
328 force_fork = FORK_NEVER;
329 else if (strcmp (arg, "--fork") == 0)
330 force_fork = FORK_ALWAYS;
331 else if (strcmp (arg, "--systemd-activation") == 0)
332 systemd_activation = TRUE;
333 else if (strcmp (arg, "--system") == 0)
335 check_two_config_files (&config_file, "system");
337 if (!_dbus_append_system_config_file (&config_file))
340 else if (strcmp (arg, "--session") == 0)
342 check_two_config_files (&config_file, "session");
344 if (!_dbus_append_session_config_file (&config_file))
347 else if (strstr (arg, "--config-file=") == arg)
351 check_two_config_files (&config_file, "config-file");
353 file = strchr (arg, '=');
356 if (!_dbus_string_append (&config_file, file))
360 strcmp (prev_arg, "--config-file") == 0)
362 check_two_config_files (&config_file, "config-file");
364 if (!_dbus_string_append (&config_file, arg))
367 else if (strcmp (arg, "--config-file") == 0)
368 ; /* wait for next arg */
369 else if (strstr (arg, "--address=") == arg)
373 check_two_addresses (&address, "address");
375 file = strchr (arg, '=');
378 if (!_dbus_string_append (&address, file))
382 strcmp (prev_arg, "--address") == 0)
384 check_two_addresses (&address, "address");
386 if (!_dbus_string_append (&address, arg))
389 else if (strcmp (arg, "--address") == 0)
390 ; /* wait for next arg */
391 else if (strstr (arg, "--print-address=") == arg)
395 check_two_addr_descriptors (&addr_fd, "print-address");
397 desc = strchr (arg, '=');
400 if (!_dbus_string_append (&addr_fd, desc))
403 print_address = TRUE;
406 strcmp (prev_arg, "--print-address") == 0)
408 check_two_addr_descriptors (&addr_fd, "print-address");
410 if (!_dbus_string_append (&addr_fd, arg))
413 print_address = TRUE;
415 else if (strcmp (arg, "--print-address") == 0)
416 print_address = TRUE; /* and we'll get the next arg if appropriate */
417 else if (strstr (arg, "--print-pid=") == arg)
421 check_two_pid_descriptors (&pid_fd, "print-pid");
423 desc = strchr (arg, '=');
426 if (!_dbus_string_append (&pid_fd, desc))
432 strcmp (prev_arg, "--print-pid") == 0)
434 check_two_pid_descriptors (&pid_fd, "print-pid");
436 if (!_dbus_string_append (&pid_fd, arg))
441 else if (strcmp (arg, "--print-pid") == 0)
442 print_pid = TRUE; /* and we'll get the next arg if appropriate */
451 if (_dbus_string_get_length (&config_file) == 0)
453 fprintf (stderr, "No configuration file specified.\n");
457 _dbus_pipe_invalidate (&print_addr_pipe);
460 _dbus_pipe_init_stdout (&print_addr_pipe);
461 if (_dbus_string_get_length (&addr_fd) > 0)
465 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
466 end != _dbus_string_get_length (&addr_fd) ||
467 val < 0 || val > _DBUS_INT_MAX)
469 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
470 _dbus_string_get_const_data (&addr_fd));
474 _dbus_pipe_init (&print_addr_pipe, val);
477 _dbus_string_free (&addr_fd);
479 _dbus_pipe_invalidate (&print_pid_pipe);
482 _dbus_pipe_init_stdout (&print_pid_pipe);
483 if (_dbus_string_get_length (&pid_fd) > 0)
487 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
488 end != _dbus_string_get_length (&pid_fd) ||
489 val < 0 || val > _DBUS_INT_MAX)
491 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
492 _dbus_string_get_const_data (&pid_fd));
496 _dbus_pipe_init (&print_pid_pipe, val);
499 _dbus_string_free (&pid_fd);
501 if (!bus_selinux_pre_init ())
503 _dbus_warn ("SELinux pre-initialization failed\n");
507 dbus_error_init (&error);
508 context = bus_context_new (&config_file, force_fork,
509 &print_addr_pipe, &print_pid_pipe,
510 _dbus_string_get_length(&address) > 0 ? &address : NULL,
513 _dbus_string_free (&config_file);
516 _dbus_warn ("Failed to start message bus: %s\n",
518 dbus_error_free (&error);
522 /* bus_context_new() closes the print_addr_pipe and
526 setup_reload_pipe (bus_context_get_loop (context));
529 /* POSIX signals are Unix-specific, and _dbus_set_signal_handler is
530 * unimplemented (and probably unimplementable) on Windows, so there's
531 * no point in trying to make the handler portable to non-Unix. */
533 _dbus_set_signal_handler (SIGHUP, signal_handler);
535 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
536 _dbus_set_signal_handler (SIGIO, signal_handler);
537 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
538 #endif /* DBUS_UNIX */
540 _dbus_verbose ("We are on D-Bus...\n");
541 _dbus_loop_run (bus_context_get_loop (context));
543 bus_context_shutdown (context);
544 bus_context_unref (context);
545 bus_selinux_shutdown ();