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 1.2
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>
31 static BusContext *context;
32 static dbus_bool_t got_sighup = FALSE;
35 signal_handler (int sig)
42 _dbus_loop_quit (bus_context_get_loop (context));
50 fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork]\n");
57 printf ("D-BUS Message Bus Daemon %s\n"
58 "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
59 "This is free software; see the source for copying conditions.\n"
60 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
66 check_two_config_files (const DBusString *config_file,
67 const char *extra_arg)
69 if (_dbus_string_get_length (config_file) > 0)
71 fprintf (stderr, "--%s specified but configuration file %s already requested\n",
72 extra_arg, _dbus_string_get_const_data (config_file));
78 check_two_addr_descriptors (const DBusString *addr_fd,
79 const char *extra_arg)
81 if (_dbus_string_get_length (addr_fd) > 0)
83 fprintf (stderr, "--%s specified but printing address to %s already requested\n",
84 extra_arg, _dbus_string_get_const_data (addr_fd));
90 check_two_pid_descriptors (const DBusString *pid_fd,
91 const char *extra_arg)
93 if (_dbus_string_get_length (pid_fd) > 0)
95 fprintf (stderr, "--%s specified but printing pid to %s already requested\n",
96 extra_arg, _dbus_string_get_const_data (pid_fd));
102 main (int argc, char **argv)
105 DBusString config_file;
108 const char *prev_arg;
112 dbus_bool_t print_address;
113 dbus_bool_t print_pid;
114 dbus_bool_t force_fork;
116 if (!_dbus_string_init (&config_file))
119 if (!_dbus_string_init (&addr_fd))
122 if (!_dbus_string_init (&pid_fd))
125 print_address = FALSE;
133 const char *arg = argv[i];
135 if (strcmp (arg, "--help") == 0 ||
136 strcmp (arg, "-h") == 0 ||
137 strcmp (arg, "-?") == 0)
139 else if (strcmp (arg, "--version") == 0)
141 else if (strcmp (arg, "--fork") == 0)
143 else if (strcmp (arg, "--system") == 0)
145 check_two_config_files (&config_file, "system");
147 if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
150 else if (strcmp (arg, "--session") == 0)
152 check_two_config_files (&config_file, "session");
154 if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
157 else if (strstr (arg, "--config-file=") == arg)
161 check_two_config_files (&config_file, "config-file");
163 file = strchr (arg, '=');
166 if (!_dbus_string_append (&config_file, file))
170 strcmp (prev_arg, "--config-file") == 0)
172 check_two_config_files (&config_file, "config-file");
174 if (!_dbus_string_append (&config_file, arg))
177 else if (strcmp (arg, "--config-file") == 0)
178 ; /* wait for next arg */
179 else if (strstr (arg, "--print-address=") == arg)
183 check_two_addr_descriptors (&addr_fd, "print-address");
185 desc = strchr (arg, '=');
188 if (!_dbus_string_append (&addr_fd, desc))
191 print_address = TRUE;
194 strcmp (prev_arg, "--print-address") == 0)
196 check_two_addr_descriptors (&addr_fd, "print-address");
198 if (!_dbus_string_append (&addr_fd, arg))
201 print_address = TRUE;
203 else if (strcmp (arg, "--print-address") == 0)
204 print_address = TRUE; /* and we'll get the next arg if appropriate */
205 else if (strstr (arg, "--print-pid=") == arg)
209 check_two_pid_descriptors (&pid_fd, "print-pid");
211 desc = strchr (arg, '=');
214 if (!_dbus_string_append (&pid_fd, desc))
220 strcmp (prev_arg, "--print-pid") == 0)
222 check_two_pid_descriptors (&pid_fd, "print-pid");
224 if (!_dbus_string_append (&pid_fd, arg))
229 else if (strcmp (arg, "--print-pid") == 0)
230 print_pid = TRUE; /* and we'll get the next arg if appropriate */
239 if (_dbus_string_get_length (&config_file) == 0)
241 fprintf (stderr, "No configuration file specified.\n");
248 print_addr_fd = 1; /* stdout */
249 if (_dbus_string_get_length (&addr_fd) > 0)
253 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
254 end != _dbus_string_get_length (&addr_fd) ||
255 val < 0 || val > _DBUS_INT_MAX)
257 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
258 _dbus_string_get_const_data (&addr_fd));
269 print_pid_fd = 1; /* stdout */
270 if (_dbus_string_get_length (&pid_fd) > 0)
274 if (!_dbus_string_parse_int (&pid_fd, 0, &val, &end) ||
275 end != _dbus_string_get_length (&pid_fd) ||
276 val < 0 || val > _DBUS_INT_MAX)
278 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
279 _dbus_string_get_const_data (&pid_fd));
287 dbus_error_init (&error);
288 context = bus_context_new (&config_file, force_fork,
289 print_addr_fd, print_pid_fd,
291 _dbus_string_free (&config_file);
294 _dbus_warn ("Failed to start message bus: %s\n",
296 dbus_error_free (&error);
300 /* FIXME we have to handle this properly below _dbus_set_signal_handler (SIGHUP, signal_handler); */
301 _dbus_set_signal_handler (SIGTERM, signal_handler);
303 _dbus_verbose ("We are on D-Bus...\n");
304 _dbus_loop_run (bus_context_get_loop (context));
306 bus_context_shutdown (context);
307 bus_context_unref (context);
309 /* If we exited on TERM we just exit, if we exited on
310 * HUP we restart the daemon.
314 /* FIXME execv (argv) basically */