services.c 23.4 KB
Newer Older
1 2 3 4
/* -*- mode: C; c-file-style: "gnu" -*- */
/* services.c  Service management
 *
 * Copyright (C) 2003  Red Hat, Inc.
5
 * Copyright (C) 2003  CodeFactory AB
6
 *
7
 * Licensed under the Academic Free License version 2.1
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include <dbus/dbus-hash.h>
#include <dbus/dbus-list.h>
#include <dbus/dbus-mempool.h>
27
#include <dbus/dbus-marshal-validate.h>
28

29 30 31 32
#include "driver.h"
#include "services.h"
#include "connection.h"
#include "utils.h"
33
#include "activation.h"
34
#include "policy.h"
35 36
#include "bus.h"
#include "selinux.h"
37

38 39
struct BusService
{
40 41
  int refcount;

42
  BusRegistry *registry;
43 44
  char *name;
  DBusList *owners;
45 46
  
  unsigned int prohibit_replacement : 1;
47 48
};

49 50 51
struct BusRegistry
{
  int refcount;
52 53

  BusContext *context;
54 55 56
  
  DBusHashTable *service_hash;
  DBusMemPool   *service_pool;
57 58

  DBusHashTable *service_sid_table;
59 60 61
};

BusRegistry*
62
bus_registry_new (BusContext *context)
63 64 65 66 67 68 69 70
{
  BusRegistry *registry;

  registry = dbus_new0 (BusRegistry, 1);
  if (registry == NULL)
    return NULL;

  registry->refcount = 1;
71 72
  registry->context = context;
  
73 74 75 76 77 78 79 80 81 82
  registry->service_hash = _dbus_hash_table_new (DBUS_HASH_STRING,
                                                 NULL, NULL);
  if (registry->service_hash == NULL)
    goto failed;
  
  registry->service_pool = _dbus_mem_pool_new (sizeof (BusService),
                                               TRUE);
  if (registry->service_pool == NULL)
    goto failed;

83 84
  registry->service_sid_table = NULL;
  
85 86 87 88 89 90 91
  return registry;

 failed:
  bus_registry_unref (registry);
  return NULL;
}

92
BusRegistry *
93 94 95 96
bus_registry_ref (BusRegistry *registry)
{
  _dbus_assert (registry->refcount > 0);
  registry->refcount += 1;
97 98

  return registry;
99
}
100

101 102
void
bus_registry_unref  (BusRegistry *registry)
103
{
104 105 106 107
  _dbus_assert (registry->refcount > 0);
  registry->refcount -= 1;

  if (registry->refcount == 0)
108
    {
109 110 111 112
      if (registry->service_hash)
        _dbus_hash_table_unref (registry->service_hash);
      if (registry->service_pool)
        _dbus_mem_pool_free (registry->service_pool);
113 114 115
      if (registry->service_sid_table)
        _dbus_hash_table_unref (registry->service_sid_table);
      
116
      dbus_free (registry);
117
    }
118 119 120
}

BusService*
121 122
bus_registry_lookup (BusRegistry      *registry,
                     const DBusString *service_name)
123 124
{
  BusService *service;
125

126
  service = _dbus_hash_table_lookup_string (registry->service_hash,
127
                                            _dbus_string_get_const_data (service_name));
128

129 130 131 132
  return service;
}

BusService*
133 134 135 136 137
bus_registry_ensure (BusRegistry               *registry,
                     const DBusString          *service_name,
                     DBusConnection            *owner_if_created,
                     BusTransaction            *transaction,
                     DBusError                 *error)
138 139 140
{
  BusService *service;

141 142
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
143 144 145
  _dbus_assert (owner_if_created != NULL);
  _dbus_assert (transaction != NULL);

146
  service = _dbus_hash_table_lookup_string (registry->service_hash,
147
                                            _dbus_string_get_const_data (service_name));
148 149 150
  if (service != NULL)
    return service;
  
151
  service = _dbus_mem_pool_alloc (registry->service_pool);
152
  if (service == NULL)
153 154 155 156
    {
      BUS_SET_OOM (error);
      return NULL;
    }
157

158
  service->registry = registry;  
159
  service->refcount = 1;
160 161 162

  _dbus_verbose ("copying string %p '%s' to service->name\n",
                 service_name, _dbus_string_get_const_data (service_name));
163
  if (!_dbus_string_copy_data (service_name, &service->name))
164
    {
165
      _dbus_mem_pool_dealloc (registry->service_pool, service);
166
      BUS_SET_OOM (error);
167 168
      return NULL;
    }
169 170 171
  _dbus_verbose ("copied string %p '%s' to '%s'\n",
                 service_name, _dbus_string_get_const_data (service_name),
                 service->name);
172

173 174 175 176
  if (!bus_driver_send_service_owner_changed (service->name, 
					      NULL,
					      bus_connection_get_name (owner_if_created),
					      transaction, error))
177
    {
178
      bus_service_unref (service);
179 180 181
      return NULL;
    }

182
  if (!bus_activation_service_created (bus_context_get_activation (registry->context),
183
				       service->name, transaction, error))
184
    {
185
      bus_service_unref (service);
186 187 188
      return NULL;
    }
  
189 190 191
  if (!bus_service_add_owner (service, owner_if_created,
                              transaction, error))
    {
192
      bus_service_unref (service);
193 194 195
      return NULL;
    }
  
196
  if (!_dbus_hash_table_insert_string (registry->service_hash,
197 198 199
                                       service->name,
                                       service))
    {
200
      /* The add_owner gets reverted on transaction cancel */
