1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* services.c Service management
4 * Copyright (C) 2003 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 1.2
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <dbus/dbus-hash.h>
25 #include <dbus/dbus-list.h>
26 #include <dbus/dbus-mempool.h>
30 #include "connection.h"
32 #include "activation.h"
39 BusRegistry *registry;
43 unsigned int prohibit_replacement : 1;
52 DBusHashTable *service_hash;
53 DBusMemPool *service_pool;
57 bus_registry_new (BusContext *context)
59 BusRegistry *registry;
61 registry = dbus_new0 (BusRegistry, 1);
65 registry->refcount = 1;
66 registry->context = context;
68 registry->service_hash = _dbus_hash_table_new (DBUS_HASH_STRING,
70 if (registry->service_hash == NULL)
73 registry->service_pool = _dbus_mem_pool_new (sizeof (BusService),
75 if (registry->service_pool == NULL)
81 bus_registry_unref (registry);
86 bus_registry_ref (BusRegistry *registry)
88 _dbus_assert (registry->refcount > 0);
89 registry->refcount += 1;
93 bus_registry_unref (BusRegistry *registry)
95 _dbus_assert (registry->refcount > 0);
96 registry->refcount -= 1;
98 if (registry->refcount == 0)
100 if (registry->service_hash)
101 _dbus_hash_table_unref (registry->service_hash);
102 if (registry->service_pool)
103 _dbus_mem_pool_free (registry->service_pool);
105 dbus_free (registry);
110 bus_registry_lookup (BusRegistry *registry,
111 const DBusString *service_name)
115 service = _dbus_hash_table_lookup_string (registry->service_hash,
116 _dbus_string_get_const_data (service_name));
122 bus_registry_ensure (BusRegistry *registry,
123 const DBusString *service_name,
124 DBusConnection *owner_if_created,
125 BusTransaction *transaction,
130 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
132 _dbus_assert (owner_if_created != NULL);
133 _dbus_assert (transaction != NULL);
135 service = _dbus_hash_table_lookup_string (registry->service_hash,
136 _dbus_string_get_const_data (service_name));
140 service = _dbus_mem_pool_alloc (registry->service_pool);
147 service->registry = registry;
148 service->refcount = 1;
150 if (!_dbus_string_copy_data (service_name, &service->name))
152 _dbus_mem_pool_dealloc (registry->service_pool, service);
157 if (!bus_driver_send_service_created (service->name, transaction, error))
159 bus_service_unref (service);
163 if (!bus_activation_service_created (bus_context_get_activation (registry->context),
164 service->name, transaction, error))
166 bus_service_unref (service);
170 if (!bus_service_add_owner (service, owner_if_created,
173 bus_service_unref (service);
177 if (!_dbus_hash_table_insert_string (registry->service_hash,
181 /* The add_owner gets reverted on transaction cancel */
190 bus_registry_foreach (BusRegistry *registry,
191 BusServiceForeachFunction function,
196 _dbus_hash_iter_init (registry->service_hash, &iter);
197 while (_dbus_hash_iter_next (&iter))
199 BusService *service = _dbus_hash_iter_get_value (&iter);
201 (* function) (service, data);
206 bus_registry_list_services (BusRegistry *registry,
214 len = _dbus_hash_table_get_n_entries (registry->service_hash);
215 retval = dbus_new (char *, len + 1);
220 _dbus_hash_iter_init (registry->service_hash, &iter);
222 while (_dbus_hash_iter_next (&iter))
224 BusService *service = _dbus_hash_iter_get_value (&iter);
226 retval[i] = _dbus_strdup (service->name);
227 if (retval[i] == NULL)
242 for (j = 0; j < i; j++)
243 dbus_free (retval[i]);
250 bus_registry_acquire_service (BusRegistry *registry,
251 DBusConnection *connection,
252 const DBusString *service_name,
254 dbus_uint32_t *result,
255 BusTransaction *transaction,
259 DBusConnection *old_owner;
260 DBusConnection *current_owner;
261 BusClientPolicy *policy;
266 if (_dbus_string_get_length (service_name) == 0)
268 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
269 "Zero-length service name is not allowed");
271 _dbus_verbose ("Attempt to acquire zero-length service name\n");
276 if (_dbus_string_get_byte (service_name, 0) == ':')
278 /* Not allowed; only base services can start with ':' */
279 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
280 "Cannot acquire a service starting with ':' such as \"%s\"",
281 _dbus_string_get_const_data (service_name));
283 _dbus_verbose ("Attempt to acquire invalid base service name \"%s\"",
284 _dbus_string_get_const_data (service_name));
289 policy = bus_connection_get_policy (connection);
290 _dbus_assert (policy != NULL);
292 if (!bus_client_policy_check_can_own (policy, connection,
295 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
296 "Connection \"%s\" is not allowed to own the service \"%s\" due "
297 "to security policies in the configuration file",
298 bus_connection_is_active (connection) ?
299 bus_connection_get_name (connection) :
304 if (bus_connection_get_n_services_owned (connection) >=
305 bus_context_get_max_services_per_connection (registry->context))
307 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
308 "Connection \"%s\" is not allowed to own more services "
309 "(increase limits in configuration file if required)",
310 bus_connection_is_active (connection) ?
311 bus_connection_get_name (connection) :
316 service = bus_registry_lookup (registry, service_name);
319 old_owner = bus_service_get_primary_owner (service);
325 service = bus_registry_ensure (registry,
326 service_name, connection, transaction, error);
331 current_owner = bus_service_get_primary_owner (service);
333 if (old_owner == NULL)
335 _dbus_assert (current_owner == connection);
337 bus_service_set_prohibit_replacement (service,
338 (flags & DBUS_SERVICE_FLAG_PROHIBIT_REPLACEMENT));
340 *result = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
342 else if (old_owner == connection)
343 *result = DBUS_SERVICE_REPLY_ALREADY_OWNER;
344 else if (!((flags & DBUS_SERVICE_FLAG_REPLACE_EXISTING)))
345 *result = DBUS_SERVICE_REPLY_SERVICE_EXISTS;
346 else if (bus_service_get_prohibit_replacement (service))
348 /* Queue the connection */
349 if (!bus_service_add_owner (service, connection,
353 *result = DBUS_SERVICE_REPLY_IN_QUEUE;
357 /* Replace the current owner */
359 /* We enqueue the new owner and remove the first one because
360 * that will cause ServiceAcquired and ServiceLost messages to
364 if (!bus_service_add_owner (service, connection,
368 if (!bus_service_remove_owner (service, old_owner,
372 _dbus_assert (connection == bus_service_get_primary_owner (service));
373 *result = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
383 bus_service_unlink_owner (BusService *service,
384 DBusConnection *owner)
386 _dbus_list_remove_last (&service->owners, owner);
387 bus_connection_remove_owned_service (owner, service);
391 bus_service_unlink (BusService *service)
393 _dbus_assert (service->owners == NULL);
395 /* the service may not be in the hash, if
396 * the failure causing transaction cancel
397 * was in the right place, but that's OK
399 _dbus_hash_table_remove_string (service->registry->service_hash,
402 bus_service_unref (service);
406 bus_service_relink (BusService *service,
407 DBusPreallocatedHash *preallocated)
409 _dbus_assert (service->owners == NULL);
410 _dbus_assert (preallocated != NULL);
412 _dbus_hash_table_insert_string_preallocated (service->registry->service_hash,
417 bus_service_ref (service);
421 * Data used to represent an ownership cancellation in
426 DBusConnection *connection; /**< the connection */
427 BusService *service; /**< service to cancel ownership of */
428 } OwnershipCancelData;
431 cancel_ownership (void *data)
433 OwnershipCancelData *d = data;
435 /* We don't need to send messages notifying of these
436 * changes, since we're reverting something that was
437 * cancelled (effectively never really happened)
439 bus_service_unlink_owner (d->service, d->connection);
441 if (d->service->owners == NULL)
442 bus_service_unlink (d->service);
446 free_ownership_cancel_data (void *data)
448 OwnershipCancelData *d = data;
450 dbus_connection_unref (d->connection);
451 bus_service_unref (d->service);
457 add_cancel_ownership_to_transaction (BusTransaction *transaction,
459 DBusConnection *connection)
461 OwnershipCancelData *d;
463 d = dbus_new (OwnershipCancelData, 1);
467 d->service = service;
468 d->connection = connection;
470 if (!bus_transaction_add_cancel_hook (transaction, cancel_ownership, d,
471 free_ownership_cancel_data))
477 bus_service_ref (d->service);
478 dbus_connection_ref (d->connection);
483 /* this function is self-cancelling if you cancel the transaction */
485 bus_service_add_owner (BusService *service,
486 DBusConnection *owner,
487 BusTransaction *transaction,
490 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
492 /* Send service acquired message first, OOM will result
493 * in cancelling the transaction
495 if (service->owners == NULL)
497 if (!bus_driver_send_service_acquired (owner, service->name, transaction, error))
501 if (!_dbus_list_append (&service->owners,
508 if (!bus_connection_add_owned_service (owner, service))
510 _dbus_list_remove_last (&service->owners, owner);
515 if (!add_cancel_ownership_to_transaction (transaction,
519 bus_service_unlink_owner (service, owner);
529 DBusConnection *connection;
531 DBusConnection *before_connection; /* restore to position before this connection in owners list */
532 DBusList *connection_link;
533 DBusList *service_link;
534 DBusPreallocatedHash *hash_entry;
535 } OwnershipRestoreData;
538 restore_ownership (void *data)
540 OwnershipRestoreData *d = data;
543 _dbus_assert (d->service_link != NULL);
544 _dbus_assert (d->connection_link != NULL);
546 if (d->service->owners == NULL)
548 _dbus_assert (d->hash_entry != NULL);
549 bus_service_relink (d->service, d->hash_entry);
553 _dbus_assert (d->hash_entry == NULL);
556 /* We don't need to send messages notifying of these
557 * changes, since we're reverting something that was
558 * cancelled (effectively never really happened)
560 link = _dbus_list_get_first_link (&d->service->owners);
563 if (link->data == d->before_connection)
566 link = _dbus_list_get_next_link (&d->service->owners, link);
569 _dbus_list_insert_before_link (&d->service->owners, link, d->connection_link);
571 /* Note that removing then restoring this changes the order in which
572 * ServiceDeleted messages are sent on destruction of the
573 * connection. This should be OK as the only guarantee there is
574 * that the base service is destroyed last, and we never even
575 * tentatively remove the base service.
577 bus_connection_add_owned_service_link (d->connection, d->service_link);
579 d->hash_entry = NULL;
580 d->service_link = NULL;
581 d->connection_link = NULL;
585 free_ownership_restore_data (void *data)
587 OwnershipRestoreData *d = data;
590 _dbus_list_free_link (d->service_link);
591 if (d->connection_link)
592 _dbus_list_free_link (d->connection_link);
594 _dbus_hash_table_free_preallocated_entry (d->service->registry->service_hash,
597 dbus_connection_unref (d->connection);
598 bus_service_unref (d->service);
604 add_restore_ownership_to_transaction (BusTransaction *transaction,
606 DBusConnection *connection)
608 OwnershipRestoreData *d;
611 d = dbus_new (OwnershipRestoreData, 1);
615 d->service = service;
616 d->connection = connection;
617 d->service_link = _dbus_list_alloc_link (service);
618 d->connection_link = _dbus_list_alloc_link (connection);
619 d->hash_entry = _dbus_hash_table_preallocate_entry (service->registry->service_hash);
621 bus_service_ref (d->service);
622 dbus_connection_ref (d->connection);
624 d->before_connection = NULL;
625 link = _dbus_list_get_first_link (&service->owners);
628 if (link->data == connection)
630 link = _dbus_list_get_next_link (&service->owners, link);
633 d->before_connection = link->data;
638 link = _dbus_list_get_next_link (&service->owners, link);
641 if (d->service_link == NULL ||
642 d->connection_link == NULL ||
643 d->hash_entry == NULL ||
644 !bus_transaction_add_cancel_hook (transaction, restore_ownership, d,
645 free_ownership_restore_data))
647 free_ownership_restore_data (d);
654 /* this function is self-cancelling if you cancel the transaction */
656 bus_service_remove_owner (BusService *service,
657 DBusConnection *owner,
658 BusTransaction *transaction,
661 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
663 /* We send out notifications before we do any work we
664 * might have to undo if the notification-sending failed
667 /* Send service lost message */
668 if (bus_service_get_primary_owner (service) == owner)
670 if (!bus_driver_send_service_lost (owner, service->name,
675 if (service->owners == NULL)
677 _dbus_assert_not_reached ("Tried to remove owner of a service that has no owners");
679 else if (_dbus_list_length_is_one (&service->owners))
681 if (!bus_driver_send_service_deleted (service->name,
688 link = _dbus_list_get_first (&service->owners);
689 _dbus_assert (link != NULL);
690 link = _dbus_list_get_next_link (&service->owners, link);
692 _dbus_assert (link != NULL);
694 /* This will be our new owner */
695 if (!bus_driver_send_service_acquired (link->data,
702 if (!add_restore_ownership_to_transaction (transaction, service, owner))
708 bus_service_unlink_owner (service, owner);
710 if (service->owners == NULL)
711 bus_service_unlink (service);
717 bus_service_ref (BusService *service)
719 _dbus_assert (service->refcount > 0);
721 service->refcount += 1;
725 bus_service_unref (BusService *service)
727 _dbus_assert (service->refcount > 0);
729 service->refcount -= 1;
731 if (service->refcount == 0)
733 _dbus_assert (service->owners == NULL);
735 dbus_free (service->name);
736 _dbus_mem_pool_dealloc (service->registry->service_pool, service);
741 bus_service_get_primary_owner (BusService *service)
743 return _dbus_list_get_first (&service->owners);
747 bus_service_get_name (BusService *service)
749 return service->name;
753 bus_service_set_prohibit_replacement (BusService *service,
754 dbus_bool_t prohibit_replacement)
756 service->prohibit_replacement = prohibit_replacement != FALSE;
760 bus_service_get_prohibit_replacement (BusService *service)
762 return service->prohibit_replacement;
766 bus_service_has_owner (BusService *service,
767 DBusConnection *owner)
771 link = _dbus_list_get_first_link (&service->owners);
775 if (link->data == owner)
778 link = _dbus_list_get_next_link (&service->owners, link);