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