201
      BUS_SET_OOM (error);
202 203
      return NULL;
    }
204
  
205 206 207
  return service;
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
void
bus_registry_foreach (BusRegistry               *registry,
                      BusServiceForeachFunction  function,
                      void                      *data)
{
  DBusHashIter iter;
  
  _dbus_hash_iter_init (registry->service_hash, &iter);
  while (_dbus_hash_iter_next (&iter))
    {
      BusService *service = _dbus_hash_iter_get_value (&iter);

      (* function) (service, data);
    }
}

dbus_bool_t
bus_registry_list_services (BusRegistry *registry,
                            char      ***listp,
                            int         *array_len)
{
  int i, j, len;
  char **retval;
  DBusHashIter iter;
   
  len = _dbus_hash_table_get_n_entries (registry->service_hash);
  retval = dbus_new (char *, len + 1);

  if (retval == NULL)
    return FALSE;

  _dbus_hash_iter_init (registry->service_hash, &iter);
  i = 0;
  while (_dbus_hash_iter_next (&iter))
    {
      BusService *service = _dbus_hash_iter_get_value (&iter);

      retval[i] = _dbus_strdup (service->name);
      if (retval[i] == NULL)
	goto error;

      i++;
    }

  retval[i] = NULL;
  
  if (array_len)
    *array_len = len;
  
  *listp = retval;
  return TRUE;
  
 error:
  for (j = 0; j < i; j++)
    dbus_free (retval[i]);
  dbus_free (retval);

  return FALSE;
}

268 269 270 271 272 273 274 275 276 277 278 279
dbus_bool_t
bus_registry_acquire_service (BusRegistry      *registry,
                              DBusConnection   *connection,
                              const DBusString *service_name,
                              dbus_uint32_t     flags,
                              dbus_uint32_t    *result,
                              BusTransaction   *transaction,
                              DBusError        *error)
{
  dbus_bool_t retval;
  DBusConnection *old_owner;
  DBusConnection *current_owner;
280
  BusClientPolicy *policy;
281
  BusService *service;
282
  BusActivation  *activation;
283
  BusSELinuxID *sid;
284 285 286
  
  retval = FALSE;

287 288
  if (!_dbus_validate_bus_name (service_name, 0,
                                _dbus_string_get_length (service_name)))
289
    {
290 291 292
      dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
                      "Requested bus name \"%s\" is not valid",
                      _dbus_string_get_const_data (service_name));
293
      
294
      _dbus_verbose ("Attempt to acquire invalid service name\n");
295 296 297 298 299 300 301
      
      goto out;
    }
  
  if (_dbus_string_get_byte (service_name, 0) == ':')
    {
      /* Not allowed; only base services can start with ':' */
302
      dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
303 304 305 306 307 308 309 310
                      "Cannot acquire a service starting with ':' such as \"%s\"",
                      _dbus_string_get_const_data (service_name));
      
      _dbus_verbose ("Attempt to acquire invalid base service name \"%s\"",
                     _dbus_string_get_const_data (service_name));
      
      goto out;
    }
