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>
40 static BusContext *context;
42 static int reload_pipe[2];
43 #define RELOAD_READ_END 0
44 #define RELOAD_WRITE_END 1
46 static void close_reload_pipe (void);
49 signal_handler (int sig)
54 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
56 /* explicit fall-through */
57 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
62 _dbus_string_init_const (&str, "foo");
63 if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
64 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
66 _dbus_warn ("Unable to write to reload pipe.\n");
78 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");
85 printf ("D-Bus Message Bus Daemon %s\n"
86 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
87 "This is free software; see the source for copying conditions.\n"
88 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
99 if (!_dbus_string_init (&xml))
102 if (!bus_driver_generate_introspect_string (&xml))
104 _dbus_string_free (&xml);
108 v_STRING = _dbus_string_get_const_data (&xml);
109 printf ("%s\n", v_STRING);
114 _dbus_warn ("Can not introspect - Out of memory\n");
119 check_two_config_files (const DBusString *config_file,
120 const char *extra_arg)
122 if (_dbus_string_get_length (config_file) > 0)
124 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
125 extra_arg, _dbus_string_get_const_data (config_file));
131 check_two_addresses (const DBusString *address,
132 const char *extra_arg)
134 if (_dbus_string_get_length (address) > 0)
136 fprintf (stderr, "--%s specified but address %s already requested\n",
137 extra_arg, _dbus_string_get_const_data (address));
143 check_two_addr_descriptors (const DBusString *addr_fd,
144 const char *extra_arg)
146 if (_dbus_string_get_length (addr_fd) > 0)
148 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
149 extra_arg, _dbus_string_get_const_data (addr_fd));
155 check_two_pid_descriptors (const DBusString *pid_fd,
156 const char *extra_arg)
158 if (_dbus_string_get_length (pid_fd) > 0)
160 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
161 extra_arg, _dbus_string_get_const_data (pid_fd));
167 handle_reload_watch (DBusWatch *watch,
174 while (!_dbus_string_init (&str))
175 _dbus_wait_for_memory ();
177 if ((reload_pipe[RELOAD_READ_END] > 0) &&
178 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
180 _dbus_warn ("Couldn't read from reload pipe.\n");
181 close_reload_pipe ();
184 _dbus_string_free (&str);
186 /* this can only fail if we don't understand the config file
187 * or OOM. Either way we should just stick with the currently
190 dbus_error_init (&error);
191 if (! bus_context_reload_config (context, &error))
193 _DBUS_ASSERT_ERROR_IS_SET (&error);
194 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
195 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
196 _dbus_warn ("Unable to reload configuration: %s\n",
198 dbus_error_free (&error);
204 reload_watch_callback (DBusWatch *watch,
205 unsigned int condition,
208 return dbus_watch_handle (watch, condition);
212 setup_reload_pipe (DBusLoop *loop)
217 dbus_error_init (&error);
219 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
222 _dbus_warn ("Unable to create reload pipe: %s\n",
224 dbus_error_free (&error);
228 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
229 DBUS_WATCH_READABLE, TRUE,
230 handle_reload_watch, NULL, NULL);
234 _dbus_warn ("Unable to create reload watch: %s\n",
236 dbus_error_free (&error);
240 if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
243 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
245 dbus_error_free (&error);
252 close_reload_pipe (void)
254 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
255 reload_pipe[RELOAD_READ_END] = -1;
257 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
258 reload_pipe[RELOAD_WRITE_END] = -1;
262 main (int argc, char **argv)
265 DBusString config_file;
269 const char *prev_arg;
270 DBusPipe print_addr_pipe;
271 DBusPipe print_pid_pipe;
273 dbus_bool_t print_address;
274 dbus_bool_t print_pid;
275 dbus_bool_t is_session_bus;
277 dbus_bool_t systemd_activation;
279 if (!_dbus_string_init (&config_file))
282 if (!_dbus_string_init (&address))
285 if (!_dbus_string_init (&addr_fd))
288 if (!_dbus_string_init (&pid_fd))
291 print_address = FALSE;
293 is_session_bus = FALSE;
294 force_fork = FORK_FOLLOW_CONFIG_FILE;
295 systemd_activation = FALSE;
301 const char *arg = argv[i];
303 if (strcmp (arg, "--help") == 0 ||
304 strcmp (arg, "-h") == 0 ||
305 strcmp (arg, "-?") == 0)
307 else if (strcmp (arg, "--version") == 0)
309 else if (strcmp (arg, "--introspect") == 0)
311 else if (strcmp (arg, "--nofork") == 0)
312 force_fork = FORK_NEVER;
313 else if (strcmp (arg, "--fork") == 0)
314 force_fork = FORK_ALWAYS;
315 else if (strcmp (arg, "--systemd-activation") == 0)
316 systemd_activation = TRUE;
317 else if (strcmp (arg, "--system") == 0)
319 check_two_config_files (&config_file, "system");
321 if (!_dbus_append_system_config_file (&config_file))
324 else if (strcmp (arg, "--session") == 0)
326 check_two_config_files (&config_file, "session");
328 if (!_dbus_append_session_config_file (&config_file))
331 else if (strstr (arg, "--config-file=") == arg)
335 check_two_config_files (&config_file, "config-file");
337 file = strchr (arg, '=');
340 if (!_dbus_string_append (&config_file, file))
344 strcmp (prev_arg, "--config-file") == 0)
346 check_two_config_files (&config_file, "config-file");
348 if (!_dbus_string_append (&config_file, arg))
351 else if (strcmp (arg, "--config-file") == 0)
352 ; /* wait for next arg */
353 else if (strstr (arg, "--address=") == arg)
357 check_two_addresses (&address, "address");
359 file = strchr (arg, '=');
362 if (!_dbus_string_append (&address, file))
366 strcmp (prev_arg, "--address") == 0)
368 check_two_addresses (&address, "address");
370 if (!_dbus_string_append (&address, arg))
373 else if (strcmp (arg, "--address") == 0)
374 ; /* wait for next arg */
375 else if (strstr (arg, "--print-address=") == arg)
379 check_two_addr_descriptors (&addr_fd, "print-address");
381 desc = strchr (arg, '=');
384 if (!_dbus_string_append (&addr_fd, desc))
387 print_address = TRUE;
390 strcmp (prev_arg, "--print-address") == 0)
392 check_two_addr_descriptors (&addr_fd, "print-address");
394 if (!_dbus_string_append (&addr_fd, arg))
397 print_address = TRUE;
399 else if (strcmp (arg, "--print-address") == 0)
400 print_address = TRUE; /* and we'll get the next arg if appropriate */
401 else if (strstr (arg, "--print-pid=") == arg)
405 check_two_pid_descriptors (&pid_fd, "print-pid");
407 desc = strchr (arg, '=');
410 if (!_dbus_string_append (&pid_fd, desc))
416 strcmp (prev_arg, "--print-pid") == 0)
418 check_two_pid_descriptors (&pid_fd, "print-pid");
420 if (!_dbus_string_append (&pid_fd, arg))
425 else if (strcmp (arg, "--print-pid") == 0)
426 print_pid = TRUE; /* and we'll get the next arg if appropriate */
435 if (_dbus_string_get_length (&config_file) == 0)
437 fprintf (stderr, "No configuration file specified.\n");
441 _dbus_pipe_invalidate (&print_addr_pipe);
444 _dbus_pipe_init_stdout (&print_addr_pipe);
445 if (_dbus_string_get_length (&addr_fd) > 0)
449 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
450 end != _dbus_string_get_length (&addr_fd) ||
451 val < 0 || val > _DBUS_INT_MAX)
453 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
454 _dbus_string_get_const_data (&addr_fd));
458 _dbus_pipe_init (&print_addr_pipe, val);
461 _dbus_string_free (&addr_fd);
463 _dbus_pipe_invalidate (&print_pid_pipe);
466 _dbus_pipe_init_stdout (&print_pid_pipe);
467 if (_dbus_string_get_length (&pid_fd) > 0)
471 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
472 end != _dbus_string_get_length (&pid_fd) ||
473 val < 0 || val > _DBUS_INT_MAX)
475 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
476 _dbus_string_get_const_data (&pid_fd));
480 _dbus_pipe_init (&print_pid_pipe, val);
483 _dbus_string_free (&pid_fd);
485 if (!bus_selinux_pre_init ())
487 _dbus_warn ("SELinux pre-initialization failed\n");
491 dbus_error_init (&error);
492 context = bus_context_new (&config_file, force_fork,
493 &print_addr_pipe, &print_pid_pipe,
494 _dbus_string_get_length(&address) > 0 ? &address : NULL,
497 _dbus_string_free (&config_file);
500 _dbus_warn ("Failed to start message bus: %s\n",
502 dbus_error_free (&error);
506 is_session_bus = bus_context_get_type(context) != NULL
507 && strcmp(bus_context_get_type(context),"session") == 0;
510 _dbus_daemon_publish_session_bus_address (bus_context_get_address (context));
512 /* bus_context_new() closes the print_addr_pipe and
516 setup_reload_pipe (bus_context_get_loop (context));
519 _dbus_set_signal_handler (SIGHUP, signal_handler);
521 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
522 _dbus_set_signal_handler (SIGIO, signal_handler);
523 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
525 _dbus_verbose ("We are on D-Bus...\n");
526 _dbus_loop_run (bus_context_get_loop (context));
528 bus_context_shutdown (context);
529 bus_context_unref (context);
530 bus_selinux_shutdown ();
533 _dbus_daemon_unpublish_session_bus_address ();