summaryrefslogtreecommitdiff
path: root/bus/kdbus-d.c
blob: be9fccc44007d264128c4cc06d2cdd34a2daaea8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* kdbus-d.c  kdbus related daemon functions
 *
 * Copyright (C) 2013  Samsung Electronics
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version and under the terms of the GNU
 * Lesser General Public License as published by the
 * Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <dbus/dbus-connection-internal.h>
#include "kdbus-d.h"
#include <dbus/kdbus.h>
#include <dbus/dbus-bus.h>
#include "dispatch.h"
#include <dbus/kdbus-common.h>
#include <dbus/dbus-transport.h>
#include <dbus/dbus-transport-kdbus.h>
#include "connection.h"
#include "activation.h"
#include "services.h"
#include <dbus/dbus-connection.h>

#include <utils.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

/*
 * Converts string with unique name into __u64 id number. If the name is not unique, sets error.
 */
__u64 sender_name_to_id(const char* name, DBusError* error)
{
	__u64 sender_id = 0;

	if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
		sender_id = strtoull(&name[3], NULL, 10);
	else
		dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");

	return sender_id;
}

/**
 * Seeks key in rule string, and duplicates value of the key into pValue.
 * Because of the duplication, pValue must be freed after use.
 *
 * @param rule rule to look through
 * @param key key to look for
 * @param pValue pointer to value of the key found
 * @return length of the value string, 0 means not found
 */
static int parse_match_key(const char *rule, const char* key, char** pValue)
{
  const char* pBegin;
  const char* pValueEnd;
  int value_length = 0;

  pBegin = strstr(rule, key);
  if(pBegin)
  {
    pBegin += strlen(key);
    pValueEnd = strchr(pBegin, '\'');
    if(pValueEnd)
    {
      value_length = pValueEnd - pBegin;
      *pValue = strndup(pBegin, value_length);
      if(*pValue)
        _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
    }
  }
  return value_length;
}

/**
 * Adds a match rule to match broadcast messages going through the message bus.
 * Do no affect messages addressed directly.
 *
 * The "rule" argument is the string form of a match rule.
 *
 * Only part of the dbus's matching capabilities is implemented in kdbus now, because of different mechanism.
 * Current mapping:
 * interface match key mapped to bloom
 * sender match key mapped to src_name
 *
 * @param transport transport
 * @param id id of connection for which the rule is to be added
 * @param rule textual form of match rule
  */
dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rule)
{
  struct kdbus_cmd_match* pCmd_match;
  struct kdbus_item *pItem;
  __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
  uint64_t size;
  int name_size;
  char* pName = NULL;
  char* pInterface = NULL;
  dbus_bool_t ret_value = FALSE;
  int fd;
  __u64 bloom_size;

  if(!_dbus_transport_get_socket_fd(transport, &fd))
    return FALSE;

  bloom_size = dbus_transport_get_bloom_size(transport);

  /*parsing rule and calculating size of command*/
  size = sizeof(struct kdbus_cmd_match);
  if(parse_match_key(rule, "interface='", &pInterface))       /*actual size is not important for interface because bloom size is defined by bus*/
    size += KDBUS_PART_HEADER_SIZE + bloom_size;
  name_size = parse_match_key(rule, "sender='", &pName);
  if(name_size)
  {
    if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
    {
      src_id = strtoull(&pName[3], NULL, 10);
      free(pName);
      pName = NULL;
    }
    else
      size += KDBUS_ITEM_SIZE(name_size + 1);  //well known name
  }

  pCmd_match = alloca(size);
  if(pCmd_match == NULL)
    goto out;

  pCmd_match->id = id;
  pCmd_match->cookie = id;
  pCmd_match->size = size;
  pCmd_match->src_id = src_id;

  pItem = pCmd_match->items;
  if(pName)
  {
    pItem->type = KDBUS_MATCH_SRC_NAME;
    pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
    memcpy(pItem->str, pName, strlen(pName) + 1);
    pItem = KDBUS_PART_NEXT(pItem);
  }
  if(pInterface)
  {
    pItem->type = KDBUS_MATCH_BLOOM;
    pItem->size = KDBUS_PART_HEADER_SIZE + bloom_size;
    strncpy(pItem->data, pInterface, bloom_size);
  }

  if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
    _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
  else
  {
    _dbus_verbose("Added match bus rule %s for id:%llu\n", rule, (unsigned long long)id);
    ret_value = TRUE;
  }

out:
  if(pName)
    free(pName);
  if(pInterface)
    free(pInterface);
  return ret_value;
}

