From 29c71168cd17b11eed65023c97aff401d5305b01 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Mon, 31 Mar 2003 08:19:50 +0000 Subject: 2003-03-31 Havoc Pennington * dbus/dbus-transport-unix.c (_dbus_transport_new_for_domain_socket) (_dbus_transport_new_for_tcp_socket): these didn't need the "server" argument since they are always client side * dbus/dbus-server.c (dbus_server_get_address): new function * bus/main.c (main): take the configuration file as an argument. * test/data/valid-config-files/debug-allow-all.conf: new file to use with dispatch.c tests for example * bus/test-main.c (main): require test data dir * bus/bus.c (bus_context_new): change this to take a configuration file name as argument * doc/config-file.txt (Elements): add * bus/system.conf, bus/session.conf: new files * dbus/dbus-bus.c (dbus_bus_get): look for system bus on well-known socket if none set * configure.in: create system.conf and session.conf --- bus/Makefile.am | 6 ++ bus/activation.c | 12 ++- bus/activation.h | 29 +++---- bus/bus.c | 224 ++++++++++++++++++++++++++++++++++++++-------------- bus/bus.h | 26 +++--- bus/config-parser.c | 13 +++ bus/config-parser.h | 3 +- bus/dispatch.c | 13 ++- bus/main.c | 20 +++-- bus/session.conf.in | 31 ++++++++ bus/system.conf.in | 20 +++++ bus/test-main.c | 5 +- bus/test.c | 53 +++++++++++++ bus/test.h | 3 + bus/utils.c | 2 +- 15 files changed, 347 insertions(+), 113 deletions(-) create mode 100644 bus/session.conf.in create mode 100644 bus/system.conf.in (limited to 'bus') diff --git a/bus/Makefile.am b/bus/Makefile.am index 904e0c5e..ece0aa55 100644 --- a/bus/Makefile.am +++ b/bus/Makefile.am @@ -4,6 +4,12 @@ INCLUDES=-I$(top_srcdir) $(DBUS_BUS_CFLAGS) \ EFENCE= +configdir=$(sysconfdir)/dbus-1 + +config_DATA= \ + system.conf \ + session.conf + bin_PROGRAMS=dbus-daemon-1 if DBUS_USE_LIBXML diff --git a/bus/activation.c b/bus/activation.c index 03a01937..8def99b9 100644 --- a/bus/activation.c +++ b/bus/activation.c @@ -323,10 +323,10 @@ load_directory (BusActivation *activation, } BusActivation* -bus_activation_new (BusContext *context, - const char *address, - const char **directories, - DBusError *error) +bus_activation_new (BusContext *context, + const DBusString *address, + const char **directories, + DBusError *error) { int i; BusActivation *activation; @@ -343,9 +343,7 @@ bus_activation_new (BusContext *context, activation->refcount = 1; activation->context = context; - /* FIXME: We should split up the server addresses. */ - activation->server_address = _dbus_strdup (address); - if (activation->server_address == NULL) + if (!_dbus_string_copy_data (address, &activation->server_address)) { BUS_SET_OOM (error); goto failed; diff --git a/bus/activation.h b/bus/activation.h index 1fd416ea..fd3a72c9 100644 --- a/bus/activation.h +++ b/bus/activation.h @@ -27,19 +27,20 @@ #include #include "bus.h" -BusActivation* bus_activation_new (BusContext *context, - const char *address, - const char **paths, - DBusError *error); -void bus_activation_ref (BusActivation *activation); -void bus_activation_unref (BusActivation *activation); -dbus_bool_t bus_activation_activate_service (BusActivation *activation, - DBusConnection *connection, - DBusMessage *activation_message, - const char *service_name, - DBusError *error); -dbus_bool_t bus_activation_service_created (BusActivation *activation, - const char *service_name, - DBusError *error); +BusActivation* bus_activation_new (BusContext *context, + const DBusString *address, + const char **paths, + DBusError *error); +void bus_activation_ref (BusActivation *activation); +void bus_activation_unref (BusActivation *activation); +dbus_bool_t bus_activation_activate_service (BusActivation *activation, + DBusConnection *connection, + DBusMessage *activation_message, + const char *service_name, + DBusError *error); +dbus_bool_t bus_activation_service_created (BusActivation *activation, + const char *service_name, + DBusError *error); + #endif /* BUS_ACTIVATION_H */ diff --git a/bus/bus.c b/bus/bus.c index 1a619b47..6d4f71b3 100644 --- a/bus/bus.c +++ b/bus/bus.c @@ -28,6 +28,7 @@ #include "services.h" #include "utils.h" #include "policy.h" +#include "config-parser.h" #include #include #include @@ -36,7 +37,7 @@ struct BusContext { int refcount; char *address; - DBusServer *server; + DBusList *servers; BusConnections *connections; BusActivation *activation; BusRegistry *registry; @@ -51,24 +52,24 @@ server_watch_callback (DBusWatch *watch, unsigned int condition, void *data) { - BusContext *context = data; + DBusServer *server = data; - return dbus_server_handle_watch (context->server, watch, condition); + return dbus_server_handle_watch (server, watch, condition); } static dbus_bool_t add_server_watch (DBusWatch *watch, - BusContext *context) + void *data) { - return bus_loop_add_watch (watch, server_watch_callback, context, + return bus_loop_add_watch (watch, server_watch_callback, data, NULL); } static void remove_server_watch (DBusWatch *watch, - BusContext *context) + void *data) { - bus_loop_remove_watch (watch, server_watch_callback, context); + bus_loop_remove_watch (watch, server_watch_callback, data); } @@ -82,16 +83,16 @@ server_timeout_callback (DBusTimeout *timeout, static dbus_bool_t add_server_timeout (DBusTimeout *timeout, - BusContext *context) + void *data) { - return bus_loop_add_timeout (timeout, server_timeout_callback, context, NULL); + return bus_loop_add_timeout (timeout, server_timeout_callback, data, NULL); } static void remove_server_timeout (DBusTimeout *timeout, - BusContext *context) + void *data) { - bus_loop_remove_timeout (timeout, server_timeout_callback, context); + bus_loop_remove_timeout (timeout, server_timeout_callback, data); } static void @@ -137,37 +138,136 @@ free_rule_list_func (void *data) dbus_free (list); } +static dbus_bool_t +setup_server (BusContext *context, + DBusServer *server, + DBusError *error) +{ + dbus_server_set_new_connection_function (server, + new_connection_callback, + context, NULL); + + if (!dbus_server_set_watch_functions (server, + add_server_watch, + remove_server_watch, + NULL, + server, + NULL)) + { + BUS_SET_OOM (error); + return FALSE; + } + + if (!dbus_server_set_timeout_functions (server, + add_server_timeout, + remove_server_timeout, + NULL, + server, NULL)) + { + BUS_SET_OOM (error); + return FALSE; + } + + return TRUE; +} + BusContext* -bus_context_new (const char *address, - const char **service_dirs, - DBusError *error) +bus_context_new (const DBusString *config_file, + DBusError *error) { BusContext *context; - + DBusList *link; + DBusList **addresses; + BusConfigParser *parser; + DBusString full_address; + const char *service_dirs[] = { NULL, NULL }; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + if (!_dbus_string_init (&full_address, _DBUS_INT_MAX)) + return NULL; + + parser = NULL; + context = NULL; + + parser = bus_config_load (config_file, error); + if (parser == NULL) + goto failed; context = dbus_new0 (BusContext, 1); if (context == NULL) { BUS_SET_OOM (error); - return NULL; + goto failed; } context->refcount = 1; + + addresses = bus_config_parser_get_addresses (parser); + + link = _dbus_list_get_first_link (addresses); + while (link != NULL) + { + DBusServer *server; + + server = dbus_server_listen (link->data, error); + if (server == NULL) + goto failed; + else if (!setup_server (context, server, error)) + goto failed; - context->address = _dbus_strdup (address); - if (context->address == NULL) + if (!_dbus_list_append (&context->servers, server)) + { + BUS_SET_OOM (error); + goto failed; + } + + link = _dbus_list_get_next_link (addresses, link); + } + + /* We have to build the address backward, so that + * later in the config file have priority + */ + link = _dbus_list_get_last_link (&context->servers); + while (link != NULL) + { + char *addr; + + addr = dbus_server_get_address (link->data); + if (addr == NULL) + { + BUS_SET_OOM (error); + goto failed; + } + + if (_dbus_string_get_length (&full_address) > 0) + { + if (!_dbus_string_append (&full_address, ";")) + { + BUS_SET_OOM (error); + goto failed; + } + } + + if (!_dbus_string_append (&full_address, addr)) + { + BUS_SET_OOM (error); + goto failed; + } + + dbus_free (addr); + + link = _dbus_list_get_prev_link (&context->servers, link); + } + + if (!_dbus_string_copy_data (&full_address, &context->address)) { BUS_SET_OOM (error); goto failed; } - context->server = dbus_server_listen (address, error); - if (context->server == NULL) - goto failed; - - context->activation = bus_activation_new (context, address, service_dirs, - error); + context->activation = bus_activation_new (context, &full_address, + service_dirs, error); if (context->activation == NULL) { _DBUS_ASSERT_ERROR_IS_SET (error); @@ -205,59 +305,58 @@ bus_context_new (const char *address, BUS_SET_OOM (error); goto failed; } - - dbus_server_set_new_connection_function (context->server, - new_connection_callback, - context, NULL); - - if (!dbus_server_set_watch_functions (context->server, - (DBusAddWatchFunction) add_server_watch, - (DBusRemoveWatchFunction) remove_server_watch, - NULL, - context, - NULL)) - { - BUS_SET_OOM (error); - goto failed; - } - if (!dbus_server_set_timeout_functions (context->server, - (DBusAddTimeoutFunction) add_server_timeout, - (DBusRemoveTimeoutFunction) remove_server_timeout, - NULL, - context, NULL)) - { - BUS_SET_OOM (error); - goto failed; - } + bus_config_parser_unref (parser); + _dbus_string_free (&full_address); return context; failed: - bus_context_unref (context); + if (parser != NULL) + bus_config_parser_unref (parser); + + if (context != NULL) + bus_context_unref (context); + + _dbus_string_free (&full_address); return NULL; } -void -bus_context_shutdown (BusContext *context) +static void +shutdown_server (BusContext *context, + DBusServer *server) { - if (context->server == NULL || - !dbus_server_get_is_connected (context->server)) + if (server == NULL || + !dbus_server_get_is_connected (server)) return; - if (!dbus_server_set_watch_functions (context->server, + if (!dbus_server_set_watch_functions (server, NULL, NULL, NULL, context, NULL)) _dbus_assert_not_reached ("setting watch functions to NULL failed"); - if (!dbus_server_set_timeout_functions (context->server, + if (!dbus_server_set_timeout_functions (server, NULL, NULL, NULL, context, NULL)) _dbus_assert_not_reached ("setting timeout functions to NULL failed"); - dbus_server_disconnect (context->server); + dbus_server_disconnect (server); +} + +void +bus_context_shutdown (BusContext *context) +{ + DBusList *link; + + link = _dbus_list_get_first_link (&context->servers); + while (link != NULL) + { + shutdown_server (context, link->data); + + link = _dbus_list_get_next_link (&context->servers, link); + } } void @@ -275,6 +374,8 @@ bus_context_unref (BusContext *context) if (context->refcount == 0) { + DBusList *link; + _dbus_verbose ("Finalizing bus context %p\n", context); bus_context_shutdown (context); @@ -296,12 +397,15 @@ bus_context_unref (BusContext *context) bus_activation_unref (context->activation); context->activation = NULL; } - - if (context->server) + + link = _dbus_list_get_first_link (&context->servers); + while (link != NULL) { - dbus_server_unref (context->server); - context->server = NULL; + dbus_server_unref (link->data); + + link = _dbus_list_get_next_link (&context->servers, link); } + _dbus_list_clear (&context->servers); if (context->rules_by_uid) { diff --git a/bus/bus.h b/bus/bus.h index 56ed5156..3e2dc465 100644 --- a/bus/bus.h +++ b/bus/bus.h @@ -38,19 +38,17 @@ typedef struct BusRegistry BusRegistry; typedef struct BusService BusService; typedef struct BusTransaction BusTransaction; -BusContext* bus_context_new (const char *address, - const char **service_dirs, - DBusError *error); -void bus_context_shutdown (BusContext *context); -void bus_context_ref (BusContext *context); -void bus_context_unref (BusContext *context); -BusRegistry* bus_context_get_registry (BusContext *context); -BusConnections* bus_context_get_connections (BusContext *context); -BusActivation* bus_context_get_activation (BusContext *context); -dbus_bool_t bus_context_allow_user (BusContext *context, - unsigned long uid); -BusPolicy* bus_context_create_connection_policy (BusContext *context, - DBusConnection *connection); - +BusContext* bus_context_new (const DBusString *config_file, + DBusError *error); +void bus_context_shutdown (BusContext *context); +void bus_context_ref (BusContext *context); +void bus_context_unref (BusContext *context); +BusRegistry* bus_context_get_registry (BusContext *context); +BusConnections* bus_context_get_connections (BusContext *context); +BusActivation* bus_context_get_activation (BusContext *context); +dbus_bool_t bus_context_allow_user (BusContext *context, + unsigned long uid); +BusPolicy* bus_context_create_connection_policy (BusContext *context, + DBusConnection *connection); #endif /* BUS_BUS_H */ diff --git a/bus/config-parser.c b/bus/config-parser.c index 972c05ac..bf4f6f7b 100644 --- a/bus/config-parser.c +++ b/bus/config-parser.c @@ -828,6 +828,13 @@ bus_config_parser_finished (BusConfigParser *parser, return FALSE; } + + if (parser->listen_on == NULL) + { + dbus_set_error (error, DBUS_ERROR_FAILED, + "Configuration file needs one or more elements giving addresses"); + return FALSE; + } return TRUE; } @@ -838,6 +845,12 @@ bus_config_parser_get_user (BusConfigParser *parser) return parser->user; } +DBusList** +bus_config_parser_get_addresses (BusConfigParser *parser) +{ + return &parser->listen_on; +} + #ifdef DBUS_BUILD_TESTS #include diff --git a/bus/config-parser.h b/bus/config-parser.h index 2fa651a3..8c66fa63 100644 --- a/bus/config-parser.h +++ b/bus/config-parser.h @@ -28,6 +28,7 @@ #include #include +#include /* Whatever XML library we're using just pushes data into this API */ @@ -55,7 +56,7 @@ dbus_bool_t bus_config_parser_finished (BusConfigParser *parser, /* Functions for extracting the parse results */ const char* bus_config_parser_get_user (BusConfigParser *parser); - +DBusList** bus_config_parser_get_addresses (BusConfigParser *parser); /* Loader functions (backended off one of the XML parsers). Returns a * finished ConfigParser. diff --git a/bus/dispatch.c b/bus/dispatch.c index 2b1dc782..e75d8e6d 100644 --- a/bus/dispatch.c +++ b/bus/dispatch.c @@ -958,18 +958,17 @@ dbus_bool_t bus_dispatch_test (const DBusString *test_data_dir) { BusContext *context; - DBusError error; - const char *activation_dirs[] = { NULL, NULL }; DBusConnection *foo; DBusConnection *bar; DBusConnection *baz; + DBusError error; + + context = bus_context_new_test (test_data_dir, + "valid-config-files/debug-allow-all.conf"); + if (context == NULL) + return FALSE; dbus_error_init (&error); - context = bus_context_new ("debug-pipe:name=test-server", - activation_dirs, - &error); - if (context == NULL) - _dbus_assert_not_reached ("could not alloc context"); foo = dbus_connection_open ("debug-pipe:name=test-server", &error); if (foo == NULL) diff --git a/bus/main.c b/bus/main.c index ce03a6a7..905e4ba0 100644 --- a/bus/main.c +++ b/bus/main.c @@ -29,19 +29,23 @@ main (int argc, char **argv) { BusContext *context; DBusError error; - const char *paths[] = { NULL, NULL }; + DBusString config_file; + + /* FIXME I think the arguments should be like: + * --system use standard system config file + * --session use standard session config file + * --config-file=foo.conf use some other file + */ - if (argc < 3) + if (argc != 2) { - /* FIXME obviously just for testing */ - _dbus_warn ("Give the server address as an argument and activation directory as args\n"); + _dbus_warn ("The message bus configuration file must be given as the only argument\n"); return 1; } - - paths[0] = argv[2]; dbus_error_init (&error); - context = bus_context_new (argv[1], paths, &error); + _dbus_string_init_const (&config_file, argv[1]); + context = bus_context_new (&config_file, &error); if (context == NULL) { _dbus_warn ("Failed to start message bus: %s\n", @@ -55,6 +59,6 @@ main (int argc, char **argv) bus_context_shutdown (context); bus_context_unref (context); - + return 0; } diff --git a/bus/session.conf.in b/bus/session.conf.in new file mode 100644 index 00000000..fe7aa5f0 --- /dev/null +++ b/bus/session.conf.in @@ -0,0 +1,31 @@ + + + + + + unix:path=/tmp/foobar + + + + + + + + + session-local.conf + diff --git a/bus/system.conf.in b/bus/system.conf.in new file mode 100644 index 00000000..fe4e049a --- /dev/null +++ b/bus/system.conf.in @@ -0,0 +1,20 @@ + + + + + fixme + unix:path=@EXPANDED_LOCALSTATEDIR@/@DBUS_SYSTEM_SOCKET@ + + + + + + + + + system-local.conf + diff --git a/bus/test-main.c b/bus/test-main.c index 862ba604..8ef6bfc4 100644 --- a/bus/test-main.c +++ b/bus/test-main.c @@ -62,7 +62,10 @@ main (int argc, char **argv) dir = _dbus_getenv ("DBUS_TEST_DATA"); if (dir == NULL) - dir = ""; + { + fprintf (stderr, "Must specify test data directory as argv[1] or in DBUS_TEST_DATA env variable\n"); + return 1; + } _dbus_string_init_const (&test_data_dir, dir); diff --git a/bus/test.c b/bus/test.c index ea2c3a19..c492cccc 100644 --- a/bus/test.c +++ b/bus/test.c @@ -287,4 +287,57 @@ bus_test_flush_bus (BusContext *context) ; } +BusContext* +bus_context_new_test (const DBusString *test_data_dir, + const char *filename) +{ + DBusError error; + DBusString config_file; + DBusString relative; + BusContext *context; + + if (!_dbus_string_init (&config_file, _DBUS_INT_MAX)) + { + _dbus_warn ("No memory\n"); + return NULL; + } + + if (!_dbus_string_copy (test_data_dir, 0, + &config_file, 0)) + { + _dbus_warn ("No memory\n"); + _dbus_string_free (&config_file); + return NULL; + } + + _dbus_string_init_const (&relative, filename); + + if (!_dbus_concat_dir_and_file (&config_file, &relative)) + { + _dbus_warn ("No memory\n"); + _dbus_string_free (&config_file); + return NULL; + } + + dbus_error_init (&error); + context = bus_context_new (&config_file, &error); + if (context == NULL) + { + _DBUS_ASSERT_ERROR_IS_SET (&error); + + _dbus_warn ("Failed to create debug bus context from configuration file %s: %s\n", + filename, error.message); + + dbus_error_free (&error); + + _dbus_string_free (&config_file); + + return NULL; + } + + _dbus_string_free (&config_file); + + return context; +} + #endif diff --git a/bus/test.h b/bus/test.h index 583a3119..d8ab67b8 100644 --- a/bus/test.h +++ b/bus/test.h @@ -41,6 +41,9 @@ void bus_test_clients_foreach (BusConnectionForeachFunction function, dbus_bool_t bus_test_client_listed (DBusConnection *connection); void bus_test_flush_bus (BusContext *context); +BusContext* bus_context_new_test (const DBusString *test_data_dir, + const char *filename); + #endif #endif /* BUS_TEST_H */ diff --git a/bus/utils.c b/bus/utils.c index 090e27f0..df061165 100644 --- a/bus/utils.c +++ b/bus/utils.c @@ -33,7 +33,7 @@ bus_get_oom_wait (void) { #ifdef DBUS_BUILD_TESTS /* make tests go fast */ - return 10; + return 0; #else return 500; #endif -- cgit