chiark / gitweb /
libsystemd: split up into subdirs
[elogind.git] / src / libsystemd / sd-bus / bus-internal.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2013 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <netinet/in.h>
27 #include <pthread.h>
28
29 #include "hashmap.h"
30 #include "prioq.h"
31 #include "list.h"
32 #include "util.h"
33 #include "refcnt.h"
34
35 #include "sd-bus.h"
36 #include "bus-error.h"
37 #include "bus-match.h"
38 #include "bus-kernel.h"
39 #include "kdbus.h"
40
41 struct reply_callback {
42         sd_bus_message_handler_t callback;
43         void *userdata;
44         usec_t timeout;
45         uint64_t cookie;
46         unsigned prioq_idx;
47 };
48
49 struct filter_callback {
50         sd_bus_message_handler_t callback;
51         void *userdata;
52
53         unsigned last_iteration;
54
55         LIST_FIELDS(struct filter_callback, callbacks);
56 };
57
58 struct node {
59         char *path;
60         struct node *parent;
61         LIST_HEAD(struct node, child);
62         LIST_FIELDS(struct node, siblings);
63
64         LIST_HEAD(struct node_callback, callbacks);
65         LIST_HEAD(struct node_vtable, vtables);
66         LIST_HEAD(struct node_enumerator, enumerators);
67
68         bool object_manager;
69 };
70
71 struct node_callback {
72         struct node *node;
73
74         bool is_fallback;
75         sd_bus_message_handler_t callback;
76         void *userdata;
77
78         unsigned last_iteration;
79
80         LIST_FIELDS(struct node_callback, callbacks);
81 };
82
83 struct node_enumerator {
84         struct node *node;
85
86         sd_bus_node_enumerator_t callback;
87         void *userdata;
88
89         unsigned last_iteration;
90
91         LIST_FIELDS(struct node_enumerator, enumerators);
92 };
93
94 struct node_vtable {
95         struct node *node;
96
97         char *interface;
98         bool is_fallback;
99         const sd_bus_vtable *vtable;
100         void *userdata;
101         sd_bus_object_find_t find;
102
103         unsigned last_iteration;
104
105         LIST_FIELDS(struct node_vtable, vtables);
106 };
107
108 struct vtable_member {
109         const char *path;
110         const char *interface;
111         const char *member;
112         struct node_vtable *parent;
113         unsigned last_iteration;
114         const sd_bus_vtable *vtable;
115 };
116
117 enum bus_state {
118         BUS_UNSET,
119         BUS_OPENING,
120         BUS_AUTHENTICATING,
121         BUS_HELLO,
122         BUS_RUNNING,
123         BUS_CLOSING,
124         BUS_CLOSED
125 };
126
127 static inline bool BUS_IS_OPEN(enum bus_state state) {
128         return state > BUS_UNSET && state < BUS_CLOSING;
129 }
130
131 enum bus_auth {
132         _BUS_AUTH_INVALID,
133         BUS_AUTH_EXTERNAL,
134         BUS_AUTH_ANONYMOUS
135 };
136
137 struct sd_bus {
138         /* We use atomic ref counting here since sd_bus_message
139            objects retain references to their originating sd_bus but
140            we want to allow them to be processed in a different
141            thread. We won't provide full thread safety, but only the
142            bare minimum that makes it possible to use sd_bus and
143            sd_bus_message objects independently and on different
144            threads as long as each object is used only once at the
145            same time. */
146         RefCount n_ref;
147
148         enum bus_state state;
149         int input_fd, output_fd;
150         int message_version;
151         int message_endian;
152
153         bool is_kernel:1;
154         bool can_fds:1;
155         bool bus_client:1;
156         bool ucred_valid:1;
157         bool is_server:1;
158         bool anonymous_auth:1;
159         bool prefer_readv:1;
160         bool prefer_writev:1;
161         bool match_callbacks_modified:1;
162         bool filter_callbacks_modified:1;
163         bool nodes_modified:1;
164         bool trusted:1;
165         bool fake_creds_valid:1;
166         bool manual_peer_interface:1;
167
168         int use_memfd;
169
170         void *rbuffer;
171         size_t rbuffer_size;
172
173         sd_bus_message **rqueue;
174         unsigned rqueue_size;
175         size_t rqueue_allocated;
176
177         sd_bus_message **wqueue;
178         unsigned wqueue_size;
179         size_t windex;
180         size_t wqueue_allocated;
181
182         uint64_t cookie;
183
184         char *unique_name;
185         uint64_t unique_id;
186
187         struct bus_match_node match_callbacks;
188         Prioq *reply_callbacks_prioq;
189         Hashmap *reply_callbacks;
190         LIST_HEAD(struct filter_callback, filter_callbacks);
191
192         Hashmap *nodes;
193         Hashmap *vtable_methods;
194         Hashmap *vtable_properties;
195
196         union {
197                 struct sockaddr sa;
198                 struct sockaddr_un un;
199                 struct sockaddr_in in;
200                 struct sockaddr_in6 in6;
201         } sockaddr;
202         socklen_t sockaddr_size;
203
204         char *kernel;
205         char *machine;
206
207         sd_id128_t server_id;
208
209         char *address;
210         unsigned address_index;
211
212         int last_connect_error;
213
214         enum bus_auth auth;
215         size_t auth_rbegin;
216         struct iovec auth_iovec[3];
217         unsigned auth_index;
218         char *auth_buffer;
219         usec_t auth_timeout;
220
221         struct ucred ucred;
222         char label[NAME_MAX];
223
224         uint64_t creds_mask;
225
226         int *fds;
227         unsigned n_fds;
228
229         char *exec_path;
230         char **exec_argv;
231
232         uint64_t hello_cookie;
233         unsigned iteration_counter;
234
235         void *kdbus_buffer;
236
237         /* We do locking around the memfd cache, since we want to
238          * allow people to process a sd_bus_message in a different
239          * thread then it was generated on and free it there. Since
240          * adding something to the memfd cache might happen when a
241          * message is released, we hence need to protect this bit with
242          * a mutex. */
243         pthread_mutex_t memfd_cache_mutex;
244         struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
245         unsigned n_memfd_cache;
246
247         pid_t original_pid;
248
249         uint64_t hello_flags;
250         uint64_t attach_flags;
251
252         uint64_t match_cookie;
253
254         sd_event_source *input_io_event_source;
255         sd_event_source *output_io_event_source;
256         sd_event_source *time_event_source;
257         sd_event_source *quit_event_source;
258         sd_event *event;
259         int event_priority;
260
261         sd_bus_message *current;
262
263         sd_bus **default_bus_ptr;
264         pid_t tid;
265
266         struct kdbus_creds fake_creds;
267         char *fake_label;
268
269         char *cgroup_root;
270 };
271
272 #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
273
274 #define BUS_WQUEUE_MAX 1024
275 #define BUS_RQUEUE_MAX 64*1024
276
277 #define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
278 #define BUS_AUTH_SIZE_MAX (64*1024)
279
280 #define BUS_CONTAINER_DEPTH 128
281
282 /* Defined by the specification as maximum size of an array in
283  * bytes */
284 #define BUS_ARRAY_MAX_SIZE 67108864
285
286 #define BUS_FDS_MAX 1024
287
288 #define BUS_EXEC_ARGV_MAX 256
289
290 bool interface_name_is_valid(const char *p) _pure_;
291 bool service_name_is_valid(const char *p) _pure_;
292 bool member_name_is_valid(const char *p) _pure_;
293 bool object_path_is_valid(const char *p) _pure_;
294 char *object_path_startswith(const char *a, const char *b) _pure_;
295
296 bool namespace_complex_pattern(const char *pattern, const char *value) _pure_;
297 bool path_complex_pattern(const char *pattern, const char *value) _pure_;
298
299 bool namespace_simple_pattern(const char *pattern, const char *value) _pure_;
300 bool path_simple_pattern(const char *pattern, const char *value) _pure_;
301
302 int bus_message_type_from_string(const char *s, uint8_t *u) _pure_;
303 const char *bus_message_type_to_string(uint8_t u) _pure_;
304
305 #define error_name_is_valid interface_name_is_valid
306
307 int bus_ensure_running(sd_bus *bus);
308 int bus_start_running(sd_bus *bus);
309 int bus_next_address(sd_bus *bus);
310
311 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m);
312
313 int bus_rqueue_make_room(sd_bus *bus);
314
315 bool bus_pid_changed(sd_bus *bus);
316
317 char *bus_address_escape(const char *v);
318
319 #define OBJECT_PATH_FOREACH_PREFIX(prefix, path)                        \
320         for (char *_slash = ({ strcpy((prefix), (path)); streq((prefix), "/") ? NULL : strrchr((prefix), '/'); }) ; \
321              _slash && !(_slash[(_slash) == (prefix)] = 0);             \
322              _slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/'))
323
324 /* If we are invoking callbacks of a bus object, ensure unreffing the
325  * bus from the callback doesn't destroy the object we are working
326  * on */
327 #define BUS_DONT_DESTROY(bus) \
328         _cleanup_bus_unref_ _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)