/**
 * Opposing to dbus, in kdbus removes all match rules with given
 * cookie, which in this implementation is equal to uniqe id.
 *
 * @param transport transport
 * @param id connection id for which rules are to be removed
 */
dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id)
{
  struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
  int fd;

  if(!_dbus_transport_get_socket_fd(transport, &fd))
    return FALSE;

  cmd.cookie = id;
  cmd.id = id;
  cmd.size = sizeof(struct kdbus_cmd_match);

  if(ioctl(fd, KDBUS_CMD_MATCH_REMOVE, &cmd))
  {
    _dbus_verbose("Failed removing match rule for id: %llu; error: %d, %m\n", (unsigned long long)id, errno);
    return FALSE;
  }
  else
  {
    _dbus_verbose("Match rule removed correctly.\n");
    return TRUE;
  }
}

/**
 * Performs kdbus query of id of the given name
 *
 * @param name name to query for
 * @param transport transport
 * @param pInfo nameInfo structure address to store info about the name
 * @return 0 on success, -errno if failed
 */
int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo* pInfo)
{
  struct kdbus_cmd_name_info *msg;
  struct kdbus_item *item;
  uint64_t size;
  int ret;
  uint64_t item_size;
  int fd;

  pInfo->sec_label_len = 0;
  pInfo->sec_label = NULL;

  if(!_dbus_transport_get_socket_fd(transport, &fd))
    return -EPERM;

  item_size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
  item_size = (item_size < 56) ? 56 : item_size;  //at least 56 bytes are needed by kernel to place info about name, otherwise error
  size = sizeof(struct kdbus_cmd_name_info) + item_size;

  msg = malloc(size);
  if (!msg)
  {
    _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
    return -errno;
  }

  memset(msg, 0, size);
  msg->size = size;
    if((name[0] == ':') && (name[1] == '1') && (name[2] == '.'))  /* if name starts with ":1." it is a unique name and should be send as number */
      msg->id = strtoull(&name[3], NULL, 10);
    else
      msg->id = 0;

  item = msg->items;
  item->type = KDBUS_NAME_INFO_ITEM_NAME;
  item->size = item_size;
  memcpy(item->str, name, strlen(name) + 1);

  again:
  ret = ioctl(fd, KDBUS_CMD_NAME_QUERY, msg);
  if (ret < 0)
  {
    if(errno == EINTR)
      goto again;
    if(errno == EAGAIN)
        goto again;
    else if(ret == -ENOBUFS)
    {
      msg = realloc(msg, msg->size);  //prepare memory
      if(msg != NULL)
        goto again;
    }
    pInfo->uniqueId = 0;
    ret = -errno;
  }
  else
  {
    pInfo->uniqueId = msg->id;
    pInfo->userId = msg->creds.uid;
    pInfo->processId = msg->creds.pid;
    item = msg->items;
    while((uint8_t *)(item) < (uint8_t *)(msg) + msg->size)
    {
      if(item->type == KDBUS_NAME_INFO_ITEM_SECLABEL)
      {
          pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
        if(pInfo->sec_label_len != 0)
        {
          pInfo->sec_label = malloc(pInfo->sec_label_len);
          if(pInfo->sec_label == NULL)
            ret = -1;
          else
            memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
        }
        break;
      }
      item = KDBUS_PART_NEXT(item);
    }
  }

  free(msg);
  return ret;
}

/*
 * Creates kdbus bus of given type.
 */
