7c42c7d2417e3b1055665ebac4b2582e9534e870
[platform/upstream/dbus.git] / bus / services.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* services.c  Service management
3  *
4  * Copyright (C) 2003  Red Hat, Inc.
5  * Copyright (C) 2003  CodeFactory AB
6  * Copyright (C) 2013  Samsung Electronics
7  *
8  * Licensed under the Academic Free License version 2.1
9  * 
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23  *
24  */
25
26 #include <config.h>
27 #include <dbus/dbus-hash.h>
28 #include <dbus/dbus-list.h>
29 #include <dbus/dbus-mempool.h>
30 #include <dbus/dbus-marshal-validate.h>
31 #include "driver.h"
32 #include "services.h"
33 #include "connection.h"
34 #include "utils.h"
35 #include "activation.h"
36 #include "policy.h"
37 #include "bus.h"
38 #include "selinux.h"
39
40 #ifdef ENABLE_KDBUS_TRANSPORT
41 #include <linux/types.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <limits.h>
46
47 #include "kdbus-d.h"
48 #include "dbus/kdbus.h"
49 #endif
50
51 struct BusService
52 {
53   int refcount;
54
55   BusRegistry *registry;
56   char *name;
57   DBusList *owners;
58 };
59
60 struct BusOwner
61 {
62   int refcount;
63
64   BusService *service;
65   DBusConnection *conn;
66
67   unsigned int allow_replacement : 1;
68   unsigned int do_not_queue : 1;
69 #ifdef ENABLE_KDBUS_TRANSPORT
70   unsigned int is_kdbus_starter : 1;
71 #endif
72 };
73
74 struct BusRegistry
75 {
76   int refcount;
77
78   BusContext *context;
79   
80   DBusHashTable *service_hash;
81   DBusMemPool   *service_pool;
82   DBusMemPool   *owner_pool;
83
84   DBusHashTable *service_sid_table;
85 };
86
87 BusRegistry*
88 bus_registry_new (BusContext *context)
89 {
90   BusRegistry *registry;
91
92   registry = dbus_new0 (BusRegistry, 1);
93   if (registry == NULL)
94     return NULL;
95
96   registry->refcount = 1;
97   registry->context = context;
98   
99   registry->service_hash = _dbus_hash_table_new (DBUS_HASH_STRING,
100                                                  NULL, NULL);
101   if (registry->service_hash == NULL)
102     goto failed;
103   
104   registry->service_pool = _dbus_mem_pool_new (sizeof (BusService),
105                                                TRUE);
106
107   if (registry->service_pool == NULL)
108     goto failed;
109
110   registry->owner_pool = _dbus_mem_pool_new (sizeof (BusOwner),
111                                              TRUE);
112
113   if (registry->owner_pool == NULL)
114     goto failed;
115
116   registry->service_sid_table = NULL;
117   
118   return registry;
119
120  failed:
121   bus_registry_unref (registry);
122   return NULL;
123 }
124
125 BusRegistry *
126 bus_registry_ref (BusRegistry *registry)
127 {
128   _dbus_assert (registry->refcount > 0);
129   registry->refcount += 1;
130
131   return registry;
132 }
133
134 void
135 bus_registry_unref  (BusRegistry *registry)
136 {
137   _dbus_assert (registry->refcount > 0);
138   registry->refcount -= 1;
139
140   if (registry->refcount == 0)
141     {
142       if (registry->service_hash)
143         _dbus_hash_table_unref (registry->service_hash);
144       if (registry->service_pool)
145         _dbus_mem_pool_free (registry->service_pool);
146       if (registry->owner_pool)
147         _dbus_mem_pool_free (registry->owner_pool);
148       if (registry->service_sid_table)
149         _dbus_hash_table_unref (registry->service_sid_table);
150       
151       dbus_free (registry);
152     }
153 }
154
155 BusService*
156 bus_registry_lookup (BusRegistry      *registry,
157                      const DBusString *service_name)
158 {
159   BusService *service;
160
161   service = _dbus_hash_table_lookup_string (registry->service_hash,
162                                             _dbus_string_get_const_data (service_name));
163
164   return service;
165 }
166
167 static DBusList *
168 _bus_service_find_owner_link (BusService *service,
169                               DBusConnection *connection)
170 {
171   DBusList *link;
172   
173   link = _dbus_list_get_first_link (&service->owners);
174
175   while (link != NULL)
176     {
177       BusOwner *bus_owner;
178
179       bus_owner = (BusOwner *) link->data;
180       if (bus_owner->conn == connection) 
181         break;
182
183       link = _dbus_list_get_next_link (&service->owners, link);
184     }
185
186   return link;
187 }
188
189 #ifdef ENABLE_KDBUS_TRANSPORT
190 static DBusConnection *
191 _bus_service_find_owner_connection (BusService *service,
192                                    const char* unique_name)
193 {
194   DBusList *link;
195
196   link = _dbus_list_get_first_link (&service->owners);
197
198   while (link != NULL)
199     {
200       BusOwner *bus_owner;
201
202       bus_owner = (BusOwner *) link->data;
203       if(!strcmp(bus_connection_get_name(bus_owner->conn), unique_name))
204           return bus_owner->conn;
205
206       link = _dbus_list_get_next_link (&service->owners, link);
207     }
208
209   return NULL;
210 }
211 #endif
212
213 static void
214 bus_owner_set_flags (BusOwner *owner,
215                      dbus_uint32_t flags)
216 {
217    owner->allow_replacement = 
218         (flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT) != FALSE;
219
220    owner->do_not_queue =
221         (flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) != FALSE;
222
223 #ifdef ENABLE_KDBUS_TRANSPORT
224    owner->is_kdbus_starter =
225         (flags & KDBUS_NAME_STARTER) != FALSE;
226 #endif
227 }
228
229 static BusOwner *
230 bus_owner_new (BusService *service, 
231                DBusConnection *conn, 
232                dbus_uint32_t flags)
233 {
234   BusOwner *result;
235
236   result = _dbus_mem_pool_alloc (service->registry->owner_pool);
237   if (result != NULL)
238     {
239       result->refcount = 1;
240       /* don't ref the connection because we don't want
241          to block the connection from going away.
242          transactions take care of reffing the connection
243          but we need to use refcounting on the owner
244          so that the owner does not get freed before
245          we can deref the connection in the transaction
246        */
247       result->conn = conn;
248       result->service = service;
249
250       if (!bus_connection_add_owned_service (conn, service))
251         {
252           _dbus_mem_pool_dealloc (service->registry->owner_pool, result);
253           return NULL;
254         }
255         
256       bus_owner_set_flags (result, flags);
257     }
258   return result;
259 }
260
261 static BusOwner *
262 bus_owner_ref (BusOwner *owner)
263 {
264   _dbus_assert (owner->refcount > 0);
265   owner->refcount += 1;
266
267   return owner;
268 }
269
270 static void
271 bus_owner_unref  (BusOwner *owner)
272 {
273   _dbus_assert (owner->refcount > 0);
274   owner->refcount -= 1;
275
276   if (owner->refcount == 0)
277     {
278       bus_connection_remove_owned_service (owner->conn, owner->service);
279       _dbus_mem_pool_dealloc (owner->service->registry->owner_pool, owner);
280     }
281 }
282
283 BusService*
284 bus_registry_ensure (BusRegistry               *registry,
285                      const DBusString          *service_name,
286                      DBusConnection            *owner_connection_if_created,
287                      dbus_uint32_t              flags,
288                      BusTransaction            *transaction,
289                      DBusError                 *error)
290 {
291   BusService *service;
292
293   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
294   
295   _dbus_assert (owner_connection_if_created != NULL);
296   _dbus_assert (transaction != NULL);
297
298   service = _dbus_hash_table_lookup_string (registry->service_hash,
299                                             _dbus_string_get_const_data (service_name));
300   if (service != NULL)
301     return service;
302   
303   service = _dbus_mem_pool_alloc (registry->service_pool);
304   if (service == NULL)
305     {
306       BUS_SET_OOM (error);
307       return NULL;
308     }
309
310   service->registry = registry;  
311   service->refcount = 1;
312
313   _dbus_verbose ("copying string %p '%s' to service->name\n",
314                  service_name, _dbus_string_get_const_data (service_name));
315   if (!_dbus_string_copy_data (service_name, &service->name))
316     {
317       _dbus_mem_pool_dealloc (registry->service_pool, service);
318       BUS_SET_OOM (error);
319       return NULL;
320     }
321   _dbus_verbose ("copied string %p '%s' to '%s'\n",
322                  service_name, _dbus_string_get_const_data (service_name),
323                  service->name);
324
325   if (!bus_driver_send_service_owner_changed (service->name, 
326                                               NULL,
327                                               bus_connection_get_name (owner_connection_if_created),
328                                               transaction, error))
329     {
330       bus_service_unref (service);
331       return NULL;
332     }
333
334   if (!bus_activation_service_created (bus_context_get_activation (registry->context),
335                                        service->name, transaction, error))
336     {
337       bus_service_unref (service);
338       return NULL;
339     }
340   
341   if (!bus_service_add_owner (service, owner_connection_if_created, flags,
342                                               transaction, error))
343     {
344       bus_service_unref (service);
345       return NULL;
346     }
347   
348   if (!_dbus_hash_table_insert_string (registry->service_hash,
349                                        service->name,
350                                        service))
351     {
352       /* The add_owner gets reverted on transaction cancel */
353       BUS_SET_OOM (error);
354       return NULL;
355     }
356
357   return service;
358 }
359
360 void
361 bus_registry_foreach (BusRegistry               *registry,
362                       BusServiceForeachFunction  function,
363                       void                      *data)
364 {
365   DBusHashIter iter;
366   
367   _dbus_hash_iter_init (registry->service_hash, &iter);
368   while (_dbus_hash_iter_next (&iter))
369     {
370       BusService *service = _dbus_hash_iter_get_value (&iter);
371
372       (* function) (service, data);
373     }
374 }
375
376 dbus_bool_t
377 bus_registry_list_services (BusRegistry *registry,
378                             char      ***listp,
379                             int         *array_len)
380 {
381   int i, j, len;
382   char **retval;
383   DBusHashIter iter;
384    
385   len = _dbus_hash_table_get_n_entries (registry->service_hash);
386   retval = dbus_new (char *, len + 1);
387
388   if (retval == NULL)
389     return FALSE;
390
391   _dbus_hash_iter_init (registry->service_hash, &iter);
392   i = 0;
393   while (_dbus_hash_iter_next (&iter))
394     {
395       BusService *service = _dbus_hash_iter_get_value (&iter);
396
397       retval[i] = _dbus_strdup (service->name);
398       if (retval[i] == NULL)
399         goto error;
400
401       i++;
402     }
403
404   retval[i] = NULL;
405   
406   if (array_len)
407     *array_len = len;
408   
409   *listp = retval;
410   return TRUE;
411   
412  error:
413   for (j = 0; j < i; j++)
414     dbus_free (retval[j]);
415   dbus_free (retval);
416
417   return FALSE;
418 }
419
420 dbus_bool_t
421 bus_registry_acquire_service (BusRegistry      *registry,
422                               DBusConnection   *connection,
423                               const DBusString *service_name,
424                               dbus_uint32_t     flags,
425                               dbus_uint32_t    *result,
426                               BusTransaction   *transaction,
427                               DBusError        *error)
428 {
429   dbus_bool_t retval;
430   DBusConnection *old_owner_conn;
431   BusClientPolicy *policy;
432   BusService *service;
433   BusActivation  *activation;
434   BusSELinuxID *sid;
435   BusOwner *primary_owner;
436  
437   retval = FALSE;
438
439   if (!_dbus_validate_bus_name (service_name, 0,
440                                 _dbus_string_get_length (service_name)))
441     {
442       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
443                       "Requested bus name \"%s\" is not valid",
444                       _dbus_string_get_const_data (service_name));
445       
446       _dbus_verbose ("Attempt to acquire invalid service name\n");
447       
448       goto out;
449     }
450   
451   if (_dbus_string_get_byte (service_name, 0) == ':')
452     {
453       /* Not allowed; only base services can start with ':' */
454       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
455                       "Cannot acquire a service starting with ':' such as \"%s\"",
456                       _dbus_string_get_const_data (service_name));
457       
458       _dbus_verbose ("Attempt to acquire invalid base service name \"%s\"",
459                      _dbus_string_get_const_data (service_name));
460       
461       goto out;
462     }
463
464   if (_dbus_string_equal_c_str (service_name, DBUS_SERVICE_DBUS))
465     {
466       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
467                       "Connection \"%s\" is not allowed to own the service \"%s\"because "
468                       "it is reserved for D-Bus' use only",
469                       bus_connection_is_active (connection) ?
470                       bus_connection_get_name (connection) :
471                       "(inactive)",
472                       DBUS_SERVICE_DBUS);
473       goto out;
474     }
475
476   policy = bus_connection_get_policy (connection);
477   _dbus_assert (policy != NULL);
478
479   /* Note that if sid is #NULL then the bus's own context gets used
480    * in bus_connection_selinux_allows_acquire_service()
481    */
482   sid = bus_selinux_id_table_lookup (registry->service_sid_table,
483                                                                          service_name);
484
485   if (!bus_selinux_allows_acquire_service (connection, sid,
486                                            _dbus_string_get_const_data (service_name), error))
487         {
488
489           if (dbus_error_is_set (error) &&
490           dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
491         {
492           goto out;
493         }
494
495           dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
496                                           "Connection \"%s\" is not allowed to own the service \"%s\" due "
497                                           "to SELinux policy",
498                                           bus_connection_is_active (connection) ?
499                                           bus_connection_get_name (connection) :
500                                           "(inactive)",
501                                           _dbus_string_get_const_data (service_name));
502           goto out;
503         }
504
505   if (!bus_client_policy_check_can_own (policy, service_name))
506         {
507           dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
508                                           "Connection \"%s\" is not allowed to own the service \"%s\" due "
509                                           "to security policies in the configuration file",
510                                           bus_connection_is_active (connection) ?
511                                           bus_connection_get_name (connection) :
512                                           "(inactive)",
513                                           _dbus_string_get_const_data (service_name));
514           goto out;
515         }
516
517   if (bus_connection_get_n_services_owned (connection) >=
518           bus_context_get_max_services_per_connection (registry->context))
519         {
520           dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
521                                           "Connection \"%s\" is not allowed to own more services "
522                                           "(increase limits in configuration file if required)",
523                                           bus_connection_is_active (connection) ?
524                                           bus_connection_get_name (connection) :
525                                           "(inactive)");
526           goto out;
527         }
528
529   service = bus_registry_lookup (registry, service_name);
530
531   if (service != NULL)
532         {
533           primary_owner = bus_service_get_primary_owner (service);
534           if (primary_owner != NULL)
535                 old_owner_conn = primary_owner->conn;
536           else
537                 old_owner_conn = NULL;
538         }
539   else
540         old_owner_conn = NULL;
541
542   if (service == NULL)
543         {
544           service = bus_registry_ensure (registry,
545                                                                          service_name, connection, flags,
546                                                                          transaction, error);
547           if (service == NULL)
548                 goto out;
549         }
550
551   primary_owner = bus_service_get_primary_owner (service);
552   if (primary_owner == NULL)
553         goto out;
554
555   if (old_owner_conn == NULL)
556         {
557           _dbus_assert (primary_owner->conn == connection);
558
559           *result = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
560         }
561   else if (old_owner_conn == connection)
562         {
563           bus_owner_set_flags (primary_owner, flags);
564           *result = DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
565         }
566   else if (((flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) &&
567                    !(bus_service_get_allow_replacement (service))) ||
568            ((flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) &&
569                    !(flags & DBUS_NAME_FLAG_REPLACE_EXISTING)))
570         {
571           DBusList *link;
572           BusOwner *temp_owner;
573         /* Since we can't be queued if we are already in the queue
574            remove us */
575
576           link = _bus_service_find_owner_link (service, connection);
577           if (link != NULL)
578                 {
579                   _dbus_list_unlink (&service->owners, link);
580                   temp_owner = (BusOwner *)link->data;
581                   bus_owner_unref (temp_owner);
582                   _dbus_list_free_link (link);
583                 }
584
585           *result = DBUS_REQUEST_NAME_REPLY_EXISTS;
586         }
587   else if (!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) &&
588                    (!(flags & DBUS_NAME_FLAG_REPLACE_EXISTING) ||
589                 !(bus_service_get_allow_replacement (service))))
590         {
591           /* Queue the connection */
592           if (!bus_service_add_owner (service, connection,
593                                                                   flags,
594                                                                   transaction, error))
595                 goto out;
596
597           *result = DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
598         }
599   else
600         {
601           /* Replace the current owner */
602
603           /* We enqueue the new owner and remove the first one because
604            * that will cause NameAcquired and NameLost messages to
605            * be sent.
606            */
607
608           if (!bus_service_add_owner (service, connection,
609                                                                   flags,
610                                                                   transaction, error))
611                 goto out;
612
613           if (primary_owner->do_not_queue)
614                 {
615                   if (!bus_service_remove_owner (service, old_owner_conn,
616                                                                                  transaction, error))
617                         goto out;
618                 }
619           else
620                 {
621                   if (!bus_service_swap_owner (service, old_owner_conn,
622                                                                            transaction, error))
623                         goto out;
624                 }
625
626
627           _dbus_assert (connection == bus_service_get_primary_owner (service)->conn);
628           *result = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
629         }
630
631   activation = bus_context_get_activation (registry->context);
632   retval = bus_activation_send_pending_auto_activation_messages (activation,
633                                                                  service,
634                                                                  transaction,
635                                                                  error);
636
637  out:
638   return retval;
639 }
640
641 #ifdef ENABLE_KDBUS_TRANSPORT
642 dbus_bool_t
643 bus_registry_acquire_kdbus_service (BusRegistry      *registry,
644                               DBusConnection   *connection,
645                               DBusMessage *message,
646                               dbus_uint32_t    *result,
647                               BusTransaction   *transaction,
648                               DBusError        *error)
649 {
650   dbus_bool_t retval;
651   BusService *service;
652   BusActivation  *activation;
653
654   DBusString service_name_real;
655   const DBusString *service_name = &service_name_real;
656   char* name;
657   dbus_uint32_t flags;
658   __u64 sender_id;
659   dbus_bool_t rm_owner_daemon = FALSE;
660   const char* conn_unique_name;
661
662   if (!dbus_message_get_args (message, error,
663                               DBUS_TYPE_STRING, &name,
664                               DBUS_TYPE_UINT32, &flags,
665                               DBUS_TYPE_INVALID))
666     return FALSE;
667
668   retval = FALSE;
669
670   _dbus_string_init_const (&service_name_real, name);
671
672   if (!_dbus_validate_bus_name (service_name, 0,
673                                 _dbus_string_get_length (service_name)))
674     {
675       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
676                       "Requested bus name \"%s\" is not valid",
677                       _dbus_string_get_const_data (service_name));
678
679       _dbus_verbose ("Attempt to acquire invalid service name\n");
680
681       goto out;
682     }
683
684   if (_dbus_string_get_byte (service_name, 0) == ':')
685     {
686       /* Not allowed; only base services can start with ':' */
687       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
688                       "Cannot acquire a service starting with ':' such as \"%s\"",
689                       _dbus_string_get_const_data (service_name));
690
691       _dbus_verbose ("Attempt to acquire invalid base service name \"%s\"",
692                      _dbus_string_get_const_data (service_name));
693
694       goto out;
695     }
696
697   conn_unique_name = dbus_message_get_sender(message);
698
699   if (_dbus_string_equal_c_str (service_name, DBUS_SERVICE_DBUS))
700     {
701       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
702                       "Connection \"%s\" is not allowed to own the service \"%s\"because "
703                       "it is reserved for D-Bus' use only",
704                       conn_unique_name, DBUS_SERVICE_DBUS);
705       goto out;
706     }
707
708   if (!bus_client_policy_check_can_own (bus_connection_get_policy (connection), service_name))
709         {
710           dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
711                                           "Connection \"%s\" is not allowed to own the service \"%s\" due "
712                                           "to security policies in the configuration file",
713                                           conn_unique_name, _dbus_string_get_const_data (service_name));
714           goto out;
715         }
716
717         service = bus_registry_lookup (registry, service_name);
718         if (service == NULL)
719         {
720                 service = bus_registry_ensure (registry, service_name, connection, flags,
721                                                                          transaction, error);  //adds daemon to service owners list - must be removed after right owner is set
722                 if (service == NULL)
723                   goto out;
724
725                 rm_owner_daemon = TRUE;
726                 if(!kdbus_register_policy(service_name, connection))
727                 {
728                         dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
729                                                   "Kdbus error when setting policy for connection \"%s\" and  service name \"%s\"",
730                                                   conn_unique_name, _dbus_string_get_const_data (service_name));
731                         goto failed;
732                 }
733         }
734
735         sender_id = sender_name_to_id(conn_unique_name, error);
736         if(dbus_error_is_set(error))
737                 goto failed;
738
739         *result = kdbus_request_name(connection, service_name, flags, sender_id);
740         if(*result == -EPERM)
741         {
742                 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
743                                           "Kdbus not allowed to own the service \"%s\"",
744                                           _dbus_string_get_const_data (service_name));
745                 goto failed;
746         }
747         else if(*result < 0)
748         {
749                 dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
750                 goto failed;
751         }
752
753         if((*result == DBUS_REQUEST_NAME_REPLY_IN_QUEUE) || (*result == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER))
754         {
755             DBusConnection* phantom;
756             const char* name;
757 //          DBusList *link;
758
759             name = dbus_message_get_sender(message);
760             phantom = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
761         if(phantom == NULL)
762             phantom = create_phantom_connection(connection, name, error);
763             if(phantom == NULL)
764                 goto failed2;
765             if (!bus_service_add_owner (service, phantom, flags, transaction, error))
766                 goto failed2;
767             if((*result == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) && rm_owner_daemon)
768             {
769             /* Here we are removing DBus daemon as an owner of the service,
770              * which is set by bus_registry_ensure.
771              * If bus_service_remove_owner fail, we ignore it, because it has
772              * almost none impact on the usage
773              */
774                 if(_bus_service_find_owner_link (service, connection))
775                 bus_service_remove_owner (service, connection, transaction, NULL);
776             }
777             /*if(bus_service_get_is_kdbus_starter(service))
778             {
779                 if (!bus_service_swap_owner (service, bus_service_get_primary_owners_connection(service),
780                                                transaction, error))
781                     goto failed2;
782             }*/
783             /*if((link = _bus_service_find_owner_link (service, connection)))  //if daemon is a starter
784             {
785                 _dbus_list_unlink (&service->owners, link);
786                 _dbus_list_append_link (&service->owners, link);  //it must be moved at the end of the queue
787             }*/
788         }
789
790   activation = bus_context_get_activation (registry->context);
791   retval = bus_activation_send_pending_auto_activation_messages (activation,
792                                                                  service,
793                                                                  transaction,
794                                                                  error);
795  out:
796      return retval;
797   
798 failed2:
799     kdbus_release_name(connection, service_name, sender_id);
800 failed:
801     if(_bus_service_find_owner_link (service, connection))
802         bus_service_remove_owner (service, connection, transaction, NULL);
803
804   return FALSE;
805 }
806 #endif
807
808 dbus_bool_t
809 bus_registry_release_service (BusRegistry      *registry,
810                               DBusConnection   *connection,
811                               const DBusString *service_name,
812                               dbus_uint32_t    *result,
813                               BusTransaction   *transaction,
814                               DBusError        *error)
815 {
816   dbus_bool_t retval;
817   BusService *service;
818
819   retval = FALSE;
820
821   if (!_dbus_validate_bus_name (service_name, 0,
822                                 _dbus_string_get_length (service_name)))
823     {
824       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
825                       "Given bus name \"%s\" is not valid",
826                       _dbus_string_get_const_data (service_name));
827
828       _dbus_verbose ("Attempt to release invalid service name\n");
829
830       goto out;
831     }
832
833   if (_dbus_string_get_byte (service_name, 0) == ':')
834     {
835       /* Not allowed; the base service name cannot be created or released */
836       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
837                       "Cannot release a service starting with ':' such as \"%s\"",
838                       _dbus_string_get_const_data (service_name));
839
840       _dbus_verbose ("Attempt to release invalid base service name \"%s\"",
841                      _dbus_string_get_const_data (service_name));
842
843       goto out;
844     }
845
846    if (_dbus_string_equal_c_str (service_name, DBUS_SERVICE_DBUS))
847     {
848       /* Not allowed; the base service name cannot be created or released */
849       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
850                       "Cannot release the %s service because it is owned by the bus",
851                      DBUS_SERVICE_DBUS);
852
853       _dbus_verbose ("Attempt to release service name \"%s\"",
854                      DBUS_SERVICE_DBUS);
855
856       goto out;
857     }
858
859   service = bus_registry_lookup (registry, service_name);
860
861   if (service == NULL)
862     {
863       *result = DBUS_RELEASE_NAME_REPLY_NON_EXISTENT;
864     }
865   else if (!bus_service_has_owner (service, connection))
866     {
867       *result = DBUS_RELEASE_NAME_REPLY_NOT_OWNER;
868     }
869   else
870     {
871       if (!bus_service_remove_owner (service, connection,
872                                      transaction, error))
873         goto out;
874
875       _dbus_assert (!bus_service_has_owner (service, connection));
876       *result = DBUS_RELEASE_NAME_REPLY_RELEASED;
877     }
878
879   retval = TRUE;
880
881  out:
882   return retval;
883 }
884
885 #ifdef ENABLE_KDBUS_TRANSPORT
886 dbus_bool_t
887 bus_registry_release_service_kdbus (const char* sender_name,
888                               DBusConnection   *connection,
889                               const DBusString *service_name,
890                               dbus_uint32_t    *result,
891                               BusTransaction   *transaction,
892                               DBusError        *error)
893 {
894   dbus_bool_t retval = FALSE;
895   __u64 sender_id;
896
897   if (!_dbus_validate_bus_name (service_name, 0,
898                                 _dbus_string_get_length (service_name)))
899     {
900       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
901                       "Given bus name \"%s\" is not valid",
902                       _dbus_string_get_const_data (service_name));
903
904       _dbus_verbose ("Attempt to release invalid service name\n");
905
906       goto out;
907     }
908
909   if (_dbus_string_get_byte (service_name, 0) == ':')
910     {
911       /* Not allowed; the base service name cannot be created or released */
912       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
913                       "Cannot release a service starting with ':' such as \"%s\"",
914                       _dbus_string_get_const_data (service_name));
915
916       _dbus_verbose ("Attempt to release invalid base service name \"%s\"",
917                      _dbus_string_get_const_data (service_name));
918
919       goto out;
920     }
921
922    if (_dbus_string_equal_c_str (service_name, DBUS_SERVICE_DBUS))
923     {
924       /* Not allowed; the base service name cannot be created or released */
925       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
926                       "Cannot release the %s service because it is owned by the bus",
927                      DBUS_SERVICE_DBUS);
928
929       _dbus_verbose ("Attempt to release service name \"%s\"",
930                      DBUS_SERVICE_DBUS);
931
932       goto out;
933     }
934
935     sender_id = sender_name_to_id(sender_name, error);
936     if(dbus_error_is_set(error))
937         return FALSE;
938
939     *result = kdbus_release_name(connection, service_name, sender_id);
940
941     if(*result == DBUS_RELEASE_NAME_REPLY_RELEASED)
942     {
943         BusRegistry* registry;
944         BusService *service;
945
946         registry = bus_connection_get_registry (connection);
947         service = bus_registry_lookup (registry, service_name);
948         if(service)
949         {
950             DBusConnection* phantom;
951
952             phantom = _bus_service_find_owner_connection(service, sender_name);
953             if(phantom)
954             {
955                 bus_service_remove_owner (service, phantom, transaction, NULL);
956                 /* todo we could remove phantom if he doesn't own any name
957                  * to do this we should write function in connection.c to check if
958                  * _dbus_list_get_last (&d->services_owned) returns not NULL
959                  *  or we can leave phantom - he will be removed when he disconnects from the bus
960                  */
961             }
962             else
963                 _dbus_verbose ("Didn't find phantom connection for released name!\n");
964         }
965    }
966
967   retval = TRUE;
968
969  out:
970   return retval;
971 }
972 #endif
973
974 dbus_bool_t
975 bus_registry_set_service_context_table (BusRegistry   *registry,
976                                         DBusHashTable *table)
977 {
978   DBusHashTable *new_table;
979   DBusHashIter iter;
980   
981   new_table = bus_selinux_id_table_new ();
982   if (!new_table)
983     return FALSE;
984
985   _dbus_hash_iter_init (table, &iter);
986   while (_dbus_hash_iter_next (&iter))
987     {
988       const char *service = _dbus_hash_iter_get_string_key (&iter);
989       const char *context = _dbus_hash_iter_get_value (&iter);
990
991       if (!bus_selinux_id_table_insert (new_table,
992                                         service,
993                                         context))
994         return FALSE;
995     }
996   
997   if (registry->service_sid_table)
998     _dbus_hash_table_unref (registry->service_sid_table);
999   registry->service_sid_table = new_table;
1000   return TRUE;
1001 }
1002
1003 static void
1004 bus_service_unlink_owner (BusService      *service,
1005                           BusOwner        *owner)
1006 {
1007   _dbus_list_remove_last (&service->owners, owner);
1008   bus_owner_unref (owner);
1009 }
1010
1011 static void
1012 bus_service_unlink (BusService *service)
1013 {
1014   _dbus_assert (service->owners == NULL);
1015
1016   /* the service may not be in the hash, if
1017    * the failure causing transaction cancel
1018    * was in the right place, but that's OK
1019    */
1020   _dbus_hash_table_remove_string (service->registry->service_hash,
1021                                   service->name);
1022   
1023   bus_service_unref (service);
1024 }
1025
1026 static void
1027 bus_service_relink (BusService           *service,
1028                     DBusPreallocatedHash *preallocated)
1029 {
1030   _dbus_assert (service->owners == NULL);
1031   _dbus_assert (preallocated != NULL);
1032
1033   _dbus_hash_table_insert_string_preallocated (service->registry->service_hash,
1034                                                preallocated,
1035                                                service->name,
1036                                                service);
1037   
1038   bus_service_ref (service);
1039 }
1040
1041 /**
1042  * Data used to represent an ownership cancellation in
1043  * a bus transaction.
1044  */
1045 typedef struct
1046 {
1047   BusOwner *owner;            /**< the owner */
1048   BusService *service;        /**< service to cancel ownership of */
1049 } OwnershipCancelData;
1050
1051 static void
1052 cancel_ownership (void *data)
1053 {
1054   OwnershipCancelData *d = data;
1055
1056   /* We don't need to send messages notifying of these
1057    * changes, since we're reverting something that was
1058    * cancelled (effectively never really happened)
1059    */
1060   bus_service_unlink_owner (d->service, d->owner);
1061   
1062   if (d->service->owners == NULL)
1063     bus_service_unlink (d->service);
1064 }
1065
1066 static void
1067 free_ownership_cancel_data (void *data)
1068 {
1069   OwnershipCancelData *d = data;
1070
1071   dbus_connection_unref (d->owner->conn);
1072   bus_owner_unref (d->owner);
1073   bus_service_unref (d->service);
1074   
1075   dbus_free (d);
1076 }
1077
1078 static dbus_bool_t
1079 add_cancel_ownership_to_transaction (BusTransaction *transaction,
1080                                      BusService     *service,
1081                                      BusOwner       *owner)
1082 {
1083   OwnershipCancelData *d;
1084
1085   d = dbus_new (OwnershipCancelData, 1);
1086   if (d == NULL)
1087     return FALSE;
1088   
1089   d->service = service;
1090   d->owner = owner;
1091
1092   if (!bus_transaction_add_cancel_hook (transaction, cancel_ownership, d,
1093                                         free_ownership_cancel_data))
1094     {
1095       dbus_free (d);
1096       return FALSE;
1097     }
1098
1099   bus_service_ref (d->service);
1100   bus_owner_ref (owner);
1101   dbus_connection_ref (d->owner->conn);
1102
1103   return TRUE;
1104 }
1105
1106 /* this function is self-cancelling if you cancel the transaction */
1107 dbus_bool_t
1108 bus_service_add_owner (BusService     *service,
1109                        DBusConnection *connection,
1110                        dbus_uint32_t  flags,
1111                        BusTransaction *transaction,
1112                        DBusError      *error)
1113 {
1114   BusOwner *bus_owner;
1115   DBusList *bus_owner_link;
1116   
1117   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1118   
1119  /* Send service acquired message first, OOM will result
1120   * in cancelling the transaction
1121   */
1122   if (service->owners == NULL)
1123     {
1124       if (!bus_driver_send_service_acquired (connection, service->name, transaction, error))
1125         return FALSE;
1126     }
1127   
1128   bus_owner_link = _bus_service_find_owner_link (service, connection);
1129   
1130   if (bus_owner_link == NULL)
1131     {
1132       bus_owner = bus_owner_new (service, connection, flags);
1133       if (bus_owner == NULL)
1134         {
1135           BUS_SET_OOM (error);
1136           return FALSE;
1137         }
1138
1139       bus_owner_set_flags (bus_owner, flags);
1140       if (!(flags & DBUS_NAME_FLAG_REPLACE_EXISTING) || service->owners == NULL)
1141         {
1142           if (!_dbus_list_append (&service->owners,
1143                                   bus_owner))
1144             {
1145               bus_owner_unref (bus_owner);
1146               BUS_SET_OOM (error);
1147               return FALSE;
1148             }
1149         }
1150       else
1151         {
1152           if (!_dbus_list_insert_after (&service->owners,
1153                                          _dbus_list_get_first_link (&service->owners),
1154                                          bus_owner))
1155             {
1156               bus_owner_unref (bus_owner);
1157               BUS_SET_OOM (error);
1158               return FALSE;
1159             }
1160         }      
1161     } 
1162   else 
1163     {
1164       /* Update the link since we are already in the queue
1165        * No need for operations that can produce OOM
1166        */
1167
1168       bus_owner = (BusOwner *) bus_owner_link->data;
1169       if (flags & DBUS_NAME_FLAG_REPLACE_EXISTING)
1170         {
1171           DBusList *link;
1172           _dbus_list_unlink (&service->owners, bus_owner_link);
1173           link = _dbus_list_get_first_link (&service->owners);
1174           _dbus_assert (link != NULL);
1175           
1176           _dbus_list_insert_after_link (&service->owners, link, bus_owner_link);
1177         }
1178       
1179       bus_owner_set_flags (bus_owner, flags);
1180       return TRUE;
1181     }
1182
1183   if (!add_cancel_ownership_to_transaction (transaction,
1184                                             service,
1185                                             bus_owner))
1186     {
1187       bus_service_unlink_owner (service, bus_owner);
1188       BUS_SET_OOM (error);
1189       return FALSE;
1190     }
1191
1192   return TRUE;
1193 }
1194
1195 typedef struct
1196 {
1197   BusOwner       *owner;
1198   BusService     *service;
1199   BusOwner       *before_owner; /* restore to position before this connection in owners list */
1200   DBusList       *owner_link;
1201   DBusList       *service_link;
1202   DBusPreallocatedHash *hash_entry;
1203 } OwnershipRestoreData;
1204
1205 static void
1206 restore_ownership (void *data)
1207 {
1208   OwnershipRestoreData *d = data;
1209   DBusList *link;
1210
1211   _dbus_assert (d->service_link != NULL);
1212   _dbus_assert (d->owner_link != NULL);
1213   
1214   if (d->service->owners == NULL)
1215     {
1216       _dbus_assert (d->hash_entry != NULL);
1217       bus_service_relink (d->service, d->hash_entry);
1218     }
1219   else
1220     {
1221       _dbus_assert (d->hash_entry == NULL);
1222     }
1223   
1224   /* We don't need to send messages notifying of these
1225    * changes, since we're reverting something that was
1226    * cancelled (effectively never really happened)
1227    */
1228   link = _dbus_list_get_first_link (&d->service->owners);
1229   while (link != NULL)
1230     {
1231       if (link->data == d->before_owner)
1232         break;
1233
1234       link = _dbus_list_get_next_link (&d->service->owners, link);
1235     }
1236   
1237   _dbus_list_insert_before_link (&d->service->owners, link, d->owner_link);
1238
1239   /* Note that removing then restoring this changes the order in which
1240    * ServiceDeleted messages are sent on destruction of the
1241    * connection.  This should be OK as the only guarantee there is
1242    * that the base service is destroyed last, and we never even
1243    * tentatively remove the base service.
1244    */
1245   bus_connection_add_owned_service_link (d->owner->conn, d->service_link);
1246   
1247   d->hash_entry = NULL;
1248   d->service_link = NULL;
1249   d->owner_link = NULL;
1250 }
1251
1252 static void
1253 free_ownership_restore_data (void *data)
1254 {
1255   OwnershipRestoreData *d = data;
1256
1257   if (d->service_link)
1258     _dbus_list_free_link (d->service_link);
1259   if (d->owner_link)
1260     _dbus_list_free_link (d->owner_link);
1261   if (d->hash_entry)
1262     _dbus_hash_table_free_preallocated_entry (d->service->registry->service_hash,
1263                                               d->hash_entry);
1264
1265   dbus_connection_unref (d->owner->conn);
1266   bus_owner_unref (d->owner);
1267   bus_service_unref (d->service);
1268   
1269   dbus_free (d);
1270 }
1271
1272 static dbus_bool_t
1273 add_restore_ownership_to_transaction (BusTransaction *transaction,
1274                                       BusService     *service,
1275                                       BusOwner       *owner)
1276 {
1277   OwnershipRestoreData *d;
1278   DBusList *link;
1279
1280   d = dbus_new (OwnershipRestoreData, 1);
1281   if (d == NULL)
1282     return FALSE;
1283   
1284   d->service = service;
1285   d->owner = owner;
1286   d->service_link = _dbus_list_alloc_link (service);
1287   d->owner_link = _dbus_list_alloc_link (owner);
1288   d->hash_entry = _dbus_hash_table_preallocate_entry (service->registry->service_hash);
1289   
1290   bus_service_ref (d->service);
1291   bus_owner_ref (d->owner);
1292   dbus_connection_ref (d->owner->conn);
1293
1294   d->before_owner = NULL;
1295   link = _dbus_list_get_first_link (&service->owners);
1296   while (link != NULL)
1297     {
1298       if (link->data == owner)
1299         {
1300           link = _dbus_list_get_next_link (&service->owners, link);
1301
1302           if (link)
1303             d->before_owner = link->data;
1304
1305           break;
1306         }
1307       
1308       link = _dbus_list_get_next_link (&service->owners, link);
1309     }
1310   
1311   if (d->service_link == NULL ||
1312       d->owner_link == NULL ||
1313       d->hash_entry == NULL ||
1314       !bus_transaction_add_cancel_hook (transaction, restore_ownership, d,
1315                                         free_ownership_restore_data))
1316     {
1317       free_ownership_restore_data (d);
1318       return FALSE;
1319     }
1320   
1321   return TRUE;
1322 }
1323
1324 dbus_bool_t
1325 bus_service_swap_owner (BusService     *service,
1326                         DBusConnection *connection,
1327                         BusTransaction *transaction,
1328                         DBusError      *error)
1329 {
1330   DBusList *swap_link;
1331   BusOwner *primary_owner;
1332
1333   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1334
1335   /* We send out notifications before we do any work we
1336    * might have to undo if the notification-sending failed
1337    */
1338   
1339   /* Send service lost message */
1340   primary_owner = bus_service_get_primary_owner (service);
1341   if (primary_owner == NULL || primary_owner->conn != connection)
1342     _dbus_assert_not_reached ("Tried to swap a non primary owner");
1343
1344     
1345   if (!bus_driver_send_service_lost (connection, service->name,
1346                                      transaction, error))
1347     return FALSE;
1348
1349   if (service->owners == NULL)
1350     {
1351       _dbus_assert_not_reached ("Tried to swap owner of a service that has no owners");
1352     }
1353   else if (_dbus_list_length_is_one (&service->owners))
1354     {
1355       _dbus_assert_not_reached ("Tried to swap owner of a service that has no other owners in the queue");
1356     }
1357   else
1358     {
1359       DBusList *link;
1360       BusOwner *new_owner;
1361       DBusConnection *new_owner_conn;
1362       link = _dbus_list_get_first_link (&service->owners);
1363       _dbus_assert (link != NULL);
1364       link = _dbus_list_get_next_link (&service->owners, link);
1365       _dbus_assert (link != NULL);
1366
1367       new_owner = (BusOwner *)link->data;
1368       new_owner_conn = new_owner->conn;
1369
1370       if (!bus_driver_send_service_owner_changed (service->name,
1371                                                   bus_connection_get_name (connection),
1372                                                   bus_connection_get_name (new_owner_conn),
1373                                                   transaction, error))
1374         return FALSE;
1375
1376       /* This will be our new owner */
1377       if (!bus_driver_send_service_acquired (new_owner_conn,
1378                                              service->name,
1379                                              transaction,
1380                                              error))
1381         return FALSE;
1382     }
1383
1384   if (!add_restore_ownership_to_transaction (transaction, service, primary_owner))
1385     {
1386       BUS_SET_OOM (error);
1387       return FALSE;
1388     }
1389
1390   /* unlink the primary and make it the second link */
1391   swap_link = _dbus_list_get_first_link (&service->owners);
1392   _dbus_list_unlink (&service->owners, swap_link);
1393
1394   _dbus_list_insert_after_link (&service->owners,
1395                                 _dbus_list_get_first_link (&service->owners),
1396                                 swap_link);
1397
1398   return TRUE;
1399 }
1400
1401 /* this function is self-cancelling if you cancel the transaction */
1402 dbus_bool_t
1403 bus_service_remove_owner (BusService     *service,
1404                           DBusConnection *connection,
1405                           BusTransaction *transaction,
1406                           DBusError      *error)
1407 {
1408   BusOwner *primary_owner;
1409   
1410   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1411   
1412   /* We send out notifications before we do any work we
1413    * might have to undo if the notification-sending failed
1414    */
1415   
1416   /* Send service lost message */
1417   primary_owner = bus_service_get_primary_owner (service);
1418   if (primary_owner != NULL && primary_owner->conn == connection)
1419     {
1420       if (!bus_driver_send_service_lost (connection, service->name,
1421                                          transaction, error))
1422         return FALSE;
1423     }
1424   else
1425     {
1426       /* if we are not the primary owner then just remove us from the queue */
1427       DBusList *link;
1428       BusOwner *temp_owner;
1429
1430       link = _bus_service_find_owner_link (service, connection);
1431       _dbus_list_unlink (&service->owners, link);
1432       temp_owner = (BusOwner *)link->data;
1433       bus_owner_unref (temp_owner); 
1434       _dbus_list_free_link (link);
1435
1436       return TRUE; 
1437     }
1438
1439   if (service->owners == NULL)
1440     {
1441       _dbus_assert_not_reached ("Tried to remove owner of a service that has no owners");
1442     }
1443   else if (_dbus_list_length_is_one (&service->owners))
1444     {
1445       if (!bus_driver_send_service_owner_changed (service->name,
1446                                                   bus_connection_get_name (connection),
1447                                                   NULL,
1448                                                   transaction, error))
1449         return FALSE;
1450     }
1451   else
1452     {
1453       DBusList *link;
1454       BusOwner *new_owner;
1455       DBusConnection *new_owner_conn;
1456       link = _dbus_list_get_first_link (&service->owners);
1457       _dbus_assert (link != NULL);
1458       link = _dbus_list_get_next_link (&service->owners, link);
1459       _dbus_assert (link != NULL);
1460
1461       new_owner = (BusOwner *)link->data;
1462       new_owner_conn = new_owner->conn;
1463
1464       if (!bus_driver_send_service_owner_changed (service->name,
1465                                                   bus_connection_get_name (connection),
1466                                                   bus_connection_get_name (new_owner_conn),
1467                                                   transaction, error))
1468         return FALSE;
1469
1470       /* This will be our new owner */
1471       if (!bus_driver_send_service_acquired (new_owner_conn,
1472                                              service->name,
1473                                              transaction,
1474                                              error))
1475         return FALSE;
1476     }
1477
1478   if (!add_restore_ownership_to_transaction (transaction, service, primary_owner))
1479     {
1480       BUS_SET_OOM (error);
1481       return FALSE;
1482     }
1483  
1484   bus_service_unlink_owner (service, primary_owner);
1485
1486   if (service->owners == NULL)
1487     bus_service_unlink (service);
1488
1489   return TRUE;
1490 }
1491
1492 BusService *
1493 bus_service_ref (BusService *service)
1494 {
1495   _dbus_assert (service->refcount > 0);
1496   
1497   service->refcount += 1;
1498
1499   return service;
1500 }
1501
1502 void
1503 bus_service_unref (BusService *service)
1504 {
1505   _dbus_assert (service->refcount > 0);
1506   
1507   service->refcount -= 1;
1508
1509   if (service->refcount == 0)
1510     {
1511       _dbus_assert (service->owners == NULL);
1512       
1513       dbus_free (service->name);
1514       _dbus_mem_pool_dealloc (service->registry->service_pool, service);
1515     }
1516 }
1517
1518 DBusConnection *
1519 bus_service_get_primary_owners_connection (BusService *service)
1520 {
1521   BusOwner *owner;
1522 #ifdef ENABLE_KDBUS_TRANSPORT
1523   char unique_name[(unsigned int)(snprintf((char*)NULL, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
1524 #endif
1525
1526   owner = bus_service_get_primary_owner (service);
1527
1528 #ifdef ENABLE_KDBUS_TRANSPORT
1529   if(kdbus_get_name_owner(owner->conn, bus_service_get_name(service), unique_name) < 0)
1530     return NULL;
1531   return _bus_service_find_owner_connection(service, unique_name);  //bus_connections_find_conn_by_name would be safer? but slower
1532 #else
1533   if (owner != NULL)
1534     return owner->conn;
1535   else
1536     return NULL;
1537 #endif
1538 }
1539
1540 BusOwner*
1541 bus_service_get_primary_owner (BusService *service)
1542 {
1543   return _dbus_list_get_first (&service->owners);
1544 }
1545
1546 const char*
1547 bus_service_get_name (BusService *service)
1548 {
1549   return service->name;
1550 }
1551
1552 dbus_bool_t
1553 bus_service_get_allow_replacement (BusService *service)
1554 {
1555   BusOwner *owner;
1556   DBusList *link;
1557  
1558   _dbus_assert (service->owners != NULL);
1559
1560   link = _dbus_list_get_first_link (&service->owners);
1561   owner = (BusOwner *) link->data;
1562
1563   return owner->allow_replacement;
1564 }
1565
1566 #ifdef ENABLE_KDBUS_TRANSPORT
1567 dbus_bool_t
1568 bus_service_get_is_kdbus_starter (BusService *service)
1569 {
1570   BusOwner *owner;
1571   DBusList *link;
1572
1573   _dbus_assert (service->owners != NULL);
1574
1575   link = _dbus_list_get_first_link (&service->owners);
1576   owner = (BusOwner *) link->data;
1577
1578   return owner->is_kdbus_starter;
1579 }
1580 #endif
1581
1582 dbus_bool_t
1583 bus_service_has_owner (BusService     *service,
1584                        DBusConnection *connection)
1585 {
1586   DBusList *link;
1587
1588   link = _bus_service_find_owner_link (service, connection);
1589  
1590   if (link == NULL)
1591     return FALSE;
1592   else
1593     return TRUE;
1594 }
1595
1596 dbus_bool_t 
1597 bus_service_list_queued_owners (BusService *service,
1598                                 DBusList  **return_list,
1599                                 DBusError  *error)
1600 {
1601   DBusList *link;
1602
1603   _dbus_assert (*return_list == NULL);
1604
1605   link = _dbus_list_get_first_link (&service->owners);
1606   _dbus_assert (link != NULL);
1607   
1608   while (link != NULL)
1609     {
1610       BusOwner *owner;
1611       const char *uname;
1612
1613       owner = (BusOwner *) link->data;
1614       uname = bus_connection_get_name (owner->conn);
1615
1616 #ifdef ENABLE_KDBUS_TRANSPORT
1617       if(!owner->is_kdbus_starter)
1618 #endif
1619           if (!_dbus_list_append (return_list, (char *)uname))
1620               goto oom;
1621
1622       link = _dbus_list_get_next_link (&service->owners, link);
1623     }
1624   
1625   return TRUE;
1626   
1627  oom:
1628   _dbus_list_clear (return_list);
1629   BUS_SET_OOM (error);
1630   return FALSE;
1631 }