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