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