]>
Commit | Line | Data |
---|---|---|
de1c301e LP |
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> | |
45fbe937 | 27 | #include <pthread.h> |
de1c301e LP |
28 | |
29 | #include "hashmap.h" | |
e3017af9 | 30 | #include "prioq.h" |
de1c301e LP |
31 | #include "list.h" |
32 | #include "util.h" | |
e4ee6e5c | 33 | #include "refcnt.h" |
de1c301e LP |
34 | |
35 | #include "sd-bus.h" | |
36 | #include "bus-error.h" | |
392d5b37 | 37 | #include "bus-match.h" |
bc7fd8cd | 38 | #include "bus-kernel.h" |
8a0e0ed9 | 39 | #include "kdbus.h" |
de1c301e LP |
40 | |
41 | struct reply_callback { | |
52f3ba91 | 42 | sd_bus_message_handler_t callback; |
de1c301e LP |
43 | void *userdata; |
44 | usec_t timeout; | |
693eb9a2 | 45 | uint64_t cookie; |
e3017af9 | 46 | unsigned prioq_idx; |
de1c301e LP |
47 | }; |
48 | ||
49 | struct filter_callback { | |
52f3ba91 | 50 | sd_bus_message_handler_t callback; |
de1c301e LP |
51 | void *userdata; |
52 | ||
7286037f LP |
53 | unsigned last_iteration; |
54 | ||
de1c301e LP |
55 | LIST_FIELDS(struct filter_callback, callbacks); |
56 | }; | |
57 | ||
29ddb38f LP |
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; | |
52f3ba91 | 75 | sd_bus_message_handler_t callback; |
a652755d LP |
76 | void *userdata; |
77 | ||
29ddb38f LP |
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; | |
a652755d | 98 | bool is_fallback; |
29ddb38f LP |
99 | const sd_bus_vtable *vtable; |
100 | void *userdata; | |
101 | sd_bus_object_find_t find; | |
7286037f LP |
102 | |
103 | unsigned last_iteration; | |
29ddb38f LP |
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; | |
a652755d LP |
115 | }; |
116 | ||
de1c301e | 117 | enum bus_state { |
021a1e78 | 118 | BUS_UNSET, |
de1c301e LP |
119 | BUS_OPENING, |
120 | BUS_AUTHENTICATING, | |
121 | BUS_HELLO, | |
f54514f3 | 122 | BUS_RUNNING, |
718db961 | 123 | BUS_CLOSING, |
f54514f3 | 124 | BUS_CLOSED |
de1c301e LP |
125 | }; |
126 | ||
f54514f3 | 127 | static inline bool BUS_IS_OPEN(enum bus_state state) { |
718db961 | 128 | return state > BUS_UNSET && state < BUS_CLOSING; |
f54514f3 LP |
129 | } |
130 | ||
2181a7f5 LP |
131 | enum bus_auth { |
132 | _BUS_AUTH_INVALID, | |
133 | BUS_AUTH_EXTERNAL, | |
134 | BUS_AUTH_ANONYMOUS | |
135 | }; | |
136 | ||
de1c301e | 137 | struct sd_bus { |
e4ee6e5c LP |
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 | ||
de1c301e | 148 | enum bus_state state; |
e82c9509 | 149 | int input_fd, output_fd; |
de1c301e | 150 | int message_version; |
0f437184 | 151 | int message_endian; |
021a1e78 | 152 | |
6629161f | 153 | bool is_kernel:1; |
de1c301e | 154 | bool can_fds:1; |
94bbf1ba | 155 | bool bus_client:1; |
2571ead1 | 156 | bool ucred_valid:1; |
2181a7f5 LP |
157 | bool is_server:1; |
158 | bool anonymous_auth:1; | |
15d5af81 LP |
159 | bool prefer_readv:1; |
160 | bool prefer_writev:1; | |
7286037f LP |
161 | bool match_callbacks_modified:1; |
162 | bool filter_callbacks_modified:1; | |
29ddb38f | 163 | bool nodes_modified:1; |
adacb957 | 164 | bool trusted:1; |
8a0e0ed9 | 165 | bool fake_creds_valid:1; |
758bf0c7 | 166 | bool manual_peer_interface:1; |
5972fe95 LP |
167 | bool is_system:1; |
168 | bool is_user:1; | |
de1c301e | 169 | |
8f155917 LP |
170 | int use_memfd; |
171 | ||
de1c301e LP |
172 | void *rbuffer; |
173 | size_t rbuffer_size; | |
174 | ||
175 | sd_bus_message **rqueue; | |
821e0756 LP |
176 | unsigned rqueue_size; |
177 | size_t rqueue_allocated; | |
de1c301e LP |
178 | |
179 | sd_bus_message **wqueue; | |
180 | unsigned wqueue_size; | |
181 | size_t windex; | |
821e0756 | 182 | size_t wqueue_allocated; |
de1c301e | 183 | |
693eb9a2 | 184 | uint64_t cookie; |
de1c301e LP |
185 | |
186 | char *unique_name; | |
219728b3 | 187 | uint64_t unique_id; |
de1c301e | 188 | |
392d5b37 | 189 | struct bus_match_node match_callbacks; |
e3017af9 | 190 | Prioq *reply_callbacks_prioq; |
de1c301e LP |
191 | Hashmap *reply_callbacks; |
192 | LIST_HEAD(struct filter_callback, filter_callbacks); | |
29ddb38f LP |
193 | |
194 | Hashmap *nodes; | |
29ddb38f LP |
195 | Hashmap *vtable_methods; |
196 | Hashmap *vtable_properties; | |
de1c301e LP |
197 | |
198 | union { | |
199 | struct sockaddr sa; | |
200 | struct sockaddr_un un; | |
201 | struct sockaddr_in in; | |
202 | struct sockaddr_in6 in6; | |
203 | } sockaddr; | |
204 | socklen_t sockaddr_size; | |
205 | ||
6629161f | 206 | char *kernel; |
a7893c6b | 207 | char *machine; |
6629161f | 208 | |
98178d39 | 209 | sd_id128_t server_id; |
de1c301e LP |
210 | |
211 | char *address; | |
212 | unsigned address_index; | |
213 | ||
214 | int last_connect_error; | |
215 | ||
2181a7f5 LP |
216 | enum bus_auth auth; |
217 | size_t auth_rbegin; | |
de1c301e LP |
218 | struct iovec auth_iovec[3]; |
219 | unsigned auth_index; | |
2181a7f5 | 220 | char *auth_buffer; |
e3017af9 | 221 | usec_t auth_timeout; |
2571ead1 LP |
222 | |
223 | struct ucred ucred; | |
224 | char label[NAME_MAX]; | |
2c93b4ef | 225 | |
5b12334d LP |
226 | uint64_t creds_mask; |
227 | ||
2c93b4ef LP |
228 | int *fds; |
229 | unsigned n_fds; | |
2fd9ae2e LP |
230 | |
231 | char *exec_path; | |
232 | char **exec_argv; | |
9d373862 | 233 | |
693eb9a2 | 234 | uint64_t hello_cookie; |
7286037f | 235 | unsigned iteration_counter; |
fd8d62d9 LP |
236 | |
237 | void *kdbus_buffer; | |
bc7fd8cd | 238 | |
45fbe937 LP |
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 | |
244 | * a mutex. */ | |
245 | pthread_mutex_t memfd_cache_mutex; | |
bc7fd8cd LP |
246 | struct memfd_cache memfd_cache[MEMFD_CACHE_MAX]; |
247 | unsigned n_memfd_cache; | |
d5a2b9a6 LP |
248 | |
249 | pid_t original_pid; | |
264ad849 LP |
250 | |
251 | uint64_t hello_flags; | |
d21a7bb1 | 252 | uint64_t attach_flags; |
c7819669 LP |
253 | |
254 | uint64_t match_cookie; | |
89ffcd2a | 255 | |
40ca29a1 LP |
256 | sd_event_source *input_io_event_source; |
257 | sd_event_source *output_io_event_source; | |
258 | sd_event_source *time_event_source; | |
abc5fe72 | 259 | sd_event_source *quit_event_source; |
40ca29a1 | 260 | sd_event *event; |
1e05d493 | 261 | int event_priority; |
affff0b6 LP |
262 | |
263 | sd_bus_message *current; | |
76b54375 LP |
264 | |
265 | sd_bus **default_bus_ptr; | |
266 | pid_t tid; | |
8a0e0ed9 LP |
267 | |
268 | struct kdbus_creds fake_creds; | |
269 | char *fake_label; | |
751bc6ac LP |
270 | |
271 | char *cgroup_root; | |
5972fe95 LP |
272 | |
273 | char *connection_name; | |
b28ff39f LP |
274 | |
275 | size_t bloom_size; | |
276 | unsigned bloom_n_hash; | |
8f8f05a9 LP |
277 | |
278 | sd_bus_track *track_queue; | |
40ca29a1 | 279 | }; |
e3017af9 LP |
280 | |
281 | #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC)) | |
25220239 | 282 | |
7a4a2105 LP |
283 | #define BUS_WQUEUE_MAX 1024 |
284 | #define BUS_RQUEUE_MAX 64*1024 | |
25220239 LP |
285 | |
286 | #define BUS_MESSAGE_SIZE_MAX (64*1024*1024) | |
287 | #define BUS_AUTH_SIZE_MAX (64*1024) | |
ac89bf1d | 288 | |
ed205a6b LP |
289 | #define BUS_CONTAINER_DEPTH 128 |
290 | ||
ac89bf1d LP |
291 | /* Defined by the specification as maximum size of an array in |
292 | * bytes */ | |
293 | #define BUS_ARRAY_MAX_SIZE 67108864 | |
294 | ||
2c93b4ef LP |
295 | #define BUS_FDS_MAX 1024 |
296 | ||
2fd9ae2e LP |
297 | #define BUS_EXEC_ARGV_MAX 256 |
298 | ||
0ce036ce LP |
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_; | |
6693860f | 304 | |
0ce036ce LP |
305 | bool namespace_complex_pattern(const char *pattern, const char *value) _pure_; |
306 | bool path_complex_pattern(const char *pattern, const char *value) _pure_; | |
392d5b37 | 307 | |
0ce036ce LP |
308 | bool namespace_simple_pattern(const char *pattern, const char *value) _pure_; |
309 | bool path_simple_pattern(const char *pattern, const char *value) _pure_; | |
392d5b37 | 310 | |
0ce036ce LP |
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_; | |
392d5b37 | 313 | |
6693860f | 314 | #define error_name_is_valid interface_name_is_valid |
20902f3e LP |
315 | |
316 | int bus_ensure_running(sd_bus *bus); | |
a7e3212d LP |
317 | int bus_start_running(sd_bus *bus); |
318 | int bus_next_address(sd_bus *bus); | |
d5a2b9a6 | 319 | |
7adc46fc | 320 | int bus_seal_synthetic_message(sd_bus *b, sd_bus_message *m); |
777d7a61 | 321 | |
7adc46fc | 322 | int bus_rqueue_make_room(sd_bus *bus); |
7d22c717 | 323 | |
d5a2b9a6 | 324 | bool bus_pid_changed(sd_bus *bus); |
92e189e5 | 325 | |
0f8bd8de LP |
326 | char *bus_address_escape(const char *v); |
327 | ||
92e189e5 LP |
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), '/')) | |
8ce2afd6 LP |
332 | |
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 | |
335 | * on */ | |
336 | #define BUS_DONT_DESTROY(bus) \ | |
d7726e57 | 337 | _cleanup_bus_unref_ _unused_ sd_bus *_dont_destroy_##bus = sd_bus_ref(bus) |