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>
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 (void);
58 signal_handler (int sig)
62 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
64 /* explicit fall-through */
65 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
70 char action[2] = { ACTION_RELOAD, '\0' };
72 _dbus_string_init_const (&str, action);
73 if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
74 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
76 static const char message[] =
77 "Unable to write to reload pipe - buffer full?\n";
79 write (STDERR_FILENO, message, strlen (message));
88 char action[2] = { ACTION_QUIT, '\0' };
89 _dbus_string_init_const (&str, action);
90 if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
91 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
93 static const char message[] =
94 "Unable to write to reload pipe - buffer full?\n";
96 write (STDERR_FILENO, message, strlen (message));
106 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");
113 printf ("D-Bus Message Bus Daemon %s\n"
114 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
115 "This is free software; see the source for copying conditions.\n"
116 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
117 DBUS_VERSION_STRING);
125 const char *v_STRING;
127 if (!_dbus_string_init (&xml))
130 if (!bus_driver_generate_introspect_string (&xml))
132 _dbus_string_free (&xml);
136 v_STRING = _dbus_string_get_const_data (&xml);
137 printf ("%s\n", v_STRING);
142 _dbus_warn ("Can not introspect - Out of memory\n");
147 check_two_config_files (const DBusString *config_file,
148 const char *extra_arg)
150 if (_dbus_string_get_length (config_file) > 0)
152 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
153 extra_arg, _dbus_string_get_const_data (config_file));
159 check_two_addresses (const DBusString *address,
160 const char *extra_arg)
162 if (_dbus_string_get_length (address) > 0)
164 fprintf (stderr, "--%s specified but address %s already requested\n",
165 extra_arg, _dbus_string_get_const_data (address));
171 check_two_addr_descriptors (const DBusString *addr_fd,
172 const char *extra_arg)
174 if (_dbus_string_get_length (addr_fd) > 0)
176 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
177 extra_arg, _dbus_string_get_const_data (addr_fd));
183 check_two_pid_descriptors (const DBusString *pid_fd,
184 const char *extra_arg)
186 if (_dbus_string_get_length (pid_fd) > 0)
188 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
189 extra_arg, _dbus_string_get_const_data (pid_fd));
195 handle_reload_watch (DBusWatch *watch,
204 while (!_dbus_string_init (&str))
205 _dbus_wait_for_memory ();
207 if ((reload_pipe[RELOAD_READ_END] > 0) &&
208 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
210 _dbus_warn ("Couldn't read from reload pipe.\n");
211 close_reload_pipe ();
215 action_str = _dbus_string_get_data (&str);
216 if (action_str != NULL)
218 action = action_str[0];
220 _dbus_string_free (&str);
222 /* this can only fail if we don't understand the config file
223 * or OOM. Either way we should just stick with the currently
226 dbus_error_init (&error);
231 if (! bus_context_reload_config (context, &error))
233 _DBUS_ASSERT_ERROR_IS_SET (&error);
234 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
235 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
236 _dbus_warn ("Unable to reload configuration: %s\n",
238 dbus_error_free (&error);
246 * On OSs without abstract sockets, we want to quit
247 * gracefully rather than being killed by SIGTERM,
248 * so that DBusServer gets a chance to clean up the
249 * sockets from the filesystem. fd.o #38656
251 loop = bus_context_get_loop (context);
254 _dbus_loop_quit (loop);
267 reload_watch_callback (DBusWatch *watch,
268 unsigned int condition,
271 return dbus_watch_handle (watch, condition);
275 setup_reload_pipe (DBusLoop *loop)
280 dbus_error_init (&error);
282 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
285 _dbus_warn ("Unable to create reload pipe: %s\n",
287 dbus_error_free (&error);
291 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
292 DBUS_WATCH_READABLE, TRUE,
293 handle_reload_watch, NULL, NULL);
297 _dbus_warn ("Unable to create reload watch: %s\n",
299 dbus_error_free (&error);
303 if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
306 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
308 dbus_error_free (&error);
315 close_reload_pipe (void)
317 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
318 reload_pipe[RELOAD_READ_END] = -1;
320 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
321 reload_pipe[RELOAD_WRITE_END] = -1;
325 main (int argc, char **argv)
328 DBusString config_file;
332 const char *prev_arg;
333 DBusPipe print_addr_pipe;
334 DBusPipe print_pid_pipe;
336 dbus_bool_t print_address;
337 dbus_bool_t print_pid;
338 dbus_bool_t is_session_bus;
340 dbus_bool_t systemd_activation;
342 if (!_dbus_string_init (&config_file))
345 if (!_dbus_string_init (&address))
348 if (!_dbus_string_init (&addr_fd))
351 if (!_dbus_string_init (&pid_fd))
354 print_address = FALSE;
356 is_session_bus = FALSE;
357 force_fork = FORK_FOLLOW_CONFIG_FILE;
358 systemd_activation = FALSE;
364 const char *arg = argv[i];
366 if (strcmp (arg, "--help") == 0 ||
367 strcmp (arg, "-h") == 0 ||
368 strcmp (arg, "-?") == 0)
370 else if (strcmp (arg, "--version") == 0)
372 else if (strcmp (arg, "--introspect") == 0)
374 else if (strcmp (arg, "--nofork") == 0)
375 force_fork = FORK_NEVER;
376 else if (strcmp (arg, "--fork") == 0)
377 force_fork = FORK_ALWAYS;
378 else if (strcmp (arg, "--systemd-activation") == 0)
379 systemd_activation = TRUE;
380 else if (strcmp (arg, "--system") == 0)
382 check_two_config_files (&config_file, "system");
384 if (!_dbus_append_system_config_file (&config_file))
387 else if (strcmp (arg, "--session") == 0)
389 check_two_config_files (&config_file, "session");
391 if (!_dbus_append_session_config_file (&config_file))
394 else if (strstr (arg, "--config-file=") == arg)
398 check_two_config_files (&config_file, "config-file");
400 file = strchr (arg, '=');
403 if (!_dbus_string_append (&config_file, file))
407 strcmp (prev_arg, "--config-file") == 0)
409 check_two_config_files (&config_file, "config-file");
411 if (!_dbus_string_append (&config_file, arg))
414 else if (strcmp (arg, "--config-file") == 0)
415 ; /* wait for next arg */
416 else if (strstr (arg, "--address=") == arg)
420 check_two_addresses (&address, "address");
422 file = strchr (arg, '=');
425 if (!_dbus_string_append (&address, file))
429 strcmp (prev_arg, "--address") == 0)
431 check_two_addresses (&address, "address");
433 if (!_dbus_string_append (&address, arg))
436 else if (strcmp (arg, "--address") == 0)
437 ; /* wait for next arg */
438 else if (strstr (arg, "--print-address=") == arg)
442 check_two_addr_descriptors (&addr_fd, "print-address");
444 desc = strchr (arg, '=');
447 if (!_dbus_string_append (&addr_fd, desc))
450 print_address = TRUE;
453 strcmp (prev_arg, "--print-address") == 0)
455 check_two_addr_descriptors (&addr_fd, "print-address");
457 if (!_dbus_string_append (&addr_fd, arg))
460 print_address = TRUE;
462 else if (strcmp (arg, "--print-address") == 0)
463 print_address = TRUE; /* and we'll get the next arg if appropriate */
464 else if (strstr (arg, "--print-pid=") == arg)
468 check_two_pid_descriptors (&pid_fd, "print-pid");
470 desc = strchr (arg, '=');
473 if (!_dbus_string_append (&pid_fd, desc))
479 strcmp (prev_arg, "--print-pid") == 0)
481 check_two_pid_descriptors (&pid_fd, "print-pid");
483 if (!_dbus_string_append (&pid_fd, arg))
488 else if (strcmp (arg, "--print-pid") == 0)
489 print_pid = TRUE; /* and we'll get the next arg if appropriate */
498 if (_dbus_string_get_length (&config_file) == 0)
500 fprintf (stderr, "No configuration file specified.\n");
504 _dbus_pipe_invalidate (&print_addr_pipe);
507 _dbus_pipe_init_stdout (&print_addr_pipe);
508 if (_dbus_string_get_length (&addr_fd) > 0)
512 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
513 end != _dbus_string_get_length (&addr_fd) ||
514 val < 0 || val > _DBUS_INT_MAX)
516 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
517 _dbus_string_get_const_data (&addr_fd));
521 _dbus_pipe_init (&print_addr_pipe, val);
524 _dbus_string_free (&addr_fd);
526 _dbus_pipe_invalidate (&print_pid_pipe);
529 _dbus_pipe_init_stdout (&print_pid_pipe);
530 if (_dbus_string_get_length (&pid_fd) > 0)
534 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
535 end != _dbus_string_get_length (&pid_fd) ||
536 val < 0 || val > _DBUS_INT_MAX)
538 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
539 _dbus_string_get_const_data (&pid_fd));
543 _dbus_pipe_init (&print_pid_pipe, val);
546 _dbus_string_free (&pid_fd);
548 if (!bus_selinux_pre_init ())
550 _dbus_warn ("SELinux pre-initialization failed\n");
554 dbus_error_init (&error);
555 context = bus_context_new (&config_file, force_fork,
556 &print_addr_pipe, &print_pid_pipe,
557 _dbus_string_get_length(&address) > 0 ? &address : NULL,
560 _dbus_string_free (&config_file);
563 _dbus_warn ("Failed to start message bus: %s\n",
565 dbus_error_free (&error);
569 /* bus_context_new() closes the print_addr_pipe and
573 setup_reload_pipe (bus_context_get_loop (context));
575 _dbus_set_signal_handler (SIGTERM, signal_handler);
577 _dbus_set_signal_handler (SIGHUP, signal_handler);
579 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
580 _dbus_set_signal_handler (SIGIO, signal_handler);
581 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
583 _dbus_verbose ("We are on D-Bus...\n");
584 _dbus_loop_run (bus_context_get_loop (context));
586 bus_context_shutdown (context);
587 bus_context_unref (context);
588 bus_selinux_shutdown ();