]>
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); | |
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. */ | |
408 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_CALLING, VARLINK_IDLE_SERVER) && v->read_disconnected) | |
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 | ||
481 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_CALLING, VARLINK_IDLE_SERVER)) | |
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 | ||
599 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_CALLING)) | |
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 | ||
676 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_CALLING)) | |
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 | ||
718 | if (error && FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) | |
719 | goto invalid; | |
720 | ||
721 | r = varlink_sanitize_parameters(¶meters); | |
722 | if (r < 0) | |
723 | goto invalid; | |
724 | ||
725 | if (v->state == VARLINK_AWAITING_REPLY) { | |
726 | varlink_set_state(v, VARLINK_PROCESSING_REPLY); | |
727 | ||
728 | if (v->reply_callback) { | |
729 | r = v->reply_callback(v, parameters, error, flags, v->userdata); | |
730 | if (r < 0) | |
731 | log_debug_errno(r, "Reply callback returned error, ignoring: %m"); | |
732 | } | |
733 | ||
734 | v->current = json_variant_unref(v->current); | |
735 | ||
736 | if (v->state == VARLINK_PROCESSING_REPLY) { | |
737 | assert(v->n_pending > 0); | |
738 | v->n_pending--; | |
739 | ||
740 | varlink_set_state(v, v->n_pending == 0 ? VARLINK_IDLE_CLIENT : VARLINK_AWAITING_REPLY); | |
741 | } | |
742 | } else { | |
743 | assert(v->state == VARLINK_CALLING); | |
744 | ||
745 | if (FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) | |
746 | goto invalid; | |
747 | ||
748 | varlink_set_state(v, VARLINK_CALLED); | |
749 | } | |
750 | ||
751 | return 1; | |
752 | ||
753 | invalid: | |
754 | varlink_set_state(v, VARLINK_PROCESSING_FAILURE); | |
755 | varlink_dispatch_local_error(v, VARLINK_ERROR_PROTOCOL); | |
756 | varlink_close(v); | |
757 | ||
758 | return 1; | |
759 | } | |
760 | ||
761 | static int varlink_dispatch_method(Varlink *v) { | |
762 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
763 | VarlinkMethodFlags flags = 0; | |
764 | const char *method = NULL, *error; | |
765 | JsonVariant *e; | |
766 | VarlinkMethod callback; | |
767 | const char *k; | |
768 | int r; | |
769 | ||
770 | assert(v); | |
771 | ||
772 | if (v->state != VARLINK_IDLE_SERVER) | |
773 | return 0; | |
774 | if (!v->current) | |
775 | return 0; | |
776 | ||
777 | if (!json_variant_is_object(v->current)) | |
778 | goto invalid; | |
779 | ||
780 | JSON_VARIANT_OBJECT_FOREACH(k, e, v->current) { | |
781 | ||
782 | if (streq(k, "method")) { | |
783 | if (method) | |
784 | goto invalid; | |
785 | if (!json_variant_is_string(e)) | |
786 | goto invalid; | |
787 | ||
788 | method = json_variant_string(e); | |
789 | ||
790 | } else if (streq(k, "parameters")) { | |
791 | if (parameters) | |
792 | goto invalid; | |
793 | if (!json_variant_is_object(e)) | |
794 | goto invalid; | |
795 | ||
796 | parameters = json_variant_ref(e); | |
797 | ||
798 | } else if (streq(k, "oneway")) { | |
799 | ||
800 | if ((flags & (VARLINK_METHOD_ONEWAY|VARLINK_METHOD_MORE)) != 0) | |
801 | goto invalid; | |
802 | ||
803 | if (!json_variant_is_boolean(e)) | |
804 | goto invalid; | |
805 | ||
806 | if (json_variant_boolean(e)) | |
807 | flags |= VARLINK_METHOD_ONEWAY; | |
808 | ||
809 | } else if (streq(k, "more")) { | |
810 | ||
811 | if ((flags & (VARLINK_METHOD_ONEWAY|VARLINK_METHOD_MORE)) != 0) | |
812 | goto invalid; | |
813 | ||
814 | if (!json_variant_is_boolean(e)) | |
815 | goto invalid; | |
816 | ||
817 | if (json_variant_boolean(e)) | |
818 | flags |= VARLINK_METHOD_MORE; | |
819 | ||
820 | } else | |
821 | goto invalid; | |
822 | } | |
823 | ||
824 | if (!method) | |
825 | goto invalid; | |
826 | ||
827 | r = varlink_sanitize_parameters(¶meters); | |
828 | if (r < 0) | |
829 | goto fail; | |
830 | ||
831 | varlink_set_state(v, (flags & VARLINK_METHOD_MORE) ? VARLINK_PROCESSING_METHOD_MORE : | |
832 | (flags & VARLINK_METHOD_ONEWAY) ? VARLINK_PROCESSING_METHOD_ONEWAY : | |
833 | VARLINK_PROCESSING_METHOD); | |
834 | ||
835 | assert(v->server); | |
836 | ||
837 | if (STR_IN_SET(method, "org.varlink.service.GetInfo", "org.varlink.service.GetInterface")) { | |
838 | /* For now, we don't implement a single of varlink's own methods */ | |
839 | callback = NULL; | |
840 | error = VARLINK_ERROR_METHOD_NOT_IMPLEMENTED; | |
841 | } else if (startswith(method, "org.varlink.service.")) { | |
842 | callback = NULL; | |
843 | error = VARLINK_ERROR_METHOD_NOT_FOUND; | |
844 | } else { | |
845 | callback = hashmap_get(v->server->methods, method); | |
846 | error = VARLINK_ERROR_METHOD_NOT_FOUND; | |
847 | } | |
848 | ||
849 | if (callback) { | |
850 | r = callback(v, parameters, flags, v->userdata); | |
851 | if (r < 0) { | |
852 | log_debug_errno(r, "Callback for %s returned error: %m", method); | |
853 | ||
854 | /* We got an error back from the callback. Propagate it to the client if the method call remains unanswered. */ | |
855 | if (!FLAGS_SET(flags, VARLINK_METHOD_ONEWAY)) { | |
856 | r = varlink_errorb(v, VARLINK_ERROR_SYSTEM, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("errno", JSON_BUILD_INTEGER(-r)))); | |
857 | if (r < 0) | |
858 | return r; | |
859 | } | |
860 | } | |
861 | } else if (!FLAGS_SET(flags, VARLINK_METHOD_ONEWAY)) { | |
862 | assert(error); | |
863 | ||
864 | r = varlink_errorb(v, error, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)))); | |
865 | if (r < 0) | |
866 | return r; | |
867 | } | |
868 | ||
869 | switch (v->state) { | |
870 | ||
871 | case VARLINK_PROCESSED_METHOD: /* Method call is fully processed */ | |
872 | case VARLINK_PROCESSING_METHOD_ONEWAY: /* ditto */ | |
873 | v->current = json_variant_unref(v->current); | |
874 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
875 | break; | |
876 | ||
877 | case VARLINK_PROCESSING_METHOD: /* Method call wasn't replied to, will be replied to later */ | |
878 | varlink_set_state(v, VARLINK_PENDING_METHOD); | |
879 | break; | |
880 | ||
881 | case VARLINK_PROCESSED_METHOD_MORE: /* One reply for a "more" message was sent, more to come */ | |
882 | case VARLINK_PROCESSING_METHOD_MORE: /* No reply for a "more" message was sent, more to come */ | |
883 | varlink_set_state(v, VARLINK_PENDING_METHOD_MORE); | |
884 | break; | |
885 | ||
886 | default: | |
887 | assert_not_reached("Unexpected state"); | |
888 | ||
889 | } | |
890 | ||
891 | return r; | |
892 | ||
893 | invalid: | |
894 | r = -EINVAL; | |
895 | ||
896 | fail: | |
897 | varlink_set_state(v, VARLINK_PROCESSING_FAILURE); | |
898 | varlink_dispatch_local_error(v, VARLINK_ERROR_PROTOCOL); | |
899 | varlink_close(v); | |
900 | ||
901 | return r; | |
902 | } | |
903 | ||
904 | int varlink_process(Varlink *v) { | |
905 | int r; | |
906 | ||
907 | assert_return(v, -EINVAL); | |
908 | ||
909 | if (v->state == VARLINK_DISCONNECTED) | |
910 | return -ENOTCONN; | |
911 | ||
912 | varlink_ref(v); | |
913 | ||
914 | r = varlink_write(v); | |
915 | if (r != 0) | |
916 | goto finish; | |
917 | ||
918 | r = varlink_dispatch_reply(v); | |
919 | if (r != 0) | |
920 | goto finish; | |
921 | ||
922 | r = varlink_dispatch_method(v); | |
923 | if (r != 0) | |
924 | goto finish; | |
925 | ||
926 | r = varlink_parse_message(v); | |
927 | if (r != 0) | |
928 | goto finish; | |
929 | ||
930 | r = varlink_read(v); | |
931 | if (r != 0) | |
932 | goto finish; | |
933 | ||
934 | r = varlink_test_disconnect(v); | |
935 | if (r != 0) | |
936 | goto finish; | |
937 | ||
938 | r = varlink_dispatch_disconnect(v); | |
939 | if (r != 0) | |
940 | goto finish; | |
941 | ||
942 | r = varlink_test_timeout(v); | |
943 | if (r != 0) | |
944 | goto finish; | |
945 | ||
946 | r = varlink_dispatch_timeout(v); | |
947 | if (r != 0) | |
948 | goto finish; | |
949 | ||
950 | finish: | |
951 | if (r >= 0 && v->defer_event_source) { | |
952 | int q; | |
953 | ||
954 | /* If we did some processing, make sure we are called again soon */ | |
955 | q = sd_event_source_set_enabled(v->defer_event_source, r > 0 ? SD_EVENT_ON : SD_EVENT_OFF); | |
956 | if (q < 0) | |
957 | r = q; | |
958 | } | |
959 | ||
960 | if (r < 0) { | |
961 | if (VARLINK_STATE_IS_ALIVE(v->state)) | |
962 | /* Initiate disconnection */ | |
963 | varlink_set_state(v, VARLINK_PENDING_DISCONNECT); | |
964 | else | |
965 | /* We failed while disconnecting, in that case close right away */ | |
966 | varlink_close(v); | |
967 | } | |
968 | ||
969 | varlink_unref(v); | |
970 | return r; | |
971 | } | |
972 | ||
973 | static void handle_revents(Varlink *v, int revents) { | |
974 | assert(v); | |
975 | ||
976 | if (v->connecting) { | |
977 | /* If we have seen POLLOUT or POLLHUP on a socket we are asynchronously waiting a connect() | |
978 | * to complete on, we know we are ready. We don't read the connection error here though, | |
979 | * we'll get the error on the next read() or write(). */ | |
980 | if ((revents & (POLLOUT|POLLHUP)) == 0) | |
981 | return; | |
982 | ||
983 | varlink_log(v, "Anynchronous connection completed."); | |
984 | v->connecting = false; | |
985 | } else { | |
986 | /* Note that we don't care much about POLLIN/POLLOUT here, we'll just try reading and writing | |
987 | * what we can. However, we do care about POLLHUP to detect connection termination even if we | |
988 | * momentarily don't want to read nor write anything. */ | |
989 | ||
990 | if (!FLAGS_SET(revents, POLLHUP)) | |
991 | return; | |
992 | ||
993 | varlink_log(v, "Got POLLHUP from socket."); | |
994 | v->got_pollhup = true; | |
995 | } | |
996 | } | |
997 | ||
998 | int varlink_wait(Varlink *v, usec_t timeout) { | |
999 | struct timespec ts; | |
1000 | struct pollfd pfd; | |
1001 | int r, fd, events; | |
1002 | usec_t t; | |
1003 | ||
1004 | assert_return(v, -EINVAL); | |
1005 | assert_return(!v->server, -ENOTTY); | |
1006 | ||
1007 | if (v->state == VARLINK_DISCONNECTED) | |
1008 | return -ENOTCONN; | |
1009 | ||
1010 | r = varlink_get_timeout(v, &t); | |
1011 | if (r < 0) | |
1012 | return r; | |
1013 | if (t != USEC_INFINITY) { | |
1014 | usec_t n; | |
1015 | ||
1016 | n = now(CLOCK_MONOTONIC); | |
1017 | if (t < n) | |
1018 | t = 0; | |
1019 | else | |
1020 | t = usec_sub_unsigned(t, n); | |
1021 | } | |
1022 | ||
1023 | if (timeout != USEC_INFINITY && | |
1024 | (t == USEC_INFINITY || timeout < t)) | |
1025 | t = timeout; | |
1026 | ||
1027 | fd = varlink_get_fd(v); | |
1028 | if (fd < 0) | |
1029 | return fd; | |
1030 | ||
1031 | events = varlink_get_events(v); | |
1032 | if (events < 0) | |
1033 | return events; | |
1034 | ||
1035 | pfd = (struct pollfd) { | |
1036 | .fd = fd, | |
1037 | .events = events, | |
1038 | }; | |
1039 | ||
1040 | r = ppoll(&pfd, 1, | |
1041 | t == USEC_INFINITY ? NULL : timespec_store(&ts, t), | |
1042 | NULL); | |
1043 | if (r < 0) | |
1044 | return -errno; | |
1045 | ||
1046 | handle_revents(v, pfd.revents); | |
1047 | ||
1048 | return r > 0 ? 1 : 0; | |
1049 | } | |
1050 | ||
1051 | int varlink_get_fd(Varlink *v) { | |
1052 | ||
1053 | assert_return(v, -EINVAL); | |
1054 | ||
1055 | if (v->state == VARLINK_DISCONNECTED) | |
1056 | return -ENOTCONN; | |
1057 | if (v->fd < 0) | |
1058 | return -EBADF; | |
1059 | ||
1060 | return v->fd; | |
1061 | } | |
1062 | ||
1063 | int varlink_get_events(Varlink *v) { | |
1064 | int ret = 0; | |
1065 | ||
1066 | assert_return(v, -EINVAL); | |
1067 | ||
1068 | if (v->state == VARLINK_DISCONNECTED) | |
1069 | return -ENOTCONN; | |
1070 | ||
1071 | if (v->connecting) /* When processing an asynchronous connect(), we only wait for EPOLLOUT, which | |
1072 | * tells us that the connection is now complete. Before that we should neither | |
1073 | * write() or read() from the fd. */ | |
1074 | return EPOLLOUT; | |
1075 | ||
1076 | if (!v->read_disconnected && | |
1077 | IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_CALLING, VARLINK_IDLE_SERVER) && | |
1078 | !v->current && | |
1079 | v->input_buffer_unscanned <= 0) | |
1080 | ret |= EPOLLIN; | |
1081 | ||
1082 | if (!v->write_disconnected && | |
1083 | v->output_buffer_size > 0) | |
1084 | ret |= EPOLLOUT; | |
1085 | ||
1086 | return ret; | |
1087 | } | |
1088 | ||
1089 | int varlink_get_timeout(Varlink *v, usec_t *ret) { | |
1090 | assert_return(v, -EINVAL); | |
1091 | ||
1092 | if (v->state == VARLINK_DISCONNECTED) | |
1093 | return -ENOTCONN; | |
1094 | ||
1095 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_CALLING) && | |
1096 | v->timeout != USEC_INFINITY) { | |
1097 | if (ret) | |
1098 | *ret = usec_add(v->timestamp, v->timeout); | |
1099 | return 1; | |
1100 | } else { | |
1101 | if (ret) | |
1102 | *ret = USEC_INFINITY; | |
1103 | return 0; | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | int varlink_flush(Varlink *v) { | |
1108 | int ret = 0, r; | |
1109 | ||
1110 | assert_return(v, -EINVAL); | |
1111 | ||
1112 | if (v->state == VARLINK_DISCONNECTED) | |
1113 | return -ENOTCONN; | |
1114 | ||
1115 | for (;;) { | |
1116 | struct pollfd pfd; | |
1117 | ||
1118 | if (v->output_buffer_size == 0) | |
1119 | break; | |
1120 | if (v->write_disconnected) | |
1121 | return -ECONNRESET; | |
1122 | ||
1123 | r = varlink_write(v); | |
1124 | if (r < 0) | |
1125 | return r; | |
1126 | if (r > 0) { | |
1127 | ret = 1; | |
1128 | continue; | |
1129 | } | |
1130 | ||
1131 | pfd = (struct pollfd) { | |
1132 | .fd = v->fd, | |
1133 | .events = POLLOUT, | |
1134 | }; | |
1135 | ||
1136 | if (poll(&pfd, 1, -1) < 0) | |
1137 | return -errno; | |
1138 | ||
1139 | handle_revents(v, pfd.revents); | |
1140 | } | |
1141 | ||
1142 | return ret; | |
1143 | } | |
1144 | ||
1145 | static void varlink_detach_server(Varlink *v) { | |
1146 | assert(v); | |
1147 | ||
1148 | if (!v->server) | |
1149 | return; | |
1150 | ||
1151 | if (v->server->by_uid && | |
1152 | v->ucred_acquired && | |
1153 | uid_is_valid(v->ucred.uid)) { | |
1154 | unsigned c; | |
1155 | ||
1156 | c = PTR_TO_UINT(hashmap_get(v->server->by_uid, UID_TO_PTR(v->ucred.uid))); | |
1157 | assert(c > 0); | |
1158 | ||
1159 | if (c == 1) | |
1160 | (void) hashmap_remove(v->server->by_uid, UID_TO_PTR(v->ucred.uid)); | |
1161 | else | |
1162 | (void) hashmap_replace(v->server->by_uid, UID_TO_PTR(v->ucred.uid), UINT_TO_PTR(c - 1)); | |
1163 | } | |
1164 | ||
1165 | assert(v->server->n_connections > 0); | |
1166 | v->server->n_connections--; | |
1167 | ||
1168 | /* If this is a connection associated to a server, then let's disconnect the server and the | |
1169 | * connection from each other. This drops the dangling reference that connect_callback() set up. */ | |
1170 | v->server = varlink_server_unref(v->server); | |
1171 | varlink_unref(v); | |
1172 | } | |
1173 | ||
1174 | int varlink_close(Varlink *v) { | |
1175 | ||
1176 | assert_return(v, -EINVAL); | |
1177 | ||
1178 | if (v->state == VARLINK_DISCONNECTED) | |
1179 | return 0; | |
1180 | ||
1181 | varlink_set_state(v, VARLINK_DISCONNECTED); | |
1182 | ||
1183 | /* Let's take a reference first, since varlink_detach_server() might drop the final (dangling) ref | |
1184 | * which would destroy us before we can call varlink_clear() */ | |
1185 | varlink_ref(v); | |
1186 | varlink_detach_server(v); | |
1187 | varlink_clear(v); | |
1188 | varlink_unref(v); | |
1189 | ||
1190 | return 1; | |
1191 | } | |
1192 | ||
1193 | Varlink* varlink_flush_close_unref(Varlink *v) { | |
1194 | ||
1195 | if (!v) | |
1196 | return NULL; | |
1197 | ||
1198 | (void) varlink_flush(v); | |
1199 | (void) varlink_close(v); | |
1200 | ||
1201 | return varlink_unref(v); | |
1202 | } | |
1203 | ||
1204 | static int varlink_enqueue_json(Varlink *v, JsonVariant *m) { | |
1205 | _cleanup_free_ char *text = NULL; | |
1206 | int r; | |
1207 | ||
1208 | assert(v); | |
1209 | assert(m); | |
1210 | ||
1211 | r = json_variant_format(m, 0, &text); | |
1212 | if (r < 0) | |
1213 | return r; | |
2a04712c | 1214 | assert(text[r] == '\0'); |
d41bd96f LP |
1215 | |
1216 | if (v->output_buffer_size + r + 1 > VARLINK_BUFFER_MAX) | |
1217 | return -ENOBUFS; | |
1218 | ||
1219 | varlink_log(v, "Sending message: %s", text); | |
1220 | ||
1221 | if (v->output_buffer_size == 0) { | |
1222 | ||
1223 | free_and_replace(v->output_buffer, text); | |
1224 | ||
1225 | v->output_buffer_size = v->output_buffer_allocated = r + 1; | |
1226 | v->output_buffer_index = 0; | |
1227 | ||
1228 | } else if (v->output_buffer_index == 0) { | |
1229 | ||
1230 | if (!GREEDY_REALLOC(v->output_buffer, v->output_buffer_allocated, v->output_buffer_size + r + 1)) | |
1231 | return -ENOMEM; | |
1232 | ||
1233 | memcpy(v->output_buffer + v->output_buffer_size, text, r + 1); | |
1234 | v->output_buffer_size += r + 1; | |
1235 | ||
1236 | } else { | |
1237 | char *n; | |
be44e091 | 1238 | const size_t new_size = v->output_buffer_size + r + 1; |
d41bd96f | 1239 | |
be44e091 | 1240 | n = new(char, new_size); |
d41bd96f LP |
1241 | if (!n) |
1242 | return -ENOMEM; | |
1243 | ||
1244 | memcpy(mempcpy(n, v->output_buffer + v->output_buffer_index, v->output_buffer_size), text, r + 1); | |
1245 | ||
1246 | free_and_replace(v->output_buffer, n); | |
be44e091 | 1247 | v->output_buffer_allocated = v->output_buffer_size = new_size; |
d41bd96f LP |
1248 | v->output_buffer_index = 0; |
1249 | } | |
1250 | ||
1251 | return 0; | |
1252 | } | |
1253 | ||
1254 | int varlink_send(Varlink *v, const char *method, JsonVariant *parameters) { | |
1255 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1256 | int r; | |
1257 | ||
1258 | assert_return(v, -EINVAL); | |
1259 | assert_return(method, -EINVAL); | |
1260 | ||
1261 | if (v->state == VARLINK_DISCONNECTED) | |
1262 | return -ENOTCONN; | |
1263 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT, VARLINK_AWAITING_REPLY)) | |
1264 | return -EBUSY; | |
1265 | ||
1266 | r = varlink_sanitize_parameters(¶meters); | |
1267 | if (r < 0) | |
1268 | return r; | |
1269 | ||
1270 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1271 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1272 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
1273 | JSON_BUILD_PAIR("oneway", JSON_BUILD_BOOLEAN(true)))); | |
1274 | if (r < 0) | |
1275 | return r; | |
1276 | ||
1277 | r = varlink_enqueue_json(v, m); | |
1278 | if (r < 0) | |
1279 | return r; | |
1280 | ||
1281 | /* No state change here, this is one-way only after all */ | |
1282 | v->timestamp = now(CLOCK_MONOTONIC); | |
1283 | return 0; | |
1284 | } | |
1285 | ||
1286 | int varlink_sendb(Varlink *v, const char *method, ...) { | |
1287 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1288 | va_list ap; | |
1289 | int r; | |
1290 | ||
1291 | assert_return(v, -EINVAL); | |
1292 | ||
1293 | va_start(ap, method); | |
1294 | r = json_buildv(¶meters, ap); | |
1295 | va_end(ap); | |
1296 | ||
1297 | if (r < 0) | |
1298 | return r; | |
1299 | ||
1300 | return varlink_send(v, method, parameters); | |
1301 | } | |
1302 | ||
1303 | int varlink_invoke(Varlink *v, const char *method, JsonVariant *parameters) { | |
1304 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1305 | int r; | |
1306 | ||
1307 | assert_return(v, -EINVAL); | |
1308 | assert_return(method, -EINVAL); | |
1309 | ||
1310 | if (v->state == VARLINK_DISCONNECTED) | |
1311 | return -ENOTCONN; | |
1312 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT, VARLINK_AWAITING_REPLY)) | |
1313 | return -EBUSY; | |
1314 | ||
1315 | r = varlink_sanitize_parameters(¶meters); | |
1316 | if (r < 0) | |
1317 | return r; | |
1318 | ||
1319 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1320 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1321 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1322 | if (r < 0) | |
1323 | return r; | |
1324 | ||
1325 | r = varlink_enqueue_json(v, m); | |
1326 | if (r < 0) | |
1327 | return r; | |
1328 | ||
1329 | varlink_set_state(v, VARLINK_AWAITING_REPLY); | |
1330 | v->n_pending++; | |
1331 | v->timestamp = now(CLOCK_MONOTONIC); | |
1332 | ||
1333 | return 0; | |
1334 | } | |
1335 | ||
1336 | int varlink_invokeb(Varlink *v, const char *method, ...) { | |
1337 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1338 | va_list ap; | |
1339 | int r; | |
1340 | ||
1341 | assert_return(v, -EINVAL); | |
1342 | ||
1343 | va_start(ap, method); | |
1344 | r = json_buildv(¶meters, ap); | |
1345 | va_end(ap); | |
1346 | ||
1347 | if (r < 0) | |
1348 | return r; | |
1349 | ||
1350 | return varlink_invoke(v, method, parameters); | |
1351 | } | |
1352 | ||
1353 | int varlink_call( | |
1354 | Varlink *v, | |
1355 | const char *method, | |
1356 | JsonVariant *parameters, | |
1357 | JsonVariant **ret_parameters, | |
1358 | const char **ret_error_id, | |
1359 | VarlinkReplyFlags *ret_flags) { | |
1360 | ||
1361 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1362 | int r; | |
1363 | ||
1364 | assert_return(v, -EINVAL); | |
1365 | assert_return(method, -EINVAL); | |
1366 | ||
1367 | if (v->state == VARLINK_DISCONNECTED) | |
1368 | return -ENOTCONN; | |
1369 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT)) | |
1370 | return -EBUSY; | |
1371 | ||
1372 | assert(v->n_pending == 0); /* n_pending can't be > 0 if we are in VARLINK_IDLE_CLIENT state */ | |
1373 | ||
1374 | r = varlink_sanitize_parameters(¶meters); | |
1375 | if (r < 0) | |
1376 | return r; | |
1377 | ||
1378 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1379 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1380 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1381 | if (r < 0) | |
1382 | return r; | |
1383 | ||
1384 | r = varlink_enqueue_json(v, m); | |
1385 | if (r < 0) | |
1386 | return r; | |
1387 | ||
1388 | varlink_set_state(v, VARLINK_CALLING); | |
1389 | v->n_pending++; | |
1390 | v->timestamp = now(CLOCK_MONOTONIC); | |
1391 | ||
1392 | while (v->state == VARLINK_CALLING) { | |
1393 | ||
1394 | r = varlink_process(v); | |
1395 | if (r < 0) | |
1396 | return r; | |
1397 | if (r > 0) | |
1398 | continue; | |
1399 | ||
1400 | r = varlink_wait(v, USEC_INFINITY); | |
1401 | if (r < 0) | |
1402 | return r; | |
1403 | } | |
1404 | ||
1405 | switch (v->state) { | |
1406 | ||
1407 | case VARLINK_CALLED: | |
1408 | assert(v->current); | |
1409 | ||
1410 | json_variant_unref(v->reply); | |
1411 | v->reply = TAKE_PTR(v->current); | |
1412 | ||
1413 | varlink_set_state(v, VARLINK_IDLE_CLIENT); | |
1414 | assert(v->n_pending == 1); | |
1415 | v->n_pending--; | |
1416 | ||
1417 | if (ret_parameters) | |
1418 | *ret_parameters = json_variant_by_key(v->reply, "parameters"); | |
1419 | if (ret_error_id) | |
1420 | *ret_error_id = json_variant_string(json_variant_by_key(v->reply, "error")); | |
1421 | if (ret_flags) | |
1422 | *ret_flags = 0; | |
1423 | ||
1424 | return 1; | |
1425 | ||
1426 | case VARLINK_PENDING_DISCONNECT: | |
1427 | case VARLINK_DISCONNECTED: | |
1428 | return -ECONNRESET; | |
1429 | ||
1430 | case VARLINK_PENDING_TIMEOUT: | |
1431 | return -ETIME; | |
1432 | ||
1433 | default: | |
1434 | assert_not_reached("Unexpected state after method call."); | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | int varlink_callb( | |
1439 | Varlink *v, | |
1440 | const char *method, | |
1441 | JsonVariant **ret_parameters, | |
1442 | const char **ret_error_id, | |
1443 | VarlinkReplyFlags *ret_flags, ...) { | |
1444 | ||
1445 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1446 | va_list ap; | |
1447 | int r; | |
1448 | ||
1449 | assert_return(v, -EINVAL); | |
1450 | ||
1451 | va_start(ap, ret_flags); | |
1452 | r = json_buildv(¶meters, ap); | |
1453 | va_end(ap); | |
1454 | ||
1455 | if (r < 0) | |
1456 | return r; | |
1457 | ||
1458 | return varlink_call(v, method, parameters, ret_parameters, ret_error_id, ret_flags); | |
1459 | } | |
1460 | ||
1461 | int varlink_reply(Varlink *v, JsonVariant *parameters) { | |
1462 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1463 | int r; | |
1464 | ||
1465 | assert_return(v, -EINVAL); | |
1466 | ||
1467 | if (v->state == VARLINK_DISCONNECTED) | |
1468 | return -ENOTCONN; | |
1469 | if (!IN_SET(v->state, | |
1470 | VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE, | |
1471 | VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) | |
1472 | return -EBUSY; | |
1473 | ||
1474 | r = varlink_sanitize_parameters(¶meters); | |
1475 | if (r < 0) | |
1476 | return r; | |
1477 | ||
1478 | r = json_build(&m, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1479 | if (r < 0) | |
1480 | return r; | |
1481 | ||
1482 | r = varlink_enqueue_json(v, m); | |
1483 | if (r < 0) | |
1484 | return r; | |
1485 | ||
1486 | if (IN_SET(v->state, VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) { | |
1487 | /* We just replied to a method call that was let hanging for a while (i.e. we were outside of | |
1488 | * the varlink_dispatch_method() stack frame), which means with this reply we are ready to | |
1489 | * process further messages. */ | |
1490 | v->current = json_variant_unref(v->current); | |
1491 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
1492 | } else | |
1493 | /* We replied to a method call from within the varlink_dispatch_method() stack frame), which | |
1494 | * means we should it handle the rest of the state engine. */ | |
1495 | varlink_set_state(v, VARLINK_PROCESSED_METHOD); | |
1496 | ||
1497 | return 1; | |
1498 | } | |
1499 | ||
1500 | int varlink_replyb(Varlink *v, ...) { | |
1501 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1502 | va_list ap; | |
1503 | int r; | |
1504 | ||
1505 | assert_return(v, -EINVAL); | |
1506 | ||
1507 | va_start(ap, v); | |
1508 | r = json_buildv(¶meters, ap); | |
1509 | va_end(ap); | |
1510 | ||
1511 | if (r < 0) | |
1512 | return r; | |
1513 | ||
1514 | return varlink_reply(v, parameters); | |
1515 | } | |
1516 | ||
1517 | int varlink_error(Varlink *v, const char *error_id, JsonVariant *parameters) { | |
1518 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1519 | int r; | |
1520 | ||
1521 | assert_return(v, -EINVAL); | |
1522 | assert_return(error_id, -EINVAL); | |
1523 | ||
1524 | if (v->state == VARLINK_DISCONNECTED) | |
1525 | return -ENOTCONN; | |
1526 | if (!IN_SET(v->state, | |
1527 | VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE, | |
1528 | VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) | |
1529 | return -EBUSY; | |
1530 | ||
1531 | r = varlink_sanitize_parameters(¶meters); | |
1532 | if (r < 0) | |
1533 | return r; | |
1534 | ||
1535 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1536 | JSON_BUILD_PAIR("error", JSON_BUILD_STRING(error_id)), | |
1537 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1538 | if (r < 0) | |
1539 | return r; | |
1540 | ||
1541 | r = varlink_enqueue_json(v, m); | |
1542 | if (r < 0) | |
1543 | return r; | |
1544 | ||
1545 | if (IN_SET(v->state, VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) { | |
1546 | v->current = json_variant_unref(v->current); | |
1547 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
1548 | } else | |
1549 | varlink_set_state(v, VARLINK_PROCESSED_METHOD); | |
1550 | ||
1551 | return 1; | |
1552 | } | |
1553 | ||
1554 | int varlink_errorb(Varlink *v, const char *error_id, ...) { | |
1555 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1556 | va_list ap; | |
1557 | int r; | |
1558 | ||
1559 | assert_return(v, -EINVAL); | |
1560 | assert_return(error_id, -EINVAL); | |
1561 | ||
1562 | va_start(ap, error_id); | |
1563 | r = json_buildv(¶meters, ap); | |
1564 | va_end(ap); | |
1565 | ||
1566 | if (r < 0) | |
1567 | return r; | |
1568 | ||
1569 | return varlink_error(v, error_id, parameters); | |
1570 | } | |
1571 | ||
1572 | int varlink_error_invalid_parameter(Varlink *v, JsonVariant *parameters) { | |
1573 | ||
1574 | assert_return(v, -EINVAL); | |
1575 | assert_return(parameters, -EINVAL); | |
1576 | ||
1577 | /* We expect to be called in one of two ways: the 'parameters' argument is a string variant in which | |
1578 | * case it is the parameter key name that is invalid. Or the 'parameters' argument is an object | |
1579 | * variant in which case we'll pull out the first key. The latter mode is useful in functions that | |
1580 | * don't expect any arguments. */ | |
1581 | ||
1582 | if (json_variant_is_string(parameters)) | |
1583 | return varlink_error(v, VARLINK_ERROR_INVALID_PARAMETER, parameters); | |
1584 | ||
1585 | if (json_variant_is_object(parameters) && | |
1586 | json_variant_elements(parameters) > 0) | |
1587 | return varlink_error(v, VARLINK_ERROR_INVALID_PARAMETER, | |
1588 | json_variant_by_index(parameters, 0)); | |
1589 | ||
1590 | return -EINVAL; | |
1591 | } | |
1592 | ||
1593 | int varlink_notify(Varlink *v, JsonVariant *parameters) { | |
1594 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1595 | int r; | |
1596 | ||
1597 | assert_return(v, -EINVAL); | |
1598 | ||
1599 | if (v->state == VARLINK_DISCONNECTED) | |
1600 | return -ENOTCONN; | |
1601 | if (!IN_SET(v->state, VARLINK_PROCESSING_METHOD_MORE, VARLINK_PENDING_METHOD_MORE)) | |
1602 | return -EBUSY; | |
1603 | ||
1604 | r = varlink_sanitize_parameters(¶meters); | |
1605 | if (r < 0) | |
1606 | return r; | |
1607 | ||
1608 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1609 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
1610 | JSON_BUILD_PAIR("continues", JSON_BUILD_BOOLEAN(true)))); | |
1611 | if (r < 0) | |
1612 | return r; | |
1613 | ||
1614 | r = varlink_enqueue_json(v, m); | |
1615 | if (r < 0) | |
1616 | return r; | |
1617 | ||
1618 | /* No state change, as more is coming */ | |
1619 | return 1; | |
1620 | } | |
1621 | ||
1622 | int varlink_notifyb(Varlink *v, ...) { | |
1623 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1624 | va_list ap; | |
1625 | int r; | |
1626 | ||
1627 | assert_return(v, -EINVAL); | |
1628 | ||
1629 | va_start(ap, v); | |
1630 | r = json_buildv(¶meters, ap); | |
1631 | va_end(ap); | |
1632 | ||
1633 | if (r < 0) | |
1634 | return r; | |
1635 | ||
1636 | return varlink_notify(v, parameters); | |
1637 | } | |
1638 | ||
1639 | int varlink_bind_reply(Varlink *v, VarlinkReply callback) { | |
1640 | assert_return(v, -EINVAL); | |
1641 | ||
1642 | if (callback && v->reply_callback && callback != v->reply_callback) | |
1643 | return -EBUSY; | |
1644 | ||
1645 | v->reply_callback = callback; | |
1646 | ||
1647 | return 0; | |
1648 | } | |
1649 | ||
1650 | void* varlink_set_userdata(Varlink *v, void *userdata) { | |
1651 | void *old; | |
1652 | ||
1653 | assert_return(v, NULL); | |
1654 | ||
1655 | old = v->userdata; | |
1656 | v->userdata = userdata; | |
1657 | ||
1658 | return old; | |
1659 | } | |
1660 | ||
1661 | void* varlink_get_userdata(Varlink *v) { | |
1662 | assert_return(v, NULL); | |
1663 | ||
1664 | return v->userdata; | |
1665 | } | |
1666 | ||
1667 | static int varlink_acquire_ucred(Varlink *v) { | |
1668 | int r; | |
1669 | ||
1670 | assert(v); | |
1671 | ||
1672 | if (v->ucred_acquired) | |
1673 | return 0; | |
1674 | ||
1675 | r = getpeercred(v->fd, &v->ucred); | |
1676 | if (r < 0) | |
1677 | return r; | |
1678 | ||
1679 | v->ucred_acquired = true; | |
1680 | return 0; | |
1681 | } | |
1682 | ||
1683 | int varlink_get_peer_uid(Varlink *v, uid_t *ret) { | |
1684 | int r; | |
1685 | ||
1686 | assert_return(v, -EINVAL); | |
1687 | assert_return(ret, -EINVAL); | |
1688 | ||
1689 | r = varlink_acquire_ucred(v); | |
1690 | if (r < 0) | |
1691 | return r; | |
1692 | ||
1693 | if (!uid_is_valid(v->ucred.uid)) | |
1694 | return -ENODATA; | |
1695 | ||
1696 | *ret = v->ucred.uid; | |
1697 | return 0; | |
1698 | } | |
1699 | ||
1700 | int varlink_get_peer_pid(Varlink *v, pid_t *ret) { | |
1701 | int r; | |
1702 | ||
1703 | assert_return(v, -EINVAL); | |
1704 | assert_return(ret, -EINVAL); | |
1705 | ||
1706 | r = varlink_acquire_ucred(v); | |
1707 | if (r < 0) | |
1708 | return r; | |
1709 | ||
1710 | if (!pid_is_valid(v->ucred.pid)) | |
1711 | return -ENODATA; | |
1712 | ||
1713 | *ret = v->ucred.pid; | |
1714 | return 0; | |
1715 | } | |
1716 | ||
1717 | int varlink_set_relative_timeout(Varlink *v, usec_t timeout) { | |
1718 | assert_return(v, -EINVAL); | |
1719 | assert_return(timeout > 0, -EINVAL); | |
1720 | ||
1721 | v->timeout = timeout; | |
1722 | return 0; | |
1723 | } | |
1724 | ||
1725 | VarlinkServer *varlink_get_server(Varlink *v) { | |
1726 | assert_return(v, NULL); | |
1727 | ||
1728 | return v->server; | |
1729 | } | |
1730 | ||
1731 | int varlink_set_description(Varlink *v, const char *description) { | |
1732 | assert_return(v, -EINVAL); | |
1733 | ||
1734 | return free_and_strdup(&v->description, description); | |
1735 | } | |
1736 | ||
1737 | static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
1738 | Varlink *v = userdata; | |
1739 | ||
1740 | assert(s); | |
1741 | assert(v); | |
1742 | ||
1743 | handle_revents(v, revents); | |
1744 | (void) varlink_process(v); | |
1745 | ||
1746 | return 1; | |
1747 | } | |
1748 | ||
1749 | static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) { | |
1750 | Varlink *v = userdata; | |
1751 | ||
1752 | assert(s); | |
1753 | assert(v); | |
1754 | ||
1755 | (void) varlink_process(v); | |
1756 | return 1; | |
1757 | } | |
1758 | ||
1759 | static int defer_callback(sd_event_source *s, void *userdata) { | |
1760 | Varlink *v = userdata; | |
1761 | ||
1762 | assert(s); | |
1763 | assert(v); | |
1764 | ||
1765 | (void) varlink_process(v); | |
1766 | return 1; | |
1767 | } | |
1768 | ||
1769 | static int prepare_callback(sd_event_source *s, void *userdata) { | |
1770 | Varlink *v = userdata; | |
1771 | int r, e; | |
1772 | usec_t until; | |
1773 | ||
1774 | assert(s); | |
1775 | assert(v); | |
1776 | ||
1777 | e = varlink_get_events(v); | |
1778 | if (e < 0) | |
1779 | return e; | |
1780 | ||
1781 | r = sd_event_source_set_io_events(v->io_event_source, e); | |
1782 | if (r < 0) | |
1783 | return r; | |
1784 | ||
1785 | r = varlink_get_timeout(v, &until); | |
1786 | if (r < 0) | |
1787 | return r; | |
1788 | if (r > 0) { | |
1789 | r = sd_event_source_set_time(v->time_event_source, until); | |
1790 | if (r < 0) | |
1791 | return r; | |
1792 | } | |
1793 | ||
1794 | r = sd_event_source_set_enabled(v->time_event_source, r > 0 ? SD_EVENT_ON : SD_EVENT_OFF); | |
1795 | if (r < 0) | |
1796 | return r; | |
1797 | ||
1798 | return 1; | |
1799 | } | |
1800 | ||
1801 | static int quit_callback(sd_event_source *event, void *userdata) { | |
1802 | Varlink *v = userdata; | |
1803 | ||
1804 | assert(event); | |
1805 | assert(v); | |
1806 | ||
1807 | varlink_flush(v); | |
1808 | varlink_close(v); | |
1809 | ||
1810 | return 1; | |
1811 | } | |
1812 | ||
1813 | int varlink_attach_event(Varlink *v, sd_event *e, int64_t priority) { | |
1814 | int r; | |
1815 | ||
1816 | assert_return(v, -EINVAL); | |
1817 | assert_return(!v->event, -EBUSY); | |
1818 | ||
1819 | if (e) | |
1820 | v->event = sd_event_ref(e); | |
1821 | else { | |
1822 | r = sd_event_default(&v->event); | |
1823 | if (r < 0) | |
1824 | return r; | |
1825 | } | |
1826 | ||
1827 | r = sd_event_add_time(v->event, &v->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, v); | |
1828 | if (r < 0) | |
1829 | goto fail; | |
1830 | ||
1831 | r = sd_event_source_set_priority(v->time_event_source, priority); | |
1832 | if (r < 0) | |
1833 | goto fail; | |
1834 | ||
1835 | (void) sd_event_source_set_description(v->time_event_source, "varlink-time"); | |
1836 | ||
1837 | r = sd_event_add_exit(v->event, &v->quit_event_source, quit_callback, v); | |
1838 | if (r < 0) | |
1839 | goto fail; | |
1840 | ||
1841 | r = sd_event_source_set_priority(v->quit_event_source, priority); | |
1842 | if (r < 0) | |
1843 | goto fail; | |
1844 | ||
1845 | (void) sd_event_source_set_description(v->quit_event_source, "varlink-quit"); | |
1846 | ||
1847 | r = sd_event_add_io(v->event, &v->io_event_source, v->fd, 0, io_callback, v); | |
1848 | if (r < 0) | |
1849 | goto fail; | |
1850 | ||
1851 | r = sd_event_source_set_prepare(v->io_event_source, prepare_callback); | |
1852 | if (r < 0) | |
1853 | goto fail; | |
1854 | ||
1855 | r = sd_event_source_set_priority(v->io_event_source, priority); | |
1856 | if (r < 0) | |
1857 | goto fail; | |
1858 | ||
1859 | (void) sd_event_source_set_description(v->io_event_source, "varlink-io"); | |
1860 | ||
1861 | r = sd_event_add_defer(v->event, &v->defer_event_source, defer_callback, v); | |
1862 | if (r < 0) | |
1863 | goto fail; | |
1864 | ||
1865 | r = sd_event_source_set_priority(v->defer_event_source, priority); | |
1866 | if (r < 0) | |
1867 | goto fail; | |
1868 | ||
1869 | (void) sd_event_source_set_description(v->defer_event_source, "varlink-defer"); | |
1870 | ||
1871 | return 0; | |
1872 | ||
1873 | fail: | |
1874 | varlink_detach_event(v); | |
1875 | return r; | |
1876 | } | |
1877 | ||
1878 | ||
1879 | void varlink_detach_event(Varlink *v) { | |
1880 | if (!v) | |
1881 | return; | |
1882 | ||
1883 | varlink_detach_event_sources(v); | |
1884 | ||
1885 | v->event = sd_event_unref(v->event); | |
1886 | } | |
1887 | ||
1888 | sd_event *varlink_get_event(Varlink *v) { | |
1889 | assert_return(v, NULL); | |
1890 | ||
1891 | return v->event; | |
1892 | } | |
1893 | ||
1894 | int varlink_server_new(VarlinkServer **ret, VarlinkServerFlags flags) { | |
1895 | VarlinkServer *s; | |
1896 | ||
1897 | assert_return(ret, -EINVAL); | |
1898 | assert_return((flags & ~_VARLINK_SERVER_FLAGS_ALL) == 0, -EINVAL); | |
1899 | ||
1900 | s = new(VarlinkServer, 1); | |
1901 | if (!s) | |
1902 | return -ENOMEM; | |
1903 | ||
1904 | *s = (VarlinkServer) { | |
1905 | .n_ref = 1, | |
1906 | .flags = flags, | |
1907 | .connections_max = varlink_server_connections_max(NULL), | |
1908 | .connections_per_uid_max = varlink_server_connections_per_uid_max(NULL), | |
1909 | }; | |
1910 | ||
1911 | *ret = s; | |
1912 | return 0; | |
1913 | } | |
1914 | ||
1915 | static VarlinkServer* varlink_server_destroy(VarlinkServer *s) { | |
1916 | char *m; | |
1917 | ||
1918 | if (!s) | |
1919 | return NULL; | |
1920 | ||
1921 | varlink_server_shutdown(s); | |
1922 | ||
1923 | while ((m = hashmap_steal_first_key(s->methods))) | |
1924 | free(m); | |
1925 | ||
1926 | hashmap_free(s->methods); | |
1927 | hashmap_free(s->by_uid); | |
1928 | ||
1929 | sd_event_unref(s->event); | |
1930 | ||
1931 | free(s->description); | |
1932 | ||
1933 | return mfree(s); | |
1934 | } | |
1935 | ||
1936 | DEFINE_TRIVIAL_REF_UNREF_FUNC(VarlinkServer, varlink_server, varlink_server_destroy); | |
1937 | ||
1938 | static int validate_connection(VarlinkServer *server, const struct ucred *ucred) { | |
1939 | int allowed = -1; | |
1940 | ||
1941 | assert(server); | |
1942 | assert(ucred); | |
1943 | ||
1944 | if (FLAGS_SET(server->flags, VARLINK_SERVER_ROOT_ONLY)) | |
1945 | allowed = ucred->uid == 0; | |
1946 | ||
1947 | if (FLAGS_SET(server->flags, VARLINK_SERVER_MYSELF_ONLY)) | |
1948 | allowed = allowed > 0 || ucred->uid == getuid(); | |
1949 | ||
1950 | if (allowed == 0) { /* Allow access when it is explicitly allowed or when neither | |
1951 | * VARLINK_SERVER_ROOT_ONLY nor VARLINK_SERVER_MYSELF_ONLY are specified. */ | |
1952 | varlink_server_log(server, "Unprivileged client attempted connection, refusing."); | |
1953 | return 0; | |
1954 | } | |
1955 | ||
1956 | if (server->n_connections >= server->connections_max) { | |
1957 | varlink_server_log(server, "Connection limit of %u reached, refusing.", server->connections_max); | |
1958 | return 0; | |
1959 | } | |
1960 | ||
1961 | if (FLAGS_SET(server->flags, VARLINK_SERVER_ACCOUNT_UID)) { | |
1962 | unsigned c; | |
1963 | ||
1964 | if (!uid_is_valid(ucred->uid)) { | |
1965 | varlink_server_log(server, "Client with invalid UID attempted connection, refusing."); | |
1966 | return 0; | |
1967 | } | |
1968 | ||
1969 | c = PTR_TO_UINT(hashmap_get(server->by_uid, UID_TO_PTR(ucred->uid))); | |
1970 | if (c >= server->connections_per_uid_max) { | |
1971 | varlink_server_log(server, "Per-UID connection limit of %u reached, refusing.", | |
1972 | server->connections_per_uid_max); | |
1973 | return 0; | |
1974 | } | |
1975 | } | |
1976 | ||
1977 | return 1; | |
1978 | } | |
1979 | ||
1980 | static int count_connection(VarlinkServer *server, struct ucred *ucred) { | |
1981 | unsigned c; | |
1982 | int r; | |
1983 | ||
1984 | assert(server); | |
1985 | assert(ucred); | |
1986 | ||
1987 | server->n_connections++; | |
1988 | ||
1989 | if (FLAGS_SET(server->flags, VARLINK_SERVER_ACCOUNT_UID)) { | |
1990 | r = hashmap_ensure_allocated(&server->by_uid, NULL); | |
1991 | if (r < 0) | |
1992 | return log_debug_errno(r, "Failed to allocate UID hash table: %m"); | |
1993 | ||
1994 | c = PTR_TO_UINT(hashmap_get(server->by_uid, UID_TO_PTR(ucred->uid))); | |
1995 | ||
1996 | varlink_server_log(server, "Connections of user " UID_FMT ": %u (of %u max)", | |
1997 | ucred->uid, c, server->connections_per_uid_max); | |
1998 | ||
1999 | r = hashmap_replace(server->by_uid, UID_TO_PTR(ucred->uid), UINT_TO_PTR(c + 1)); | |
2000 | if (r < 0) | |
2001 | return log_debug_errno(r, "Failed to increment counter in UID hash table: %m"); | |
2002 | } | |
2003 | ||
2004 | return 0; | |
2005 | } | |
2006 | ||
2007 | int varlink_server_add_connection(VarlinkServer *server, int fd, Varlink **ret) { | |
2008 | _cleanup_(varlink_unrefp) Varlink *v = NULL; | |
2009 | bool ucred_acquired; | |
2010 | struct ucred ucred; | |
2011 | int r; | |
2012 | ||
2013 | assert_return(server, -EINVAL); | |
2014 | assert_return(fd >= 0, -EBADF); | |
2015 | ||
2016 | if ((server->flags & (VARLINK_SERVER_ROOT_ONLY|VARLINK_SERVER_ACCOUNT_UID)) != 0) { | |
2017 | r = getpeercred(fd, &ucred); | |
2018 | if (r < 0) | |
2019 | return varlink_server_log_errno(server, r, "Failed to acquire peer credentials of incoming socket, refusing: %m"); | |
2020 | ||
2021 | ucred_acquired = true; | |
2022 | ||
2023 | r = validate_connection(server, &ucred); | |
2024 | if (r < 0) | |
2025 | return r; | |
2026 | if (r == 0) | |
2027 | return -EPERM; | |
2028 | } else | |
2029 | ucred_acquired = false; | |
2030 | ||
2031 | r = varlink_new(&v); | |
2032 | if (r < 0) | |
2033 | return varlink_server_log_errno(server, r, "Failed to allocate connection object: %m"); | |
2034 | ||
2035 | r = count_connection(server, &ucred); | |
2036 | if (r < 0) | |
2037 | return r; | |
2038 | ||
2039 | v->fd = fd; | |
2040 | v->userdata = server->userdata; | |
2041 | if (ucred_acquired) { | |
2042 | v->ucred = ucred; | |
2043 | v->ucred_acquired = true; | |
2044 | } | |
2045 | ||
2046 | (void) asprintf(&v->description, "%s-%i", server->description ?: "varlink", v->fd); | |
2047 | ||
2048 | /* Link up the server and the connection, and take reference in both directions. Note that the | |
2049 | * reference on the connection is left dangling. It will be dropped when the connection is closed, | |
2050 | * which happens in varlink_close(), including in the event loop quit callback. */ | |
2051 | v->server = varlink_server_ref(server); | |
2052 | varlink_ref(v); | |
2053 | ||
2054 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
2055 | ||
2056 | r = varlink_attach_event(v, server->event, server->event_priority); | |
2057 | if (r < 0) { | |
2058 | varlink_log_errno(v, r, "Failed to attach new connection: %m"); | |
2059 | v->fd = -1; /* take the fd out of the connection again */ | |
2060 | varlink_close(v); | |
2061 | return r; | |
2062 | } | |
2063 | ||
2064 | if (ret) | |
2065 | *ret = v; | |
2066 | ||
2067 | return 0; | |
2068 | } | |
2069 | ||
2070 | static int connect_callback(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |
2071 | VarlinkServerSocket *ss = userdata; | |
2072 | _cleanup_close_ int cfd = -1; | |
2073 | Varlink *v = NULL; | |
2074 | int r; | |
2075 | ||
2076 | assert(source); | |
2077 | assert(ss); | |
2078 | ||
2079 | varlink_server_log(ss->server, "New incoming connection."); | |
2080 | ||
2081 | cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); | |
2082 | if (cfd < 0) { | |
2083 | if (ERRNO_IS_ACCEPT_AGAIN(errno)) | |
2084 | return 0; | |
2085 | ||
2086 | return varlink_server_log_errno(ss->server, errno, "Failed to accept incoming socket: %m"); | |
2087 | } | |
2088 | ||
2089 | r = varlink_server_add_connection(ss->server, cfd, &v); | |
2090 | if (r < 0) | |
2091 | return 0; | |
2092 | ||
2093 | TAKE_FD(cfd); | |
2094 | ||
2095 | if (ss->server->connect_callback) { | |
2096 | r = ss->server->connect_callback(ss->server, v, ss->server->userdata); | |
2097 | if (r < 0) { | |
2098 | varlink_log_errno(v, r, "Connection callback returned error, disconnecting client: %m"); | |
2099 | varlink_close(v); | |
2100 | return 0; | |
2101 | } | |
2102 | } | |
2103 | ||
2104 | return 0; | |
2105 | } | |
2106 | ||
2107 | int varlink_server_listen_fd(VarlinkServer *s, int fd) { | |
2108 | _cleanup_free_ VarlinkServerSocket *ss = NULL; | |
2109 | int r; | |
2110 | ||
2111 | assert_return(s, -EINVAL); | |
2112 | assert_return(fd >= 0, -EBADF); | |
2113 | ||
2114 | r = fd_nonblock(fd, true); | |
2115 | if (r < 0) | |
2116 | return r; | |
2117 | ||
2118 | ss = new(VarlinkServerSocket, 1); | |
2119 | if (!ss) | |
2120 | return -ENOMEM; | |
2121 | ||
2122 | *ss = (VarlinkServerSocket) { | |
2123 | .server = s, | |
2124 | .fd = fd, | |
2125 | }; | |
2126 | ||
2127 | if (s->event) { | |
2128 | _cleanup_(sd_event_source_unrefp) sd_event_source *es = NULL; | |
2129 | ||
2130 | r = sd_event_add_io(s->event, &es, fd, EPOLLIN, connect_callback, ss); | |
2131 | if (r < 0) | |
2132 | return r; | |
2133 | ||
2134 | r = sd_event_source_set_priority(ss->event_source, s->event_priority); | |
2135 | if (r < 0) | |
2136 | return r; | |
2137 | } | |
2138 | ||
2139 | LIST_PREPEND(sockets, s->sockets, TAKE_PTR(ss)); | |
2140 | return 0; | |
2141 | } | |
2142 | ||
2143 | int varlink_server_listen_address(VarlinkServer *s, const char *address, mode_t m) { | |
2144 | union sockaddr_union sockaddr; | |
2145 | _cleanup_close_ int fd = -1; | |
2146 | int r; | |
2147 | ||
2148 | assert_return(s, -EINVAL); | |
2149 | assert_return(address, -EINVAL); | |
2150 | assert_return((m & ~0777) == 0, -EINVAL); | |
2151 | ||
2152 | r = sockaddr_un_set_path(&sockaddr.un, address); | |
2153 | if (r < 0) | |
2154 | return r; | |
2155 | ||
2156 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
2157 | if (fd < 0) | |
2158 | return -errno; | |
2159 | ||
2160 | (void) sockaddr_un_unlink(&sockaddr.un); | |
2161 | ||
2162 | RUN_WITH_UMASK(~m & 0777) | |
2163 | if (bind(fd, &sockaddr.sa, SOCKADDR_UN_LEN(sockaddr.un)) < 0) | |
2164 | return -errno; | |
2165 | ||
2166 | if (listen(fd, SOMAXCONN) < 0) | |
2167 | return -errno; | |
2168 | ||
2169 | r = varlink_server_listen_fd(s, fd); | |
2170 | if (r < 0) | |
2171 | return r; | |
2172 | ||
2173 | TAKE_FD(fd); | |
2174 | return 0; | |
2175 | } | |
2176 | ||
2177 | void* varlink_server_set_userdata(VarlinkServer *s, void *userdata) { | |
2178 | void *ret; | |
2179 | ||
2180 | assert_return(s, NULL); | |
2181 | ||
2182 | ret = s->userdata; | |
2183 | s->userdata = userdata; | |
2184 | ||
2185 | return ret; | |
2186 | } | |
2187 | ||
2188 | void* varlink_server_get_userdata(VarlinkServer *s) { | |
2189 | assert_return(s, NULL); | |
2190 | ||
2191 | return s->userdata; | |
2192 | } | |
2193 | ||
2194 | static VarlinkServerSocket* varlink_server_socket_destroy(VarlinkServerSocket *ss) { | |
2195 | if (!ss) | |
2196 | return NULL; | |
2197 | ||
2198 | if (ss->server) | |
2199 | LIST_REMOVE(sockets, ss->server->sockets, ss); | |
2200 | ||
1d3fe304 | 2201 | sd_event_source_disable_unref(ss->event_source); |
d41bd96f LP |
2202 | |
2203 | free(ss->address); | |
2204 | safe_close(ss->fd); | |
2205 | ||
2206 | return mfree(ss); | |
2207 | } | |
2208 | ||
2209 | int varlink_server_shutdown(VarlinkServer *s) { | |
2210 | assert_return(s, -EINVAL); | |
2211 | ||
2212 | while (s->sockets) | |
2213 | varlink_server_socket_destroy(s->sockets); | |
2214 | ||
2215 | return 0; | |
2216 | } | |
2217 | ||
2218 | int varlink_server_attach_event(VarlinkServer *s, sd_event *e, int64_t priority) { | |
2219 | VarlinkServerSocket *ss; | |
2220 | int r; | |
2221 | ||
2222 | assert_return(s, -EINVAL); | |
2223 | assert_return(!s->event, -EBUSY); | |
2224 | ||
2225 | if (e) | |
2226 | s->event = sd_event_ref(e); | |
2227 | else { | |
2228 | r = sd_event_default(&s->event); | |
2229 | if (r < 0) | |
2230 | return r; | |
2231 | } | |
2232 | ||
2233 | LIST_FOREACH(sockets, ss, s->sockets) { | |
2234 | assert(!ss->event_source); | |
2235 | ||
2236 | r = sd_event_add_io(s->event, &ss->event_source, ss->fd, EPOLLIN, connect_callback, ss); | |
2237 | if (r < 0) | |
2238 | goto fail; | |
2239 | ||
2240 | r = sd_event_source_set_priority(ss->event_source, priority); | |
2241 | if (r < 0) | |
2242 | goto fail; | |
2243 | } | |
2244 | ||
2245 | s->event_priority = priority; | |
2246 | return 0; | |
2247 | ||
2248 | fail: | |
2249 | varlink_server_detach_event(s); | |
2250 | return r; | |
2251 | } | |
2252 | ||
2253 | int varlink_server_detach_event(VarlinkServer *s) { | |
2254 | VarlinkServerSocket *ss; | |
2255 | ||
2256 | assert_return(s, -EINVAL); | |
2257 | ||
2258 | LIST_FOREACH(sockets, ss, s->sockets) { | |
2259 | ||
2260 | if (!ss->event_source) | |
2261 | continue; | |
2262 | ||
2263 | (void) sd_event_source_set_enabled(ss->event_source, SD_EVENT_OFF); | |
2264 | ss->event_source = sd_event_source_unref(ss->event_source); | |
2265 | } | |
2266 | ||
2267 | sd_event_unref(s->event); | |
2268 | return 0; | |
2269 | } | |
2270 | ||
2271 | sd_event *varlink_server_get_event(VarlinkServer *s) { | |
2272 | assert_return(s, NULL); | |
2273 | ||
2274 | return s->event; | |
2275 | } | |
2276 | ||
2277 | int varlink_server_bind_method(VarlinkServer *s, const char *method, VarlinkMethod callback) { | |
2278 | char *m; | |
2279 | int r; | |
2280 | ||
2281 | assert_return(s, -EINVAL); | |
2282 | assert_return(method, -EINVAL); | |
2283 | assert_return(callback, -EINVAL); | |
2284 | ||
2285 | if (startswith(method, "org.varlink.service.")) | |
2286 | return -EEXIST; | |
2287 | ||
2288 | r = hashmap_ensure_allocated(&s->methods, &string_hash_ops); | |
2289 | if (r < 0) | |
2290 | return r; | |
2291 | ||
2292 | m = strdup(method); | |
2293 | if (!m) | |
2294 | return -ENOMEM; | |
2295 | ||
2296 | r = hashmap_put(s->methods, m, callback); | |
2297 | if (r < 0) { | |
2298 | free(m); | |
2299 | return r; | |
2300 | } | |
2301 | ||
2302 | return 0; | |
2303 | } | |
2304 | ||
2305 | int varlink_server_bind_method_many_internal(VarlinkServer *s, ...) { | |
2306 | va_list ap; | |
e7b93f97 | 2307 | int r = 0; |
d41bd96f LP |
2308 | |
2309 | assert_return(s, -EINVAL); | |
2310 | ||
2311 | va_start(ap, s); | |
2312 | for (;;) { | |
2313 | VarlinkMethod callback; | |
2314 | const char *method; | |
2315 | ||
2316 | method = va_arg(ap, const char *); | |
2317 | if (!method) | |
2318 | break; | |
2319 | ||
2320 | callback = va_arg(ap, VarlinkMethod); | |
2321 | ||
2322 | r = varlink_server_bind_method(s, method, callback); | |
2323 | if (r < 0) | |
e7b93f97 | 2324 | break; |
d41bd96f | 2325 | } |
e7b93f97 | 2326 | va_end(ap); |
d41bd96f | 2327 | |
e7b93f97 | 2328 | return r; |
d41bd96f LP |
2329 | } |
2330 | ||
2331 | int varlink_server_bind_connect(VarlinkServer *s, VarlinkConnect callback) { | |
2332 | assert_return(s, -EINVAL); | |
2333 | ||
2334 | if (callback && s->connect_callback && callback != s->connect_callback) | |
2335 | return -EBUSY; | |
2336 | ||
2337 | s->connect_callback = callback; | |
2338 | return 0; | |
2339 | } | |
2340 | ||
2341 | unsigned varlink_server_connections_max(VarlinkServer *s) { | |
2342 | struct rlimit rl; | |
2343 | ||
2344 | /* If a server is specified, return the setting for that server, otherwise the default value */ | |
2345 | if (s) | |
2346 | return s->connections_max; | |
2347 | ||
2348 | assert_se(getrlimit(RLIMIT_NOFILE, &rl) >= 0); | |
2349 | ||
2350 | /* Make sure we never use up more than ¾th of RLIMIT_NOFILE for IPC */ | |
2351 | if (VARLINK_DEFAULT_CONNECTIONS_MAX > rl.rlim_cur / 4 * 3) | |
2352 | return rl.rlim_cur / 4 * 3; | |
2353 | ||
2354 | return VARLINK_DEFAULT_CONNECTIONS_MAX; | |
2355 | } | |
2356 | ||
2357 | unsigned varlink_server_connections_per_uid_max(VarlinkServer *s) { | |
2358 | unsigned m; | |
2359 | ||
2360 | if (s) | |
2361 | return s->connections_per_uid_max; | |
2362 | ||
2363 | /* Make sure to never use up more than ¾th of available connections for a single user */ | |
2364 | m = varlink_server_connections_max(NULL); | |
2365 | if (VARLINK_DEFAULT_CONNECTIONS_PER_UID_MAX > m) | |
2366 | return m / 4 * 3; | |
2367 | ||
2368 | return VARLINK_DEFAULT_CONNECTIONS_PER_UID_MAX; | |
2369 | } | |
2370 | ||
2371 | int varlink_server_set_connections_per_uid_max(VarlinkServer *s, unsigned m) { | |
2372 | assert_return(s, -EINVAL); | |
2373 | assert_return(m > 0, -EINVAL); | |
2374 | ||
2375 | s->connections_per_uid_max = m; | |
2376 | return 0; | |
2377 | } | |
2378 | ||
2379 | int varlink_server_set_connections_max(VarlinkServer *s, unsigned m) { | |
2380 | assert_return(s, -EINVAL); | |
2381 | assert_return(m > 0, -EINVAL); | |
2382 | ||
2383 | s->connections_max = m; | |
2384 | return 0; | |
2385 | } | |
2386 | ||
2387 | int varlink_server_set_description(VarlinkServer *s, const char *description) { | |
2388 | assert_return(s, -EINVAL); | |
2389 | ||
2390 | return free_and_strdup(&s->description, description); | |
2391 | } |