char* make_kdbus_bus(DBusBusType type, DBusError *error)
{
    struct {
        struct kdbus_cmd_bus_make head;
        uint64_t n_size;
        uint64_t n_type;
        char name[64];
    } __attribute__ ((__aligned__(8))) bus_make;

    int fdc, ret;
    char *bus;

    _dbus_verbose("Opening /dev/kdbus/control\n");
    fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
    if (fdc < 0)
    {
        _dbus_verbose("--- error %d (%m)\n", fdc);
        dbus_set_error(error, DBUS_ERROR_FAILED, "Opening /dev/kdbus/control failed: %d (%m)", fdc);
        return NULL;
    }

    memset(&bus_make, 0, sizeof(bus_make));
    bus_make.head.bloom_size = 64;
    bus_make.head.flags = KDBUS_MAKE_ACCESS_WORLD;

    if(type == DBUS_BUS_SYSTEM)
        snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%s", getuid(), "system");
    else if(type == DBUS_BUS_SESSION)
        snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
    else
        snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%u", getuid(), getpid());

    bus_make.n_type = KDBUS_MAKE_NAME;
    bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
    bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;

    _dbus_verbose("Creating bus '%s'\n", bus_make.name);
    ret = ioctl(fdc, KDBUS_CMD_BUS_MAKE, &bus_make);
    if (ret)
    {
        _dbus_verbose("--- error %d (%m)\n", ret);
        dbus_set_error(error, DBUS_ERROR_FAILED, "Creating bus '%s' failed: %d (%m)", bus_make.name, fdc);
        return NULL;
    }

    if (asprintf(&bus, "kdbus:path=/dev/kdbus/%s/bus", bus_make.name) < 0)
    {
        BUS_SET_OOM (error);
        return NULL;
    }

    _dbus_verbose("Return value '%s'\n", bus);
	return bus;
}

/*
 * Minimal server init needed by context to go further.
 */
DBusServer* empty_server_init(char* address)
{
	return dbus_server_init_mini(address);
}

static dbus_bool_t add_matches_for_kdbus_broadcasts(DBusTransport* transport)
{
  struct kdbus_cmd_match* pCmd_match;
  struct kdbus_item *pItem;
  uint64_t size;
  int fd;

  if(!_dbus_transport_get_socket_fd(transport, &fd))
    {
      errno = EPERM;
      return FALSE;
    }


  size = sizeof(struct kdbus_cmd_match);
  size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2;  /*3 name related items plus 2 id related items*/

  pCmd_match = alloca(size);
  if(pCmd_match == NULL)
    {
      errno = ENOMEM;
      return FALSE;
    }

  pCmd_match->id = 1;
  pCmd_match->cookie = 1;
  pCmd_match->size = size;

  pItem = pCmd_match->items;
  pCmd_match->src_id = 0;
  pItem->type = KDBUS_MATCH_NAME_CHANGE;
  pItem->size = KDBUS_PART_HEADER_SIZE + 1;
  pItem = KDBUS_PART_NEXT(pItem);
  pItem->type = KDBUS_MATCH_NAME_ADD;
  pItem->size = KDBUS_PART_HEADER_SIZE + 1;
  pItem = KDBUS_PART_NEXT(pItem);
  pItem->type = KDBUS_MATCH_NAME_REMOVE;
  pItem->size = KDBUS_PART_HEADER_SIZE + 1;
  pItem = KDBUS_PART_NEXT(pItem);
  pItem->type = KDBUS_MATCH_ID_ADD;
  pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
  pItem = KDBUS_PART_NEXT(pItem);
  pItem->type = KDBUS_MATCH_ID_REMOVE;
  pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);

  if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
    {
      _dbus_verbose("Failed adding match rule for daemon, error: %d, %m\n", errno);
      return FALSE;
    }

  _dbus_verbose("Added match rule for daemon correctly.\n");
  return TRUE;
}

/*
 * Connects daemon to bus created by him and adds matches for "system" broadcasts.
 * Do not requests org.freedesktop.DBus name, because it's to early
 * (some structures of BusContext are not ready yet).
 */
DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
{
  DBusConnection* connection;

  dbus_bus_set_bus_connection_address(type, address);

  connection = dbus_bus_get_private(type, error);  /*todo possibly could be optimised by using lower functions*/
  if(connection == NULL)
    return NULL;

  if(!add_matches_for_kdbus_broadcasts(dbus_connection_get_transport(connection)))
    {
      dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for daemon, %s", _dbus_strerror_from_errno ());
      goto failed;
    }

  if(dbus_error_is_set(error))
    {
      failed:
      _dbus_connection_close_possibly_shared (connection);
      dbus_connection_unref (connection);
      connection = NULL;
    }
  else
    _dbus_verbose ("Daemon connected as kdbus client.\n");

  return connection;
}

