]>
Commit | Line | Data |
---|---|---|
d41bd96f LP |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include <sys/poll.h> | |
4 | ||
5 | #include "alloc-util.h" | |
6 | #include "errno-util.h" | |
7 | #include "fd-util.h" | |
8 | #include "hashmap.h" | |
9 | #include "list.h" | |
10 | #include "process-util.h" | |
11 | #include "set.h" | |
12 | #include "socket-util.h" | |
13 | #include "string-table.h" | |
14 | #include "string-util.h" | |
15 | #include "strv.h" | |
16 | #include "time-util.h" | |
17 | #include "umask-util.h" | |
18 | #include "user-util.h" | |
19 | #include "varlink.h" | |
20 | ||
21 | #define VARLINK_DEFAULT_CONNECTIONS_MAX 4096U | |
22 | #define VARLINK_DEFAULT_CONNECTIONS_PER_UID_MAX 1024U | |
23 | ||
24 | #define VARLINK_DEFAULT_TIMEOUT_USEC (45U*USEC_PER_SEC) | |
25 | #define VARLINK_BUFFER_MAX (16U*1024U*1024U) | |
26 | #define VARLINK_READ_SIZE (64U*1024U) | |
27 | ||
28 | typedef enum VarlinkState { | |
29 | /* Client side states */ | |
30 | VARLINK_IDLE_CLIENT, | |
31 | VARLINK_AWAITING_REPLY, | |
45a6c965 | 32 | VARLINK_AWAITING_REPLY_MORE, |
d41bd96f LP |
33 | VARLINK_CALLING, |
34 | VARLINK_CALLED, | |
35 | VARLINK_PROCESSING_REPLY, | |
36 | ||
37 | /* Server side states */ | |
38 | VARLINK_IDLE_SERVER, | |
39 | VARLINK_PROCESSING_METHOD, | |
40 | VARLINK_PROCESSING_METHOD_MORE, | |
41 | VARLINK_PROCESSING_METHOD_ONEWAY, | |
42 | VARLINK_PROCESSED_METHOD, | |
d41bd96f LP |
43 | VARLINK_PENDING_METHOD, |
44 | VARLINK_PENDING_METHOD_MORE, | |
45 | ||
46 | /* Common states (only during shutdown) */ | |
47 | VARLINK_PENDING_DISCONNECT, | |
48 | VARLINK_PENDING_TIMEOUT, | |
49 | VARLINK_PROCESSING_DISCONNECT, | |
50 | VARLINK_PROCESSING_TIMEOUT, | |
51 | VARLINK_PROCESSING_FAILURE, | |
52 | VARLINK_DISCONNECTED, | |
53 | ||
54 | _VARLINK_STATE_MAX, | |
55 | _VARLINK_STATE_INVALID = -1 | |
56 | } VarlinkState; | |
57 | ||
58 | /* Tests whether we are not yet disconnected. Note that this is true during all states where the connection | |
59 | * is still good for something, and false only when it's dead for good. This means: when we are | |
60 | * asynchronously connecting to a peer and the connect() is still pending, then this will return 'true', as | |
61 | * the connection is still good, and we are likely to be able to properly operate on it soon. */ | |
62 | #define VARLINK_STATE_IS_ALIVE(state) \ | |
63 | IN_SET(state, \ | |
64 | VARLINK_IDLE_CLIENT, \ | |
65 | VARLINK_AWAITING_REPLY, \ | |
45a6c965 | 66 | VARLINK_AWAITING_REPLY_MORE, \ |
d41bd96f LP |
67 | VARLINK_CALLING, \ |
68 | VARLINK_CALLED, \ | |
69 | VARLINK_PROCESSING_REPLY, \ | |
70 | VARLINK_IDLE_SERVER, \ | |
71 | VARLINK_PROCESSING_METHOD, \ | |
72 | VARLINK_PROCESSING_METHOD_MORE, \ | |
73 | VARLINK_PROCESSING_METHOD_ONEWAY, \ | |
74 | VARLINK_PROCESSED_METHOD, \ | |
d41bd96f LP |
75 | VARLINK_PENDING_METHOD, \ |
76 | VARLINK_PENDING_METHOD_MORE) | |
77 | ||
78 | struct Varlink { | |
79 | unsigned n_ref; | |
80 | ||
81 | VarlinkServer *server; | |
82 | ||
83 | VarlinkState state; | |
84 | bool connecting; /* This boolean indicates whether the socket fd we are operating on is currently | |
85 | * processing an asynchronous connect(). In that state we watch the socket for | |
86 | * EPOLLOUT, but we refrain from calling read() or write() on the socket as that | |
87 | * will trigger ENOTCONN. Note that this boolean is kept separate from the | |
88 | * VarlinkState above on purpose: while the connect() is still not complete we | |
89 | * already want to allow queuing of messages and similar. Thus it's nice to keep | |
90 | * these two state concepts separate: the VarlinkState encodes what our own view of | |
91 | * the connection is, i.e. whether we think it's a server, a client, and has | |
92 | * something queued already, while 'connecting' tells us a detail about the | |
93 | * transport used below, that should have no effect on how we otherwise accept and | |
94 | * process operations from the user. | |
95 | * | |
96 | * Or to say this differently: VARLINK_STATE_IS_ALIVE(state) tells you whether the | |
97 | * connection is good to use, even if it might not be fully connected | |
98 | * yet. connecting=true then informs you that actually we are still connecting, and | |
99 | * the connection is actually not established yet and thus any requests you enqueue | |
100 | * now will still work fine but will be queued only, not sent yet, but that | |
101 | * shouldn't stop you from using the connection, since eventually whatever you queue | |
102 | * *will* be sent. | |
103 | * | |
104 | * Or to say this even differently: 'state' is a high-level ("application layer" | |
105 | * high, if you so will) state, while 'conecting' is a low-level ("transport layer" | |
106 | * low, if you so will) state, and while they are not entirely unrelated and | |
107 | * sometimes propagate effects to each other they are only asynchronously connected | |
108 | * at most. */ | |
109 | unsigned n_pending; | |
110 | ||
111 | int fd; | |
112 | ||
113 | char *input_buffer; /* valid data starts at input_buffer_index, ends at input_buffer_index+input_buffer_size */ | |
114 | size_t input_buffer_allocated; | |
115 | size_t input_buffer_index; | |
116 | size_t input_buffer_size; | |
117 | size_t input_buffer_unscanned; | |
118 | ||
119 | char *output_buffer; /* valid data starts at output_buffer_index, ends at output_buffer_index+output_buffer_size */ | |
120 | size_t output_buffer_allocated; | |
121 | size_t output_buffer_index; | |
122 | size_t output_buffer_size; | |
123 | ||
124 | VarlinkReply reply_callback; | |
125 | ||
126 | JsonVariant *current; | |
127 | JsonVariant *reply; | |
128 | ||
129 | struct ucred ucred; | |
130 | bool ucred_acquired:1; | |
131 | ||
132 | bool write_disconnected:1; | |
133 | bool read_disconnected:1; | |
134 | bool prefer_read_write:1; | |
135 | bool got_pollhup:1; | |
136 | ||
137 | usec_t timestamp; | |
138 | usec_t timeout; | |
139 | ||
140 | void *userdata; | |
141 | char *description; | |
142 | ||
143 | sd_event *event; | |
144 | sd_event_source *io_event_source; | |
145 | sd_event_source *time_event_source; | |
146 | sd_event_source *quit_event_source; | |
147 | sd_event_source *defer_event_source; | |
148 | }; | |
149 | ||
150 | typedef struct VarlinkServerSocket VarlinkServerSocket; | |
151 | ||
152 | struct VarlinkServerSocket { | |
153 | VarlinkServer *server; | |
154 | ||
155 | int fd; | |
156 | char *address; | |
157 | ||
158 | sd_event_source *event_source; | |
159 | ||
160 | LIST_FIELDS(VarlinkServerSocket, sockets); | |
161 | }; | |
162 | ||
163 | struct VarlinkServer { | |
164 | unsigned n_ref; | |
165 | VarlinkServerFlags flags; | |
166 | ||
167 | LIST_HEAD(VarlinkServerSocket, sockets); | |
168 | ||
169 | Hashmap *methods; | |
170 | VarlinkConnect connect_callback; | |
171 | ||
172 | sd_event *event; | |
173 | int64_t event_priority; | |
174 | ||
175 | unsigned n_connections; | |
176 | Hashmap *by_uid; | |
177 | ||
178 | void *userdata; | |
179 | char *description; | |
180 | ||
181 | unsigned connections_max; | |
182 | unsigned connections_per_uid_max; | |
183 | }; | |
184 | ||
185 | static const char* const varlink_state_table[_VARLINK_STATE_MAX] = { | |
186 | [VARLINK_IDLE_CLIENT] = "idle-client", | |
187 | [VARLINK_AWAITING_REPLY] = "awaiting-reply", | |
45a6c965 | 188 | [VARLINK_AWAITING_REPLY_MORE] = "awaiting-reply-more", |
d41bd96f LP |
189 | [VARLINK_CALLING] = "calling", |
190 | [VARLINK_CALLED] = "called", | |
191 | [VARLINK_PROCESSING_REPLY] = "processing-reply", | |
192 | [VARLINK_IDLE_SERVER] = "idle-server", | |
193 | [VARLINK_PROCESSING_METHOD] = "processing-method", | |
194 | [VARLINK_PROCESSING_METHOD_MORE] = "processing-method-more", | |
195 | [VARLINK_PROCESSING_METHOD_ONEWAY] = "processing-method-oneway", | |
196 | [VARLINK_PROCESSED_METHOD] = "processed-method", | |
d41bd96f LP |
197 | [VARLINK_PENDING_METHOD] = "pending-method", |
198 | [VARLINK_PENDING_METHOD_MORE] = "pending-method-more", | |
199 | [VARLINK_PENDING_DISCONNECT] = "pending-disconnect", | |
200 | [VARLINK_PENDING_TIMEOUT] = "pending-timeout", | |
201 | [VARLINK_PROCESSING_DISCONNECT] = "processing-disconnect", | |
202 | [VARLINK_PROCESSING_TIMEOUT] = "processing-timeout", | |
203 | [VARLINK_PROCESSING_FAILURE] = "processing-failure", | |
204 | [VARLINK_DISCONNECTED] = "disconnected", | |
205 | }; | |
206 | ||
207 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(varlink_state, VarlinkState); | |
208 | ||
209 | #define varlink_log_errno(v, error, fmt, ...) \ | |
210 | log_debug_errno(error, "%s: " fmt, varlink_description(v), ##__VA_ARGS__) | |
211 | ||
212 | #define varlink_log(v, fmt, ...) \ | |
213 | log_debug("%s: " fmt, varlink_description(v), ##__VA_ARGS__) | |
214 | ||
215 | #define varlink_server_log_errno(s, error, fmt, ...) \ | |
216 | log_debug_errno(error, "%s: " fmt, varlink_server_description(s), ##__VA_ARGS__) | |
217 | ||
218 | #define varlink_server_log(s, fmt, ...) \ | |
219 | log_debug("%s: " fmt, varlink_server_description(s), ##__VA_ARGS__) | |
220 | ||
221 | static inline const char *varlink_description(Varlink *v) { | |
222 | return strna(v ? v->description : NULL); | |
223 | } | |
224 | ||
225 | static inline const char *varlink_server_description(VarlinkServer *s) { | |
226 | return strna(s ? s->description : NULL); | |
227 | } | |
228 | ||
229 | static void varlink_set_state(Varlink *v, VarlinkState state) { | |
230 | assert(v); | |
77740b59 ZJS |
231 | assert(state >= 0 && state < _VARLINK_STATE_MAX); |
232 | ||
233 | if (v->state < 0) | |
234 | varlink_log(v, "varlink: setting state %s", | |
235 | varlink_state_to_string(state)); | |
236 | else | |
237 | varlink_log(v, "varlink: changing state %s → %s", | |
238 | varlink_state_to_string(v->state), | |
239 | varlink_state_to_string(state)); | |
d41bd96f LP |
240 | |
241 | v->state = state; | |
242 | } | |
243 | ||
244 | static int varlink_new(Varlink **ret) { | |
245 | Varlink *v; | |
246 | ||
247 | assert(ret); | |
248 | ||
a48481dc | 249 | v = new(Varlink, 1); |
d41bd96f LP |
250 | if (!v) |
251 | return -ENOMEM; | |
252 | ||
253 | *v = (Varlink) { | |
254 | .n_ref = 1, | |
255 | .fd = -1, | |
256 | ||
257 | .state = _VARLINK_STATE_INVALID, | |
258 | ||
259 | .ucred.uid = UID_INVALID, | |
260 | .ucred.gid = GID_INVALID, | |
261 | ||
262 | .timestamp = USEC_INFINITY, | |
263 | .timeout = VARLINK_DEFAULT_TIMEOUT_USEC | |
264 | }; | |
265 | ||
266 | *ret = v; | |
267 | return 0; | |
268 | } | |
269 | ||
270 | int varlink_connect_address(Varlink **ret, const char *address) { | |
271 | _cleanup_(varlink_unrefp) Varlink *v = NULL; | |
272 | union sockaddr_union sockaddr; | |
273 | int r; | |
274 | ||
275 | assert_return(ret, -EINVAL); | |
276 | assert_return(address, -EINVAL); | |
277 | ||
278 | r = sockaddr_un_set_path(&sockaddr.un, address); | |
279 | if (r < 0) | |
280 | return r; | |
281 | ||
282 | r = varlink_new(&v); | |
283 | if (r < 0) | |
284 | return r; | |
285 | ||
286 | v->fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
287 | if (v->fd < 0) | |
288 | return -errno; | |
289 | ||
290 | if (connect(v->fd, &sockaddr.sa, SOCKADDR_UN_LEN(sockaddr.un)) < 0) { | |
291 | if (!IN_SET(errno, EAGAIN, EINPROGRESS)) | |
292 | return -errno; | |
293 | ||
294 | v->connecting = true; /* We are asynchronously connecting, i.e. the connect() is being | |
295 | * processed in the background. As long as that's the case the socket | |
296 | * is in a special state: it's there, we can poll it for EPOLLOUT, but | |
297 | * if we attempt to write() to it before we see EPOLLOUT we'll get | |
298 | * ENOTCONN (and not EAGAIN, like we would for a normal connected | |
299 | * socket that isn't writable at the moment). Since ENOTCONN on write() | |
300 | * hence can mean two different things (i.e. connection not complete | |
301 | * yet vs. already disconnected again), we store as a boolean whether | |
302 | * we are still in connect(). */ | |
303 | } | |
304 | ||
305 | varlink_set_state(v, VARLINK_IDLE_CLIENT); | |
306 | ||
307 | *ret = TAKE_PTR(v); | |
308 | return r; | |
309 | } | |
310 | ||
311 | int varlink_connect_fd(Varlink **ret, int fd) { | |
312 | Varlink *v; | |
313 | int r; | |
314 | ||
315 | assert_return(ret, -EINVAL); | |
316 | assert_return(fd >= 0, -EBADF); | |
317 | ||
318 | r = fd_nonblock(fd, true); | |
319 | if (r < 0) | |
320 | return r; | |
321 | ||
322 | r = varlink_new(&v); | |
323 | if (r < 0) | |
324 | return r; | |
325 | ||
326 | v->fd = fd; | |
327 | varlink_set_state(v, VARLINK_IDLE_CLIENT); | |
328 | ||
329 | /* Note that if this function is called we assume the passed socket (if it is one) is already | |
330 | * properly connected, i.e. any asynchronous connect() done on it already completed. Because of that | |
331 | * we'll not set the 'connecting' boolean here, i.e. we don't need to avoid write()ing to the socket | |
332 | * until the connection is fully set up. Behaviour here is hence a bit different from | |
333 | * varlink_connect_address() above, as there we do handle asynchronous connections ourselves and | |
334 | * avoid doing write() on it before we saw EPOLLOUT for the first time. */ | |
335 | ||
336 | *ret = v; | |
337 | return 0; | |
338 | } | |
339 | ||
340 | static void varlink_detach_event_sources(Varlink *v) { | |
341 | assert(v); | |
342 | ||
1d3fe304 | 343 | v->io_event_source = sd_event_source_disable_unref(v->io_event_source); |
d41bd96f | 344 | |
1d3fe304 | 345 | v->time_event_source = sd_event_source_disable_unref(v->time_event_source); |
d41bd96f | 346 | |
1d3fe304 | 347 | v->quit_event_source = sd_event_source_disable_unref(v->quit_event_source); |
d41bd96f | 348 | |
1d3fe304 | 349 | v->defer_event_source = sd_event_source_disable_unref(v->defer_event_source); |
d41bd96f LP |
350 | } |
351 | ||
352 | static void varlink_clear(Varlink *v) { | |
353 | assert(v); | |
354 | ||
355 | varlink_detach_event_sources(v); | |
356 | ||
357 | v->fd = safe_close(v->fd); | |
358 | ||
359 | v->input_buffer = mfree(v->input_buffer); | |
360 | v->output_buffer = mfree(v->output_buffer); | |
361 | ||
362 | v->current = json_variant_unref(v->current); | |
363 | v->reply = json_variant_unref(v->reply); | |
364 | ||
365 | v->event = sd_event_unref(v->event); | |
366 | } | |
367 | ||
368 | static Varlink* varlink_destroy(Varlink *v) { | |
369 | if (!v) | |
370 | return NULL; | |
371 | ||
372 | /* If this is called the server object must already been unreffed here. Why that? because when we | |
373 | * linked up the varlink connection with the server object we took one ref in each direction */ | |
374 | assert(!v->server); | |
375 | ||
376 | varlink_clear(v); | |
377 | ||
378 | free(v->description); | |
379 | return mfree(v); | |
380 | } | |
381 | ||
382 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Varlink, varlink, varlink_destroy); | |
383 | ||
384 | static int varlink_test_disconnect(Varlink *v) { | |
385 | assert(v); | |
386 | ||
387 | /* Tests whether we the the connection has been terminated. We are careful to not stop processing it | |
388 | * prematurely, since we want to handle half-open connections as well as possible and want to flush | |
389 | * out and read data before we close down if we can. */ | |
390 | ||
391 | /* Already disconnected? */ | |
392 | if (!VARLINK_STATE_IS_ALIVE(v->state)) | |
393 | return 0; | |
394 | ||
395 | /* Wait until connection setup is complete, i.e. until asynchronous connect() completes */ | |
396 | if (v->connecting) | |
397 | return 0; | |
398 | ||
399 | /* Still something to write and we can write? Stay around */ | |
400 | if (v->output_buffer_size > 0 && !v->write_disconnected) | |
401 | return 0; | |
402 | ||
403 | /* Both sides gone already? Then there's no need to stick around */ | |
404 | if (v->read_disconnected && v->write_disconnected) | |
405 | goto disconnect; | |
406 | ||
407 | /* If we are waiting for incoming data but the read side is shut down, disconnect. */ | |
45a6c965 | 408 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING, VARLINK_IDLE_SERVER) && v->read_disconnected) |
d41bd96f LP |
409 | goto disconnect; |
410 | ||
411 | /* Similar, if are a client that hasn't written anything yet but the write side is dead, also | |
412 | * disconnect. We also explicitly check for POLLHUP here since we likely won't notice the write side | |
413 | * being down if we never wrote anything. */ | |
414 | if (IN_SET(v->state, VARLINK_IDLE_CLIENT) && (v->write_disconnected || v->got_pollhup)) | |
415 | goto disconnect; | |
416 | ||
417 | return 0; | |
418 | ||
419 | disconnect: | |
420 | varlink_set_state(v, VARLINK_PENDING_DISCONNECT); | |
421 | return 1; | |
422 | } | |
423 | ||
424 | static int varlink_write(Varlink *v) { | |
425 | ssize_t n; | |
426 | ||
427 | assert(v); | |
428 | ||
429 | if (!VARLINK_STATE_IS_ALIVE(v->state)) | |
430 | return 0; | |
431 | if (v->connecting) /* Writing while we are still wait for a non-blocking connect() to complete will | |
432 | * result in ENOTCONN, hence exit early here */ | |
433 | return 0; | |
434 | if (v->output_buffer_size == 0) | |
435 | return 0; | |
436 | if (v->write_disconnected) | |
437 | return 0; | |
438 | ||
439 | assert(v->fd >= 0); | |
440 | ||
441 | /* We generally prefer recv()/send() (mostly because of MSG_NOSIGNAL) but also want to be compatible | |
442 | * with non-socket IO, hence fall back automatically */ | |
443 | if (!v->prefer_read_write) { | |
444 | n = send(v->fd, v->output_buffer + v->output_buffer_index, v->output_buffer_size, MSG_DONTWAIT|MSG_NOSIGNAL); | |
445 | if (n < 0 && errno == ENOTSOCK) | |
446 | v->prefer_read_write = true; | |
447 | } | |
448 | if (v->prefer_read_write) | |
449 | n = write(v->fd, v->output_buffer + v->output_buffer_index, v->output_buffer_size); | |
450 | if (n < 0) { | |
451 | if (errno == EAGAIN) | |
452 | return 0; | |
453 | ||
454 | if (ERRNO_IS_DISCONNECT(errno)) { | |
455 | /* If we get informed about a disconnect on write, then let's remember that, but not | |
456 | * act on it just yet. Let's wait for read() to report the issue first. */ | |
457 | v->write_disconnected = true; | |
458 | return 1; | |
459 | } | |
460 | ||
461 | return -errno; | |
462 | } | |
463 | ||
464 | v->output_buffer_size -= n; | |
465 | ||
466 | if (v->output_buffer_size == 0) | |
467 | v->output_buffer_index = 0; | |
468 | else | |
469 | v->output_buffer_index += n; | |
470 | ||
471 | v->timestamp = now(CLOCK_MONOTONIC); | |
472 | return 1; | |
473 | } | |
474 | ||
475 | static int varlink_read(Varlink *v) { | |
476 | size_t rs; | |
477 | ssize_t n; | |
478 | ||
479 | assert(v); | |
480 | ||
45a6c965 | 481 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING, VARLINK_IDLE_SERVER)) |
d41bd96f LP |
482 | return 0; |
483 | if (v->connecting) /* read() on a socket while we are in connect() will fail with EINVAL, hence exit early here */ | |
484 | return 0; | |
485 | if (v->current) | |
486 | return 0; | |
487 | if (v->input_buffer_unscanned > 0) | |
488 | return 0; | |
489 | if (v->read_disconnected) | |
490 | return 0; | |
491 | ||
492 | if (v->input_buffer_size >= VARLINK_BUFFER_MAX) | |
493 | return -ENOBUFS; | |
494 | ||
495 | assert(v->fd >= 0); | |
496 | ||
497 | if (v->input_buffer_allocated <= v->input_buffer_index + v->input_buffer_size) { | |
498 | size_t add; | |
499 | ||
500 | add = MIN(VARLINK_BUFFER_MAX - v->input_buffer_size, VARLINK_READ_SIZE); | |
501 | ||
502 | if (v->input_buffer_index == 0) { | |
503 | ||
504 | if (!GREEDY_REALLOC(v->input_buffer, v->input_buffer_allocated, v->input_buffer_size + add)) | |
505 | return -ENOMEM; | |
506 | ||
507 | } else { | |
508 | char *b; | |
509 | ||
510 | b = new(char, v->input_buffer_size + add); | |
511 | if (!b) | |
512 | return -ENOMEM; | |
513 | ||
514 | memcpy(b, v->input_buffer + v->input_buffer_index, v->input_buffer_size); | |
515 | ||
516 | free_and_replace(v->input_buffer, b); | |
517 | ||
518 | v->input_buffer_allocated = v->input_buffer_size + add; | |
519 | v->input_buffer_index = 0; | |
520 | } | |
521 | } | |
522 | ||
523 | rs = v->input_buffer_allocated - (v->input_buffer_index + v->input_buffer_size); | |
524 | ||
525 | if (!v->prefer_read_write) { | |
526 | n = recv(v->fd, v->input_buffer + v->input_buffer_index + v->input_buffer_size, rs, MSG_DONTWAIT); | |
527 | if (n < 0 && errno == ENOTSOCK) | |
528 | v->prefer_read_write = true; | |
529 | } | |
530 | if (v->prefer_read_write) | |
531 | n = read(v->fd, v->input_buffer + v->input_buffer_index + v->input_buffer_size, rs); | |
532 | if (n < 0) { | |
533 | if (errno == EAGAIN) | |
534 | return 0; | |
535 | ||
536 | if (ERRNO_IS_DISCONNECT(errno)) { | |
537 | v->read_disconnected = true; | |
538 | return 1; | |
539 | } | |
540 | ||
541 | return -errno; | |
542 | } | |
543 | if (n == 0) { /* EOF */ | |
544 | v->read_disconnected = true; | |
545 | return 1; | |
546 | } | |
547 | ||
548 | v->input_buffer_size += n; | |
549 | v->input_buffer_unscanned += n; | |
550 | ||
551 | return 1; | |
552 | } | |
553 | ||
554 | static int varlink_parse_message(Varlink *v) { | |
555 | const char *e, *begin; | |
556 | size_t sz; | |
557 | int r; | |
558 | ||
559 | assert(v); | |
560 | ||
561 | if (v->current) | |
562 | return 0; | |
563 | if (v->input_buffer_unscanned <= 0) | |
564 | return 0; | |
565 | ||
566 | assert(v->input_buffer_unscanned <= v->input_buffer_size); | |
567 | assert(v->input_buffer_index + v->input_buffer_size <= v->input_buffer_allocated); | |
568 | ||
569 | begin = v->input_buffer + v->input_buffer_index; | |
570 | ||
571 | e = memchr(begin + v->input_buffer_size - v->input_buffer_unscanned, 0, v->input_buffer_unscanned); | |
572 | if (!e) { | |
573 | v->input_buffer_unscanned = 0; | |
574 | return 0; | |
575 | } | |
576 | ||
577 | sz = e - begin + 1; | |
578 | ||
579 | varlink_log(v, "New incoming message: %s", begin); | |
580 | ||
581 | r = json_parse(begin, &v->current, NULL, NULL); | |
582 | if (r < 0) | |
583 | return r; | |
584 | ||
585 | v->input_buffer_size -= sz; | |
586 | ||
587 | if (v->input_buffer_size == 0) | |
588 | v->input_buffer_index = 0; | |
589 | else | |
590 | v->input_buffer_index += sz; | |
591 | ||
592 | v->input_buffer_unscanned = v->input_buffer_size; | |
593 | return 1; | |
594 | } | |
595 | ||
596 | static int varlink_test_timeout(Varlink *v) { | |
597 | assert(v); | |
598 | ||
45a6c965 | 599 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING)) |
d41bd96f LP |
600 | return 0; |
601 | if (v->timeout == USEC_INFINITY) | |
602 | return 0; | |
603 | ||
604 | if (now(CLOCK_MONOTONIC) < usec_add(v->timestamp, v->timeout)) | |
605 | return 0; | |
606 | ||
607 | varlink_set_state(v, VARLINK_PENDING_TIMEOUT); | |
608 | ||
609 | return 1; | |
610 | } | |
611 | ||
612 | static int varlink_dispatch_local_error(Varlink *v, const char *error) { | |
613 | int r; | |
614 | ||
615 | assert(v); | |
616 | assert(error); | |
617 | ||
618 | if (!v->reply_callback) | |
619 | return 0; | |
620 | ||
621 | r = v->reply_callback(v, NULL, error, VARLINK_REPLY_ERROR|VARLINK_REPLY_LOCAL, v->userdata); | |
622 | if (r < 0) | |
623 | log_debug_errno(r, "Reply callback returned error, ignoring: %m"); | |
624 | ||
625 | return 1; | |
626 | } | |
627 | ||
628 | static int varlink_dispatch_timeout(Varlink *v) { | |
629 | assert(v); | |
630 | ||
631 | if (v->state != VARLINK_PENDING_TIMEOUT) | |
632 | return 0; | |
633 | ||
634 | varlink_set_state(v, VARLINK_PROCESSING_TIMEOUT); | |
635 | varlink_dispatch_local_error(v, VARLINK_ERROR_TIMEOUT); | |
636 | varlink_close(v); | |
637 | ||
638 | return 1; | |
639 | } | |
640 | ||
641 | static int varlink_dispatch_disconnect(Varlink *v) { | |
642 | assert(v); | |
643 | ||
644 | if (v->state != VARLINK_PENDING_DISCONNECT) | |
645 | return 0; | |
646 | ||
647 | varlink_set_state(v, VARLINK_PROCESSING_DISCONNECT); | |
648 | varlink_dispatch_local_error(v, VARLINK_ERROR_DISCONNECTED); | |
649 | varlink_close(v); | |
650 | ||
651 | return 1; | |
652 | } | |
653 | ||
654 | static int varlink_sanitize_parameters(JsonVariant **v) { | |
655 | assert(v); | |
656 | ||
657 | /* Varlink always wants a parameters list, hence make one if the caller doesn't want any */ | |
658 | if (!*v) | |
659 | return json_variant_new_object(v, NULL, 0); | |
660 | else if (!json_variant_is_object(*v)) | |
661 | return -EINVAL; | |
662 | ||
663 | return 0; | |
664 | } | |
665 | ||
666 | static int varlink_dispatch_reply(Varlink *v) { | |
667 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
668 | VarlinkReplyFlags flags = 0; | |
669 | const char *error = NULL; | |
670 | JsonVariant *e; | |
671 | const char *k; | |
672 | int r; | |
673 | ||
674 | assert(v); | |
675 | ||
45a6c965 | 676 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING)) |
d41bd96f LP |
677 | return 0; |
678 | if (!v->current) | |
679 | return 0; | |
680 | ||
681 | assert(v->n_pending > 0); | |
682 | ||
683 | if (!json_variant_is_object(v->current)) | |
684 | goto invalid; | |
685 | ||
686 | JSON_VARIANT_OBJECT_FOREACH(k, e, v->current) { | |
687 | ||
688 | if (streq(k, "error")) { | |
689 | if (error) | |
690 | goto invalid; | |
691 | if (!json_variant_is_string(e)) | |
692 | goto invalid; | |
693 | ||
694 | error = json_variant_string(e); | |
695 | flags |= VARLINK_REPLY_ERROR; | |
696 | ||
697 | } else if (streq(k, "parameters")) { | |
698 | if (parameters) | |
699 | goto invalid; | |
700 | if (!json_variant_is_object(e)) | |
701 | goto invalid; | |
702 | ||
703 | parameters = json_variant_ref(e); | |
704 | ||
705 | } else if (streq(k, "continues")) { | |
706 | if (FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) | |
707 | goto invalid; | |
708 | ||
709 | if (!json_variant_is_boolean(e)) | |
710 | goto invalid; | |
711 | ||
712 | if (json_variant_boolean(e)) | |
713 | flags |= VARLINK_REPLY_CONTINUES; | |
714 | } else | |
715 | goto invalid; | |
716 | } | |
717 | ||
45a6c965 LP |
718 | /* Replies with 'continue' set are only OK if we set 'more' when the method call was initiated */ |
719 | if (v->state != VARLINK_AWAITING_REPLY_MORE && FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) | |
720 | goto invalid; | |
721 | ||
722 | /* An error is final */ | |
d41bd96f LP |
723 | if (error && FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) |
724 | goto invalid; | |
725 | ||
726 | r = varlink_sanitize_parameters(¶meters); | |
727 | if (r < 0) | |
728 | goto invalid; | |
729 | ||
45a6c965 | 730 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE)) { |
d41bd96f LP |
731 | varlink_set_state(v, VARLINK_PROCESSING_REPLY); |
732 | ||
733 | if (v->reply_callback) { | |
734 | r = v->reply_callback(v, parameters, error, flags, v->userdata); | |
735 | if (r < 0) | |
736 | log_debug_errno(r, "Reply callback returned error, ignoring: %m"); | |
737 | } | |
738 | ||
739 | v->current = json_variant_unref(v->current); | |
740 | ||
741 | if (v->state == VARLINK_PROCESSING_REPLY) { | |
45a6c965 | 742 | |
d41bd96f | 743 | assert(v->n_pending > 0); |
d41bd96f | 744 | |
45a6c965 LP |
745 | if (!FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) |
746 | v->n_pending--; | |
747 | ||
748 | varlink_set_state(v, | |
749 | FLAGS_SET(flags, VARLINK_REPLY_CONTINUES) ? VARLINK_AWAITING_REPLY_MORE : | |
750 | v->n_pending == 0 ? VARLINK_IDLE_CLIENT : VARLINK_AWAITING_REPLY); | |
d41bd96f LP |
751 | } |
752 | } else { | |
753 | assert(v->state == VARLINK_CALLING); | |
d41bd96f LP |
754 | varlink_set_state(v, VARLINK_CALLED); |
755 | } | |
756 | ||
757 | return 1; | |
758 | ||
759 | invalid: | |
760 | varlink_set_state(v, VARLINK_PROCESSING_FAILURE); | |
761 | varlink_dispatch_local_error(v, VARLINK_ERROR_PROTOCOL); | |
762 | varlink_close(v); | |
763 | ||
764 | return 1; | |
765 | } | |
766 | ||
767 | static int varlink_dispatch_method(Varlink *v) { | |
768 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
769 | VarlinkMethodFlags flags = 0; | |
770 | const char *method = NULL, *error; | |
771 | JsonVariant *e; | |
772 | VarlinkMethod callback; | |
773 | const char *k; | |
774 | int r; | |
775 | ||
776 | assert(v); | |
777 | ||
778 | if (v->state != VARLINK_IDLE_SERVER) | |
779 | return 0; | |
780 | if (!v->current) | |
781 | return 0; | |
782 | ||
783 | if (!json_variant_is_object(v->current)) | |
784 | goto invalid; | |
785 | ||
786 | JSON_VARIANT_OBJECT_FOREACH(k, e, v->current) { | |
787 | ||
788 | if (streq(k, "method")) { | |
789 | if (method) | |
790 | goto invalid; | |
791 | if (!json_variant_is_string(e)) | |
792 | goto invalid; | |
793 | ||
794 | method = json_variant_string(e); | |
795 | ||
796 | } else if (streq(k, "parameters")) { | |
797 | if (parameters) | |
798 | goto invalid; | |
799 | if (!json_variant_is_object(e)) | |
800 | goto invalid; | |
801 | ||
802 | parameters = json_variant_ref(e); | |
803 | ||
804 | } else if (streq(k, "oneway")) { | |
805 | ||
806 | if ((flags & (VARLINK_METHOD_ONEWAY|VARLINK_METHOD_MORE)) != 0) | |
807 | goto invalid; | |
808 | ||
809 | if (!json_variant_is_boolean(e)) | |
810 | goto invalid; | |
811 | ||
812 | if (json_variant_boolean(e)) | |
813 | flags |= VARLINK_METHOD_ONEWAY; | |
814 | ||
815 | } else if (streq(k, "more")) { | |
816 | ||
817 | if ((flags & (VARLINK_METHOD_ONEWAY|VARLINK_METHOD_MORE)) != 0) | |
818 | goto invalid; | |
819 | ||
820 | if (!json_variant_is_boolean(e)) | |
821 | goto invalid; | |
822 | ||
823 | if (json_variant_boolean(e)) | |
824 | flags |= VARLINK_METHOD_MORE; | |
825 | ||
826 | } else | |
827 | goto invalid; | |
828 | } | |
829 | ||
830 | if (!method) | |
831 | goto invalid; | |
832 | ||
833 | r = varlink_sanitize_parameters(¶meters); | |
834 | if (r < 0) | |
835 | goto fail; | |
836 | ||
837 | varlink_set_state(v, (flags & VARLINK_METHOD_MORE) ? VARLINK_PROCESSING_METHOD_MORE : | |
838 | (flags & VARLINK_METHOD_ONEWAY) ? VARLINK_PROCESSING_METHOD_ONEWAY : | |
839 | VARLINK_PROCESSING_METHOD); | |
840 | ||
841 | assert(v->server); | |
842 | ||
843 | if (STR_IN_SET(method, "org.varlink.service.GetInfo", "org.varlink.service.GetInterface")) { | |
844 | /* For now, we don't implement a single of varlink's own methods */ | |
845 | callback = NULL; | |
846 | error = VARLINK_ERROR_METHOD_NOT_IMPLEMENTED; | |
847 | } else if (startswith(method, "org.varlink.service.")) { | |
848 | callback = NULL; | |
849 | error = VARLINK_ERROR_METHOD_NOT_FOUND; | |
850 | } else { | |
851 | callback = hashmap_get(v->server->methods, method); | |
852 | error = VARLINK_ERROR_METHOD_NOT_FOUND; | |
853 | } | |
854 | ||
855 | if (callback) { | |
856 | r = callback(v, parameters, flags, v->userdata); | |
857 | if (r < 0) { | |
858 | log_debug_errno(r, "Callback for %s returned error: %m", method); | |
859 | ||
860 | /* We got an error back from the callback. Propagate it to the client if the method call remains unanswered. */ | |
861 | if (!FLAGS_SET(flags, VARLINK_METHOD_ONEWAY)) { | |
862 | r = varlink_errorb(v, VARLINK_ERROR_SYSTEM, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("errno", JSON_BUILD_INTEGER(-r)))); | |
863 | if (r < 0) | |
864 | return r; | |
865 | } | |
866 | } | |
867 | } else if (!FLAGS_SET(flags, VARLINK_METHOD_ONEWAY)) { | |
868 | assert(error); | |
869 | ||
870 | r = varlink_errorb(v, error, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)))); | |
871 | if (r < 0) | |
872 | return r; | |
873 | } | |
874 | ||
875 | switch (v->state) { | |
876 | ||
877 | case VARLINK_PROCESSED_METHOD: /* Method call is fully processed */ | |
878 | case VARLINK_PROCESSING_METHOD_ONEWAY: /* ditto */ | |
879 | v->current = json_variant_unref(v->current); | |
880 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
881 | break; | |
882 | ||
883 | case VARLINK_PROCESSING_METHOD: /* Method call wasn't replied to, will be replied to later */ | |
884 | varlink_set_state(v, VARLINK_PENDING_METHOD); | |
885 | break; | |
886 | ||
d41bd96f LP |
887 | case VARLINK_PROCESSING_METHOD_MORE: /* No reply for a "more" message was sent, more to come */ |
888 | varlink_set_state(v, VARLINK_PENDING_METHOD_MORE); | |
889 | break; | |
890 | ||
891 | default: | |
892 | assert_not_reached("Unexpected state"); | |
893 | ||
894 | } | |
895 | ||
896 | return r; | |
897 | ||
898 | invalid: | |
899 | r = -EINVAL; | |
900 | ||
901 | fail: | |
902 | varlink_set_state(v, VARLINK_PROCESSING_FAILURE); | |
903 | varlink_dispatch_local_error(v, VARLINK_ERROR_PROTOCOL); | |
904 | varlink_close(v); | |
905 | ||
906 | return r; | |
907 | } | |
908 | ||
909 | int varlink_process(Varlink *v) { | |
910 | int r; | |
911 | ||
912 | assert_return(v, -EINVAL); | |
913 | ||
914 | if (v->state == VARLINK_DISCONNECTED) | |
915 | return -ENOTCONN; | |
916 | ||
917 | varlink_ref(v); | |
918 | ||
919 | r = varlink_write(v); | |
920 | if (r != 0) | |
921 | goto finish; | |
922 | ||
923 | r = varlink_dispatch_reply(v); | |
924 | if (r != 0) | |
925 | goto finish; | |
926 | ||
927 | r = varlink_dispatch_method(v); | |
928 | if (r != 0) | |
929 | goto finish; | |
930 | ||
931 | r = varlink_parse_message(v); | |
932 | if (r != 0) | |
933 | goto finish; | |
934 | ||
935 | r = varlink_read(v); | |
936 | if (r != 0) | |
937 | goto finish; | |
938 | ||
939 | r = varlink_test_disconnect(v); | |
940 | if (r != 0) | |
941 | goto finish; | |
942 | ||
943 | r = varlink_dispatch_disconnect(v); | |
944 | if (r != 0) | |
945 | goto finish; | |
946 | ||
947 | r = varlink_test_timeout(v); | |
948 | if (r != 0) | |
949 | goto finish; | |
950 | ||
951 | r = varlink_dispatch_timeout(v); | |
952 | if (r != 0) | |
953 | goto finish; | |
954 | ||
955 | finish: | |
956 | if (r >= 0 && v->defer_event_source) { | |
957 | int q; | |
958 | ||
959 | /* If we did some processing, make sure we are called again soon */ | |
960 | q = sd_event_source_set_enabled(v->defer_event_source, r > 0 ? SD_EVENT_ON : SD_EVENT_OFF); | |
961 | if (q < 0) | |
962 | r = q; | |
963 | } | |
964 | ||
965 | if (r < 0) { | |
966 | if (VARLINK_STATE_IS_ALIVE(v->state)) | |
967 | /* Initiate disconnection */ | |
968 | varlink_set_state(v, VARLINK_PENDING_DISCONNECT); | |
969 | else | |
970 | /* We failed while disconnecting, in that case close right away */ | |
971 | varlink_close(v); | |
972 | } | |
973 | ||
974 | varlink_unref(v); | |
975 | return r; | |
976 | } | |
977 | ||
978 | static void handle_revents(Varlink *v, int revents) { | |
979 | assert(v); | |
980 | ||
981 | if (v->connecting) { | |
982 | /* If we have seen POLLOUT or POLLHUP on a socket we are asynchronously waiting a connect() | |
983 | * to complete on, we know we are ready. We don't read the connection error here though, | |
984 | * we'll get the error on the next read() or write(). */ | |
985 | if ((revents & (POLLOUT|POLLHUP)) == 0) | |
986 | return; | |
987 | ||
988 | varlink_log(v, "Anynchronous connection completed."); | |
989 | v->connecting = false; | |
990 | } else { | |
991 | /* Note that we don't care much about POLLIN/POLLOUT here, we'll just try reading and writing | |
992 | * what we can. However, we do care about POLLHUP to detect connection termination even if we | |
993 | * momentarily don't want to read nor write anything. */ | |
994 | ||
995 | if (!FLAGS_SET(revents, POLLHUP)) | |
996 | return; | |
997 | ||
998 | varlink_log(v, "Got POLLHUP from socket."); | |
999 | v->got_pollhup = true; | |
1000 | } | |
1001 | } | |
1002 | ||
1003 | int varlink_wait(Varlink *v, usec_t timeout) { | |
1004 | struct timespec ts; | |
1005 | struct pollfd pfd; | |
1006 | int r, fd, events; | |
1007 | usec_t t; | |
1008 | ||
1009 | assert_return(v, -EINVAL); | |
d41bd96f LP |
1010 | |
1011 | if (v->state == VARLINK_DISCONNECTED) | |
1012 | return -ENOTCONN; | |
1013 | ||
1014 | r = varlink_get_timeout(v, &t); | |
1015 | if (r < 0) | |
1016 | return r; | |
1017 | if (t != USEC_INFINITY) { | |
1018 | usec_t n; | |
1019 | ||
1020 | n = now(CLOCK_MONOTONIC); | |
1021 | if (t < n) | |
1022 | t = 0; | |
1023 | else | |
1024 | t = usec_sub_unsigned(t, n); | |
1025 | } | |
1026 | ||
1027 | if (timeout != USEC_INFINITY && | |
1028 | (t == USEC_INFINITY || timeout < t)) | |
1029 | t = timeout; | |
1030 | ||
1031 | fd = varlink_get_fd(v); | |
1032 | if (fd < 0) | |
1033 | return fd; | |
1034 | ||
1035 | events = varlink_get_events(v); | |
1036 | if (events < 0) | |
1037 | return events; | |
1038 | ||
1039 | pfd = (struct pollfd) { | |
1040 | .fd = fd, | |
1041 | .events = events, | |
1042 | }; | |
1043 | ||
1044 | r = ppoll(&pfd, 1, | |
1045 | t == USEC_INFINITY ? NULL : timespec_store(&ts, t), | |
1046 | NULL); | |
1047 | if (r < 0) | |
1048 | return -errno; | |
1049 | ||
1050 | handle_revents(v, pfd.revents); | |
1051 | ||
1052 | return r > 0 ? 1 : 0; | |
1053 | } | |
1054 | ||
1055 | int varlink_get_fd(Varlink *v) { | |
1056 | ||
1057 | assert_return(v, -EINVAL); | |
1058 | ||
1059 | if (v->state == VARLINK_DISCONNECTED) | |
1060 | return -ENOTCONN; | |
1061 | if (v->fd < 0) | |
1062 | return -EBADF; | |
1063 | ||
1064 | return v->fd; | |
1065 | } | |
1066 | ||
1067 | int varlink_get_events(Varlink *v) { | |
1068 | int ret = 0; | |
1069 | ||
1070 | assert_return(v, -EINVAL); | |
1071 | ||
1072 | if (v->state == VARLINK_DISCONNECTED) | |
1073 | return -ENOTCONN; | |
1074 | ||
1075 | if (v->connecting) /* When processing an asynchronous connect(), we only wait for EPOLLOUT, which | |
1076 | * tells us that the connection is now complete. Before that we should neither | |
1077 | * write() or read() from the fd. */ | |
1078 | return EPOLLOUT; | |
1079 | ||
1080 | if (!v->read_disconnected && | |
45a6c965 | 1081 | IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING, VARLINK_IDLE_SERVER) && |
d41bd96f LP |
1082 | !v->current && |
1083 | v->input_buffer_unscanned <= 0) | |
1084 | ret |= EPOLLIN; | |
1085 | ||
1086 | if (!v->write_disconnected && | |
1087 | v->output_buffer_size > 0) | |
1088 | ret |= EPOLLOUT; | |
1089 | ||
1090 | return ret; | |
1091 | } | |
1092 | ||
1093 | int varlink_get_timeout(Varlink *v, usec_t *ret) { | |
1094 | assert_return(v, -EINVAL); | |
1095 | ||
1096 | if (v->state == VARLINK_DISCONNECTED) | |
1097 | return -ENOTCONN; | |
1098 | ||
45a6c965 | 1099 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING) && |
d41bd96f LP |
1100 | v->timeout != USEC_INFINITY) { |
1101 | if (ret) | |
1102 | *ret = usec_add(v->timestamp, v->timeout); | |
1103 | return 1; | |
1104 | } else { | |
1105 | if (ret) | |
1106 | *ret = USEC_INFINITY; | |
1107 | return 0; | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | int varlink_flush(Varlink *v) { | |
1112 | int ret = 0, r; | |
1113 | ||
1114 | assert_return(v, -EINVAL); | |
1115 | ||
1116 | if (v->state == VARLINK_DISCONNECTED) | |
1117 | return -ENOTCONN; | |
1118 | ||
1119 | for (;;) { | |
1120 | struct pollfd pfd; | |
1121 | ||
1122 | if (v->output_buffer_size == 0) | |
1123 | break; | |
1124 | if (v->write_disconnected) | |
1125 | return -ECONNRESET; | |
1126 | ||
1127 | r = varlink_write(v); | |
1128 | if (r < 0) | |
1129 | return r; | |
1130 | if (r > 0) { | |
1131 | ret = 1; | |
1132 | continue; | |
1133 | } | |
1134 | ||
1135 | pfd = (struct pollfd) { | |
1136 | .fd = v->fd, | |
1137 | .events = POLLOUT, | |
1138 | }; | |
1139 | ||
1140 | if (poll(&pfd, 1, -1) < 0) | |
1141 | return -errno; | |
1142 | ||
1143 | handle_revents(v, pfd.revents); | |
1144 | } | |
1145 | ||
1146 | return ret; | |
1147 | } | |
1148 | ||
1149 | static void varlink_detach_server(Varlink *v) { | |
1150 | assert(v); | |
1151 | ||
1152 | if (!v->server) | |
1153 | return; | |
1154 | ||
1155 | if (v->server->by_uid && | |
1156 | v->ucred_acquired && | |
1157 | uid_is_valid(v->ucred.uid)) { | |
1158 | unsigned c; | |
1159 | ||
1160 | c = PTR_TO_UINT(hashmap_get(v->server->by_uid, UID_TO_PTR(v->ucred.uid))); | |
1161 | assert(c > 0); | |
1162 | ||
1163 | if (c == 1) | |
1164 | (void) hashmap_remove(v->server->by_uid, UID_TO_PTR(v->ucred.uid)); | |
1165 | else | |
1166 | (void) hashmap_replace(v->server->by_uid, UID_TO_PTR(v->ucred.uid), UINT_TO_PTR(c - 1)); | |
1167 | } | |
1168 | ||
1169 | assert(v->server->n_connections > 0); | |
1170 | v->server->n_connections--; | |
1171 | ||
1172 | /* If this is a connection associated to a server, then let's disconnect the server and the | |
1173 | * connection from each other. This drops the dangling reference that connect_callback() set up. */ | |
1174 | v->server = varlink_server_unref(v->server); | |
1175 | varlink_unref(v); | |
1176 | } | |
1177 | ||
1178 | int varlink_close(Varlink *v) { | |
1179 | ||
1180 | assert_return(v, -EINVAL); | |
1181 | ||
1182 | if (v->state == VARLINK_DISCONNECTED) | |
1183 | return 0; | |
1184 | ||
1185 | varlink_set_state(v, VARLINK_DISCONNECTED); | |
1186 | ||
1187 | /* Let's take a reference first, since varlink_detach_server() might drop the final (dangling) ref | |
1188 | * which would destroy us before we can call varlink_clear() */ | |
1189 | varlink_ref(v); | |
1190 | varlink_detach_server(v); | |
1191 | varlink_clear(v); | |
1192 | varlink_unref(v); | |
1193 | ||
1194 | return 1; | |
1195 | } | |
1196 | ||
1197 | Varlink* varlink_flush_close_unref(Varlink *v) { | |
1198 | ||
1199 | if (!v) | |
1200 | return NULL; | |
1201 | ||
1202 | (void) varlink_flush(v); | |
1203 | (void) varlink_close(v); | |
1204 | ||
1205 | return varlink_unref(v); | |
1206 | } | |
1207 | ||
1208 | static int varlink_enqueue_json(Varlink *v, JsonVariant *m) { | |
1209 | _cleanup_free_ char *text = NULL; | |
1210 | int r; | |
1211 | ||
1212 | assert(v); | |
1213 | assert(m); | |
1214 | ||
1215 | r = json_variant_format(m, 0, &text); | |
1216 | if (r < 0) | |
1217 | return r; | |
2a04712c | 1218 | assert(text[r] == '\0'); |
d41bd96f LP |
1219 | |
1220 | if (v->output_buffer_size + r + 1 > VARLINK_BUFFER_MAX) | |
1221 | return -ENOBUFS; | |
1222 | ||
1223 | varlink_log(v, "Sending message: %s", text); | |
1224 | ||
1225 | if (v->output_buffer_size == 0) { | |
1226 | ||
1227 | free_and_replace(v->output_buffer, text); | |
1228 | ||
1229 | v->output_buffer_size = v->output_buffer_allocated = r + 1; | |
1230 | v->output_buffer_index = 0; | |
1231 | ||
1232 | } else if (v->output_buffer_index == 0) { | |
1233 | ||
1234 | if (!GREEDY_REALLOC(v->output_buffer, v->output_buffer_allocated, v->output_buffer_size + r + 1)) | |
1235 | return -ENOMEM; | |
1236 | ||
1237 | memcpy(v->output_buffer + v->output_buffer_size, text, r + 1); | |
1238 | v->output_buffer_size += r + 1; | |
1239 | ||
1240 | } else { | |
1241 | char *n; | |
be44e091 | 1242 | const size_t new_size = v->output_buffer_size + r + 1; |
d41bd96f | 1243 | |
be44e091 | 1244 | n = new(char, new_size); |
d41bd96f LP |
1245 | if (!n) |
1246 | return -ENOMEM; | |
1247 | ||
1248 | memcpy(mempcpy(n, v->output_buffer + v->output_buffer_index, v->output_buffer_size), text, r + 1); | |
1249 | ||
1250 | free_and_replace(v->output_buffer, n); | |
be44e091 | 1251 | v->output_buffer_allocated = v->output_buffer_size = new_size; |
d41bd96f LP |
1252 | v->output_buffer_index = 0; |
1253 | } | |
1254 | ||
1255 | return 0; | |
1256 | } | |
1257 | ||
1258 | int varlink_send(Varlink *v, const char *method, JsonVariant *parameters) { | |
1259 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1260 | int r; | |
1261 | ||
1262 | assert_return(v, -EINVAL); | |
1263 | assert_return(method, -EINVAL); | |
1264 | ||
1265 | if (v->state == VARLINK_DISCONNECTED) | |
1266 | return -ENOTCONN; | |
45a6c965 LP |
1267 | |
1268 | /* We allow enqueuing multiple method calls at once! */ | |
d41bd96f LP |
1269 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT, VARLINK_AWAITING_REPLY)) |
1270 | return -EBUSY; | |
1271 | ||
1272 | r = varlink_sanitize_parameters(¶meters); | |
1273 | if (r < 0) | |
1274 | return r; | |
1275 | ||
1276 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1277 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1278 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
1279 | JSON_BUILD_PAIR("oneway", JSON_BUILD_BOOLEAN(true)))); | |
1280 | if (r < 0) | |
1281 | return r; | |
1282 | ||
1283 | r = varlink_enqueue_json(v, m); | |
1284 | if (r < 0) | |
1285 | return r; | |
1286 | ||
1287 | /* No state change here, this is one-way only after all */ | |
1288 | v->timestamp = now(CLOCK_MONOTONIC); | |
1289 | return 0; | |
1290 | } | |
1291 | ||
1292 | int varlink_sendb(Varlink *v, const char *method, ...) { | |
1293 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1294 | va_list ap; | |
1295 | int r; | |
1296 | ||
1297 | assert_return(v, -EINVAL); | |
1298 | ||
1299 | va_start(ap, method); | |
1300 | r = json_buildv(¶meters, ap); | |
1301 | va_end(ap); | |
1302 | ||
1303 | if (r < 0) | |
1304 | return r; | |
1305 | ||
1306 | return varlink_send(v, method, parameters); | |
1307 | } | |
1308 | ||
1309 | int varlink_invoke(Varlink *v, const char *method, JsonVariant *parameters) { | |
1310 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1311 | int r; | |
1312 | ||
1313 | assert_return(v, -EINVAL); | |
1314 | assert_return(method, -EINVAL); | |
1315 | ||
1316 | if (v->state == VARLINK_DISCONNECTED) | |
1317 | return -ENOTCONN; | |
45a6c965 LP |
1318 | |
1319 | /* We allow enqueing multiple method calls at once! */ | |
d41bd96f LP |
1320 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT, VARLINK_AWAITING_REPLY)) |
1321 | return -EBUSY; | |
1322 | ||
1323 | r = varlink_sanitize_parameters(¶meters); | |
1324 | if (r < 0) | |
1325 | return r; | |
1326 | ||
1327 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1328 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1329 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1330 | if (r < 0) | |
1331 | return r; | |
1332 | ||
1333 | r = varlink_enqueue_json(v, m); | |
1334 | if (r < 0) | |
1335 | return r; | |
1336 | ||
1337 | varlink_set_state(v, VARLINK_AWAITING_REPLY); | |
1338 | v->n_pending++; | |
1339 | v->timestamp = now(CLOCK_MONOTONIC); | |
1340 | ||
1341 | return 0; | |
1342 | } | |
1343 | ||
1344 | int varlink_invokeb(Varlink *v, const char *method, ...) { | |
1345 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1346 | va_list ap; | |
1347 | int r; | |
1348 | ||
1349 | assert_return(v, -EINVAL); | |
1350 | ||
1351 | va_start(ap, method); | |
1352 | r = json_buildv(¶meters, ap); | |
1353 | va_end(ap); | |
1354 | ||
1355 | if (r < 0) | |
1356 | return r; | |
1357 | ||
1358 | return varlink_invoke(v, method, parameters); | |
45a6c965 LP |
1359 | } |
1360 | ||
1361 | int varlink_observe(Varlink *v, const char *method, JsonVariant *parameters) { | |
1362 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1363 | int r; | |
1364 | ||
1365 | assert_return(v, -EINVAL); | |
1366 | assert_return(method, -EINVAL); | |
1367 | ||
1368 | if (v->state == VARLINK_DISCONNECTED) | |
1369 | return -ENOTCONN; | |
1370 | /* Note that we don't allow enqueuing multiple method calls when we are in more/continues mode! We | |
1371 | * thus insist on an idle client here. */ | |
1372 | if (v->state != VARLINK_IDLE_CLIENT) | |
1373 | return -EBUSY; | |
1374 | ||
1375 | r = varlink_sanitize_parameters(¶meters); | |
1376 | if (r < 0) | |
1377 | return r; | |
1378 | ||
1379 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1380 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1381 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
1382 | JSON_BUILD_PAIR("more", JSON_BUILD_BOOLEAN(true)))); | |
1383 | if (r < 0) | |
1384 | return r; | |
1385 | ||
1386 | r = varlink_enqueue_json(v, m); | |
1387 | if (r < 0) | |
1388 | return r; | |
1389 | ||
1390 | ||
1391 | varlink_set_state(v, VARLINK_AWAITING_REPLY_MORE); | |
1392 | v->n_pending++; | |
1393 | v->timestamp = now(CLOCK_MONOTONIC); | |
1394 | ||
1395 | return 0; | |
1396 | } | |
1397 | ||
1398 | int varlink_observeb(Varlink *v, const char *method, ...) { | |
1399 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1400 | va_list ap; | |
1401 | int r; | |
1402 | ||
1403 | assert_return(v, -EINVAL); | |
1404 | ||
1405 | va_start(ap, method); | |
1406 | r = json_buildv(¶meters, ap); | |
1407 | va_end(ap); | |
1408 | ||
1409 | if (r < 0) | |
1410 | return r; | |
1411 | ||
1412 | return varlink_observe(v, method, parameters); | |
d41bd96f LP |
1413 | } |
1414 | ||
1415 | int varlink_call( | |
1416 | Varlink *v, | |
1417 | const char *method, | |
1418 | JsonVariant *parameters, | |
1419 | JsonVariant **ret_parameters, | |
1420 | const char **ret_error_id, | |
1421 | VarlinkReplyFlags *ret_flags) { | |
1422 | ||
1423 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1424 | int r; | |
1425 | ||
1426 | assert_return(v, -EINVAL); | |
1427 | assert_return(method, -EINVAL); | |
1428 | ||
1429 | if (v->state == VARLINK_DISCONNECTED) | |
1430 | return -ENOTCONN; | |
1431 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT)) | |
1432 | return -EBUSY; | |
1433 | ||
1434 | assert(v->n_pending == 0); /* n_pending can't be > 0 if we are in VARLINK_IDLE_CLIENT state */ | |
1435 | ||
1436 | r = varlink_sanitize_parameters(¶meters); | |
1437 | if (r < 0) | |
1438 | return r; | |
1439 | ||
1440 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1441 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1442 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1443 | if (r < 0) | |
1444 | return r; | |
1445 | ||
1446 | r = varlink_enqueue_json(v, m); | |
1447 | if (r < 0) | |
1448 | return r; | |
1449 | ||
1450 | varlink_set_state(v, VARLINK_CALLING); | |
1451 | v->n_pending++; | |
1452 | v->timestamp = now(CLOCK_MONOTONIC); | |
1453 | ||
1454 | while (v->state == VARLINK_CALLING) { | |
1455 | ||
1456 | r = varlink_process(v); | |
1457 | if (r < 0) | |
1458 | return r; | |
1459 | if (r > 0) | |
1460 | continue; | |
1461 | ||
1462 | r = varlink_wait(v, USEC_INFINITY); | |
1463 | if (r < 0) | |
1464 | return r; | |
1465 | } | |
1466 | ||
1467 | switch (v->state) { | |
1468 | ||
1469 | case VARLINK_CALLED: | |
1470 | assert(v->current); | |
1471 | ||
1472 | json_variant_unref(v->reply); | |
1473 | v->reply = TAKE_PTR(v->current); | |
1474 | ||
1475 | varlink_set_state(v, VARLINK_IDLE_CLIENT); | |
1476 | assert(v->n_pending == 1); | |
1477 | v->n_pending--; | |
1478 | ||
1479 | if (ret_parameters) | |
1480 | *ret_parameters = json_variant_by_key(v->reply, "parameters"); | |
1481 | if (ret_error_id) | |
1482 | *ret_error_id = json_variant_string(json_variant_by_key(v->reply, "error")); | |
1483 | if (ret_flags) | |
1484 | *ret_flags = 0; | |
1485 | ||
1486 | return 1; | |
1487 | ||
1488 | case VARLINK_PENDING_DISCONNECT: | |
1489 | case VARLINK_DISCONNECTED: | |
1490 | return -ECONNRESET; | |
1491 | ||
1492 | case VARLINK_PENDING_TIMEOUT: | |
1493 | return -ETIME; | |
1494 | ||
1495 | default: | |
1496 | assert_not_reached("Unexpected state after method call."); | |
1497 | } | |
1498 | } | |
1499 | ||
1500 | int varlink_callb( | |
1501 | Varlink *v, | |
1502 | const char *method, | |
1503 | JsonVariant **ret_parameters, | |
1504 | const char **ret_error_id, | |
1505 | VarlinkReplyFlags *ret_flags, ...) { | |
1506 | ||
1507 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1508 | va_list ap; | |
1509 | int r; | |
1510 | ||
1511 | assert_return(v, -EINVAL); | |
1512 | ||
1513 | va_start(ap, ret_flags); | |
1514 | r = json_buildv(¶meters, ap); | |
1515 | va_end(ap); | |
1516 | ||
1517 | if (r < 0) | |
1518 | return r; | |
1519 | ||
1520 | return varlink_call(v, method, parameters, ret_parameters, ret_error_id, ret_flags); | |
1521 | } | |
1522 | ||
1523 | int varlink_reply(Varlink *v, JsonVariant *parameters) { | |
1524 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1525 | int r; | |
1526 | ||
1527 | assert_return(v, -EINVAL); | |
1528 | ||
1529 | if (v->state == VARLINK_DISCONNECTED) | |
1530 | return -ENOTCONN; | |
1531 | if (!IN_SET(v->state, | |
1532 | VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE, | |
1533 | VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) | |
1534 | return -EBUSY; | |
1535 | ||
1536 | r = varlink_sanitize_parameters(¶meters); | |
1537 | if (r < 0) | |
1538 | return r; | |
1539 | ||
1540 | r = json_build(&m, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1541 | if (r < 0) | |
1542 | return r; | |
1543 | ||
1544 | r = varlink_enqueue_json(v, m); | |
1545 | if (r < 0) | |
1546 | return r; | |
1547 | ||
1548 | if (IN_SET(v->state, VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) { | |
1549 | /* We just replied to a method call that was let hanging for a while (i.e. we were outside of | |
1550 | * the varlink_dispatch_method() stack frame), which means with this reply we are ready to | |
1551 | * process further messages. */ | |
1552 | v->current = json_variant_unref(v->current); | |
1553 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
1554 | } else | |
1555 | /* We replied to a method call from within the varlink_dispatch_method() stack frame), which | |
1556 | * means we should it handle the rest of the state engine. */ | |
1557 | varlink_set_state(v, VARLINK_PROCESSED_METHOD); | |
1558 | ||
1559 | return 1; | |
1560 | } | |
1561 | ||
1562 | int varlink_replyb(Varlink *v, ...) { | |
1563 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1564 | va_list ap; | |
1565 | int r; | |
1566 | ||
1567 | assert_return(v, -EINVAL); | |
1568 | ||
1569 | va_start(ap, v); | |
1570 | r = json_buildv(¶meters, ap); | |
1571 | va_end(ap); | |
1572 | ||
1573 | if (r < 0) | |
1574 | return r; | |
1575 | ||
1576 | return varlink_reply(v, parameters); | |
1577 | } | |
1578 | ||
1579 | int varlink_error(Varlink *v, const char *error_id, JsonVariant *parameters) { | |
1580 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1581 | int r; | |
1582 | ||
1583 | assert_return(v, -EINVAL); | |
1584 | assert_return(error_id, -EINVAL); | |
1585 | ||
1586 | if (v->state == VARLINK_DISCONNECTED) | |
1587 | return -ENOTCONN; | |
1588 | if (!IN_SET(v->state, | |
1589 | VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE, | |
1590 | VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) | |
1591 | return -EBUSY; | |
1592 | ||
1593 | r = varlink_sanitize_parameters(¶meters); | |
1594 | if (r < 0) | |
1595 | return r; | |
1596 | ||
1597 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1598 | JSON_BUILD_PAIR("error", JSON_BUILD_STRING(error_id)), | |
1599 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1600 | if (r < 0) | |
1601 | return r; | |
1602 | ||
1603 | r = varlink_enqueue_json(v, m); | |
1604 | if (r < 0) | |
1605 | return r; | |
1606 | ||
1607 | if (IN_SET(v->state, VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) { | |
1608 | v->current = json_variant_unref(v->current); | |
1609 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
1610 | } else | |
1611 | varlink_set_state(v, VARLINK_PROCESSED_METHOD); | |
1612 | ||
1613 | return 1; | |
1614 | } | |
1615 | ||
1616 | int varlink_errorb(Varlink *v, const char *error_id, ...) { | |
1617 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1618 | va_list ap; | |
1619 | int r; | |
1620 | ||
1621 | assert_return(v, -EINVAL); | |
1622 | assert_return(error_id, -EINVAL); | |
1623 | ||
1624 | va_start(ap, error_id); | |
1625 | r = json_buildv(¶meters, ap); | |
1626 | va_end(ap); | |
1627 | ||
1628 | if (r < 0) | |
1629 | return r; | |
1630 | ||
1631 | return varlink_error(v, error_id, parameters); | |
1632 | } | |
1633 | ||
1634 | int varlink_error_invalid_parameter(Varlink *v, JsonVariant *parameters) { | |
1635 | ||
1636 | assert_return(v, -EINVAL); | |
1637 | assert_return(parameters, -EINVAL); | |
1638 | ||
1639 | /* We expect to be called in one of two ways: the 'parameters' argument is a string variant in which | |
1640 | * case it is the parameter key name that is invalid. Or the 'parameters' argument is an object | |
1641 | * variant in which case we'll pull out the first key. The latter mode is useful in functions that | |
1642 | * don't expect any arguments. */ | |
1643 | ||
1644 | if (json_variant_is_string(parameters)) | |
1645 | return varlink_error(v, VARLINK_ERROR_INVALID_PARAMETER, parameters); | |
1646 | ||
1647 | if (json_variant_is_object(parameters) && | |
1648 | json_variant_elements(parameters) > 0) | |
1649 | return varlink_error(v, VARLINK_ERROR_INVALID_PARAMETER, | |
1650 | json_variant_by_index(parameters, 0)); | |
1651 | ||
1652 | return -EINVAL; | |
1653 | } | |
1654 | ||
1655 | int varlink_notify(Varlink *v, JsonVariant *parameters) { | |
1656 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1657 | int r; | |
1658 | ||
1659 | assert_return(v, -EINVAL); | |
1660 | ||
1661 | if (v->state == VARLINK_DISCONNECTED) | |
1662 | return -ENOTCONN; | |
1663 | if (!IN_SET(v->state, VARLINK_PROCESSING_METHOD_MORE, VARLINK_PENDING_METHOD_MORE)) | |
1664 | return -EBUSY; | |
1665 | ||
1666 | r = varlink_sanitize_parameters(¶meters); | |
1667 | if (r < 0) | |
1668 | return r; | |
1669 | ||
1670 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1671 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
1672 | JSON_BUILD_PAIR("continues", JSON_BUILD_BOOLEAN(true)))); | |
1673 | if (r < 0) | |
1674 | return r; | |
1675 | ||
1676 | r = varlink_enqueue_json(v, m); | |
1677 | if (r < 0) | |
1678 | return r; | |
1679 | ||
1680 | /* No state change, as more is coming */ | |
1681 | return 1; | |
1682 | } | |
1683 | ||
1684 | int varlink_notifyb(Varlink *v, ...) { | |
1685 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1686 | va_list ap; | |
1687 | int r; | |
1688 | ||
1689 | assert_return(v, -EINVAL); | |
1690 | ||
1691 | va_start(ap, v); | |
1692 | r = json_buildv(¶meters, ap); | |
1693 | va_end(ap); | |
1694 | ||
1695 | if (r < 0) | |
1696 | return r; | |
1697 | ||
1698 | return varlink_notify(v, parameters); | |
1699 | } | |
1700 | ||
1701 | int varlink_bind_reply(Varlink *v, VarlinkReply callback) { | |
1702 | assert_return(v, -EINVAL); | |
1703 | ||
1704 | if (callback && v->reply_callback && callback != v->reply_callback) | |
1705 | return -EBUSY; | |
1706 | ||
1707 | v->reply_callback = callback; | |
1708 | ||
1709 | return 0; | |
1710 | } | |
1711 | ||
1712 | void* varlink_set_userdata(Varlink *v, void *userdata) { | |
1713 | void *old; | |
1714 | ||
1715 | assert_return(v, NULL); | |
1716 | ||
1717 | old = v->userdata; | |
1718 | v->userdata = userdata; | |
1719 | ||
1720 | return old; | |
1721 | } | |
1722 | ||
1723 | void* varlink_get_userdata(Varlink *v) { | |
1724 | assert_return(v, NULL); | |
1725 | ||
1726 | return v->userdata; | |
1727 | } | |
1728 | ||
1729 | static int varlink_acquire_ucred(Varlink *v) { | |
1730 | int r; | |
1731 | ||
1732 | assert(v); | |
1733 | ||
1734 | if (v->ucred_acquired) | |
1735 | return 0; | |
1736 | ||
1737 | r = getpeercred(v->fd, &v->ucred); | |
1738 | if (r < 0) | |
1739 | return r; | |
1740 | ||
1741 | v->ucred_acquired = true; | |
1742 | return 0; | |
1743 | } | |
1744 | ||
1745 | int varlink_get_peer_uid(Varlink *v, uid_t *ret) { | |
1746 | int r; | |
1747 | ||
1748 | assert_return(v, -EINVAL); | |
1749 | assert_return(ret, -EINVAL); | |
1750 | ||
1751 | r = varlink_acquire_ucred(v); | |
1752 | if (r < 0) | |
1753 | return r; | |
1754 | ||
1755 | if (!uid_is_valid(v->ucred.uid)) | |
1756 | return -ENODATA; | |
1757 | ||
1758 | *ret = v->ucred.uid; | |
1759 | return 0; | |
1760 | } | |
1761 | ||
1762 | int varlink_get_peer_pid(Varlink *v, pid_t *ret) { | |
1763 | int r; | |
1764 | ||
1765 | assert_return(v, -EINVAL); | |
1766 | assert_return(ret, -EINVAL); | |
1767 | ||
1768 | r = varlink_acquire_ucred(v); | |
1769 | if (r < 0) | |
1770 | return r; | |
1771 | ||
1772 | if (!pid_is_valid(v->ucred.pid)) | |
1773 | return -ENODATA; | |
1774 | ||
1775 | *ret = v->ucred.pid; | |
1776 | return 0; | |
1777 | } | |
1778 | ||
1779 | int varlink_set_relative_timeout(Varlink *v, usec_t timeout) { | |
1780 | assert_return(v, -EINVAL); | |
1781 | assert_return(timeout > 0, -EINVAL); | |
1782 | ||
1783 | v->timeout = timeout; | |
1784 | return 0; | |
1785 | } | |
1786 | ||
1787 | VarlinkServer *varlink_get_server(Varlink *v) { | |
1788 | assert_return(v, NULL); | |
1789 | ||
1790 | return v->server; | |
1791 | } | |
1792 | ||
1793 | int varlink_set_description(Varlink *v, const char *description) { | |
1794 | assert_return(v, -EINVAL); | |
1795 | ||
1796 | return free_and_strdup(&v->description, description); | |
1797 | } | |
1798 | ||
1799 | static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
1800 | Varlink *v = userdata; | |
1801 | ||
1802 | assert(s); | |
1803 | assert(v); | |
1804 | ||
1805 | handle_revents(v, revents); | |
1806 | (void) varlink_process(v); | |
1807 | ||
1808 | return 1; | |
1809 | } | |
1810 | ||
1811 | static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) { | |
1812 | Varlink *v = userdata; | |
1813 | ||
1814 | assert(s); | |
1815 | assert(v); | |
1816 | ||
1817 | (void) varlink_process(v); | |
1818 | return 1; | |
1819 | } | |
1820 | ||
1821 | static int defer_callback(sd_event_source *s, void *userdata) { | |
1822 | Varlink *v = userdata; | |
1823 | ||
1824 | assert(s); | |
1825 | assert(v); | |
1826 | ||
1827 | (void) varlink_process(v); | |
1828 | return 1; | |
1829 | } | |
1830 | ||
1831 | static int prepare_callback(sd_event_source *s, void *userdata) { | |
1832 | Varlink *v = userdata; | |
1833 | int r, e; | |
1834 | usec_t until; | |
1835 | ||
1836 | assert(s); | |
1837 | assert(v); | |
1838 | ||
1839 | e = varlink_get_events(v); | |
1840 | if (e < 0) | |
1841 | return e; | |
1842 | ||
1843 | r = sd_event_source_set_io_events(v->io_event_source, e); | |
1844 | if (r < 0) | |
1845 | return r; | |
1846 | ||
1847 | r = varlink_get_timeout(v, &until); | |
1848 | if (r < 0) | |
1849 | return r; | |
1850 | if (r > 0) { | |
1851 | r = sd_event_source_set_time(v->time_event_source, until); | |
1852 | if (r < 0) | |
1853 | return r; | |
1854 | } | |
1855 | ||
1856 | r = sd_event_source_set_enabled(v->time_event_source, r > 0 ? SD_EVENT_ON : SD_EVENT_OFF); | |
1857 | if (r < 0) | |
1858 | return r; | |
1859 | ||
1860 | return 1; | |
1861 | } | |
1862 | ||
1863 | static int quit_callback(sd_event_source *event, void *userdata) { | |
1864 | Varlink *v = userdata; | |
1865 | ||
1866 | assert(event); | |
1867 | assert(v); | |
1868 | ||
1869 | varlink_flush(v); | |
1870 | varlink_close(v); | |
1871 | ||
1872 | return 1; | |
1873 | } | |
1874 | ||
1875 | int varlink_attach_event(Varlink *v, sd_event *e, int64_t priority) { | |
1876 | int r; | |
1877 | ||
1878 | assert_return(v, -EINVAL); | |
1879 | assert_return(!v->event, -EBUSY); | |
1880 | ||
1881 | if (e) | |
1882 | v->event = sd_event_ref(e); | |
1883 | else { | |
1884 | r = sd_event_default(&v->event); | |
1885 | if (r < 0) | |
1886 | return r; | |
1887 | } | |
1888 | ||
1889 | r = sd_event_add_time(v->event, &v->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, v); | |
1890 | if (r < 0) | |
1891 | goto fail; | |
1892 | ||
1893 | r = sd_event_source_set_priority(v->time_event_source, priority); | |
1894 | if (r < 0) | |
1895 | goto fail; | |
1896 | ||
1897 | (void) sd_event_source_set_description(v->time_event_source, "varlink-time"); | |
1898 | ||
1899 | r = sd_event_add_exit(v->event, &v->quit_event_source, quit_callback, v); | |
1900 | if (r < 0) | |
1901 | goto fail; | |
1902 | ||
1903 | r = sd_event_source_set_priority(v->quit_event_source, priority); | |
1904 | if (r < 0) | |
1905 | goto fail; | |
1906 | ||
1907 | (void) sd_event_source_set_description(v->quit_event_source, "varlink-quit"); | |
1908 | ||
1909 | r = sd_event_add_io(v->event, &v->io_event_source, v->fd, 0, io_callback, v); | |
1910 | if (r < 0) | |
1911 | goto fail; | |
1912 | ||
1913 | r = sd_event_source_set_prepare(v->io_event_source, prepare_callback); | |
1914 | if (r < 0) | |
1915 | goto fail; | |
1916 | ||
1917 | r = sd_event_source_set_priority(v->io_event_source, priority); | |
1918 | if (r < 0) | |
1919 | goto fail; | |
1920 | ||
1921 | (void) sd_event_source_set_description(v->io_event_source, "varlink-io"); | |
1922 | ||
1923 | r = sd_event_add_defer(v->event, &v->defer_event_source, defer_callback, v); | |
1924 | if (r < 0) | |
1925 | goto fail; | |
1926 | ||
1927 | r = sd_event_source_set_priority(v->defer_event_source, priority); | |
1928 | if (r < 0) | |
1929 | goto fail; | |
1930 | ||
1931 | (void) sd_event_source_set_description(v->defer_event_source, "varlink-defer"); | |
1932 | ||
1933 | return 0; | |
1934 | ||
1935 | fail: | |
1936 | varlink_detach_event(v); | |
1937 | return r; | |
1938 | } | |
1939 | ||
d41bd96f LP |
1940 | void varlink_detach_event(Varlink *v) { |
1941 | if (!v) | |
1942 | return; | |
1943 | ||
1944 | varlink_detach_event_sources(v); | |
1945 | ||
1946 | v->event = sd_event_unref(v->event); | |
1947 | } | |
1948 | ||
1949 | sd_event *varlink_get_event(Varlink *v) { | |
1950 | assert_return(v, NULL); | |
1951 | ||
1952 | return v->event; | |
1953 | } | |
1954 | ||
1955 | int varlink_server_new(VarlinkServer **ret, VarlinkServerFlags flags) { | |
1956 | VarlinkServer *s; | |
1957 | ||
1958 | assert_return(ret, -EINVAL); | |
1959 | assert_return((flags & ~_VARLINK_SERVER_FLAGS_ALL) == 0, -EINVAL); | |
1960 | ||
1961 | s = new(VarlinkServer, 1); | |
1962 | if (!s) | |
1963 | return -ENOMEM; | |
1964 | ||
1965 | *s = (VarlinkServer) { | |
1966 | .n_ref = 1, | |
1967 | .flags = flags, | |
1968 | .connections_max = varlink_server_connections_max(NULL), | |
1969 | .connections_per_uid_max = varlink_server_connections_per_uid_max(NULL), | |
1970 | }; | |
1971 | ||
1972 | *ret = s; | |
1973 | return 0; | |
1974 | } | |
1975 | ||
1976 | static VarlinkServer* varlink_server_destroy(VarlinkServer *s) { | |
1977 | char *m; | |
1978 | ||
1979 | if (!s) | |
1980 | return NULL; | |
1981 | ||
1982 | varlink_server_shutdown(s); | |
1983 | ||
1984 | while ((m = hashmap_steal_first_key(s->methods))) | |
1985 | free(m); | |
1986 | ||
1987 | hashmap_free(s->methods); | |
1988 | hashmap_free(s->by_uid); | |
1989 | ||
1990 | sd_event_unref(s->event); | |
1991 | ||
1992 | free(s->description); | |
1993 | ||
1994 | return mfree(s); | |
1995 | } | |
1996 | ||
1997 | DEFINE_TRIVIAL_REF_UNREF_FUNC(VarlinkServer, varlink_server, varlink_server_destroy); | |
1998 | ||
1999 | static int validate_connection(VarlinkServer *server, const struct ucred *ucred) { | |
2000 | int allowed = -1; | |
2001 | ||
2002 | assert(server); | |
2003 | assert(ucred); | |
2004 | ||
2005 | if (FLAGS_SET(server->flags, VARLINK_SERVER_ROOT_ONLY)) | |
2006 | allowed = ucred->uid == 0; | |
2007 | ||
2008 | if (FLAGS_SET(server->flags, VARLINK_SERVER_MYSELF_ONLY)) | |
2009 | allowed = allowed > 0 || ucred->uid == getuid(); | |
2010 | ||
2011 | if (allowed == 0) { /* Allow access when it is explicitly allowed or when neither | |
2012 | * VARLINK_SERVER_ROOT_ONLY nor VARLINK_SERVER_MYSELF_ONLY are specified. */ | |
2013 | varlink_server_log(server, "Unprivileged client attempted connection, refusing."); | |
2014 | return 0; | |
2015 | } | |
2016 | ||
2017 | if (server->n_connections >= server->connections_max) { | |
2018 | varlink_server_log(server, "Connection limit of %u reached, refusing.", server->connections_max); | |
2019 | return 0; | |
2020 | } | |
2021 | ||
2022 | if (FLAGS_SET(server->flags, VARLINK_SERVER_ACCOUNT_UID)) { | |
2023 | unsigned c; | |
2024 | ||
2025 | if (!uid_is_valid(ucred->uid)) { | |
2026 | varlink_server_log(server, "Client with invalid UID attempted connection, refusing."); | |
2027 | return 0; | |
2028 | } | |
2029 | ||
2030 | c = PTR_TO_UINT(hashmap_get(server->by_uid, UID_TO_PTR(ucred->uid))); | |
2031 | if (c >= server->connections_per_uid_max) { | |
2032 | varlink_server_log(server, "Per-UID connection limit of %u reached, refusing.", | |
2033 | server->connections_per_uid_max); | |
2034 | return 0; | |
2035 | } | |
2036 | } | |
2037 | ||
2038 | return 1; | |
2039 | } | |
2040 | ||
2041 | static int count_connection(VarlinkServer *server, struct ucred *ucred) { | |
2042 | unsigned c; | |
2043 | int r; | |
2044 | ||
2045 | assert(server); | |
2046 | assert(ucred); | |
2047 | ||
2048 | server->n_connections++; | |
2049 | ||
2050 | if (FLAGS_SET(server->flags, VARLINK_SERVER_ACCOUNT_UID)) { | |
2051 | r = hashmap_ensure_allocated(&server->by_uid, NULL); | |
2052 | if (r < 0) | |
2053 | return log_debug_errno(r, "Failed to allocate UID hash table: %m"); | |
2054 | ||
2055 | c = PTR_TO_UINT(hashmap_get(server->by_uid, UID_TO_PTR(ucred->uid))); | |
2056 | ||
2057 | varlink_server_log(server, "Connections of user " UID_FMT ": %u (of %u max)", | |
2058 | ucred->uid, c, server->connections_per_uid_max); | |
2059 | ||
2060 | r = hashmap_replace(server->by_uid, UID_TO_PTR(ucred->uid), UINT_TO_PTR(c + 1)); | |
2061 | if (r < 0) | |
2062 | return log_debug_errno(r, "Failed to increment counter in UID hash table: %m"); | |
2063 | } | |
2064 | ||
2065 | return 0; | |
2066 | } | |
2067 | ||
2068 | int varlink_server_add_connection(VarlinkServer *server, int fd, Varlink **ret) { | |
2069 | _cleanup_(varlink_unrefp) Varlink *v = NULL; | |
2070 | bool ucred_acquired; | |
2071 | struct ucred ucred; | |
2072 | int r; | |
2073 | ||
2074 | assert_return(server, -EINVAL); | |
2075 | assert_return(fd >= 0, -EBADF); | |
2076 | ||
2077 | if ((server->flags & (VARLINK_SERVER_ROOT_ONLY|VARLINK_SERVER_ACCOUNT_UID)) != 0) { | |
2078 | r = getpeercred(fd, &ucred); | |
2079 | if (r < 0) | |
2080 | return varlink_server_log_errno(server, r, "Failed to acquire peer credentials of incoming socket, refusing: %m"); | |
2081 | ||
2082 | ucred_acquired = true; | |
2083 | ||
2084 | r = validate_connection(server, &ucred); | |
2085 | if (r < 0) | |
2086 | return r; | |
2087 | if (r == 0) | |
2088 | return -EPERM; | |
2089 | } else | |
2090 | ucred_acquired = false; | |
2091 | ||
2092 | r = varlink_new(&v); | |
2093 | if (r < 0) | |
2094 | return varlink_server_log_errno(server, r, "Failed to allocate connection object: %m"); | |
2095 | ||
2096 | r = count_connection(server, &ucred); | |
2097 | if (r < 0) | |
2098 | return r; | |
2099 | ||
2100 | v->fd = fd; | |
2101 | v->userdata = server->userdata; | |
2102 | if (ucred_acquired) { | |
2103 | v->ucred = ucred; | |
2104 | v->ucred_acquired = true; | |
2105 | } | |
2106 | ||
2107 | (void) asprintf(&v->description, "%s-%i", server->description ?: "varlink", v->fd); | |
2108 | ||
2109 | /* Link up the server and the connection, and take reference in both directions. Note that the | |
2110 | * reference on the connection is left dangling. It will be dropped when the connection is closed, | |
2111 | * which happens in varlink_close(), including in the event loop quit callback. */ | |
2112 | v->server = varlink_server_ref(server); | |
2113 | varlink_ref(v); | |
2114 | ||
2115 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
2116 | ||
7e69d90c LP |
2117 | if (server->event) { |
2118 | r = varlink_attach_event(v, server->event, server->event_priority); | |
2119 | if (r < 0) { | |
2120 | varlink_log_errno(v, r, "Failed to attach new connection: %m"); | |
2121 | v->fd = -1; /* take the fd out of the connection again */ | |
2122 | varlink_close(v); | |
2123 | return r; | |
2124 | } | |
d41bd96f LP |
2125 | } |
2126 | ||
2127 | if (ret) | |
2128 | *ret = v; | |
2129 | ||
2130 | return 0; | |
2131 | } | |
2132 | ||
2133 | static int connect_callback(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |
2134 | VarlinkServerSocket *ss = userdata; | |
2135 | _cleanup_close_ int cfd = -1; | |
2136 | Varlink *v = NULL; | |
2137 | int r; | |
2138 | ||
2139 | assert(source); | |
2140 | assert(ss); | |
2141 | ||
2142 | varlink_server_log(ss->server, "New incoming connection."); | |
2143 | ||
2144 | cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); | |
2145 | if (cfd < 0) { | |
2146 | if (ERRNO_IS_ACCEPT_AGAIN(errno)) | |
2147 | return 0; | |
2148 | ||
2149 | return varlink_server_log_errno(ss->server, errno, "Failed to accept incoming socket: %m"); | |
2150 | } | |
2151 | ||
2152 | r = varlink_server_add_connection(ss->server, cfd, &v); | |
2153 | if (r < 0) | |
2154 | return 0; | |
2155 | ||
2156 | TAKE_FD(cfd); | |
2157 | ||
2158 | if (ss->server->connect_callback) { | |
2159 | r = ss->server->connect_callback(ss->server, v, ss->server->userdata); | |
2160 | if (r < 0) { | |
2161 | varlink_log_errno(v, r, "Connection callback returned error, disconnecting client: %m"); | |
2162 | varlink_close(v); | |
2163 | return 0; | |
2164 | } | |
2165 | } | |
2166 | ||
2167 | return 0; | |
2168 | } | |
2169 | ||
2170 | int varlink_server_listen_fd(VarlinkServer *s, int fd) { | |
2171 | _cleanup_free_ VarlinkServerSocket *ss = NULL; | |
2172 | int r; | |
2173 | ||
2174 | assert_return(s, -EINVAL); | |
2175 | assert_return(fd >= 0, -EBADF); | |
2176 | ||
2177 | r = fd_nonblock(fd, true); | |
2178 | if (r < 0) | |
2179 | return r; | |
2180 | ||
2181 | ss = new(VarlinkServerSocket, 1); | |
2182 | if (!ss) | |
2183 | return -ENOMEM; | |
2184 | ||
2185 | *ss = (VarlinkServerSocket) { | |
2186 | .server = s, | |
2187 | .fd = fd, | |
2188 | }; | |
2189 | ||
2190 | if (s->event) { | |
2191 | _cleanup_(sd_event_source_unrefp) sd_event_source *es = NULL; | |
2192 | ||
2193 | r = sd_event_add_io(s->event, &es, fd, EPOLLIN, connect_callback, ss); | |
2194 | if (r < 0) | |
2195 | return r; | |
2196 | ||
2197 | r = sd_event_source_set_priority(ss->event_source, s->event_priority); | |
2198 | if (r < 0) | |
2199 | return r; | |
2200 | } | |
2201 | ||
2202 | LIST_PREPEND(sockets, s->sockets, TAKE_PTR(ss)); | |
2203 | return 0; | |
2204 | } | |
2205 | ||
2206 | int varlink_server_listen_address(VarlinkServer *s, const char *address, mode_t m) { | |
2207 | union sockaddr_union sockaddr; | |
2208 | _cleanup_close_ int fd = -1; | |
2209 | int r; | |
2210 | ||
2211 | assert_return(s, -EINVAL); | |
2212 | assert_return(address, -EINVAL); | |
2213 | assert_return((m & ~0777) == 0, -EINVAL); | |
2214 | ||
2215 | r = sockaddr_un_set_path(&sockaddr.un, address); | |
2216 | if (r < 0) | |
2217 | return r; | |
2218 | ||
2219 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
2220 | if (fd < 0) | |
2221 | return -errno; | |
2222 | ||
2223 | (void) sockaddr_un_unlink(&sockaddr.un); | |
2224 | ||
2225 | RUN_WITH_UMASK(~m & 0777) | |
2226 | if (bind(fd, &sockaddr.sa, SOCKADDR_UN_LEN(sockaddr.un)) < 0) | |
2227 | return -errno; | |
2228 | ||
2229 | if (listen(fd, SOMAXCONN) < 0) | |
2230 | return -errno; | |
2231 | ||
2232 | r = varlink_server_listen_fd(s, fd); | |
2233 | if (r < 0) | |
2234 | return r; | |
2235 | ||
2236 | TAKE_FD(fd); | |
2237 | return 0; | |
2238 | } | |
2239 | ||
2240 | void* varlink_server_set_userdata(VarlinkServer *s, void *userdata) { | |
2241 | void *ret; | |
2242 | ||
2243 | assert_return(s, NULL); | |
2244 | ||
2245 | ret = s->userdata; | |
2246 | s->userdata = userdata; | |
2247 | ||
2248 | return ret; | |
2249 | } | |
2250 | ||
2251 | void* varlink_server_get_userdata(VarlinkServer *s) { | |
2252 | assert_return(s, NULL); | |
2253 | ||
2254 | return s->userdata; | |
2255 | } | |
2256 | ||
2257 | static VarlinkServerSocket* varlink_server_socket_destroy(VarlinkServerSocket *ss) { | |
2258 | if (!ss) | |
2259 | return NULL; | |
2260 | ||
2261 | if (ss->server) | |
2262 | LIST_REMOVE(sockets, ss->server->sockets, ss); | |
2263 | ||
1d3fe304 | 2264 | sd_event_source_disable_unref(ss->event_source); |
d41bd96f LP |
2265 | |
2266 | free(ss->address); | |
2267 | safe_close(ss->fd); | |
2268 | ||
2269 | return mfree(ss); | |
2270 | } | |
2271 | ||
2272 | int varlink_server_shutdown(VarlinkServer *s) { | |
2273 | assert_return(s, -EINVAL); | |
2274 | ||
2275 | while (s->sockets) | |
2276 | varlink_server_socket_destroy(s->sockets); | |
2277 | ||
2278 | return 0; | |
2279 | } | |
2280 | ||
2281 | int varlink_server_attach_event(VarlinkServer *s, sd_event *e, int64_t priority) { | |
2282 | VarlinkServerSocket *ss; | |
2283 | int r; | |
2284 | ||
2285 | assert_return(s, -EINVAL); | |
2286 | assert_return(!s->event, -EBUSY); | |
2287 | ||
2288 | if (e) | |
2289 | s->event = sd_event_ref(e); | |
2290 | else { | |
2291 | r = sd_event_default(&s->event); | |
2292 | if (r < 0) | |
2293 | return r; | |
2294 | } | |
2295 | ||
2296 | LIST_FOREACH(sockets, ss, s->sockets) { | |
2297 | assert(!ss->event_source); | |
2298 | ||
2299 | r = sd_event_add_io(s->event, &ss->event_source, ss->fd, EPOLLIN, connect_callback, ss); | |
2300 | if (r < 0) | |
2301 | goto fail; | |
2302 | ||
2303 | r = sd_event_source_set_priority(ss->event_source, priority); | |
2304 | if (r < 0) | |
2305 | goto fail; | |
2306 | } | |
2307 | ||
2308 | s->event_priority = priority; | |
2309 | return 0; | |
2310 | ||
2311 | fail: | |
2312 | varlink_server_detach_event(s); | |
2313 | return r; | |
2314 | } | |
2315 | ||
2316 | int varlink_server_detach_event(VarlinkServer *s) { | |
2317 | VarlinkServerSocket *ss; | |
2318 | ||
2319 | assert_return(s, -EINVAL); | |
2320 | ||
2321 | LIST_FOREACH(sockets, ss, s->sockets) { | |
2322 | ||
2323 | if (!ss->event_source) | |
2324 | continue; | |
2325 | ||
2326 | (void) sd_event_source_set_enabled(ss->event_source, SD_EVENT_OFF); | |
2327 | ss->event_source = sd_event_source_unref(ss->event_source); | |
2328 | } | |
2329 | ||
2330 | sd_event_unref(s->event); | |
2331 | return 0; | |
2332 | } | |
2333 | ||
2334 | sd_event *varlink_server_get_event(VarlinkServer *s) { | |
2335 | assert_return(s, NULL); | |
2336 | ||
2337 | return s->event; | |
2338 | } | |
2339 | ||
2340 | int varlink_server_bind_method(VarlinkServer *s, const char *method, VarlinkMethod callback) { | |
2341 | char *m; | |
2342 | int r; | |
2343 | ||
2344 | assert_return(s, -EINVAL); | |
2345 | assert_return(method, -EINVAL); | |
2346 | assert_return(callback, -EINVAL); | |
2347 | ||
2348 | if (startswith(method, "org.varlink.service.")) | |
2349 | return -EEXIST; | |
2350 | ||
2351 | r = hashmap_ensure_allocated(&s->methods, &string_hash_ops); | |
2352 | if (r < 0) | |
2353 | return r; | |
2354 | ||
2355 | m = strdup(method); | |
2356 | if (!m) | |
2357 | return -ENOMEM; | |
2358 | ||
2359 | r = hashmap_put(s->methods, m, callback); | |
2360 | if (r < 0) { | |
2361 | free(m); | |
2362 | return r; | |
2363 | } | |
2364 | ||
2365 | return 0; | |
2366 | } | |
2367 | ||
2368 | int varlink_server_bind_method_many_internal(VarlinkServer *s, ...) { | |
2369 | va_list ap; | |
e7b93f97 | 2370 | int r = 0; |
d41bd96f LP |
2371 | |
2372 | assert_return(s, -EINVAL); | |
2373 | ||
2374 | va_start(ap, s); | |
2375 | for (;;) { | |
2376 | VarlinkMethod callback; | |
2377 | const char *method; | |
2378 | ||
2379 | method = va_arg(ap, const char *); | |
2380 | if (!method) | |
2381 | break; | |
2382 | ||
2383 | callback = va_arg(ap, VarlinkMethod); | |
2384 | ||
2385 | r = varlink_server_bind_method(s, method, callback); | |
2386 | if (r < 0) | |
e7b93f97 | 2387 | break; |
d41bd96f | 2388 | } |
e7b93f97 | 2389 | va_end(ap); |
d41bd96f | 2390 | |
e7b93f97 | 2391 | return r; |
d41bd96f LP |
2392 | } |
2393 | ||
2394 | int varlink_server_bind_connect(VarlinkServer *s, VarlinkConnect callback) { | |
2395 | assert_return(s, -EINVAL); | |
2396 | ||
2397 | if (callback && s->connect_callback && callback != s->connect_callback) | |
2398 | return -EBUSY; | |
2399 | ||
2400 | s->connect_callback = callback; | |
2401 | return 0; | |
2402 | } | |
2403 | ||
2404 | unsigned varlink_server_connections_max(VarlinkServer *s) { | |
2405 | struct rlimit rl; | |
2406 | ||
2407 | /* If a server is specified, return the setting for that server, otherwise the default value */ | |
2408 | if (s) | |
2409 | return s->connections_max; | |
2410 | ||
2411 | assert_se(getrlimit(RLIMIT_NOFILE, &rl) >= 0); | |
2412 | ||
2413 | /* Make sure we never use up more than ¾th of RLIMIT_NOFILE for IPC */ | |
2414 | if (VARLINK_DEFAULT_CONNECTIONS_MAX > rl.rlim_cur / 4 * 3) | |
2415 | return rl.rlim_cur / 4 * 3; | |
2416 | ||
2417 | return VARLINK_DEFAULT_CONNECTIONS_MAX; | |
2418 | } | |
2419 | ||
2420 | unsigned varlink_server_connections_per_uid_max(VarlinkServer *s) { | |
2421 | unsigned m; | |
2422 | ||
2423 | if (s) | |
2424 | return s->connections_per_uid_max; | |
2425 | ||
2426 | /* Make sure to never use up more than ¾th of available connections for a single user */ | |
2427 | m = varlink_server_connections_max(NULL); | |
2428 | if (VARLINK_DEFAULT_CONNECTIONS_PER_UID_MAX > m) | |
2429 | return m / 4 * 3; | |
2430 | ||
2431 | return VARLINK_DEFAULT_CONNECTIONS_PER_UID_MAX; | |
2432 | } | |
2433 | ||
2434 | int varlink_server_set_connections_per_uid_max(VarlinkServer *s, unsigned m) { | |
2435 | assert_return(s, -EINVAL); | |
2436 | assert_return(m > 0, -EINVAL); | |
2437 | ||
2438 | s->connections_per_uid_max = m; | |
2439 | return 0; | |
2440 | } | |
2441 | ||
2442 | int varlink_server_set_connections_max(VarlinkServer *s, unsigned m) { | |
2443 | assert_return(s, -EINVAL); | |
2444 | assert_return(m > 0, -EINVAL); | |
2445 | ||
2446 | s->connections_max = m; | |
2447 | return 0; | |
2448 | } | |
2449 | ||
2450 | int varlink_server_set_description(VarlinkServer *s, const char *description) { | |
2451 | assert_return(s, -EINVAL); | |
2452 | ||
2453 | return free_and_strdup(&s->description, description); | |
2454 | } |