[daemon-dev][daemon-fix] starting services by direct message (autostart) and some...
[platform/upstream/dbus.git] / dbus / kdbus.h
1 /*
2  * Copyright (C) 2013 Kay Sievers
3  * Copyright (C) 2013 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
4  * Copyright (C) 2013 Linux Foundation
5  * Copyright (C) 2013 Lennart Poettering
6  * Copyright (C) 2013 Daniel Mack <daniel@zonque.org>
7  *
8  * kdbus is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the
10  * Free Software Foundation; either version 2.1 of the License, or (at
11  * your option) any later version.
12  */
13
14 #ifndef _KDBUS_H_
15 #define _KDBUS_H_
16
17 #ifndef __KERNEL__
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <linux/types.h>
21 #endif
22
23 /* todo #define KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE          SZ_8M   - maximum size of message header and items
24  * taken from internal.h of kdbus
25  * finally it should be placed higher - e.g. kdbus.h of kdbus kernel module
26  */
27 #define KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE          0x00800000              /* maximum size of message header and items */
28
29 #define KDBUS_IOC_MAGIC                 0x95
30 #define KDBUS_SRC_ID_KERNEL             (0)
31 #define KDBUS_DST_ID_WELL_KNOWN_NAME    (0)
32 #define KDBUS_MATCH_SRC_ID_ANY          (~0ULL)
33 #define KDBUS_DST_ID_BROADCAST          (~0ULL)
34
35 /* Common first elements in a structure which are used to iterate over
36  * a list of elements. */
37 #define KDBUS_PART_HEADER \
38         struct {                                                        \
39                 __u64 size;                                             \
40                 __u64 type;                                             \
41         }
42
43 #define KDBUS_PART_HEADER_SIZE offsetof(struct kdbus_item, data)
44
45 /* Message sent from kernel to userspace, when the owner or starter of
46  * a well-known name changes */
47 struct kdbus_manager_msg_name_change {
48         __u64 old_id;
49         __u64 new_id;
50         __u64 flags;                    /* 0 or (possibly?) KDBUS_NAME_IN_QUEUE */
51         char name[0];
52 };
53
54 struct kdbus_manager_msg_id_change {
55         __u64 id;
56         __u64 flags;                    /* The kernel flags field from KDBUS_HELLO */
57 };
58
59 struct kdbus_creds {
60         __u64 uid;
61         __u64 gid;
62         __u64 pid;
63         __u64 tid;
64
65         /* The starttime of the process PID. This is useful to detect
66         PID overruns from the client side. i.e. if you use the PID to
67         look something up in /proc/$PID/ you can afterwards check the
68         starttime field of it to ensure you didn't run into a PID
69         ovretun. */
70         __u64 starttime;
71 };
72
73 struct kdbus_audit {
74         __u64 sessionid;
75         __u64 loginuid;
76 };
77
78 struct kdbus_timestamp {
79         __u64 monotonic_ns;
80         __u64 realtime_ns;
81 };
82
83 struct kdbus_vec {
84         __u64 size;
85         union {
86                 __u64 address;
87                 __u64 offset;
88         };
89 };
90
91 struct kdbus_memfd {
92         __u64 size;
93         int fd;
94         __u32 __pad;
95 };
96
97 /* Message Item Types */
98 enum {
99         _KDBUS_MSG_NULL,
100
101         /* Filled in by userspace */
102         KDBUS_MSG_PAYLOAD_VEC,          /* .data_vec, reference to memory area */
103         KDBUS_MSG_PAYLOAD_OFF,          /* .data_vec, reference to memory area */
104         KDBUS_MSG_PAYLOAD_MEMFD,        /* file descriptor of a special data file */
105         KDBUS_MSG_FDS,                  /* .data_fds of file descriptors */
106         KDBUS_MSG_BLOOM,                /* for broadcasts, carries bloom filter blob in .data */
107         KDBUS_MSG_DST_NAME,             /* destination's well-known name, in .str */
108         KDBUS_MSG_PRIORITY,             /* queue priority for message */
109
110         /* Filled in by kernelspace */
111         KDBUS_MSG_SRC_NAMES     = 0x400,/* NUL separated string list with well-known names of source */
112         KDBUS_MSG_TIMESTAMP,            /* .timestamp */
113         KDBUS_MSG_SRC_CREDS,            /* .creds */
114         KDBUS_MSG_SRC_PID_COMM,         /* optional, in .str */
115         KDBUS_MSG_SRC_TID_COMM,         /* optional, in .str */
116         KDBUS_MSG_SRC_EXE,              /* optional, in .str */
117         KDBUS_MSG_SRC_CMDLINE,          /* optional, in .str (a chain of NUL str) */
118         KDBUS_MSG_SRC_CGROUP,           /* optional, in .str */
119         KDBUS_MSG_SRC_CAPS,             /* caps data blob, in .data */
120         KDBUS_MSG_SRC_SECLABEL,         /* NUL terminated string, in .str */
121         KDBUS_MSG_SRC_AUDIT,            /* .audit */
122
123         /* Special messages from kernel, consisting of one and only one of these data blocks */
124         KDBUS_MSG_NAME_ADD      = 0x800,/* .name_change */
125         KDBUS_MSG_NAME_REMOVE,          /* .name_change */
126         KDBUS_MSG_NAME_CHANGE,          /* .name_change */
127         KDBUS_MSG_ID_ADD,               /* .id_change */
128         KDBUS_MSG_ID_REMOVE,            /* .id_change */
129         KDBUS_MSG_REPLY_TIMEOUT,        /* empty, but .reply_cookie in .kdbus_msg is filled in */
130         KDBUS_MSG_REPLY_DEAD,           /* dito */
131 };
132
133 /**
134  * struct  kdbus_item - chain of data blocks
135  *
136  * size: overall data record size
137  * type: kdbus_item type of data
138  */
139 struct kdbus_item {
140         KDBUS_PART_HEADER;
141         union {
142                 /* inline data */
143                 __u8 data[0];
144                 __u32 data32[0];
145                 __u64 data64[0];
146                 char str[0];
147
148                 /* connection */
149                 __u64 id;
150
151                 /* data vector */
152                 struct kdbus_vec vec;
153
154                 /* process credentials and properties*/
155                 struct kdbus_creds creds;
156                 struct kdbus_audit audit;
157                 struct kdbus_timestamp timestamp;
158
159                 /* specific fields */
160                 struct kdbus_memfd memfd;
161                 int fds[0];
162                 struct kdbus_manager_msg_name_change name_change;
163                 struct kdbus_manager_msg_id_change id_change;
164         };
165 };
166
167 enum {
168         KDBUS_MSG_FLAGS_EXPECT_REPLY    = 1 << 0,
169         KDBUS_MSG_FLAGS_NO_AUTO_START   = 1 << 1,
170 };
171
172 enum {
173         KDBUS_PAYLOAD_KERNEL,
174         KDBUS_PAYLOAD_DBUS1     = 0x4442757356657231ULL, /* 'DBusVer1' */
175         KDBUS_PAYLOAD_GVARIANT  = 0x4756617269616e74ULL, /* 'GVariant' */
176 };
177
178 /**
179  * struct kdbus_msg
180  *
181  * set by userspace:
182  * dst_id: destination id
183  * flags: KDBUS_MSG_FLAGS_*
184  * items: data records
185  *
186  * set by kernel:
187  * src_id: who sent the message
188  */
189 struct kdbus_msg {
190         __u64 size;
191         __u64 flags;
192         __u64 dst_id;                   /* connection, 0 == name in data, ~0 broadcast */
193         __u64 src_id;                   /* connection, 0 == kernel */
194         __u64 payload_type;             /* 'DBusVer1', 'GVariant', ... */
195         __u64 cookie;                   /* userspace-supplied cookie */
196         union {
197                 __u64 cookie_reply;     /* cookie we reply to */
198                 __u64 timeout_ns;       /* timespan to wait for reply */
199         };
200         struct kdbus_item items[0];
201 };
202
203 enum {
204         _KDBUS_POLICY_NULL,
205         KDBUS_POLICY_NAME,
206         KDBUS_POLICY_ACCESS,
207 };
208
209 enum {
210         _KDBUS_POLICY_ACCESS_NULL,
211         KDBUS_POLICY_ACCESS_USER,
212         KDBUS_POLICY_ACCESS_GROUP,
213         KDBUS_POLICY_ACCESS_WORLD,
214 };
215
216 enum {
217         KDBUS_POLICY_RECV               = 1 <<  2,
218         KDBUS_POLICY_SEND               = 1 <<  1,
219         KDBUS_POLICY_OWN                = 1 <<  0,
220 };
221
222 struct kdbus_policy_access {
223         __u64 type;             /* USER, GROUP, WORLD */
224         __u64 bits;             /* RECV, SEND, OWN */
225         __u64 id;               /* uid, gid, 0 */
226 };
227
228 //FIXME: convert access to access[]
229 struct kdbus_policy {
230         KDBUS_PART_HEADER;
231         union {
232                 char name[0];
233                 struct kdbus_policy_access access;
234         };
235 };
236
237 /* A series of KDBUS_POLICY_NAME, plus one or more KDBUS_POLICY_ACCESS */
238 struct kdbus_cmd_policy {
239         __u64 size;
240         struct kdbus_policy policies[0];
241 };
242
243 /* Flags for struct kdbus_cmd_hello */
244 enum {
245         KDBUS_HELLO_STARTER             =  1 <<  0,
246         KDBUS_HELLO_ACCEPT_FD           =  1 <<  1,
247
248         /* The following have an effect on directed messages only --
249          * not for broadcasts */
250         KDBUS_HELLO_ATTACH_COMM         =  1 << 10,
251         KDBUS_HELLO_ATTACH_EXE          =  1 << 11,
252         KDBUS_HELLO_ATTACH_CMDLINE      =  1 << 12,
253         KDBUS_HELLO_ATTACH_CGROUP       =  1 << 13,
254         KDBUS_HELLO_ATTACH_CAPS         =  1 << 14,
255         KDBUS_HELLO_ATTACH_SECLABEL     =  1 << 15,
256         KDBUS_HELLO_ATTACH_AUDIT        =  1 << 16,
257 };
258
259 struct kdbus_cmd_hello {
260         __u64 size;
261
262         /* userspace → kernel, kernel → userspace */
263         __u64 conn_flags;       /* userspace specifies its
264                                  * capabilities and more, kernel
265                                  * returns its capabilites and
266                                  * more. Kernel might refuse client's
267                                  * capabilities by returning an error
268                                  * from KDBUS_HELLO */
269
270         /* kernel → userspace */
271         __u64 bus_flags;        /* this is .flags copied verbatim from
272                                  * from original KDBUS_CMD_BUS_MAKE
273                                  * ioctl. It's intended to be useful
274                                  * to do negotiation of features of
275                                  * the payload that is transfreted. */
276         __u64 id;               /* id assigned to this connection */
277         __u64 bloom_size;       /* The bloom filter size chosen by the
278                                  * bus owner */
279         __u64 pool_size;        /* maximum size of pool buffer */
280         struct kdbus_item items[0];
281 };
282
283 /* Flags for kdbus_cmd_{bus,ep,ns}_make */
284 enum {
285         KDBUS_MAKE_ACCESS_GROUP         = 1 <<  0,
286         KDBUS_MAKE_ACCESS_WORLD         = 1 <<  1,
287         KDBUS_MAKE_POLICY_OPEN          = 1 <<  2,
288 };
289
290 /* Items to append to kdbus_cmd_{bus,ep,ns}_make */
291 enum {
292         _KDBUS_MAKE_NULL,
293         KDBUS_MAKE_NAME,
294         KDBUS_MAKE_CGROUP,      /* the cgroup hierarchy ID for which to attach
295                                  * cgroup membership paths to messages.
296                                  * FIXME: remove, use *the* hierarchy */
297         KDBUS_MAKE_CRED,        /* allow translator services which connect
298                                  * to the bus on behalf of somebody else,
299                                  * allow specifiying the credentials of the
300                                  * client to connect on behalf on. Needs
301                                  * privileges */
302 };
303
304 struct kdbus_cmd_bus_make {
305         __u64 size;
306         __u64 flags;            /* userspace → kernel, kernel → userspace
307                                  * When creating a bus feature
308                                  * kernel negotiation. */
309         __u64 bus_flags;        /* userspace → kernel
310                                  * When a bus is created this value is
311                                  * copied verbatim into the bus
312                                  * structure and returned from
313                                  * KDBUS_CMD_HELLO, later */
314         __u64 bloom_size;       /* size of the bloom filter for this bus */
315         struct kdbus_item items[0];
316 };
317
318 struct kdbus_cmd_ep_make {
319         __u64 size;
320         __u64 flags;            /* userspace → kernel, kernel → userspace
321                                  * When creating an entry point
322                                  * feature kernel negotiation done the
323                                  * same way as for
324                                  * KDBUS_CMD_BUS_MAKE. Unused for
325                                  * now. */
326         struct kdbus_item items[0];
327 };
328
329 struct kdbus_cmd_ns_make {
330         __u64 size;
331         __u64 flags;            /* userspace → kernel, kernel → userspace
332                                  * When creating an entry point
333                                  * feature kernel negotiation done the
334                                  * same way as for
335                                  * KDBUS_CMD_BUS_MAKE. Unused for
336                                  * now. */
337         struct kdbus_item items[0];
338 };
339
340 enum {
341         /* userspace → kernel */
342         KDBUS_NAME_REPLACE_EXISTING             = 1 <<  0,
343         KDBUS_NAME_QUEUE                        = 1 <<  1,
344         KDBUS_NAME_ALLOW_REPLACEMENT            = 1 <<  2,
345
346         KDBUS_NAME_STARTER        = 1 <<  7,
347
348         /* kernel → userspace */
349         KDBUS_NAME_IN_QUEUE                     = 1 << 16,
350 };
351
352 /* We allow (de)regestration of names of other peers */
353 struct kdbus_cmd_name {
354         __u64 size;
355         __u64 flags;
356         __u64 id;
357         __u64 conn_flags;
358         char name[0];
359 };
360
361 struct kdbus_cmd_names {
362         __u64 size;
363         struct kdbus_cmd_name names[0];
364 };
365
366 enum {
367         _KDBUS_NAME_INFO_ITEM_NULL,
368         KDBUS_NAME_INFO_ITEM_NAME,      /* userspace → kernel */
369         KDBUS_NAME_INFO_ITEM_SECLABEL,  /* kernel → userspace */
370         KDBUS_NAME_INFO_ITEM_AUDIT,     /* kernel → userspace */
371 };
372
373 struct kdbus_cmd_name_info {
374         __u64 size;                     /* overall size of info */
375         __u64 flags;
376         __u64 id;                       /* either ID, or 0 and _ITEM_NAME follows */
377         struct kdbus_creds creds;
378         struct kdbus_item items[0];     /* list of item records */
379 };
380
381 enum {
382         _KDBUS_MATCH_NULL,
383         KDBUS_MATCH_BLOOM,              /* Matches a mask blob against KDBUS_MSG_BLOOM */
384         KDBUS_MATCH_SRC_NAME,           /* Matches a name string against KDBUS_MSG_SRC_NAMES */
385         KDBUS_MATCH_NAME_ADD,           /* Matches a name string against KDBUS_MSG_NAME_ADD */
386         KDBUS_MATCH_NAME_REMOVE,        /* Matches a name string against KDBUS_MSG_NAME_REMOVE */
387         KDBUS_MATCH_NAME_CHANGE,        /* Matches a name string against KDBUS_MSG_NAME_CHANGE */
388         KDBUS_MATCH_ID_ADD,             /* Matches an ID against KDBUS_MSG_ID_ADD */
389         KDBUS_MATCH_ID_REMOVE,          /* Matches an ID against KDBUS_MSG_ID_REMOVE */
390 };
391
392 struct kdbus_cmd_match {
393         __u64 size;
394         __u64 id;       /* We allow registration/deregestration of matches for other peers */
395         __u64 cookie;   /* userspace supplied cookie; when removing; kernel deletes everything with same cookie */
396         __u64 src_id;   /* ~0: any. other: exact unique match */
397         struct kdbus_item items[0];
398 };
399
400 struct kdbus_cmd_monitor {
401         __u64 id;               /* We allow setting the monitor flag of other peers */
402         unsigned int enable;    /* A boolean to enable/disable monitoring */
403         __u32 __pad;
404 };
405
406 /* FD states:
407  * control nodes: unset
408  *   bus owner  (via KDBUS_CMD_BUS_MAKE)
409  *   ns owner   (via KDBUS_CMD_NS_MAKE)
410  *
411  * ep nodes: unset
412  *   connected  (via KDBUS_CMD_HELLO)
413  *   starter    (via KDBUS_CMD_HELLO with KDBUS_CMD_HELLO_STARTER)
414  *   ep owner   (via KDBUS_CMD_EP_MAKE)
415  */
416 enum {
417         /* kdbus control node commands: require unset state */
418         KDBUS_CMD_BUS_MAKE =            _IOW(KDBUS_IOC_MAGIC, 0x00, struct kdbus_cmd_bus_make),
419         KDBUS_CMD_NS_MAKE =             _IOR(KDBUS_IOC_MAGIC, 0x10, struct kdbus_cmd_ns_make),
420
421         /* kdbus ep node commands: require unset state */
422         KDBUS_CMD_EP_MAKE =             _IOW(KDBUS_IOC_MAGIC, 0x20, struct kdbus_cmd_ep_make),
423         KDBUS_CMD_HELLO =               _IOWR(KDBUS_IOC_MAGIC, 0x30, struct kdbus_cmd_hello),
424
425         /* kdbus ep node commands: require connected state */
426         KDBUS_CMD_MSG_SEND =            _IOW(KDBUS_IOC_MAGIC, 0x40, struct kdbus_msg),
427         KDBUS_CMD_MSG_RECV =            _IOR(KDBUS_IOC_MAGIC, 0x41, __u64 *),
428         KDBUS_CMD_MSG_RELEASE =         _IOW(KDBUS_IOC_MAGIC, 0x42, __u64 *),
429
430         KDBUS_CMD_NAME_ACQUIRE =        _IOWR(KDBUS_IOC_MAGIC, 0x50, struct kdbus_cmd_name),
431         KDBUS_CMD_NAME_RELEASE =        _IOW(KDBUS_IOC_MAGIC, 0x51, struct kdbus_cmd_name),
432         KDBUS_CMD_NAME_LIST =           _IOWR(KDBUS_IOC_MAGIC, 0x52, struct kdbus_cmd_names),
433         KDBUS_CMD_NAME_QUERY =          _IOWR(KDBUS_IOC_MAGIC, 0x53, struct kdbus_cmd_name_info),
434
435         KDBUS_CMD_MATCH_ADD =           _IOW(KDBUS_IOC_MAGIC, 0x60, struct kdbus_cmd_match),
436         KDBUS_CMD_MATCH_REMOVE =        _IOW(KDBUS_IOC_MAGIC, 0x61, struct kdbus_cmd_match),
437         KDBUS_CMD_MONITOR =             _IOW(KDBUS_IOC_MAGIC, 0x62, struct kdbus_cmd_monitor),
438
439         /* kdbus ep node commands: require ep owner state */
440         KDBUS_CMD_EP_POLICY_SET =       _IOW(KDBUS_IOC_MAGIC, 0x70, struct kdbus_cmd_policy),
441
442         /* kdbus memfd commands: */
443         KDBUS_CMD_MEMFD_NEW =           _IOR(KDBUS_IOC_MAGIC, 0x80, int *),
444         KDBUS_CMD_MEMFD_SIZE_GET =      _IOR(KDBUS_IOC_MAGIC, 0x81, __u64 *),
445         KDBUS_CMD_MEMFD_SIZE_SET =      _IOW(KDBUS_IOC_MAGIC, 0x82, __u64 *),
446         KDBUS_CMD_MEMFD_SEAL_GET =      _IOR(KDBUS_IOC_MAGIC, 0x83, int *),
447         KDBUS_CMD_MEMFD_SEAL_SET =      _IO(KDBUS_IOC_MAGIC, 0x84),
448 };
449 #endif