/*
 * Asks bus for org.freedesktop.DBus well-known name.
 */
dbus_bool_t register_daemon_name(DBusConnection* connection)
{
    DBusString daemon_name;
    dbus_bool_t retval = FALSE;
    BusTransaction *transaction;

    _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
    if(!register_kdbus_policy(DBUS_SERVICE_DBUS, dbus_connection_get_transport(connection), geteuid()))
      return FALSE;

    if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
       return FALSE;

    transaction = bus_transaction_new (bus_connection_get_context(connection));
    if (transaction == NULL)
    {
        kdbus_release_name(connection, &daemon_name, 0);
        goto out;
    }

    if(!bus_registry_ensure (bus_connection_get_registry (connection), &daemon_name, connection, 0, transaction, NULL))
    {
        kdbus_release_name(connection, &daemon_name, 0);
        goto out;
    }

    retval = TRUE;

out:
	bus_transaction_cancel_and_free(transaction);
    return retval;
}

dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
{
	int fd;

	_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);

	return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
}

dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
{
	int fd;

	_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);

	return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
}

/*
 * Asks kdbus for well-known names registered on the bus
 */
dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
{
	int fd;
	struct kdbus_cmd_names* pCmd;
	__u64 cmd_size;
	dbus_bool_t ret_val = FALSE;
	char** list;
	int list_len = 0;
	int i = 0;
	int j;

	cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
	pCmd = malloc(cmd_size);
	if(pCmd == NULL)
		goto out;
	pCmd->size = cmd_size;

	_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);

again:
	cmd_size = 0;
	if(ioctl(fd, KDBUS_CMD_NAME_LIST, pCmd))
	{
		if(errno == EINTR)
			goto again;
		if(errno == ENOBUFS)			//buffer to small to put all names into it
			cmd_size = pCmd->size;		//here kernel tells how much memory it needs
		else
		{
			_dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
			goto out;
		}
	}
	if(cmd_size)  //kernel needs more memory
	{
		pCmd = realloc(pCmd, cmd_size);  //prepare memory
		if(pCmd == NULL)
			return FALSE;
		goto again;						//and try again
	}
	else
	{
		struct kdbus_cmd_name* pCmd_name;

		for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
			list_len++;

		list = malloc(sizeof(char*) * (list_len + 1));
		if(list == NULL)
			goto out;

		for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
		{
			list[i] = strdup(pCmd_name->name);
			if(list[i] == NULL)
			{
				for(j=0; j<i; j++)
					free(list[j]);
				free(list);
				goto out;
			}
			_dbus_verbose ("Name %d: %s\n", i, list[i]);
			++i;
		}
		list[i] = NULL;
	}

	*array_len = list_len;
	*listp = list;
	ret_val = TRUE;

out:
	if(pCmd)
		free(pCmd);
	return ret_val;
}

/*
 * Asks kdbus for list of connections being in the queue to own
 * given well-known name. The list includes the owner of the name on the
 * first position.
 */
