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"
40 struct reply_callback {
41 sd_bus_message_handler_t callback;
48 struct filter_callback {
49 sd_bus_message_handler_t callback;
52 unsigned last_iteration;
54 LIST_FIELDS(struct filter_callback, callbacks);
60 LIST_HEAD(struct node, child);
61 LIST_FIELDS(struct node, siblings);
63 LIST_HEAD(struct node_callback, callbacks);
64 LIST_HEAD(struct node_vtable, vtables);
65 LIST_HEAD(struct node_enumerator, enumerators);
70 struct node_callback {
74 sd_bus_message_handler_t callback;
77 unsigned last_iteration;
79 LIST_FIELDS(struct node_callback, callbacks);
82 struct node_enumerator {
85 sd_bus_node_enumerator_t callback;
88 unsigned last_iteration;
90 LIST_FIELDS(struct node_enumerator, enumerators);
98 const sd_bus_vtable *vtable;
100 sd_bus_object_find_t find;
102 unsigned last_iteration;
104 LIST_FIELDS(struct node_vtable, vtables);
107 struct vtable_member {
109 const char *interface;
111 struct node_vtable *parent;
112 unsigned last_iteration;
113 const sd_bus_vtable *vtable;
125 static inline bool BUS_IS_OPEN(enum bus_state state) {
126 return state > BUS_UNSET && state < BUS_CLOSED;
136 /* We use atomic ref counting here since sd_bus_message
137 objects retain references to their originating sd_bus but
138 we want to allow them to be processed in a different
139 thread. We won't provide full thread safety, but only the
140 bare minimum that makes it possible to use sd_bus and
141 sd_bus_message objects independently and on different
142 threads as long as each object is used only once at the
146 enum bus_state state;
147 int input_fd, output_fd;
155 bool anonymous_auth:1;
157 bool prefer_writev:1;
159 bool match_callbacks_modified:1;
160 bool filter_callbacks_modified:1;
161 bool nodes_modified:1;
168 sd_bus_message **rqueue;
169 unsigned rqueue_size;
171 sd_bus_message **wqueue;
172 unsigned wqueue_size;
179 struct bus_match_node match_callbacks;
180 Prioq *reply_callbacks_prioq;
181 Hashmap *reply_callbacks;
182 LIST_HEAD(struct filter_callback, filter_callbacks);
187 Hashmap *vtable_methods;
188 Hashmap *vtable_properties;
192 struct sockaddr_un un;
193 struct sockaddr_in in;
194 struct sockaddr_in6 in6;
196 socklen_t sockaddr_size;
200 sd_id128_t server_id;
203 unsigned address_index;
205 int last_connect_error;
209 struct iovec auth_iovec[3];
215 char label[NAME_MAX];
223 uint64_t hello_serial;
224 unsigned iteration_counter;
228 /* We do locking around the memfd cache, since we want to
229 * allow people to process a sd_bus_message in a different
230 * thread then it was generated on and free it there. Since
231 * adding something to the memfd cache might happen when a
232 * message is released, we hence need to protect this bit with
234 pthread_mutex_t memfd_cache_mutex;
235 struct memfd_cache memfd_cache[MEMFD_CACHE_MAX];
236 unsigned n_memfd_cache;
240 uint64_t hello_flags;
242 uint64_t match_cookie;
244 sd_event_source *input_io_event_source;
245 sd_event_source *output_io_event_source;
246 sd_event_source *time_event_source;
247 sd_event_source *quit_event_source;
251 #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
253 #define BUS_WQUEUE_MAX 128
254 #define BUS_RQUEUE_MAX 128
256 #define BUS_MESSAGE_SIZE_MAX (64*1024*1024)
257 #define BUS_AUTH_SIZE_MAX (64*1024)
259 #define BUS_CONTAINER_DEPTH 128
261 /* Defined by the specification as maximum size of an array in
263 #define BUS_ARRAY_MAX_SIZE 67108864
265 #define BUS_FDS_MAX 1024
267 #define BUS_EXEC_ARGV_MAX 256
269 bool interface_name_is_valid(const char *p);
270 bool service_name_is_valid(const char *p);
271 bool member_name_is_valid(const char *p);
272 bool object_path_is_valid(const char *p);
273 char *object_path_startswith(const char *a, const char *b);
275 bool namespace_complex_pattern(const char *pattern, const char *value);
276 bool path_complex_pattern(const char *pattern, const char *value);
278 bool namespace_simple_pattern(const char *pattern, const char *value);
279 bool path_simple_pattern(const char *pattern, const char *value);
281 int bus_message_type_from_string(const char *s, uint8_t *u);
282 const char *bus_message_type_to_string(uint8_t u);
284 #define error_name_is_valid interface_name_is_valid
286 int bus_ensure_running(sd_bus *bus);
287 int bus_start_running(sd_bus *bus);
288 int bus_next_address(sd_bus *bus);
290 bool bus_pid_changed(sd_bus *bus);
292 #define OBJECT_PATH_FOREACH_PREFIX(prefix, path) \
293 for (char *_slash = ({ strcpy((prefix), (path)); streq((prefix), "/") ? NULL : strrchr((prefix), '/'); }) ; \
294 _slash && !(_slash[(_slash) == (prefix)] = 0); \
295 _slash = streq((prefix), "/") ? NULL : strrchr((prefix), '/'))
297 /* If we are invoking callbacks of a bus object, ensure unreffing the
298 * bus from the callback doesn't destroy the object we are working
300 #define BUS_DONT_DESTROY(bus) \
301 _cleanup_bus_unref_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus)