1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <dbus/dbus-internals.h>
25 #include <dbus/dbus-watch.h>
33 static BusContext *context;
35 static int reload_pipe[2];
36 #define RELOAD_READ_END 0
37 #define RELOAD_WRITE_END 1
41 signal_handler (int sig)
48 _dbus_string_init_const (&str, "foo");
49 if (!_dbus_write (reload_pipe[RELOAD_WRITE_END], &str, 0, 1))
51 _dbus_warn ("Unable to write to reload pipe.\n");
57 _dbus_loop_quit (bus_context_get_loop (context));
65 fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork]\n");
72 printf ("D-BUS Message Bus Daemon %s\n"
73 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
74 "This is free software; see the source for copying conditions.\n"
75 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
81 check_two_config_files (const DBusString *config_file,
82 const char *extra_arg)
84 if (_dbus_string_get_length (config_file) > 0)
86 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
87 extra_arg, _dbus_string_get_const_data (config_file));
93 check_two_addr_descriptors (const DBusString *addr_fd,
94 const char *extra_arg)
96 if (_dbus_string_get_length (addr_fd) > 0)
98 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
99 extra_arg, _dbus_string_get_const_data (addr_fd));
105 check_two_pid_descriptors (const DBusString *pid_fd,
106 const char *extra_arg)
108 if (_dbus_string_get_length (pid_fd) > 0)
110 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
111 extra_arg, _dbus_string_get_const_data (pid_fd));
117 handle_reload_watch (DBusWatch *watch,
123 _dbus_string_init (&str);
124 if (_dbus_read (reload_pipe[RELOAD_READ_END], &str, 1) != 1)
126 _dbus_warn ("Couldn't read from reload pipe.\n");
129 _dbus_string_free (&str);
131 dbus_error_init (&error);
132 if (! bus_context_reload_config (context, &error))
134 _dbus_warn ("Unable to reload configuration: %s\n",
136 dbus_error_free (&error);
143 reload_watch_callback (DBusWatch *watch,
144 unsigned int condition,
147 return dbus_watch_handle (watch, condition);
151 setup_reload_pipe (DBusLoop *loop)
156 dbus_error_init (&error);
158 if (!_dbus_full_duplex_pipe (&reload_pipe[0], &reload_pipe[1],
161 _dbus_warn ("Unable to create reload pipe: %s\n",
163 dbus_error_free (&error);
167 watch = _dbus_watch_new (reload_pipe[RELOAD_READ_END],
168 DBUS_WATCH_READABLE, TRUE,
169 handle_reload_watch, NULL, NULL);
173 _dbus_warn ("Unable to create reload watch: %s\n",
175 dbus_error_free (&error);
179 if (!_dbus_loop_add_watch (loop, watch, reload_watch_callback,
182 _dbus_warn ("Unable to add reload watch to main loop: %s\n",
184 dbus_error_free (&error);
191 main (int argc, char **argv)
194 DBusString config_file;
197 const char *prev_arg;
201 dbus_bool_t print_address;
202 dbus_bool_t print_pid;
205 if (!_dbus_string_init (&config_file))
208 if (!_dbus_string_init (&addr_fd))
211 if (!_dbus_string_init (&pid_fd))
214 print_address = FALSE;
216 force_fork = FORK_FOLLOW_CONFIG_FILE;
222 const char *arg = argv[i];
224 if (strcmp (arg, "--help") == 0 ||
225 strcmp (arg, "-h") == 0 ||
226 strcmp (arg, "-?") == 0)
228 else if (strcmp (arg, "--version") == 0)
230 else if (strcmp (arg, "--nofork") == 0)
231 force_fork = FORK_NEVER;
232 else if (strcmp (arg, "--fork") == 0)
233 force_fork = FORK_ALWAYS;
234 else if (strcmp (arg, "--system") == 0)
236 check_two_config_files (&config_file, "system");
238 if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
241 else if (strcmp (arg, "--session") == 0)
243 check_two_config_files (&config_file, "session");
245 if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
248 else if (strstr (arg, "--config-file=") == arg)
252 check_two_config_files (&config_file, "config-file");
254 file = strchr (arg, '=');
257 if (!_dbus_string_append (&config_file, file))
261 strcmp (prev_arg, "--config-file") == 0)
263 check_two_config_files (&config_file, "config-file");
265 if (!_dbus_string_append (&config_file, arg))
268 else if (strcmp (arg, "--config-file") == 0)
269 ; /* wait for next arg */
270 else if (strstr (arg, "--print-address=") == arg)
274 check_two_addr_descriptors (&addr_fd, "print-address");
276 desc = strchr (arg, '=');
279 if (!_dbus_string_append (&addr_fd, desc))
282 print_address = TRUE;
285 strcmp (prev_arg, "--print-address") == 0)
287 check_two_addr_descriptors (&addr_fd, "print-address");
289 if (!_dbus_string_append (&addr_fd, arg))
292 print_address = TRUE;
294 else if (strcmp (arg, "--print-address") == 0)
295 print_address = TRUE; /* and we'll get the next arg if appropriate */
296 else if (strstr (arg, "--print-pid=") == arg)
300 check_two_pid_descriptors (&pid_fd, "print-pid");
302 desc = strchr (arg, '=');
305 if (!_dbus_string_append (&pid_fd, desc))
311 strcmp (prev_arg, "--print-pid") == 0)
313 check_two_pid_descriptors (&pid_fd, "print-pid");
315 if (!_dbus_string_append (&pid_fd, arg))
320 else if (strcmp (arg, "--print-pid") == 0)
321 print_pid = TRUE; /* and we'll get the next arg if appropriate */
330 if (_dbus_string_get_length (&config_file) == 0)
332 fprintf (stderr, "No configuration file specified.\n");
339 print_addr_fd = 1; /* stdout */
340 if (_dbus_string_get_length (&addr_fd) > 0)
344 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
345 end != _dbus_string_get_length (&addr_fd) ||
346 val < 0 || val > _DBUS_INT_MAX)
348 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
349 _dbus_string_get_const_data (&addr_fd));
356 _dbus_string_free (&addr_fd);
361 print_pid_fd = 1; /* stdout */
362 if (_dbus_string_get_length (&pid_fd) > 0)
366 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
367 end != _dbus_string_get_length (&pid_fd) ||
368 val < 0 || val > _DBUS_INT_MAX)
370 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
371 _dbus_string_get_const_data (&pid_fd));
378 _dbus_string_free (&pid_fd);
380 if (!bus_selinux_pre_init ())
382 _dbus_warn ("SELinux pre-initialization failed\n");
386 dbus_error_init (&error);
387 context = bus_context_new (&config_file, force_fork,
388 print_addr_fd, print_pid_fd,
390 _dbus_string_free (&config_file);
393 _dbus_warn ("Failed to start message bus: %s\n",
395 dbus_error_free (&error);
399 setup_reload_pipe (bus_context_get_loop (context));
401 _dbus_set_signal_handler (SIGHUP, signal_handler);
402 _dbus_set_signal_handler (SIGTERM, signal_handler);
404 _dbus_verbose ("We are on D-Bus...\n");
405 _dbus_loop_run (bus_context_get_loop (context));
407 bus_context_shutdown (context);
408 bus_context_unref (context);
409 bus_selinux_shutdown ();