dbus_bool_t kdbus_list_queued (DBusConnection *connection, DBusList  **return_list,
                               const char *name, DBusError  *error)
{
  int fd;
  struct kdbus_cmd_names* pCmd;
  __u64 cmd_size;
  dbus_bool_t ret_val = FALSE;
  int name_length;

  _dbus_assert (*return_list == NULL);

  name_length = strlen(name) + 1;
  cmd_size = sizeof(struct kdbus_cmd_names) + sizeof(struct kdbus_cmd_name) + name_length;
  pCmd = malloc(cmd_size);
  if(pCmd == NULL)
    goto out;
  pCmd->size = cmd_size;
  pCmd->names[0].id = 0;
  pCmd->names[0].size =  sizeof(struct kdbus_cmd_name) + name_length;
  memcpy(pCmd->names[0].name, name, name_length);
  _dbus_verbose ("Asking for queued owners of %s\n", pCmd->names[0].name);

  _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);

  again:
  cmd_size = 0;
  if(ioctl(fd, KDBUS_CMD_NAME_LIST_QUEUED, pCmd))
    {
      if(errno == EINTR)
        goto again;
      if(errno == ENOBUFS)      //buffer to small to put all names into it
        cmd_size = pCmd->size;    //here kernel tells how much memory it needs
      else if(errno == ENOENT)
        {
          dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
                      "Could not get owners of name '%s': no such name", name);
          free(pCmd);
          return FALSE;
        }
      else
        {
          _dbus_verbose("kdbus error asking for queued owners list: err %d (%m)\n",errno);
          goto out;
        }
    }
  if(cmd_size)  //kernel needs more memory
    {
      pCmd = realloc(pCmd, cmd_size);  //prepare memory
      if(pCmd == NULL)
        return FALSE;
      goto again;           //and try again
    }
  else
    {
      struct kdbus_cmd_name* pCmd_name;

      for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
        {
          char *uname = NULL;

          _dbus_verbose ("Got queued owner id: %llu\n", (unsigned long long)pCmd_name->id);
          uname = malloc(snprintf(uname, 0, ":1.%llu0", (unsigned long long)pCmd_name->id));
          if(uname == NULL)
            goto out;
          sprintf(uname, ":1.%llu", (unsigned long long int)pCmd_name->id);
          if (!_dbus_list_append (return_list, uname))
            goto out;
        }
    }

  ret_val = TRUE;

  out:
  if(pCmd)
    free(pCmd);
  if(ret_val == FALSE)
    {
      DBusList *link;

      dbus_set_error (error, _dbus_error_from_errno (errno),
          "Failed to list queued owners of \"%s\": %s",
          name, _dbus_strerror (errno));

      link = _dbus_list_get_first_link (return_list);
      while (link != NULL)
        {
          DBusList *next = _dbus_list_get_next_link (return_list, link);

          if(link->data != NULL)
            free(link->data);

          _dbus_list_free_link (link);
          link = next;
        }
    }

  return ret_val;
}

/*
 *  Register match rule in kdbus on behalf of sender of the message
 */
dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
{
	__u64 sender_id;

	sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
	if(dbus_error_is_set(error))
		return FALSE;

	if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
	{
	      dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
	                      sender_id, _dbus_strerror_from_errno ());
	      return FALSE;
	}

	return TRUE;
}

/*
 *  Removes match rule in kdbus on behalf of sender of the message
 */
dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
{
	__u64 sender_id;

	sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
	if(dbus_error_is_set(error))
		return FALSE;

	if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
	{
	      dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
	      return FALSE;
	}

	return TRUE;
}

int kdbus_get_name_owner(DBusConnection* connection, const char* name, char* owner)
{
  int ret;
  struct nameInfo info;

  ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
  if(ret == 0) //unique id of the name
  {
    sprintf(owner, ":1.%llu", (unsigned long long int)info.uniqueId);
    _dbus_verbose("Unique name discovered:%s\n", owner);
  }
  else if(ret != -ENOENT)
    _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);

  return ret;
}

/*
 *  Asks kdbus for uid of the owner of the name given in the message
 */
dbus_bool_t kdbus_get_unix_user(DBusConnection* connection, const char* name, unsigned long* uid, DBusError* error)
{
  struct nameInfo info;
  int inter_ret;
  dbus_bool_t ret = FALSE;

  inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
  if(inter_ret == 0) //name found
  {
    _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
    *uid = info.userId;
    return TRUE;
  }
  else if(inter_ret == -ENOENT)  //name has no owner
    {
      _dbus_verbose ("Name %s has no owner.\n", name);
      dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
    }

  else
  {
    _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
    dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
  }

  return ret;
}

/*
 *  Asks kdbus for pid of the owner of the name given in the message
 */
dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, const char* name, unsigned long* pid, DBusError* error)
{
	struct nameInfo info;
	int inter_ret;
	dbus_bool_t ret = FALSE;

	inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
	if(inter_ret == 0) //name found
	{
		_dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
		*pid = info.processId;
		return TRUE;
	}
	else if(inter_ret == -ENOENT)  //name has no owner
		dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
	else
	{
		_dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
		dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
	}

	return ret;
}

