Commit 29c71168 authored by Havoc Pennington's avatar Havoc Pennington

2003-03-31 Havoc Pennington <hp@pobox.com>

	* 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 <servicedir>

	* 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
parent bc86794f
2003-03-31 Havoc Pennington <hp@pobox.com>
* 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 <servicedir>
* 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
2003-03-30 Havoc Pennington <hp@pobox.com>
* bus/config-parser.c: hacking
......
......@@ -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
......
......@@ -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;
......
......@@ -27,19 +27,20 @@
#include <dbus/dbus.h>
#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 */
......@@ -28,6 +28,7 @@
#include "services.h"
#include "utils.h"
#include "policy.h"
#include "config-parser.h"
#include <dbus/dbus-list.h>
#include <dbus/dbus-hash.h>
#include <dbus/dbus-internals.h>
......@@ -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
* <listen> 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)
{
......
......@@ -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 */
......@@ -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 <listen> 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 <stdio.h>
......
......@@ -28,6 +28,7 @@
#include <dbus/dbus.h>
#include <dbus/dbus-string.h>
#include <dbus/dbus-list.h>
/* 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.
......
......@@ -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)
......
......@@ -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;
}
<!-- This configuration file controls the per-user-login-session message bus.
Add a session-local.conf and edit that rather than changing this
file directly. -->
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<!-- FIXME - this is fairly complicated to fix.
Propose the following:
- add "unix:tmpdir=/tmp" which means unix domain transport
creates a socket with a random secure name
- add dbus_server_get_address() that gets the actual
server address
- add command line option or env variable to the daemon
causing it to print its list of addresses to a given
file descriptor
- session manager or whatever launches the session bus
reads the address from there and sets the env variable
-->
<listen>unix:path=/tmp/foobar</listen>
<policy context="default">
<!-- Allow everything -->
<allow send="*"/>
<allow receive="*"/>
<allow own="*"/>
</policy>
<!-- This is included last so local configuration can override what's
in this standard file -->
<include ignore_missing="yes">session-local.conf</include>
</busconfig>
<!-- This configuration file controls the systemwide message bus.
Add a system-local.conf and edit that rather than changing this
file directly. -->
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<user>fixme</user>
<listen>unix:path=@EXPANDED_LOCALSTATEDIR@/@DBUS_SYSTEM_SOCKET@</listen>
<policy context="default">
<!-- Deny everything -->
<deny send="*"/>
<deny receive="*"/>
<deny own="*"/>
</policy>
<!-- This is included last so local configuration can override what's
in this standard file -->
<include ignore_missing="yes">system-local.conf</include>
</busconfig>
......@@ -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);
......
......@@ -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
......@@ -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 */
......@@ -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
......
......@@ -47,6 +47,8 @@ if test x$enable_asserts = xno; then
AC_DEFINE(DBUS_DISABLE_ASSERT,1,[Disable assertion checking])
fi
#### gcc warning flags
if test "x$GCC" = "xyes"; then
changequote(,)dnl
case " $CFLAGS " in
......@@ -120,6 +122,8 @@ else
fi
fi
#### Assorted checks
AC_CHECK_SIZEOF(char)
AC_CHECK_SIZEOF(short)
AC_CHECK_SIZEOF(long)
......@@ -350,9 +354,33 @@ dnl Qt flags
AC_SUBST(DBUS_QT_CXXFLAGS)
AC_SUBST(DBUS_QT_LIBS)
##### Set up location for system bus socket
## name of socket relative to localstatedir
DBUS_SYSTEM_SOCKET=run/dbus/system_bus_socket