1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 This file is part of systemd.
8 Copyright 2013 Lennart Poettering
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.
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.
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/>.
24 #include <sys/socket.h>
26 #include <netinet/in.h>
36 #include "bus-error.h"
37 #include "bus-match.h"
38 #include "bus-kernel.h"
41 struct reply_callback {
42 sd_bus_message_handler_t callback;
49 struct filter_callback {
50 sd_bus_message_handler_t callback;
53 unsigned last_iteration;
55 LIST_FIELDS(struct filter_callback, callbacks);
61 LIST_HEAD(struct node, child);
62 LIST_FIELDS(struct node, siblings);
64 LIST_HEAD(struct node_callback, callbacks);
65 LIST_HEAD(struct node_vtable, vtables);
66 LIST_HEAD(struct node_enumerator, enumerators);
71 struct node_callback {
75 sd_bus_message_handler_t callback;
78 unsigned last_iteration;
80 LIST_FIELDS(struct node_callback, callbacks);
83 struct node_enumerator {
86 sd_bus_node_enumerator_t callback;
89 unsigned last_iteration;
91 LIST_FIELDS(struct node_enumerator, enumerators);
99 const sd_bus_vtable *vtable;
101 sd_bus_object_find_t find;
103 unsigned last_iteration;
105 LIST_FIELDS(struct node_vtable, vtables);
108 struct vtable_member {
110 const char *interface;
112 struct node_vtable *parent;
113 unsigned last_iteration;
114 const sd_bus_vtable *vtable;
127 static inline bool BUS_IS_OPEN(enum bus_state state) {
128 return state > BUS_UNSET && state < BUS_CLOSING;
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
148 enum bus_state state;
149 int input_fd, output_fd;
158 bool anonymous_auth:1;
160 bool prefer_writev:1;
161 bool match_callbacks_modified:1;
162 bool filter_callbacks_modified:1;
163 bool nodes_modified:1;
165 bool fake_creds_valid:1;
166 bool manual_peer_interface:1;
175 sd_bus_message **rqueue;
176 unsigned rqueue_size;
177 size_t rqueue_allocated;
179 sd_bus_message **wqueue;
180 unsigned wqueue_size;
182 size_t wqueue_allocated;
189 struct bus_match_node match_callbacks;
190 Prioq *reply_callbacks_prioq;
191 Hashmap *reply_callbacks;
192 LIST_HEAD(struct filter_callback, filter_callbacks);
195 Hashmap *vtable_methods;
196 Hashmap *vtable_properties;
200 struct sockaddr_un un;
201 struct sockaddr_in in;
202 struct sockaddr_in6 in6;
204 socklen_t sockaddr_size;
209 sd_id128_t server_id;
212 unsigned address_index;
214 int last_connect_error;
218 struct iovec auth_iovec[3];
224 char label[NAME_MAX];
234 uint64_t hello_cookie;
235 unsigned iteration_counter;
239 /* We do locking around the memfd cache, since we want to
240 * allow people to process a sd_bus_message in a different
241 * thread then it was generated on and free it there. Since
242 * adding something to the memfd cache might happen when a
243 * message is released, we hence need to protect this bit with
245 pthread_mutex_t memfd_cache_mutex;
246 struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
247 unsigned n_memfd_cache;
251 uint64_t hello_flags;
252 uint64_t attach_flags;
254 uint64_t match_cookie;
256 sd_event_source *input_io_event_source;
257 sd_event_source *output_io_event_source;
258 sd_event_source *time_event_source;
259 sd_event_source *quit_event_source;
263 sd_bus_message *current;
265 sd_bus **default_bus_ptr;
268 struct kdbus_creds fake_creds;
273 char *connection_name;
276 unsigned bloom_n_hash;
278 sd_bus_track *track_queue;
281 #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
283 #define BUS_WQUEUE_MAX 1024
284 #define BUS_RQUEUE_MAX 64*1024
286 #define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
287 #define BUS_AUTH_SIZE_MAX (64*1024)
289 #define BUS_CONTAINER_DEPTH 128
291 /* Defined by the specification as maximum size of an array in
293 #define BUS_ARRAY_MAX_SIZE 67108864
295 #define BUS_FDS_MAX 1024
297 #define BUS_EXEC_ARGV_MAX 256
299 bool interface_name_is_valid(const char *p) _pure_;
300 bool service_name_is_valid(const char *p) _pure_;
301 bool member_name_is_valid(const char *p) _pure_;
302 bool object_path_is_valid(const char *p) _pure_;
303 char *object_path_startswith(const char *a, const char *b) _pure_;
305 bool namespace_complex_pattern(const char *pattern, const char *value) _pure_;
306 bool path_complex_pattern(const char *pattern, const char *value) _pure_;
308 bool namespace_simple_pattern(const char *pattern, const char *value) _pure_;
309 bool path_simple_pattern(const char *pattern, const char *value) _pure_;
311 int bus_message_type_from_string(const char *s, uint8_t *u) _pure_;
312 const char *bus_message_type_to_string(uint8_t u) _pure_;
314 #define error_name_is_valid interface_name_is_valid
316 int bus_ensure_running(sd_bus *bus);
317 int bus_start_running(sd_bus *bus);
318 int bus_next_address(sd_bus *bus);
320 int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m);
322 int bus_rqueue_make_room(sd_bus *bus);
324 bool bus_pid_changed(sd_bus *bus);
326 char *bus_address_escape(const char *v);
328 #define OBJECT_PATH_FOREACH_PREFIX(prefix, path) \
329 for (char *_slash = ({ strcpy((prefix), (path)); streq((prefix), "/") ? NULL : strrchr((prefix), '/'); }) ; \
330 _slash && !(_slash[(_slash) == (prefix)] = 0); \
331 _slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/'))
333 /* If we are invoking callbacks of a bus object, ensure unreffing the
334 * bus from the callback doesn't destroy the object we are working
336 #define BUS_DONT_DESTROY(bus) \
337 _cleanup_bus_unref_ _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)