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