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;
95 bus_registry_unref (BusRegistry *registry)
97 _dbus_assert (registry->refcount > 0);
98 registry->refcount -= 1;
100 if (registry->refcount == 0)
102 if (registry->service_hash)
103 _dbus_hash_table_unref (registry->service_hash);
104 if (registry->service_pool)
105 _dbus_mem_pool_free (registry->service_pool);
107 dbus_free (registry);
112 bus_registry_lookup (BusRegistry *registry,
113 const DBusString *service_name)
117 service = _dbus_hash_table_lookup_string (registry->service_hash,
118 _dbus_string_get_const_data (service_name));
124 bus_registry_ensure (BusRegistry *registry,
125 const DBusString *service_name,
126 DBusConnection *owner_if_created,
127 BusTransaction *transaction,
132 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
134 _dbus_assert (owner_if_created != NULL);
135 _dbus_assert (transaction != NULL);
137 service = _dbus_hash_table_lookup_string (registry->service_hash,
138 _dbus_string_get_const_data (service_name));
142 service = _dbus_mem_pool_alloc (registry->service_pool);
149 service->registry = registry;
150 service->refcount = 1;
152 if (!_dbus_string_copy_data (service_name, &service->name))
154 _dbus_mem_pool_dealloc (registry->service_pool, service);
159 if (!bus_driver_send_service_created (service->name, transaction, error))
161 bus_service_unref (service);
165 if (!bus_activation_service_created (bus_context_get_activation (registry->context),
166 service->name, transaction, error))
168 bus_service_unref (service);
172 if (!bus_service_add_owner (service, owner_if_created,
175 bus_service_unref (service);
179 if (!_dbus_hash_table_insert_string (registry->service_hash,
183 /* The add_owner gets reverted on transaction cancel */
192 bus_registry_foreach (BusRegistry *registry,
193 BusServiceForeachFunction function,
198 _dbus_hash_iter_init (registry->service_hash, &iter);
199 while (_dbus_hash_iter_next (&iter))
201 BusService *service = _dbus_hash_iter_get_value (&iter);
203 (* function) (service, data);
208 bus_registry_list_services (BusRegistry *registry,
216 len = _dbus_hash_table_get_n_entries (registry->service_hash);
217 retval = dbus_new (char *, len + 1);
222 _dbus_hash_iter_init (registry->service_hash, &iter);
224 while (_dbus_hash_iter_next (&iter))
226 BusService *service = _dbus_hash_iter_get_value (&iter);
228 retval[i] = _dbus_strdup (service->name);
229 if (retval[i] == NULL)
244 for (j = 0; j < i; j++)
245 dbus_free (retval[i]);
252 bus_registry_acquire_service (BusRegistry *registry,
253 DBusConnection *connection,
254 const DBusString *service_name,
256 dbus_uint32_t *result,
257 BusTransaction *transaction,
261 DBusConnection *old_owner;
262 DBusConnection *current_owner;
263 BusClientPolicy *policy;
268 if (_dbus_string_get_length (service_name) == 0)
270 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
271 "Zero-length service name is not allowed");
273 _dbus_verbose ("Attempt to acquire zero-length service name\n");
278 if (_dbus_string_get_byte (service_name, 0) == ':')
280 /* Not allowed; only base services can start with ':' */
281 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
282 "Cannot acquire a service starting with ':' such as \"%s\"",
283 _dbus_string_get_const_data (service_name));
285 _dbus_verbose ("Attempt to acquire invalid base service name \"%s\"",
286 _dbus_string_get_const_data (service_name));
291 policy = bus_connection_get_policy (connection);
292 _dbus_assert (policy != NULL);
294 if (!bus_client_policy_check_can_own (policy, connection,
297 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
298 "Connection \"%s\" is not allowed to own the service \"%s\" due "
299 "to security policies in the configuration file",
300 bus_connection_is_active (connection) ?
301 bus_connection_get_name (connection) :
306 if (bus_connection_get_n_services_owned (connection) >=
307 bus_context_get_max_services_per_connection (registry->context))
309 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
310 "Connection \"%s\" is not allowed to own more services "
311 "(increase limits in configuration file if required)",
312 bus_connection_is_active (connection) ?
313 bus_connection_get_name (connection) :
318 service = bus_registry_lookup (registry, service_name);
321 old_owner = bus_service_get_primary_owner (service);
327 service = bus_registry_ensure (registry,
328 service_name, connection, transaction, error);
333 current_owner = bus_service_get_primary_owner (service);
335 if (old_owner == NULL)
337 _dbus_assert (current_owner == connection);
339 bus_service_set_prohibit_replacement (service,
340 (flags & DBUS_SERVICE_FLAG_PROHIBIT_REPLACEMENT));
342 *result = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
344 else if (old_owner == connection)
345 *result = DBUS_SERVICE_REPLY_ALREADY_OWNER;
346 else if (!((flags & DBUS_SERVICE_FLAG_REPLACE_EXISTING)))
347 *result = DBUS_SERVICE_REPLY_SERVICE_EXISTS;
348 else if (bus_service_get_prohibit_replacement (service))
350 /* Queue the connection */
351 if (!bus_service_add_owner (service, connection,
355 *result = DBUS_SERVICE_REPLY_IN_QUEUE;
359 /* Replace the current owner */
361 /* We enqueue the new owner and remove the first one because
362 * that will cause ServiceAcquired and ServiceLost messages to
366 if (!bus_service_add_owner (service, connection,
370 if (!bus_service_remove_owner (service, old_owner,
374 _dbus_assert (connection == bus_service_get_primary_owner (service));
375 *result = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
385 bus_service_unlink_owner (BusService *service,
386 DBusConnection *owner)
388 _dbus_list_remove_last (&service->owners, owner);
389 bus_connection_remove_owned_service (owner, service);
393 bus_service_unlink (BusService *service)
395 _dbus_assert (service->owners == NULL);
397 /* the service may not be in the hash, if
398 * the failure causing transaction cancel
399 * was in the right place, but that's OK
401 _dbus_hash_table_remove_string (service->registry->service_hash,
404 bus_service_unref (service);
408 bus_service_relink (BusService *service,
409 DBusPreallocatedHash *preallocated)
411 _dbus_assert (service->owners == NULL);
412 _dbus_assert (preallocated != NULL);
414 _dbus_hash_table_insert_string_preallocated (service->registry->service_hash,
419 bus_service_ref (service);
423 * Data used to represent an ownership cancellation in
428 DBusConnection *connection; /**< the connection */
429 BusService *service; /**< service to cancel ownership of */
430 } OwnershipCancelData;
433 cancel_ownership (void *data)
435 OwnershipCancelData *d = data;
437 /* We don't need to send messages notifying of these
438 * changes, since we're reverting something that was
439 * cancelled (effectively never really happened)
441 bus_service_unlink_owner (d->service, d->connection);
443 if (d->service->owners == NULL)
444 bus_service_unlink (d->service);
448 free_ownership_cancel_data (void *data)
450 OwnershipCancelData *d = data;
452 dbus_connection_unref (d->connection);
453 bus_service_unref (d->service);
459 add_cancel_ownership_to_transaction (BusTransaction *transaction,
461 DBusConnection *connection)
463 OwnershipCancelData *d;
465 d = dbus_new (OwnershipCancelData, 1);
469 d->service = service;
470 d->connection = connection;
472 if (!bus_transaction_add_cancel_hook (transaction, cancel_ownership, d,
473 free_ownership_cancel_data))
479 bus_service_ref (d->service);
480 dbus_connection_ref (d->connection);
485 /* this function is self-cancelling if you cancel the transaction */
487 bus_service_add_owner (BusService *service,
488 DBusConnection *owner,
489 BusTransaction *transaction,
492 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
494 /* Send service acquired message first, OOM will result
495 * in cancelling the transaction
497 if (service->owners == NULL)
499 if (!bus_driver_send_service_acquired (owner, service->name, transaction, error))
503 if (!_dbus_list_append (&service->owners,
510 if (!bus_connection_add_owned_service (owner, service))
512 _dbus_list_remove_last (&service->owners, owner);
517 if (!add_cancel_ownership_to_transaction (transaction,
521 bus_service_unlink_owner (service, owner);
531 DBusConnection *connection;
533 DBusConnection *before_connection; /* restore to position before this connection in owners list */
534 DBusList *connection_link;
535 DBusList *service_link;
536 DBusPreallocatedHash *hash_entry;
537 } OwnershipRestoreData;
540 restore_ownership (void *data)
542 OwnershipRestoreData *d = data;
545 _dbus_assert (d->service_link != NULL);
546 _dbus_assert (d->connection_link != NULL);
548 if (d->service->owners == NULL)
550 _dbus_assert (d->hash_entry != NULL);
551 bus_service_relink (d->service, d->hash_entry);
555 _dbus_assert (d->hash_entry == NULL);
558 /* We don't need to send messages notifying of these
559 * changes, since we're reverting something that was
560 * cancelled (effectively never really happened)
562 link = _dbus_list_get_first_link (&d->service->owners);
565 if (link->data == d->before_connection)
568 link = _dbus_list_get_next_link (&d->service->owners, link);
571 _dbus_list_insert_before_link (&d->service->owners, link, d->connection_link);
573 /* Note that removing then restoring this changes the order in which
574 * ServiceDeleted messages are sent on destruction of the
575 * connection. This should be OK as the only guarantee there is
576 * that the base service is destroyed last, and we never even
577 * tentatively remove the base service.
579 bus_connection_add_owned_service_link (d->connection, d->service_link);
581 d->hash_entry = NULL;
582 d->service_link = NULL;
583 d->connection_link = NULL;
587 free_ownership_restore_data (void *data)
589 OwnershipRestoreData *d = data;
592 _dbus_list_free_link (d->service_link);
593 if (d->connection_link)
594 _dbus_list_free_link (d->connection_link);
596 _dbus_hash_table_free_preallocated_entry (d->service->registry->service_hash,
599 dbus_connection_unref (d->connection);
600 bus_service_unref (d->service);
606 add_restore_ownership_to_transaction (BusTransaction *transaction,
608 DBusConnection *connection)
610 OwnershipRestoreData *d;
613 d = dbus_new (OwnershipRestoreData, 1);
617 d->service = service;
618 d->connection = connection;
619 d->service_link = _dbus_list_alloc_link (service);
620 d->connection_link = _dbus_list_alloc_link (connection);
621 d->hash_entry = _dbus_hash_table_preallocate_entry (service->registry->service_hash);
623 bus_service_ref (d->service);
624 dbus_connection_ref (d->connection);
626 d->before_connection = NULL;
627 link = _dbus_list_get_first_link (&service->owners);
630 if (link->data == connection)
632 link = _dbus_list_get_next_link (&service->owners, link);
635 d->before_connection = link->data;
640 link = _dbus_list_get_next_link (&service->owners, link);
643 if (d->service_link == NULL ||
644 d->connection_link == NULL ||
645 d->hash_entry == NULL ||
646 !bus_transaction_add_cancel_hook (transaction, restore_ownership, d,
647 free_ownership_restore_data))
649 free_ownership_restore_data (d);
656 /* this function is self-cancelling if you cancel the transaction */
658 bus_service_remove_owner (BusService *service,
659 DBusConnection *owner,
660 BusTransaction *transaction,
663 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
665 /* We send out notifications before we do any work we
666 * might have to undo if the notification-sending failed
669 /* Send service lost message */
670 if (bus_service_get_primary_owner (service) == owner)
672 if (!bus_driver_send_service_lost (owner, service->name,
677 if (service->owners == NULL)
679 _dbus_assert_not_reached ("Tried to remove owner of a service that has no owners");
681 else if (_dbus_list_length_is_one (&service->owners))
683 if (!bus_driver_send_service_deleted (service->name,
690 link = _dbus_list_get_first (&service->owners);
691 _dbus_assert (link != NULL);
692 link = _dbus_list_get_next_link (&service->owners, link);
694 _dbus_assert (link != NULL);
696 /* This will be our new owner */
697 if (!bus_driver_send_service_acquired (link->data,
704 if (!add_restore_ownership_to_transaction (transaction, service, owner))
710 bus_service_unlink_owner (service, owner);
712 if (service->owners == NULL)
713 bus_service_unlink (service);
719 bus_service_ref (BusService *service)
721 _dbus_assert (service->refcount > 0);
723 service->refcount += 1;
729 bus_service_unref (BusService *service)
731 _dbus_assert (service->refcount > 0);
733 service->refcount -= 1;
735 if (service->refcount == 0)
737 _dbus_assert (service->owners == NULL);
739 dbus_free (service->name);
740 _dbus_mem_pool_dealloc (service->registry->service_pool, service);
745 bus_service_get_primary_owner (BusService *service)
747 return _dbus_list_get_first (&service->owners);
751 bus_service_get_name (BusService *service)
753 return service->name;
757 bus_service_set_prohibit_replacement (BusService *service,
758 dbus_bool_t prohibit_replacement)
760 service->prohibit_replacement = prohibit_replacement != FALSE;
764 bus_service_get_prohibit_replacement (BusService *service)
766 return service->prohibit_replacement;
770 bus_service_has_owner (BusService *service,
771 DBusConnection *owner)
775 link = _dbus_list_get_first_link (&service->owners);
779 if (link->data == owner)
782 link = _dbus_list_get_next_link (&service->owners, link);