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