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>
38 static BusContext *context;
40 static int reload_pipe[2];
41 #define RELOAD_READ_END 0
42 #define RELOAD_WRITE_END 1
44 static void close_reload_pipe (void);
47 signal_handler (int sig)
52 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
54 /* explicit fall-through */
55 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
60 _dbus_string_init_const (&str, "foo");
61 if ((reload_pipe[RELOAD_WRITE_END] > 0) &&
62 !_dbus_write_socket (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
64 _dbus_warn ("Unable to write to reload pipe.\n");
71 _dbus_loop_quit (bus_context_get_loop (context));
79 fprintf (stderr, DBUS_DAEMON_NAME " [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork] [--introspect]\n");
86 printf ("D-Bus Message Bus Daemon %s\n"
87 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
88 "This is free software; see the source for copying conditions.\n"
89 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
100 if (!_dbus_string_init (&xml))
103 if (!bus_driver_generate_introspect_string (&xml))
105 _dbus_string_free (&xml);
109 v_STRING = _dbus_string_get_const_data (&xml);
110 printf ("%s\n", v_STRING);
115 _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_addr_descriptors (const DBusString *addr_fd,
132 const char *extra_arg)
134 if (_dbus_string_get_length (addr_fd) > 0)
136 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
137 extra_arg, _dbus_string_get_const_data (addr_fd));
143 check_two_pid_descriptors (const DBusString *pid_fd,
144 const char *extra_arg)
146 if (_dbus_string_get_length (pid_fd) > 0)
148 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
149 extra_arg, _dbus_string_get_const_data (pid_fd));
155 handle_reload_watch (DBusWatch *watch,
162 while (!_dbus_string_init (&str))
163 _dbus_wait_for_memory ();
165 if ((reload_pipe[RELOAD_READ_END] > 0) &&
166 _dbus_read_socket (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
168 _dbus_warn ("Couldn't read from reload pipe.\n");
169 close_reload_pipe ();
172 _dbus_string_free (&str);
174 /* this can only fail if we don't understand the config file
175 * or OOM. Either way we should just stick with the currently
178 dbus_error_init (&error);
179 if (! bus_context_reload_config (context, &error))
181 _DBUS_ASSERT_ERROR_IS_SET (&error);
182 _dbus_assert (dbus_error_has_name (&error, DBUS_ERROR_FAILED) ||
183 dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY));
184 _dbus_warn ("Unable to reload configuration: %s\n",
186 dbus_error_free (&error);
192 reload_watch_callback (DBusWatch *watch,
193 unsigned int condition,
196 return dbus_watch_handle (watch, condition);
200 setup_reload_pipe (DBusLoop *loop)
205 dbus_error_init (&error);
207 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
210 _dbus_warn ("Unable to create reload pipe: %s\n",
212 dbus_error_free (&error);
216 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
217 DBUS_WATCH_READABLE, TRUE,
218 handle_reload_watch, NULL, NULL);
222 _dbus_warn ("Unable to create reload watch: %s\n",
224 dbus_error_free (&error);
228 if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
231 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
233 dbus_error_free (&error);
240 close_reload_pipe (void)
242 _dbus_close_socket (reload_pipe[RELOAD_READ_END], NULL);
243 reload_pipe[RELOAD_READ_END] = -1;
245 _dbus_close_socket (reload_pipe[RELOAD_WRITE_END], NULL);
246 reload_pipe[RELOAD_WRITE_END] = -1;
250 main (int argc, char **argv)
253 DBusString config_file;
256 const char *prev_arg;
257 DBusPipe print_addr_pipe;
258 DBusPipe print_pid_pipe;
260 dbus_bool_t print_address;
261 dbus_bool_t print_pid;
262 dbus_bool_t is_session_bus;
265 if (!_dbus_string_init (&config_file))
268 if (!_dbus_string_init (&addr_fd))
271 if (!_dbus_string_init (&pid_fd))
274 print_address = FALSE;
276 is_session_bus = FALSE;
277 force_fork = FORK_FOLLOW_CONFIG_FILE;
283 const char *arg = argv[i];
285 if (strcmp (arg, "--help") == 0 ||
286 strcmp (arg, "-h") == 0 ||
287 strcmp (arg, "-?") == 0)
289 else if (strcmp (arg, "--version") == 0)
291 else if (strcmp (arg, "--introspect") == 0)
293 else if (strcmp (arg, "--nofork") == 0)
294 force_fork = FORK_NEVER;
295 else if (strcmp (arg, "--fork") == 0)
296 force_fork = FORK_ALWAYS;
297 else if (strcmp (arg, "--system") == 0)
299 check_two_config_files (&config_file, "system");
301 if (!_dbus_append_system_config_file (&config_file))
304 else if (strcmp (arg, "--session") == 0)
306 check_two_config_files (&config_file, "session");
308 if (!_dbus_append_session_config_file (&config_file))
311 else if (strstr (arg, "--config-file=") == arg)
315 check_two_config_files (&config_file, "config-file");
317 file = strchr (arg, '=');
320 if (!_dbus_string_append (&config_file, file))
324 strcmp (prev_arg, "--config-file") == 0)
326 check_two_config_files (&config_file, "config-file");
328 if (!_dbus_string_append (&config_file, arg))
331 else if (strcmp (arg, "--config-file") == 0)
332 ; /* wait for next arg */
333 else if (strstr (arg, "--print-address=") == arg)
337 check_two_addr_descriptors (&addr_fd, "print-address");
339 desc = strchr (arg, '=');
342 if (!_dbus_string_append (&addr_fd, desc))
345 print_address = TRUE;
348 strcmp (prev_arg, "--print-address") == 0)
350 check_two_addr_descriptors (&addr_fd, "print-address");
352 if (!_dbus_string_append (&addr_fd, arg))
355 print_address = TRUE;
357 else if (strcmp (arg, "--print-address") == 0)
358 print_address = TRUE; /* and we'll get the next arg if appropriate */
359 else if (strstr (arg, "--print-pid=") == arg)
363 check_two_pid_descriptors (&pid_fd, "print-pid");
365 desc = strchr (arg, '=');
368 if (!_dbus_string_append (&pid_fd, desc))
374 strcmp (prev_arg, "--print-pid") == 0)
376 check_two_pid_descriptors (&pid_fd, "print-pid");
378 if (!_dbus_string_append (&pid_fd, arg))
383 else if (strcmp (arg, "--print-pid") == 0)
384 print_pid = TRUE; /* and we'll get the next arg if appropriate */
393 if (_dbus_string_get_length (&config_file) == 0)
395 fprintf (stderr, "No configuration file specified.\n");
399 _dbus_pipe_invalidate (&print_addr_pipe);
402 _dbus_pipe_init_stdout (&print_addr_pipe);
403 if (_dbus_string_get_length (&addr_fd) > 0)
407 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
408 end != _dbus_string_get_length (&addr_fd) ||
409 val < 0 || val > _DBUS_INT_MAX)
411 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
412 _dbus_string_get_const_data (&addr_fd));
416 _dbus_pipe_init (&print_addr_pipe, val);
419 _dbus_string_free (&addr_fd);
421 _dbus_pipe_invalidate (&print_pid_pipe);
424 _dbus_pipe_init_stdout (&print_pid_pipe);
425 if (_dbus_string_get_length (&pid_fd) > 0)
429 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
430 end != _dbus_string_get_length (&pid_fd) ||
431 val < 0 || val > _DBUS_INT_MAX)
433 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
434 _dbus_string_get_const_data (&pid_fd));
438 _dbus_pipe_init (&print_pid_pipe, val);
441 _dbus_string_free (&pid_fd);
443 if (!bus_selinux_pre_init ())
445 _dbus_warn ("SELinux pre-initialization failed\n");
449 dbus_error_init (&error);
450 context = bus_context_new (&config_file, force_fork,
451 &print_addr_pipe, &print_pid_pipe,
453 _dbus_string_free (&config_file);
456 _dbus_warn ("Failed to start message bus: %s\n",
458 dbus_error_free (&error);
462 is_session_bus = bus_context_get_type(context) != NULL
463 && strcmp(bus_context_get_type(context),"session") == 0;
466 _dbus_daemon_publish_session_bus_address (bus_context_get_address (context));
468 /* bus_context_new() closes the print_addr_pipe and
472 setup_reload_pipe (bus_context_get_loop (context));
475 _dbus_set_signal_handler (SIGHUP, signal_handler);
477 _dbus_set_signal_handler (SIGTERM, signal_handler);
478 #ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
479 _dbus_set_signal_handler (SIGIO, signal_handler);
480 #endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
482 _dbus_verbose ("We are on D-Bus...\n");
483 _dbus_loop_run (bus_context_get_loop (context));
485 bus_context_shutdown (context);
486 bus_context_unref (context);
487 bus_selinux_shutdown ();
490 _dbus_daemon_unpublish_session_bus_address ();