From e45e4382274149ca60c11f068ccca719f3598074 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Sun, 6 Apr 2003 18:03:03 +0000 Subject: 2003-04-06 Havoc Pennington * bus/bus.c (bus_context_new): fix wrong handling of server_data_slot_unref() in the error case. * dbus/dbus-internals.h (_dbus_assert): change so it passes "(condition) != 0" to _dbus_real_assert so that "_dbus_assert (pointer)" doesn't cause a warning * bus/main.c (main): accept --print-address option to print out the message bus address * dbus/dbus-sysdeps.c (_dbus_generate_random_ascii): export this * dbus/dbus-transport.c (_dbus_transport_open): special error for "tmpdir" option to unix: address on client side * dbus/dbus-server.c (dbus_server_listen): handle "tmpdir" option to unix: address * configure.in (TEST_SOCKET_DIR): locate a temporary directory we can use to create sockets in the test suite. * bus/main.c (signal_handler): on SIGTERM, exit the daemon cleanly. To be used for testing. * dbus/dbus-spawn.c (babysit): use _dbus_set_signal_handler() * dbus/dbus-sysdeps.c (_dbus_set_signal_handler): new * dbus/dbus-server-debug-pipe.c (_dbus_transport_debug_pipe_new): handle trying to call this when there's no servers active --- bus/bus.c | 20 +++++++- bus/bus.h | 1 + bus/main.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++-- bus/session.conf.in | 14 +----- 4 files changed, 150 insertions(+), 19 deletions(-) (limited to 'bus') diff --git a/bus/bus.c b/bus/bus.c index 6c5a3f28..e426706a 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -107,7 +107,10 @@ server_get_context (DBusServer *server) bd = BUS_SERVER_DATA (server); if (bd == NULL) - return NULL; + { + server_data_slot_unref (); + return NULL; + } context = bd->context; @@ -314,7 +317,7 @@ bus_context_new (const DBusString *config_file, BUS_SET_OOM (error); return NULL; } - + if (!server_data_slot_ref ()) { BUS_SET_OOM (error); @@ -339,6 +342,12 @@ bus_context_new (const DBusString *config_file, context->refcount = 1; + /* we need another ref of the server data slot for the context + * to own + */ + if (!server_data_slot_ref ()) + _dbus_assert_not_reached ("second ref of server data slot failed"); + #ifdef DBUS_BUILD_TESTS context->activation_timeout = 6000; /* 6 seconds */ #else @@ -542,6 +551,7 @@ bus_context_new (const DBusString *config_file, bus_config_parser_unref (parser); _dbus_string_free (&full_address); dbus_free_string_array (auth_mechanisms); + server_data_slot_unref (); return context; @@ -678,6 +688,12 @@ bus_context_get_type (BusContext *context) return context->type; } +const char* +bus_context_get_address (BusContext *context) +{ + return context->address; +} + BusRegistry* bus_context_get_registry (BusContext *context) { diff --git a/bus/bus.h b/bus/bus.h index 8b26cffc..aa9f848d 100644 --- a/bus/bus.h +++ b/bus/bus.h @@ -46,6 +46,7 @@ void bus_context_shutdown (BusContext *context) void bus_context_ref (BusContext *context); void bus_context_unref (BusContext *context); const char* bus_context_get_type (BusContext *context); +const char* bus_context_get_address (BusContext *context); BusRegistry* bus_context_get_registry (BusContext *context); BusConnections* bus_context_get_connections (BusContext *context); BusActivation* bus_context_get_activation (BusContext *context); diff --git a/bus/main.c b/bus/main.c index 099219c3..119520ff 100644 --- a/bus/main.c +++ b/bus/main.c @@ -26,11 +26,29 @@ #include #include #include +#include +#include + +static BusContext *context; +static dbus_bool_t got_sighup = FALSE; + +static void +signal_handler (int sig) +{ + switch (sig) + { + case SIGHUP: + got_sighup = TRUE; + case SIGTERM: + bus_loop_quit (bus_context_get_loop (context)); + break; + } +} static void usage (void) { - fprintf (stderr, "dbus-daemon-1 [--session] [--system] [--config-file=FILE] [--version]\n"); + fprintf (stderr, "dbus-daemon-1 [--version] [--session] [--system] [--config-file=FILE] [--print-address[=descriptor]]\n"); exit (1); } @@ -57,17 +75,36 @@ check_two_config_files (const DBusString *config_file, } } +static void +check_two_addr_descriptors (const DBusString *addr_fd, + const char *extra_arg) +{ + if (_dbus_string_get_length (addr_fd) > 0) + { + fprintf (stderr, "--%s specified but printing address to %s already requested\n", + extra_arg, _dbus_string_get_const_data (addr_fd)); + exit (1); + } +} + int main (int argc, char **argv) { - BusContext *context; DBusError error; DBusString config_file; + DBusString addr_fd; const char *prev_arg; + int print_addr_fd; int i; - + dbus_bool_t print_address; + if (!_dbus_string_init (&config_file)) return 1; + + if (!_dbus_string_init (&addr_fd)) + return 1; + + print_address = FALSE; prev_arg = NULL; i = 1; @@ -117,6 +154,32 @@ main (int argc, char **argv) } else if (strcmp (arg, "--config-file") == 0) ; /* wait for next arg */ + else if (strstr (arg, "--print-address=") == arg) + { + const char *desc; + + check_two_addr_descriptors (&addr_fd, "print-address"); + + desc = strchr (arg, '='); + ++desc; + + if (!_dbus_string_append (&addr_fd, desc)) + exit (1); + + print_address = TRUE; + } + else if (prev_arg && + strcmp (prev_arg, "--print-address") == 0) + { + check_two_addr_descriptors (&addr_fd, "print-address"); + + if (!_dbus_string_append (&addr_fd, arg)) + exit (1); + + print_address = TRUE; + } + else if (strcmp (arg, "--print-address") == 0) + print_address = TRUE; /* and we'll get the next arg if appropriate */ else usage (); @@ -130,6 +193,27 @@ main (int argc, char **argv) fprintf (stderr, "No configuration file specified.\n"); usage (); } + + print_addr_fd = -1; + if (print_address) + { + print_addr_fd = 1; /* stdout */ + if (_dbus_string_get_length (&addr_fd) > 0) + { + long val; + int end; + if (!_dbus_string_parse_int (&addr_fd, 0, &val, &end) || + end != _dbus_string_get_length (&addr_fd) || + val < 0 || val > _DBUS_INT_MAX) + { + fprintf (stderr, "Invalid file descriptor: \"%s\"\n", + _dbus_string_get_const_data (&addr_fd)); + exit (1); + } + + print_addr_fd = val; + } + } dbus_error_init (&error); context = bus_context_new (&config_file, &error); @@ -139,14 +223,56 @@ main (int argc, char **argv) _dbus_warn ("Failed to start message bus: %s\n", error.message); dbus_error_free (&error); - return 1; + exit (1); } + + /* Note that we don't know whether the print_addr_fd is + * one of the sockets we're using to listen on, or some + * other random thing. But I think the answer is "don't do + * that then" + */ + if (print_addr_fd >= 0) + { + DBusString addr; + const char *a = bus_context_get_address (context); + int bytes; + + _dbus_assert (a != NULL); + if (!_dbus_string_init (&addr) || + !_dbus_string_append (&addr, a) || + !_dbus_string_append (&addr, "\n")) + exit (1); + + bytes = _dbus_string_get_length (&addr); + if (_dbus_write (print_addr_fd, &addr, 0, bytes) != bytes) + { + _dbus_warn ("Failed to print message bus address: %s\n", + _dbus_strerror (errno)); + exit (1); + } + + if (print_addr_fd > 2) + _dbus_close (print_addr_fd, NULL); + + _dbus_string_free (&addr); + } + + /* FIXME we have to handle this properly below _dbus_set_signal_handler (SIGHUP, signal_handler); */ + _dbus_set_signal_handler (SIGTERM, signal_handler); _dbus_verbose ("We are on D-Bus...\n"); bus_loop_run (bus_context_get_loop (context)); bus_context_shutdown (context); bus_context_unref (context); + + /* If we exited on TERM we just exit, if we exited on + * HUP we restart the daemon. + */ + if (got_sighup) + { + /* FIXME execv (argv) basically */ + } return 0; } diff --git a/bus/session.conf.in b/bus/session.conf.in index 28478955..d430d990 100644 --- a/bus/session.conf.in +++ b/bus/session.conf.in @@ -8,19 +8,7 @@ session - - unix:path=/tmp/foobar + unix:tmpdir=@DBUS_SESSION_SOCKET_DIR@ -- cgit