diff options
author | Havoc Pennington <hp@redhat.com> | 2003-03-13 00:56:43 +0000 |
---|---|---|
committer | Havoc Pennington <hp@redhat.com> | 2003-03-13 00:56:43 +0000 |
commit | 29560adcc79a259a0be3511c056ee7453aa26c04 (patch) | |
tree | 57e72dd26b5876da48379e5ff910c63e66cb7001 /bus | |
parent | 799a3ff443f5357ae7857ebe989a7f92f7bd84df (diff) |
2003-03-12 Havoc Pennington <hp@redhat.com>
Mega-patch that gets the message bus daemon initially handling
out-of-memory. Work still needed. Also lots of random
moving stuff to DBusError instead of ResultCode.
* dbus/dbus-list.c (_dbus_list_length_is_one): new function
* dbus/dbus-connection.c
(dbus_connection_send_with_reply_and_block): use DBusError
* dbus/dbus-bus.c: adapt to API changes, make it use DBusError not
DBusResultCode
* dbus/dbus-connection.c (dbus_connection_send): drop the result
code here, as the only failure possible is OOM.
* bus/connection.c (bus_connection_disconnect):
rename bus_connection_disconnected as it's a notification only
* bus/driver.c (bus_driver_handle_acquire_service): don't free
"name" on get_args failure, should be done by get_args;
don't disconnect client for bad args, just return an error.
(bus_driver_handle_service_exists): ditto
* bus/services.c (bus_services_list): NULL-terminate returned array
* bus/driver.c (bus_driver_send_service_lost)
(bus_driver_send_service_acquired): send messages from driver to a
specific client to the client's unique name, not to the broadcast
service.
* dbus/dbus-message.c (decode_header_data): reject messages that
contain no name field
(_dbus_message_get_client_serial): rename to
dbus_message_get_serial and make public
(_dbus_message_set_serial): rename from set_client_serial
(_dbus_message_set_reply_serial): make public
(_dbus_message_get_reply_serial): make public
* bus/connection.c (bus_connection_foreach): allow stopping
iteration by returning FALSE from foreach function.
* dbus/dbus-connection.c (dbus_connection_send_preallocated)
(dbus_connection_free_preallocated_send)
(dbus_connection_preallocate_send): new API for sending a message
without possibility of malloc failure.
(dbus_connection_send_message): rename to just
dbus_connection_send (and same for whole function family)
* dbus/dbus-errors.c (dbus_error_free): make this reinit the error
* dbus/dbus-sysdeps.c (_dbus_exit): new function
* bus/activation.c: handle/return errors
* dbus/dbus-errors.h: add more DBUS_ERROR #define
* dbus/dbus-sysdeps.c (_dbus_directory_open) (_dbus_file_get_contents)
(_dbus_directory_get_next_file): use DBusError instead of DBusResultCode
(_dbus_result_from_errno): move to this file
Diffstat (limited to 'bus')
-rw-r--r-- | bus/activation.c | 231 | ||||
-rw-r--r-- | bus/activation.h | 5 | ||||
-rw-r--r-- | bus/connection.c | 422 | ||||
-rw-r--r-- | bus/connection.h | 26 | ||||
-rw-r--r-- | bus/desktop-file.c | 180 | ||||
-rw-r--r-- | bus/dispatch.c | 281 | ||||
-rw-r--r-- | bus/dispatch.h | 6 | ||||
-rw-r--r-- | bus/driver.c | 680 | ||||
-rw-r--r-- | bus/driver.h | 31 | ||||
-rw-r--r-- | bus/main.c | 13 | ||||
-rw-r--r-- | bus/services.c | 165 | ||||
-rw-r--r-- | bus/services.h | 21 | ||||
-rw-r--r-- | bus/utils.c | 2 | ||||
-rw-r--r-- | bus/utils.h | 5 |
14 files changed, 1626 insertions, 442 deletions
diff --git a/bus/activation.c b/bus/activation.c index b5cec44a..4e428bde 100644 --- a/bus/activation.c +++ b/bus/activation.c @@ -59,18 +59,24 @@ bus_activation_entry_free (BusActivationEntry *entry) } static dbus_bool_t -add_desktop_file_entry (BusDesktopFile *desktop_file) +add_desktop_file_entry (BusDesktopFile *desktop_file, + DBusError *error) { char *name, *exec; BusActivationEntry *entry; + + name = NULL; + exec = NULL; + entry = NULL; if (!bus_desktop_file_get_string (desktop_file, DBUS_SERVICE_SECTION, DBUS_SERVICE_NAME, &name)) { - _dbus_verbose ("No \""DBUS_SERVICE_NAME"\" key in .service file\n"); - return FALSE; + dbus_set_error (error, DBUS_ERROR_FAILED, + "No \""DBUS_SERVICE_NAME"\" key in .service file\n"); + goto failed; } if (!bus_desktop_file_get_string (desktop_file, @@ -78,57 +84,104 @@ add_desktop_file_entry (BusDesktopFile *desktop_file) DBUS_SERVICE_EXEC, &exec)) { - _dbus_verbose ("No \""DBUS_SERVICE_EXEC"\" key in .service file\n"); - - dbus_free (name); - return FALSE; + dbus_set_error (error, DBUS_ERROR_FAILED, + "No \""DBUS_SERVICE_EXEC"\" key in .service file\n"); + goto failed; } + /* FIXME we need a better-defined algorithm for which service file to + * pick than "whichever one is first in the directory listing" + */ if (_dbus_hash_table_lookup_string (activation_entries, name)) { - _dbus_verbose ("Service %s already exists in activation entry list\n", name); - dbus_free (name); - dbus_free (exec); - - return FALSE; + dbus_set_error (error, DBUS_ERROR_FAILED, + "Service %s already exists in activation entry list\n", name); + goto failed; + } + + entry = dbus_new0 (BusActivationEntry, 1); + if (entry == NULL) + { + BUS_SET_OOM (error); + goto failed; } - BUS_HANDLE_OOM (entry = dbus_malloc0 (sizeof (BusActivationEntry))); entry->name = name; entry->exec = exec; - BUS_HANDLE_OOM (_dbus_hash_table_insert_string (activation_entries, entry->name, entry)); + if (!_dbus_hash_table_insert_string (activation_entries, entry->name, entry)) + { + BUS_SET_OOM (error); + goto failed; + } _dbus_verbose ("Added \"%s\" to list of services\n", entry->name); return TRUE; + + failed: + dbus_free (name); + dbus_free (exec); + dbus_free (entry); + + return FALSE; } -static void -load_directory (const char *directory) +/* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip + * hash entries it already added. + */ +static dbus_bool_t +load_directory (const char *directory, + DBusError *error) { DBusDirIter *iter; DBusString dir, filename; - DBusResultCode result; - + DBusString full_path; + BusDesktopFile *desktop_file; + DBusError tmp_error; + _dbus_string_init_const (&dir, directory); + + iter = NULL; + desktop_file = NULL; - iter = _dbus_directory_open (&dir, &result); - if (iter == NULL) + if (!_dbus_string_init (&filename, _DBUS_INT_MAX)) { - _dbus_verbose ("Failed to open directory %s: &s\n", directory, - result); - return; + BUS_SET_OOM (error); + return FALSE; } - BUS_HANDLE_OOM (_dbus_string_init (&filename, _DBUS_INT_MAX)); + if (!_dbus_string_init (&full_path, _DBUS_INT_MAX)) + { + BUS_SET_OOM (error); + _dbus_string_free (&filename); + return FALSE; + } + + /* from this point it's safe to "goto failed" */ + + iter = _dbus_directory_open (&dir, error); + if (iter == NULL) + { + _dbus_verbose ("Failed to open directory %s: %s\n", + directory, error ? error->message : "unknown"); + goto failed; + } /* Now read the files */ - while (_dbus_directory_get_next_file (iter, &filename, &result)) + dbus_error_init (&tmp_error); + while (_dbus_directory_get_next_file (iter, &filename, &tmp_error)) { - DBusString full_path; - BusDesktopFile *desktop_file; - DBusError error; + _dbus_assert (!dbus_error_is_set (&tmp_error)); + + _dbus_string_set_length (&full_path, 0); + + if (!_dbus_string_append (&full_path, directory) || + !_dbus_concat_dir_and_file (&full_path, &filename)) + { + BUS_SET_OOM (error); + goto failed; + } if (!_dbus_string_ends_with_c_str (&filename, ".service")) { @@ -136,71 +189,133 @@ load_directory (const char *directory) _dbus_string_get_const_data (&filename, &filename_c); _dbus_verbose ("Skipping non-.service file %s\n", filename_c); - continue; + continue; } - BUS_HANDLE_OOM (_dbus_string_init (&full_path, _DBUS_INT_MAX)); - BUS_HANDLE_OOM (_dbus_string_append (&full_path, directory)); - - BUS_HANDLE_OOM (_dbus_concat_dir_and_file (&full_path, &filename)); + desktop_file = bus_desktop_file_load (&full_path, &tmp_error); - desktop_file = bus_desktop_file_load (&full_path, &error); - - if (!desktop_file) + if (desktop_file == NULL) { const char *full_path_c; _dbus_string_get_const_data (&full_path, &full_path_c); _dbus_verbose ("Could not load %s: %s\n", full_path_c, - error.message); - dbus_error_free (&error); - _dbus_string_free (&full_path); + tmp_error.message); + + if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY)) + { + dbus_move_error (&tmp_error, error); + goto failed; + } + + dbus_error_free (&tmp_error); continue; } - if (!add_desktop_file_entry (desktop_file)) + if (!add_desktop_file_entry (desktop_file, &tmp_error)) { const char *full_path_c; + bus_desktop_file_free (desktop_file); + desktop_file = NULL; + _dbus_string_get_const_data (&full_path, &full_path_c); - _dbus_verbose ("Could not add %s to activation entry list.\n", full_path_c); + _dbus_verbose ("Could not add %s to activation entry list: %s\n", + full_path_c, tmp_error.message); + + if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY)) + { + dbus_move_error (&tmp_error, error); + goto failed; + } + + dbus_error_free (&tmp_error); + continue; } + else + { + bus_desktop_file_free (desktop_file); + desktop_file = NULL; + continue; + } + } - bus_desktop_file_free (desktop_file); - _dbus_string_free (&full_path); + if (dbus_error_is_set (&tmp_error)) + { + dbus_move_error (&tmp_error, error); + goto failed; } + + return TRUE; + + failed: + _DBUS_ASSERT_ERROR_IS_SET (error); + + if (iter != NULL) + _dbus_directory_close (iter); + if (desktop_file) + bus_desktop_file_free (desktop_file); + _dbus_string_free (&filename); + _dbus_string_free (&full_path); + + return FALSE; } - -void -bus_activation_init (const char *address, - const char **directories) +dbus_bool_t +bus_activation_init (const char *address, + const char **directories, + DBusError *error) { int i; + _dbus_assert (server_address == NULL); + _dbus_assert (activation_entries == NULL); + /* FIXME: We should split up the server addresses. */ - BUS_HANDLE_OOM (server_address = _dbus_strdup (address)); + server_address = _dbus_strdup (address); + if (server_address == NULL) + { + BUS_SET_OOM (error); + goto failed; + } - BUS_HANDLE_OOM (activation_entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, - (DBusFreeFunction)bus_activation_entry_free)); - - i = 0; + activation_entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, + (DBusFreeFunction)bus_activation_entry_free); + if (activation_entries == NULL) + { + BUS_SET_OOM (error); + goto failed; + } /* Load service files */ + i = 0; while (directories[i] != NULL) { - load_directory (directories[i]); - i++; + if (!load_directory (directories[i], error)) + goto failed; + ++i; } + + return TRUE; + + failed: + dbus_free (server_address); + if (activation_entries) + _dbus_hash_table_unref (activation_entries); + + return FALSE; } static void child_setup (void *data) { - /* FIXME: Check return value in case of OOM */ - _dbus_setenv ("DBUS_ADDRESS", server_address); + /* If no memory, we simply have the child exit, so it won't try + * to connect to the wrong thing. + */ + if (!_dbus_setenv ("DBUS_ADDRESS", server_address)) + _dbus_exit (1); } dbus_bool_t @@ -220,6 +335,10 @@ bus_activation_activate_service (const char *service_name, return FALSE; } + /* FIXME we need to support a full command line, not just a single + * argv[0] + */ + /* Now try to spawn the process */ argv[0] = entry->exec; argv[1] = NULL; diff --git a/bus/activation.h b/bus/activation.h index 5f29871e..e7a9cdfc 100644 --- a/bus/activation.h +++ b/bus/activation.h @@ -26,8 +26,9 @@ #include <dbus/dbus.h> -void bus_activation_init (const char *address, - const char **paths); +dbus_bool_t bus_activation_init (const char *address, + const char **paths, + DBusError *error); dbus_bool_t bus_activation_activate_service (const char *service_name, DBusError *error); diff --git a/bus/connection.c b/bus/connection.c index 40bbc325..ff671c58 100644 --- a/bus/connection.c +++ b/bus/connection.c @@ -24,34 +24,79 @@ #include "dispatch.h" #include "loop.h" #include "services.h" +#include "utils.h" #include <dbus/dbus-list.h> +static void bus_connection_remove_transactions (DBusConnection *connection); + static int connection_data_slot; static DBusList *connections = NULL; typedef struct { + DBusConnection *connection; DBusList *services_owned; - char *name; + DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */ + DBusMessage *oom_message; + DBusPreallocatedSend *oom_preallocated; } BusConnectionData; #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot)) void -bus_connection_disconnect (DBusConnection *connection) +bus_connection_disconnected (DBusConnection *connection) { BusConnectionData *d; BusService *service; - + _dbus_warn ("Disconnected\n"); d = BUS_CONNECTION_DATA (connection); _dbus_assert (d != NULL); - /* Drop any service ownership */ - while ((service = _dbus_list_get_last (&d->services_owned))) - bus_service_remove_owner (service, connection); + /* Drop any service ownership. FIXME Unfortunately, this requires + * memory allocation and there doesn't seem to be a good way to + * handle it other than sleeping; we can't "fail" the operation of + * disconnecting a client, and preallocating a broadcast "service is + * now gone" message for every client-service pair seems kind of + * involved. Probably we need to do that though, and also + * extend BusTransaction to be able to revert generic + * stuff, not just sending a message (so we can e.g. revert + * removal of service owners). + */ + { + BusTransaction *transaction; + DBusError error; + + dbus_error_init (&error); + + transaction = NULL; + while (transaction == NULL) + { + transaction = bus_transaction_new (); + bus_wait_for_memory (); + } + + while ((service = _dbus_list_get_last (&d->services_owned))) + { + retry: + if (!bus_service_remove_owner (service, connection, + transaction, &error)) + { + if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) + { + dbus_error_free (&error); + bus_wait_for_memory (); + goto retry; + } + else + _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason"); + } + } + + bus_transaction_execute_and_free (transaction); + } bus_dispatch_remove_connection (connection); @@ -60,12 +105,14 @@ bus_connection_disconnect (DBusConnection *connection) NULL, NULL, connection, NULL); + + bus_connection_remove_transactions (connection); dbus_connection_set_data (connection, connection_data_slot, NULL, NULL); - _dbus_list_remove (&connections, connection); + _dbus_list_remove (&connections, connection); dbus_connection_unref (connection); } @@ -106,7 +153,14 @@ free_connection_data (void *data) /* services_owned should be NULL since we should be disconnected */ _dbus_assert (d->services_owned == NULL); + /* similarly */ + _dbus_assert (d->transaction_messages == NULL); + if (d->oom_preallocated) + dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated); + if (d->oom_message) + dbus_message_unref (d->oom_message); + dbus_free (d->name); dbus_free (d); @@ -132,6 +186,8 @@ bus_connection_setup (DBusConnection *connection) if (d == NULL) return FALSE; + + d->connection = connection; if (!dbus_connection_set_data (connection, connection_data_slot, @@ -163,6 +219,88 @@ bus_connection_setup (DBusConnection *connection) return TRUE; } +/** + * Checks whether the connection is registered with the message bus. + * + * @param connection the connection + * @returns #TRUE if we're an active message bus participant + */ +dbus_bool_t +bus_connection_is_active (DBusConnection *connection) +{ + BusConnectionData *d; + + d = BUS_CONNECTION_DATA (connection); + + return d != NULL && d->name != NULL; +} + +dbus_bool_t +bus_connection_preallocate_oom_error (DBusConnection *connection) +{ + DBusMessage *message; + DBusPreallocatedSend *preallocated; + BusConnectionData *d; + + d = BUS_CONNECTION_DATA (connection); + + _dbus_assert (d != NULL); + + if (d->oom_preallocated != NULL) + return TRUE; + + preallocated = dbus_connection_preallocate_send (connection); + if (preallocated == NULL) + return FALSE; + + message = dbus_message_new (DBUS_SERVICE_DBUS, + DBUS_ERROR_NO_MEMORY); + if (message == NULL) + { + dbus_connection_free_preallocated_send (connection, preallocated); + return FALSE; + } + + /* set reply serial to placeholder value just so space is already allocated + * for it. + */ + if (!dbus_message_set_reply_serial (message, 14)) + { + dbus_connection_free_preallocated_send (connection, preallocated); + dbus_message_unref (message); + return FALSE; + } + + d->oom_message = message; + d->oom_preallocated = preallocated; + + return TRUE; +} + +void +bus_connection_send_oom_error (DBusConnection *connection, + DBusMessage *in_reply_to) +{ + BusConnectionData *d; + + d = BUS_CONNECTION_DATA (connection); + + _dbus_assert (d != NULL); + _dbus_assert (d->oom_message != NULL); + + /* should always succeed since we set it to a placeholder earlier */ + if (!dbus_message_set_reply_serial (d->oom_message, + dbus_message_get_serial (in_reply_to))) + _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message"); + + dbus_connection_send_preallocated (connection, d->oom_preallocated, + d->oom_message, NULL); + + dbus_message_unref (d->oom_message); + d->oom_message = NULL; + d->oom_preallocated = NULL; +} + dbus_bool_t bus_connection_add_owned_service (DBusConnection *connection, BusService *service) @@ -223,9 +361,277 @@ bus_connection_get_name (DBusConnection *connection) return d->name; } +/** + * Calls function on each connection; if the function returns + * #FALSE, stops iterating. + * + * @param function the function + * @param data data to pass to it as a second arg + */ void bus_connection_foreach (BusConnectionForeachFunction function, void *data) { - _dbus_list_foreach (&connections, (DBusForeachFunction)function, data); + DBusList *link; + + link = _dbus_list_get_first_link (&connections); + while (link != NULL) + { + DBusConnection *connection = link->data; + DBusList *next = _dbus_list_get_next_link (&connections, link); + + if (!(* function) (connection, data)) + break; + + link = next; + } +} + +typedef struct +{ + BusTransaction *transaction; + DBusMessage *message; + DBusPreallocatedSend *preallocated; +} MessageToSend; + +struct BusTransaction +{ + DBusList *connections; + +}; + +static void +message_to_send_free (DBusConnection *connection, + MessageToSend *to_send) +{ + if (to_send->message) + dbus_message_unref (to_send->message); + + if (to_send->preallocated) + dbus_connection_free_preallocated_send (connection, to_send->preallocated); + + dbus_free (to_send); +} + +BusTransaction* +bus_transaction_new (void) +{ + BusTransaction *transaction; + + transaction = dbus_new0 (BusTransaction, 1); + if (transaction == NULL) + return NULL; + + return transaction; +} + +dbus_bool_t +bus_transaction_send_message (BusTransaction *transaction, + DBusConnection *connection, + DBusMessage *message) +{ + MessageToSend *to_send; + BusConnectionData *d; + DBusList *link; + + if (!dbus_connection_get_is_connected (connection)) + return TRUE; /* silently ignore disconnected connections */ + + d = BUS_CONNECTION_DATA (connection); + _dbus_assert (d != NULL); + + to_send = dbus_new (MessageToSend, 1); + if (to_send == NULL) + { + return FALSE; + } + + to_send->preallocated = dbus_connection_preallocate_send (connection); + if (to_send->preallocated == NULL) + { + dbus_free (to_send); + return FALSE; + } + + dbus_message_ref (message); + to_send->message = message; + to_send->transaction = transaction; + + if (!_dbus_list_prepend (&d->transaction_messages, to_send)) + { + message_to_send_free (connection, to_send); + return FALSE; + } + + /* See if we already had this connection in the list + * for this transaction. If we have a pending message, + * then we should already be in transaction->connections + */ + link = _dbus_list_get_first_link (&d->transaction_messages); + _dbus_assert (link->data == to_send); + link = _dbus_list_get_next_link (&d->transaction_messages, link); + while (link != NULL) + { + MessageToSend *m = link->data; + DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link); + + if (m->transaction == transaction) + break; + + link = next; + } + + if (link == NULL) + { + if (!_dbus_list_prepend (&transaction->connections, connection)) + { + _dbus_list_remove (&d->transaction_messages, to_send); + message_to_send_free (connection, to_send); + return FALSE; + } + } + + return TRUE; +} + +static void +connection_cancel_transaction (DBusConnection *connection, + BusTransaction *transaction) +{ + DBusList *link; + BusConnectionData *d; + + d = BUS_CONNECTION_DATA (connection); + _dbus_assert (d != NULL); + + link = _dbus_list_get_first_link (&d->transaction_messages); + while (link != NULL) + { + MessageToSend *m = link->data; + DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link); + + if (m->transaction == transaction) + { + _dbus_list_remove_link (&d->transaction_messages, + link); + + message_to_send_free (connection, m); + } + + link = next; + } +} + +void +bus_transaction_cancel_and_free (BusTransaction *transaction) +{ + DBusConnection *connection; + + while ((connection = _dbus_list_pop_first (&transaction->connections))) + connection_cancel_transaction (connection, transaction); + + _dbus_assert (transaction->connections == NULL); + + dbus_free (transaction); +} + +static void +connection_execute_transaction (DBusConnection *connection, + BusTransaction *transaction) +{ + DBusList *link; + BusConnectionData *d; + + d = BUS_CONNECTION_DATA (connection); + _dbus_assert (d != NULL); + + /* Send the queue in order (FIFO) */ + link = _dbus_list_get_last_link (&d->transaction_messages); + while (link != NULL) + { + MessageToSend *m = link->data; + DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link); + + if (m->transaction == transaction) + { + _dbus_list_remove_link (&d->transaction_messages, + link); + + dbus_connection_send_preallocated (connection, + m->preallocated, + m->message, + NULL); + + m->preallocated = NULL; /* so we don't double-free it */ + + message_to_send_free (connection, m); + } + + link = prev; + } +} + +void +bus_transaction_execute_and_free (BusTransaction *transaction) +{ + /* For each connection in transaction->connections + * send the messages + */ + DBusConnection *connection; + + while ((connection = _dbus_list_pop_first (&transaction->connections))) + connection_execute_transaction (connection, transaction); + + _dbus_assert (transaction->connections == NULL); + + dbus_free (transaction); +} + +static void +bus_connection_remove_transactions (DBusConnection *connection) +{ + MessageToSend *to_send; + BusConnectionData *d; + + d = BUS_CONNECTION_DATA (connection); + _dbus_assert (d != NULL); + + while ((to_send = _dbus_list_get_first (&d->transaction_messages))) + { + /* only has an effect for the first MessageToSend listing this transaction */ + _dbus_list_remove (&to_send->transaction->connections, + connection); + + _dbus_list_remove (&d->transaction_messages, to_send); + message_to_send_free (connection, to_send); + } +} + +/** + * Converts the DBusError to a message reply + */ +dbus_bool_t +bus_transaction_send_error_reply (BusTransaction *transaction, + DBusConnection *connection, + const DBusError *error, + DBusMessage *in_reply_to) +{ + DBusMessage *reply; + + _dbus_assert (error != NULL); + _DBUS_ASSERT_ERROR_IS_SET (error); + + reply = dbus_message_new_error_reply (in_reply_to, + error->name, + error->message); + if (reply == NULL) + return FALSE; + + if (!bus_transaction_send_message (transaction, connection, reply)) + { + dbus_message_unref (reply); + return FALSE; + } + + return TRUE; } diff --git a/bus/connection.h b/bus/connection.h index 04ab1f08..a7a448a1 100644 --- a/bus/connection.h +++ b/bus/connection.h @@ -27,13 +27,19 @@ #include <dbus/dbus.h> #include "services.h" -typedef void (* BusConnectionForeachFunction) (DBusConnection *connection, - void *data); +typedef dbus_bool_t (* BusConnectionForeachFunction) (DBusConnection *connection, + void *data); dbus_bool_t bus_connection_init (void); dbus_bool_t bus_connection_setup (DBusConnection *connection); +dbus_bool_t bus_connection_is_active (DBusConnection *connection); + +dbus_bool_t bus_connection_preallocate_oom_error (DBusConnection *connection); +void bus_connection_send_oom_error (DBusConnection *connection, + DBusMessage *in_reply_to); + /* called by services.c */ dbus_bool_t bus_connection_add_owned_service (DBusConnection *connection, BusService *service); @@ -47,8 +53,20 @@ const char *bus_connection_get_name (DBusConnection *connection); void bus_connection_foreach (BusConnectionForeachFunction function, void *data); -/* called by dispatch.c */ -void bus_connection_disconnect (DBusConnection *connection); +/* called by dispatch.c when the connection is dropped */ +void bus_connection_disconnected (DBusConnection *connection); + +/* transaction API so we can send or not send a block of messages as a whole */ +BusTransaction* bus_transaction_new (void); +dbus_bool_t bus_transaction_send_message (BusTransaction *transaction, + DBusConnection *connection, + DBusMessage *message); +dbus_bool_t bus_transaction_send_error_reply (BusTransaction *transaction, + DBusConnection *connection, + const DBusError *error, + DBusMessage *in_reply_to); +void bus_transaction_cancel_and_free (BusTransaction *transaction); +void bus_transaction_execute_and_free (BusTransaction *transaction); #endif /* BUS_CONNECTION_H */ diff --git a/bus/desktop-file.c b/bus/desktop-file.c index 0ab6afc8..65a0d76f 100644 --- a/bus/desktop-file.c +++ b/bus/desktop-file.c @@ -125,7 +125,7 @@ bus_desktop_file_free (BusDesktopFile *desktop_file) dbus_free (desktop_file); } -static void +static dbus_bool_t grow_lines_in_section (BusDesktopFileSection *section) { BusDesktopFileLine *lines; @@ -137,14 +137,19 @@ grow_lines_in_section (BusDesktopFileSection *section) else new_n_lines = section->n_allocated_lines*2; - BUS_HANDLE_OOM (lines = dbus_realloc (section->lines, - sizeof (BusDesktopFileLine) * new_n_lines)); - section->lines = lines; + lines = dbus_realloc (section->lines, + sizeof (BusDesktopFileLine) * new_n_lines); + + if (lines == NULL) + return FALSE; + section->lines = lines; section->n_allocated_lines = new_n_lines; + + return TRUE; } -static void +static dbus_bool_t grow_sections (BusDesktopFile *desktop_file) { int new_n_sections; @@ -155,21 +160,36 @@ grow_sections (BusDesktopFile *desktop_file) else new_n_sections = desktop_file->n_allocated_sections*2; - BUS_HANDLE_OOM (sections = dbus_realloc (desktop_file->sections, - sizeof (BusDesktopFileSection) * new_n_sections)); + sections = dbus_realloc (desktop_file->sections, + sizeof (BusDesktopFileSection) * new_n_sections); + if (sections == NULL) + return FALSE; + desktop_file->sections = sections; desktop_file->n_allocated_sections = new_n_sections; + + return TRUE; } static char * -unescape_string (const DBusString *str, int pos, int end_pos) +unescape_string (BusDesktopFileParser *parser, + const DBusString *str, + int pos, + int end_pos, + DBusError *error) { char *retval, *q; /* len + 1 is enough, because unescaping never makes the - * string longer */ - BUS_HANDLE_OOM (retval = dbus_malloc (end_pos - pos + 1)); + * string longer + */ + retval = dbus_malloc (end_pos - pos + 1); + if (retval == NULL) + { + BUS_SET_OOM (error); + return NULL; + } q = retval; @@ -179,6 +199,8 @@ unescape_string (const DBusString *str, int pos, int end_pos) { /* Found an embedded null */ dbus_free (retval); + report_error (parser, "Text to be unescaped contains embedded nul", + BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); return NULL; } @@ -190,6 +212,8 @@ unescape_string (const DBusString *str, int pos, int end_pos) { /* Escape at end of string */ dbus_free (retval); + report_error (parser, "Text to be unescaped ended in \\", + BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); return NULL; } @@ -213,7 +237,9 @@ unescape_string (const DBusString *str, int pos, int end_pos) default: /* Invalid escape code */ dbus_free (retval); - return NULL; + report_error (parser, "Text to be unescaped had invalid escape sequence", + BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); + return NULL; } pos++; } @@ -235,20 +261,34 @@ new_section (BusDesktopFile *desktop_file, const char *name) { int n; + char *name_copy; if (desktop_file->n_allocated_sections == desktop_file->n_sections) - grow_sections (desktop_file); - - n = desktop_file->n_sections++; + { + if (!grow_sections (desktop_file)) + return NULL; + } - BUS_HANDLE_OOM (desktop_file->sections[n].section_name = _dbus_strdup (name)); + name_copy = _dbus_strdup (name); + if (name_copy == NULL) + return NULL; + + n = desktop_file->n_sections + 1; + desktop_file->sections[n].section_name = name_copy; desktop_file->sections[n].n_lines = 0; desktop_file->sections[n].lines = NULL; desktop_file->sections[n].n_allocated_lines = 0; - grow_lines_in_section (&desktop_file->sections[n]); + if (!grow_lines_in_section (&desktop_file->sections[n])) + { + dbus_free (desktop_file->sections[n].section_name); + desktop_file->sections[n].section_name = NULL; + return NULL; + } + desktop_file->n_sections = n; + return &desktop_file->sections[n]; } @@ -277,7 +317,10 @@ new_line (BusDesktopFileParser *parser) section = &parser->desktop_file->sections[parser->current_section]; if (section->n_allocated_lines == section->n_lines) - grow_lines_in_section (section); + { + if (!grow_lines_in_section (section)) + return NULL; + } line = §ion->lines[section->n_lines++]; @@ -358,11 +401,12 @@ parse_section_start (BusDesktopFileParser *parser, DBusError *error) return FALSE; } - section_name = unescape_string (&parser->data, parser->pos + 1, line_end - 1); + section_name = unescape_string (parser, + &parser->data, parser->pos + 1, line_end - 1, + error); if (section_name == NULL) { - report_error (parser, "Invalid escaping in section name", BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); parser_free (parser); return FALSE; } @@ -450,20 +494,39 @@ parse_key_value (BusDesktopFileParser *parser, DBusError *error) value_start = p; - value = unescape_string (&parser->data, value_start, line_end); + value = unescape_string (parser, &parser->data, value_start, line_end, error); if (value == NULL) { - report_error (parser, "Invalid escaping in value", BUS_DESKTOP_PARSE_ERROR_INVALID_ESCAPES, error); parser_free (parser); return FALSE; } line = new_line (parser); - - BUS_HANDLE_OOM (_dbus_string_init (&key, key_end - key_start)); - BUS_HANDLE_OOM (_dbus_string_copy_len (&parser->data, key_start, key_end - key_start, - &key, 0)); - BUS_HANDLE_OOM (_dbus_string_steal_data (&key, &tmp)); + if (line == NULL) + { + parser_free (parser); + return FALSE; + } + + if (!_dbus_string_init (&key, key_end - key_start)) + { + parser_free (parser); + return FALSE; + } + + if (!_dbus_string_copy_len (&parser->data, key_start, key_end - key_start, + &key, 0)) + { + parser_free (parser); + return FALSE; + } + + if (!_dbus_string_steal_data (&key, &tmp)) + { + parser_free (parser); + return FALSE; + } + _dbus_string_free (&key); line->key = tmp; @@ -491,11 +554,11 @@ report_error (BusDesktopFileParser *parser, section_name = parser->desktop_file->sections[parser->current_section].section_name; if (section_name) - BUS_HANDLE_OOM (dbus_set_error (error, error_name, - "Error in section %s at line %d: %s\n", section_name, parser->line_num, message)); + dbus_set_error (error, error_name, + "Error in section %s at line %d: %s\n", section_name, parser->line_num, message); else - BUS_HANDLE_OOM (dbus_set_error (error, error_name, - "Error at line %d: %s\n", parser->line_num, message)); + dbus_set_error (error, error_name, + "Error at line %d: %s\n", parser->line_num, message); } #if 0 @@ -519,39 +582,52 @@ dump_desktop_file (BusDesktopFile *file) } #endif -BusDesktopFile * +BusDesktopFile* bus_desktop_file_load (DBusString *filename, DBusError *error) { DBusString str; - DBusResultCode result_code; BusDesktopFileParser parser; + DBusStat sb; - /* FIXME: Check file size so we don't try to load a ridicously large file. */ + /* Clearly there's a race here, but it's just to make it unlikely + * that we do something silly, we still handle doing it below. + */ + if (!_dbus_stat (filename, &sb, error)) + return NULL; - BUS_HANDLE_OOM (_dbus_string_init (&str, _DBUS_INT_MAX)); + if (sb.size > _DBUS_ONE_KILOBYTE * 128) + { + dbus_set_error (error, DBUS_ERROR_FAILED, + "Desktop file size (%ld bytes) is too large", (long) sb.size); + return NULL; + } - BUS_HANDLE_OOM ((result_code = _dbus_file_get_contents (&str, filename)) != - DBUS_RESULT_NO_MEMORY); + if (!_dbus_string_init (&str, _DBUS_INT_MAX)) + return NULL; - if (result_code != DBUS_RESULT_SUCCESS) + if (!_dbus_file_get_contents (&str, filename, error)) { _dbus_string_free (&str); - - /* FIXME: Set error */ return NULL; } if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str))) { _dbus_string_free (&str); - - /* FIXME: Set error */ + dbus_set_error (error, DBUS_ERROR_FAILED, + "invalid UTF-8"); + return NULL; + } + + parser.desktop_file = dbus_new0 (BusDesktopFile, 1); + if (parser.desktop_file == NULL) + { + _dbus_string_free (&str); + BUS_SET_OOM (error); return NULL; } - BUS_HANDLE_OOM (parser.desktop_file = dbus_malloc0 (sizeof (BusDesktopFile))); - parser.data = str; parser.line_num = 1; parser.pos = 0; @@ -563,7 +639,10 @@ bus_desktop_file_load (DBusString *filename, if (_dbus_string_get_byte (&parser.data, parser.pos) == '[') { if (!parse_section_start (&parser, error)) - return NULL; + { + _dbus_string_free (&parser.data); + return NULL; + } } else if (is_blank_line (&parser) || _dbus_string_get_byte (&parser.data, parser.pos) == '#') @@ -571,7 +650,10 @@ bus_desktop_file_load (DBusString *filename, else { if (!parse_key_value (&parser, error)) - return NULL; + { + _dbus_string_free (&parser.data); + return NULL; + } } } @@ -661,7 +743,13 @@ bus_desktop_file_get_string (BusDesktopFile *desktop_file, if (!bus_desktop_file_get_raw (desktop_file, section, keyname, &raw)) return FALSE; - BUS_HANDLE_OOM (*val = _dbus_strdup (raw)); + *val = _dbus_strdup (raw); + + /* FIXME we don't distinguish "key not found" from "out of memory" here, + * which is broken. + */ + if (*val == NULL) + return FALSE; return TRUE; } diff --git a/bus/dispatch.c b/bus/dispatch.c index 76e10a9f..d9fe81ac 100644 --- a/bus/dispatch.c +++ b/bus/dispatch.c @@ -30,23 +30,110 @@ static int message_handler_slot; -static void +typedef struct +{ + DBusMessage *message; + BusTransaction *transaction; + DBusError *error; +} SendMessageData; + +static dbus_bool_t send_one_message (DBusConnection *connection, void *data) { - /* Only send messages to registered connections */ - if (bus_connection_get_name (connection) == NULL) - return; + SendMessageData *d = data; - BUS_HANDLE_OOM (dbus_connection_send_message (connection, data, NULL, NULL)); + if (!bus_connection_is_active (connection)) + return TRUE; + + if (!bus_transaction_send_message (d->transaction, + connection, + d->message)) + { + BUS_SET_OOM (d->error); + return FALSE; + } + + return TRUE; } -void -bus_dispatch_broadcast_message (DBusMessage *message) +dbus_bool_t +bus_dispatch_broadcast_message (BusTransaction *transaction, + DBusMessage *message, + DBusError *error) { - _dbus_assert (dbus_message_get_sender (message) != NULL); - - bus_connection_foreach (send_one_message, message); + DBusError tmp_error; + SendMessageData d; + _dbus_assert (dbus_message_get_sender (message) != NULL); + + dbus_error_init (&tmp_error); + d.message = message; + d.transaction = transaction; + d.error = &tmp_error; + bus_connection_foreach (send_one_message, &d); + + if (dbus_error_is_set (&tmp_error)) + { + dbus_move_error (&tmp_error, error); + return FALSE; + } + else + return TRUE; +} + +static dbus_bool_t +send_service_nonexistent_error (BusTransaction *transaction, + DBusConnection *connection, + const char *service_name, + DBusMessage *in_reply_to, + DBusError *error) +{ + DBusMessage *error_reply; + DBusString error_message; + const char *error_str; + + /* Trying to send a message to a non-existant service, + * bounce back an error message. + */ + + if (!_dbus_string_init (&error_message, _DBUS_INT_MAX)) + { + BUS_SET_OOM (error); + return FALSE; + } + + if (!_dbus_string_append (&error_message, "Service \"") || + !_dbus_string_append (&error_message, service_name) || + !_dbus_string_append (&error_message, "does not exist")) + { + _dbus_string_free (&error_message); + BUS_SET_OOM (error); + return FALSE; + } + + _dbus_string_get_const_data (&error_message, &error_str); + error_reply = dbus_message_new_error_reply (in_reply_to, + DBUS_ERROR_SERVICE_DOES_NOT_EXIST, + error_str); + + _dbus_string_free (&error_message); + + if (error_reply == NULL) + { + BUS_SET_OOM (error); + return FALSE; + } + + if (!bus_transaction_send_message (transaction, connection, error_reply)) + { + dbus_message_unref (error_reply); + BUS_SET_OOM (error); + return FALSE; + } + + dbus_message_unref (error_reply); + + return TRUE; } static DBusHandlerResult @@ -56,76 +143,164 @@ bus_dispatch_message_handler (DBusMessageHandler *handler, void *user_data) { const char *sender, *service_name, *message_name; + DBusError error; + BusTransaction *transaction; + + transaction = NULL; + dbus_error_init (&error); - /* Assign a sender to the message */ - sender = bus_connection_get_name (connection); - BUS_HANDLE_OOM (dbus_message_set_sender (message, sender)); + /* If we can't even allocate an OOM error, we just go to sleep + * until we can. + */ + while (!bus_connection_preallocate_oom_error (connection)) + bus_wait_for_memory (); + + /* Ref connection in case we disconnect it at some point in here */ + dbus_connection_ref (connection); service_name = dbus_message_get_service (message); message_name = dbus_message_get_name (message); + + _dbus_assert (message_name != NULL); /* DBusMessageLoader is supposed to check this */ + + /* If service_name is NULL, this is a message to the bus daemon, not intended + * to actually go "on the bus"; e.g. a peer-to-peer ping. Handle these + * immediately, especially disconnection messages. + */ + if (service_name == NULL) + { + if (strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0) + bus_connection_disconnected (connection); + + /* DBusConnection also handles some of these automatically, we leave + * it to do so. + */ + goto out; + } + + _dbus_assert (service_name != NULL); /* this message is intended for bus routing */ - /* TODO: Crashes if service_name == NULL */ + /* Create our transaction */ + transaction = bus_transaction_new (); + if (transaction == NULL) + { + BUS_SET_OOM (&error); + goto out; + } - /* See if the message is to the driver */ - if (message_name && strcmp (message_name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0) + /* Assign a sender to the message */ + if (bus_connection_is_active (connection)) { - bus_connection_disconnect (connection); + sender = bus_connection_get_name (connection); + _dbus_assert (sender != NULL); + + if (!dbus_message_set_sender (message, sender)) + { + BUS_SET_OOM (&error); + goto out; + } } - else if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) + + if (strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */ { - bus_driver_handle_message (connection, message); + if (!bus_driver_handle_message (connection, transaction, message, &error)) + goto out; } - else if (sender == NULL) + else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */ { _dbus_verbose ("Received message from non-registered client. Disconnecting.\n"); dbus_connection_disconnect (connection); } - else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) + /* FIXME what if we un-special-case this service and just have a flag + * on services that all service owners will get messages to it, not just + * the primary owner. + */ + else if (strcmp (service_name, DBUS_SERVICE_BROADCAST) == 0) /* spam! */ { - bus_dispatch_broadcast_message (message); + if (!bus_dispatch_broadcast_message (transaction, message, &error)) + goto out; } - else + else /* route to named service */ { DBusString service_string; BusService *service; _dbus_string_init_const (&service_string, service_name); - service = bus_service_lookup (&service_string, FALSE); - - if (!service) - { - DBusMessage *error_reply; - DBusString error_message; - const char *error_str; - - /* Trying to send a message to a non-existant service, - bounce back an error message. */ - - BUS_HANDLE_OOM (_dbus_string_init (&error_message, _DBUS_INT_MAX)); - - BUS_HANDLE_OOM (_dbus_string_append (&error_message, "Service \"")); - BUS_HANDLE_OOM (_dbus_string_append (&error_message, service_name)); - BUS_HANDLE_OOM (_dbus_string_append (&error_message, "does not exist")); + service = bus_service_lookup (&service_string); - _dbus_string_get_const_data (&error_message, &error_str); - BUS_HANDLE_OOM (error_reply = dbus_message_new_error_reply (message, DBUS_ERROR_SERVICE_DOES_NOT_EXIST, - error_str)); - _dbus_string_free (&error_message); + if (service == NULL) + { + if (!send_service_nonexistent_error (transaction, connection, + service_name, + message, &error)) + goto out; + } + else + { + _dbus_assert (bus_service_get_primary_owner (service) != NULL); + + /* Dispatch the message */ + if (!bus_transaction_send_message (transaction, + bus_service_get_primary_owner (service), + message)) + { + BUS_SET_OOM (&error); + goto out; + } + } + } + + out: + if (dbus_error_is_set (&error)) + { + if (!dbus_connection_get_is_connected (connection)) + { + /* If we disconnected it, we won't bother to send it any error + * messages. + */ + } + else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) + { + bus_connection_send_oom_error (connection, message); - /* Dispatch the message */ - BUS_HANDLE_OOM (dbus_connection_send_message (connection, error_reply, NULL, NULL)); - dbus_message_unref (error_reply); - } + /* cancel transaction due to OOM */ + if (transaction != NULL) + { + bus_transaction_cancel_and_free (transaction); + transaction = NULL; + } + } else - { - _dbus_assert (bus_service_get_primary_owner (service) != NULL); + { + /* Try to send the real error, if no mem to do that, send + * the OOM error + */ + _dbus_assert (transaction != NULL); + + if (!bus_transaction_send_error_reply (transaction, connection, + &error, message)) + { + bus_connection_send_oom_error (connection, message); + + /* cancel transaction due to OOM */ + if (transaction != NULL) + { + bus_transaction_cancel_and_free (transaction); + transaction = NULL; + } + } + } - /* Dispatch the message */ - BUS_HANDLE_OOM (dbus_connection_send_message (bus_service_get_primary_owner (service), - message, NULL, NULL)); - } + dbus_error_free (&error); } + if (transaction != NULL) + { + bus_transaction_execute_and_free (transaction); + } + + dbus_connection_unref (connection); + return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS; } diff --git a/bus/dispatch.h b/bus/dispatch.h index 2fe3479c..c24170dc 100644 --- a/bus/dispatch.h +++ b/bus/dispatch.h @@ -25,10 +25,12 @@ #define BUS_DISPATCH_H #include <dbus/dbus.h> +#include "connection.h" dbus_bool_t bus_dispatch_add_connection (DBusConnection *connection); void bus_dispatch_remove_connection (DBusConnection *connection); -void bus_dispatch_broadcast_message (DBusMessage *message); - +dbus_bool_t bus_dispatch_broadcast_message (BusTransaction *transaction, + DBusMessage *message, + DBusError *error); #endif /* BUS_DISPATCH_H */ diff --git a/bus/driver.c b/bus/driver.c index 80478e45..46985594 100644 --- a/bus/driver.c +++ b/bus/driver.c @@ -31,79 +31,171 @@ #include <dbus/dbus-internals.h> #include <string.h> -static void bus_driver_send_welcome_message (DBusConnection *connection, - DBusMessage *hello_message); - -void -bus_driver_send_service_deleted (const char *service_name) +static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection, + DBusMessage *hello_message, + BusTransaction *transaction, + DBusError *error); + +dbus_bool_t +bus_driver_send_service_deleted (const char *service_name, + BusTransaction *transaction, + DBusError *error) { DBusMessage *message; - + dbus_bool_t retval; + _dbus_verbose ("sending service deleted: %s\n", service_name); - BUS_HANDLE_OOM (message = dbus_message_new (DBUS_SERVICE_BROADCAST, - DBUS_MESSAGE_SERVICE_DELETED)); + message = dbus_message_new (DBUS_SERVICE_BROADCAST, + DBUS_MESSAGE_SERVICE_DELETED); + if (message == NULL) + { + BUS_SET_OOM (error); + return FALSE; + } - BUS_HANDLE_OOM (dbus_message_set_sender (message, DBUS_SERVICE_DBUS)); + if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) || + !dbus_message_append_args (message, + DBUS_TYPE_STRING, service_name, + 0)) + { + dbus_message_unref (message); + BUS_SET_OOM (error); + return FALSE; + } - BUS_HANDLE_OOM (dbus_message_append_args (message, - DBUS_TYPE_STRING, service_name, - 0)); - bus_dispatch_broadcast_message (message); - dbus_message_unref (message); + retval = bus_dispatch_broadcast_message (transaction, message, error); + dbus_message_unref (message); + + return retval; } -void -bus_driver_send_service_created (const char *service_name) +dbus_bool_t +bus_driver_send_service_created (const char *service_name, + BusTransaction *transaction, + DBusError *error) { DBusMessage *message; - - BUS_HANDLE_OOM (message = dbus_message_new (DBUS_SERVICE_BROADCAST, - DBUS_MESSAGE_SERVICE_CREATED)); + dbus_bool_t retval; + + message = dbus_message_new (DBUS_SERVICE_BROADCAST, + DBUS_MESSAGE_SERVICE_CREATED); + if (message == NULL) + { + BUS_SET_OOM (error); + return FALSE; + } - BUS_HANDLE_OOM (dbus_message_set_sender (message, DBUS_SERVICE_DBUS)); + if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS)) + { + dbus_message_unref (message); + BUS_SET_OOM (error); + return FALSE; + } - BUS_HANDLE_OOM (dbus_message_append_args (message, - DBUS_TYPE_STRING, service_name, - 0)); - bus_dispatch_broadcast_message (message); + if (!dbus_message_append_args (message, + DBUS_TYPE_STRING, service_name, + 0)) + { + dbus_message_unref (message); + BUS_SET_OOM (error); + return FALSE; + } + + retval = bus_dispatch_broadcast_message (transaction, message, error); dbus_message_unref (message); + + return retval; } -void +dbus_bool_t bus_driver_send_service_lost (DBusConnection *connection, - const char *service_name) + const char *service_name, + BusTransaction *transaction, + DBusError *error) { DBusMessage *message; - BUS_HANDLE_OOM (message = dbus_message_new (DBUS_SERVICE_BROADCAST, - DBUS_MESSAGE_SERVICE_LOST)); + message = dbus_message_new (bus_connection_get_name (connection), + DBUS_MESSAGE_SERVICE_LOST); + if (message == NULL) + { + BUS_SET_OOM (error); + return FALSE; + } - BUS_HANDLE_OOM (dbus_message_set_sender (message, DBUS_SERVICE_DBUS)); - BUS_HANDLE_OOM (dbus_message_append_args (message, - DBUS_TYPE_STRING, service_name, - 0)); - BUS_HANDLE_OOM (dbus_connection_send_message (connection, message, NULL, NULL)); + if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS)) + { + dbus_message_unref (message); + BUS_SET_OOM (error); + return FALSE; + } - dbus_message_unref (message); + if (!dbus_message_append_args (message, + DBUS_TYPE_STRING, service_name, + 0)) + { + dbus_message_unref (message); + BUS_SET_OOM (error); + return FALSE; + } + + if (!bus_transaction_send_message (transaction, connection, message)) + { + dbus_message_unref (message); + BUS_SET_OOM (error); + return FALSE; + } + else + { + dbus_message_unref (message); + return TRUE; + } } -void +dbus_bool_t bus_driver_send_service_acquired (DBusConnection *connection, - const char *service_name) + const char *service_name, + BusTransaction *transaction, + DBusError *error) { DBusMessage *message; - BUS_HANDLE_OOM (message = dbus_message_new (DBUS_SERVICE_BROADCAST, - DBUS_MESSAGE_SERVICE_ACQUIRED)); + message = dbus_message_new (bus_connection_get_name (connection), + DBUS_MESSAGE_SERVICE_ACQUIRED); + if (message == NULL) + { + BUS_SET_OOM (error); + return FALSE; + } - BUS_HANDLE_OOM (dbus_message_set_sender (message, DBUS_SERVICE_DBUS)); - BUS_HANDLE_OOM (dbus_message_append_args (message, - DBUS_TYPE_STRING, service_name, - 0)); - BUS_HANDLE_OOM (dbus_connection_send_message (connection, message, NULL, NULL)); + if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS)) + { + dbus_message_unref (message); + BUS_SET_OOM (error); + return FALSE; + } - dbus_message_unref (message); + if (!dbus_message_append_args (message, + DBUS_TYPE_STRING, service_name, + 0)) + { + dbus_message_unref (message); + BUS_SET_OOM (error); + return FALSE; + } + + if (!bus_transaction_send_message (transaction, connection, message)) + { + dbus_message_unref (message); + BUS_SET_OOM (error); + return FALSE; + } + else + { + dbus_message_unref (message); + return TRUE; + } } static dbus_bool_t @@ -154,7 +246,7 @@ create_unique_client_name (DBusString *str) next_minor_number += 1; /* Check if a client with the name exists */ - if (bus_service_lookup (str, FALSE) == NULL) + if (bus_service_lookup (str) == NULL) break; /* drop the number again, try the next one. */ @@ -164,35 +256,65 @@ create_unique_client_name (DBusString *str) return TRUE; } -static void +static dbus_bool_t bus_driver_handle_hello (DBusConnection *connection, - DBusMessage *message) + BusTransaction *transaction, + DBusMessage *message, + DBusError *error) { DBusString unique_name; BusService *service; + dbus_bool_t retval; + + if (!_dbus_string_init (&unique_name, _DBUS_INT_MAX)) + { + BUS_SET_OOM (error); + return FALSE; + } - BUS_HANDLE_OOM (_dbus_string_init (&unique_name, _DBUS_INT_MAX)); - BUS_HANDLE_OOM (create_unique_client_name (&unique_name)); + retval = FALSE; + + if (!create_unique_client_name (&unique_name)) + { + BUS_SET_OOM (error); + goto out_0; + } - BUS_HANDLE_OOM (bus_connection_set_name (connection, &unique_name)); - BUS_HANDLE_OOM (dbus_message_set_sender (message, - bus_connection_get_name (connection))); + if (!bus_connection_set_name (connection, &unique_name)) + { + BUS_SET_OOM (error); + goto out_0; + } + + if (!dbus_message_set_sender (message, + bus_connection_get_name (connection))) + { + BUS_SET_OOM (error); + goto out_0; + } - BUS_HANDLE_OOM (bus_driver_send_welcome_message (connection, message)); + if (!bus_driver_send_welcome_message (connection, message, transaction, error)) + goto out_0; /* Create the service */ - BUS_HANDLE_OOM (service = bus_service_lookup (&unique_name, TRUE)); - bus_service_set_prohibit_replacement (service, TRUE); + service = bus_service_ensure (&unique_name, connection, transaction, error); + if (service == NULL) + goto out_0; - /* Add the connection as the owner */ - BUS_HANDLE_OOM (bus_service_add_owner (service, connection)); + bus_service_set_prohibit_replacement (service, TRUE); + retval = TRUE; + + out_0: _dbus_string_free (&unique_name); + return retval; } -static void +static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection, - DBusMessage *hello_message) + DBusMessage *hello_message, + BusTransaction *transaction, + DBusError *error) { DBusMessage *welcome; const char *name; @@ -200,209 +322,330 @@ bus_driver_send_welcome_message (DBusConnection *connection, name = bus_connection_get_name (connection); _dbus_assert (name != NULL); - BUS_HANDLE_OOM (welcome = dbus_message_new_reply (hello_message)); - - BUS_HANDLE_OOM (dbus_message_set_sender (welcome, DBUS_SERVICE_DBUS)); - - BUS_HANDLE_OOM (dbus_message_append_args (welcome, - DBUS_TYPE_STRING, name, - NULL)); + welcome = dbus_message_new_reply (hello_message); + if (welcome == NULL) + { + BUS_SET_OOM (error); + return FALSE; + } - BUS_HANDLE_OOM (dbus_connection_send_message (connection, welcome, NULL, NULL)); + if (!dbus_message_set_sender (welcome, DBUS_SERVICE_DBUS)) + { + dbus_message_unref (welcome); + BUS_SET_OOM (error); + return FALSE; + } - dbus_message_unref (welcome); + if (!dbus_message_append_args (welcome, + DBUS_TYPE_STRING, name, + NULL)) + { + dbus_message_unref (welcome); + BUS_SET_OOM (error); + return FALSE; + } + + if (!bus_transaction_send_message (transaction, connection, welcome)) + { + dbus_message_unref (welcome); + BUS_SET_OOM (error); + return FALSE; + } + else + { + dbus_message_unref (welcome); + return TRUE; + } } -static void +static dbus_bool_t bus_driver_handle_list_services (DBusConnection *connection, - DBusMessage *message) + BusTransaction *transaction, + DBusMessage *message, + DBusError *error) { DBusMessage *reply; - int len, i; + int len; char **services; - BUS_HANDLE_OOM (reply = dbus_message_new_reply (message)); - - BUS_HANDLE_OOM (services = bus_services_list (&len)); - - BUS_HANDLE_OOM (dbus_message_append_args (reply, - DBUS_TYPE_STRING_ARRAY, services, len, - 0)); - - BUS_HANDLE_OOM (dbus_connection_send_message (connection, reply, NULL, NULL)); + reply = dbus_message_new_reply (message); + if (reply == NULL) + { + BUS_SET_OOM (error); + return FALSE; + } - dbus_message_unref (reply); + services = bus_services_list (&len); + if (services == NULL) + { + dbus_message_unref (reply); + BUS_SET_OOM (error); + return FALSE; + } + + if (!dbus_message_append_args (reply, + DBUS_TYPE_STRING_ARRAY, services, len, + 0)) + { + dbus_free_string_array (services); + dbus_message_unref (reply); + BUS_SET_OOM (error); + return FALSE; + } - if (services != NULL) + dbus_free_string_array (services); + + if (!bus_transaction_send_message (transaction, connection, reply)) { - for (i = 0; i < len; i++) - dbus_free (services[i]); - dbus_free (services); + dbus_message_unref (reply); + BUS_SET_OOM (error); + return FALSE; + } + else + { + dbus_message_unref (reply); + return TRUE; } } -static void +static dbus_bool_t bus_driver_handle_acquire_service (DBusConnection *connection, - DBusMessage *message) + BusTransaction *transaction, + DBusMessage *message, + DBusError *error) { DBusMessage *reply; - DBusResultCode result; DBusString service_name; BusService *service; char *name; int service_reply; int flags; + dbus_bool_t retval; + DBusConnection *old_owner; + DBusConnection *current_owner; - BUS_HANDLE_OOM ((result = dbus_message_get_args (message, - DBUS_TYPE_STRING, &name, - DBUS_TYPE_UINT32, &flags, - 0)) != DBUS_RESULT_NO_MEMORY); + if (!dbus_message_get_args (message, + error, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_UINT32, &flags, + 0)) + return FALSE; - if (result != DBUS_RESULT_SUCCESS) - { - dbus_free (name); - dbus_connection_disconnect (connection); - return; - } + _dbus_verbose ("Trying to own service %s with flags 0x%x\n", name, flags); - _dbus_verbose ("Trying to own service %s with flags %d\n", name, flags); + retval = FALSE; + reply = NULL; _dbus_string_init_const (&service_name, name); - service = bus_service_lookup (&service_name, TRUE); + + service = bus_service_lookup (&service_name); - BUS_HANDLE_OOM ((reply = dbus_message_new_reply (message))); + if (service != NULL) + old_owner = bus_service_get_primary_owner (service); + else + old_owner = NULL; - /* - * Check if the service already has an owner - */ - if (bus_service_get_primary_owner (service) != NULL) - { - if (bus_service_has_owner (service, connection)) - service_reply = DBUS_SERVICE_REPLY_ALREADY_OWNER; - else if (!(flags & DBUS_SERVICE_FLAG_REPLACE_EXISTING)) - service_reply = DBUS_SERVICE_REPLY_SERVICE_EXISTS; - else - { - if (bus_service_get_prohibit_replacement (service)) - { - - /* Queue the connection */ - BUS_HANDLE_OOM (bus_service_add_owner (service, connection)); - - service_reply = DBUS_SERVICE_REPLY_IN_QUEUE; - } - else - { - DBusConnection *owner; - - /* We can replace the primary owner */ - owner = bus_service_get_primary_owner (service); - - /* We enqueue the new owner and remove the first one because - * that will cause ServiceAcquired and ServiceLost messages to - * be sent. - */ - BUS_HANDLE_OOM (bus_service_add_owner (service, connection)); - bus_service_remove_owner (service, owner); - _dbus_assert (connection == bus_service_get_primary_owner (service)); - service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER; - } - } + reply = dbus_message_new_reply (message); + if (reply == NULL) + { + BUS_SET_OOM (error); + goto out; } - else + + if (service == NULL) { + service = bus_service_ensure (&service_name, connection, transaction, error); + if (service == NULL) + goto out; + } + + current_owner = bus_service_get_primary_owner (service); + + if (old_owner == NULL) + { + _dbus_assert (current_owner == connection); + bus_service_set_prohibit_replacement (service, - (flags & DBUS_SERVICE_FLAG_PROHIBIT_REPLACEMENT)); + (flags & DBUS_SERVICE_FLAG_PROHIBIT_REPLACEMENT)); + + service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER; + } + else if (old_owner == connection) + service_reply = DBUS_SERVICE_REPLY_ALREADY_OWNER; + else if (!((flags & DBUS_SERVICE_FLAG_REPLACE_EXISTING))) + service_reply = DBUS_SERVICE_REPLY_SERVICE_EXISTS; + else if (bus_service_get_prohibit_replacement (service)) + { + /* Queue the connection */ + if (!bus_service_add_owner (service, connection, + transaction, error)) + goto out; - /* Broadcast service created message */ - bus_driver_send_service_created (bus_service_get_name (service)); + service_reply = DBUS_SERVICE_REPLY_IN_QUEUE; + } + else + { + /* Replace the current owner */ + + /* We enqueue the new owner and remove the first one because + * that will cause ServiceAcquired and ServiceLost messages to + * be sent. + */ - BUS_HANDLE_OOM (bus_service_add_owner (service, connection)); - + /* FIXME this is broken, if the remove_owner fails + * we don't undo the add_owner + * (easiest fix is probably to move all this to + * services.c and have a single routine for it) + */ + + if (!bus_service_add_owner (service, connection, + transaction, error)) + goto out; + + if (!bus_service_remove_owner (service, old_owner, + transaction, error)) + goto out; + + _dbus_assert (connection == bus_service_get_primary_owner (service)); service_reply = DBUS_SERVICE_REPLY_PRIMARY_OWNER; } - BUS_HANDLE_OOM (dbus_message_append_args (reply, DBUS_TYPE_UINT32, service_reply, 0)); + if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, service_reply, 0)) + { + BUS_SET_OOM (error); + goto out; + } - /* Send service reply */ - BUS_HANDLE_OOM (dbus_connection_send_message (connection, reply, NULL, NULL)); + if (!bus_transaction_send_message (transaction, connection, reply)) + { + BUS_SET_OOM (error); + goto out; + } + + retval = TRUE; + + out: dbus_free (name); - dbus_message_unref (reply); -} + if (reply) + dbus_message_unref (reply); + return retval; +} -static void +static dbus_bool_t bus_driver_handle_service_exists (DBusConnection *connection, - DBusMessage *message) + BusTransaction *transaction, + DBusMessage *message, + DBusError *error) { DBusMessage *reply; - DBusResultCode result; DBusString service_name; BusService *service; char *name; + dbus_bool_t retval; - BUS_HANDLE_OOM ((result = dbus_message_get_args (message, - DBUS_TYPE_STRING, &name, - 0)) != DBUS_RESULT_NO_MEMORY); - if (result != DBUS_RESULT_SUCCESS) - { - dbus_free (name); - dbus_connection_disconnect (connection); - return; - } + if (!dbus_message_get_args (message, error, + DBUS_TYPE_STRING, &name, + 0)) + return FALSE; + retval = FALSE; + _dbus_string_init_const (&service_name, name); - service = bus_service_lookup (&service_name, FALSE); + service = bus_service_lookup (&service_name); - BUS_HANDLE_OOM ((reply = dbus_message_new_reply (message))); - BUS_HANDLE_OOM (dbus_message_set_sender (reply, DBUS_SERVICE_DBUS)); - - BUS_HANDLE_OOM (dbus_message_append_args (reply, - DBUS_TYPE_UINT32, service != NULL, - 0)); - BUS_HANDLE_OOM (dbus_connection_send_message (connection, reply, NULL, NULL)); - dbus_message_unref (reply); + reply = dbus_message_new_reply (message); + if (reply == NULL) + { + BUS_SET_OOM (error); + goto out; + } + + if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS)) + { + BUS_SET_OOM (error); + goto out; + } + + if (!dbus_message_append_args (reply, + DBUS_TYPE_UINT32, service != NULL, + 0)) + { + BUS_SET_OOM (error); + goto out; + } + + if (!bus_transaction_send_message (transaction, connection, reply)) + { + BUS_SET_OOM (error); + goto out; + } + + retval = TRUE; + + out: + if (reply) + dbus_message_unref (reply); dbus_free (name); + + return retval; } -static void +static dbus_bool_t bus_driver_handle_activate_service (DBusConnection *connection, - DBusMessage *message) + BusTransaction *transaction, + DBusMessage *message, + DBusError *error) { - DBusResultCode result; dbus_uint32_t flags; char *name; - DBusError error; + dbus_bool_t retval; - BUS_HANDLE_OOM ((result = dbus_message_get_args (message, - DBUS_TYPE_STRING, &name, - DBUS_TYPE_UINT32, &flags, - 0)) != DBUS_RESULT_NO_MEMORY); - if (result != DBUS_RESULT_SUCCESS) - { - dbus_free (name); - dbus_connection_disconnect (connection); - return; - } + if (!dbus_message_get_args (message, error, + DBUS_TYPE_STRING, &name, + DBUS_TYPE_UINT32, &flags, + 0)) + return FALSE; - if (!bus_activation_activate_service (name, &error)) - { - DBusMessage *error_reply; - - BUS_HANDLE_OOM (error_reply = dbus_message_new_error_reply (message, - error.name, error.message)); - dbus_error_free (&error); + retval = FALSE; - BUS_HANDLE_OOM (dbus_connection_send_message (connection, error_reply, NULL, NULL)); - dbus_message_unref (error_reply); - } + if (!bus_activation_activate_service (name, error)) + goto out; + + retval = TRUE; + + out: + dbus_free (name); + return retval; } -void +/* For speed it might be useful to sort this in order of + * frequency of use (but doesn't matter with only a few items + * anyhow) + */ +struct +{ + const char *name; + dbus_bool_t (* handler) (DBusConnection *connection, + BusTransaction *transaction, + DBusMessage *message, + DBusError *error); +} message_handlers[] = { + { DBUS_MESSAGE_ACQUIRE_SERVICE, bus_driver_handle_acquire_service }, + { DBUS_MESSAGE_ACTIVATE_SERVICE, bus_driver_handle_activate_service }, + { DBUS_MESSAGE_HELLO, bus_driver_handle_hello }, + { DBUS_MESSAGE_SERVICE_EXISTS, bus_driver_handle_service_exists }, + { DBUS_MESSAGE_LIST_SERVICES, bus_driver_handle_list_services } +}; + +dbus_bool_t bus_driver_handle_message (DBusConnection *connection, - DBusMessage *message) + BusTransaction *transaction, + DBusMessage *message, + DBusError *error) { const char *name, *sender; - + int i; + _dbus_verbose ("Driver got a message: %s\n", dbus_message_get_name (message)); @@ -411,26 +654,39 @@ bus_driver_handle_message (DBusConnection *connection, if (sender == NULL && (strcmp (name, DBUS_MESSAGE_HELLO) != 0)) { - _dbus_verbose ("Trying to send a message without being registered. Disconnecting.\n"); + dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, + "Client tried to send a message other than %s without being registered", + DBUS_MESSAGE_HELLO); + dbus_connection_disconnect (connection); - return; - } - - /* Now check names. */ - if (strcmp (name, DBUS_MESSAGE_HELLO) == 0) - bus_driver_handle_hello (connection, message); - else if (strcmp (name, DBUS_MESSAGE_LIST_SERVICES) == 0) - bus_driver_handle_list_services (connection, message); - else if (strcmp (name, DBUS_MESSAGE_ACQUIRE_SERVICE) == 0) - bus_driver_handle_acquire_service (connection, message); - else if (strcmp (name, DBUS_MESSAGE_SERVICE_EXISTS) == 0) - bus_driver_handle_service_exists (connection, message); - else if (strcmp (name, DBUS_MESSAGE_ACTIVATE_SERVICE) == 0) - bus_driver_handle_activate_service (connection, message); + return FALSE; + } + + i = 0; + while (i < _DBUS_N_ELEMENTS (message_handlers)) + { + if (strcmp (message_handlers[i].name, name) == 0) + { + if ((* message_handlers[i].handler) (connection, transaction, message, error)) + return TRUE; + else + return FALSE; + } + + ++i; + } + + dbus_set_error (error, DBUS_ERROR_UNKNOWN_MESSAGE, + "%s does not understand message %s", + DBUS_SERVICE_DBUS, name); + + return FALSE; } void bus_driver_remove_connection (DBusConnection *connection) { - /* Does nothing for now */ + /* FIXME Does nothing for now, should unregister the connection + * with the bus driver. + */ } diff --git a/bus/driver.h b/bus/driver.h index 3019fe43..ac80c15f 100644 --- a/bus/driver.h +++ b/bus/driver.h @@ -25,17 +25,26 @@ #define BUS_DRIVER_H #include <dbus/dbus.h> +#include "connection.h" -void bus_driver_remove_connection (DBusConnection *connection); -void bus_driver_handle_message (DBusConnection *connection, - DBusMessage *message); -void bus_driver_send_service_deleted (const char *service_name); -void bus_driver_send_service_lost (DBusConnection *connection, - const char *service_name); -void bus_driver_send_service_acquired (DBusConnection *connection, - const char *service_name); -void bus_driver_send_service_created (const char *service_name); - - +void bus_driver_remove_connection (DBusConnection *connection); +dbus_bool_t bus_driver_handle_message (DBusConnection *connection, + BusTransaction *transaction, + DBusMessage *message, + DBusError *error); +dbus_bool_t bus_driver_send_service_deleted (const char *service_name, + BusTransaction *transaction, + DBusError *error); +dbus_bool_t bus_driver_send_service_lost (DBusConnection *connection, + const char *service_name, + BusTransaction *transaction, + DBusError *error); +dbus_bool_t bus_driver_send_service_acquired (DBusConnection *connection, + const char *service_name, + BusTransaction *transaction, + DBusError *error); +dbus_bool_t bus_driver_send_service_created (const char *service_name, + BusTransaction *transaction, + DBusError *error); #endif /* BUS_DRIVER_H */ @@ -97,9 +97,18 @@ main (int argc, char **argv) } else { - char *paths[] = { argv[2], NULL }; + const char *paths[] = { argv[2], NULL }; + DBusError error; - bus_activation_init (argv[1], paths); + dbus_error_init (&error); + if (!bus_activation_init (argv[1], paths, + &error)) + { + _dbus_warn ("Could not initialize service activation: %s\n", + error.message); + dbus_error_free (&error); + return 1; + } } setup_server (server); diff --git a/bus/services.c b/bus/services.c index 497978d0..22302af4 100644 --- a/bus/services.c +++ b/bus/services.c @@ -21,31 +21,29 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ -#include "driver.h" -#include "services.h" -#include "connection.h" #include <dbus/dbus-hash.h> #include <dbus/dbus-list.h> #include <dbus/dbus-mempool.h> +#include "driver.h" +#include "services.h" +#include "connection.h" +#include "utils.h" + struct BusService { char *name; DBusList *owners; - - unsigned int prohibit_replacement:1; + + unsigned int prohibit_replacement : 1; }; static DBusHashTable *service_hash = NULL; static DBusMemPool *service_pool = NULL; -BusService* -bus_service_lookup (const DBusString *service_name, - dbus_bool_t create_if_not_found) +static dbus_bool_t +init_hash (void) { - const char *c_name; - BusService *service; - if (service_hash == NULL) { service_hash = _dbus_hash_table_new (DBUS_HASH_STRING, @@ -65,93 +63,182 @@ bus_service_lookup (const DBusString *service_name, _dbus_mem_pool_free (service_pool); service_pool = NULL; } - return NULL; + return FALSE; } } + return TRUE; +} + +BusService* +bus_service_lookup (const DBusString *service_name) +{ + const char *c_name; + BusService *service; + + if (!init_hash ()) + return NULL; _dbus_string_get_const_data (service_name, &c_name); service = _dbus_hash_table_lookup_string (service_hash, c_name); - if (service != NULL) - return service; - if (!create_if_not_found) + return service; +} + +BusService* +bus_service_ensure (const DBusString *service_name, + DBusConnection *owner_if_created, + BusTransaction *transaction, + DBusError *error) +{ + const char *c_name; + BusService *service; + + _dbus_assert (owner_if_created != NULL); + _dbus_assert (transaction != NULL); + + if (!init_hash ()) return NULL; + _dbus_string_get_const_data (service_name, &c_name); + + service = _dbus_hash_table_lookup_string (service_hash, + c_name); + if (service != NULL) + return service; + service = _dbus_mem_pool_alloc (service_pool); if (service == NULL) - return NULL; + { + BUS_SET_OOM (error); + return NULL; + } service->name = _dbus_strdup (c_name); if (service->name == NULL) { _dbus_mem_pool_dealloc (service_pool, service); + BUS_SET_OOM (error); return NULL; } + if (!bus_driver_send_service_created (service->name, transaction, error)) + { + dbus_free (service->name); + _dbus_mem_pool_dealloc (service_pool, service); + return NULL; + } + + if (!bus_service_add_owner (service, owner_if_created, + transaction, error)) + { + dbus_free (service->name); + _dbus_mem_pool_dealloc (service_pool, service); + return NULL; + } + if (!_dbus_hash_table_insert_string (service_hash, service->name, service)) { + _dbus_list_clear (&service->owners); dbus_free (service->name); _dbus_mem_pool_dealloc (service_pool, service); + BUS_SET_OOM (error); return NULL; } - - bus_driver_send_service_created (service->name); return service; } dbus_bool_t bus_service_add_owner (BusService *service, - DBusConnection *owner) + DBusConnection *owner, + BusTransaction *transaction, + DBusError *error) { + /* Send service acquired message first, OOM will result + * in cancelling the transaction + */ + if (service->owners == NULL) + { + if (!bus_driver_send_service_acquired (owner, service->name, transaction, error)) + return FALSE; + } + if (!_dbus_list_append (&service->owners, owner)) - return FALSE; + { + BUS_SET_OOM (error); + return FALSE; + } if (!bus_connection_add_owned_service (owner, service)) { _dbus_list_remove_last (&service->owners, owner); + BUS_SET_OOM (error); return FALSE; } - - /* Send service acquired message */ - if (bus_service_get_primary_owner (service) == owner) - bus_driver_send_service_acquired (owner, service->name); return TRUE; } -void +dbus_bool_t bus_service_remove_owner (BusService *service, - DBusConnection *owner) + DBusConnection *owner, + BusTransaction *transaction, + DBusError *error) { + /* We send out notifications before we do any work we + * might have to undo if the notification-sending failed + */ + /* Send service lost message */ if (bus_service_get_primary_owner (service) == owner) - bus_driver_send_service_lost (owner, service->name); + { + if (!bus_driver_send_service_lost (owner, service->name, + transaction, error)) + return FALSE; + } + + if (_dbus_list_length_is_one (&service->owners)) + { + /* We are the only owner - send service deleted */ + if (!bus_driver_send_service_deleted (service->name, + transaction, error)) + return FALSE; + } + else + { + DBusList *link; + link = _dbus_list_get_first (&service->owners); + link = _dbus_list_get_next_link (&service->owners, link); + + if (link != NULL) + { + /* This will be our new owner */ + if (!bus_driver_send_service_acquired (link->data, + service->name, + transaction, + error)) + return FALSE; + } + } _dbus_list_remove_last (&service->owners, owner); bus_connection_remove_owned_service (owner, service); if (service->owners == NULL) { - /* Delete service */ - bus_driver_send_service_deleted (service->name); - + /* Delete service (already sent message that it was deleted above) */ _dbus_hash_table_remove_string (service_hash, service->name); dbus_free (service->name); _dbus_mem_pool_dealloc (service_pool, service); } - else - { - /* Send service acquired to the new owner */ - bus_driver_send_service_acquired (bus_service_get_primary_owner (service), - service->name); - } + + return TRUE; } DBusConnection* @@ -192,7 +279,7 @@ bus_services_list (int *array_len) DBusHashIter iter; len = _dbus_hash_table_get_n_entries (service_hash); - retval = dbus_new (char *, len); + retval = dbus_new (char *, len + 1); if (retval == NULL) return NULL; @@ -210,6 +297,8 @@ bus_services_list (int *array_len) i++; } + retval[i] = NULL; + if (array_len) *array_len = len; @@ -227,8 +316,6 @@ void bus_service_set_prohibit_replacement (BusService *service, dbus_bool_t prohibit_replacement) { - _dbus_assert (service->owners == NULL); - service->prohibit_replacement = prohibit_replacement != FALSE; } diff --git a/bus/services.h b/bus/services.h index 3f6b31a9..97583582 100644 --- a/bus/services.h +++ b/bus/services.h @@ -26,6 +26,10 @@ #include <dbus/dbus.h> #include <dbus/dbus-string.h> +#include "connection.h" + +/* forward decl that probably shouldn't be in this file */ +typedef struct BusTransaction BusTransaction; /* Each service can have multiple owners; one owner is the "real * owner" and the others are queued up. For example, if I have @@ -38,12 +42,19 @@ typedef struct BusService BusService; typedef void (* BusServiceForeachFunction) (BusService *service, void *data); -BusService* bus_service_lookup (const DBusString *service_name, - dbus_bool_t create_if_not_found); +BusService* bus_service_lookup (const DBusString *service_name); +BusService* bus_service_ensure (const DBusString *service_name, + DBusConnection *owner_if_created, + BusTransaction *transaction, + DBusError *error); dbus_bool_t bus_service_add_owner (BusService *service, - DBusConnection *owner); -void bus_service_remove_owner (BusService *service, - DBusConnection *owner); + DBusConnection *owner, + BusTransaction *transaction, + DBusError *error); +dbus_bool_t bus_service_remove_owner (BusService *service, + DBusConnection *owner, + BusTransaction *transaction, + DBusError *error); dbus_bool_t bus_service_has_owner (BusService *service, DBusConnection *owner); DBusConnection* bus_service_get_primary_owner (BusService *service); diff --git a/bus/utils.c b/bus/utils.c index 8663b1e5..8b964cc4 100644 --- a/bus/utils.c +++ b/bus/utils.c @@ -25,6 +25,8 @@ #include "utils.h" #include <dbus/dbus-sysdeps.h> +const char bus_no_memory_message[] = "Memory allocation failure in message bus"; + void bus_wait_for_memory (void) { diff --git a/bus/utils.h b/bus/utils.h index 5cbe2849..41eb5557 100644 --- a/bus/utils.h +++ b/bus/utils.h @@ -25,8 +25,9 @@ #ifndef BUS_UTILS_H #define BUS_UTILS_H -#define BUS_HANDLE_OOM(stat) (stat) - void bus_wait_for_memory (void); +extern const char bus_no_memory_message[]; +#define BUS_SET_OOM(error) dbus_set_error ((error), DBUS_ERROR_NO_MEMORY, bus_no_memory_message) + #endif /* BUS_ACTIVATION_H */ |