[PATCH 2/3] bus: add --address switch
Lennart Poettering
mzqohf at 0pointer.de
Wed Jun 23 09:10:47 PDT 2010
This is allows overriding of the listening address on the command line,
which is particularly useful for systemd socket-based activation.
---
bus/bus.c | 26 +++++++++++++++++++++++++-
bus/bus.h | 1 +
bus/main.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
bus/test.c | 2 +-
4 files changed, 71 insertions(+), 3 deletions(-)
diff --git a/bus/bus.c b/bus/bus.c
index 2e9a505..8687fb6 100644
--- a/bus/bus.c
+++ b/bus/bus.c
@@ -275,6 +275,7 @@ setup_server (BusContext *context,
static dbus_bool_t
process_config_first_time_only (BusContext *context,
BusConfigParser *parser,
+ const DBusString *address,
DBusError *error)
{
DBusString log_prefix;
@@ -386,6 +387,27 @@ process_config_first_time_only (BusContext *context,
/* Listen on our addresses */
+ if (address)
+ {
+ DBusServer *server;
+
+ server = dbus_server_listen (_dbus_string_get_const_data(address), error);
+ if (server == NULL)
+ {
+ _DBUS_ASSERT_ERROR_IS_SET (error);
+ goto failed;
+ }
+ else if (!setup_server (context, server, auth_mechanisms, error))
+ {
+ _DBUS_ASSERT_ERROR_IS_SET (error);
+ goto failed;
+ }
+
+ if (!_dbus_list_append (&context->servers, server))
+ goto oom;
+ }
+ else
+ {
addresses = bus_config_parser_get_addresses (parser);
link = _dbus_list_get_first_link (addresses);
@@ -410,6 +432,7 @@ process_config_first_time_only (BusContext *context,
link = _dbus_list_get_next_link (addresses, link);
}
+ }
context->fork = bus_config_parser_get_fork (parser);
context->syslog = bus_config_parser_get_syslog (parser);
@@ -628,6 +651,7 @@ bus_context_new (const DBusString *config_file,
ForceForkSetting force_fork,
DBusPipe *print_addr_pipe,
DBusPipe *print_pid_pipe,
+ const DBusString *address,
DBusError *error)
{
DBusString log_prefix;
@@ -682,7 +706,7 @@ bus_context_new (const DBusString *config_file,
goto failed;
}
- if (!process_config_first_time_only (context, parser, error))
+ if (!process_config_first_time_only (context, parser, address, error))
{
_DBUS_ASSERT_ERROR_IS_SET (error);
goto failed;
diff --git a/bus/bus.h b/bus/bus.h
index 7eb5fd8..b5c7c20 100644
--- a/bus/bus.h
+++ b/bus/bus.h
@@ -75,6 +75,7 @@ BusContext* bus_context_new (const DBusStri
ForceForkSetting force_fork,
DBusPipe *print_addr_pipe,
DBusPipe *print_pid_pipe,
+ const DBusString *address,
DBusError *error);
dbus_bool_t bus_context_reload_config (BusContext *context,
DBusError *error);
diff --git a/bus/main.c b/bus/main.c
index ab17452..603e1f8 100644
--- a/bus/main.c
+++ b/bus/main.c
@@ -75,7 +75,7 @@ signal_handler (int sig)
static void
usage (void)
{
- fprintf (stderr, DBUS_DAEMON_NAME " [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork] [--introspect]\n");
+ fprintf (stderr, DBUS_DAEMON_NAME " [--version] [--session] [--system] [--config-file=FILE] [--print-address[=DESCRIPTOR]] [--print-pid[=DESCRIPTOR]] [--fork] [--nofork] [--introspect] [--address=ADDRESS]\n");
exit (1);
}
@@ -114,6 +114,7 @@ introspect (void)
_dbus_warn ("Can not introspect - Out of memory\n");
exit (1);
}
+
static void
check_two_config_files (const DBusString *config_file,
const char *extra_arg)
@@ -127,6 +128,18 @@ check_two_config_files (const DBusString *config_file,
}
static void
+check_two_addresses (const DBusString *address,
+ const char *extra_arg)
+{
+ if (_dbus_string_get_length (address) > 0)
+ {
+ fprintf (stderr, "--%s specified but address %s already requested\n",
+ extra_arg, _dbus_string_get_const_data (address));
+ exit (1);
+ }
+}
+
+static void
check_two_addr_descriptors (const DBusString *addr_fd,
const char *extra_arg)
{
@@ -250,6 +263,7 @@ main (int argc, char **argv)
{
DBusError error;
DBusString config_file;
+ DBusString address;
DBusString addr_fd;
DBusString pid_fd;
const char *prev_arg;
@@ -264,6 +278,9 @@ main (int argc, char **argv)
if (!_dbus_string_init (&config_file))
return 1;
+ if (!_dbus_string_init (&address))
+ return 1;
+
if (!_dbus_string_init (&addr_fd))
return 1;
@@ -329,6 +346,28 @@ main (int argc, char **argv)
}
else if (strcmp (arg, "--config-file") == 0)
; /* wait for next arg */
+ else if (strstr (arg, "--address=") == arg)
+ {
+ const char *file;
+
+ check_two_addresses (&address, "address");
+
+ file = strchr (arg, '=');
+ ++file;
+
+ if (!_dbus_string_append (&address, file))
+ exit (1);
+ }
+ else if (prev_arg &&
+ strcmp (prev_arg, "--address") == 0)
+ {
+ check_two_addresses (&address, "address");
+
+ if (!_dbus_string_append (&address, arg))
+ exit (1);
+ }
+ else if (strcmp (arg, "--address") == 0)
+ ; /* wait for next arg */
else if (strstr (arg, "--print-address=") == arg)
{
const char *desc;
@@ -448,6 +487,7 @@ main (int argc, char **argv)
dbus_error_init (&error);
context = bus_context_new (&config_file, force_fork,
&print_addr_pipe, &print_pid_pipe,
+ _dbus_string_get_length(&address) > 0 ? &address : NULL,
&error);
_dbus_string_free (&config_file);
if (context == NULL)
@@ -473,6 +513,9 @@ main (int argc, char **argv)
#ifdef SIGHUP
_dbus_set_signal_handler (SIGHUP, signal_handler);
#endif
+#ifdef SIGTERM
+ _dbus_set_signal_handler (SIGTERM, signal_handler);
+#endif
#ifdef DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX
_dbus_set_signal_handler (SIGIO, signal_handler);
#endif /* DBUS_BUS_ENABLE_DNOTIFY_ON_LINUX */
diff --git a/bus/test.c b/bus/test.c
index 8dfe098..7af143b 100644
--- a/bus/test.c
+++ b/bus/test.c
@@ -323,7 +323,7 @@ bus_context_new_test (const DBusString *test_data_dir,
}
dbus_error_init (&error);
- context = bus_context_new (&config_file, FALSE, NULL, NULL, &error);
+ context = bus_context_new (&config_file, FALSE, NULL, NULL, NULL, &error);
if (context == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (&error);
--
1.7.0.1
Lennart
--
Lennart Poettering Red Hat, Inc.
lennart [at] poettering [dot] net
http://0pointer.net/lennart/ GnuPG 0x1A015CC4
More information about the dbus
mailing list