311

312 313
  policy = bus_connection_get_policy (connection);
  _dbus_assert (policy != NULL);
314

315 316 317 318 319 320
  /* Note that if sid is #NULL then the bus's own context gets used
   * in bus_connection_selinux_allows_acquire_service()
   */
  sid = bus_selinux_id_table_lookup (registry->service_sid_table,
                                     service_name);

321
  if (!bus_selinux_allows_acquire_service (connection, sid,
322
					   _dbus_string_get_const_data (service_name), error))
323
    {
324 325 326 327 328 329 330

      if (dbus_error_is_set (error) &&
	  dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
	{
	  goto out;
	}

331 332 333 334 335 336
      dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
                      "Connection \"%s\" is not allowed to own the service \"%s\" due "
                      "to SELinux policy",
                      bus_connection_is_active (connection) ?
                      bus_connection_get_name (connection) :
                      "(inactive)",
337
                      _dbus_string_get_const_data (service_name));
338 339 340
      goto out;
    }
      
341 342 343 344 345 346 347 348
  if (!bus_client_policy_check_can_own (policy, connection,
                                        service_name))
    {
      dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
                      "Connection \"%s\" is not allowed to own the service \"%s\" due "
                      "to security policies in the configuration file",
                      bus_connection_is_active (connection) ?
                      bus_connection_get_name (connection) :
349
                      "(inactive)",
350
                      _dbus_string_get_const_data (service_name));
351 352 353 354 355 356 357 358 359 360 361 362 363 364
      goto out;
    }

  if (bus_connection_get_n_services_owned (connection) >=
      bus_context_get_max_services_per_connection (registry->context))
    {
      dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
                      "Connection \"%s\" is not allowed to own more services "
                      "(increase limits in configuration file if required)",
                      bus_connection_is_active (connection) ?
                      bus_connection_get_name (connection) :
                      "(inactive)");
      goto out;
    }
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  
  service = bus_registry_lookup (registry, service_name);

  if (service != NULL)
    old_owner = bus_service_get_primary_owner (service);
  else
    old_owner = NULL;
      
  if (service == NULL)
    {
      service = bus_registry_ensure (registry,
                                     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,
388
					    (flags & DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT));      
389
			
390
      *result = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;      
391 392
    }
  else if (old_owner == connection)
393 394 395
    *result = DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
  else if (!((flags & DBUS_NAME_FLAG_REPLACE_EXISTING)))
    *result = DBUS_REQUEST_NAME_REPLY_EXISTS;
396 397 398 399 400 401 402
  else if (bus_service_get_prohibit_replacement (service))
    {
      /* Queue the connection */
      if (!bus_service_add_owner (service, connection,
                                  transaction, error))
        goto out;
      
403
      *result = DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
404 405 406 407 408 409
    }
  else
    {
      /* Replace the current owner */

      /* We enqueue the new owner and remove the first one because
410
       * that will cause NameAcquired and NameLost messages to
411 412 413 414 415 416 417 418 419 420 421 422
       * be sent.
       */
      
      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));
423
      *result = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
424 425
    }

426 427 428 429 430
  activation = bus_context_get_activation (registry->context);
  retval = bus_activation_send_pending_auto_activation_messages (activation,
								 service,
								 transaction,
								 error);
