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 2.0
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"
41 BusRegistry *registry;
45 unsigned int prohibit_replacement : 1;
54 DBusHashTable *service_hash;
55 DBusMemPool *service_pool;
57 DBusHashTable *service_sid_table;
61 bus_registry_new (BusContext *context)
63 BusRegistry *registry;
65 registry = dbus_new0 (BusRegistry, 1);
69 registry->refcount = 1;
70 registry->context = context;
72 registry->service_hash = _dbus_hash_table_new (DBUS_HASH_STRING,
74 if (registry->service_hash == NULL)
77 registry->service_pool = _dbus_mem_pool_new (sizeof (BusService),
79 if (registry->service_pool == NULL)
82 registry->service_sid_table = NULL;
87 bus_registry_unref (registry);
92 bus_registry_ref (BusRegistry *registry)
94 _dbus_assert (registry->refcount > 0);
95 registry->refcount += 1;
101 bus_registry_unref (BusRegistry *registry)
103 _dbus_assert (registry->refcount > 0);
104 registry->refcount -= 1;
106 if (registry->refcount == 0)
108 if (registry->service_hash)
109 _dbus_hash_table_unref (registry->service_hash);
110 if (registry->service_pool)
111 _dbus_mem_pool_free (registry->service_pool);
112 if (registry->service_sid_table)
113 _dbus_hash_table_unref (registry->service_sid_table);
115 dbus_free (registry);
120 bus_registry_lookup (BusRegistry *registry,
121 const DBusString *service_name)
125 service = _dbus_hash_table_lookup_string (registry->service_hash,
126 _dbus_string_get_const_data (service_name));
132 bus_registry_ensure (BusRegistry *registry,
133 const DBusString *service_name,
134 DBusConnection *owner_if_created,
135 BusTransaction *transaction,
140 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
142 _dbus_assert (owner_if_created != NULL);
143 _dbus_assert (transaction != NULL);
145 service = _dbus_hash_table_lookup_string (registry->service_hash,
146 _dbus_string_get_const_data (service_name));
150 service = _dbus_mem_pool_alloc (registry->service_pool);
157 service->registry = registry;
158 service->refcount = 1;
160 if (!_dbus_string_copy_data (service_name, &service->name))
162 _dbus_mem_pool_dealloc (registry->service_pool, service);
167 if (!bus_driver_send_service_created (service->name, transaction, error))
169 bus_service_unref (service);
173 if (!bus_activation_service_created (bus_context_get_activation (registry->context),
174 service->name, transaction, error))
176 bus_service_unref (service);
180 if (!bus_service_add_owner (service, owner_if_created,
183 bus_service_unref (service);
187 if (!_dbus_hash_table_insert_string (registry->service_hash,
191 /* The add_owner gets reverted on transaction cancel */
200 bus_registry_foreach (BusRegistry *registry,
201 BusServiceForeachFunction function,
206 _dbus_hash_iter_init (registry->service_hash, &iter);
207 while (_dbus_hash_iter_next (&iter))
209 BusService *service = _dbus_hash_iter_get_value (&iter);
211 (* function) (service, data);
216 bus_registry_list_services (BusRegistry *registry,
224 len = _dbus_hash_table_get_n_entries (registry->service_hash);
225 retval = dbus_new (char *, len + 1);
230 _dbus_hash_iter_init (registry->service_hash, &iter);
232 while (_dbus_hash_iter_next (&iter))
234 BusService *service = _dbus_hash_iter_get_value (&iter);
236 retval[i] = _dbus_strdup (service->name);
237 if (retval[i] == NULL)
252 for (j = 0; j < i; j++)
253 dbus_free (retval[i]);
260 bus_registry_acquire_service (BusRegistry *registry,
261 DBusConnection *connection,
262 const DBusString *service_name,
264 dbus_uint32_t *result,
265 BusTransaction *transaction,
269 DBusConnection *old_owner;
270 DBusConnection *current_owner;
271 BusClientPolicy *policy;
273 BusActivation *activation;
278 if (_dbus_string_get_length (service_name) == 0)
280 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
281 "Zero-length service name is not allowed");
283 _dbus_verbose ("Attempt to acquire zero-length service name\n");
288 if (_dbus_string_get_byte (service_name, 0) == ':')
290 /* Not allowed; only base services can start with ':' */
291 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
292 "Cannot acquire a service starting with ':' such as \"%s\"",
293 _dbus_string_get_const_data (service_name));
295 _dbus_verbose ("Attempt to acquire invalid base service name \"%s\"",
296 _dbus_string_get_const_data (service_name));
301 policy = bus_connection_get_policy (connection);
302 _dbus_assert (policy != NULL);
304 /* Note that if sid is #NULL then the bus's own context gets used
305 * in bus_connection_selinux_allows_acquire_service()
307 sid = bus_selinux_id_table_lookup (registry->service_sid_table,
310 if (!bus_selinux_allows_acquire_service (connection, sid))
312 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
313 "Connection \"%s\" is not allowed to own the service \"%s\" due "
315 bus_connection_is_active (connection) ?
316 bus_connection_get_name (connection) :
322 if (!bus_client_policy_check_can_own (policy, connection,
325 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
326 "Connection \"%s\" is not allowed to own the service \"%s\" due "
327 "to security policies in the configuration file",
328 bus_connection_is_active (connection) ?
329 bus_connection_get_name (connection) :
335 if (bus_connection_get_n_services_owned (connection) >=
336 bus_context_get_max_services_per_connection (registry->context))
338 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
339 "Connection \"%s\" is not allowed to own more services "
340 "(increase limits in configuration file if required)",
341 bus_connection_is_active (connection) ?
342 bus_connection_get_name (connection) :
347 service = bus_registry_lookup (registry, service_name);
350 old_owner = bus_service_get_primary_owner (service);
356 service = bus_registry_ensure (registry,
357 service_name, connection, transaction, error);
362 current_owner = bus_service_get_primary_owner (service);
364 if (old_owner == NULL)
366 _dbus_assert (current_owner == connection);
368 bus_service_set_prohibit_replacement (service,
369 (flags & DBUS_SERVICE_FLAG_PROHIBIT_REPLACEMENT));
371 *result = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
373 else if (old_owner == connection)
374 *result = DBUS_SERVICE_REPLY_ALREADY_OWNER;
375 else if (!((flags & DBUS_SERVICE_FLAG_REPLACE_EXISTING)))
376 *result = DBUS_SERVICE_REPLY_SERVICE_EXISTS;
377 else if (bus_service_get_prohibit_replacement (service))
379 /* Queue the connection */
380 if (!bus_service_add_owner (service, connection,
384 *result = DBUS_SERVICE_REPLY_IN_QUEUE;
388 /* Replace the current owner */
390 /* We enqueue the new owner and remove the first one because
391 * that will cause ServiceAcquired and ServiceLost messages to
395 if (!bus_service_add_owner (service, connection,
399 if (!bus_service_remove_owner (service, old_owner,
403 _dbus_assert (connection == bus_service_get_primary_owner (service));
404 *result = DBUS_SERVICE_REPLY_PRIMARY_OWNER;
407 activation = bus_context_get_activation (registry->context);
408 retval = bus_activation_send_pending_auto_activation_messages (activation,
418 bus_registry_set_service_sid_table (BusRegistry *registry,
419 DBusHashTable *table)
421 _dbus_assert (registry->service_sid_table != table);
423 if (registry->service_sid_table)
424 _dbus_hash_table_unref (registry->service_sid_table);
426 registry->service_sid_table = table;
427 _dbus_hash_table_ref (table);
431 bus_service_unlink_owner (BusService *service,
432 DBusConnection *owner)
434 _dbus_list_remove_last (&service->owners, owner);
435 bus_connection_remove_owned_service (owner, service);
439 bus_service_unlink (BusService *service)
441 _dbus_assert (service->owners == NULL);
443 /* the service may not be in the hash, if
444 * the failure causing transaction cancel
445 * was in the right place, but that's OK
447 _dbus_hash_table_remove_string (service->registry->service_hash,
450 bus_service_unref (service);
454 bus_service_relink (BusService *service,
455 DBusPreallocatedHash *preallocated)
457 _dbus_assert (service->owners == NULL);
458 _dbus_assert (preallocated != NULL);
460 _dbus_hash_table_insert_string_preallocated (service->registry->service_hash,
465 bus_service_ref (service);
469 * Data used to represent an ownership cancellation in
474 DBusConnection *connection; /**< the connection */
475 BusService *service; /**< service to cancel ownership of */
476 } OwnershipCancelData;
479 cancel_ownership (void *data)
481 OwnershipCancelData *d = data;
483 /* We don't need to send messages notifying of these
484 * changes, since we're reverting something that was
485 * cancelled (effectively never really happened)
487 bus_service_unlink_owner (d->service, d->connection);
489 if (d->service->owners == NULL)
490 bus_service_unlink (d->service);
494 free_ownership_cancel_data (void *data)
496 OwnershipCancelData *d = data;
498 dbus_connection_unref (d->connection);
499 bus_service_unref (d->service);
505 add_cancel_ownership_to_transaction (BusTransaction *transaction,
507 DBusConnection *connection)
509 OwnershipCancelData *d;
511 d = dbus_new (OwnershipCancelData, 1);
515 d->service = service;
516 d->connection = connection;
518 if (!bus_transaction_add_cancel_hook (transaction, cancel_ownership, d,
519 free_ownership_cancel_data))
525 bus_service_ref (d->service);
526 dbus_connection_ref (d->connection);
531 /* this function is self-cancelling if you cancel the transaction */
533 bus_service_add_owner (BusService *service,
534 DBusConnection *owner,
535 BusTransaction *transaction,
538 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
540 /* Send service acquired message first, OOM will result
541 * in cancelling the transaction
543 if (service->owners == NULL)
545 if (!bus_driver_send_service_acquired (owner, service->name, transaction, error))
549 if (!_dbus_list_append (&service->owners,
556 if (!bus_connection_add_owned_service (owner, service))
558 _dbus_list_remove_last (&service->owners, owner);
563 if (!add_cancel_ownership_to_transaction (transaction,
567 bus_service_unlink_owner (service, owner);
577 DBusConnection *connection;
579 DBusConnection *before_connection; /* restore to position before this connection in owners list */
580 DBusList *connection_link;
581 DBusList *service_link;
582 DBusPreallocatedHash *hash_entry;
583 } OwnershipRestoreData;
586 restore_ownership (void *data)
588 OwnershipRestoreData *d = data;
591 _dbus_assert (d->service_link != NULL);
592 _dbus_assert (d->connection_link != NULL);
594 if (d->service->owners == NULL)
596 _dbus_assert (d->hash_entry != NULL);
597 bus_service_relink (d->service, d->hash_entry);
601 _dbus_assert (d->hash_entry == NULL);
604 /* We don't need to send messages notifying of these
605 * changes, since we're reverting something that was
606 * cancelled (effectively never really happened)
608 link = _dbus_list_get_first_link (&d->service->owners);
611 if (link->data == d->before_connection)
614 link = _dbus_list_get_next_link (&d->service->owners, link);
617 _dbus_list_insert_before_link (&d->service->owners, link, d->connection_link);
619 /* Note that removing then restoring this changes the order in which
620 * ServiceDeleted messages are sent on destruction of the
621 * connection. This should be OK as the only guarantee there is
622 * that the base service is destroyed last, and we never even
623 * tentatively remove the base service.
625 bus_connection_add_owned_service_link (d->connection, d->service_link);
627 d->hash_entry = NULL;
628 d->service_link = NULL;
629 d->connection_link = NULL;
633 free_ownership_restore_data (void *data)
635 OwnershipRestoreData *d = data;
638 _dbus_list_free_link (d->service_link);
639 if (d->connection_link)
640 _dbus_list_free_link (d->connection_link);
642 _dbus_hash_table_free_preallocated_entry (d->service->registry->service_hash,
645 dbus_connection_unref (d->connection);
646 bus_service_unref (d->service);
652 add_restore_ownership_to_transaction (BusTransaction *transaction,
654 DBusConnection *connection)
656 OwnershipRestoreData *d;
659 d = dbus_new (OwnershipRestoreData, 1);
663 d->service = service;
664 d->connection = connection;
665 d->service_link = _dbus_list_alloc_link (service);
666 d->connection_link = _dbus_list_alloc_link (connection);
667 d->hash_entry = _dbus_hash_table_preallocate_entry (service->registry->service_hash);
669 bus_service_ref (d->service);
670 dbus_connection_ref (d->connection);
672 d->before_connection = NULL;
673 link = _dbus_list_get_first_link (&service->owners);
676 if (link->data == connection)
678 link = _dbus_list_get_next_link (&service->owners, link);
681 d->before_connection = link->data;
686 link = _dbus_list_get_next_link (&service->owners, link);
689 if (d->service_link == NULL ||
690 d->connection_link == NULL ||
691 d->hash_entry == NULL ||
692 !bus_transaction_add_cancel_hook (transaction, restore_ownership, d,
693 free_ownership_restore_data))
695 free_ownership_restore_data (d);
702 /* this function is self-cancelling if you cancel the transaction */
704 bus_service_remove_owner (BusService *service,
705 DBusConnection *owner,
706 BusTransaction *transaction,
709 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
711 /* We send out notifications before we do any work we
712 * might have to undo if the notification-sending failed
715 /* Send service lost message */
716 if (bus_service_get_primary_owner (service) == owner)
718 if (!bus_driver_send_service_lost (owner, service->name,
723 if (service->owners == NULL)
725 _dbus_assert_not_reached ("Tried to remove owner of a service that has no owners");
727 else if (_dbus_list_length_is_one (&service->owners))
729 if (!bus_driver_send_service_deleted (service->name,
736 link = _dbus_list_get_first_link (&service->owners);
737 _dbus_assert (link != NULL);
738 link = _dbus_list_get_next_link (&service->owners, link);
739 _dbus_assert (link != NULL);
741 /* This will be our new owner */
742 if (!bus_driver_send_service_acquired (link->data,
749 if (!add_restore_ownership_to_transaction (transaction, service, owner))
755 bus_service_unlink_owner (service, owner);
757 if (service->owners == NULL)
758 bus_service_unlink (service);
764 bus_service_ref (BusService *service)
766 _dbus_assert (service->refcount > 0);
768 service->refcount += 1;
774 bus_service_unref (BusService *service)
776 _dbus_assert (service->refcount > 0);
778 service->refcount -= 1;
780 if (service->refcount == 0)
782 _dbus_assert (service->owners == NULL);
784 dbus_free (service->name);
785 _dbus_mem_pool_dealloc (service->registry->service_pool, service);
790 bus_service_get_primary_owner (BusService *service)
792 return _dbus_list_get_first (&service->owners);
796 bus_service_get_name (BusService *service)
798 return service->name;
802 bus_service_set_prohibit_replacement (BusService *service,
803 dbus_bool_t prohibit_replacement)
805 service->prohibit_replacement = prohibit_replacement != FALSE;
809 bus_service_get_prohibit_replacement (BusService *service)
811 return service->prohibit_replacement;
815 bus_service_has_owner (BusService *service,
816 DBusConnection *owner)
820 link = _dbus_list_get_first_link (&service->owners);
824 if (link->data == owner)
827 link = _dbus_list_get_next_link (&service->owners, link);