/*
 *  Asks kdbus for selinux_security_context of the owner of the name given in the message
 */
dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
{
	char* name = NULL;
	struct nameInfo info;
	int inter_ret;
	dbus_bool_t ret = FALSE;

	dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
	inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
	if(inter_ret == -ENOENT)  //name has no owner
		dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
	else if(inter_ret < 0)
	{
		_dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
		dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
	}
	else
	{
		if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
		{
		      _DBUS_SET_OOM (error);
		      return FALSE;
		}
		ret = TRUE;
	}

	return ret;
}

/**
 * Gets the UNIX user ID of the connection from kdbus, if known. Returns #TRUE if
 * the uid is filled in.  Always returns #FALSE on non-UNIX platforms
 * for now., though in theory someone could hook Windows to NIS or
 * something.  Always returns #FALSE prior to authenticating the
 * connection.
 *
 * The UID of is only read by bus daemon from kdbus. You can not
 * call this function from client side of the connection.
 *
 * You can ask the bus to tell you the UID of another connection though
 * if you like; this is done with dbus_bus_get_unix_user().
 *
 * @param connection the connection
 * @param uid return location for the user ID
 * @returns #TRUE if uid is filled in with a valid user ID
 */
dbus_bool_t
dbus_connection_get_unix_user (DBusConnection *connection,
                               unsigned long  *uid)
{
  _dbus_return_val_if_fail (connection != NULL, FALSE);
  _dbus_return_val_if_fail (uid != NULL, FALSE);

  if(bus_context_is_kdbus(bus_connection_get_context (connection)))
    return kdbus_get_unix_user(connection, bus_connection_get_name(connection), uid, NULL);

  return dbus_connection_get_unix_user_dbus(connection, uid);
}

/**
 * Gets the process ID of the connection if any.
 * Returns #TRUE if the pid is filled in.
 *
 * @param connection the connection
 * @param pid return location for the process ID
 * @returns #TRUE if uid is filled in with a valid process ID
 */
dbus_bool_t
dbus_connection_get_unix_process_id (DBusConnection *connection,
             unsigned long  *pid)
{
  _dbus_return_val_if_fail (connection != NULL, FALSE);
  _dbus_return_val_if_fail (pid != NULL, FALSE);

  if(bus_context_is_kdbus(bus_connection_get_context (connection)))
    return kdbus_get_connection_unix_process_id(connection, bus_connection_get_name(connection), pid, NULL);

  return dbus_connection_get_unix_process_id_dbus(connection, pid);
}

/*
 * Create connection structure for given name. It is needed to control starters - activatable services
 * and for ListQueued method (as long as kdbus is not supporting it). This connections don't have it's own
 * fd so it is set up on the basis of daemon's transport. Functionality of such connection is limited.
 */
DBusConnection* create_phantom_connection(DBusConnection* connection, const char* name, DBusError* error)
{
    DBusConnection *phantom_connection;
    DBusString Sname;

    _dbus_string_init_const(&Sname, name);

    phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
    if(phantom_connection == NULL)
        return FALSE;
    if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
    {
        dbus_connection_unref_phantom(phantom_connection);
        phantom_connection = NULL;
        dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
        goto out;
    }
    if(!bus_connection_complete(phantom_connection, &Sname, error))
    {
        bus_connection_disconnected(phantom_connection);
        phantom_connection = NULL;
        goto out;
    }

    _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));

out:
    return phantom_connection;
}

/*
 * Registers activatable services as kdbus starters.
 */
