kdbus: move dbuspolicy init to bus_register_kdbus
[platform/upstream/dbus.git] / dbus / kdbus-common.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* kdbus-common.c  kdbus related utils for daemon and libdbus
3  *
4  * Copyright (C) 2013  Samsung Electronics
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version and under the terms of the GNU
12  * Lesser General Public License as published by the
13  * Free Software Foundation; either version 2.1 of the License, or (at
14  * your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
24  *
25  */
26 #include <config.h>
27 #include <linux/kdbus.h>
28 #include "kdbus-common.h"
29 #include "dbus-transport-kdbus.h"
30 #include "dbus-valgrind-internal.h"
31 #include <string.h>
32 #include <stddef.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <dbus/dbus-internals.h>
37 #include <dbus/dbus-shared.h>
38 #include "dbus-signals.h"
39 #include <stdio.h>
40 #include <sys/ioctl.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <sys/mman.h>
45
46 struct kdbus_t
47 {
48   const char *path;                 /**< Path to kdbus file */
49   int fd;                                                     /**< File descriptor */
50   void *mmap_ptr;                   /**< Mapped memory where kdbus (kernel) writes
51                                      *   messages incoming to us.
52                                      */
53   size_t pool_size;                                     /**< Size of mapped memory */
54   __u64 id;                                       /**< unique id of the connection */
55   char bus_id[sizeof (((struct kdbus_cmd_hello *)(0))->id128)];  /**< id of the bus */
56   struct kdbus_bloom_parameter bloom;                         /**< bloom parameters*/
57 };
58
59 /* ALIGN8 and KDBUS_FOREACH taken from systemd */
60 #define ALIGN8(l) (((l) + 7) & ~7)
61 #define KDBUS_FOREACH(iter, first, _size)                               \
62         for (iter = (first);                                            \
63              ((uint8_t *)(iter) < (uint8_t *)(first) + (_size)) &&      \
64                ((uint8_t *)(iter) >= (uint8_t *)(first));               \
65              iter = (void*)(((uint8_t *)iter) + ALIGN8((iter)->size)))
66
67 static int
68 safe_ioctl (int fd,
69             unsigned long request,
70             void *data)
71 {
72   int ret;
73
74   do {
75     ret = ioctl (fd, request, data);
76   }
77   while (-1 == ret && EINTR == errno);
78
79   return ret;
80 }
81
82 static int
83 free_by_offset (kdbus_t  *kdbus,
84                 __u64     offset)
85 {
86   struct kdbus_cmd_free cmd;
87
88   /*
89    * Kdbus requires to initialize ioctl params partially. Some parts
90    * are for data passed from user to kernel, and other parts
91    * for data passed from kernel to user.
92    *
93    * Valgrind detects when uninitialized data is passed to kernel
94    * and has no way to know that it is meant to be filled by kernel.
95    * Thus, we initialize params for Valgrind to stop complaining.
96    */
97   VALGRIND_MAKE_MEM_DEFINED (&cmd, sizeof (cmd));
98
99   cmd.size = sizeof (cmd);
100   cmd.offset = offset;
101   cmd.flags = 0;
102
103   if (safe_ioctl (kdbus->fd, KDBUS_CMD_FREE, &cmd )!= 0)
104     return errno;
105
106   return 0;
107 }
108
109 static void make_item_name (const char *name, struct kdbus_item *item)
110 {
111   size_t len = strlen (name) + 1;
112   item->size = KDBUS_ITEM_HEADER_SIZE + len;
113   item->type = KDBUS_ITEM_NAME;
114
115   memcpy (item->str, name, len);
116 }
117
118 /**
119  * Adds an item in the current position of items array.
120  *
121  * @param item item to fill
122  * @param item_type type of the item
123  * @param string value of the item
124  * @param string_size size of the value
125  * @returns pointer to the next item
126  */
127 struct kdbus_item *
128 _kdbus_item_add_string (struct kdbus_item *item,
129                         __u64              item_type,
130                         const char        *item_string,
131                         __u64              item_string_size)
132 {
133   item->size = KDBUS_ITEM_HEADER_SIZE + item_string_size;
134   item->type = item_type;
135   memcpy (item->str, item_string, item_string_size);
136   return KDBUS_ITEM_NEXT (item);
137 }
138
139 struct kdbus_item *
140 _kdbus_item_add_payload_memfd (struct kdbus_item *item,
141                                __u64              start,
142                                __u64              size,
143                                int                fd)
144 {
145   item->type = KDBUS_ITEM_PAYLOAD_MEMFD;
146   item->size = KDBUS_ITEM_HEADER_SIZE + sizeof (struct kdbus_memfd);
147   item->memfd.start = start;
148   item->memfd.size = size;
149   item->memfd.fd = fd;
150   return KDBUS_ITEM_NEXT (item);
151 }
152
153 struct kdbus_item *
154 _kdbus_item_add_payload_vec (struct kdbus_item *item,
155                              __u64              size,
156                              __u64              address_or_offset)
157 {
158   item->type = KDBUS_ITEM_PAYLOAD_VEC;
159   item->size = KDBUS_ITEM_HEADER_SIZE + sizeof (struct kdbus_vec);
160   item->vec.size = size;
161   item->vec.address = address_or_offset;
162   return KDBUS_ITEM_NEXT (item);
163 }
164
165 struct kdbus_item *
166 _kdbus_item_add_fds (struct kdbus_item *item,
167                      const int         *fds,
168                      int                fds_count)
169 {
170   item->type = KDBUS_ITEM_FDS;
171   item->size = KDBUS_ITEM_HEADER_SIZE + (__u64)fds_count * sizeof (int);
172   memcpy (item->fds, fds, fds_count * sizeof (int));
173   return KDBUS_ITEM_NEXT (item);
174 }
175
176 struct kdbus_item *
177 _kdbus_item_add_bloom_filter (struct kdbus_item          *item,
178                               kdbus_t                    *kdbus,
179                               struct kdbus_bloom_filter **out_ptr)
180 {
181   item->type = KDBUS_ITEM_BLOOM_FILTER;
182   item->size = KDBUS_ITEM_HEADER_SIZE
183                + sizeof (struct kdbus_bloom_filter)
184                + kdbus->bloom.size;
185   memset (item->bloom_filter.data, 0, kdbus->bloom.size);
186   item->bloom_filter.generation = 0;
187   *out_ptr = &item->bloom_filter;
188   return KDBUS_ITEM_NEXT (item);
189 }
190
191 kdbus_bloom_data_t *
192 _kdbus_bloom_filter_get_data (struct kdbus_bloom_filter *bloom_filter)
193 {
194   return bloom_filter->data;
195 }
196
197 struct kdbus_item *
198 _kdbus_item_add_name_change (struct kdbus_item *item,
199                              __u64              old_id,
200                              __u64              old_id_flags,
201                              __u64              new_id,
202                              __u64              new_id_flags)
203 {
204   item->size = KDBUS_ITEM_HEADER_SIZE + sizeof (struct kdbus_notify_name_change);
205   item->type = KDBUS_ITEM_NAME_CHANGE;
206   item->name_change.old_id.id = old_id;
207   item->name_change.old_id.flags = old_id_flags;
208   item->name_change.new_id.id = new_id;
209   item->name_change.new_id.flags = new_id_flags;
210   return KDBUS_ITEM_NEXT (item);
211 }
212
213 struct kdbus_item *
214 _kdbus_item_add_id_add (struct kdbus_item *item,
215                         __u64              id,
216                         __u64              id_flags)
217 {
218   item->size = KDBUS_ITEM_HEADER_SIZE + sizeof (struct kdbus_notify_id_change);
219   item->type = KDBUS_ITEM_ID_ADD;
220   item->id_change.id = id;
221   item->id_change.flags = id_flags;
222   return KDBUS_ITEM_NEXT (item);
223 }
224
225 struct kdbus_item *
226 _kdbus_item_add_id (struct kdbus_item *item,
227                     __u64              id)
228 {
229   item->size = KDBUS_ITEM_HEADER_SIZE + sizeof (id);
230   item->type = KDBUS_ITEM_ID;
231   item->id = id;
232   return KDBUS_ITEM_NEXT (item);
233 }
234
235 struct kdbus_item *
236 _kdbus_item_add_bloom_mask (struct kdbus_item   *item,
237                             kdbus_t             *kdbus,
238                             kdbus_bloom_data_t **bloom)
239 {
240   item->size = KDBUS_ITEM_HEADER_SIZE + kdbus->bloom.size;
241   item->type = KDBUS_ITEM_BLOOM_MASK;
242   memset (item->data64, 0, kdbus->bloom.size);
243   if (NULL != bloom)
244     *bloom = item->data64;
245   return KDBUS_ITEM_NEXT (item);
246 }
247
248 static inline void *
249 get_from_offset (kdbus_t *kdbus,
250                  __u64    offset)
251 {
252   return ((char *)kdbus->mmap_ptr) + offset;
253 }
254
255 kdbus_t *
256 _kdbus_new (const char *path)
257 {
258   kdbus_t *kdbus = dbus_new (kdbus_t, 1);
259   if (NULL != kdbus)
260     {
261       kdbus->path = _dbus_strdup(path);
262       if (NULL == kdbus->path)
263         {
264           dbus_free (kdbus);
265           kdbus = NULL;
266         }
267     }
268   return kdbus;
269 }
270
271 void
272 _kdbus_free (kdbus_t *kdbus)
273 {
274   dbus_free ((char*)kdbus->path);
275   dbus_free (kdbus);
276 }
277
278 /**
279  * Opens a connection to the kdbus bus
280  *
281  * @param kdbus kdbus object
282  * @returns 0 on success, -errno on failure
283  */
284 int
285 _kdbus_open (kdbus_t *kdbus)
286 {
287   int fd = open (kdbus->path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
288   if (-1 == fd)
289     return -errno;
290
291   kdbus->fd = fd;
292   return 0;
293 }
294
295 int
296 _kdbus_close (kdbus_t *kdbus)
297 {
298   int ret;
299   int errclose = 0;
300   int errunmap = 0;
301
302   do
303   {
304     ret = close (kdbus->fd);
305   } while (-1 == ret && EINTR == errno);
306   if (-1 == ret)
307     errclose = errno;
308
309   ret = munmap (kdbus->mmap_ptr, kdbus->pool_size);
310   if (-1 == ret)
311     errunmap = errno;
312
313   if (0 != errclose)
314     return -errclose;
315   if (0 != errunmap)
316     return -errunmap;
317   return 0;
318 }
319
320 const char *
321 _kdbus_get_path (kdbus_t *kdbus)
322 {
323   return kdbus->path;
324 }
325
326 int
327 _kdbus_get_fd (kdbus_t *kdbus)
328 {
329   return kdbus->fd;
330 }
331
332 __u64
333 _kdbus_get_id (kdbus_t *kdbus)
334 {
335   return kdbus->id;
336 }
337
338 char *
339 _kdbus_get_bus_id (kdbus_t *kdbus)
340 {
341   return kdbus->bus_id;
342 }
343
344 __u64
345 _kdbus_get_bus_id_size (void)
346 {
347   return sizeof (((struct kdbus_t *)(0))->bus_id);
348 }
349
350 int
351 _kdbus_hello (kdbus_t       *kdbus,
352               __u64          flags,
353               __u64          attach_flags_send,
354               __u64          attach_flags_recv,
355               __u64          pool_size,
356               const char    *activator_name,
357               const char    *connection_name)
358 {
359   struct kdbus_cmd_hello *hello;
360   struct kdbus_item *item, *items;
361   __u64 hello_size;
362   size_t activator_name_size = 0;
363   size_t connection_name_size = 0;
364   __u64 offset;
365   __u64 items_size;
366
367   hello_size = sizeof (struct kdbus_cmd_hello);
368
369   if (NULL != activator_name)
370     {
371       activator_name_size = strlen (activator_name) + 1;
372       hello_size += KDBUS_ITEM_SIZE (activator_name_size);
373     }
374
375   if (NULL != connection_name)
376     {
377       connection_name_size  = strlen (connection_name) + 1;
378       hello_size += KDBUS_ITEM_SIZE (connection_name_size);
379     }
380
381   hello = dbus_malloc (hello_size);
382   if (NULL == hello)
383     return -ENOMEM;
384
385   VALGRIND_MAKE_MEM_DEFINED (hello, hello_size);
386
387   hello->flags = flags;
388   hello->attach_flags_send = attach_flags_send;
389   hello->attach_flags_recv = attach_flags_recv;
390   hello->pool_size = pool_size;
391
392   item = hello->items;
393   if (connection_name_size > 0)
394     item = _kdbus_item_add_string (item,
395                                    KDBUS_ITEM_CONN_DESCRIPTION,
396                                    connection_name,
397                                    connection_name_size);
398   if (activator_name_size > 0)
399     {
400       _kdbus_item_add_string (item,
401                               KDBUS_ITEM_NAME,
402                               activator_name,
403                               activator_name_size);
404       hello->flags |= KDBUS_HELLO_ACTIVATOR;
405     }
406
407   hello->size = hello_size;
408
409   if (safe_ioctl (kdbus->fd, KDBUS_CMD_HELLO, hello) != 0)
410     {
411       dbus_free (hello);
412       return -errno;
413     }
414
415   kdbus->id = hello->id;
416   memcpy (kdbus->bus_id, hello->id128, sizeof (kdbus->bus_id));
417
418   offset = hello->offset;
419   items_size = hello->items_size;
420   dbus_free (hello);
421
422   kdbus->mmap_ptr = mmap (NULL, pool_size, PROT_READ, MAP_SHARED, kdbus->fd, 0);
423   if (MAP_FAILED == kdbus->mmap_ptr)
424       return -errno;
425
426   kdbus->pool_size = pool_size;
427
428   items = get_from_offset (kdbus, offset);
429   KDBUS_FOREACH (item, items, items_size)
430     {
431       if (KDBUS_ITEM_BLOOM_PARAMETER == item->type)
432         kdbus->bloom = item->bloom_parameter;
433     }
434
435   free_by_offset (kdbus, offset);
436
437   return 0;
438 }
439
440 int
441 _kdbus_send (kdbus_t           *kdbus,
442              __u64              flags,
443              struct kdbus_msg  *msg,
444              struct kdbus_msg **msg_reply)
445 {
446   struct kdbus_cmd_send cmd;
447
448   VALGRIND_MAKE_MEM_DEFINED (&cmd, sizeof (cmd));
449
450   cmd.size = sizeof (cmd);
451   cmd.msg_address = (uintptr_t)msg;
452   cmd.flags = flags;
453
454   if (-1 == safe_ioctl (kdbus->fd, KDBUS_CMD_SEND, &cmd))
455     return errno;
456
457   if (flags & KDBUS_SEND_SYNC_REPLY)
458     {
459       if (NULL != msg_reply)
460         *msg_reply = get_from_offset (kdbus, cmd.reply.offset);
461       else
462         free_by_offset (kdbus, cmd.reply.offset);
463     }
464
465   return 0;
466 }
467
468 int
469 _kdbus_recv (kdbus_t           *kdbus,
470              __u64              flags,
471              __s64              priority,
472              struct kdbus_msg **msg)
473 {
474   struct kdbus_cmd_recv cmd;
475
476   VALGRIND_MAKE_MEM_DEFINED (&cmd, sizeof (cmd));
477
478   cmd.size = sizeof (cmd);
479   cmd.flags = flags;
480   cmd.priority = priority;
481
482   if (-1 == safe_ioctl (kdbus->fd, KDBUS_CMD_RECV, &cmd))
483     return errno;
484
485   *msg = get_from_offset (kdbus, cmd.msg.offset);
486
487   return 0;
488 }
489
490 /**
491  * Provides list of names on the bus.
492  *
493  * @param kdbus kdbus object
494  * @param flags specification of required names. See enum kdbus_list_flags in kdbus.h.
495  * @param name_list data from kdbus response - the list
496  * @param list_size size of name_list in bytes
497  * @return 0 on success, errno on failure
498  *
499  * Note that name_list is allocated in kdbus pool and must be freed by _kdbus_free_mem.
500  */
501 int
502 _kdbus_list (kdbus_t            *kdbus,
503              __u64               flags,
504              struct kdbus_info **name_list,
505              __u64              *list_size)
506 {
507   struct kdbus_cmd_list cmd;
508
509   VALGRIND_MAKE_MEM_DEFINED (&cmd, sizeof (cmd));
510
511   cmd.size = sizeof (cmd);
512   cmd.flags = flags;
513
514   if (-1 == safe_ioctl (kdbus->fd, KDBUS_CMD_LIST, &cmd))
515     return errno;
516
517   *name_list = get_from_offset (kdbus, cmd.offset);
518   *list_size = cmd.list_size;
519
520   return 0;
521 }
522
523 __u64
524 _kdbus_compute_match_items_size (kdbus_t       *kdbus,
525                                  dbus_bool_t    with_bloom_mask,
526                                  __u64          sender_id,
527                                  const char    *sender_name)
528 {
529   __u64 size = 0;
530
531   if (with_bloom_mask)
532     size += KDBUS_ITEM_SIZE (kdbus->bloom.size);
533
534   if (KDBUS_MATCH_ID_ANY != sender_id) /* unique name present */
535     size += KDBUS_ITEM_SIZE (sizeof (sender_id));
536   else if (NULL != sender_name)
537     size += KDBUS_ITEM_SIZE (strlen (sender_name) + 1);
538
539   return size;
540 }
541
542 struct kdbus_cmd_match *
543 _kdbus_new_cmd_match (kdbus_t       *kdbus,
544                       __u64          items_size,
545                       __u64          flags,
546                       __u64          cookie)
547 {
548   struct kdbus_cmd_match *cmd;
549   __u64 cmd_size = sizeof (*cmd) + items_size;
550   cmd = dbus_malloc (cmd_size);
551   if (NULL == cmd)
552     return NULL;
553
554   VALGRIND_MAKE_MEM_DEFINED (cmd, cmd_size);
555
556   cmd->size = cmd_size;
557   cmd->flags = flags;
558   cmd->cookie = cookie;
559
560   return cmd;
561 }
562
563 void
564 _kdbus_free_cmd_match (struct kdbus_cmd_match *cmd)
565 {
566   dbus_free (cmd);
567 }
568
569 int
570 _kdbus_add_match_name_acquired (kdbus_t *kdbus,
571                             __u64    flags,
572                             __u64    cookie,
573                             __u64    old_id,
574                             __u64    old_id_flags,
575                             __u64    new_id,
576                             __u64    new_id_flags,
577                             const char *name)
578 {
579   struct kdbus_cmd_match *cmd;
580   struct kdbus_item *item;
581   int ret;
582   unsigned int len = 0;
583
584   if (name)
585     len = strlen(name) + 1;
586   cmd = _kdbus_new_cmd_match (kdbus,
587                               KDBUS_ITEM_SIZE (sizeof (struct kdbus_notify_name_change) + len),
588                               flags,
589                               cookie);
590   if (NULL == cmd)
591     return ENOMEM;
592
593   item = cmd->items;
594   _kdbus_item_add_name_change (item,
595                                old_id, old_id_flags,
596                                new_id, new_id_flags);
597   item->size += len;
598   if (name)
599     memcpy(item->name_change.name, name, len);
600
601   ret = safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_ADD, cmd);
602   if (ret != 0)
603     goto err_name_acquired;
604
605   item->type = KDBUS_ITEM_NAME_ADD;
606   ret = safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_ADD, cmd);
607   if (ret != 0)
608     goto err_name_acquired;
609
610 err_name_acquired:
611   if (0 != ret)
612     ret = errno;
613   _kdbus_free_cmd_match (cmd);
614   return ret;
615 }
616
617 int
618 _kdbus_add_match_name_lost (kdbus_t *kdbus,
619                             __u64    flags,
620                             __u64    cookie,
621                             __u64    old_id,
622                             __u64    old_id_flags,
623                             __u64    new_id,
624                             __u64    new_id_flags,
625                             const char *name)
626 {
627   struct kdbus_cmd_match *cmd;
628   struct kdbus_item *item;
629   int ret;
630   unsigned int len = 0;
631
632   if (name)
633     len = strlen(name) + 1;
634
635   cmd = _kdbus_new_cmd_match (kdbus,
636                               KDBUS_ITEM_SIZE (sizeof (struct kdbus_notify_name_change) + len),
637                               flags,
638                               cookie);
639   if (NULL == cmd)
640     return ENOMEM;
641
642   item = cmd->items;
643   _kdbus_item_add_name_change (item,
644                                old_id, old_id_flags,
645                                new_id, new_id_flags);
646
647   item->size += len;
648   if (name)
649   memcpy(item->name_change.name, name, len);
650
651   ret = safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_ADD, cmd);
652   if (ret != 0)
653     goto err_name_remove;
654
655   item->type = KDBUS_ITEM_NAME_REMOVE;
656   ret = safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_ADD, cmd);
657   if (ret != 0)
658     goto err_name_remove;
659
660 err_name_remove:
661   if (0 != ret)
662     ret = errno;
663   _kdbus_free_cmd_match (cmd);
664   return ret;
665 }
666
667 int
668 _kdbus_add_match_name_change (kdbus_t *kdbus,
669                               __u64    flags,
670                               __u64    cookie,
671                               __u64    old_id,
672                               __u64    old_id_flags,
673                               __u64    new_id,
674                               __u64    new_id_flags,
675                               const char *name)
676 {
677   struct kdbus_cmd_match *cmd;
678   struct kdbus_item *item;
679   int ret;
680   unsigned int len = 0;
681
682   if (name)
683     len = strlen(name) + 1;
684
685   /* name = NULL or Well-known Name */
686   if (name == NULL || (name[0] != ':')) {
687     cmd = _kdbus_new_cmd_match (kdbus,
688                                 KDBUS_ITEM_SIZE (sizeof (struct kdbus_notify_name_change) + len),
689                                 flags,
690                                 cookie);
691     if (NULL == cmd)
692       return ENOMEM;
693
694     item = cmd->items;
695     _kdbus_item_add_name_change (item,
696                                  old_id, old_id_flags,
697                                  new_id, new_id_flags);
698
699     item->size += len;
700     if (name)
701       memcpy(item->name_change.name, name, len);
702
703     ret = safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_ADD, cmd);
704     if (0 == ret)
705       {
706         item->type = KDBUS_ITEM_NAME_ADD;
707         ret = safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_ADD, cmd);
708         if (0 == ret)
709           {
710            item->type = KDBUS_ITEM_NAME_REMOVE;
711            ret = safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_ADD, cmd);
712           }
713       }
714
715     if (0 != ret)
716       ret = errno;
717
718     _kdbus_free_cmd_match (cmd);
719     return ret;
720   }
721
722   return FALSE;
723 }
724
725 int
726 _kdbus_add_match_id_change (kdbus_t *kdbus,
727                             __u64    flags,
728                             __u64    cookie,
729                             __u64    id,
730                             __u64    id_flags,
731                             const char *name)
732 {
733   struct kdbus_cmd_match *cmd;
734   struct kdbus_item *item;
735   int ret;
736
737   /* name = NULL or Unique Name */
738   if (name == NULL || name[0] == ':')
739     {
740     cmd = _kdbus_new_cmd_match (kdbus,
741                                 KDBUS_ITEM_SIZE (sizeof (struct kdbus_notify_id_change)),
742                                 flags,
743                                 cookie);
744     if (NULL == cmd)
745       return ENOMEM;
746
747     item = cmd->items;
748     _kdbus_item_add_id_add (item, id, id_flags);
749
750     if (name)
751       item->id_change.id = strtoull (name + 3, NULL, 10);
752
753     ret = safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_ADD, cmd);
754     if (0 == ret)
755       {
756         item->type = KDBUS_ITEM_ID_REMOVE;
757         ret = safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_ADD, cmd);
758       }
759
760    if (0 != ret)
761      ret = errno;
762
763     _kdbus_free_cmd_match (cmd);
764     return ret;
765   }
766   return FALSE;
767 }
768
769 int _kdbus_add_match (kdbus_t *kdbus,
770                       struct kdbus_cmd_match *cmd)
771 {
772   int ret = safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_ADD, cmd);
773   if (0 != ret)
774     return errno;
775
776   return 0;
777 }
778
779 /**
780  * Allocates and initializes kdbus message structure.
781  * @param kdbus kdbus object
782  * @param size_for_items size of items that will be attached to this message
783  * @param flags flags for message
784  * @returns initialized kdbus message or NULL if malloc failed
785  */
786 struct kdbus_msg *
787 _kdbus_new_msg (kdbus_t                *kdbus,
788                 __u64                   size_for_items,
789                 __u64                   flags,
790                 __s64                   priority,
791                 __u64                   dst_id,
792                 __u64                   src_id,
793                 enum kdbus_payload_type payload_type,
794                 __u64                   cookie,
795                 __u64                   timeout_ns_or_cookie_reply)
796 {
797   struct kdbus_msg *msg;
798   __u64 msg_size = sizeof (struct kdbus_msg) + size_for_items;
799
800   msg = dbus_malloc (msg_size);
801   if (NULL == msg)
802     return NULL;
803
804   msg->size = msg_size;
805   msg->flags = flags;
806   msg->priority = priority;
807   msg->dst_id = dst_id;
808   msg->src_id = src_id;
809   msg->payload_type = payload_type;
810   msg->cookie = cookie;
811   msg->timeout_ns = timeout_ns_or_cookie_reply;
812
813   return msg;
814 }
815
816 void
817 _kdbus_free_msg (struct kdbus_msg *msg)
818 {
819   dbus_free (msg);
820 }
821
822 int
823 _kdbus_free_mem (kdbus_t *kdbus, void *mem)
824 {
825   char *base_ptr = kdbus->mmap_ptr;
826   char *mem_ptr = (char *)mem;
827
828   return free_by_offset (kdbus, mem_ptr - base_ptr);
829 }
830
831 /**
832  * Computes size of items that will be attached to a message.
833  *
834  * @param kdbus kdbus object
835  * @param destination Well-known name or NULL. If NULL, dst_id must be supplied.
836  * @param dst_id Numeric id of recipient. Ignored if name is not NULL.
837  * @param body_size Size of message body (may be 0).
838  * @param use_memfd Flag to build memfd message.
839  * @param fds_count Number of file descriptors sent in the message.
840  * @returns size in bytes needed for the message object
841  */
842 __u64
843 _kdbus_compute_msg_items_size (kdbus_t       *kdbus,
844                                const char    *destination,
845                                __u64          dst_id,
846                                __u64          body_size,
847                                dbus_bool_t    use_memfd,
848                                int            fds_count)
849 {
850   __u64 items_size = 0;
851
852   /*  header */
853   items_size += KDBUS_ITEM_SIZE (sizeof (struct kdbus_vec));
854
855   if (use_memfd)
856     {
857       /* body */
858       items_size += KDBUS_ITEM_SIZE (sizeof (struct kdbus_memfd));
859
860       /* footer */
861       items_size += KDBUS_ITEM_SIZE (sizeof (struct kdbus_vec));
862     }
863   else
864     {
865       __u64 vectors = (body_size + KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE - 1)
866                        / KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE;
867       /* subsequent vectors -> parts of body */
868       items_size += vectors * KDBUS_ITEM_SIZE (sizeof (struct kdbus_vec));
869     }
870
871   if (fds_count > 0)
872     items_size += KDBUS_ITEM_SIZE (sizeof (int) * fds_count);
873
874   if (destination)
875     items_size += KDBUS_ITEM_SIZE (strlen (destination) + 1);
876   else if (KDBUS_DST_ID_BROADCAST == dst_id)
877     items_size += KDBUS_ITEM_SIZE (sizeof (struct kdbus_bloom_filter))
878                   + kdbus->bloom.size;
879   return items_size;
880 }
881
882 /**
883  *
884  * Asks the bus to assign the given name to the connection.
885  *
886  * Use same flags as original dbus version with one exception below.
887  * Result flag #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER is currently
888  * never returned by kdbus, instead DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
889  * is returned by kdbus.
890  *
891  * @param transport transport of the connection
892  * @param name the name to request
893  * @param flags flags
894  * @returns a DBus result code on success, -errno on error
895  */
896 int
897 _kdbus_request_name (kdbus_t     *kdbus,
898                      const char  *name,
899                      const __u64  flags)
900 {
901   struct kdbus_cmd *cmd_name;
902   size_t len = strlen (name) + 1;
903   __u64 size = sizeof (*cmd_name) + KDBUS_ITEM_SIZE (len);
904   __u64 flags_kdbus = 0;
905
906   cmd_name = alloca (size);
907   cmd_name->size = size;
908
909   if (flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)
910     flags_kdbus |= KDBUS_NAME_ALLOW_REPLACEMENT;
911   if (!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE))
912     flags_kdbus |= KDBUS_NAME_QUEUE;
913   if (flags & DBUS_NAME_FLAG_REPLACE_EXISTING)
914     flags_kdbus |= KDBUS_NAME_REPLACE_EXISTING;
915
916   cmd_name->flags = flags_kdbus;
917   make_item_name (name, &(cmd_name->items[0]));
918
919   _dbus_verbose ("Request name - flags sent: 0x%llx       !!!!!!!!!\n", cmd_name->flags);
920
921   if (ioctl (kdbus->fd, KDBUS_CMD_NAME_ACQUIRE, cmd_name) < 0)
922     {
923       _dbus_verbose ("error acquiring name '%s': %m, %d\n", name, errno);
924       if (errno == EEXIST)
925         return DBUS_REQUEST_NAME_REPLY_EXISTS;
926       if (errno == EALREADY)
927         return DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
928       return -errno;
929     }
930   else if ((cmd_name->return_flags & KDBUS_NAME_PRIMARY)
931        && !(cmd_name->return_flags & KDBUS_NAME_ACQUIRED))
932     return DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
933
934   _dbus_verbose ("Request name - received flag: 0x%llx       !!!!!!!!!\n", cmd_name->flags);
935
936   if (cmd_name->return_flags & KDBUS_NAME_IN_QUEUE)
937     return DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
938
939   return DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
940 }
941
942 /**
943  *
944  * Releases well-known name - the connections resign from the name
945  * which can be then assigned to another connection or the connection
946  * is being removed from the queue for that name
947  *
948  * @param name the name to request
949  * @param id unique id of the connection for which the name is being released
950  * @returns a DBus result code on success, -errno on error
951  */
952 int
953 _kdbus_release_name (kdbus_t    *kdbus,
954                      const char *name)
955 {
956   struct kdbus_cmd *cmd_name;
957   size_t len = strlen (name)+1;
958   __u64 size = sizeof (*cmd_name) + KDBUS_ITEM_SIZE (len);
959
960   cmd_name = alloca (size);
961   cmd_name->size = size;
962   cmd_name->flags = 0;
963   make_item_name (name, &(cmd_name->items[0]));
964
965   if (ioctl (kdbus->fd, KDBUS_CMD_NAME_RELEASE, cmd_name))
966     {
967       if ((errno == ESRCH))
968         return DBUS_RELEASE_NAME_REPLY_NON_EXISTENT;
969       else if (errno == EADDRINUSE)
970         return DBUS_RELEASE_NAME_REPLY_NOT_OWNER;
971       _dbus_verbose ("error releasing name '%s'. Error: %m, %d\n", name, errno);
972       return -errno;
973     }
974
975   _dbus_verbose ("Name '%s' released\n", name);
976
977   return DBUS_RELEASE_NAME_REPLY_RELEASED;
978 }
979
980 static int
981 decode_connection_info (struct kdbus_info *connection_info,
982                         struct nameInfo   *pInfo,
983                         dbus_bool_t        get_sec_label)
984 {
985   struct kdbus_item *item;
986
987   memset (pInfo, 0, sizeof (*pInfo));
988
989   pInfo->uniqueId = connection_info->id;
990   pInfo->flags = connection_info->flags;
991
992   item = connection_info->items;
993
994   while ((uint8_t *)item < ((uint8_t *)connection_info) + connection_info->size)
995     {
996       switch (item->type)
997         {
998           case KDBUS_ITEM_PIDS:
999             pInfo->processId = item->pids.pid;
1000             break;
1001                   case KDBUS_ITEM_CREDS:
1002                           pInfo->userId = item->creds.uid;
1003                           break;
1004           case KDBUS_ITEM_SECLABEL:
1005               if (get_sec_label)
1006                 {
1007                   pInfo->sec_label_len = item->size - KDBUS_ITEM_HEADER_SIZE;
1008                   if (0 != pInfo->sec_label_len)
1009                     {
1010                       pInfo->sec_label = dbus_malloc (pInfo->sec_label_len);
1011                       if (NULL == pInfo->sec_label)
1012                         return ENOMEM;
1013
1014                       memcpy (pInfo->sec_label, item->data, pInfo->sec_label_len);
1015                     }
1016                 }
1017               break;
1018         }
1019
1020       item = KDBUS_ITEM_NEXT (item);
1021     }
1022   return 0;
1023 }
1024
1025 static int
1026 process_connection_info_cmd (kdbus_t               *kdbus,
1027                              struct kdbus_cmd_info *cmd,
1028                              struct nameInfo       *pInfo,
1029                              dbus_bool_t            get_sec_label)
1030 {
1031   int ret;
1032   struct kdbus_info *kdbus_info;
1033
1034   if (NULL == cmd)
1035     return -1;
1036
1037   ret = safe_ioctl (kdbus->fd, KDBUS_CMD_CONN_INFO, cmd);
1038
1039   if (ret < 0)
1040   {
1041     pInfo->uniqueId = 0;
1042     dbus_free(cmd);
1043     return errno;
1044   }
1045
1046   kdbus_info = get_from_offset (kdbus, cmd->offset);
1047   ret = decode_connection_info (kdbus_info,
1048                                 pInfo,
1049                                 get_sec_label);
1050   if (ret != 0) {
1051     dbus_free(cmd);
1052     return ret;
1053   }
1054
1055   ret = free_by_offset (kdbus, cmd->offset);
1056   if (ret != 0)
1057     {
1058       _dbus_verbose ("kdbus error freeing pool: %d (%m)\n", errno);
1059       if (get_sec_label)
1060         {
1061           free (pInfo->sec_label);
1062           pInfo->sec_label = NULL;
1063         }
1064     }
1065
1066   dbus_free (cmd);
1067
1068   return ret;
1069 }
1070
1071 /*
1072  * In this function either id is equal to 0 AND name is not NULL,
1073  * or id is greater than 0 AND name is NULL.
1074  * Thus, condition NULL != name is equivalent to 0 == id.
1075  */
1076 static struct kdbus_cmd_info *
1077 prepare_connection_info_cmd (__u64          id,
1078                              const char    *name,
1079                              dbus_bool_t    get_sec_label)
1080 {
1081   struct kdbus_cmd_info *cmd;
1082   __u64 size = sizeof (*cmd);
1083
1084   if (NULL != name)
1085     size += KDBUS_ITEM_SIZE (strlen (name) + 1);
1086
1087   cmd = dbus_malloc (size);
1088   if (NULL == cmd)
1089     return NULL;
1090
1091   cmd->size = size;
1092   cmd->id = id;
1093   if (NULL != name)
1094     make_item_name (name, &(cmd->items[0]));
1095
1096   cmd->attach_flags = KDBUS_ATTACH_CREDS | KDBUS_ATTACH_PIDS;
1097   if (get_sec_label)
1098     cmd->attach_flags |= KDBUS_ATTACH_SECLABEL;
1099
1100   cmd->flags = 0;
1101
1102   return cmd;
1103 }
1104
1105 /**
1106  * Gets connection info for the given unique id.
1107  *
1108  * @param kdbus kdbus object
1109  * @param id unique id to query for
1110  * @param get_sec_label #TRUE if sec_label field in pInfo should be filled
1111  * @param pInfo nameInfo structure address to store info about the name
1112  * @return 0 on success, errno if failed
1113  *
1114  * @note If you specify #TRUE in get_sec_label param, you must free
1115  * pInfo.sec_label with dbus_free() after use.
1116  */
1117 int
1118 _kdbus_connection_info_by_id (kdbus_t         *kdbus,
1119                               __u64            id,
1120                               dbus_bool_t      get_sec_label,
1121                               struct nameInfo *pInfo)
1122 {
1123   struct kdbus_cmd_info *cmd = prepare_connection_info_cmd (id, NULL, get_sec_label);
1124
1125   return process_connection_info_cmd (kdbus, cmd, pInfo, get_sec_label);
1126 }
1127
1128 /**
1129  * Gets connection info for the given name
1130  *
1131  * @param kdbus kdbus object
1132  * @param name name to query for
1133  * @param get_sec_label #TRUE if sec_label field in pInfo should be filled
1134  * @param pInfo nameInfo structure address to store info about the name
1135  * @return 0 on success, errno if failed
1136  *
1137  * @note If you specify #TRUE in get_sec_label param, you must free
1138  * pInfo.sec_label with dbus_free() after use.
1139  */
1140 int
1141 _kdbus_connection_info_by_name (kdbus_t         *kdbus,
1142                                 const char      *name,
1143                                 dbus_bool_t      get_sec_label,
1144                                 struct nameInfo *pInfo)
1145 {
1146   struct kdbus_cmd_info *cmd;
1147
1148   /* if name starts with ":1." it is a unique name and should be send as number */
1149   if ((name[0] == ':') && (name[1] == '1') && (name[2] == '.'))
1150   {
1151     return _kdbus_connection_info_by_id (kdbus,
1152                                          strtoull (&name[3], NULL, 10),
1153                                          get_sec_label,
1154                                          pInfo);
1155   }
1156
1157   cmd = prepare_connection_info_cmd (0, name, get_sec_label);
1158
1159   return process_connection_info_cmd (kdbus, cmd, pInfo, get_sec_label);
1160 }
1161
1162 /*
1163  * Removes match rule in kdbus on behalf of sender of the message
1164  * @param kdbus kdbus object
1165  * @param cookie cookie of the rules to be removed
1166  */
1167 int
1168 _kdbus_remove_match (kdbus_t    *kdbus,
1169                      __u64       cookie)
1170 {
1171   struct kdbus_cmd_match cmd;
1172
1173   VALGRIND_MAKE_MEM_DEFINED (&cmd, sizeof (cmd));
1174
1175   cmd.cookie = cookie;
1176   cmd.size = sizeof (struct kdbus_cmd_match);
1177   cmd.flags = 0;
1178
1179   if (safe_ioctl (kdbus->fd, KDBUS_CMD_MATCH_REMOVE, &cmd) != 0)
1180     return errno;
1181
1182   return 0;
1183 }
1184
1185 /************************* BLOOM FILTERS ***********************/
1186
1187 /*
1188  * Macros for SipHash algorithm
1189  */
1190 #define ROTL(x,b) (uint64_t)( ((x) << (b)) | ( (x) >> (64 - (b))) )
1191
1192 #define U32TO8_LE(p, v)         \
1193     (p)[0] = (unsigned char)((v)      ); (p)[1] = (unsigned char)((v) >>  8); \
1194     (p)[2] = (unsigned char)((v) >> 16); (p)[3] = (unsigned char)((v) >> 24);
1195
1196 #define U64TO8_LE(p, v)         \
1197   U32TO8_LE((p),     (uint32_t)((v)      ));   \
1198   U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
1199
1200 #define U8TO64_LE(p) \
1201   (((uint64_t)((p)[0])      ) | \
1202    ((uint64_t)((p)[1]) <<  8) | \
1203    ((uint64_t)((p)[2]) << 16) | \
1204    ((uint64_t)((p)[3]) << 24) | \
1205    ((uint64_t)((p)[4]) << 32) | \
1206    ((uint64_t)((p)[5]) << 40) | \
1207    ((uint64_t)((p)[6]) << 48) | \
1208    ((uint64_t)((p)[7]) << 56))
1209
1210 #define SIPROUND            \
1211   do {              \
1212     v0 += v1; v1=ROTL(v1,13); v1 ^= v0; v0=ROTL(v0,32); \
1213     v2 += v3; v3=ROTL(v3,16); v3 ^= v2;     \
1214     v0 += v3; v3=ROTL(v3,21); v3 ^= v0;     \
1215     v2 += v1; v1=ROTL(v1,17); v1 ^= v2; v2=ROTL(v2,32); \
1216   } while (0)
1217
1218
1219 /*
1220  * Hash keys for bloom filters
1221  */
1222 static const unsigned char hash_keys[8][16] =
1223 {
1224   {0xb9,0x66,0x0b,0xf0,0x46,0x70,0x47,0xc1,0x88,0x75,0xc4,0x9c,0x54,0xb9,0xbd,0x15},
1225   {0xaa,0xa1,0x54,0xa2,0xe0,0x71,0x4b,0x39,0xbf,0xe1,0xdd,0x2e,0x9f,0xc5,0x4a,0x3b},
1226   {0x63,0xfd,0xae,0xbe,0xcd,0x82,0x48,0x12,0xa1,0x6e,0x41,0x26,0xcb,0xfa,0xa0,0xc8},
1227   {0x23,0xbe,0x45,0x29,0x32,0xd2,0x46,0x2d,0x82,0x03,0x52,0x28,0xfe,0x37,0x17,0xf5},
1228   {0x56,0x3b,0xbf,0xee,0x5a,0x4f,0x43,0x39,0xaf,0xaa,0x94,0x08,0xdf,0xf0,0xfc,0x10},
1229   {0x31,0x80,0xc8,0x73,0xc7,0xea,0x46,0xd3,0xaa,0x25,0x75,0x0f,0x9e,0x4c,0x09,0x29},
1230   {0x7d,0xf7,0x18,0x4b,0x7b,0xa4,0x44,0xd5,0x85,0x3c,0x06,0xe0,0x65,0x53,0x96,0x6d},
1231   {0xf2,0x77,0xe9,0x6f,0x93,0xb5,0x4e,0x71,0x9a,0x0c,0x34,0x88,0x39,0x25,0xbf,0x35}
1232 };
1233
1234 /*
1235  * SipHash algorithm
1236  */
1237 static void
1238 _g_siphash24 (unsigned char       out[8],
1239               const void         *_in,
1240               size_t              inlen,
1241               const unsigned char k[16])
1242 {
1243   uint64_t v0 = 0x736f6d6570736575ULL;
1244   uint64_t v1 = 0x646f72616e646f6dULL;
1245   uint64_t v2 = 0x6c7967656e657261ULL;
1246   uint64_t v3 = 0x7465646279746573ULL;
1247   uint64_t b;
1248   uint64_t k0 = U8TO64_LE (k);
1249   uint64_t k1 = U8TO64_LE (k + 8);
1250   uint64_t m;
1251   const unsigned char *in = _in;
1252   const unsigned char *end = in + inlen - (inlen % sizeof (uint64_t));
1253   const int left = inlen & 7;
1254   b = ((uint64_t) inlen) << 56;
1255   v3 ^= k1;
1256   v2 ^= k0;
1257   v1 ^= k1;
1258   v0 ^= k0;
1259
1260   for (; in != end; in += 8)
1261     {
1262       m = U8TO64_LE (in);
1263       v3 ^= m;
1264       SIPROUND;
1265       SIPROUND;
1266       v0 ^= m;
1267     }
1268
1269   switch (left)
1270     {
1271       case 7: b |= ((uint64_t) in[6]) << 48;
1272       case 6: b |= ((uint64_t) in[5]) << 40;
1273       case 5: b |= ((uint64_t) in[4]) << 32;
1274       case 4: b |= ((uint64_t) in[3]) << 24;
1275       case 3: b |= ((uint64_t) in[2]) << 16;
1276       case 2: b |= ((uint64_t) in[1]) <<  8;
1277       case 1: b |= ((uint64_t) in[0]); break;
1278       case 0: break;
1279     }
1280
1281   v3 ^= b;
1282   SIPROUND;
1283   SIPROUND;
1284   v0 ^= b;
1285
1286   v2 ^= 0xff;
1287   SIPROUND;
1288   SIPROUND;
1289   SIPROUND;
1290   SIPROUND;
1291   b = v0 ^ v1 ^ v2  ^ v3;
1292   U64TO8_LE (out, b);
1293 }
1294
1295 void
1296 _kdbus_bloom_add_data (kdbus_t            *kdbus,
1297                        kdbus_bloom_data_t *bloom_data,
1298                        const void         *data,
1299                        size_t              data_size)
1300 {
1301   unsigned char hash[8];
1302   uint64_t bit_num;
1303   unsigned int bytes_num = 0;
1304   unsigned int cnt_1, cnt_2;
1305   unsigned int hash_index = 0;
1306
1307   unsigned int c = 0;
1308   uint64_t p = 0;
1309
1310   bit_num = kdbus->bloom.size * 8;
1311
1312   if (bit_num > 1)
1313     bytes_num = ((__builtin_clzll (bit_num) ^ 63U) + 7) / 8;
1314
1315   for (cnt_1 = 0; cnt_1 < kdbus->bloom.n_hash; cnt_1++)
1316     {
1317       for (cnt_2 = 0, hash_index = 0; cnt_2 < bytes_num; cnt_2++)
1318         {
1319           if (c <= 0)
1320             {
1321               _g_siphash24 (hash, data, data_size, hash_keys[hash_index++]);
1322               c += 8;
1323             }
1324
1325           p = (p << 8ULL) | (uint64_t) hash[8 - c];
1326           c--;
1327         }
1328
1329       p &= bit_num - 1;
1330       bloom_data[p >> 6] |= 1ULL << (p & 63);
1331     }
1332 }