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