431 432 433 434 435
  
 out:
  return retval;
}

436 437 438
dbus_bool_t
bus_registry_set_service_context_table (BusRegistry   *registry,
					DBusHashTable *table)
439
{
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
  DBusHashTable *new_table;
  DBusHashIter iter;
  
  new_table = bus_selinux_id_table_new ();
  if (!new_table)
    return FALSE;

  _dbus_hash_iter_init (table, &iter);
  while (_dbus_hash_iter_next (&iter))
    {
      const char *service = _dbus_hash_iter_get_string_key (&iter);
      const char *context = _dbus_hash_iter_get_value (&iter);

      if (!bus_selinux_id_table_insert (new_table,
					service,
					context))
	return FALSE;
    }
458 459 460
  
  if (registry->service_sid_table)
    _dbus_hash_table_unref (registry->service_sid_table);
461 462
  registry->service_sid_table = new_table;
  return TRUE;
463 464
}

465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
static void
bus_service_unlink_owner (BusService      *service,
                          DBusConnection  *owner)
{
  _dbus_list_remove_last (&service->owners, owner);
  bus_connection_remove_owned_service (owner, service);
}

static void
bus_service_unlink (BusService *service)
{
  _dbus_assert (service->owners == NULL);

  /* the service may not be in the hash, if
   * the failure causing transaction cancel
   * was in the right place, but that's OK
   */
  _dbus_hash_table_remove_string (service->registry->service_hash,
                                  service->name);
  
  bus_service_unref (service);
}

static void
bus_service_relink (BusService           *service,
                    DBusPreallocatedHash *preallocated)
{
  _dbus_assert (service->owners == NULL);
  _dbus_assert (preallocated != NULL);

  _dbus_hash_table_insert_string_preallocated (service->registry->service_hash,
                                               preallocated,
                                               service->name,
                                               service);
  
  bus_service_ref (service);
}

503 504 505 506
/**
 * Data used to represent an ownership cancellation in
 * a bus transaction.
 */
507 508
typedef struct
{
509 510
  DBusConnection *connection; /**< the connection */
  BusService *service;        /**< service to cancel ownership of */
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
} OwnershipCancelData;

static void
cancel_ownership (void *data)
{
  OwnershipCancelData *d = data;

  /* We don't need to send messages notifying of these
   * changes, since we're reverting something that was
   * cancelled (effectively never really happened)
   */
  bus_service_unlink_owner (d->service, d->connection);
  
  if (d->service->owners == NULL)
    bus_service_unlink (d->service);
}

static void
free_ownership_cancel_data (void *data)
{
  OwnershipCancelData *d = data;

  dbus_connection_unref (d->connection);
  bus_service_unref (d->service);
  
  dbus_free (d);
}

static dbus_bool_t
add_cancel_ownership_to_transaction (BusTransaction *transaction,
                                     BusService     *service,
                                     DBusConnection *connection)
{
  OwnershipCancelData *d;

  d = dbus_new (OwnershipCancelData, 1);
  if (d == NULL)
    return FALSE;
  
  d->service = service;
  d->connection = connection;

  if (!bus_transaction_add_cancel_hook (transaction, cancel_ownership, d,
                                        free_ownership_cancel_data))
    {
      dbus_free (d);
      return FALSE;
    }

  bus_service_ref (d->service);
  dbus_connection_ref (d->connection);
  
  return TRUE;
}

/* this function is self-cancelling if you cancel the transaction */
567 568
dbus_bool_t
bus_service_add_owner (BusService     *service,
569 570 571
                       DBusConnection *owner,
                       BusTransaction *transaction,
                       DBusError      *error)
