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.0
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]\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;
203 dbus_bool_t force_fork;
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;
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, "--fork") == 0)
232 else if (strcmp (arg, "--system") == 0)
234 check_two_config_files (&config_file, "system");
236 if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
239 else if (strcmp (arg, "--session") == 0)
241 check_two_config_files (&config_file, "session");
243 if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
246 else if (strstr (arg, "--config-file=") == arg)
250 check_two_config_files (&config_file, "config-file");
252 file = strchr (arg, '=');
255 if (!_dbus_string_append (&config_file, file))
259 strcmp (prev_arg, "--config-file") == 0)
261 check_two_config_files (&config_file, "config-file");
263 if (!_dbus_string_append (&config_file, arg))
266 else if (strcmp (arg, "--config-file") == 0)
267 ; /* wait for next arg */
268 else if (strstr (arg, "--print-address=") == arg)
272 check_two_addr_descriptors (&addr_fd, "print-address");
274 desc = strchr (arg, '=');
277 if (!_dbus_string_append (&addr_fd, desc))
280 print_address = TRUE;
283 strcmp (prev_arg, "--print-address") == 0)
285 check_two_addr_descriptors (&addr_fd, "print-address");
287 if (!_dbus_string_append (&addr_fd, arg))
290 print_address = TRUE;
292 else if (strcmp (arg, "--print-address") == 0)
293 print_address = TRUE; /* and we'll get the next arg if appropriate */
294 else if (strstr (arg, "--print-pid=") == arg)
298 check_two_pid_descriptors (&pid_fd, "print-pid");
300 desc = strchr (arg, '=');
303 if (!_dbus_string_append (&pid_fd, desc))
309 strcmp (prev_arg, "--print-pid") == 0)
311 check_two_pid_descriptors (&pid_fd, "print-pid");
313 if (!_dbus_string_append (&pid_fd, arg))
318 else if (strcmp (arg, "--print-pid") == 0)
319 print_pid = TRUE; /* and we'll get the next arg if appropriate */
328 if (_dbus_string_get_length (&config_file) == 0)
330 fprintf (stderr, "No configuration file specified.\n");
337 print_addr_fd = 1; /* stdout */
338 if (_dbus_string_get_length (&addr_fd) > 0)
342 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
343 end != _dbus_string_get_length (&addr_fd) ||
344 val < 0 || val > _DBUS_INT_MAX)
346 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
347 _dbus_string_get_const_data (&addr_fd));
358 print_pid_fd = 1; /* stdout */
359 if (_dbus_string_get_length (&pid_fd) > 0)
363 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
364 end != _dbus_string_get_length (&pid_fd) ||
365 val < 0 || val > _DBUS_INT_MAX)
367 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
368 _dbus_string_get_const_data (&pid_fd));
376 if (!bus_selinux_init ())
378 _dbus_warn ("SELinux initialization failed\n");
382 dbus_error_init (&error);
383 context = bus_context_new (&config_file, force_fork,
384 print_addr_fd, print_pid_fd,
386 _dbus_string_free (&config_file);
389 _dbus_warn ("Failed to start message bus: %s\n",
391 dbus_error_free (&error);
395 setup_reload_pipe (bus_context_get_loop (context));
397 _dbus_set_signal_handler (SIGHUP, signal_handler);
398 _dbus_set_signal_handler (SIGTERM, signal_handler);
400 _dbus_verbose ("We are on D-Bus...\n");
401 _dbus_loop_run (bus_context_get_loop (context));
403 bus_context_shutdown (context);
404 bus_context_unref (context);
405 bus_selinux_shutdown ();