2f18f1d14b941008e088abdbf259d6dd6f2d826c
[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   DBusString service_name_real;
654   const DBusString *service_name = &service_name_real;
655   char* name;
656   dbus_uint32_t flags;
657   __u64 sender_id;
658   const char* conn_unique_name;
659   DBusConnection* phantom;
660
661   if (!dbus_message_get_args (message, error,
662                               DBUS_TYPE_STRING, &name,
663                               DBUS_TYPE_UINT32, &flags,
664                               DBUS_TYPE_INVALID))
665     return FALSE;
666
667   retval = FALSE;
668
669   _dbus_string_init_const (&service_name_real, name);
670
671   if (!_dbus_validate_bus_name (service_name, 0,
672                                 _dbus_string_get_length (service_name)))
673     {
674       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
675                       "Requested bus name \"%s\" is not valid",
676                       _dbus_string_get_const_data (service_name));
677
678       _dbus_verbose ("Attempt to acquire invalid service name\n");
679
680       return FALSE;
681     }
682
683   if (_dbus_string_get_byte (service_name, 0) == ':')
684     {
685       /* Not allowed; only base services can start with ':' */
686       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
687                       "Cannot acquire a service starting with ':' such as \"%s\"",
688                       _dbus_string_get_const_data (service_name));
689
690       _dbus_verbose ("Attempt to acquire invalid base service name \"%s\"",
691                      _dbus_string_get_const_data (service_name));
692
693       return FALSE;
694     }
695
696   conn_unique_name = dbus_message_get_sender(message);
697
698   if (_dbus_string_equal_c_str (service_name, DBUS_SERVICE_DBUS))
699     {
700       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
701                       "Connection \"%s\" is not allowed to own the service \"%s\"because "
702                       "it is reserved for D-Bus' use only",
703                       conn_unique_name, DBUS_SERVICE_DBUS);
704       return FALSE;
705     }
706
707   sender_id = sender_name_to_id(conn_unique_name, error);
708   if(dbus_error_is_set(error))
709     return FALSE;
710
711   phantom = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), conn_unique_name);
712   if(phantom == NULL)
713     {
714       phantom = create_phantom_connection(connection, conn_unique_name, error);
715       if(phantom == NULL)
716         return FALSE;
717     }
718
719   if (!bus_client_policy_check_can_own (bus_connection_get_policy (phantom), service_name))
720         {
721           dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
722                                           "Connection \"%s\" is not allowed to own the service \"%s\" due "
723                                           "to security policies in the configuration file",
724                                           conn_unique_name, _dbus_string_get_const_data (service_name));
725           goto failed;
726         }
727
728   if(!kdbus_register_policy(service_name, phantom))
729   {
730     dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
731             "Kdbus error when setting policy for connection \"%s\" and  service name \"%s\"",
732             conn_unique_name, _dbus_string_get_const_data (service_name));
733     goto failed;
734   }
735
736         *result = kdbus_request_name(connection, service_name, flags, sender_id);
737         if(*result == -EPERM)
738         {
739                 dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
740                                           "Kdbus not allowed %s to own the service \"%s\"",
741                                           conn_unique_name, _dbus_string_get_const_data (service_name));
742                 goto failed;
743         }
744         else if(*result < 0)
745         {
746                 dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
747                 goto failed;
748         }
749
750         if((*result == DBUS_REQUEST_NAME_REPLY_IN_QUEUE) || (*result == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER))
751         {
752     service = bus_registry_lookup (registry, service_name);
753     if (service == NULL)
754       {
755         service = bus_registry_ensure (registry, service_name, phantom, flags,
756                        transaction, error);
757         if (service == NULL)
758           goto failed2;
759       }
760     else
761       {
762         if (!bus_service_add_owner (service, phantom, flags, transaction, error))
763           goto failed2;
764       }
765
766     activation = bus_context_get_activation (registry->context);
767     retval = bus_activation_send_pending_auto_activation_messages (activation,
768                    service,
769                    transaction,
770                    error);
771         }
772         else
773           retval = TRUE;
774
775   return retval;
776   
777 failed2:
778   kdbus_release_name(phantom, service_name, sender_id);
779 failed:
780   bus_connection_disconnected(phantom);
781
782   return FALSE;
783 }
784 #endif
785
786 dbus_bool_t
787 bus_registry_release_service (BusRegistry      *registry,
788                               DBusConnection   *connection,
789                               const DBusString *service_name,
790                               dbus_uint32_t    *result,
791                               BusTransaction   *transaction,
792                               DBusError        *error)
793 {
794   dbus_bool_t retval;
795   BusService *service;
796
797   retval = FALSE;
798
799   if (!_dbus_validate_bus_name (service_name, 0,
800                                 _dbus_string_get_length (service_name)))
801     {
802       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
803                       "Given bus name \"%s\" is not valid",
804                       _dbus_string_get_const_data (service_name));
805
806       _dbus_verbose ("Attempt to release invalid service name\n");
807
808       goto out;
809     }
810
811   if (_dbus_string_get_byte (service_name, 0) == ':')
812     {
813       /* Not allowed; the base service name cannot be created or released */
814       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
815                       "Cannot release a service starting with ':' such as \"%s\"",
816                       _dbus_string_get_const_data (service_name));
817
818       _dbus_verbose ("Attempt to release invalid base service name \"%s\"",
819                      _dbus_string_get_const_data (service_name));
820
821       goto out;
822     }
823
824    if (_dbus_string_equal_c_str (service_name, DBUS_SERVICE_DBUS))
825     {
826       /* Not allowed; the base service name cannot be created or released */
827       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
828                       "Cannot release the %s service because it is owned by the bus",
829                      DBUS_SERVICE_DBUS);
830
831       _dbus_verbose ("Attempt to release service name \"%s\"",
832                      DBUS_SERVICE_DBUS);
833
834       goto out;
835     }
836
837   service = bus_registry_lookup (registry, service_name);
838
839   if (service == NULL)
840     {
841       *result = DBUS_RELEASE_NAME_REPLY_NON_EXISTENT;
842     }
843   else if (!bus_service_has_owner (service, connection))
844     {
845       *result = DBUS_RELEASE_NAME_REPLY_NOT_OWNER;
846     }
847   else
848     {
849       if (!bus_service_remove_owner (service, connection,
850                                      transaction, error))
851         goto out;
852
853       _dbus_assert (!bus_service_has_owner (service, connection));
854       *result = DBUS_RELEASE_NAME_REPLY_RELEASED;
855     }
856
857   retval = TRUE;
858
859  out:
860   return retval;
861 }
862
863 #ifdef ENABLE_KDBUS_TRANSPORT
864 dbus_bool_t
865 bus_registry_release_service_kdbus (const char* sender_name,
866                               DBusConnection   *connection,
867                               const DBusString *service_name,
868                               dbus_uint32_t    *result,
869                               BusTransaction   *transaction,
870                               DBusError        *error)
871 {
872   dbus_bool_t retval = FALSE;
873   __u64 sender_id;
874
875   if (!_dbus_validate_bus_name (service_name, 0,
876                                 _dbus_string_get_length (service_name)))
877     {
878       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
879                       "Given bus name \"%s\" is not valid",
880                       _dbus_string_get_const_data (service_name));
881
882       _dbus_verbose ("Attempt to release invalid service name\n");
883
884       goto out;
885     }
886
887   if (_dbus_string_get_byte (service_name, 0) == ':')
888     {
889       /* Not allowed; the base service name cannot be created or released */
890       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
891                       "Cannot release a service starting with ':' such as \"%s\"",
892                       _dbus_string_get_const_data (service_name));
893
894       _dbus_verbose ("Attempt to release invalid base service name \"%s\"",
895                      _dbus_string_get_const_data (service_name));
896
897       goto out;
898     }
899
900    if (_dbus_string_equal_c_str (service_name, DBUS_SERVICE_DBUS))
901     {
902       /* Not allowed; the base service name cannot be created or released */
903       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
904                       "Cannot release the %s service because it is owned by the bus",
905                      DBUS_SERVICE_DBUS);
906
907       _dbus_verbose ("Attempt to release service name \"%s\"",
908                      DBUS_SERVICE_DBUS);
909
910       goto out;
911     }
912
913     sender_id = sender_name_to_id(sender_name, error);
914     if(dbus_error_is_set(error))
915         return FALSE;
916
917     *result = kdbus_release_name(connection, service_name, sender_id);
918
919     if(*result == DBUS_RELEASE_NAME_REPLY_RELEASED)
920     {
921         BusRegistry* registry;
922         BusService *service;
923
924         registry = bus_connection_get_registry (connection);
925         service = bus_registry_lookup (registry, service_name);
926         if(service)
927         {
928             DBusConnection* phantom;
929
930             phantom = _bus_service_find_owner_connection(service, sender_name);
931             if(phantom)
932             {
933                 bus_service_remove_owner (service, phantom, transaction, NULL);
934                 /* todo we could remove phantom if he doesn't own any name
935                  * to do this we should write function in connection.c to check if
936                  * _dbus_list_get_last (&d->services_owned) returns not NULL
937                  *  or we can leave phantom - he will be removed when he disconnects from the bus
938                  */
939             }
940             else
941                 _dbus_verbose ("Didn't find phantom connection for released name!\n");
942         }
943    }
944
945   retval = TRUE;
946
947  out:
948   return retval;
949 }
950 #endif
951
952 dbus_bool_t
953 bus_registry_set_service_context_table (BusRegistry   *registry,
954                                         DBusHashTable *table)
955 {
956   DBusHashTable *new_table;
957   DBusHashIter iter;
958   
959   new_table = bus_selinux_id_table_new ();
960   if (!new_table)
961     return FALSE;
962
963   _dbus_hash_iter_init (table, &iter);
964   while (_dbus_hash_iter_next (&iter))
965     {
966       const char *service = _dbus_hash_iter_get_string_key (&iter);
967       const char *context = _dbus_hash_iter_get_value (&iter);
968
969       if (!bus_selinux_id_table_insert (new_table,
970                                         service,
971                                         context))
972         return FALSE;
973     }
974   
975   if (registry->service_sid_table)
976     _dbus_hash_table_unref (registry->service_sid_table);
977   registry->service_sid_table = new_table;
978   return TRUE;
979 }
980
981 static void
982 bus_service_unlink_owner (BusService      *service,
983                           BusOwner        *owner)
984 {
985   _dbus_list_remove_last (&service->owners, owner);
986   bus_owner_unref (owner);
987 }
988
989 static void
990 bus_service_unlink (BusService *service)
991 {
992   _dbus_assert (service->owners == NULL);
993
994   /* the service may not be in the hash, if
995    * the failure causing transaction cancel
996    * was in the right place, but that's OK
997    */
998   _dbus_hash_table_remove_string (service->registry->service_hash,
999                                   service->name);
1000   
1001   bus_service_unref (service);
1002 }
1003
1004 static void
1005 bus_service_relink (BusService           *service,
1006                     DBusPreallocatedHash *preallocated)
1007 {
1008   _dbus_assert (service->owners == NULL);
1009   _dbus_assert (preallocated != NULL);
1010
1011   _dbus_hash_table_insert_string_preallocated (service->registry->service_hash,
1012                                                preallocated,
1013                                                service->name,
1014                                                service);
1015   
1016   bus_service_ref (service);
1017 }
1018
1019 /**
1020  * Data used to represent an ownership cancellation in
1021  * a bus transaction.
1022  */
1023 typedef struct
1024 {
1025   BusOwner *owner;            /**< the owner */
1026   BusService *service;        /**< service to cancel ownership of */
1027 } OwnershipCancelData;
1028
1029 static void
1030 cancel_ownership (void *data)
1031 {
1032   OwnershipCancelData *d = data;
1033
1034   /* We don't need to send messages notifying of these
1035    * changes, since we're reverting something that was
1036    * cancelled (effectively never really happened)
1037    */
1038   bus_service_unlink_owner (d->service, d->owner);
1039   
1040   if (d->service->owners == NULL)
1041     bus_service_unlink (d->service);
1042 }
1043
1044 static void
1045 free_ownership_cancel_data (void *data)
1046 {
1047   OwnershipCancelData *d = data;
1048
1049   dbus_connection_unref (d->owner->conn);
1050   bus_owner_unref (d->owner);
1051   bus_service_unref (d->service);
1052   
1053   dbus_free (d);
1054 }
1055
1056 static dbus_bool_t
1057 add_cancel_ownership_to_transaction (BusTransaction *transaction,
1058                                      BusService     *service,
1059                                      BusOwner       *owner)
1060 {
1061   OwnershipCancelData *d;
1062
1063   d = dbus_new (OwnershipCancelData, 1);
1064   if (d == NULL)
1065     return FALSE;
1066   
1067   d->service = service;
1068   d->owner = owner;
1069
1070   if (!bus_transaction_add_cancel_hook (transaction, cancel_ownership, d,
1071                                         free_ownership_cancel_data))
1072     {
1073       dbus_free (d);
1074       return FALSE;
1075     }
1076
1077   bus_service_ref (d->service);
1078   bus_owner_ref (owner);
1079   dbus_connection_ref (d->owner->conn);
1080
1081   return TRUE;
1082 }
1083
1084 /* this function is self-cancelling if you cancel the transaction */
1085 dbus_bool_t
1086 bus_service_add_owner (BusService     *service,
1087                        DBusConnection *connection,
1088                        dbus_uint32_t  flags,
1089                        BusTransaction *transaction,
1090                        DBusError      *error)
1091 {
1092   BusOwner *bus_owner;
1093   DBusList *bus_owner_link;
1094   
1095   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1096   
1097  /* Send service acquired message first, OOM will result
1098   * in cancelling the transaction
1099   */
1100   if (service->owners == NULL)
1101     {
1102       if (!bus_driver_send_service_acquired (connection, service->name, transaction, error))
1103         return FALSE;
1104     }
1105   
1106   bus_owner_link = _bus_service_find_owner_link (service, connection);
1107   
1108   if (bus_owner_link == NULL)
1109     {
1110       bus_owner = bus_owner_new (service, connection, flags);
1111       if (bus_owner == NULL)
1112         {
1113           BUS_SET_OOM (error);
1114           return FALSE;
1115         }
1116
1117       bus_owner_set_flags (bus_owner, flags);
1118       if (!(flags & DBUS_NAME_FLAG_REPLACE_EXISTING) || service->owners == NULL)
1119         {
1120           if (!_dbus_list_append (&service->owners,
1121                                   bus_owner))
1122             {
1123               bus_owner_unref (bus_owner);
1124               BUS_SET_OOM (error);
1125               return FALSE;
1126             }
1127         }
1128       else
1129         {
1130           if (!_dbus_list_insert_after (&service->owners,
1131                                          _dbus_list_get_first_link (&service->owners),
1132                                          bus_owner))
1133             {
1134               bus_owner_unref (bus_owner);
1135               BUS_SET_OOM (error);
1136               return FALSE;
1137             }
1138         }      
1139     } 
1140   else 
1141     {
1142       /* Update the link since we are already in the queue
1143        * No need for operations that can produce OOM
1144        */
1145
1146       bus_owner = (BusOwner *) bus_owner_link->data;
1147       if (flags & DBUS_NAME_FLAG_REPLACE_EXISTING)
1148         {
1149           DBusList *link;
1150           _dbus_list_unlink (&service->owners, bus_owner_link);
1151           link = _dbus_list_get_first_link (&service->owners);
1152           _dbus_assert (link != NULL);
1153           
1154           _dbus_list_insert_after_link (&service->owners, link, bus_owner_link);
1155         }
1156       
1157       bus_owner_set_flags (bus_owner, flags);
1158       return TRUE;
1159     }
1160
1161   if (!add_cancel_ownership_to_transaction (transaction,
1162                                             service,
1163                                             bus_owner))
1164     {
1165       bus_service_unlink_owner (service, bus_owner);
1166       BUS_SET_OOM (error);
1167       return FALSE;
1168     }
1169
1170   return TRUE;
1171 }
1172
1173 typedef struct
1174 {
1175   BusOwner       *owner;
1176   BusService     *service;
1177   BusOwner       *before_owner; /* restore to position before this connection in owners list */
1178   DBusList       *owner_link;
1179   DBusList       *service_link;
1180   DBusPreallocatedHash *hash_entry;
1181 } OwnershipRestoreData;
1182
1183 static void
1184 restore_ownership (void *data)
1185 {
1186   OwnershipRestoreData *d = data;
1187   DBusList *link;
1188
1189   _dbus_assert (d->service_link != NULL);
1190   _dbus_assert (d->owner_link != NULL);
1191   
1192   if (d->service->owners == NULL)
1193     {
1194       _dbus_assert (d->hash_entry != NULL);
1195       bus_service_relink (d->service, d->hash_entry);
1196     }
1197   else
1198     {
1199       _dbus_assert (d->hash_entry == NULL);
1200     }
1201   
1202   /* We don't need to send messages notifying of these
1203    * changes, since we're reverting something that was
1204    * cancelled (effectively never really happened)
1205    */
1206   link = _dbus_list_get_first_link (&d->service->owners);
1207   while (link != NULL)
1208     {
1209       if (link->data == d->before_owner)
1210         break;
1211
1212       link = _dbus_list_get_next_link (&d->service->owners, link);
1213     }
1214   
1215   _dbus_list_insert_before_link (&d->service->owners, link, d->owner_link);
1216
1217   /* Note that removing then restoring this changes the order in which
1218    * ServiceDeleted messages are sent on destruction of the
1219    * connection.  This should be OK as the only guarantee there is
1220    * that the base service is destroyed last, and we never even
1221    * tentatively remove the base service.
1222    */
1223   bus_connection_add_owned_service_link (d->owner->conn, d->service_link);
1224   
1225   d->hash_entry = NULL;
1226   d->service_link = NULL;
1227   d->owner_link = NULL;
1228 }
1229
1230 static void
1231 free_ownership_restore_data (void *data)
1232 {
1233   OwnershipRestoreData *d = data;
1234
1235   if (d->service_link)
1236     _dbus_list_free_link (d->service_link);
1237   if (d->owner_link)
1238     _dbus_list_free_link (d->owner_link);
1239   if (d->hash_entry)
1240     _dbus_hash_table_free_preallocated_entry (d->service->registry->service_hash,
1241                                               d->hash_entry);
1242
1243   dbus_connection_unref (d->owner->conn);
1244   bus_owner_unref (d->owner);
1245   bus_service_unref (d->service);
1246   
1247   dbus_free (d);
1248 }
1249
1250 static dbus_bool_t
1251 add_restore_ownership_to_transaction (BusTransaction *transaction,
1252                                       BusService     *service,
1253                                       BusOwner       *owner)
1254 {
1255   OwnershipRestoreData *d;
1256   DBusList *link;
1257
1258   d = dbus_new (OwnershipRestoreData, 1);
1259   if (d == NULL)
1260     return FALSE;
1261   
1262   d->service = service;
1263   d->owner = owner;
1264   d->service_link = _dbus_list_alloc_link (service);
1265   d->owner_link = _dbus_list_alloc_link (owner);
1266   d->hash_entry = _dbus_hash_table_preallocate_entry (service->registry->service_hash);
1267   
1268   bus_service_ref (d->service);
1269   bus_owner_ref (d->owner);
1270   dbus_connection_ref (d->owner->conn);
1271
1272   d->before_owner = NULL;
1273   link = _dbus_list_get_first_link (&service->owners);
1274   while (link != NULL)
1275     {
1276       if (link->data == owner)
1277         {
1278           link = _dbus_list_get_next_link (&service->owners, link);
1279
1280           if (link)
1281             d->before_owner = link->data;
1282
1283           break;
1284         }
1285       
1286       link = _dbus_list_get_next_link (&service->owners, link);
1287     }
1288   
1289   if (d->service_link == NULL ||
1290       d->owner_link == NULL ||
1291       d->hash_entry == NULL ||
1292       !bus_transaction_add_cancel_hook (transaction, restore_ownership, d,
1293                                         free_ownership_restore_data))
1294     {
1295       free_ownership_restore_data (d);
1296       return FALSE;
1297     }
1298   
1299   return TRUE;
1300 }
1301
1302 dbus_bool_t
1303 bus_service_swap_owner (BusService     *service,
1304                         DBusConnection *connection,
1305                         BusTransaction *transaction,
1306                         DBusError      *error)
1307 {
1308   DBusList *swap_link;
1309   BusOwner *primary_owner;
1310
1311   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1312
1313   /* We send out notifications before we do any work we
1314    * might have to undo if the notification-sending failed
1315    */
1316   
1317   /* Send service lost message */
1318   primary_owner = bus_service_get_primary_owner (service);
1319   if (primary_owner == NULL || primary_owner->conn != connection)
1320     _dbus_assert_not_reached ("Tried to swap a non primary owner");
1321
1322     
1323   if (!bus_driver_send_service_lost (connection, service->name,
1324                                      transaction, error))
1325     return FALSE;
1326
1327   if (service->owners == NULL)
1328     {
1329       _dbus_assert_not_reached ("Tried to swap owner of a service that has no owners");
1330     }
1331   else if (_dbus_list_length_is_one (&service->owners))
1332     {
1333       _dbus_assert_not_reached ("Tried to swap owner of a service that has no other owners in the queue");
1334     }
1335   else
1336     {
1337       DBusList *link;
1338       BusOwner *new_owner;
1339       DBusConnection *new_owner_conn;
1340       link = _dbus_list_get_first_link (&service->owners);
1341       _dbus_assert (link != NULL);
1342       link = _dbus_list_get_next_link (&service->owners, link);
1343       _dbus_assert (link != NULL);
1344
1345       new_owner = (BusOwner *)link->data;
1346       new_owner_conn = new_owner->conn;
1347
1348       if (!bus_driver_send_service_owner_changed (service->name,
1349                                                   bus_connection_get_name (connection),
1350                                                   bus_connection_get_name (new_owner_conn),
1351                                                   transaction, error))
1352         return FALSE;
1353
1354       /* This will be our new owner */
1355       if (!bus_driver_send_service_acquired (new_owner_conn,
1356                                              service->name,
1357                                              transaction,
1358                                              error))
1359         return FALSE;
1360     }
1361
1362   if (!add_restore_ownership_to_transaction (transaction, service, primary_owner))
1363     {
1364       BUS_SET_OOM (error);
1365       return FALSE;
1366     }
1367
1368   /* unlink the primary and make it the second link */
1369   swap_link = _dbus_list_get_first_link (&service->owners);
1370   _dbus_list_unlink (&service->owners, swap_link);
1371
1372   _dbus_list_insert_after_link (&service->owners,
1373                                 _dbus_list_get_first_link (&service->owners),
1374                                 swap_link);
1375
1376   return TRUE;
1377 }
1378
1379 /* this function is self-cancelling if you cancel the transaction */
1380 dbus_bool_t
1381 bus_service_remove_owner (BusService     *service,
1382                           DBusConnection *connection,
1383                           BusTransaction *transaction,
1384                           DBusError      *error)
1385 {
1386   BusOwner *primary_owner;
1387   
1388   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1389   
1390   /* We send out notifications before we do any work we
1391    * might have to undo if the notification-sending failed
1392    */
1393   
1394   /* Send service lost message */
1395   primary_owner = bus_service_get_primary_owner (service);
1396   if (primary_owner != NULL && primary_owner->conn == connection)
1397     {
1398       if (!bus_driver_send_service_lost (connection, service->name,
1399                                          transaction, error))
1400         return FALSE;
1401     }
1402   else
1403     {
1404       /* if we are not the primary owner then just remove us from the queue */
1405       DBusList *link;
1406       BusOwner *temp_owner;
1407
1408       link = _bus_service_find_owner_link (service, connection);
1409       _dbus_list_unlink (&service->owners, link);
1410       temp_owner = (BusOwner *)link->data;
1411       bus_owner_unref (temp_owner); 
1412       _dbus_list_free_link (link);
1413
1414       return TRUE; 
1415     }
1416
1417   if (service->owners == NULL)
1418     {
1419       _dbus_assert_not_reached ("Tried to remove owner of a service that has no owners");
1420     }
1421   else if (_dbus_list_length_is_one (&service->owners))
1422     {
1423       if (!bus_driver_send_service_owner_changed (service->name,
1424                                                   bus_connection_get_name (connection),
1425                                                   NULL,
1426                                                   transaction, error))
1427         return FALSE;
1428     }
1429   else
1430     {
1431       DBusList *link;
1432       BusOwner *new_owner;
1433       DBusConnection *new_owner_conn;
1434       link = _dbus_list_get_first_link (&service->owners);
1435       _dbus_assert (link != NULL);
1436       link = _dbus_list_get_next_link (&service->owners, link);
1437       _dbus_assert (link != NULL);
1438
1439       new_owner = (BusOwner *)link->data;
1440       new_owner_conn = new_owner->conn;
1441
1442       if (!bus_driver_send_service_owner_changed (service->name,
1443                                                   bus_connection_get_name (connection),
1444                                                   bus_connection_get_name (new_owner_conn),
1445                                                   transaction, error))
1446         return FALSE;
1447
1448       /* This will be our new owner */
1449       if (!bus_driver_send_service_acquired (new_owner_conn,
1450                                              service->name,
1451                                              transaction,
1452                                              error))
1453         return FALSE;
1454     }
1455
1456   if (!add_restore_ownership_to_transaction (transaction, service, primary_owner))
1457     {
1458       BUS_SET_OOM (error);
1459       return FALSE;
1460     }
1461  
1462   bus_service_unlink_owner (service, primary_owner);
1463
1464   if (service->owners == NULL)
1465     bus_service_unlink (service);
1466
1467   return TRUE;
1468 }
1469
1470 BusService *
1471 bus_service_ref (BusService *service)
1472 {
1473   _dbus_assert (service->refcount > 0);
1474   
1475   service->refcount += 1;
1476
1477   return service;
1478 }
1479
1480 void
1481 bus_service_unref (BusService *service)
1482 {
1483   _dbus_assert (service->refcount > 0);
1484   
1485   service->refcount -= 1;
1486
1487   if (service->refcount == 0)
1488     {
1489       _dbus_assert (service->owners == NULL);
1490       
1491       dbus_free (service->name);
1492       _dbus_mem_pool_dealloc (service->registry->service_pool, service);
1493     }
1494 }
1495
1496 DBusConnection *
1497 bus_service_get_primary_owners_connection (BusService *service)
1498 {
1499   BusOwner *owner;
1500 #ifdef ENABLE_KDBUS_TRANSPORT
1501   char unique_name[(unsigned int)(snprintf((char*)NULL, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
1502 #endif
1503
1504   owner = bus_service_get_primary_owner (service);
1505
1506 #ifdef ENABLE_KDBUS_TRANSPORT
1507   if(kdbus_get_name_owner(owner->conn, bus_service_get_name(service), unique_name) < 0)
1508     return NULL;
1509   return _bus_service_find_owner_connection(service, unique_name);  //bus_connections_find_conn_by_name would be safer? but slower
1510 #else
1511   if (owner != NULL)
1512     return owner->conn;
1513   else
1514     return NULL;
1515 #endif
1516 }
1517
1518 BusOwner*
1519 bus_service_get_primary_owner (BusService *service)
1520 {
1521   return _dbus_list_get_first (&service->owners);
1522 }
1523
1524 const char*
1525 bus_service_get_name (BusService *service)
1526 {
1527   return service->name;
1528 }
1529
1530 dbus_bool_t
1531 bus_service_get_allow_replacement (BusService *service)
1532 {
1533   BusOwner *owner;
1534   DBusList *link;
1535  
1536   _dbus_assert (service->owners != NULL);
1537
1538   link = _dbus_list_get_first_link (&service->owners);
1539   owner = (BusOwner *) link->data;
1540
1541   return owner->allow_replacement;
1542 }
1543
1544 #ifdef ENABLE_KDBUS_TRANSPORT
1545 dbus_bool_t
1546 bus_service_get_is_kdbus_starter (BusService *service)
1547 {
1548   BusOwner *owner;
1549   DBusList *link;
1550
1551   _dbus_assert (service->owners != NULL);
1552
1553   link = _dbus_list_get_first_link (&service->owners);
1554   owner = (BusOwner *) link->data;
1555
1556   return owner->is_kdbus_starter;
1557 }
1558 #endif
1559
1560 dbus_bool_t
1561 bus_service_has_owner (BusService     *service,
1562                        DBusConnection *connection)
1563 {
1564   DBusList *link;
1565
1566   link = _bus_service_find_owner_link (service, connection);
1567  
1568   if (link == NULL)
1569     return FALSE;
1570   else
1571     return TRUE;
1572 }
1573
1574 dbus_bool_t 
1575 bus_service_list_queued_owners (BusService *service,
1576                                 DBusList  **return_list,
1577                                 DBusError  *error)
1578 {
1579   DBusList *link;
1580
1581   _dbus_assert (*return_list == NULL);
1582
1583   link = _dbus_list_get_first_link (&service->owners);
1584   _dbus_assert (link != NULL);
1585   
1586   while (link != NULL)
1587     {
1588       BusOwner *owner;
1589       const char *uname;
1590
1591       owner = (BusOwner *) link->data;
1592       uname = bus_connection_get_name (owner->conn);
1593
1594 #ifdef ENABLE_KDBUS_TRANSPORT
1595       if(!owner->is_kdbus_starter)
1596 #endif
1597           if (!_dbus_list_append (return_list, (char *)uname))
1598               goto oom;
1599
1600       link = _dbus_list_get_next_link (&service->owners, link);
1601     }
1602   
1603   return TRUE;
1604   
1605  oom:
1606   _dbus_list_clear (return_list);
1607   BUS_SET_OOM (error);
1608   return FALSE;
1609 }