572
{
573 574
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
575 576 577 578 579 580 581 582 583
 /* 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;
    }
  
584 585
  if (!_dbus_list_append (&service->owners,
                          owner))
586 587 588 589
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
590 591 592 593

  if (!bus_connection_add_owned_service (owner, service))
    {
      _dbus_list_remove_last (&service->owners, owner);
594
      BUS_SET_OOM (error);
595 596
      return FALSE;
    }
597 598 599 600 601 602 603 604 605

  if (!add_cancel_ownership_to_transaction (transaction,
                                            service,
                                            owner))
    {
      bus_service_unlink_owner (service, owner);
      BUS_SET_OOM (error);
      return FALSE;
    }
606
  
607 608 609
  return TRUE;
}

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
typedef struct
{
  DBusConnection *connection;
  BusService     *service;
  DBusConnection *before_connection; /* restore to position before this connection in owners list */
  DBusList       *connection_link;
  DBusList       *service_link;
  DBusPreallocatedHash *hash_entry;
} OwnershipRestoreData;

static void
restore_ownership (void *data)
{
  OwnershipRestoreData *d = data;
  DBusList *link;

  _dbus_assert (d->service_link != NULL);
  _dbus_assert (d->connection_link != NULL);
  
  if (d->service->owners == NULL)
    {
      _dbus_assert (d->hash_entry != NULL);
      bus_service_relink (d->service, d->hash_entry);
    }
  else
    {
      _dbus_assert (d->hash_entry == NULL);
    }
  
  /* We don't need to send messages notifying of these
   * changes, since we're reverting something that was
   * cancelled (effectively never really happened)
   */
  link = _dbus_list_get_first_link (&d->service->owners);
  while (link != NULL)
    {
      if (link->data == d->before_connection)
        break;

      link = _dbus_list_get_next_link (&d->service->owners, link);
    }
  
  _dbus_list_insert_before_link (&d->service->owners, link, d->connection_link);

  /* Note that removing then restoring this changes the order in which
   * ServiceDeleted messages are sent on destruction of the
   * connection.  This should be OK as the only guarantee there is
   * that the base service is destroyed last, and we never even
   * tentatively remove the base service.
   */
  bus_connection_add_owned_service_link (d->connection, d->service_link);
  
  d->hash_entry = NULL;
  d->service_link = NULL;
  d->connection_link = NULL;
}

static void
free_ownership_restore_data (void *data)
{
  OwnershipRestoreData *d = data;

  if (d->service_link)
    _dbus_list_free_link (d->service_link);
  if (d->connection_link)
    _dbus_list_free_link (d->connection_link);
  if (d->hash_entry)
    _dbus_hash_table_free_preallocated_entry (d->service->registry->service_hash,
                                              d->hash_entry);

  dbus_connection_unref (d->connection);
  bus_service_unref (d->service);
  
  dbus_free (d);
}

static dbus_bool_t
add_restore_ownership_to_transaction (BusTransaction *transaction,
                                      BusService     *service,
                                      DBusConnection *connection)
{
  OwnershipRestoreData *d;
  DBusList *link;

  d = dbus_new (OwnershipRestoreData, 1);
  if (d == NULL)
    return FALSE;
  
  d->service = service;
  d->connection = connection;
  d->service_link = _dbus_list_alloc_link (service);
  d->connection_link = _dbus_list_alloc_link (connection);
  d->hash_entry = _dbus_hash_table_preallocate_entry (service->registry->service_hash);
  
  bus_service_ref (d->service);
  dbus_connection_ref (d->connection);

  d->before_connection = NULL;
  link = _dbus_list_get_first_link (&service->owners);
  while (link != NULL)
    {
      if (link->data == connection)
        {
          link = _dbus_list_get_next_link (&service->owners, link);

          if (link)
            d->before_connection = link->data;

          break;
        }
      
      link = _dbus_list_get_next_link (&service->owners, link);
    }
  
  if (d->service_link == NULL ||
      d->connection_link == NULL ||
      d->hash_entry == NULL ||
      !bus_transaction_add_cancel_hook (transaction, restore_ownership, d,
                                        free_ownership_restore_data))
    {
      free_ownership_restore_data (d);
      return FALSE;
    }
  
  return TRUE;
}