dbus_bool_t register_kdbus_starters(DBusConnection* connection)
{
    int i,j, len;
    char **services;
    dbus_bool_t retval = FALSE;
    int fd;
    BusTransaction *transaction;
    DBusString name;
    DBusTransport* transport;
    unsigned long int euid;

    transaction = bus_transaction_new (bus_connection_get_context(connection));
    if (transaction == NULL)
    	return FALSE;

    if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
        return FALSE;

    transport = dbus_connection_get_transport(connection);
    euid = geteuid();

    if(!_dbus_transport_get_socket_fd (transport, &fd))
      return FALSE;

    _dbus_string_init(&name);

    for(i=0; i<len; i++)
    {
        if(!register_kdbus_policy(services[i], transport, euid))
          goto out;

        if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
            goto out;

        if(!_dbus_string_append(&name, services[i]))
        	goto out;
        if(!bus_registry_ensure (bus_connection_get_registry (connection), &name, connection,
        		(DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER), transaction, NULL))
        	goto out;
        if(!_dbus_string_set_length(&name, 0))
        	goto out;
    }
    retval = TRUE;

out:
    if(retval == FALSE)
    {
        for(j=0; j<i; j++)
            release_kdbus_name(fd, services[j], 0);
    }
    dbus_free_string_array (services);
    _dbus_string_free(&name);
    bus_transaction_cancel_and_free(transaction);
    return retval;
}

/*
 * Updates kdbus starters (activatable services) after configuration was reloaded.
 * It releases all previous starters and registers all new.
 */
dbus_bool_t update_kdbus_starters(DBusConnection* connection)
{
    dbus_bool_t retval = FALSE;
    DBusList **services_old;
    DBusList *link;
    BusService *service = NULL;
    BusTransaction *transaction;
    int fd;

    transaction = bus_transaction_new (bus_connection_get_context(connection));
    if (transaction == NULL)
        return FALSE;

    if(!_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd))
        goto out;

    services_old = bus_connection_get_services_owned(connection);
    link = _dbus_list_get_first_link(services_old);
    link = _dbus_list_get_next_link (services_old, link); //skip org.freedesktop.DBus which is not starter

    while (link != NULL)
    {
        int ret;

        service = (BusService*) link->data;
        if(service == NULL)
            goto out;

        ret = release_kdbus_name(fd, bus_service_get_name(service), 0);

        if (ret == DBUS_RELEASE_NAME_REPLY_RELEASED)
        {
            if(!bus_service_remove_owner(service, connection, transaction, NULL))
                _dbus_verbose ("Unable to remove\n");
        }
        else if(ret < 0)
            goto out;

        link = _dbus_list_get_next_link (services_old, link);
    }

    if(!register_kdbus_starters(connection))
    {
        _dbus_verbose ("Registering kdbus starters for dbus activatable names failed!\n");
        goto out;
    }
    retval = TRUE;

out:
	bus_transaction_cancel_and_free(transaction);
    return retval;
}

/*
 * Analyzes system broadcasts about id and name changes.
 * Basing on this it sends NameAcquired and NameLost signals and clear phantom connections.
 */
void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection)
{
    const char *name, *old, *new;

    if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID))
    {
        _dbus_verbose ("Couldn't get args of NameOwnerChanged signal.\n");//, error.message);
        return;
    }

    _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new);

    if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
    {
        if(!strcmp(name, old))  //yes it is - someone has disconnected
        {
            DBusConnection* conn;

            conn = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
            if(conn)
                bus_connection_disconnected(conn);
        }
    }
    else //it is well-known name
    {
        if((*old != 0) && (strcmp(old, ":1.1")))
        {
            DBusMessage *message;

            if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL)
                goto next;

            _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);

            message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
            if (message == NULL)
                goto next;

            if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message,
                                                                 DBUS_TYPE_STRING, &name,
                                                                 DBUS_TYPE_INVALID))
            {
                dbus_message_unref (message);
                goto next;
            }

            bus_transaction_send_from_driver (transaction, connection, message);
            dbus_message_unref (message);
        }
    next:
        if((*new != 0) && (strcmp(new, ":1.1")))
        {
            DBusMessage *message;

            _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name);

            message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired");
            if (message == NULL)
                return;

            if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message,
                                                                 DBUS_TYPE_STRING, &name,
                                                                 DBUS_TYPE_INVALID))
            {
                dbus_message_unref (message);
                return;
            }

            bus_transaction_send_from_driver (transaction, connection, message);
            dbus_message_unref (message);
        }
    }

    if(bus_transaction_send(transaction, connection, msg))
      _dbus_verbose ("NameOwnerChanged sent\n");
    else
      _dbus_verbose ("Sending NameOwnerChanged failed\n");
}