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]]\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 main (int argc, char **argv)
93 DBusString config_file;
98 dbus_bool_t print_address;
100 if (!_dbus_string_init (&config_file))
103 if (!_dbus_string_init (&addr_fd))
106 print_address = FALSE;
112 const char *arg = argv[i];
114 if (strcmp (arg, "--help") == 0 ||
115 strcmp (arg, "-h") == 0 ||
116 strcmp (arg, "-?") == 0)
118 else if (strcmp (arg, "--version") == 0)
120 else if (strcmp (arg, "--system") == 0)
122 check_two_config_files (&config_file, "system");
124 if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
127 else if (strcmp (arg, "--session") == 0)
129 check_two_config_files (&config_file, "session");
131 if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
134 else if (strstr (arg, "--config-file=") == arg)
138 check_two_config_files (&config_file, "config-file");
140 file = strchr (arg, '=');
143 if (!_dbus_string_append (&config_file, file))
147 strcmp (prev_arg, "--config-file") == 0)
149 check_two_config_files (&config_file, "config-file");
151 if (!_dbus_string_append (&config_file, arg))
154 else if (strcmp (arg, "--config-file") == 0)
155 ; /* wait for next arg */
156 else if (strstr (arg, "--print-address=") == arg)
160 check_two_addr_descriptors (&addr_fd, "print-address");
162 desc = strchr (arg, '=');
165 if (!_dbus_string_append (&addr_fd, desc))
168 print_address = TRUE;
171 strcmp (prev_arg, "--print-address") == 0)
173 check_two_addr_descriptors (&addr_fd, "print-address");
175 if (!_dbus_string_append (&addr_fd, arg))
178 print_address = TRUE;
180 else if (strcmp (arg, "--print-address") == 0)
181 print_address = TRUE; /* and we'll get the next arg if appropriate */
190 if (_dbus_string_get_length (&config_file) == 0)
192 fprintf (stderr, "No configuration file specified.\n");
199 print_addr_fd = 1; /* stdout */
200 if (_dbus_string_get_length (&addr_fd) > 0)
204 if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) ||
205 end != _dbus_string_get_length (&addr_fd) ||
206 val < 0 || val > _DBUS_INT_MAX)
208 fprintf (stderr, "Invalid file descriptor: \"%s\"\n",
209 _dbus_string_get_const_data (&addr_fd));
217 dbus_error_init (&error);
218 context = bus_context_new (&config_file, print_addr_fd, &error);
219 _dbus_string_free (&config_file);
222 _dbus_warn ("Failed to start message bus: %s\n",
224 dbus_error_free (&error);
228 /* FIXME we have to handle this properly below _dbus_set_signal_handler (SIGHUP, signal_handler); */
229 _dbus_set_signal_handler (SIGTERM, signal_handler);
231 _dbus_verbose ("We are on D-Bus...\n");
232 _dbus_loop_run (bus_context_get_loop (context));
234 bus_context_shutdown (context);
235 bus_context_unref (context);
237 /* If we exited on TERM we just exit, if we exited on
238 * HUP we restart the daemon.
242 /* FIXME execv (argv) basically */