chiark / gitweb /
__thread --> thread_local for C11 compat
[elogind.git] / src / libsystemd-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
40 struct reply_callback {
41         sd_bus_message_handler_t callback;
42         void *userdata;
43         usec_t timeout;
44         uint64_t serial;
45         unsigned prioq_idx;
46 };
47
48 struct filter_callback {
49         sd_bus_message_handler_t callback;
50         void *userdata;
51
52         unsigned last_iteration;
53
54         LIST_FIELDS(struct filter_callback, callbacks);
55 };
56
57 struct node {
58         char *path;
59         struct node *parent;
60         LIST_HEAD(struct node, child);
61         LIST_FIELDS(struct node, siblings);
62
63         LIST_HEAD(struct node_callback, callbacks);
64         LIST_HEAD(struct node_vtable, vtables);
65         LIST_HEAD(struct node_enumerator, enumerators);
66
67         bool object_manager;
68 };
69
70 struct node_callback {
71         struct node *node;
72
73         bool is_fallback;
74         sd_bus_message_handler_t callback;
75         void *userdata;
76
77         unsigned last_iteration;
78
79         LIST_FIELDS(struct node_callback, callbacks);
80 };
81
82 struct node_enumerator {
83         struct node *node;
84
85         sd_bus_node_enumerator_t callback;
86         void *userdata;
87
88         unsigned last_iteration;
89
90         LIST_FIELDS(struct node_enumerator, enumerators);
91 };
92
93 struct node_vtable {
94         struct node *node;
95
96         char *interface;
97         bool is_fallback;
98         const sd_bus_vtable *vtable;
99         void *userdata;
100         sd_bus_object_find_t find;
101
102         unsigned last_iteration;
103
104         LIST_FIELDS(struct node_vtable, vtables);
105 };
106
107 struct vtable_member {
108         const char *path;
109         const char *interface;
110         const char *member;
111         struct node_vtable *parent;
112         unsigned last_iteration;
113         const sd_bus_vtable *vtable;
114 };
115
116 enum bus_state {
117         BUS_UNSET,
118         BUS_OPENING,
119         BUS_AUTHENTICATING,
120         BUS_HELLO,
121         BUS_RUNNING,
122         BUS_CLOSING,
123         BUS_CLOSED
124 };
125
126 static inline bool BUS_IS_OPEN(enum bus_state state) {
127         return state > BUS_UNSET && state < BUS_CLOSING;
128 }
129
130 enum bus_auth {
131         _BUS_AUTH_INVALID,
132         BUS_AUTH_EXTERNAL,
133         BUS_AUTH_ANONYMOUS
134 };
135
136 struct sd_bus {
137         /* We use atomic ref counting here since sd_bus_message
138            objects retain references to their originating sd_bus but
139            we want to allow them to be processed in a different
140            thread. We won't provide full thread safety, but only the
141            bare minimum that makes it possible to use sd_bus and
142            sd_bus_message objects independently and on different
143            threads as long as each object is used only once at the
144            same time. */
145         RefCount n_ref;
146
147         enum bus_state state;
148         int input_fd, output_fd;
149         int message_version;
150         int message_endian;
151
152         bool is_kernel:1;
153         bool can_fds:1;
154         bool bus_client:1;
155         bool ucred_valid:1;
156         bool is_server:1;
157         bool anonymous_auth:1;
158         bool prefer_readv:1;
159         bool prefer_writev:1;
160         bool match_callbacks_modified:1;
161         bool filter_callbacks_modified:1;
162         bool nodes_modified:1;
163         bool trusted:1;
164
165         int use_memfd;
166
167         void *rbuffer;
168         size_t rbuffer_size;
169
170         sd_bus_message **rqueue;
171         unsigned rqueue_size;
172         size_t rqueue_allocated;
173
174         sd_bus_message **wqueue;
175         unsigned wqueue_size;
176         size_t windex;
177         size_t wqueue_allocated;
178
179         uint64_t serial;
180
181         char *unique_name;
182         uint64_t unique_id;
183
184         struct bus_match_node match_callbacks;
185         Prioq *reply_callbacks_prioq;
186         Hashmap *reply_callbacks;
187         LIST_HEAD(struct filter_callback, filter_callbacks);
188
189         Hashmap *nodes;
190         Hashmap *vtable_methods;
191         Hashmap *vtable_properties;
192
193         union {
194                 struct sockaddr sa;
195                 struct sockaddr_un un;
196                 struct sockaddr_in in;
197                 struct sockaddr_in6 in6;
198         } sockaddr;
199         socklen_t sockaddr_size;
200
201         char *kernel;
202         char *machine;
203
204         sd_id128_t server_id;
205
206         char *address;
207         unsigned address_index;
208
209         int last_connect_error;
210
211         enum bus_auth auth;
212         size_t auth_rbegin;
213         struct iovec auth_iovec[3];
214         unsigned auth_index;
215         char *auth_buffer;
216         usec_t auth_timeout;
217
218         struct ucred ucred;
219         char label[NAME_MAX];
220
221         uint64_t creds_mask;
222
223         int *fds;
224         unsigned n_fds;
225
226         char *exec_path;
227         char **exec_argv;
228
229         uint64_t hello_serial;
230         unsigned iteration_counter;
231
232         void *kdbus_buffer;
233
234         /* We do locking around the memfd cache, since we want to
235          * allow people to process a sd_bus_message in a different
236          * thread then it was generated on and free it there. Since
237          * adding something to the memfd cache might happen when a
238          * message is released, we hence need to protect this bit with
239          * a mutex. */
240         pthread_mutex_t memfd_cache_mutex;
241         struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
242         unsigned n_memfd_cache;
243
244         pid_t original_pid;
245
246         uint64_t hello_flags;
247         uint64_t attach_flags;
248
249         uint64_t match_cookie;
250
251         sd_event_source *input_io_event_source;
252         sd_event_source *output_io_event_source;
253         sd_event_source *time_event_source;
254         sd_event_source *quit_event_source;
255         sd_event *event;
256         int event_priority;
257
258         sd_bus_message *current;
259
260         sd_bus **default_bus_ptr;
261         pid_t tid;
262 };
263
264 #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
265
266 #define BUS_WQUEUE_MAX 1024
267 #define BUS_RQUEUE_MAX 64*1024
268
269 #define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
270 #define BUS_AUTH_SIZE_MAX (64*1024)
271
272 #define BUS_CONTAINER_DEPTH 128
273
274 /* Defined by the specification as maximum size of an array in
275  * bytes */
276 #define BUS_ARRAY_MAX_SIZE 67108864
277
278 #define BUS_FDS_MAX 1024
279
280 #define BUS_EXEC_ARGV_MAX 256
281
282 bool interface_name_is_valid(const char *p);
283 bool service_name_is_valid(const char *p);
284 bool member_name_is_valid(const char *p);
285 bool object_path_is_valid(const char *p);
286 char *object_path_startswith(const char *a, const char *b);
287
288 bool namespace_complex_pattern(const char *pattern, const char *value);
289 bool path_complex_pattern(const char *pattern, const char *value);
290
291 bool namespace_simple_pattern(const char *pattern, const char *value);
292 bool path_simple_pattern(const char *pattern, const char *value);
293
294 int bus_message_type_from_string(const char *s, uint8_t *u);
295 const char *bus_message_type_to_string(uint8_t u);
296
297 #define error_name_is_valid interface_name_is_valid
298
299 int bus_ensure_running(sd_bus *bus);
300 int bus_start_running(sd_bus *bus);
301 int bus_next_address(sd_bus *bus);
302
303 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m);
304
305 int bus_rqueue_make_room(sd_bus *bus);
306
307 bool bus_pid_changed(sd_bus *bus);
308
309 char *bus_address_escape(const char *v);
310
311 #define OBJECT_PATH_FOREACH_PREFIX(prefix, path)                        \
312         for (char *_slash = ({ strcpy((prefix), (path)); streq((prefix), "/") ? NULL : strrchr((prefix), '/'); }) ; \
313              _slash && !(_slash[(_slash) == (prefix)] = 0);             \
314              _slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/'))
315
316 /* If we are invoking callbacks of a bus object, ensure unreffing the
317  * bus from the callback doesn't destroy the object we are working
318  * on */
319 #define BUS_DONT_DESTROY(bus) \
320         _cleanup_bus_unref_ _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)