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