/* this function is self-cancelling if you cancel the transaction */
738
dbus_bool_t
739
bus_service_remove_owner (BusService     *service,
740 741 742
                          DBusConnection *owner,
                          BusTransaction *transaction,
                          DBusError      *error)
743
{
744 745
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
746 747 748 749
  /* We send out notifications before we do any work we
   * might have to undo if the notification-sending failed
   */
  
750 751
  /* Send service lost message */
  if (bus_service_get_primary_owner (service) == owner)
752 753 754 755 756 757
    {
      if (!bus_driver_send_service_lost (owner, service->name,
                                         transaction, error))
        return FALSE;
    }

758 759 760 761 762
  if (service->owners == NULL)
    {
      _dbus_assert_not_reached ("Tried to remove owner of a service that has no owners");
    }
  else if (_dbus_list_length_is_one (&service->owners))
763
    {
764 765 766 767
      if (!bus_driver_send_service_owner_changed (service->name,
 						  bus_connection_get_name (owner),
 						  NULL,
 						  transaction, error))
768 769 770 771 772
        return FALSE;
    }
  else
    {
      DBusList *link;
773
      DBusConnection *new_owner;
774
      link = _dbus_list_get_first_link (&service->owners);
775
      _dbus_assert (link != NULL);
776
      link = _dbus_list_get_next_link (&service->owners, link);
777 778
      _dbus_assert (link != NULL);

779 780 781 782 783 784 785 786
      new_owner = link->data;

      if (!bus_driver_send_service_owner_changed (service->name,
 						  bus_connection_get_name (owner),
 						  bus_connection_get_name (new_owner),
 						  transaction, error))
        return FALSE;

787
      /* This will be our new owner */
788
      if (!bus_driver_send_service_acquired (new_owner,
789 790 791 792 793 794 795 796 797 798
                                             service->name,
                                             transaction,
                                             error))
        return FALSE;
    }

  if (!add_restore_ownership_to_transaction (transaction, service, owner))
    {
      BUS_SET_OOM (error);
      return FALSE;
799
    }
800
  
801
  bus_service_unlink_owner (service, owner);
802 803

  if (service->owners == NULL)
804 805 806 807 808
    bus_service_unlink (service);

  return TRUE;
}

809
BusService *
810 811 812 813 814
bus_service_ref (BusService *service)
{
  _dbus_assert (service->refcount > 0);
  
  service->refcount += 1;
815 816

  return service;
817 818 819 820 821 822 823 824 825 826
}

void
bus_service_unref (BusService *service)
{
  _dbus_assert (service->refcount > 0);
  
  service->refcount -= 1;

  if (service->refcount == 0)
827
    {
828
      _dbus_assert (service->owners == NULL);
829 830
      
      dbus_free (service->name);
831
      _dbus_mem_pool_dealloc (service->registry->service_pool, service);
832
    }
833 834 835 836 837 838 839 840 841 842 843 844 845 846
}

DBusConnection*
bus_service_get_primary_owner (BusService *service)
{
  return _dbus_list_get_first (&service->owners);
}

const char*
bus_service_get_name (BusService *service)
{
  return service->name;
}

847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
void
bus_service_set_prohibit_replacement (BusService  *service,
				      dbus_bool_t  prohibit_replacement)
{
  service->prohibit_replacement = prohibit_replacement != FALSE;
}

dbus_bool_t
bus_service_get_prohibit_replacement (BusService *service)
{
  return service->prohibit_replacement;
}

dbus_bool_t
bus_service_has_owner (BusService     *service,
		       DBusConnection *owner)
{
  DBusList *link;

  link = _dbus_list_get_first_link (&service->owners);
  
  while (link != NULL)
    {
      if (link->data == owner)
	return TRUE;
      
      link = _dbus_list_get_next_link (&service->owners, link);
    }

  return FALSE;
}