Added NameList method handling, small improvemnets and cleanups
[platform/upstream/dbus.git] / dbus / dbus-transport-kdbus.c
1 /*
2  * dbus-transport-kdbus.c
3  *
4  * Transport layer using kdbus
5  *
6  *  Created on: Jun 20, 2013
7  *      Author: r.pajak
8  *
9  *
10  */
11
12 #include "dbus-transport.h"
13 #include "dbus-transport-kdbus.h"
14 #include <dbus/dbus-transport-protected.h>
15 #include "dbus-connection-internal.h"
16 #include "kdbus.h"
17 #include "dbus-watch.h"
18 #include "dbus-errors.h"
19 #include "dbus-bus.h"
20 #include <linux/types.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30 #include <limits.h>
31
32 #define KDBUS_ALIGN8(l) (((l) + 7) & ~7)
33 #define KDBUS_PART_HEADER_SIZE offsetof(struct kdbus_item, data)
34 #define KDBUS_ITEM_SIZE(s) KDBUS_ALIGN8((s) + KDBUS_PART_HEADER_SIZE)
35
36 #define KDBUS_PART_NEXT(part) \
37         (typeof(part))(((uint8_t *)part) + KDBUS_ALIGN8((part)->size))
38 #define KDBUS_PART_FOREACH(part, head, first)                           \
39         for (part = (head)->first;                                      \
40              (uint8_t *)(part) < (uint8_t *)(head) + (head)->size;      \
41              part = KDBUS_PART_NEXT(part))
42 #define RECEIVE_POOL_SIZE (10 * 1024LU * 1024LU)
43 #define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU) // over this memfd is used
44
45 #define KDBUS_MSG_DECODE_DEBUG 0
46
47
48 /**
49  * Opaque object representing a socket file descriptor transport.
50  */
51 typedef struct DBusTransportSocket DBusTransportSocket;
52
53 /**
54  * Implementation details of DBusTransportSocket. All members are private.
55  */
56 struct DBusTransportSocket
57 {
58   DBusTransport base;                   /**< Parent instance */
59   int fd;                               /**< File descriptor. */
60   DBusWatch *read_watch;                /**< Watch for readability. */
61   DBusWatch *write_watch;               /**< Watch for writability. */
62
63   int max_bytes_read_per_iteration;     /**< To avoid blocking too long. */
64   int max_bytes_written_per_iteration;  /**< To avoid blocking too long. */
65
66   int message_bytes_written;            /**< Number of bytes of current
67                                          *   outgoing message that have
68                                          *   been written.
69                                          */
70   DBusString encoded_outgoing;          /**< Encoded version of current
71                                          *   outgoing message.
72                                          */
73   DBusString encoded_incoming;          /**< Encoded version of current
74                                          *   incoming data.
75                                          */
76   void* kdbus_mmap_ptr;
77   int memfd;                            /*   File descriptor to memory
78                                          *   pool from Kdbus kernel module */
79 };
80
81
82
83 static dbus_bool_t
84 socket_get_socket_fd (DBusTransport *transport,
85                       int           *fd_p)
86 {
87   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
88
89   *fd_p = socket_transport->fd;
90
91   return TRUE;
92 }
93
94 //static void check_read_watch (DBusTransport *transport);
95
96 /*
97  * Adds locally generated message to received messages queue
98  *
99  */
100 static dbus_bool_t add_message_to_received(DBusMessage *message, DBusTransport *transport)
101 {
102         DBusList *message_link;
103
104         message_link = _dbus_list_alloc_link (message);
105         if (message_link == NULL)
106         {
107                 /* it's OK to unref this, nothing that could have attached a callback
108                  * has ever seen it */
109                 dbus_message_unref (message);
110                 return FALSE;
111         }
112
113         _dbus_connection_queue_synthesized_message_link(transport->connection, message_link);
114 //      check_read_watch(transport);
115
116         return TRUE;
117 }
118
119 /*static void kdbus_debug_print_bytes(struct DBusString *data, int size)
120 {
121     uint64_t i;
122
123     fprintf (stderr, "\ndata:\n");
124     for(i=0; i < size; i++)
125     {
126         fprintf (stderr, "%02x", _dbus_string_get_byte(data,i));
127     }
128     fprintf (stderr, "\nsize: %d, i: %lu\n", size, i);
129     
130 }*/
131
132 static int kdbus_init_memfd(DBusTransportSocket* socket_transport)
133 {
134         int memfd;
135         int ret;
136         
137         if(socket_transport->memfd == -1) 
138         {
139                 ret = ioctl(socket_transport->fd, KDBUS_CMD_MEMFD_NEW, &memfd);
140                 if (ret < 0) {
141                         _dbus_verbose("KDBUS_CMD_MEMFD_NEW failed: \n");
142                         return -1;
143                 }
144
145                 socket_transport->memfd = memfd;
146                 _dbus_verbose("kdbus_init_memfd: %d!!\n", socket_transport->memfd);
147
148         }
149         return 0;
150 }
151
152 static struct kdbus_msg* kdbus_init_msg(const char* name, __u64 dst_id, uint64_t body_size, dbus_bool_t use_memfd, int fds_count)
153 {
154     struct kdbus_msg* msg;
155     uint64_t msg_size;
156
157     msg_size = sizeof(struct kdbus_msg);
158
159     if(use_memfd == TRUE)  // bulk data - memfd - encoded and plain
160         msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_memfd));
161     else {
162         msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
163         if(body_size)
164                 msg_size += KDBUS_ITEM_SIZE(sizeof(struct kdbus_vec));
165     }
166
167     if(fds_count)
168         msg_size += KDBUS_ITEM_SIZE(sizeof(int)*fds_count);
169
170     if (name)
171         msg_size += KDBUS_ITEM_SIZE(strlen(name) + 1);
172     else if (dst_id == KDBUS_DST_ID_BROADCAST)
173         msg_size += KDBUS_PART_HEADER_SIZE + KDBUS_BLOOM_SIZE_BYTES;
174
175     msg = malloc(msg_size);
176     if (!msg)
177     {
178         _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
179                 return NULL;
180     }
181
182     memset(msg, 0, msg_size);
183     msg->size = msg_size;
184     msg->payload_type = KDBUS_PAYLOAD_DBUS1;
185     msg->dst_id = name ? 0 : dst_id;
186
187     return msg;
188 }
189
190 static int kdbus_write_msg(DBusTransportSocket *transport, DBusMessage *message, dbus_bool_t encoded)
191 {
192     struct kdbus_msg *msg;
193     struct kdbus_item *item;
194     const char *name;
195     uint64_t dst_id = KDBUS_DST_ID_BROADCAST;
196     const DBusString *header;
197     const DBusString *body;
198     uint64_t ret_size = 0;
199     uint64_t body_size = 0;
200     uint64_t header_size = 0;
201     int ret;
202     dbus_bool_t use_memfd;
203     const int *unix_fds;
204     unsigned fds_count;
205     char *buf;
206
207     // determine name and destination id
208     if((name = dbus_message_get_destination(message)))
209     {
210         dst_id = KDBUS_DST_ID_WELL_KNOWN_NAME;
211         
212                 if((name[0] == ':') && (name[1] == '1') && (name[2] == '.'))  /* if name starts with ":1." it is a unique name and should be send as number */
213         {
214                 dst_id = strtoll(&name[3], NULL, 10);
215                 name = NULL;
216         }    
217     }
218     
219     // get size of data
220     if(encoded)
221         ret_size = _dbus_string_get_length (&transport->encoded_outgoing);
222     else
223     {
224         _dbus_message_get_network_data (message, &header, &body);
225         header_size = _dbus_string_get_length(header);
226         body_size = _dbus_string_get_length(body);
227         ret_size = header_size + body_size;
228     }
229
230     // check if message size is big enough to use memfd kdbus transport
231     use_memfd = ret_size > MEMFD_SIZE_THRESHOLD ? TRUE : FALSE;
232     if(use_memfd) kdbus_init_memfd(transport);
233     
234     _dbus_message_get_unix_fds(message, &unix_fds, &fds_count);
235
236     // init basic message fields
237     msg = kdbus_init_msg(name, dst_id, body_size, use_memfd, fds_count);
238     msg->cookie = dbus_message_get_serial(message);
239     msg->src_id = strtoll(dbus_bus_get_unique_name(transport->base.connection), NULL , 10);
240     
241     // build message contents
242     item = msg->items;
243
244     // case 1 - bulk data transfer - memfd - encoded and plain
245     if(use_memfd)          
246     {
247             ret = ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 0);
248             if (ret < 0) 
249             {
250                         _dbus_verbose("memfd sealing failed: \n");
251                         goto out;
252             }
253
254             buf = mmap(NULL, ret_size, PROT_WRITE, MAP_SHARED, transport->memfd, 0);
255             if (buf == MAP_FAILED) 
256             {
257                         _dbus_verbose("mmap() fd=%i failed:%m", transport->memfd);
258                         goto out;
259             }
260
261                 if(encoded)
262                         memcpy(buf, &transport->encoded_outgoing, ret_size);
263                 else
264                 {
265                         memcpy(buf, _dbus_string_get_const_data(header), header_size);
266                         if(body_size) {
267                                 buf+=header_size;
268                                 memcpy(buf, _dbus_string_get_const_data(body),  body_size);
269                                 buf-=header_size;
270                         }
271                 }
272
273                 munmap(buf, ret_size);
274
275                 // seal data - kdbus module needs it
276                 ret = ioctl(transport->memfd, KDBUS_CMD_MEMFD_SEAL_SET, 1);
277                 if (ret < 0) {
278                         _dbus_verbose("memfd sealing failed: %d (%m)\n", errno);
279                         ret_size = -1;
280                         goto out;
281                 }
282
283             item->type = KDBUS_MSG_PAYLOAD_MEMFD;
284                 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_memfd);
285                 item->memfd.size = ret_size;
286                 item->memfd.fd = transport->memfd;
287     // case 2 - small encoded - don't use memfd
288     } else if(encoded) { 
289         _dbus_verbose("sending encoded data\n");
290
291         item->type = KDBUS_MSG_PAYLOAD_VEC;
292         item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec);
293         item->vec.address = (unsigned long) &transport->encoded_outgoing;
294         item->vec.size = _dbus_string_get_length (&transport->encoded_outgoing);
295
296     // case 3 - small not encoded - don't use memfd
297     } else { 
298         _dbus_verbose("sending normal vector data\n");
299
300         item->type = KDBUS_MSG_PAYLOAD_VEC;
301         item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec);
302         item->vec.address = (unsigned long) _dbus_string_get_const_data(header);
303         item->vec.size = header_size;
304
305         if(body_size)
306         {
307             _dbus_verbose("body attaching\n");
308                 item = KDBUS_PART_NEXT(item);
309                 item->type = KDBUS_MSG_PAYLOAD_VEC;
310                 item->size = KDBUS_PART_HEADER_SIZE + sizeof(struct kdbus_vec);
311                 item->vec.address = (unsigned long) _dbus_string_get_const_data(body);
312                 item->vec.size = body_size;
313         }
314     }
315
316     if(fds_count)
317     {
318         item = KDBUS_PART_NEXT(item);
319         item->type = KDBUS_MSG_FDS;
320         item->size = KDBUS_PART_HEADER_SIZE + (sizeof(int) * fds_count);
321         memcpy(item->fds, unix_fds, sizeof(int) * fds_count);
322     }
323
324         if (name)
325         {
326                 item = KDBUS_PART_NEXT(item);
327                 item->type = KDBUS_MSG_DST_NAME;
328                 item->size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
329                 strcpy(item->str, name);
330         }
331         else if (dst_id == KDBUS_DST_ID_BROADCAST)
332         {
333                 item = KDBUS_PART_NEXT(item);
334                 item->type = KDBUS_MSG_BLOOM;
335                 item->size = KDBUS_PART_HEADER_SIZE + KDBUS_BLOOM_SIZE_BYTES;
336                 strncpy(item->data, dbus_message_get_interface(message), KDBUS_BLOOM_SIZE_BYTES);
337         }
338
339         again:
340         if (ioctl(transport->fd, KDBUS_CMD_MSG_SEND, msg))
341         {
342                 if(errno == EINTR)
343                         goto again;
344                 if((errno == ESRCH) || (errno == ENXIO))  //when recipient is not available on the bus
345                 {
346                         DBusMessage *errMessage = NULL;
347                         dbus_uint32_t replySerial;
348
349                         errMessage = generate_local_error_message(msg->cookie, DBUS_ERROR_SERVICE_UNKNOWN, (char*)dbus_message_get_destination(message));
350                         if(errMessage == NULL)
351                         {
352                                 ret_size = -1;
353                                 goto out;
354                         }
355                         replySerial = dbus_message_get_reply_serial(message);
356                         if(replySerial)
357                                 dbus_message_set_reply_serial(errMessage, replySerial);
358                         if (!add_message_to_received(errMessage, (DBusTransport*)transport))
359                                 ret_size = -1;
360                         goto out;
361                 }
362                 _dbus_verbose("kdbus error sending message: err %d (%m)\n", errno);
363                 ret_size = -1;
364         }
365 out:
366     free(msg);
367
368     return ret_size;
369 }
370
371 static int64_t kdbus_NameQuery(DBusTransport *transport, char* name, int fd)
372 {
373         struct kdbus_cmd_name_info *msg;
374         struct kdbus_item *item;
375         uint64_t size;
376         int64_t ret;
377         uint64_t item_size;
378
379     item_size = KDBUS_PART_HEADER_SIZE + strlen(name) + 1;
380         item_size = (item_size < 56) ? 56 : item_size;
381     size = sizeof(struct kdbus_cmd_name_info) + item_size;
382
383         msg = malloc(size);
384         if (!msg)
385         {
386                 _dbus_verbose("Error allocating memory for: %s,%s\n", _dbus_strerror (errno), _dbus_error_from_errno (errno));
387                 return -1;
388         }
389
390         memset(msg, 0, size);
391         msg->size = size;
392
393         item = msg->items;
394         item->type = KDBUS_NAME_INFO_ITEM_NAME;
395         item->size = item_size;
396         strcpy(item->str, name);
397
398         again:
399         if ((ret = ioctl(fd, KDBUS_CMD_NAME_QUERY, msg)))
400         {
401                 if(errno == EINTR)
402                         goto again;
403                 ret = -errno;
404         }
405         else
406                 ret = msg->id;
407
408         free(msg);
409         return ret;
410 }
411
412 static dbus_bool_t emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message, int fd)
413 {
414         int64_t ret;
415
416         if(!strcmp(dbus_message_get_member(message), "GetNameOwner"))
417         {
418                 char* name = NULL;
419
420                 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
421                 _dbus_verbose ("Name to discover: %s   !!!!             !!!!\n", name);
422                 ret = kdbus_NameQuery(transport, name, fd);
423                 if(ret > 0) //unique id of the name
424                 {
425                         DBusMessage *reply;
426                         DBusMessageIter args;
427                         char unique_name[(unsigned int)(snprintf(name, 0, "%llu", ULLONG_MAX) + 4)]; //+3 prefix ":1." and +1 closing NULL
428                         const char* pString = unique_name;
429
430                         sprintf(unique_name, ":1.%lld", (long long int)ret);
431                         _dbus_verbose("Unique name discovered:%s!!!                       !!!!\n", unique_name);
432                         reply = dbus_message_new_method_return(message);
433                         if(reply == NULL)
434                                 return FALSE;
435                         dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
436                     dbus_message_iter_init_append(reply, &args);
437                     if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pString))
438                         return FALSE;
439                     if(add_message_to_received(reply, transport))
440                         return TRUE;
441                 }
442                 else if(ret == -ENOENT)  //name has no owner
443                 {
444                         DBusMessage *errMessage;
445                         dbus_uint32_t replySerial;
446
447                         errMessage = generate_local_error_message(1, DBUS_ERROR_NAME_HAS_NO_OWNER, name);
448                         if(errMessage == NULL)
449                                 return FALSE;
450                         replySerial = dbus_message_get_reply_serial(message);
451                         if(replySerial)
452                                 dbus_message_set_reply_serial(errMessage, replySerial);
453                         if (add_message_to_received(errMessage, transport))
454                                 return TRUE;
455                 }
456                 else
457                         _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
458         }
459         else if(!strcmp(dbus_message_get_member(message), "NameHasOwner"))
460         {
461                 char* name = NULL;
462                 DBusMessage *reply;
463                 DBusMessageIter args;
464                 dbus_bool_t result;
465
466                 dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
467                 _dbus_verbose ("Name to discover: %s   !!!!             !!!!\n", name);
468                 ret = kdbus_NameQuery(transport, name, fd);
469
470                 result = (ret > 0) ? TRUE : FALSE;
471                 _dbus_verbose("Discovery: %d !!!                       !!!!\n", (int)result);
472                 reply = dbus_message_new_method_return(message);
473                 if(reply == NULL)
474                         return FALSE;
475                 dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
476                 dbus_message_iter_init_append(reply, &args);
477                 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &result))
478                         return FALSE;
479                 if(add_message_to_received(reply, transport))
480                         return TRUE;
481         }
482         else if(!strcmp(dbus_message_get_member(message), "ListNames"))
483         {
484                 struct kdbus_cmd_names* pCmd;
485                 uint64_t cmd_size;
486
487                 cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
488                 pCmd = malloc(cmd_size);
489                 if(pCmd == NULL)
490                         goto out;
491                 pCmd->size = cmd_size;
492
493   again:
494                 cmd_size = 0;
495                 if(ioctl(fd, KDBUS_CMD_NAME_LIST, pCmd))
496                 {
497                         if(errno == EINTR)
498                                 goto again;
499                         if(errno == ENOBUFS)
500                                 cmd_size = pCmd->size;
501                         else
502                         {
503                                 _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
504                                 goto out;
505                         }
506                 }
507                 if(cmd_size)
508                 {
509                         pCmd = realloc(pCmd, cmd_size);
510                         if(pCmd == NULL)
511                                 return FALSE;
512                         goto again;
513                 }
514                 else
515                 {
516                         DBusMessage *reply;
517                         DBusMessageIter args;
518                         struct kdbus_cmd_name* pCmd_name;
519                         char* pName;
520
521                         reply = dbus_message_new_method_return(message);
522                         if(reply == NULL)
523                                 goto out;
524                         dbus_message_set_sender(reply, DBUS_SERVICE_DBUS);
525                         dbus_message_iter_init_append(reply, &args);
526
527                         for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
528                         {
529                                 pName = pCmd_name->name;
530                                 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pName))
531                                 goto out;
532                         }
533
534                         if(add_message_to_received(reply, transport))
535                         {
536                                 free(pCmd);
537                                 return TRUE;
538                         }
539                 }
540
541 out:
542                 if(pCmd)
543                         free(pCmd);
544         }
545         else  //temporarily we send that info but methods below should be implemented
546         {
547                 DBusMessage *reply;
548                 dbus_uint32_t replySerial;
549
550                 reply = generate_local_error_message(1, DBUS_ERROR_UNKNOWN_METHOD, NULL);
551                 if(reply == NULL)
552                         return FALSE;
553                 replySerial = dbus_message_get_reply_serial(message);
554                 if(replySerial)
555                         dbus_message_set_reply_serial(reply, replySerial);
556                 if(add_message_to_received(reply, transport))
557                         return TRUE;
558         }
559 /*      else if(!strcmp(dbus_message_get_member(message), "ListActivatableNames"))
560         {
561                 //todo
562         }
563         else if(!strcmp(dbus_message_get_member(message), "StartServiceByName"))
564         {
565                 //todo
566         }
567         else if(!strcmp(dbus_message_get_member(message), "UpdateActivationEnvironment"))
568         {
569                 //todo
570         }
571         else if(!strcmp(dbus_message_get_member(message), "GetConnectionUnixUser"))
572         {
573                 //todo
574         }
575         else if(!strcmp(dbus_message_get_member(message), "GetId"))
576         {
577                 //todo
578         }*/
579
580         return FALSE;
581 }
582
583 #if KDBUS_MSG_DECODE_DEBUG == 1
584 static char *msg_id(uint64_t id, char *buf)
585 {
586         if (id == 0)
587                 return "KERNEL";
588         if (id == ~0ULL)
589                 return "BROADCAST";
590         sprintf(buf, "%llu", (unsigned long long)id);
591         return buf;
592 }
593 #endif
594 struct kdbus_enum_table {
595         long long id;
596         const char *name;
597 };
598 #define _STRINGIFY(x) #x
599 #define STRINGIFY(x) _STRINGIFY(x)
600 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
601 #define TABLE(what) static struct kdbus_enum_table kdbus_table_##what[]
602 #define ENUM(_id) { .id=_id, .name=STRINGIFY(_id) }
603 #define LOOKUP(what)                                                            \
604         const char *enum_##what(long long id) {                                 \
605         size_t i; \
606                 for (i = 0; i < ELEMENTSOF(kdbus_table_##what); i++)    \
607                         if (id == kdbus_table_##what[i].id)                     \
608                                 return kdbus_table_##what[i].name;              \
609                 return "UNKNOWN";                                               \
610         }
611 const char *enum_MSG(long long id);
612 TABLE(MSG) = {
613         ENUM(_KDBUS_MSG_NULL),
614         ENUM(KDBUS_MSG_PAYLOAD_VEC),
615         ENUM(KDBUS_MSG_PAYLOAD_OFF),
616         ENUM(KDBUS_MSG_PAYLOAD_MEMFD),
617         ENUM(KDBUS_MSG_FDS),
618         ENUM(KDBUS_MSG_BLOOM),
619         ENUM(KDBUS_MSG_DST_NAME),
620         ENUM(KDBUS_MSG_SRC_CREDS),
621         ENUM(KDBUS_MSG_SRC_PID_COMM),
622         ENUM(KDBUS_MSG_SRC_TID_COMM),
623         ENUM(KDBUS_MSG_SRC_EXE),
624         ENUM(KDBUS_MSG_SRC_CMDLINE),
625         ENUM(KDBUS_MSG_SRC_CGROUP),
626         ENUM(KDBUS_MSG_SRC_CAPS),
627         ENUM(KDBUS_MSG_SRC_SECLABEL),
628         ENUM(KDBUS_MSG_SRC_AUDIT),
629         ENUM(KDBUS_MSG_SRC_NAMES),
630         ENUM(KDBUS_MSG_TIMESTAMP),
631         ENUM(KDBUS_MSG_NAME_ADD),
632         ENUM(KDBUS_MSG_NAME_REMOVE),
633         ENUM(KDBUS_MSG_NAME_CHANGE),
634         ENUM(KDBUS_MSG_ID_ADD),
635         ENUM(KDBUS_MSG_ID_REMOVE),
636         ENUM(KDBUS_MSG_REPLY_TIMEOUT),
637         ENUM(KDBUS_MSG_REPLY_DEAD),
638 };
639 LOOKUP(MSG);
640 const char *enum_PAYLOAD(long long id);
641 TABLE(PAYLOAD) = {
642         ENUM(KDBUS_PAYLOAD_KERNEL),
643         ENUM(KDBUS_PAYLOAD_DBUS1),
644         ENUM(KDBUS_PAYLOAD_GVARIANT),
645 };
646 LOOKUP(PAYLOAD);
647
648 static int put_message_into_data(DBusMessage *message, char* data)
649 {
650         int ret_size;
651     const DBusString *header;
652     const DBusString *body;
653     int size;
654
655     dbus_message_lock (message);
656     _dbus_message_get_network_data (message, &header, &body);
657     ret_size = _dbus_string_get_length(header);
658         memcpy(data, _dbus_string_get_const_data(header), ret_size);
659         data += ret_size;
660         size = _dbus_string_get_length(body);
661         memcpy(data, _dbus_string_get_const_data(body), size);
662         ret_size += size;
663
664         return ret_size;
665 }
666
667 static int kdbus_decode_msg(const struct kdbus_msg* msg, char *data, DBusTransportSocket* socket_transport, int* fds, int* n_fds)
668 {
669         const struct kdbus_item *item = msg->items;
670         int ret_size = 0;
671         DBusMessage *message = NULL;
672         DBusMessageIter args;
673         const char* emptyString = "";
674     const char* pString = NULL;
675         char dbus_name[(unsigned int)(snprintf((char*)pString, 0, "%llu", ULLONG_MAX) + 4)];  //+3 prefix ":1." and +1 closing NULL
676         const char* pDBusName = dbus_name;
677     void* mmap_ptr;
678 #if KDBUS_MSG_DECODE_DEBUG == 1
679         char buf[32];
680 #endif
681
682 #if KDBUS_MSG_DECODE_DEBUG == 1
683         _dbus_verbose("MESSAGE: %s (%llu bytes) flags=0x%llx, %s â†’ %s, cookie=%llu, timeout=%llu\n",
684                 enum_PAYLOAD(msg->payload_type), (unsigned long long) msg->size,
685                 (unsigned long long) msg->flags,
686                 msg_id(msg->src_id, buf), msg_id(msg->dst_id, buf),
687                 (unsigned long long) msg->cookie, (unsigned long long) msg->timeout_ns);
688 #endif
689
690         *n_fds = 0;
691         mmap_ptr = socket_transport->kdbus_mmap_ptr;
692
693         KDBUS_PART_FOREACH(item, msg, items)
694         {
695                 if (item->size <= KDBUS_PART_HEADER_SIZE)
696                 {
697                         _dbus_verbose("  +%s (%llu bytes) invalid data record\n", enum_MSG(item->type), item->size);
698                         break;  //??? continue (because dbus will find error) or break
699                 }
700
701                 switch (item->type)
702                 {
703                         case KDBUS_MSG_PAYLOAD_OFF:
704                         {
705                                 uint64_t size = item->vec.size;
706
707                                 memcpy(data, (char *)mmap_ptr + item->vec.offset, item->vec.size);
708                                 data += item->vec.size;
709                                 ret_size += item->vec.size;                     
710
711                                 _dbus_verbose("  +%s (%llu bytes) off=%llu size=%llu\n",
712                                         enum_MSG(item->type), item->size,
713                                         (unsigned long long)item->vec.offset,
714                                         (unsigned long long)size);
715                         break;
716                         }
717
718                         case KDBUS_MSG_PAYLOAD_MEMFD:
719                         {
720                                 char *buf;
721                                 uint64_t size;
722
723                  size = item->memfd.size;
724
725                                 _dbus_verbose("memfd.size : %llu\n", (unsigned long long)size);
726                                 
727                                 buf = mmap(NULL, size, PROT_READ , MAP_SHARED, item->memfd.fd, 0);
728
729                                 if (buf == MAP_FAILED) 
730                                 {
731                                         _dbus_verbose("mmap() fd=%i failed:%m", item->memfd.fd);
732                                         return -1;
733                                 }
734
735                                 memcpy(data, buf, size); 
736                                 data += size;
737                                 ret_size += size;
738
739                                 munmap(buf, size);
740
741                 _dbus_verbose("  +%s (%llu bytes) off=%llu size=%llu\n",
742                                            enum_MSG(item->type), item->size,
743                                            (unsigned long long)item->vec.offset,
744                                            (unsigned long long)item->vec.size);
745                         break;
746                         }
747
748                         case KDBUS_MSG_FDS:
749                         {
750                                 int i;
751
752                                 *n_fds = (item->size - KDBUS_PART_HEADER_SIZE) / sizeof(int);
753                                 memcpy(fds, item->fds, *n_fds * sizeof(int));
754                     for (i = 0; i < *n_fds; i++)
755                       _dbus_fd_set_close_on_exec(fds[i]);
756                         break;
757                         }
758
759 #if KDBUS_MSG_DECODE_DEBUG == 1
760                         case KDBUS_MSG_SRC_CREDS:
761                                 _dbus_verbose("  +%s (%llu bytes) uid=%lld, gid=%lld, pid=%lld, tid=%lld, starttime=%lld\n",
762                                         enum_MSG(item->type), item->size,
763                                         item->creds.uid, item->creds.gid,
764                                         item->creds.pid, item->creds.tid,
765                                         item->creds.starttime);
766                         break;
767
768                         case KDBUS_MSG_SRC_PID_COMM:
769                         case KDBUS_MSG_SRC_TID_COMM:
770                         case KDBUS_MSG_SRC_EXE:
771                         case KDBUS_MSG_SRC_CGROUP:
772                         case KDBUS_MSG_SRC_SECLABEL:
773                         case KDBUS_MSG_DST_NAME:
774                                 _dbus_verbose("  +%s (%llu bytes) '%s' (%zu)\n",
775                                            enum_MSG(item->type), item->size, item->str, strlen(item->str));
776                                 break;
777
778                         case KDBUS_MSG_SRC_CMDLINE:
779                         case KDBUS_MSG_SRC_NAMES: {
780                                 __u64 size = item->size - KDBUS_PART_HEADER_SIZE;
781                                 const char *str = item->str;
782                                 int count = 0;
783
784                                 _dbus_verbose("  +%s (%llu bytes) ", enum_MSG(item->type), item->size);
785                                 while (size) {
786                                         _dbus_verbose("'%s' ", str);
787                                         size -= strlen(str) + 1;
788                                         str += strlen(str) + 1;
789                                         count++;
790                                 }
791
792                                 _dbus_verbose("(%d string%s)\n", count, (count == 1) ? "" : "s");
793                                 break;
794                         }
795
796                         case KDBUS_MSG_SRC_AUDIT:
797                                 _dbus_verbose("  +%s (%llu bytes) loginuid=%llu sessionid=%llu\n",
798                                            enum_MSG(item->type), item->size,
799                                            (unsigned long long)item->data64[0],
800                                            (unsigned long long)item->data64[1]);
801                                 break;
802
803                         case KDBUS_MSG_SRC_CAPS: {
804                                 int n;
805                                 const uint32_t *cap;
806                                 int i;
807
808                                 _dbus_verbose("  +%s (%llu bytes) len=%llu bytes)\n",
809                                            enum_MSG(item->type), item->size,
810                                            (unsigned long long)item->size - KDBUS_PART_HEADER_SIZE);
811
812                                 cap = item->data32;
813                                 n = (item->size - KDBUS_PART_HEADER_SIZE) / 4 / sizeof(uint32_t);
814
815                                 _dbus_verbose("    CapInh=");
816                                 for (i = 0; i < n; i++)
817                                         _dbus_verbose("%08x", cap[(0 * n) + (n - i - 1)]);
818
819                                 _dbus_verbose(" CapPrm=");
820                                 for (i = 0; i < n; i++)
821                                         _dbus_verbose("%08x", cap[(1 * n) + (n - i - 1)]);
822
823                                 _dbus_verbose(" CapEff=");
824                                 for (i = 0; i < n; i++)
825                                         _dbus_verbose("%08x", cap[(2 * n) + (n - i - 1)]);
826
827                                 _dbus_verbose(" CapInh=");
828                                 for (i = 0; i < n; i++)
829                                         _dbus_verbose("%08x", cap[(3 * n) + (n - i - 1)]);
830                                 _dbus_verbose("\n");
831                                 break;
832                         }
833
834                         case KDBUS_MSG_TIMESTAMP:
835                                 _dbus_verbose("  +%s (%llu bytes) realtime=%lluns monotonic=%lluns\n",
836                                            enum_MSG(item->type), item->size,
837                                            (unsigned long long)item->timestamp.realtime_ns,
838                                            (unsigned long long)item->timestamp.monotonic_ns);
839                                 break;
840 #endif
841
842                         case KDBUS_MSG_REPLY_TIMEOUT:
843                                 _dbus_verbose("  +%s (%llu bytes) cookie=%llu\n",
844                                            enum_MSG(item->type), item->size, msg->cookie_reply);
845
846                                 message = generate_local_error_message(msg->cookie_reply, DBUS_ERROR_NO_REPLY, NULL);
847                                 if(message == NULL)
848                                 {
849                                         ret_size = -1;
850                                         goto out;
851                                 }
852
853                                 ret_size = put_message_into_data(message, data);
854                         break;
855
856                         case KDBUS_MSG_NAME_ADD:
857                                 _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
858                                         enum_MSG(item->type), (unsigned long long) item->size,
859                                         item->name_change.name, item->name_change.old_id,
860                                         item->name_change.new_id, item->name_change.flags);
861
862                                 message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
863                                             DBUS_INTERFACE_DBUS, // interface name of the signal
864                                             "NameOwnerChanged"); // name of the signal
865                                 if(message == NULL)
866                                 {
867                                         ret_size = -1;
868                                         goto out;
869                                 }
870
871                                 sprintf(dbus_name,":1.%llu",item->name_change.new_id);
872                                 pString = item->name_change.name;
873                                 _dbus_verbose ("Name added: %s\n", pString);
874                             dbus_message_iter_init_append(message, &args);
875                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,&pString))
876                                 {
877                                         ret_size = -1;
878                                 goto out;
879                                 }
880                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &emptyString))
881                                 {
882                                         ret_size = -1;
883                                 goto out;
884                                 }
885                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
886                                 {
887                                         ret_size = -1;
888                                 goto out;
889                                 }
890
891                                 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
892                                 dbus_message_set_serial(message, 1);
893
894                                 ret_size = put_message_into_data(message, data);
895                         break;
896
897                         case KDBUS_MSG_NAME_REMOVE:
898                                 _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
899                                         enum_MSG(item->type), (unsigned long long) item->size,
900                                         item->name_change.name, item->name_change.old_id,
901                                         item->name_change.new_id, item->name_change.flags);
902
903                                 message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
904                                             DBUS_INTERFACE_DBUS, // interface name of the signal
905                                             "NameOwnerChanged"); // name of the signal
906                                 if(message == NULL)
907                                 {
908                                         ret_size = -1;
909                                         goto out;
910                                 }
911
912                                 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
913                                 pString = item->name_change.name;
914                                 _dbus_verbose ("Name removed: %s\n", pString);
915                             dbus_message_iter_init_append(message, &args);
916                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,&pString))
917                                 {
918                                         ret_size = -1;
919                                 goto out;
920                                 }
921                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
922                                 {
923                                         ret_size = -1;
924                                 goto out;
925                                 }
926                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &emptyString))
927                                 {
928                                         ret_size = -1;
929                                 goto out;
930                                 }
931
932                                 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
933                                 dbus_message_set_serial(message, 1);
934
935                                 ret_size = put_message_into_data(message, data);
936                         break;
937
938                         case KDBUS_MSG_NAME_CHANGE:
939                                 _dbus_verbose("  +%s (%llu bytes) '%s', old id=%lld, new id=%lld, flags=0x%llx\n",
940                                         enum_MSG(item->type), (unsigned long long) item->size,
941                                         item->name_change.name, item->name_change.old_id,
942                                         item->name_change.new_id, item->name_change.flags);
943
944                                 message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
945                                             DBUS_INTERFACE_DBUS, // interface name of the signal
946                                             "NameOwnerChanged"); // name of the signal
947                                 if(message == NULL)
948                                 {
949                                         ret_size = -1;
950                                         goto out;
951                                 }
952
953                                 sprintf(dbus_name,":1.%llu",item->name_change.old_id);
954                                 pString = item->name_change.name;
955                                 _dbus_verbose ("Name changed: %s\n", pString);
956                             dbus_message_iter_init_append(message, &args);
957                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING,&pString))
958                                 {
959                                         ret_size = -1;
960                                 goto out;
961                                 }
962                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
963                                 {
964                                         ret_size = -1;
965                                 goto out;
966                                 }
967                             sprintf(&dbus_name[3],"%llu",item->name_change.new_id);
968                             _dbus_verbose ("New id: %s\n", pDBusName);
969                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
970                                 {
971                                         ret_size = -1;
972                                 goto out;
973                                 }
974
975                                 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
976                                 dbus_message_set_serial(message, 1);
977
978                                 ret_size = put_message_into_data(message, data);
979                         break;
980
981                         case KDBUS_MSG_ID_ADD:
982                                 _dbus_verbose("  +%s (%llu bytes) id=%llu flags=%llu\n",
983                                            enum_MSG(item->type), (unsigned long long) item->size,
984                                            (unsigned long long) item->id_change.id,
985                                            (unsigned long long) item->id_change.flags);
986
987                                 message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
988                                             DBUS_INTERFACE_DBUS, // interface name of the signal
989                                             "NameOwnerChanged"); // name of the signal
990                                 if(message == NULL)
991                                 {
992                                         ret_size = -1;
993                                         goto out;
994                                 }
995
996                                 sprintf(dbus_name,":1.%llu",item->id_change.id);
997                             dbus_message_iter_init_append(message, &args);
998                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
999                                 {
1000                                         ret_size = -1;
1001                                 goto out;
1002                                 }
1003                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &emptyString))
1004                                 {
1005                                         ret_size = -1;
1006                                 goto out;
1007                                 }
1008                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
1009                                 {
1010                                         ret_size = -1;
1011                                 goto out;
1012                                 }
1013
1014                                 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1015                                 dbus_message_set_serial(message, 1);
1016
1017                                 ret_size = put_message_into_data(message, data);
1018                         break;
1019
1020                         case KDBUS_MSG_ID_REMOVE:
1021                                 _dbus_verbose("  +%s (%llu bytes) id=%llu flags=%llu\n",
1022                                            enum_MSG(item->type), (unsigned long long) item->size,
1023                                            (unsigned long long) item->id_change.id,
1024                                            (unsigned long long) item->id_change.flags);
1025
1026                                 message = dbus_message_new_signal("/org/freedesktop/DBus", // object name of the signal
1027                                             DBUS_INTERFACE_DBUS, // interface name of the signal
1028                                             "NameOwnerChanged"); // name of the signal
1029                                 if(message == NULL)
1030                                 {
1031                                         ret_size = -1;
1032                                         goto out;
1033                                 }
1034
1035                                 sprintf(dbus_name,":1.%llu",item->id_change.id);
1036                             dbus_message_iter_init_append(message, &args);
1037                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
1038                                 {
1039                                         ret_size = -1;
1040                                 goto out;
1041                                 }
1042                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &pDBusName))
1043                                 {
1044                                         ret_size = -1;
1045                                 goto out;
1046                                 }
1047                             if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &emptyString))
1048                                 {
1049                                         ret_size = -1;
1050                                 goto out;
1051                                 }
1052
1053                                 dbus_message_set_sender(message, DBUS_SERVICE_DBUS);
1054                                 dbus_message_set_serial(message, 1);
1055
1056                                 ret_size = put_message_into_data(message, data);
1057                         break;
1058 #if KDBUS_MSG_DECODE_DEBUG == 1
1059                         default:
1060                                 _dbus_verbose("  +%s (%llu bytes)\n", enum_MSG(item->type), item->size);
1061                         break;
1062 #endif
1063                 }
1064         }
1065
1066 #if KDBUS_MSG_DECODE_DEBUG == 1
1067
1068         if ((char *)item - ((char *)msg + msg->size) >= 8)
1069                 _dbus_verbose("invalid padding at end of message\n");
1070 #endif
1071
1072 out:
1073         if(message)
1074                 dbus_message_unref(message);
1075         return ret_size;
1076 }
1077
1078 static int kdbus_read_message(DBusTransportSocket *socket_transport, DBusString *buffer, int* fds, int* n_fds)
1079 {
1080         int ret_size;
1081         uint64_t __attribute__ ((__aligned__(8))) offset;
1082         struct kdbus_msg *msg;
1083         char *data;
1084
1085         _dbus_assert (socket_transport->max_bytes_read_per_iteration >= 0);
1086         if (!_dbus_string_lengthen (buffer, socket_transport->max_bytes_read_per_iteration))
1087         {
1088                 errno = ENOMEM;
1089             return -1;
1090         }
1091         data = _dbus_string_get_data_len (buffer, 0, socket_transport->max_bytes_read_per_iteration);
1092
1093         again:
1094         if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RECV, &offset) < 0)
1095         {
1096                 if(errno == EINTR)
1097                         goto again;
1098                 _dbus_verbose("kdbus error receiving message: %d (%m)\n", errno);
1099                 _dbus_string_set_length (buffer, 0);
1100                 return -1;
1101         }
1102
1103         msg = (struct kdbus_msg *)((char*)socket_transport->kdbus_mmap_ptr + offset);
1104
1105         ret_size = kdbus_decode_msg(msg, data, socket_transport, fds, n_fds);
1106         _dbus_string_set_length (buffer, ret_size);
1107
1108         again2:
1109         if (ioctl(socket_transport->fd, KDBUS_CMD_MSG_RELEASE, &offset) < 0)
1110         {
1111                 if(errno == EINTR)
1112                         goto again2;
1113                 _dbus_verbose("kdbus error freeing message: %d (%m)\n", errno);
1114                 return -1;
1115         }
1116
1117         return ret_size;
1118 }
1119
1120 static void
1121 free_watches (DBusTransport *transport)
1122 {
1123   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1124
1125   _dbus_verbose ("start\n");
1126
1127   if (socket_transport->read_watch)
1128     {
1129       if (transport->connection)
1130         _dbus_connection_remove_watch_unlocked (transport->connection,
1131                                                 socket_transport->read_watch);
1132       _dbus_watch_invalidate (socket_transport->read_watch);
1133       _dbus_watch_unref (socket_transport->read_watch);
1134       socket_transport->read_watch = NULL;
1135     }
1136
1137   if (socket_transport->write_watch)
1138     {
1139       if (transport->connection)
1140         _dbus_connection_remove_watch_unlocked (transport->connection,
1141                                                 socket_transport->write_watch);
1142       _dbus_watch_invalidate (socket_transport->write_watch);
1143       _dbus_watch_unref (socket_transport->write_watch);
1144       socket_transport->write_watch = NULL;
1145     }
1146
1147   _dbus_verbose ("end\n");
1148 }
1149
1150 static void
1151 socket_finalize (DBusTransport *transport)
1152 {
1153   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1154
1155   _dbus_verbose ("\n");
1156
1157   free_watches (transport);
1158
1159   _dbus_string_free (&socket_transport->encoded_outgoing);
1160   _dbus_string_free (&socket_transport->encoded_incoming);
1161
1162   _dbus_transport_finalize_base (transport);
1163
1164   _dbus_assert (socket_transport->read_watch == NULL);
1165   _dbus_assert (socket_transport->write_watch == NULL);
1166
1167   dbus_free (transport);
1168 }
1169
1170 static void
1171 check_write_watch (DBusTransport *transport)
1172 {
1173   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1174   dbus_bool_t needed;
1175
1176   if (transport->connection == NULL)
1177     return;
1178
1179   if (transport->disconnected)
1180     {
1181       _dbus_assert (socket_transport->write_watch == NULL);
1182       return;
1183     }
1184
1185   _dbus_transport_ref (transport);
1186
1187   if (_dbus_transport_get_is_authenticated (transport))
1188     needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
1189   else
1190     {
1191       if (transport->send_credentials_pending)
1192         needed = TRUE;
1193       else
1194         {
1195           DBusAuthState auth_state;
1196
1197           auth_state = _dbus_auth_do_work (transport->auth);
1198
1199           /* If we need memory we install the write watch just in case,
1200            * if there's no need for it, it will get de-installed
1201            * next time we try reading.
1202            */
1203           if (auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND ||
1204               auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
1205             needed = TRUE;
1206           else
1207             needed = FALSE;
1208         }
1209     }
1210
1211   _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n",
1212                  needed, transport->connection, socket_transport->write_watch,
1213                  socket_transport->fd,
1214                  _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1215
1216   _dbus_connection_toggle_watch_unlocked (transport->connection,
1217                                           socket_transport->write_watch,
1218                                           needed);
1219
1220   _dbus_transport_unref (transport);
1221 }
1222
1223 static void
1224 check_read_watch (DBusTransport *transport)
1225 {
1226   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1227   dbus_bool_t need_read_watch;
1228
1229   _dbus_verbose ("fd = %d\n",socket_transport->fd);
1230
1231   if (transport->connection == NULL)
1232     return;
1233
1234   if (transport->disconnected)
1235     {
1236       _dbus_assert (socket_transport->read_watch == NULL);
1237       return;
1238     }
1239
1240   _dbus_transport_ref (transport);
1241
1242   if (_dbus_transport_get_is_authenticated (transport))
1243     need_read_watch =
1244       (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
1245       (_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds);
1246   else
1247     {
1248       if (transport->receive_credentials_pending)
1249         need_read_watch = TRUE;
1250       else
1251         {
1252           /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
1253            * is to avoid spinning on the file descriptor when we're waiting
1254            * to write or for some other part of the auth process
1255            */
1256           DBusAuthState auth_state;
1257
1258           auth_state = _dbus_auth_do_work (transport->auth);
1259
1260           /* If we need memory we install the read watch just in case,
1261            * if there's no need for it, it will get de-installed
1262            * next time we try reading. If we're authenticated we
1263            * install it since we normally have it installed while
1264            * authenticated.
1265            */
1266           if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
1267               auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
1268               auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
1269             need_read_watch = TRUE;
1270           else
1271             need_read_watch = FALSE;
1272         }
1273     }
1274
1275   _dbus_verbose ("  setting read watch enabled = %d\n", need_read_watch);
1276   _dbus_connection_toggle_watch_unlocked (transport->connection,
1277                                           socket_transport->read_watch,
1278                                           need_read_watch);
1279
1280   _dbus_transport_unref (transport);
1281 }
1282
1283 static void
1284 do_io_error (DBusTransport *transport)
1285 {
1286   _dbus_transport_ref (transport);
1287   _dbus_transport_disconnect (transport);
1288   _dbus_transport_unref (transport);
1289 }
1290
1291 #ifdef DBUS_AUTHENTICATION
1292 /* return value is whether we successfully read any new data. */
1293 static dbus_bool_t
1294 read_data_into_auth (DBusTransport *transport,
1295                      dbus_bool_t   *oom)
1296 {
1297   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1298   DBusString *buffer;
1299   int bytes_read;
1300
1301   *oom = FALSE;
1302
1303   _dbus_auth_get_buffer (transport->auth, &buffer);
1304
1305   bytes_read = kdbus_read_message(socket_transport, buffer);
1306
1307   _dbus_auth_return_buffer (transport->auth, buffer,
1308                             bytes_read > 0 ? bytes_read : 0);
1309
1310   if (bytes_read > 0)
1311     {
1312       _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
1313       return TRUE;
1314     }
1315   else if (bytes_read < 0)
1316     {
1317       /* EINTR already handled for us */
1318
1319       if (_dbus_get_is_errno_enomem ())
1320         {
1321           *oom = TRUE;
1322         }
1323       else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1324         ; /* do nothing, just return FALSE below */
1325       else
1326         {
1327           _dbus_verbose ("Error reading from remote app: %s\n",
1328                          _dbus_strerror_from_errno ());
1329           do_io_error (transport);
1330         }
1331
1332       return FALSE;
1333     }
1334   else
1335     {
1336       _dbus_assert (bytes_read == 0);
1337
1338       _dbus_verbose ("Disconnected from remote app\n");
1339       do_io_error (transport);
1340
1341       return FALSE;
1342     }
1343 }
1344
1345 /* Return value is whether we successfully wrote any bytes */
1346 static dbus_bool_t
1347 write_data_from_auth (DBusTransport *transport)
1348 {
1349   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1350   int bytes_written;
1351   const DBusString *buffer;
1352
1353   if (!_dbus_auth_get_bytes_to_send (transport->auth,
1354                                      &buffer))
1355     return FALSE;
1356
1357   bytes_written = _dbus_write_socket (socket_transport->fd,
1358                                       buffer,
1359                                       0, _dbus_string_get_length (buffer));
1360
1361   if (bytes_written > 0)
1362     {
1363       _dbus_auth_bytes_sent (transport->auth, bytes_written);
1364       return TRUE;
1365     }
1366   else if (bytes_written < 0)
1367     {
1368       /* EINTR already handled for us */
1369
1370       if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1371         ;
1372       else
1373         {
1374           _dbus_verbose ("Error writing to remote app: %s\n",
1375                          _dbus_strerror_from_errno ());
1376           do_io_error (transport);
1377         }
1378     }
1379
1380   return FALSE;
1381 }
1382
1383 /* FALSE on OOM */
1384 static dbus_bool_t
1385 exchange_credentials (DBusTransport *transport,
1386                       dbus_bool_t    do_reading,
1387                       dbus_bool_t    do_writing)
1388 {
1389   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1390   DBusError error = DBUS_ERROR_INIT;
1391
1392   _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
1393                   do_reading, do_writing);
1394
1395   if (do_writing && transport->send_credentials_pending)
1396     {
1397       if (_dbus_send_credentials_socket (socket_transport->fd,
1398                                          &error))
1399         {
1400           transport->send_credentials_pending = FALSE;
1401         }
1402       else
1403         {
1404           _dbus_verbose ("Failed to write credentials: %s\n", error.message);
1405           dbus_error_free (&error);
1406           do_io_error (transport);
1407         }
1408     }
1409
1410   if (do_reading && transport->receive_credentials_pending)
1411     {
1412       /* FIXME this can fail due to IO error _or_ OOM, broken
1413        * (somewhat tricky to fix since the OOM error can be set after
1414        * we already read the credentials byte, so basically we need to
1415        * separate reading the byte and storing it in the
1416        * transport->credentials). Does not really matter for now
1417        * because storing in credentials never actually fails on unix.
1418        */
1419       if (_dbus_read_credentials_socket (socket_transport->fd,
1420                                          transport->credentials,
1421                                          &error))
1422         {
1423           transport->receive_credentials_pending = FALSE;
1424         }
1425       else
1426         {
1427           _dbus_verbose ("Failed to read credentials %s\n", error.message);
1428           dbus_error_free (&error);
1429           do_io_error (transport);
1430         }
1431     }
1432
1433   if (!(transport->send_credentials_pending ||
1434         transport->receive_credentials_pending))
1435     {
1436       if (!_dbus_auth_set_credentials (transport->auth,
1437                                        transport->credentials))
1438         return FALSE;
1439     }
1440
1441   return TRUE;
1442 }
1443
1444 static dbus_bool_t
1445 do_authentication (DBusTransport *transport,
1446                    dbus_bool_t    do_reading,
1447                    dbus_bool_t    do_writing,
1448                    dbus_bool_t   *auth_completed)
1449 {
1450   dbus_bool_t oom;
1451   dbus_bool_t orig_auth_state;
1452
1453   oom = FALSE;
1454
1455   orig_auth_state = _dbus_transport_get_is_authenticated (transport);
1456
1457   /* This is essential to avoid the check_write_watch() at the end,
1458    * we don't want to add a write watch in do_iteration before
1459    * we try writing and get EAGAIN
1460    */
1461   if (orig_auth_state)
1462     {
1463       if (auth_completed)
1464         *auth_completed = FALSE;
1465       return TRUE;
1466     }
1467
1468   _dbus_transport_ref (transport);
1469
1470   while (!_dbus_transport_get_is_authenticated (transport) &&
1471          _dbus_transport_get_is_connected (transport))
1472     {
1473       if (!exchange_credentials (transport, do_reading, do_writing))
1474         {
1475           oom = TRUE;
1476           goto out;
1477         }
1478
1479       if (transport->send_credentials_pending ||
1480           transport->receive_credentials_pending)
1481         {
1482           _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
1483                          transport->send_credentials_pending,
1484                          transport->receive_credentials_pending);
1485           goto out;
1486         }
1487
1488 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
1489       switch (_dbus_auth_do_work (transport->auth))
1490         {
1491         case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
1492           _dbus_verbose (" %s auth state: waiting for input\n",
1493                          TRANSPORT_SIDE (transport));
1494           if (!do_reading || !read_data_into_auth (transport, &oom))
1495             goto out;
1496           break;
1497
1498         case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
1499           _dbus_verbose (" %s auth state: waiting for memory\n",
1500                          TRANSPORT_SIDE (transport));
1501           oom = TRUE;
1502           goto out;
1503           break;
1504
1505         case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
1506           _dbus_verbose (" %s auth state: bytes to send\n",
1507                          TRANSPORT_SIDE (transport));
1508           if (!do_writing || !write_data_from_auth (transport))
1509             goto out;
1510           break;
1511
1512         case DBUS_AUTH_STATE_NEED_DISCONNECT:
1513           _dbus_verbose (" %s auth state: need to disconnect\n",
1514                          TRANSPORT_SIDE (transport));
1515           do_io_error (transport);
1516           break;
1517
1518         case DBUS_AUTH_STATE_AUTHENTICATED:
1519           _dbus_verbose (" %s auth state: authenticated\n",
1520                          TRANSPORT_SIDE (transport));
1521           break;
1522         }
1523     }
1524
1525  out:
1526   if (auth_completed)
1527     *auth_completed = (orig_auth_state != _dbus_transport_get_is_authenticated (transport));
1528
1529   check_read_watch (transport);
1530   check_write_watch (transport);
1531   _dbus_transport_unref (transport);
1532
1533   if (oom)
1534     return FALSE;
1535   else
1536     return TRUE;
1537 }
1538 #endif
1539
1540 /* returns false on oom */
1541 static dbus_bool_t
1542 do_writing (DBusTransport *transport)
1543 {
1544 //      int total;
1545         DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1546         dbus_bool_t oom;
1547
1548         /* No messages without authentication! */
1549         if (!_dbus_transport_get_is_authenticated (transport))
1550     {
1551                 _dbus_verbose ("Not authenticated, not writing anything\n");
1552                 return TRUE;
1553     }
1554
1555         if (transport->disconnected)
1556     {
1557                 _dbus_verbose ("Not connected, not writing anything\n");
1558                 return TRUE;
1559     }
1560
1561 #if 1
1562         _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
1563                  _dbus_connection_has_messages_to_send_unlocked (transport->connection),
1564                  socket_transport->fd);
1565 #endif
1566
1567         oom = FALSE;
1568 //      total = 0;
1569
1570         while (!transport->disconnected && _dbus_connection_has_messages_to_send_unlocked (transport->connection))
1571     {
1572                 int bytes_written;
1573                 DBusMessage *message;
1574                 const DBusString *header;
1575                 const DBusString *body;
1576                 int total_bytes_to_write;
1577                 const char* pDestination;
1578
1579         /*      if (total > socket_transport->max_bytes_written_per_iteration)
1580         {
1581                         _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
1582                          total, socket_transport->max_bytes_written_per_iteration);
1583                         goto out;
1584         }*/
1585
1586                 message = _dbus_connection_get_message_to_send (transport->connection);
1587                 _dbus_assert (message != NULL);
1588                 dbus_message_lock (message);
1589                 _dbus_message_get_network_data (message, &header, &body);
1590                 total_bytes_to_write = _dbus_string_get_length(header) + _dbus_string_get_length(body);
1591                 pDestination = dbus_message_get_destination(message);
1592
1593                 if(pDestination)
1594                 {
1595                         if(!strcmp(pDestination, "org.freedesktop.DBus"))
1596                         {
1597                                 if(emulateOrgFreedesktopDBus(transport, message, socket_transport->fd))
1598                                         bytes_written = total_bytes_to_write;
1599                                 else
1600                                         bytes_written = -1;
1601                                 goto written;
1602                         }
1603                 }
1604                 if (_dbus_auth_needs_encoding (transport->auth))
1605         {
1606                         if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
1607             {
1608                                 if (!_dbus_auth_encode_data (transport->auth,
1609                                            header, &socket_transport->encoded_outgoing))
1610                 {
1611                                         oom = TRUE;
1612                                         goto out;
1613                 }
1614
1615                                 if (!_dbus_auth_encode_data (transport->auth,
1616                                            body, &socket_transport->encoded_outgoing))
1617                 {
1618                                         _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
1619                                         oom = TRUE;
1620                                         goto out;
1621                 }
1622             }
1623
1624                         total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
1625                         if(total_bytes_to_write > socket_transport->max_bytes_written_per_iteration)
1626                                 return -E2BIG;
1627
1628                         bytes_written = kdbus_write_msg(socket_transport, message, TRUE);
1629         }
1630                 else
1631                 {
1632                         if(total_bytes_to_write > socket_transport->max_bytes_written_per_iteration)
1633                                 return -E2BIG;
1634
1635                         bytes_written = kdbus_write_msg(socket_transport, message, FALSE);
1636                 }
1637
1638 written:
1639                 if (bytes_written < 0)
1640                 {
1641                         /* EINTR already handled for us */
1642
1643           /* For some discussion of why we also ignore EPIPE here, see
1644            * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
1645            */
1646
1647                         if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ())
1648                                 goto out;
1649                         else
1650                         {
1651                                 _dbus_verbose ("Error writing to remote app: %s\n", _dbus_strerror_from_errno ());
1652                                 do_io_error (transport);
1653                                 goto out;
1654                         }
1655                 }
1656                 else
1657                 {
1658                         _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
1659                          total_bytes_to_write);
1660
1661 //                      total += bytes_written;
1662                         socket_transport->message_bytes_written += bytes_written;
1663
1664                         _dbus_assert (socket_transport->message_bytes_written <=
1665                         total_bytes_to_write);
1666
1667                           if (socket_transport->message_bytes_written == total_bytes_to_write)
1668                           {
1669                                   socket_transport->message_bytes_written = 0;
1670                                   _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
1671                                   _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
1672
1673                                   _dbus_connection_message_sent_unlocked (transport->connection,
1674                                                                                                                   message);
1675                           }
1676                 }
1677     }
1678
1679         out:
1680         if (oom)
1681                 return FALSE;
1682         else
1683                 return TRUE;
1684 }
1685
1686 /* returns false on out-of-memory */
1687 static dbus_bool_t
1688 do_reading (DBusTransport *transport)
1689 {
1690   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1691   DBusString *buffer;
1692   int bytes_read;
1693   dbus_bool_t oom;
1694   int *fds, n_fds;
1695
1696   _dbus_verbose ("fd = %d\n",socket_transport->fd);
1697
1698   /* No messages without authentication! */
1699   if (!_dbus_transport_get_is_authenticated (transport))
1700     return TRUE;
1701
1702   oom = FALSE;
1703
1704  again:
1705
1706   /* See if we've exceeded max messages and need to disable reading */
1707   check_read_watch (transport);
1708
1709   _dbus_assert (socket_transport->read_watch != NULL ||
1710                 transport->disconnected);
1711
1712   if (transport->disconnected)
1713     goto out;
1714
1715   if (!dbus_watch_get_enabled (socket_transport->read_watch))
1716     return TRUE;
1717
1718   if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
1719   {
1720       _dbus_verbose ("Out of memory reading file descriptors\n");
1721       oom = TRUE;
1722       goto out;
1723   }
1724   _dbus_message_loader_get_buffer (transport->loader, &buffer);
1725
1726   if (_dbus_auth_needs_decoding (transport->auth))
1727   {
1728           bytes_read = kdbus_read_message(socket_transport,  &socket_transport->encoded_incoming, fds, &n_fds);
1729
1730       _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) == bytes_read);
1731
1732       if (bytes_read > 0)
1733       {
1734           if (!_dbus_auth_decode_data (transport->auth,
1735                                        &socket_transport->encoded_incoming,
1736                                        buffer))
1737           {
1738               _dbus_verbose ("Out of memory decoding incoming data\n");
1739               _dbus_message_loader_return_buffer (transport->loader,
1740                                               buffer,
1741                                               _dbus_string_get_length (buffer));
1742               oom = TRUE;
1743               goto out;
1744           }
1745
1746           _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
1747           _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
1748       }
1749   }
1750   else
1751           bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds);
1752
1753   if (bytes_read >= 0 && n_fds > 0)
1754     _dbus_verbose("Read %i unix fds\n", n_fds);
1755
1756   _dbus_message_loader_return_buffer (transport->loader,
1757                                       buffer,
1758                                       bytes_read < 0 ? 0 : _dbus_string_get_length (buffer));
1759   _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
1760
1761  /* if (_dbus_auth_needs_decoding (transport->auth))
1762   {
1763           bytes_read = kdbus_read_message(socket_transport,  &socket_transport->encoded_incoming);
1764
1765       _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) ==
1766                     bytes_read);
1767
1768       if (bytes_read > 0)
1769       {
1770           int orig_len;
1771
1772           _dbus_message_loader_get_buffer (transport->loader, &buffer);
1773           orig_len = _dbus_string_get_length (buffer);
1774           if (!_dbus_auth_decode_data (transport->auth,
1775                                        &socket_transport->encoded_incoming,
1776                                        buffer))
1777           {
1778               _dbus_verbose ("Out of memory decoding incoming data\n");
1779               _dbus_message_loader_return_buffer (transport->loader,
1780                                               buffer,
1781                                               _dbus_string_get_length (buffer) - orig_len);
1782               oom = TRUE;
1783               goto out;
1784           }
1785
1786           _dbus_message_loader_return_buffer (transport->loader,
1787                                               buffer,
1788                                               _dbus_string_get_length (buffer) - orig_len);
1789           _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
1790           _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
1791       }
1792   }
1793   else
1794   {
1795       int *fds, n_fds;
1796
1797       if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
1798       {
1799           _dbus_verbose ("Out of memory reading file descriptors\n");
1800           _dbus_message_loader_return_buffer (transport->loader, buffer, 0);
1801           oom = TRUE;
1802           goto out;
1803       }
1804           _dbus_message_loader_get_buffer (transport->loader, &buffer);
1805
1806       bytes_read = kdbus_read_message(socket_transport, buffer, fds, &n_fds);
1807
1808       if (bytes_read >= 0 && n_fds > 0)
1809         _dbus_verbose("Read %i unix fds\n", n_fds);
1810
1811       _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
1812       _dbus_message_loader_return_buffer (transport->loader,
1813                                           buffer,
1814                                           bytes_read < 0 ? 0 : bytes_read);
1815   }*/
1816
1817   if (bytes_read < 0)
1818     {
1819       /* EINTR already handled for us */
1820
1821       if (_dbus_get_is_errno_enomem ())
1822         {
1823           _dbus_verbose ("Out of memory in read()/do_reading()\n");
1824           oom = TRUE;
1825           goto out;
1826         }
1827       else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
1828         goto out;
1829       else
1830         {
1831           _dbus_verbose ("Error reading from remote app: %s\n",
1832                          _dbus_strerror_from_errno ());
1833           do_io_error (transport);
1834           goto out;
1835         }
1836     }
1837   else if (bytes_read == 0)
1838     {
1839       _dbus_verbose ("Disconnected from remote app\n");
1840       do_io_error (transport);
1841       goto out;
1842     }
1843   else
1844     {
1845       _dbus_verbose (" read %d bytes\n", bytes_read);
1846
1847 //      total += bytes_read;
1848
1849       if (!_dbus_transport_queue_messages (transport))
1850         {
1851           oom = TRUE;
1852           _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
1853           goto out;
1854         }
1855
1856       /* Try reading more data until we get EAGAIN and return, or
1857        * exceed max bytes per iteration.  If in blocking mode of
1858        * course we'll block instead of returning.
1859        */
1860       goto again;
1861     }
1862
1863  out:
1864   if (oom)
1865     return FALSE;
1866   else
1867     return TRUE;
1868 }
1869
1870 static dbus_bool_t
1871 unix_error_with_read_to_come (DBusTransport *itransport,
1872                               DBusWatch     *watch,
1873                               unsigned int   flags)
1874 {
1875   DBusTransportSocket *transport = (DBusTransportSocket *) itransport;
1876
1877   if (!(flags & DBUS_WATCH_HANGUP || flags & DBUS_WATCH_ERROR))
1878     return FALSE;
1879
1880   /* If we have a read watch enabled ...
1881      we -might have data incoming ... => handle the HANGUP there */
1882   if (watch != transport->read_watch &&
1883       _dbus_watch_get_enabled (transport->read_watch))
1884     return FALSE;
1885
1886   return TRUE;
1887 }
1888
1889 static dbus_bool_t
1890 socket_handle_watch (DBusTransport *transport,
1891                    DBusWatch     *watch,
1892                    unsigned int   flags)
1893 {
1894   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1895
1896   _dbus_assert (watch == socket_transport->read_watch ||
1897                 watch == socket_transport->write_watch);
1898   _dbus_assert (watch != NULL);
1899
1900   /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
1901    * still be in the buffer and do_reading may need several iteration to read
1902    * it all (because of its max_bytes_read_per_iteration limit).
1903    */
1904   if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
1905     {
1906       _dbus_verbose ("Hang up or error on watch\n");
1907       _dbus_transport_disconnect (transport);
1908       return TRUE;
1909     }
1910
1911   if (watch == socket_transport->read_watch &&
1912       (flags & DBUS_WATCH_READABLE))
1913     {
1914 #ifdef DBUS_AUTHENTICATION
1915       dbus_bool_t auth_finished;
1916 #endif
1917 #if 1
1918       _dbus_verbose ("handling read watch %p flags = %x\n",
1919                      watch, flags);
1920 #endif
1921 #ifdef DBUS_AUTHENTICATION
1922       if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
1923         return FALSE;
1924
1925       /* We don't want to do a read immediately following
1926        * a successful authentication.  This is so we
1927        * have a chance to propagate the authentication
1928        * state further up.  Specifically, we need to
1929        * process any pending data from the auth object.
1930        */
1931       if (!auth_finished)
1932         {
1933 #endif
1934           if (!do_reading (transport))
1935             {
1936               _dbus_verbose ("no memory to read\n");
1937               return FALSE;
1938             }
1939 #ifdef DBUS_AUTHENTICATION
1940         }
1941       else
1942         {
1943           _dbus_verbose ("Not reading anything since we just completed the authentication\n");
1944         }
1945 #endif
1946     }
1947   else if (watch == socket_transport->write_watch &&
1948            (flags & DBUS_WATCH_WRITABLE))
1949     {
1950 #if 1
1951       _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
1952                      _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1953 #endif
1954 #ifdef DBUS_AUTHENTICATION
1955       if (!do_authentication (transport, FALSE, TRUE, NULL))
1956         return FALSE;
1957 #endif
1958       if (!do_writing (transport))
1959         {
1960           _dbus_verbose ("no memory to write\n");
1961           return FALSE;
1962         }
1963
1964       /* See if we still need the write watch */
1965       check_write_watch (transport);
1966     }
1967 #ifdef DBUS_ENABLE_VERBOSE_MODE
1968   else
1969     {
1970       if (watch == socket_transport->read_watch)
1971         _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
1972                        flags);
1973       else if (watch == socket_transport->write_watch)
1974         _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
1975                        flags);
1976       else
1977         _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
1978                        watch, dbus_watch_get_socket (watch));
1979     }
1980 #endif /* DBUS_ENABLE_VERBOSE_MODE */
1981
1982   return TRUE;
1983 }
1984
1985 static void
1986 socket_disconnect (DBusTransport *transport)
1987 {
1988   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1989
1990   _dbus_verbose ("\n");
1991
1992   free_watches (transport);
1993
1994   _dbus_close_socket (socket_transport->fd, NULL);
1995   socket_transport->fd = -1;
1996 }
1997
1998 static dbus_bool_t
1999 socket_connection_set (DBusTransport *transport)
2000 {
2001   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2002
2003   _dbus_watch_set_handler (socket_transport->write_watch,
2004                            _dbus_connection_handle_watch,
2005                            transport->connection, NULL);
2006
2007   _dbus_watch_set_handler (socket_transport->read_watch,
2008                            _dbus_connection_handle_watch,
2009                            transport->connection, NULL);
2010
2011   if (!_dbus_connection_add_watch_unlocked (transport->connection,
2012                                             socket_transport->write_watch))
2013     return FALSE;
2014
2015   if (!_dbus_connection_add_watch_unlocked (transport->connection,
2016                                             socket_transport->read_watch))
2017     {
2018       _dbus_connection_remove_watch_unlocked (transport->connection,
2019                                               socket_transport->write_watch);
2020       return FALSE;
2021     }
2022
2023   check_read_watch (transport);
2024   check_write_watch (transport);
2025
2026   return TRUE;
2027 }
2028
2029 /**
2030  * @todo We need to have a way to wake up the select sleep if
2031  * a new iteration request comes in with a flag (read/write) that
2032  * we're not currently serving. Otherwise a call that just reads
2033  * could block a write call forever (if there are no incoming
2034  * messages).
2035  */
2036 static  void
2037 kdbus_do_iteration (DBusTransport *transport,
2038                    unsigned int   flags,
2039                    int            timeout_milliseconds)
2040 {
2041         DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2042         DBusPollFD poll_fd;
2043         int poll_res;
2044         int poll_timeout;
2045
2046         _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
2047                  flags & DBUS_ITERATION_DO_READING ? "read" : "",
2048                  flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
2049                  timeout_milliseconds,
2050                  socket_transport->read_watch,
2051                  socket_transport->write_watch,
2052                  socket_transport->fd);
2053
2054   /* the passed in DO_READING/DO_WRITING flags indicate whether to
2055    * read/write messages, but regardless of those we may need to block
2056    * for reading/writing to do auth.  But if we do reading for auth,
2057    * we don't want to read any messages yet if not given DO_READING.
2058    */
2059
2060   poll_fd.fd = socket_transport->fd;
2061   poll_fd.events = 0;
2062
2063   if (_dbus_transport_get_is_authenticated (transport))
2064     {
2065       /* This is kind of a hack; if we have stuff to write, then try
2066        * to avoid the poll. This is probably about a 5% speedup on an
2067        * echo client/server.
2068        *
2069        * If both reading and writing were requested, we want to avoid this
2070        * since it could have funky effects:
2071        *   - both ends spinning waiting for the other one to read
2072        *     data so they can finish writing
2073        *   - prioritizing all writing ahead of reading
2074        */
2075       if ((flags & DBUS_ITERATION_DO_WRITING) &&
2076           !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
2077           !transport->disconnected &&
2078           _dbus_connection_has_messages_to_send_unlocked (transport->connection))
2079         {
2080           do_writing (transport);
2081
2082           if (transport->disconnected ||
2083               !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
2084             goto out;
2085         }
2086
2087       /* If we get here, we decided to do the poll() after all */
2088       _dbus_assert (socket_transport->read_watch);
2089       if (flags & DBUS_ITERATION_DO_READING)
2090         poll_fd.events |= _DBUS_POLLIN;
2091
2092       _dbus_assert (socket_transport->write_watch);
2093       if (flags & DBUS_ITERATION_DO_WRITING)
2094         poll_fd.events |= _DBUS_POLLOUT;
2095     }
2096   else
2097     {
2098       DBusAuthState auth_state;
2099
2100       auth_state = _dbus_auth_do_work (transport->auth);
2101
2102       if (transport->receive_credentials_pending ||
2103           auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
2104         poll_fd.events |= _DBUS_POLLIN;
2105
2106       if (transport->send_credentials_pending ||
2107           auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
2108         poll_fd.events |= _DBUS_POLLOUT;
2109     }
2110
2111   if (poll_fd.events)
2112     {
2113       if (flags & DBUS_ITERATION_BLOCK)
2114         poll_timeout = timeout_milliseconds;
2115       else
2116         poll_timeout = 0;
2117
2118       /* For blocking selects we drop the connection lock here
2119        * to avoid blocking out connection access during a potentially
2120        * indefinite blocking call. The io path is still protected
2121        * by the io_path_cond condvar, so we won't reenter this.
2122        */
2123       if (flags & DBUS_ITERATION_BLOCK)
2124         {
2125           _dbus_verbose ("unlock pre poll\n");
2126           _dbus_connection_unlock (transport->connection);
2127         }
2128
2129     again:
2130       poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
2131
2132       if (poll_res < 0 && _dbus_get_is_errno_eintr ())
2133       {
2134           _dbus_verbose ("Error from _dbus_poll(): %s\n",
2135                          _dbus_strerror_from_errno ());
2136           goto again;
2137       }
2138
2139       if (flags & DBUS_ITERATION_BLOCK)
2140         {
2141           _dbus_verbose ("lock post poll\n");
2142           _dbus_connection_lock (transport->connection);
2143         }
2144
2145       if (poll_res >= 0)
2146         {
2147           if (poll_res == 0)
2148             poll_fd.revents = 0; /* some concern that posix does not guarantee this;
2149                                   * valgrind flags it as an error. though it probably
2150                                   * is guaranteed on linux at least.
2151                                   */
2152
2153           if (poll_fd.revents & _DBUS_POLLERR)
2154             do_io_error (transport);
2155           else
2156             {
2157               dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
2158               dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
2159 #ifdef DBUS_AUTHENTICATION
2160               dbus_bool_t authentication_completed;
2161 #endif
2162
2163               _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
2164                              need_read, need_write);
2165 #ifdef DBUS_AUTHENTICATION
2166               do_authentication (transport, need_read, need_write,
2167                                  &authentication_completed);
2168
2169               /* See comment in socket_handle_watch. */
2170               if (authentication_completed)
2171                 goto out;
2172 #endif
2173               if (need_read && (flags & DBUS_ITERATION_DO_READING))
2174                 do_reading (transport);
2175               if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
2176                 do_writing (transport);
2177             }
2178         }
2179       else
2180         {
2181           _dbus_verbose ("Error from _dbus_poll(): %s\n",
2182                          _dbus_strerror_from_errno ());
2183         }
2184     }
2185
2186
2187  out:
2188   /* We need to install the write watch only if we did not
2189    * successfully write everything. Note we need to be careful that we
2190    * don't call check_write_watch *before* do_writing, since it's
2191    * inefficient to add the write watch, and we can avoid it most of
2192    * the time since we can write immediately.
2193    *
2194    * However, we MUST always call check_write_watch(); DBusConnection code
2195    * relies on the fact that running an iteration will notice that
2196    * messages are pending.
2197    */
2198   check_write_watch (transport);
2199
2200   _dbus_verbose (" ... leaving do_iteration()\n");
2201 }
2202
2203 static void
2204 socket_live_messages_changed (DBusTransport *transport)
2205 {
2206   /* See if we should look for incoming messages again */
2207   check_read_watch (transport);
2208 }
2209
2210 static const DBusTransportVTable kdbus_vtable = {
2211   socket_finalize,
2212   socket_handle_watch,
2213   socket_disconnect,
2214   socket_connection_set,
2215   kdbus_do_iteration,
2216   socket_live_messages_changed,
2217   socket_get_socket_fd
2218 };
2219
2220 /**
2221  * Creates a new transport for the given kdbus file descriptor.  The file
2222  * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
2223  * make it so).
2224  *
2225  * @param fd the file descriptor.
2226  * @param server_guid non-#NULL if this transport is on the server side of a connection
2227  * @param address the transport's address
2228  * @returns the new transport, or #NULL if no memory.
2229  */
2230 static DBusTransport*
2231 _dbus_transport_new_for_socket_kdbus (int       fd,
2232                                           const DBusString *server_guid,
2233                                           const DBusString *address)
2234 {
2235         DBusTransportSocket *socket_transport;
2236
2237   socket_transport = dbus_new0 (DBusTransportSocket, 1);
2238   if (socket_transport == NULL)
2239     return NULL;
2240
2241   if (!_dbus_string_init (&socket_transport->encoded_outgoing))
2242     goto failed_0;
2243
2244   if (!_dbus_string_init (&socket_transport->encoded_incoming))
2245     goto failed_1;
2246
2247   socket_transport->write_watch = _dbus_watch_new (fd,
2248                                                  DBUS_WATCH_WRITABLE,
2249                                                  FALSE,
2250                                                  NULL, NULL, NULL);
2251   if (socket_transport->write_watch == NULL)
2252     goto failed_2;
2253
2254   socket_transport->read_watch = _dbus_watch_new (fd,
2255                                                 DBUS_WATCH_READABLE,
2256                                                 FALSE,
2257                                                 NULL, NULL, NULL);
2258   if (socket_transport->read_watch == NULL)
2259     goto failed_3;
2260
2261   if (!_dbus_transport_init_base (&socket_transport->base,
2262                                   &kdbus_vtable,
2263                                   server_guid, address))
2264     goto failed_4;
2265
2266 #ifdef HAVE_UNIX_FD_PASSING
2267   _dbus_auth_set_unix_fd_possible(socket_transport->base.auth, _dbus_socket_can_pass_unix_fd(fd));
2268 #endif
2269
2270   socket_transport->fd = fd;
2271   socket_transport->message_bytes_written = 0;
2272
2273   /* These values should probably be tunable or something. */
2274   socket_transport->max_bytes_read_per_iteration = RECEIVE_POOL_SIZE;
2275   socket_transport->max_bytes_written_per_iteration = DBUS_MAXIMUM_MESSAGE_LENGTH;
2276
2277   socket_transport->kdbus_mmap_ptr = NULL;
2278   socket_transport->memfd = -1;
2279   
2280   return (DBusTransport*) socket_transport;
2281
2282  failed_4:
2283   _dbus_watch_invalidate (socket_transport->read_watch);
2284   _dbus_watch_unref (socket_transport->read_watch);
2285  failed_3:
2286   _dbus_watch_invalidate (socket_transport->write_watch);
2287   _dbus_watch_unref (socket_transport->write_watch);
2288  failed_2:
2289   _dbus_string_free (&socket_transport->encoded_incoming);
2290  failed_1:
2291   _dbus_string_free (&socket_transport->encoded_outgoing);
2292  failed_0:
2293   dbus_free (socket_transport);
2294   return NULL;
2295 }
2296
2297
2298 /**
2299  * Creates a connection to the kdbus bus
2300   *
2301  * This will set FD_CLOEXEC for the socket returned.
2302  *
2303  * @param path the path to UNIX domain socket
2304  * @param error return location for error code
2305  * @returns connection file descriptor or -1 on error
2306  */
2307 static int _dbus_connect_kdbus (const char *path, DBusError *error)
2308 {
2309         int fd;
2310
2311         _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2312         _dbus_verbose ("connecting to kdbus bus %s\n", path);
2313
2314         fd = open(path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
2315         if (fd < 0)
2316                 dbus_set_error(error, _dbus_error_from_errno (errno), "Failed to open file descriptor: %s", _dbus_strerror (errno));
2317
2318         return fd;
2319 }
2320
2321 static dbus_bool_t kdbus_mmap(DBusTransport* transport)
2322 {
2323         DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
2324
2325         socket_transport->kdbus_mmap_ptr = mmap(NULL, RECEIVE_POOL_SIZE, PROT_READ, MAP_SHARED, socket_transport->fd, 0);
2326         if (socket_transport->kdbus_mmap_ptr == MAP_FAILED)
2327                 return FALSE;
2328
2329         return TRUE;
2330 }
2331
2332 /**
2333  * Creates a new transport for kdbus.
2334  * This creates a client-side of a transport.
2335  *
2336  * @param path the path to the domain socket.
2337  * @param error address where an error can be returned.
2338  * @returns a new transport, or #NULL on failure.
2339  */
2340 static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError *error)
2341 {
2342         int fd;
2343         DBusTransport *transport;
2344         DBusString address;
2345
2346         _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2347
2348         if (!_dbus_string_init (&address))
2349     {
2350                 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2351                 return NULL;
2352     }
2353
2354         fd = -1;
2355
2356         if ((!_dbus_string_append (&address, "kdbus:path=")) || (!_dbus_string_append (&address, path)))
2357     {
2358                 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2359                 goto failed_0;
2360     }
2361
2362         fd = _dbus_connect_kdbus (path, error);
2363         if (fd < 0)
2364     {
2365                 _DBUS_ASSERT_ERROR_IS_SET (error);
2366                 goto failed_0;
2367     }
2368
2369         _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
2370
2371         transport = _dbus_transport_new_for_socket_kdbus (fd, NULL, &address);
2372         if (transport == NULL)
2373     {
2374                 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
2375                 goto failed_1;
2376     }
2377
2378         _dbus_string_free (&address);
2379
2380         return transport;
2381
2382         failed_1:
2383                 _dbus_close_socket (fd, NULL);
2384         failed_0:
2385                 _dbus_string_free (&address);
2386         return NULL;
2387 }
2388
2389
2390 /**
2391  * Opens kdbus transport.
2392  *
2393  * @param entry the address entry to try opening
2394  * @param transport_p return location for the opened transport
2395  * @param error error to be set
2396  * @returns result of the attempt
2397  */
2398 DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry  *entry,
2399                                                            DBusTransport    **transport_p,
2400                                                            DBusError         *error)
2401 {
2402         const char *method;
2403
2404         method = dbus_address_entry_get_method (entry);
2405         _dbus_assert (method != NULL);
2406
2407         if (strcmp (method, "kdbus") == 0)
2408     {
2409                 const char *path = dbus_address_entry_get_value (entry, "path");
2410
2411                 if (path == NULL)
2412         {
2413                         _dbus_set_bad_address (error, "kdbus", "path", NULL);
2414                         return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
2415         }
2416
2417         *transport_p = _dbus_transport_new_for_kdbus (path, error);
2418
2419         if (*transport_p == NULL)
2420         {
2421                 _DBUS_ASSERT_ERROR_IS_SET (error);
2422                 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
2423         }
2424         else
2425         {
2426                 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2427                 return DBUS_TRANSPORT_OPEN_OK;
2428         }
2429     }
2430         else
2431     {
2432                 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2433                 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
2434     }
2435 }
2436
2437 static struct kdbus_policy *make_policy_name(const char *name)
2438 {
2439         struct kdbus_policy *p;
2440         __u64 size;
2441
2442         size = offsetof(struct kdbus_policy, name) + strlen(name) + 1;
2443         p = malloc(size);
2444         if (!p)
2445                 return NULL;
2446         memset(p, 0, size);
2447         p->size = size;
2448         p->type = KDBUS_POLICY_NAME;
2449         strcpy(p->name, name);
2450
2451         return p;
2452 }
2453
2454 static struct kdbus_policy *make_policy_access(__u64 type, __u64 bits, __u64 id)
2455 {
2456         struct kdbus_policy *p;
2457         __u64 size = sizeof(*p);
2458
2459         p = malloc(size);
2460         if (!p)
2461                 return NULL;
2462
2463         memset(p, 0, size);
2464         p->size = size;
2465         p->type = KDBUS_POLICY_ACCESS;
2466         p->access.type = type;
2467         p->access.bits = bits;
2468         p->access.id = id;
2469
2470         return p;
2471 }
2472
2473 static void append_policy(struct kdbus_cmd_policy *cmd_policy, struct kdbus_policy *policy, __u64 max_size)
2474 {
2475         struct kdbus_policy *dst = (struct kdbus_policy *) ((char *) cmd_policy + cmd_policy->size);
2476
2477         if (cmd_policy->size + policy->size > max_size)
2478                 return;
2479
2480         memcpy(dst, policy, policy->size);
2481         cmd_policy->size += KDBUS_ALIGN8(policy->size);
2482         free(policy);
2483 }
2484
2485 dbus_bool_t bus_register_policy_kdbus(const char* name, DBusConnection *connection, DBusError *error)
2486 {
2487         struct kdbus_cmd_policy *cmd_policy;
2488         struct kdbus_policy *policy;
2489         int size = 0xffff;
2490         int fd;
2491
2492         if(!dbus_connection_get_socket(connection, &fd))
2493         {
2494                 dbus_set_error (error, "Failed to get fd for registering policy", NULL);
2495                 return FALSE;
2496         }
2497
2498         cmd_policy = (struct kdbus_cmd_policy *) alloca(size);
2499         memset(cmd_policy, 0, size);
2500
2501         policy = (struct kdbus_policy *) cmd_policy->policies;
2502         cmd_policy->size = offsetof(struct kdbus_cmd_policy, policies);
2503
2504         policy = make_policy_name(name);
2505         append_policy(cmd_policy, policy, size);
2506
2507         policy = make_policy_access(KDBUS_POLICY_ACCESS_USER, KDBUS_POLICY_OWN, getuid());
2508         append_policy(cmd_policy, policy, size);
2509
2510         policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_RECV, 0);
2511         append_policy(cmd_policy, policy, size);
2512
2513         policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_SEND, 0);
2514         append_policy(cmd_policy, policy, size);
2515
2516         if (ioctl(fd, KDBUS_CMD_EP_POLICY_SET, cmd_policy) < 0)
2517         {
2518                 dbus_set_error(error,_dbus_error_from_errno (errno), "Error setting EP policy: %s", _dbus_strerror (errno));
2519                 return FALSE;
2520         }
2521
2522         _dbus_verbose("Policy %s set correctly\n", name);
2523         return TRUE;
2524 }
2525
2526 dbus_bool_t bus_register_kdbus(char* name, DBusConnection *connection, DBusError *error)
2527 {
2528         struct kdbus_cmd_hello __attribute__ ((__aligned__(8))) hello;
2529         int fd;
2530
2531         memset(&hello, 0, sizeof(hello));
2532         hello.conn_flags = KDBUS_HELLO_ACCEPT_FD/* |
2533                            KDBUS_HELLO_ATTACH_COMM |
2534                            KDBUS_HELLO_ATTACH_EXE |
2535                            KDBUS_HELLO_ATTACH_CMDLINE |
2536                            KDBUS_HELLO_ATTACH_CAPS |
2537                            KDBUS_HELLO_ATTACH_CGROUP |
2538                            KDBUS_HELLO_ATTACH_SECLABEL |
2539                            KDBUS_HELLO_ATTACH_AUDIT*/;
2540         hello.size = sizeof(struct kdbus_cmd_hello);
2541         hello.pool_size = RECEIVE_POOL_SIZE;
2542
2543         if(!dbus_connection_get_socket(connection, &fd))
2544         {
2545                 dbus_set_error (error, "failed to get fd for bus registration", NULL);
2546                 return FALSE;
2547         }
2548         if (ioctl(fd, KDBUS_CMD_HELLO, &hello))
2549         {
2550                 dbus_set_error(error,_dbus_error_from_errno (errno), "Failed to send hello: %s", _dbus_strerror (errno));
2551                 return FALSE;
2552         }
2553
2554         _dbus_verbose("-- Our peer ID is: %llu\n", (unsigned long long)hello.id);
2555         sprintf(name, "%llu", (unsigned long long)hello.id);
2556
2557         if(!kdbus_mmap(dbus_connection_get_transport(connection)))
2558         {
2559                 dbus_set_error(error,_dbus_error_from_errno (errno), "Error when mmap: %s", _dbus_strerror (errno));
2560                 return FALSE;
2561         }
2562
2563         return TRUE;
2564 }
2565
2566 int bus_request_name_kdbus(DBusConnection *connection, const char *name, const uint64_t flags, DBusError *error)
2567 {
2568         struct kdbus_cmd_name *cmd_name;
2569         int fd;
2570         uint64_t size = sizeof(*cmd_name) + strlen(name) + 1;
2571         uint64_t flags_kdbus = 0;
2572
2573         cmd_name = alloca(size);
2574
2575         memset(cmd_name, 0, size);
2576         strcpy(cmd_name->name, name);
2577         cmd_name->size = size;
2578
2579         if(flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)
2580                 flags_kdbus |= KDBUS_NAME_ALLOW_REPLACEMENT;
2581         if(!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE))
2582                 flags_kdbus |= KDBUS_NAME_QUEUE;
2583         if(flags & DBUS_NAME_FLAG_REPLACE_EXISTING)
2584                 flags_kdbus |= KDBUS_NAME_REPLACE_EXISTING;
2585
2586         cmd_name->conn_flags = flags_kdbus;
2587
2588         if(!dbus_connection_get_socket(connection, &fd))
2589         {
2590                 dbus_set_error (error, "failed to get fd for name request", NULL);
2591                 return -1;
2592         }
2593
2594         _dbus_verbose("Request name - flags sent: 0x%llx       !!!!!!!!!\n", cmd_name->conn_flags);
2595
2596         _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2597         if (ioctl(fd, KDBUS_CMD_NAME_ACQUIRE, cmd_name))
2598         {
2599                 dbus_set_error(error,_dbus_error_from_errno (errno), "error acquiring name: %s", _dbus_strerror (errno));
2600                 if(errno == EEXIST)
2601                         return DBUS_REQUEST_NAME_REPLY_EXISTS;
2602                 return -1;
2603         }
2604
2605         _dbus_verbose("Request name - received flag: 0x%llx       !!!!!!!!!\n", cmd_name->conn_flags);
2606
2607         if(cmd_name->conn_flags & KDBUS_NAME_IN_QUEUE)
2608                 return DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
2609         else
2610                 return DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
2611         //todo now 1 code is never returned -  DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
2612 }
2613
2614 /**
2615  * Checks if the connection's transport is kdbus on the basis of its address
2616  *
2617  * @param pointer to the connection
2618  * @returns TRUE if kdbus transport, otherwise FALSE
2619  */
2620 dbus_bool_t dbus_transport_is_kdbus(DBusConnection *connection)
2621 {
2622         const char* address = _dbus_connection_get_address(connection);
2623
2624         if(address == strstr(address, "kdbus:path="))
2625                 return TRUE;
2626         else
2627                 return FALSE;
2628 }
2629
2630 static int parse_match_key(const char *rule, const char* key, char** pValue)
2631 {
2632         const char* pBegin;
2633         const char* pValueEnd;
2634         int value_length = 0;
2635
2636         pBegin = strstr(rule, key);
2637         if(pBegin)
2638         {
2639                 pBegin += strlen(key);
2640                 pValueEnd = strchr(pBegin, '\'');
2641                 if(pValueEnd)
2642                 {
2643                         value_length = pValueEnd - pBegin;
2644                         *pValue = strndup(pBegin, value_length);
2645                         if(*pValue)
2646                         {
2647                                 if(strcmp(*pValue, "org.freedesktop.DBus") == 0)
2648                                         value_length = -1;
2649                                 _dbus_verbose ("found for key: %s value:'%s'\n", key, *pValue);
2650                         }
2651                 }
2652         }
2653         return value_length;
2654 }
2655
2656 /* only part of the dbus's matching capabilities implemented in kdbus now because of different mechanism
2657  * interface match key mapped to bloom
2658  * sender match key mapped to src_name
2659  * also handled org.freedesktop.dbus members: NameOwnerChanged, NameLost, NameAcquired
2660  */
2661 void dbus_bus_add_match_kdbus (DBusConnection *connection, const char *rule, DBusError *error)
2662 {
2663         struct kdbus_cmd_match* pCmd_match;
2664         struct kdbus_item *pItem;
2665         int fd;
2666         __u64 src_id = KDBUS_MATCH_SRC_ID_ANY;
2667         uint64_t size;
2668         unsigned int kernel_item = 0;
2669         int name_size;
2670         char* pName = NULL;
2671         char* pInterface = NULL;
2672
2673         dbus_connection_get_socket(connection, &fd);
2674
2675         /*parsing rule and calculating size of command*/
2676         size = sizeof(struct kdbus_cmd_match);
2677
2678         if(strstr(rule, "member='NameOwnerChanged'"))
2679         {
2680                 kernel_item = KDBUS_MATCH_NAME_CHANGE;
2681                 size += KDBUS_ITEM_SIZE(1);
2682         }
2683         else if(strstr(rule, "member='NameLost'"))
2684         {
2685                 kernel_item = KDBUS_MATCH_NAME_REMOVE;
2686                 size += KDBUS_ITEM_SIZE(1);
2687         }
2688         else if(strstr(rule, "member='NameAcquired'"))
2689         {
2690                 kernel_item = KDBUS_MATCH_NAME_ADD;
2691                 size += KDBUS_ITEM_SIZE(1);
2692         }
2693
2694         name_size = parse_match_key(rule, "interface='", &pInterface); /*actual size is not important for interface*/
2695         if((name_size == -1) && (kernel_item == 0))
2696         {
2697                 kernel_item = ~0;
2698                 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2;
2699         }
2700         else if(name_size > 0)
2701                 size += KDBUS_PART_HEADER_SIZE + KDBUS_BLOOM_SIZE_BYTES;
2702
2703         name_size = parse_match_key(rule, "sender='", &pName);
2704         if((name_size == -1) && (kernel_item == 0))
2705         {
2706                 kernel_item = ~0;
2707                 size += KDBUS_ITEM_SIZE(1)*3 + KDBUS_ITEM_SIZE(sizeof(__u64))*2;
2708         }
2709         if(name_size > 0)
2710         {
2711                 if(!strncmp(pName, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
2712                 {
2713                         src_id = strtoll(&pName[3], NULL, 10);
2714                         free(pName);
2715                         pName = NULL;
2716                 }
2717                 else
2718                         size += KDBUS_ITEM_SIZE(name_size + 1);
2719         }
2720
2721         pCmd_match = malloc(size);
2722         if(pCmd_match == NULL)
2723                 goto out;
2724         memset(pCmd_match, 0, size);
2725
2726         pCmd_match->size = size;
2727         pCmd_match->cookie = strtoll(dbus_bus_get_unique_name(connection), NULL , 10);
2728
2729         pItem = pCmd_match->items;
2730         if(kernel_item == ~0)  //all signals from kernel
2731         {
2732                 pCmd_match->src_id = 0;
2733                 pItem->type = KDBUS_MATCH_NAME_CHANGE;
2734                 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
2735                 pItem = KDBUS_PART_NEXT(pItem);
2736                 pItem->type = KDBUS_MATCH_NAME_ADD;
2737                 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
2738                 pItem = KDBUS_PART_NEXT(pItem);
2739                 pItem->type = KDBUS_MATCH_NAME_REMOVE;
2740                 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
2741                 pItem = KDBUS_PART_NEXT(pItem);
2742                 pItem->type = KDBUS_MATCH_ID_ADD;
2743                 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
2744                 pItem = KDBUS_PART_NEXT(pItem);
2745                 pItem->type = KDBUS_MATCH_ID_REMOVE;
2746                 pItem->size = KDBUS_PART_HEADER_SIZE + sizeof(__u64);
2747         }
2748         else if(kernel_item)
2749         {
2750                 pCmd_match->src_id = 0;
2751                 pItem->type = kernel_item;
2752                 pItem->size = KDBUS_PART_HEADER_SIZE + 1;
2753         }
2754         else
2755         {
2756                 pCmd_match->src_id = src_id;
2757                 if(pName)
2758                 {
2759                         pItem->type = KDBUS_MATCH_SRC_NAME;
2760                         pItem->size = KDBUS_PART_HEADER_SIZE + name_size + 1;
2761                         strcpy(pItem->str, pName);
2762                         pItem = KDBUS_PART_NEXT(pItem);
2763                 }
2764
2765                 if(pInterface)
2766                 {
2767                         pItem->type = KDBUS_MATCH_BLOOM;
2768                         pItem->size = KDBUS_PART_HEADER_SIZE + KDBUS_BLOOM_SIZE_BYTES;
2769                         strncpy(pItem->data, pInterface, KDBUS_BLOOM_SIZE_BYTES);
2770                 }
2771         }
2772
2773         if(ioctl(fd, KDBUS_CMD_MATCH_ADD, pCmd_match))
2774         {
2775                 if(error)
2776                         dbus_set_error(error,_dbus_error_from_errno (errno), "error adding match: %s", _dbus_strerror (errno));
2777                 _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
2778         }
2779         else
2780                 _dbus_verbose("Added match bus rule %s\n", rule);
2781
2782         free(pCmd_match);
2783 out:
2784         if(pName)
2785                 free(pName);
2786         if(pInterface)
2787                 free(pInterface);
2788 }
2789
2790 void dbus_bus_remove_match_kdbus (DBusConnection *connection, const char *rule, DBusError *error)
2791 {
2792         struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
2793         int fd;
2794
2795         dbus_connection_get_socket(connection, &fd);
2796         cmd.cookie = strtoll(dbus_bus_get_unique_name(connection), NULL , 10);
2797         cmd.id = cmd.cookie;
2798         cmd.size = sizeof(struct kdbus_cmd_match);
2799
2800         if(ioctl(fd, KDBUS_CMD_MATCH_ADD, &cmd))
2801         {
2802                 if(error)
2803                         dbus_set_error(error,_dbus_error_from_errno (errno), "error removing match: %s", _dbus_strerror (errno));
2804                 _dbus_verbose("Failed removing match rule; error: %d, %m\n", errno);
2805         }
2806         else
2807                 _dbus_verbose("Match rule removed correctly.\n");
2808 }