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);
422 DBusConnection *connection;
424 } OwnershipCancelData;
427 cancel_ownership (void *data)
429 OwnershipCancelData *d = data;
431 /* We don't need to send messages notifying of these
432 * changes, since we're reverting something that was
433 * cancelled (effectively never really happened)
435 bus_service_unlink_owner (d->service, d->connection);
437 if (d->service->owners == NULL)
438 bus_service_unlink (d->service);
442 free_ownership_cancel_data (void *data)
444 OwnershipCancelData *d = data;
446 dbus_connection_unref (d->connection);
447 bus_service_unref (d->service);
453 add_cancel_ownership_to_transaction (BusTransaction *transaction,
455 DBusConnection *connection)
457 OwnershipCancelData *d;
459 d = dbus_new (OwnershipCancelData, 1);
463 d->service = service;
464 d->connection = connection;
466 if (!bus_transaction_add_cancel_hook (transaction, cancel_ownership, d,
467 free_ownership_cancel_data))
473 bus_service_ref (d->service);
474 dbus_connection_ref (d->connection);
479 /* this function is self-cancelling if you cancel the transaction */
481 bus_service_add_owner (BusService *service,
482 DBusConnection *owner,
483 BusTransaction *transaction,
486 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
488 /* Send service acquired message first, OOM will result
489 * in cancelling the transaction
491 if (service->owners == NULL)
493 if (!bus_driver_send_service_acquired (owner, service->name, transaction, error))
497 if (!_dbus_list_append (&service->owners,
504 if (!bus_connection_add_owned_service (owner, service))
506 _dbus_list_remove_last (&service->owners, owner);
511 if (!add_cancel_ownership_to_transaction (transaction,
515 bus_service_unlink_owner (service, owner);
525 DBusConnection *connection;
527 DBusConnection *before_connection; /* restore to position before this connection in owners list */
528 DBusList *connection_link;
529 DBusList *service_link;
530 DBusPreallocatedHash *hash_entry;
531 } OwnershipRestoreData;
534 restore_ownership (void *data)
536 OwnershipRestoreData *d = data;
539 _dbus_assert (d->service_link != NULL);
540 _dbus_assert (d->connection_link != NULL);
542 if (d->service->owners == NULL)
544 _dbus_assert (d->hash_entry != NULL);
545 bus_service_relink (d->service, d->hash_entry);
549 _dbus_assert (d->hash_entry == NULL);
552 /* We don't need to send messages notifying of these
553 * changes, since we're reverting something that was
554 * cancelled (effectively never really happened)
556 link = _dbus_list_get_first_link (&d->service->owners);
559 if (link->data == d->before_connection)
562 link = _dbus_list_get_next_link (&d->service->owners, link);
565 _dbus_list_insert_before_link (&d->service->owners, link, d->connection_link);
567 /* Note that removing then restoring this changes the order in which
568 * ServiceDeleted messages are sent on destruction of the
569 * connection. This should be OK as the only guarantee there is
570 * that the base service is destroyed last, and we never even
571 * tentatively remove the base service.
573 bus_connection_add_owned_service_link (d->connection, d->service_link);
575 d->hash_entry = NULL;
576 d->service_link = NULL;
577 d->connection_link = NULL;
581 free_ownership_restore_data (void *data)
583 OwnershipRestoreData *d = data;
586 _dbus_list_free_link (d->service_link);
587 if (d->connection_link)
588 _dbus_list_free_link (d->connection_link);
590 _dbus_hash_table_free_preallocated_entry (d->service->registry->service_hash,
593 dbus_connection_unref (d->connection);
594 bus_service_unref (d->service);
600 add_restore_ownership_to_transaction (BusTransaction *transaction,
602 DBusConnection *connection)
604 OwnershipRestoreData *d;
607 d = dbus_new (OwnershipRestoreData, 1);
611 d->service = service;
612 d->connection = connection;
613 d->service_link = _dbus_list_alloc_link (service);
614 d->connection_link = _dbus_list_alloc_link (connection);
615 d->hash_entry = _dbus_hash_table_preallocate_entry (service->registry->service_hash);
617 bus_service_ref (d->service);
618 dbus_connection_ref (d->connection);
620 d->before_connection = NULL;
621 link = _dbus_list_get_first_link (&service->owners);
624 if (link->data == connection)
626 link = _dbus_list_get_next_link (&service->owners, link);
629 d->before_connection = link->data;
634 link = _dbus_list_get_next_link (&service->owners, link);
637 if (d->service_link == NULL ||
638 d->connection_link == NULL ||
639 d->hash_entry == NULL ||
640 !bus_transaction_add_cancel_hook (transaction, restore_ownership, d,
641 free_ownership_restore_data))
643 free_ownership_restore_data (d);
650 /* this function is self-cancelling if you cancel the transaction */
652 bus_service_remove_owner (BusService *service,
653 DBusConnection *owner,
654 BusTransaction *transaction,
657 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
659 /* We send out notifications before we do any work we
660 * might have to undo if the notification-sending failed
663 /* Send service lost message */
664 if (bus_service_get_primary_owner (service) == owner)
666 if (!bus_driver_send_service_lost (owner, service->name,
671 if (service->owners == NULL)
673 _dbus_assert_not_reached ("Tried to remove owner of a service that has no owners");
675 else if (_dbus_list_length_is_one (&service->owners))
677 if (!bus_driver_send_service_deleted (service->name,
684 link = _dbus_list_get_first (&service->owners);
685 _dbus_assert (link != NULL);
686 link = _dbus_list_get_next_link (&service->owners, link);
688 _dbus_assert (link != NULL);
690 /* This will be our new owner */
691 if (!bus_driver_send_service_acquired (link->data,
698 if (!add_restore_ownership_to_transaction (transaction, service, owner))
704 bus_service_unlink_owner (service, owner);
706 if (service->owners == NULL)
707 bus_service_unlink (service);
713 bus_service_ref (BusService *service)
715 _dbus_assert (service->refcount > 0);
717 service->refcount += 1;
721 bus_service_unref (BusService *service)
723 _dbus_assert (service->refcount > 0);
725 service->refcount -= 1;
727 if (service->refcount == 0)
729 _dbus_assert (service->owners == NULL);
731 dbus_free (service->name);
732 _dbus_mem_pool_dealloc (service->registry->service_pool, service);
737 bus_service_get_primary_owner (BusService *service)
739 return _dbus_list_get_first (&service->owners);
743 bus_service_get_name (BusService *service)
745 return service->name;
749 bus_service_set_prohibit_replacement (BusService *service,
750 dbus_bool_t prohibit_replacement)
752 service->prohibit_replacement = prohibit_replacement != FALSE;
756 bus_service_get_prohibit_replacement (BusService *service)
758 return service->prohibit_replacement;
762 bus_service_has_owner (BusService *service,
763 DBusConnection *owner)
767 link = _dbus_list_get_first_link (&service->owners);
771 if (link->data == owner)
774 link = _dbus_list_get_next_link (&service->owners, link);