]>
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); |
d41bd96f | 346 | |
1d3fe304 | 347 | v->time_event_source = sd_event_source_disable_unref(v->time_event_source); |
d41bd96f | 348 | |
1d3fe304 | 349 | v->quit_event_source = sd_event_source_disable_unref(v->quit_event_source); |
d41bd96f | 350 | |
1d3fe304 | 351 | v->defer_event_source = sd_event_source_disable_unref(v->defer_event_source); |
d41bd96f LP |
352 | } |
353 | ||
354 | static void varlink_clear(Varlink *v) { | |
355 | assert(v); | |
356 | ||
357 | varlink_detach_event_sources(v); | |
358 | ||
359 | v->fd = safe_close(v->fd); | |
360 | ||
361 | v->input_buffer = mfree(v->input_buffer); | |
362 | v->output_buffer = mfree(v->output_buffer); | |
363 | ||
364 | v->current = json_variant_unref(v->current); | |
365 | v->reply = json_variant_unref(v->reply); | |
366 | ||
367 | v->event = sd_event_unref(v->event); | |
368 | } | |
369 | ||
370 | static Varlink* varlink_destroy(Varlink *v) { | |
371 | if (!v) | |
372 | return NULL; | |
373 | ||
374 | /* If this is called the server object must already been unreffed here. Why that? because when we | |
375 | * linked up the varlink connection with the server object we took one ref in each direction */ | |
376 | assert(!v->server); | |
377 | ||
378 | varlink_clear(v); | |
379 | ||
380 | free(v->description); | |
381 | return mfree(v); | |
382 | } | |
383 | ||
384 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Varlink, varlink, varlink_destroy); | |
385 | ||
386 | static int varlink_test_disconnect(Varlink *v) { | |
387 | assert(v); | |
388 | ||
389 | /* Tests whether we the the connection has been terminated. We are careful to not stop processing it | |
390 | * prematurely, since we want to handle half-open connections as well as possible and want to flush | |
391 | * out and read data before we close down if we can. */ | |
392 | ||
393 | /* Already disconnected? */ | |
394 | if (!VARLINK_STATE_IS_ALIVE(v->state)) | |
395 | return 0; | |
396 | ||
397 | /* Wait until connection setup is complete, i.e. until asynchronous connect() completes */ | |
398 | if (v->connecting) | |
399 | return 0; | |
400 | ||
401 | /* Still something to write and we can write? Stay around */ | |
402 | if (v->output_buffer_size > 0 && !v->write_disconnected) | |
403 | return 0; | |
404 | ||
405 | /* Both sides gone already? Then there's no need to stick around */ | |
406 | if (v->read_disconnected && v->write_disconnected) | |
407 | goto disconnect; | |
408 | ||
409 | /* If we are waiting for incoming data but the read side is shut down, disconnect. */ | |
45a6c965 | 410 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING, VARLINK_IDLE_SERVER) && v->read_disconnected) |
d41bd96f LP |
411 | goto disconnect; |
412 | ||
413 | /* Similar, if are a client that hasn't written anything yet but the write side is dead, also | |
414 | * disconnect. We also explicitly check for POLLHUP here since we likely won't notice the write side | |
415 | * being down if we never wrote anything. */ | |
416 | if (IN_SET(v->state, VARLINK_IDLE_CLIENT) && (v->write_disconnected || v->got_pollhup)) | |
417 | goto disconnect; | |
418 | ||
419 | return 0; | |
420 | ||
421 | disconnect: | |
422 | varlink_set_state(v, VARLINK_PENDING_DISCONNECT); | |
423 | return 1; | |
424 | } | |
425 | ||
426 | static int varlink_write(Varlink *v) { | |
427 | ssize_t n; | |
428 | ||
429 | assert(v); | |
430 | ||
431 | if (!VARLINK_STATE_IS_ALIVE(v->state)) | |
432 | return 0; | |
433 | if (v->connecting) /* Writing while we are still wait for a non-blocking connect() to complete will | |
434 | * result in ENOTCONN, hence exit early here */ | |
435 | return 0; | |
436 | if (v->output_buffer_size == 0) | |
437 | return 0; | |
438 | if (v->write_disconnected) | |
439 | return 0; | |
440 | ||
441 | assert(v->fd >= 0); | |
442 | ||
443 | /* We generally prefer recv()/send() (mostly because of MSG_NOSIGNAL) but also want to be compatible | |
444 | * with non-socket IO, hence fall back automatically */ | |
445 | if (!v->prefer_read_write) { | |
446 | n = send(v->fd, v->output_buffer + v->output_buffer_index, v->output_buffer_size, MSG_DONTWAIT|MSG_NOSIGNAL); | |
447 | if (n < 0 && errno == ENOTSOCK) | |
448 | v->prefer_read_write = true; | |
449 | } | |
450 | if (v->prefer_read_write) | |
451 | n = write(v->fd, v->output_buffer + v->output_buffer_index, v->output_buffer_size); | |
452 | if (n < 0) { | |
453 | if (errno == EAGAIN) | |
454 | return 0; | |
455 | ||
456 | if (ERRNO_IS_DISCONNECT(errno)) { | |
457 | /* If we get informed about a disconnect on write, then let's remember that, but not | |
458 | * act on it just yet. Let's wait for read() to report the issue first. */ | |
459 | v->write_disconnected = true; | |
460 | return 1; | |
461 | } | |
462 | ||
463 | return -errno; | |
464 | } | |
465 | ||
466 | v->output_buffer_size -= n; | |
467 | ||
468 | if (v->output_buffer_size == 0) | |
469 | v->output_buffer_index = 0; | |
470 | else | |
471 | v->output_buffer_index += n; | |
472 | ||
473 | v->timestamp = now(CLOCK_MONOTONIC); | |
474 | return 1; | |
475 | } | |
476 | ||
477 | static int varlink_read(Varlink *v) { | |
478 | size_t rs; | |
479 | ssize_t n; | |
480 | ||
481 | assert(v); | |
482 | ||
45a6c965 | 483 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING, VARLINK_IDLE_SERVER)) |
d41bd96f LP |
484 | return 0; |
485 | if (v->connecting) /* read() on a socket while we are in connect() will fail with EINVAL, hence exit early here */ | |
486 | return 0; | |
487 | if (v->current) | |
488 | return 0; | |
489 | if (v->input_buffer_unscanned > 0) | |
490 | return 0; | |
491 | if (v->read_disconnected) | |
492 | return 0; | |
493 | ||
494 | if (v->input_buffer_size >= VARLINK_BUFFER_MAX) | |
495 | return -ENOBUFS; | |
496 | ||
497 | assert(v->fd >= 0); | |
498 | ||
499 | if (v->input_buffer_allocated <= v->input_buffer_index + v->input_buffer_size) { | |
500 | size_t add; | |
501 | ||
502 | add = MIN(VARLINK_BUFFER_MAX - v->input_buffer_size, VARLINK_READ_SIZE); | |
503 | ||
504 | if (v->input_buffer_index == 0) { | |
505 | ||
506 | if (!GREEDY_REALLOC(v->input_buffer, v->input_buffer_allocated, v->input_buffer_size + add)) | |
507 | return -ENOMEM; | |
508 | ||
509 | } else { | |
510 | char *b; | |
511 | ||
512 | b = new(char, v->input_buffer_size + add); | |
513 | if (!b) | |
514 | return -ENOMEM; | |
515 | ||
516 | memcpy(b, v->input_buffer + v->input_buffer_index, v->input_buffer_size); | |
517 | ||
518 | free_and_replace(v->input_buffer, b); | |
519 | ||
520 | v->input_buffer_allocated = v->input_buffer_size + add; | |
521 | v->input_buffer_index = 0; | |
522 | } | |
523 | } | |
524 | ||
525 | rs = v->input_buffer_allocated - (v->input_buffer_index + v->input_buffer_size); | |
526 | ||
527 | if (!v->prefer_read_write) { | |
528 | n = recv(v->fd, v->input_buffer + v->input_buffer_index + v->input_buffer_size, rs, MSG_DONTWAIT); | |
529 | if (n < 0 && errno == ENOTSOCK) | |
530 | v->prefer_read_write = true; | |
531 | } | |
532 | if (v->prefer_read_write) | |
533 | n = read(v->fd, v->input_buffer + v->input_buffer_index + v->input_buffer_size, rs); | |
534 | if (n < 0) { | |
535 | if (errno == EAGAIN) | |
536 | return 0; | |
537 | ||
538 | if (ERRNO_IS_DISCONNECT(errno)) { | |
539 | v->read_disconnected = true; | |
540 | return 1; | |
541 | } | |
542 | ||
543 | return -errno; | |
544 | } | |
545 | if (n == 0) { /* EOF */ | |
546 | v->read_disconnected = true; | |
547 | return 1; | |
548 | } | |
549 | ||
550 | v->input_buffer_size += n; | |
551 | v->input_buffer_unscanned += n; | |
552 | ||
553 | return 1; | |
554 | } | |
555 | ||
556 | static int varlink_parse_message(Varlink *v) { | |
557 | const char *e, *begin; | |
558 | size_t sz; | |
559 | int r; | |
560 | ||
561 | assert(v); | |
562 | ||
563 | if (v->current) | |
564 | return 0; | |
565 | if (v->input_buffer_unscanned <= 0) | |
566 | return 0; | |
567 | ||
568 | assert(v->input_buffer_unscanned <= v->input_buffer_size); | |
569 | assert(v->input_buffer_index + v->input_buffer_size <= v->input_buffer_allocated); | |
570 | ||
571 | begin = v->input_buffer + v->input_buffer_index; | |
572 | ||
573 | e = memchr(begin + v->input_buffer_size - v->input_buffer_unscanned, 0, v->input_buffer_unscanned); | |
574 | if (!e) { | |
575 | v->input_buffer_unscanned = 0; | |
576 | return 0; | |
577 | } | |
578 | ||
579 | sz = e - begin + 1; | |
580 | ||
581 | varlink_log(v, "New incoming message: %s", begin); | |
582 | ||
583 | r = json_parse(begin, &v->current, NULL, NULL); | |
584 | if (r < 0) | |
585 | return r; | |
586 | ||
587 | v->input_buffer_size -= sz; | |
588 | ||
589 | if (v->input_buffer_size == 0) | |
590 | v->input_buffer_index = 0; | |
591 | else | |
592 | v->input_buffer_index += sz; | |
593 | ||
594 | v->input_buffer_unscanned = v->input_buffer_size; | |
595 | return 1; | |
596 | } | |
597 | ||
598 | static int varlink_test_timeout(Varlink *v) { | |
599 | assert(v); | |
600 | ||
45a6c965 | 601 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING)) |
d41bd96f LP |
602 | return 0; |
603 | if (v->timeout == USEC_INFINITY) | |
604 | return 0; | |
605 | ||
606 | if (now(CLOCK_MONOTONIC) < usec_add(v->timestamp, v->timeout)) | |
607 | return 0; | |
608 | ||
609 | varlink_set_state(v, VARLINK_PENDING_TIMEOUT); | |
610 | ||
611 | return 1; | |
612 | } | |
613 | ||
614 | static int varlink_dispatch_local_error(Varlink *v, const char *error) { | |
615 | int r; | |
616 | ||
617 | assert(v); | |
618 | assert(error); | |
619 | ||
620 | if (!v->reply_callback) | |
621 | return 0; | |
622 | ||
623 | r = v->reply_callback(v, NULL, error, VARLINK_REPLY_ERROR|VARLINK_REPLY_LOCAL, v->userdata); | |
624 | if (r < 0) | |
625 | log_debug_errno(r, "Reply callback returned error, ignoring: %m"); | |
626 | ||
627 | return 1; | |
628 | } | |
629 | ||
630 | static int varlink_dispatch_timeout(Varlink *v) { | |
631 | assert(v); | |
632 | ||
633 | if (v->state != VARLINK_PENDING_TIMEOUT) | |
634 | return 0; | |
635 | ||
636 | varlink_set_state(v, VARLINK_PROCESSING_TIMEOUT); | |
637 | varlink_dispatch_local_error(v, VARLINK_ERROR_TIMEOUT); | |
638 | varlink_close(v); | |
639 | ||
640 | return 1; | |
641 | } | |
642 | ||
643 | static int varlink_dispatch_disconnect(Varlink *v) { | |
644 | assert(v); | |
645 | ||
646 | if (v->state != VARLINK_PENDING_DISCONNECT) | |
647 | return 0; | |
648 | ||
649 | varlink_set_state(v, VARLINK_PROCESSING_DISCONNECT); | |
650 | varlink_dispatch_local_error(v, VARLINK_ERROR_DISCONNECTED); | |
651 | varlink_close(v); | |
652 | ||
653 | return 1; | |
654 | } | |
655 | ||
656 | static int varlink_sanitize_parameters(JsonVariant **v) { | |
657 | assert(v); | |
658 | ||
659 | /* Varlink always wants a parameters list, hence make one if the caller doesn't want any */ | |
660 | if (!*v) | |
661 | return json_variant_new_object(v, NULL, 0); | |
662 | else if (!json_variant_is_object(*v)) | |
663 | return -EINVAL; | |
664 | ||
665 | return 0; | |
666 | } | |
667 | ||
668 | static int varlink_dispatch_reply(Varlink *v) { | |
669 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
670 | VarlinkReplyFlags flags = 0; | |
671 | const char *error = NULL; | |
672 | JsonVariant *e; | |
673 | const char *k; | |
674 | int r; | |
675 | ||
676 | assert(v); | |
677 | ||
45a6c965 | 678 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING)) |
d41bd96f LP |
679 | return 0; |
680 | if (!v->current) | |
681 | return 0; | |
682 | ||
683 | assert(v->n_pending > 0); | |
684 | ||
685 | if (!json_variant_is_object(v->current)) | |
686 | goto invalid; | |
687 | ||
688 | JSON_VARIANT_OBJECT_FOREACH(k, e, v->current) { | |
689 | ||
690 | if (streq(k, "error")) { | |
691 | if (error) | |
692 | goto invalid; | |
693 | if (!json_variant_is_string(e)) | |
694 | goto invalid; | |
695 | ||
696 | error = json_variant_string(e); | |
697 | flags |= VARLINK_REPLY_ERROR; | |
698 | ||
699 | } else if (streq(k, "parameters")) { | |
700 | if (parameters) | |
701 | goto invalid; | |
702 | if (!json_variant_is_object(e)) | |
703 | goto invalid; | |
704 | ||
705 | parameters = json_variant_ref(e); | |
706 | ||
707 | } else if (streq(k, "continues")) { | |
708 | if (FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) | |
709 | goto invalid; | |
710 | ||
711 | if (!json_variant_is_boolean(e)) | |
712 | goto invalid; | |
713 | ||
714 | if (json_variant_boolean(e)) | |
715 | flags |= VARLINK_REPLY_CONTINUES; | |
716 | } else | |
717 | goto invalid; | |
718 | } | |
719 | ||
45a6c965 LP |
720 | /* Replies with 'continue' set are only OK if we set 'more' when the method call was initiated */ |
721 | if (v->state != VARLINK_AWAITING_REPLY_MORE && FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) | |
722 | goto invalid; | |
723 | ||
724 | /* An error is final */ | |
d41bd96f LP |
725 | if (error && FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) |
726 | goto invalid; | |
727 | ||
728 | r = varlink_sanitize_parameters(¶meters); | |
729 | if (r < 0) | |
730 | goto invalid; | |
731 | ||
45a6c965 | 732 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE)) { |
d41bd96f LP |
733 | varlink_set_state(v, VARLINK_PROCESSING_REPLY); |
734 | ||
735 | if (v->reply_callback) { | |
736 | r = v->reply_callback(v, parameters, error, flags, v->userdata); | |
737 | if (r < 0) | |
738 | log_debug_errno(r, "Reply callback returned error, ignoring: %m"); | |
739 | } | |
740 | ||
741 | v->current = json_variant_unref(v->current); | |
742 | ||
743 | if (v->state == VARLINK_PROCESSING_REPLY) { | |
45a6c965 | 744 | |
d41bd96f | 745 | assert(v->n_pending > 0); |
d41bd96f | 746 | |
45a6c965 LP |
747 | if (!FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) |
748 | v->n_pending--; | |
749 | ||
750 | varlink_set_state(v, | |
751 | FLAGS_SET(flags, VARLINK_REPLY_CONTINUES) ? VARLINK_AWAITING_REPLY_MORE : | |
752 | v->n_pending == 0 ? VARLINK_IDLE_CLIENT : VARLINK_AWAITING_REPLY); | |
d41bd96f LP |
753 | } |
754 | } else { | |
755 | assert(v->state == VARLINK_CALLING); | |
d41bd96f LP |
756 | varlink_set_state(v, VARLINK_CALLED); |
757 | } | |
758 | ||
759 | return 1; | |
760 | ||
761 | invalid: | |
762 | varlink_set_state(v, VARLINK_PROCESSING_FAILURE); | |
763 | varlink_dispatch_local_error(v, VARLINK_ERROR_PROTOCOL); | |
764 | varlink_close(v); | |
765 | ||
766 | return 1; | |
767 | } | |
768 | ||
769 | static int varlink_dispatch_method(Varlink *v) { | |
770 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
771 | VarlinkMethodFlags flags = 0; | |
772 | const char *method = NULL, *error; | |
773 | JsonVariant *e; | |
774 | VarlinkMethod callback; | |
775 | const char *k; | |
776 | int r; | |
777 | ||
778 | assert(v); | |
779 | ||
780 | if (v->state != VARLINK_IDLE_SERVER) | |
781 | return 0; | |
782 | if (!v->current) | |
783 | return 0; | |
784 | ||
785 | if (!json_variant_is_object(v->current)) | |
786 | goto invalid; | |
787 | ||
788 | JSON_VARIANT_OBJECT_FOREACH(k, e, v->current) { | |
789 | ||
790 | if (streq(k, "method")) { | |
791 | if (method) | |
792 | goto invalid; | |
793 | if (!json_variant_is_string(e)) | |
794 | goto invalid; | |
795 | ||
796 | method = json_variant_string(e); | |
797 | ||
798 | } else if (streq(k, "parameters")) { | |
799 | if (parameters) | |
800 | goto invalid; | |
801 | if (!json_variant_is_object(e)) | |
802 | goto invalid; | |
803 | ||
804 | parameters = json_variant_ref(e); | |
805 | ||
806 | } else if (streq(k, "oneway")) { | |
807 | ||
808 | if ((flags & (VARLINK_METHOD_ONEWAY|VARLINK_METHOD_MORE)) != 0) | |
809 | goto invalid; | |
810 | ||
811 | if (!json_variant_is_boolean(e)) | |
812 | goto invalid; | |
813 | ||
814 | if (json_variant_boolean(e)) | |
815 | flags |= VARLINK_METHOD_ONEWAY; | |
816 | ||
817 | } else if (streq(k, "more")) { | |
818 | ||
819 | if ((flags & (VARLINK_METHOD_ONEWAY|VARLINK_METHOD_MORE)) != 0) | |
820 | goto invalid; | |
821 | ||
822 | if (!json_variant_is_boolean(e)) | |
823 | goto invalid; | |
824 | ||
825 | if (json_variant_boolean(e)) | |
826 | flags |= VARLINK_METHOD_MORE; | |
827 | ||
828 | } else | |
829 | goto invalid; | |
830 | } | |
831 | ||
832 | if (!method) | |
833 | goto invalid; | |
834 | ||
835 | r = varlink_sanitize_parameters(¶meters); | |
836 | if (r < 0) | |
837 | goto fail; | |
838 | ||
839 | varlink_set_state(v, (flags & VARLINK_METHOD_MORE) ? VARLINK_PROCESSING_METHOD_MORE : | |
840 | (flags & VARLINK_METHOD_ONEWAY) ? VARLINK_PROCESSING_METHOD_ONEWAY : | |
841 | VARLINK_PROCESSING_METHOD); | |
842 | ||
843 | assert(v->server); | |
844 | ||
845 | if (STR_IN_SET(method, "org.varlink.service.GetInfo", "org.varlink.service.GetInterface")) { | |
846 | /* For now, we don't implement a single of varlink's own methods */ | |
847 | callback = NULL; | |
848 | error = VARLINK_ERROR_METHOD_NOT_IMPLEMENTED; | |
849 | } else if (startswith(method, "org.varlink.service.")) { | |
850 | callback = NULL; | |
851 | error = VARLINK_ERROR_METHOD_NOT_FOUND; | |
852 | } else { | |
853 | callback = hashmap_get(v->server->methods, method); | |
854 | error = VARLINK_ERROR_METHOD_NOT_FOUND; | |
855 | } | |
856 | ||
857 | if (callback) { | |
858 | r = callback(v, parameters, flags, v->userdata); | |
859 | if (r < 0) { | |
860 | log_debug_errno(r, "Callback for %s returned error: %m", method); | |
861 | ||
862 | /* We got an error back from the callback. Propagate it to the client if the method call remains unanswered. */ | |
863 | if (!FLAGS_SET(flags, VARLINK_METHOD_ONEWAY)) { | |
864 | r = varlink_errorb(v, VARLINK_ERROR_SYSTEM, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("errno", JSON_BUILD_INTEGER(-r)))); | |
865 | if (r < 0) | |
866 | return r; | |
867 | } | |
868 | } | |
869 | } else if (!FLAGS_SET(flags, VARLINK_METHOD_ONEWAY)) { | |
870 | assert(error); | |
871 | ||
872 | r = varlink_errorb(v, error, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)))); | |
873 | if (r < 0) | |
874 | return r; | |
875 | } | |
876 | ||
877 | switch (v->state) { | |
878 | ||
879 | case VARLINK_PROCESSED_METHOD: /* Method call is fully processed */ | |
880 | case VARLINK_PROCESSING_METHOD_ONEWAY: /* ditto */ | |
881 | v->current = json_variant_unref(v->current); | |
882 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
883 | break; | |
884 | ||
885 | case VARLINK_PROCESSING_METHOD: /* Method call wasn't replied to, will be replied to later */ | |
886 | varlink_set_state(v, VARLINK_PENDING_METHOD); | |
887 | break; | |
888 | ||
d41bd96f LP |
889 | case VARLINK_PROCESSING_METHOD_MORE: /* No reply for a "more" message was sent, more to come */ |
890 | varlink_set_state(v, VARLINK_PENDING_METHOD_MORE); | |
891 | break; | |
892 | ||
893 | default: | |
894 | assert_not_reached("Unexpected state"); | |
895 | ||
896 | } | |
897 | ||
898 | return r; | |
899 | ||
900 | invalid: | |
901 | r = -EINVAL; | |
902 | ||
903 | fail: | |
904 | varlink_set_state(v, VARLINK_PROCESSING_FAILURE); | |
905 | varlink_dispatch_local_error(v, VARLINK_ERROR_PROTOCOL); | |
906 | varlink_close(v); | |
907 | ||
908 | return r; | |
909 | } | |
910 | ||
911 | int varlink_process(Varlink *v) { | |
912 | int r; | |
913 | ||
914 | assert_return(v, -EINVAL); | |
915 | ||
916 | if (v->state == VARLINK_DISCONNECTED) | |
917 | return -ENOTCONN; | |
918 | ||
919 | varlink_ref(v); | |
920 | ||
921 | r = varlink_write(v); | |
922 | if (r != 0) | |
923 | goto finish; | |
924 | ||
925 | r = varlink_dispatch_reply(v); | |
926 | if (r != 0) | |
927 | goto finish; | |
928 | ||
929 | r = varlink_dispatch_method(v); | |
930 | if (r != 0) | |
931 | goto finish; | |
932 | ||
933 | r = varlink_parse_message(v); | |
934 | if (r != 0) | |
935 | goto finish; | |
936 | ||
937 | r = varlink_read(v); | |
938 | if (r != 0) | |
939 | goto finish; | |
940 | ||
941 | r = varlink_test_disconnect(v); | |
942 | if (r != 0) | |
943 | goto finish; | |
944 | ||
945 | r = varlink_dispatch_disconnect(v); | |
946 | if (r != 0) | |
947 | goto finish; | |
948 | ||
949 | r = varlink_test_timeout(v); | |
950 | if (r != 0) | |
951 | goto finish; | |
952 | ||
953 | r = varlink_dispatch_timeout(v); | |
954 | if (r != 0) | |
955 | goto finish; | |
956 | ||
957 | finish: | |
958 | if (r >= 0 && v->defer_event_source) { | |
959 | int q; | |
960 | ||
961 | /* If we did some processing, make sure we are called again soon */ | |
962 | q = sd_event_source_set_enabled(v->defer_event_source, r > 0 ? SD_EVENT_ON : SD_EVENT_OFF); | |
963 | if (q < 0) | |
964 | r = q; | |
965 | } | |
966 | ||
967 | if (r < 0) { | |
968 | if (VARLINK_STATE_IS_ALIVE(v->state)) | |
969 | /* Initiate disconnection */ | |
970 | varlink_set_state(v, VARLINK_PENDING_DISCONNECT); | |
971 | else | |
972 | /* We failed while disconnecting, in that case close right away */ | |
973 | varlink_close(v); | |
974 | } | |
975 | ||
976 | varlink_unref(v); | |
977 | return r; | |
978 | } | |
979 | ||
980 | static void handle_revents(Varlink *v, int revents) { | |
981 | assert(v); | |
982 | ||
983 | if (v->connecting) { | |
984 | /* If we have seen POLLOUT or POLLHUP on a socket we are asynchronously waiting a connect() | |
985 | * to complete on, we know we are ready. We don't read the connection error here though, | |
986 | * we'll get the error on the next read() or write(). */ | |
987 | if ((revents & (POLLOUT|POLLHUP)) == 0) | |
988 | return; | |
989 | ||
990 | varlink_log(v, "Anynchronous connection completed."); | |
991 | v->connecting = false; | |
992 | } else { | |
993 | /* Note that we don't care much about POLLIN/POLLOUT here, we'll just try reading and writing | |
994 | * what we can. However, we do care about POLLHUP to detect connection termination even if we | |
995 | * momentarily don't want to read nor write anything. */ | |
996 | ||
997 | if (!FLAGS_SET(revents, POLLHUP)) | |
998 | return; | |
999 | ||
1000 | varlink_log(v, "Got POLLHUP from socket."); | |
1001 | v->got_pollhup = true; | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | int varlink_wait(Varlink *v, usec_t timeout) { | |
1006 | struct timespec ts; | |
1007 | struct pollfd pfd; | |
1008 | int r, fd, events; | |
1009 | usec_t t; | |
1010 | ||
1011 | assert_return(v, -EINVAL); | |
d41bd96f LP |
1012 | |
1013 | if (v->state == VARLINK_DISCONNECTED) | |
1014 | return -ENOTCONN; | |
1015 | ||
1016 | r = varlink_get_timeout(v, &t); | |
1017 | if (r < 0) | |
1018 | return r; | |
1019 | if (t != USEC_INFINITY) { | |
1020 | usec_t n; | |
1021 | ||
1022 | n = now(CLOCK_MONOTONIC); | |
1023 | if (t < n) | |
1024 | t = 0; | |
1025 | else | |
1026 | t = usec_sub_unsigned(t, n); | |
1027 | } | |
1028 | ||
1029 | if (timeout != USEC_INFINITY && | |
1030 | (t == USEC_INFINITY || timeout < t)) | |
1031 | t = timeout; | |
1032 | ||
1033 | fd = varlink_get_fd(v); | |
1034 | if (fd < 0) | |
1035 | return fd; | |
1036 | ||
1037 | events = varlink_get_events(v); | |
1038 | if (events < 0) | |
1039 | return events; | |
1040 | ||
1041 | pfd = (struct pollfd) { | |
1042 | .fd = fd, | |
1043 | .events = events, | |
1044 | }; | |
1045 | ||
1046 | r = ppoll(&pfd, 1, | |
1047 | t == USEC_INFINITY ? NULL : timespec_store(&ts, t), | |
1048 | NULL); | |
1049 | if (r < 0) | |
1050 | return -errno; | |
1051 | ||
1052 | handle_revents(v, pfd.revents); | |
1053 | ||
1054 | return r > 0 ? 1 : 0; | |
1055 | } | |
1056 | ||
1057 | int varlink_get_fd(Varlink *v) { | |
1058 | ||
1059 | assert_return(v, -EINVAL); | |
1060 | ||
1061 | if (v->state == VARLINK_DISCONNECTED) | |
1062 | return -ENOTCONN; | |
1063 | if (v->fd < 0) | |
1064 | return -EBADF; | |
1065 | ||
1066 | return v->fd; | |
1067 | } | |
1068 | ||
1069 | int varlink_get_events(Varlink *v) { | |
1070 | int ret = 0; | |
1071 | ||
1072 | assert_return(v, -EINVAL); | |
1073 | ||
1074 | if (v->state == VARLINK_DISCONNECTED) | |
1075 | return -ENOTCONN; | |
1076 | ||
1077 | if (v->connecting) /* When processing an asynchronous connect(), we only wait for EPOLLOUT, which | |
1078 | * tells us that the connection is now complete. Before that we should neither | |
1079 | * write() or read() from the fd. */ | |
1080 | return EPOLLOUT; | |
1081 | ||
1082 | if (!v->read_disconnected && | |
45a6c965 | 1083 | IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING, VARLINK_IDLE_SERVER) && |
d41bd96f LP |
1084 | !v->current && |
1085 | v->input_buffer_unscanned <= 0) | |
1086 | ret |= EPOLLIN; | |
1087 | ||
1088 | if (!v->write_disconnected && | |
1089 | v->output_buffer_size > 0) | |
1090 | ret |= EPOLLOUT; | |
1091 | ||
1092 | return ret; | |
1093 | } | |
1094 | ||
1095 | int varlink_get_timeout(Varlink *v, usec_t *ret) { | |
1096 | assert_return(v, -EINVAL); | |
1097 | ||
1098 | if (v->state == VARLINK_DISCONNECTED) | |
1099 | return -ENOTCONN; | |
1100 | ||
45a6c965 | 1101 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING) && |
d41bd96f LP |
1102 | v->timeout != USEC_INFINITY) { |
1103 | if (ret) | |
1104 | *ret = usec_add(v->timestamp, v->timeout); | |
1105 | return 1; | |
1106 | } else { | |
1107 | if (ret) | |
1108 | *ret = USEC_INFINITY; | |
1109 | return 0; | |
1110 | } | |
1111 | } | |
1112 | ||
1113 | int varlink_flush(Varlink *v) { | |
1114 | int ret = 0, r; | |
1115 | ||
1116 | assert_return(v, -EINVAL); | |
1117 | ||
1118 | if (v->state == VARLINK_DISCONNECTED) | |
1119 | return -ENOTCONN; | |
1120 | ||
1121 | for (;;) { | |
1122 | struct pollfd pfd; | |
1123 | ||
1124 | if (v->output_buffer_size == 0) | |
1125 | break; | |
1126 | if (v->write_disconnected) | |
1127 | return -ECONNRESET; | |
1128 | ||
1129 | r = varlink_write(v); | |
1130 | if (r < 0) | |
1131 | return r; | |
1132 | if (r > 0) { | |
1133 | ret = 1; | |
1134 | continue; | |
1135 | } | |
1136 | ||
1137 | pfd = (struct pollfd) { | |
1138 | .fd = v->fd, | |
1139 | .events = POLLOUT, | |
1140 | }; | |
1141 | ||
1142 | if (poll(&pfd, 1, -1) < 0) | |
1143 | return -errno; | |
1144 | ||
1145 | handle_revents(v, pfd.revents); | |
1146 | } | |
1147 | ||
1148 | return ret; | |
1149 | } | |
1150 | ||
1151 | static void varlink_detach_server(Varlink *v) { | |
1152 | assert(v); | |
1153 | ||
1154 | if (!v->server) | |
1155 | return; | |
1156 | ||
1157 | if (v->server->by_uid && | |
1158 | v->ucred_acquired && | |
1159 | uid_is_valid(v->ucred.uid)) { | |
1160 | unsigned c; | |
1161 | ||
1162 | c = PTR_TO_UINT(hashmap_get(v->server->by_uid, UID_TO_PTR(v->ucred.uid))); | |
1163 | assert(c > 0); | |
1164 | ||
1165 | if (c == 1) | |
1166 | (void) hashmap_remove(v->server->by_uid, UID_TO_PTR(v->ucred.uid)); | |
1167 | else | |
1168 | (void) hashmap_replace(v->server->by_uid, UID_TO_PTR(v->ucred.uid), UINT_TO_PTR(c - 1)); | |
1169 | } | |
1170 | ||
1171 | assert(v->server->n_connections > 0); | |
1172 | v->server->n_connections--; | |
1173 | ||
1174 | /* If this is a connection associated to a server, then let's disconnect the server and the | |
1175 | * connection from each other. This drops the dangling reference that connect_callback() set up. */ | |
1176 | v->server = varlink_server_unref(v->server); | |
1177 | varlink_unref(v); | |
1178 | } | |
1179 | ||
1180 | int varlink_close(Varlink *v) { | |
1181 | ||
1182 | assert_return(v, -EINVAL); | |
1183 | ||
1184 | if (v->state == VARLINK_DISCONNECTED) | |
1185 | return 0; | |
1186 | ||
1187 | varlink_set_state(v, VARLINK_DISCONNECTED); | |
1188 | ||
1189 | /* Let's take a reference first, since varlink_detach_server() might drop the final (dangling) ref | |
1190 | * which would destroy us before we can call varlink_clear() */ | |
1191 | varlink_ref(v); | |
1192 | varlink_detach_server(v); | |
1193 | varlink_clear(v); | |
1194 | varlink_unref(v); | |
1195 | ||
1196 | return 1; | |
1197 | } | |
1198 | ||
1199 | Varlink* varlink_flush_close_unref(Varlink *v) { | |
1200 | ||
1201 | if (!v) | |
1202 | return NULL; | |
1203 | ||
1204 | (void) varlink_flush(v); | |
1205 | (void) varlink_close(v); | |
1206 | ||
1207 | return varlink_unref(v); | |
1208 | } | |
1209 | ||
1210 | static int varlink_enqueue_json(Varlink *v, JsonVariant *m) { | |
1211 | _cleanup_free_ char *text = NULL; | |
1212 | int r; | |
1213 | ||
1214 | assert(v); | |
1215 | assert(m); | |
1216 | ||
1217 | r = json_variant_format(m, 0, &text); | |
1218 | if (r < 0) | |
1219 | return r; | |
2a04712c | 1220 | assert(text[r] == '\0'); |
d41bd96f LP |
1221 | |
1222 | if (v->output_buffer_size + r + 1 > VARLINK_BUFFER_MAX) | |
1223 | return -ENOBUFS; | |
1224 | ||
1225 | varlink_log(v, "Sending message: %s", text); | |
1226 | ||
1227 | if (v->output_buffer_size == 0) { | |
1228 | ||
1229 | free_and_replace(v->output_buffer, text); | |
1230 | ||
1231 | v->output_buffer_size = v->output_buffer_allocated = r + 1; | |
1232 | v->output_buffer_index = 0; | |
1233 | ||
1234 | } else if (v->output_buffer_index == 0) { | |
1235 | ||
1236 | if (!GREEDY_REALLOC(v->output_buffer, v->output_buffer_allocated, v->output_buffer_size + r + 1)) | |
1237 | return -ENOMEM; | |
1238 | ||
1239 | memcpy(v->output_buffer + v->output_buffer_size, text, r + 1); | |
1240 | v->output_buffer_size += r + 1; | |
1241 | ||
1242 | } else { | |
1243 | char *n; | |
be44e091 | 1244 | const size_t new_size = v->output_buffer_size + r + 1; |
d41bd96f | 1245 | |
be44e091 | 1246 | n = new(char, new_size); |
d41bd96f LP |
1247 | if (!n) |
1248 | return -ENOMEM; | |
1249 | ||
1250 | memcpy(mempcpy(n, v->output_buffer + v->output_buffer_index, v->output_buffer_size), text, r + 1); | |
1251 | ||
1252 | free_and_replace(v->output_buffer, n); | |
be44e091 | 1253 | v->output_buffer_allocated = v->output_buffer_size = new_size; |
d41bd96f LP |
1254 | v->output_buffer_index = 0; |
1255 | } | |
1256 | ||
1257 | return 0; | |
1258 | } | |
1259 | ||
1260 | int varlink_send(Varlink *v, const char *method, JsonVariant *parameters) { | |
1261 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1262 | int r; | |
1263 | ||
1264 | assert_return(v, -EINVAL); | |
1265 | assert_return(method, -EINVAL); | |
1266 | ||
1267 | if (v->state == VARLINK_DISCONNECTED) | |
1268 | return -ENOTCONN; | |
45a6c965 LP |
1269 | |
1270 | /* We allow enqueuing multiple method calls at once! */ | |
d41bd96f LP |
1271 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT, VARLINK_AWAITING_REPLY)) |
1272 | return -EBUSY; | |
1273 | ||
1274 | r = varlink_sanitize_parameters(¶meters); | |
1275 | if (r < 0) | |
1276 | return r; | |
1277 | ||
1278 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1279 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1280 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
1281 | JSON_BUILD_PAIR("oneway", JSON_BUILD_BOOLEAN(true)))); | |
1282 | if (r < 0) | |
1283 | return r; | |
1284 | ||
1285 | r = varlink_enqueue_json(v, m); | |
1286 | if (r < 0) | |
1287 | return r; | |
1288 | ||
1289 | /* No state change here, this is one-way only after all */ | |
1290 | v->timestamp = now(CLOCK_MONOTONIC); | |
1291 | return 0; | |
1292 | } | |
1293 | ||
1294 | int varlink_sendb(Varlink *v, const char *method, ...) { | |
1295 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1296 | va_list ap; | |
1297 | int r; | |
1298 | ||
1299 | assert_return(v, -EINVAL); | |
1300 | ||
1301 | va_start(ap, method); | |
1302 | r = json_buildv(¶meters, ap); | |
1303 | va_end(ap); | |
1304 | ||
1305 | if (r < 0) | |
1306 | return r; | |
1307 | ||
1308 | return varlink_send(v, method, parameters); | |
1309 | } | |
1310 | ||
1311 | int varlink_invoke(Varlink *v, const char *method, JsonVariant *parameters) { | |
1312 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1313 | int r; | |
1314 | ||
1315 | assert_return(v, -EINVAL); | |
1316 | assert_return(method, -EINVAL); | |
1317 | ||
1318 | if (v->state == VARLINK_DISCONNECTED) | |
1319 | return -ENOTCONN; | |
45a6c965 LP |
1320 | |
1321 | /* We allow enqueing multiple method calls at once! */ | |
d41bd96f LP |
1322 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT, VARLINK_AWAITING_REPLY)) |
1323 | return -EBUSY; | |
1324 | ||
1325 | r = varlink_sanitize_parameters(¶meters); | |
1326 | if (r < 0) | |
1327 | return r; | |
1328 | ||
1329 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1330 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1331 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1332 | if (r < 0) | |
1333 | return r; | |
1334 | ||
1335 | r = varlink_enqueue_json(v, m); | |
1336 | if (r < 0) | |
1337 | return r; | |
1338 | ||
1339 | varlink_set_state(v, VARLINK_AWAITING_REPLY); | |
1340 | v->n_pending++; | |
1341 | v->timestamp = now(CLOCK_MONOTONIC); | |
1342 | ||
1343 | return 0; | |
1344 | } | |
1345 | ||
1346 | int varlink_invokeb(Varlink *v, const char *method, ...) { | |
1347 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1348 | va_list ap; | |
1349 | int r; | |
1350 | ||
1351 | assert_return(v, -EINVAL); | |
1352 | ||
1353 | va_start(ap, method); | |
1354 | r = json_buildv(¶meters, ap); | |
1355 | va_end(ap); | |
1356 | ||
1357 | if (r < 0) | |
1358 | return r; | |
1359 | ||
1360 | return varlink_invoke(v, method, parameters); | |
45a6c965 LP |
1361 | } |
1362 | ||
1363 | int varlink_observe(Varlink *v, const char *method, JsonVariant *parameters) { | |
1364 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1365 | int r; | |
1366 | ||
1367 | assert_return(v, -EINVAL); | |
1368 | assert_return(method, -EINVAL); | |
1369 | ||
1370 | if (v->state == VARLINK_DISCONNECTED) | |
1371 | return -ENOTCONN; | |
1372 | /* Note that we don't allow enqueuing multiple method calls when we are in more/continues mode! We | |
1373 | * thus insist on an idle client here. */ | |
1374 | if (v->state != VARLINK_IDLE_CLIENT) | |
1375 | return -EBUSY; | |
1376 | ||
1377 | r = varlink_sanitize_parameters(¶meters); | |
1378 | if (r < 0) | |
1379 | return r; | |
1380 | ||
1381 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1382 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1383 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
1384 | JSON_BUILD_PAIR("more", JSON_BUILD_BOOLEAN(true)))); | |
1385 | if (r < 0) | |
1386 | return r; | |
1387 | ||
1388 | r = varlink_enqueue_json(v, m); | |
1389 | if (r < 0) | |
1390 | return r; | |
1391 | ||
1392 | ||
1393 | varlink_set_state(v, VARLINK_AWAITING_REPLY_MORE); | |
1394 | v->n_pending++; | |
1395 | v->timestamp = now(CLOCK_MONOTONIC); | |
1396 | ||
1397 | return 0; | |
1398 | } | |
1399 | ||
1400 | int varlink_observeb(Varlink *v, const char *method, ...) { | |
1401 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1402 | va_list ap; | |
1403 | int r; | |
1404 | ||
1405 | assert_return(v, -EINVAL); | |
1406 | ||
1407 | va_start(ap, method); | |
1408 | r = json_buildv(¶meters, ap); | |
1409 | va_end(ap); | |
1410 | ||
1411 | if (r < 0) | |
1412 | return r; | |
1413 | ||
1414 | return varlink_observe(v, method, parameters); | |
d41bd96f LP |
1415 | } |
1416 | ||
1417 | int varlink_call( | |
1418 | Varlink *v, | |
1419 | const char *method, | |
1420 | JsonVariant *parameters, | |
1421 | JsonVariant **ret_parameters, | |
1422 | const char **ret_error_id, | |
1423 | VarlinkReplyFlags *ret_flags) { | |
1424 | ||
1425 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1426 | int r; | |
1427 | ||
1428 | assert_return(v, -EINVAL); | |
1429 | assert_return(method, -EINVAL); | |
1430 | ||
1431 | if (v->state == VARLINK_DISCONNECTED) | |
1432 | return -ENOTCONN; | |
1433 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT)) | |
1434 | return -EBUSY; | |
1435 | ||
1436 | assert(v->n_pending == 0); /* n_pending can't be > 0 if we are in VARLINK_IDLE_CLIENT state */ | |
1437 | ||
1438 | r = varlink_sanitize_parameters(¶meters); | |
1439 | if (r < 0) | |
1440 | return r; | |
1441 | ||
1442 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1443 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1444 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1445 | if (r < 0) | |
1446 | return r; | |
1447 | ||
1448 | r = varlink_enqueue_json(v, m); | |
1449 | if (r < 0) | |
1450 | return r; | |
1451 | ||
1452 | varlink_set_state(v, VARLINK_CALLING); | |
1453 | v->n_pending++; | |
1454 | v->timestamp = now(CLOCK_MONOTONIC); | |
1455 | ||
1456 | while (v->state == VARLINK_CALLING) { | |
1457 | ||
1458 | r = varlink_process(v); | |
1459 | if (r < 0) | |
1460 | return r; | |
1461 | if (r > 0) | |
1462 | continue; | |
1463 | ||
1464 | r = varlink_wait(v, USEC_INFINITY); | |
1465 | if (r < 0) | |
1466 | return r; | |
1467 | } | |
1468 | ||
1469 | switch (v->state) { | |
1470 | ||
1471 | case VARLINK_CALLED: | |
1472 | assert(v->current); | |
1473 | ||
1474 | json_variant_unref(v->reply); | |
1475 | v->reply = TAKE_PTR(v->current); | |
1476 | ||
1477 | varlink_set_state(v, VARLINK_IDLE_CLIENT); | |
1478 | assert(v->n_pending == 1); | |
1479 | v->n_pending--; | |
1480 | ||
1481 | if (ret_parameters) | |
1482 | *ret_parameters = json_variant_by_key(v->reply, "parameters"); | |
1483 | if (ret_error_id) | |
1484 | *ret_error_id = json_variant_string(json_variant_by_key(v->reply, "error")); | |
1485 | if (ret_flags) | |
1486 | *ret_flags = 0; | |
1487 | ||
1488 | return 1; | |
1489 | ||
1490 | case VARLINK_PENDING_DISCONNECT: | |
1491 | case VARLINK_DISCONNECTED: | |
1492 | return -ECONNRESET; | |
1493 | ||
1494 | case VARLINK_PENDING_TIMEOUT: | |
1495 | return -ETIME; | |
1496 | ||
1497 | default: | |
1498 | assert_not_reached("Unexpected state after method call."); | |
1499 | } | |
1500 | } | |
1501 | ||
1502 | int varlink_callb( | |
1503 | Varlink *v, | |
1504 | const char *method, | |
1505 | JsonVariant **ret_parameters, | |
1506 | const char **ret_error_id, | |
1507 | VarlinkReplyFlags *ret_flags, ...) { | |
1508 | ||
1509 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1510 | va_list ap; | |
1511 | int r; | |
1512 | ||
1513 | assert_return(v, -EINVAL); | |
1514 | ||
1515 | va_start(ap, ret_flags); | |
1516 | r = json_buildv(¶meters, ap); | |
1517 | va_end(ap); | |
1518 | ||
1519 | if (r < 0) | |
1520 | return r; | |
1521 | ||
1522 | return varlink_call(v, method, parameters, ret_parameters, ret_error_id, ret_flags); | |
1523 | } | |
1524 | ||
1525 | int varlink_reply(Varlink *v, JsonVariant *parameters) { | |
1526 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1527 | int r; | |
1528 | ||
1529 | assert_return(v, -EINVAL); | |
1530 | ||
1531 | if (v->state == VARLINK_DISCONNECTED) | |
1532 | return -ENOTCONN; | |
1533 | if (!IN_SET(v->state, | |
1534 | VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE, | |
1535 | VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) | |
1536 | return -EBUSY; | |
1537 | ||
1538 | r = varlink_sanitize_parameters(¶meters); | |
1539 | if (r < 0) | |
1540 | return r; | |
1541 | ||
1542 | r = json_build(&m, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1543 | if (r < 0) | |
1544 | return r; | |
1545 | ||
1546 | r = varlink_enqueue_json(v, m); | |
1547 | if (r < 0) | |
1548 | return r; | |
1549 | ||
1550 | if (IN_SET(v->state, VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) { | |
1551 | /* We just replied to a method call that was let hanging for a while (i.e. we were outside of | |
1552 | * the varlink_dispatch_method() stack frame), which means with this reply we are ready to | |
1553 | * process further messages. */ | |
1554 | v->current = json_variant_unref(v->current); | |
1555 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
1556 | } else | |
1557 | /* We replied to a method call from within the varlink_dispatch_method() stack frame), which | |
1558 | * means we should it handle the rest of the state engine. */ | |
1559 | varlink_set_state(v, VARLINK_PROCESSED_METHOD); | |
1560 | ||
1561 | return 1; | |
1562 | } | |
1563 | ||
1564 | int varlink_replyb(Varlink *v, ...) { | |
1565 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1566 | va_list ap; | |
1567 | int r; | |
1568 | ||
1569 | assert_return(v, -EINVAL); | |
1570 | ||
1571 | va_start(ap, v); | |
1572 | r = json_buildv(¶meters, ap); | |
1573 | va_end(ap); | |
1574 | ||
1575 | if (r < 0) | |
1576 | return r; | |
1577 | ||
1578 | return varlink_reply(v, parameters); | |
1579 | } | |
1580 | ||
1581 | int varlink_error(Varlink *v, const char *error_id, JsonVariant *parameters) { | |
1582 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1583 | int r; | |
1584 | ||
1585 | assert_return(v, -EINVAL); | |
1586 | assert_return(error_id, -EINVAL); | |
1587 | ||
1588 | if (v->state == VARLINK_DISCONNECTED) | |
1589 | return -ENOTCONN; | |
1590 | if (!IN_SET(v->state, | |
1591 | VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE, | |
1592 | VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) | |
1593 | return -EBUSY; | |
1594 | ||
1595 | r = varlink_sanitize_parameters(¶meters); | |
1596 | if (r < 0) | |
1597 | return r; | |
1598 | ||
1599 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1600 | JSON_BUILD_PAIR("error", JSON_BUILD_STRING(error_id)), | |
1601 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1602 | if (r < 0) | |
1603 | return r; | |
1604 | ||
1605 | r = varlink_enqueue_json(v, m); | |
1606 | if (r < 0) | |
1607 | return r; | |
1608 | ||
1609 | if (IN_SET(v->state, VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) { | |
1610 | v->current = json_variant_unref(v->current); | |
1611 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
1612 | } else | |
1613 | varlink_set_state(v, VARLINK_PROCESSED_METHOD); | |
1614 | ||
1615 | return 1; | |
1616 | } | |
1617 | ||
1618 | int varlink_errorb(Varlink *v, const char *error_id, ...) { | |
1619 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1620 | va_list ap; | |
1621 | int r; | |
1622 | ||
1623 | assert_return(v, -EINVAL); | |
1624 | assert_return(error_id, -EINVAL); | |
1625 | ||
1626 | va_start(ap, error_id); | |
1627 | r = json_buildv(¶meters, ap); | |
1628 | va_end(ap); | |
1629 | ||
1630 | if (r < 0) | |
1631 | return r; | |
1632 | ||
1633 | return varlink_error(v, error_id, parameters); | |
1634 | } | |
1635 | ||
1636 | int varlink_error_invalid_parameter(Varlink *v, JsonVariant *parameters) { | |
1637 | ||
1638 | assert_return(v, -EINVAL); | |
1639 | assert_return(parameters, -EINVAL); | |
1640 | ||
1641 | /* We expect to be called in one of two ways: the 'parameters' argument is a string variant in which | |
1642 | * case it is the parameter key name that is invalid. Or the 'parameters' argument is an object | |
1643 | * variant in which case we'll pull out the first key. The latter mode is useful in functions that | |
1644 | * don't expect any arguments. */ | |
1645 | ||
1646 | if (json_variant_is_string(parameters)) | |
1647 | return varlink_error(v, VARLINK_ERROR_INVALID_PARAMETER, parameters); | |
1648 | ||
1649 | if (json_variant_is_object(parameters) && | |
1650 | json_variant_elements(parameters) > 0) | |
1651 | return varlink_error(v, VARLINK_ERROR_INVALID_PARAMETER, | |
1652 | json_variant_by_index(parameters, 0)); | |
1653 | ||
1654 | return -EINVAL; | |
1655 | } | |
1656 | ||
1657 | int varlink_notify(Varlink *v, JsonVariant *parameters) { | |
1658 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1659 | int r; | |
1660 | ||
1661 | assert_return(v, -EINVAL); | |
1662 | ||
1663 | if (v->state == VARLINK_DISCONNECTED) | |
1664 | return -ENOTCONN; | |
1665 | if (!IN_SET(v->state, VARLINK_PROCESSING_METHOD_MORE, VARLINK_PENDING_METHOD_MORE)) | |
1666 | return -EBUSY; | |
1667 | ||
1668 | r = varlink_sanitize_parameters(¶meters); | |
1669 | if (r < 0) | |
1670 | return r; | |
1671 | ||
1672 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1673 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
1674 | JSON_BUILD_PAIR("continues", JSON_BUILD_BOOLEAN(true)))); | |
1675 | if (r < 0) | |
1676 | return r; | |
1677 | ||
1678 | r = varlink_enqueue_json(v, m); | |
1679 | if (r < 0) | |
1680 | return r; | |
1681 | ||
1682 | /* No state change, as more is coming */ | |
1683 | return 1; | |
1684 | } | |
1685 | ||
1686 | int varlink_notifyb(Varlink *v, ...) { | |
1687 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1688 | va_list ap; | |
1689 | int r; | |
1690 | ||
1691 | assert_return(v, -EINVAL); | |
1692 | ||
1693 | va_start(ap, v); | |
1694 | r = json_buildv(¶meters, ap); | |
1695 | va_end(ap); | |
1696 | ||
1697 | if (r < 0) | |
1698 | return r; | |
1699 | ||
1700 | return varlink_notify(v, parameters); | |
1701 | } | |
1702 | ||
1703 | int varlink_bind_reply(Varlink *v, VarlinkReply callback) { | |
1704 | assert_return(v, -EINVAL); | |
1705 | ||
1706 | if (callback && v->reply_callback && callback != v->reply_callback) | |
1707 | return -EBUSY; | |
1708 | ||
1709 | v->reply_callback = callback; | |
1710 | ||
1711 | return 0; | |
1712 | } | |
1713 | ||
1714 | void* varlink_set_userdata(Varlink *v, void *userdata) { | |
1715 | void *old; | |
1716 | ||
1717 | assert_return(v, NULL); | |
1718 | ||
1719 | old = v->userdata; | |
1720 | v->userdata = userdata; | |
1721 | ||
1722 | return old; | |
1723 | } | |
1724 | ||
1725 | void* varlink_get_userdata(Varlink *v) { | |
1726 | assert_return(v, NULL); | |
1727 | ||
1728 | return v->userdata; | |
1729 | } | |
1730 | ||
1731 | static int varlink_acquire_ucred(Varlink *v) { | |
1732 | int r; | |
1733 | ||
1734 | assert(v); | |
1735 | ||
1736 | if (v->ucred_acquired) | |
1737 | return 0; | |
1738 | ||
1739 | r = getpeercred(v->fd, &v->ucred); | |
1740 | if (r < 0) | |
1741 | return r; | |
1742 | ||
1743 | v->ucred_acquired = true; | |
1744 | return 0; | |
1745 | } | |
1746 | ||
1747 | int varlink_get_peer_uid(Varlink *v, uid_t *ret) { | |
1748 | int r; | |
1749 | ||
1750 | assert_return(v, -EINVAL); | |
1751 | assert_return(ret, -EINVAL); | |
1752 | ||
1753 | r = varlink_acquire_ucred(v); | |
1754 | if (r < 0) | |
1755 | return r; | |
1756 | ||
1757 | if (!uid_is_valid(v->ucred.uid)) | |
1758 | return -ENODATA; | |
1759 | ||
1760 | *ret = v->ucred.uid; | |
1761 | return 0; | |
1762 | } | |
1763 | ||
1764 | int varlink_get_peer_pid(Varlink *v, pid_t *ret) { | |
1765 | int r; | |
1766 | ||
1767 | assert_return(v, -EINVAL); | |
1768 | assert_return(ret, -EINVAL); | |
1769 | ||
1770 | r = varlink_acquire_ucred(v); | |
1771 | if (r < 0) | |
1772 | return r; | |
1773 | ||
1774 | if (!pid_is_valid(v->ucred.pid)) | |
1775 | return -ENODATA; | |
1776 | ||
1777 | *ret = v->ucred.pid; | |
1778 | return 0; | |
1779 | } | |
1780 | ||
1781 | int varlink_set_relative_timeout(Varlink *v, usec_t timeout) { | |
1782 | assert_return(v, -EINVAL); | |
1783 | assert_return(timeout > 0, -EINVAL); | |
1784 | ||
1785 | v->timeout = timeout; | |
1786 | return 0; | |
1787 | } | |
1788 | ||
1789 | VarlinkServer *varlink_get_server(Varlink *v) { | |
1790 | assert_return(v, NULL); | |
1791 | ||
1792 | return v->server; | |
1793 | } | |
1794 | ||
1795 | int varlink_set_description(Varlink *v, const char *description) { | |
1796 | assert_return(v, -EINVAL); | |
1797 | ||
1798 | return free_and_strdup(&v->description, description); | |
1799 | } | |
1800 | ||
1801 | static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
1802 | Varlink *v = userdata; | |
1803 | ||
1804 | assert(s); | |
1805 | assert(v); | |
1806 | ||
1807 | handle_revents(v, revents); | |
1808 | (void) varlink_process(v); | |
1809 | ||
1810 | return 1; | |
1811 | } | |
1812 | ||
1813 | static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) { | |
1814 | Varlink *v = userdata; | |
1815 | ||
1816 | assert(s); | |
1817 | assert(v); | |
1818 | ||
1819 | (void) varlink_process(v); | |
1820 | return 1; | |
1821 | } | |
1822 | ||
1823 | static int defer_callback(sd_event_source *s, void *userdata) { | |
1824 | Varlink *v = userdata; | |
1825 | ||
1826 | assert(s); | |
1827 | assert(v); | |
1828 | ||
1829 | (void) varlink_process(v); | |
1830 | return 1; | |
1831 | } | |
1832 | ||
1833 | static int prepare_callback(sd_event_source *s, void *userdata) { | |
1834 | Varlink *v = userdata; | |
1835 | int r, e; | |
1836 | usec_t until; | |
1837 | ||
1838 | assert(s); | |
1839 | assert(v); | |
1840 | ||
1841 | e = varlink_get_events(v); | |
1842 | if (e < 0) | |
1843 | return e; | |
1844 | ||
1845 | r = sd_event_source_set_io_events(v->io_event_source, e); | |
1846 | if (r < 0) | |
1847 | return r; | |
1848 | ||
1849 | r = varlink_get_timeout(v, &until); | |
1850 | if (r < 0) | |
1851 | return r; | |
1852 | if (r > 0) { | |
1853 | r = sd_event_source_set_time(v->time_event_source, until); | |
1854 | if (r < 0) | |
1855 | return r; | |
1856 | } | |
1857 | ||
1858 | r = sd_event_source_set_enabled(v->time_event_source, r > 0 ? SD_EVENT_ON : SD_EVENT_OFF); | |
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) { | |
2409 | struct rlimit rl; | |
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 | ||
2415 | assert_se(getrlimit(RLIMIT_NOFILE, &rl) >= 0); | |
2416 | ||
2417 | /* Make sure we never use up more than ¾th of RLIMIT_NOFILE for IPC */ | |
2418 | if (VARLINK_DEFAULT_CONNECTIONS_MAX > rl.rlim_cur / 4 * 3) | |
2419 | return rl.rlim_cur / 4 * 3; | |
2420 | ||
2421 | return VARLINK_DEFAULT_CONNECTIONS_MAX; | |
2422 | } | |
2423 | ||
2424 | unsigned varlink_server_connections_per_uid_max(VarlinkServer *s) { | |
2425 | unsigned m; | |
2426 | ||
2427 | if (s) | |
2428 | return s->connections_per_uid_max; | |
2429 | ||
2430 | /* Make sure to never use up more than ¾th of available connections for a single user */ | |
2431 | m = varlink_server_connections_max(NULL); | |
2432 | if (VARLINK_DEFAULT_CONNECTIONS_PER_UID_MAX > m) | |
2433 | return m / 4 * 3; | |
2434 | ||
2435 | return VARLINK_DEFAULT_CONNECTIONS_PER_UID_MAX; | |
2436 | } | |
2437 | ||
2438 | int varlink_server_set_connections_per_uid_max(VarlinkServer *s, unsigned m) { | |
2439 | assert_return(s, -EINVAL); | |
2440 | assert_return(m > 0, -EINVAL); | |
2441 | ||
2442 | s->connections_per_uid_max = m; | |
2443 | return 0; | |
2444 | } | |
2445 | ||
2446 | int varlink_server_set_connections_max(VarlinkServer *s, unsigned m) { | |
2447 | assert_return(s, -EINVAL); | |
2448 | assert_return(m > 0, -EINVAL); | |
2449 | ||
2450 | s->connections_max = m; | |
2451 | return 0; | |
2452 | } | |
2453 | ||
2454 | int varlink_server_set_description(VarlinkServer *s, const char *description) { | |
2455 | assert_return(s, -EINVAL); | |
2456 | ||
2457 | return free_and_strdup(&s->description, description); | |
2458 | } |