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