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