]>
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 | 5 | |
206504a5 LP |
6 | #include <sd-daemon.h> |
7 | ||
d41bd96f LP |
8 | #include "alloc-util.h" |
9 | #include "errno-util.h" | |
10 | #include "fd-util.h" | |
e2341b6b | 11 | #include "glyph-util.h" |
d41bd96f | 12 | #include "hashmap.h" |
0f2d351f | 13 | #include "io-util.h" |
bd1ae178 | 14 | #include "iovec-util.h" |
d41bd96f | 15 | #include "list.h" |
0f274a87 | 16 | #include "path-util.h" |
d41bd96f | 17 | #include "process-util.h" |
63e00ccd | 18 | #include "selinux-util.h" |
008798e9 | 19 | #include "serialize.h" |
d41bd96f LP |
20 | #include "set.h" |
21 | #include "socket-util.h" | |
22 | #include "string-table.h" | |
23 | #include "string-util.h" | |
24 | #include "strv.h" | |
25 | #include "time-util.h" | |
26 | #include "umask-util.h" | |
27 | #include "user-util.h" | |
28 | #include "varlink.h" | |
008798e9 | 29 | #include "varlink-internal.h" |
7e006b49 LP |
30 | #include "varlink-org.varlink.service.h" |
31 | #include "varlink-io.systemd.h" | |
32 | #include "version.h" | |
d41bd96f LP |
33 | |
34 | #define VARLINK_DEFAULT_CONNECTIONS_MAX 4096U | |
35 | #define VARLINK_DEFAULT_CONNECTIONS_PER_UID_MAX 1024U | |
36 | ||
37 | #define VARLINK_DEFAULT_TIMEOUT_USEC (45U*USEC_PER_SEC) | |
38 | #define VARLINK_BUFFER_MAX (16U*1024U*1024U) | |
39 | #define VARLINK_READ_SIZE (64U*1024U) | |
40 | ||
41 | typedef enum VarlinkState { | |
42 | /* Client side states */ | |
43 | VARLINK_IDLE_CLIENT, | |
44 | VARLINK_AWAITING_REPLY, | |
45a6c965 | 45 | VARLINK_AWAITING_REPLY_MORE, |
d41bd96f LP |
46 | VARLINK_CALLING, |
47 | VARLINK_CALLED, | |
48 | VARLINK_PROCESSING_REPLY, | |
49 | ||
50 | /* Server side states */ | |
51 | VARLINK_IDLE_SERVER, | |
52 | VARLINK_PROCESSING_METHOD, | |
53 | VARLINK_PROCESSING_METHOD_MORE, | |
54 | VARLINK_PROCESSING_METHOD_ONEWAY, | |
55 | VARLINK_PROCESSED_METHOD, | |
d41bd96f LP |
56 | VARLINK_PENDING_METHOD, |
57 | VARLINK_PENDING_METHOD_MORE, | |
58 | ||
59 | /* Common states (only during shutdown) */ | |
60 | VARLINK_PENDING_DISCONNECT, | |
61 | VARLINK_PENDING_TIMEOUT, | |
62 | VARLINK_PROCESSING_DISCONNECT, | |
63 | VARLINK_PROCESSING_TIMEOUT, | |
64 | VARLINK_PROCESSING_FAILURE, | |
65 | VARLINK_DISCONNECTED, | |
66 | ||
67 | _VARLINK_STATE_MAX, | |
2d93c20e | 68 | _VARLINK_STATE_INVALID = -EINVAL, |
d41bd96f LP |
69 | } VarlinkState; |
70 | ||
71 | /* Tests whether we are not yet disconnected. Note that this is true during all states where the connection | |
72 | * is still good for something, and false only when it's dead for good. This means: when we are | |
73 | * asynchronously connecting to a peer and the connect() is still pending, then this will return 'true', as | |
74 | * the connection is still good, and we are likely to be able to properly operate on it soon. */ | |
75 | #define VARLINK_STATE_IS_ALIVE(state) \ | |
76 | IN_SET(state, \ | |
77 | VARLINK_IDLE_CLIENT, \ | |
78 | VARLINK_AWAITING_REPLY, \ | |
45a6c965 | 79 | VARLINK_AWAITING_REPLY_MORE, \ |
d41bd96f LP |
80 | VARLINK_CALLING, \ |
81 | VARLINK_CALLED, \ | |
82 | VARLINK_PROCESSING_REPLY, \ | |
83 | VARLINK_IDLE_SERVER, \ | |
84 | VARLINK_PROCESSING_METHOD, \ | |
85 | VARLINK_PROCESSING_METHOD_MORE, \ | |
86 | VARLINK_PROCESSING_METHOD_ONEWAY, \ | |
87 | VARLINK_PROCESSED_METHOD, \ | |
d41bd96f LP |
88 | VARLINK_PENDING_METHOD, \ |
89 | VARLINK_PENDING_METHOD_MORE) | |
90 | ||
d37cdac6 LP |
91 | typedef struct VarlinkJsonQueueItem VarlinkJsonQueueItem; |
92 | ||
93 | /* A queued message we shall write into the socket, along with the file descriptors to send at the same | |
94 | * time. This queue item binds them together so that message/fd boundaries are maintained throughout the | |
95 | * whole pipeline. */ | |
96 | struct VarlinkJsonQueueItem { | |
97 | LIST_FIELDS(VarlinkJsonQueueItem, queue); | |
98 | JsonVariant *data; | |
99 | size_t n_fds; | |
100 | int fds[]; | |
101 | }; | |
102 | ||
d41bd96f LP |
103 | struct Varlink { |
104 | unsigned n_ref; | |
105 | ||
106 | VarlinkServer *server; | |
107 | ||
108 | VarlinkState state; | |
109 | bool connecting; /* This boolean indicates whether the socket fd we are operating on is currently | |
110 | * processing an asynchronous connect(). In that state we watch the socket for | |
111 | * EPOLLOUT, but we refrain from calling read() or write() on the socket as that | |
112 | * will trigger ENOTCONN. Note that this boolean is kept separate from the | |
113 | * VarlinkState above on purpose: while the connect() is still not complete we | |
114 | * already want to allow queuing of messages and similar. Thus it's nice to keep | |
115 | * these two state concepts separate: the VarlinkState encodes what our own view of | |
116 | * the connection is, i.e. whether we think it's a server, a client, and has | |
117 | * something queued already, while 'connecting' tells us a detail about the | |
118 | * transport used below, that should have no effect on how we otherwise accept and | |
119 | * process operations from the user. | |
120 | * | |
121 | * Or to say this differently: VARLINK_STATE_IS_ALIVE(state) tells you whether the | |
122 | * connection is good to use, even if it might not be fully connected | |
123 | * yet. connecting=true then informs you that actually we are still connecting, and | |
124 | * the connection is actually not established yet and thus any requests you enqueue | |
125 | * now will still work fine but will be queued only, not sent yet, but that | |
126 | * shouldn't stop you from using the connection, since eventually whatever you queue | |
127 | * *will* be sent. | |
128 | * | |
129 | * Or to say this even differently: 'state' is a high-level ("application layer" | |
130 | * high, if you so will) state, while 'conecting' is a low-level ("transport layer" | |
131 | * low, if you so will) state, and while they are not entirely unrelated and | |
132 | * sometimes propagate effects to each other they are only asynchronously connected | |
133 | * at most. */ | |
134 | unsigned n_pending; | |
135 | ||
136 | int fd; | |
137 | ||
138 | char *input_buffer; /* valid data starts at input_buffer_index, ends at input_buffer_index+input_buffer_size */ | |
d41bd96f LP |
139 | size_t input_buffer_index; |
140 | size_t input_buffer_size; | |
141 | size_t input_buffer_unscanned; | |
142 | ||
3c868058 LP |
143 | void *input_control_buffer; |
144 | size_t input_control_buffer_size; | |
145 | ||
d41bd96f | 146 | char *output_buffer; /* valid data starts at output_buffer_index, ends at output_buffer_index+output_buffer_size */ |
d41bd96f LP |
147 | size_t output_buffer_index; |
148 | size_t output_buffer_size; | |
149 | ||
d37cdac6 LP |
150 | int *input_fds; /* file descriptors associated with the data in input_buffer (for fd passing) */ |
151 | size_t n_input_fds; | |
152 | ||
153 | int *output_fds; /* file descriptors associated with the data in output_buffer (for fd passing) */ | |
154 | size_t n_output_fds; | |
155 | ||
156 | /* Further messages to output not yet formatted into text, and thus not included in output_buffer | |
157 | * yet. We keep them separate from output_buffer, to not violate fd message boundaries: we want that | |
158 | * each fd that is sent is associated with its fds, and that fds cannot be accidentally associated | |
94d82b59 | 159 | * with preceding or following messages. */ |
d37cdac6 LP |
160 | LIST_HEAD(VarlinkJsonQueueItem, output_queue); |
161 | VarlinkJsonQueueItem *output_queue_tail; | |
162 | ||
163 | /* The fds to associate with the next message that is about to be enqueued. The user first pushes the | |
164 | * fds it intends to send via varlink_push_fd() into this queue, and then once the message data is | |
165 | * submitted we'll combine the fds and the message data into one. */ | |
166 | int *pushed_fds; | |
167 | size_t n_pushed_fds; | |
168 | ||
d41bd96f LP |
169 | VarlinkReply reply_callback; |
170 | ||
171 | JsonVariant *current; | |
7e006b49 | 172 | VarlinkSymbol *current_method; |
d41bd96f LP |
173 | |
174 | struct ucred ucred; | |
175 | bool ucred_acquired:1; | |
176 | ||
177 | bool write_disconnected:1; | |
178 | bool read_disconnected:1; | |
179 | bool prefer_read_write:1; | |
180 | bool got_pollhup:1; | |
181 | ||
d37cdac6 LP |
182 | bool allow_fd_passing_input:1; |
183 | bool allow_fd_passing_output:1; | |
184 | ||
db1f7c84 LP |
185 | bool output_buffer_sensitive:1; /* whether to erase the output buffer after writing it to the socket */ |
186 | ||
d37cdac6 LP |
187 | int af; /* address family if socket; AF_UNSPEC if not socket; negative if not known */ |
188 | ||
d41bd96f LP |
189 | usec_t timestamp; |
190 | usec_t timeout; | |
191 | ||
192 | void *userdata; | |
193 | char *description; | |
194 | ||
195 | sd_event *event; | |
196 | sd_event_source *io_event_source; | |
197 | sd_event_source *time_event_source; | |
198 | sd_event_source *quit_event_source; | |
199 | sd_event_source *defer_event_source; | |
0f274a87 LP |
200 | |
201 | pid_t exec_pid; | |
d41bd96f LP |
202 | }; |
203 | ||
204 | typedef struct VarlinkServerSocket VarlinkServerSocket; | |
205 | ||
206 | struct VarlinkServerSocket { | |
207 | VarlinkServer *server; | |
208 | ||
209 | int fd; | |
210 | char *address; | |
211 | ||
212 | sd_event_source *event_source; | |
213 | ||
214 | LIST_FIELDS(VarlinkServerSocket, sockets); | |
215 | }; | |
216 | ||
217 | struct VarlinkServer { | |
218 | unsigned n_ref; | |
219 | VarlinkServerFlags flags; | |
220 | ||
221 | LIST_HEAD(VarlinkServerSocket, sockets); | |
222 | ||
7e006b49 LP |
223 | Hashmap *methods; /* Fully qualified symbol name of a method → VarlinkMethod */ |
224 | Hashmap *interfaces; /* Fully qualified interface name → VarlinkInterface* */ | |
939630ae | 225 | Hashmap *symbols; /* Fully qualified symbol name of method/error → VarlinkSymbol* */ |
d41bd96f | 226 | VarlinkConnect connect_callback; |
6d4d6002 | 227 | VarlinkDisconnect disconnect_callback; |
d41bd96f LP |
228 | |
229 | sd_event *event; | |
230 | int64_t event_priority; | |
231 | ||
232 | unsigned n_connections; | |
7e006b49 | 233 | Hashmap *by_uid; /* UID_TO_PTR(uid) → UINT_TO_PTR(n_connections) */ |
d41bd96f LP |
234 | |
235 | void *userdata; | |
236 | char *description; | |
237 | ||
238 | unsigned connections_max; | |
239 | unsigned connections_per_uid_max; | |
47723340 LP |
240 | |
241 | bool exit_on_idle; | |
d41bd96f LP |
242 | }; |
243 | ||
1bd0b9c0 AS |
244 | typedef struct VarlinkCollectContext { |
245 | JsonVariant *parameters; | |
246 | const char *error_id; | |
247 | VarlinkReplyFlags flags; | |
248 | } VarlinkCollectContext ; | |
249 | ||
d41bd96f LP |
250 | static const char* const varlink_state_table[_VARLINK_STATE_MAX] = { |
251 | [VARLINK_IDLE_CLIENT] = "idle-client", | |
252 | [VARLINK_AWAITING_REPLY] = "awaiting-reply", | |
45a6c965 | 253 | [VARLINK_AWAITING_REPLY_MORE] = "awaiting-reply-more", |
d41bd96f LP |
254 | [VARLINK_CALLING] = "calling", |
255 | [VARLINK_CALLED] = "called", | |
256 | [VARLINK_PROCESSING_REPLY] = "processing-reply", | |
257 | [VARLINK_IDLE_SERVER] = "idle-server", | |
258 | [VARLINK_PROCESSING_METHOD] = "processing-method", | |
259 | [VARLINK_PROCESSING_METHOD_MORE] = "processing-method-more", | |
260 | [VARLINK_PROCESSING_METHOD_ONEWAY] = "processing-method-oneway", | |
261 | [VARLINK_PROCESSED_METHOD] = "processed-method", | |
d41bd96f LP |
262 | [VARLINK_PENDING_METHOD] = "pending-method", |
263 | [VARLINK_PENDING_METHOD_MORE] = "pending-method-more", | |
264 | [VARLINK_PENDING_DISCONNECT] = "pending-disconnect", | |
265 | [VARLINK_PENDING_TIMEOUT] = "pending-timeout", | |
266 | [VARLINK_PROCESSING_DISCONNECT] = "processing-disconnect", | |
267 | [VARLINK_PROCESSING_TIMEOUT] = "processing-timeout", | |
268 | [VARLINK_PROCESSING_FAILURE] = "processing-failure", | |
269 | [VARLINK_DISCONNECTED] = "disconnected", | |
270 | }; | |
271 | ||
272 | DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(varlink_state, VarlinkState); | |
273 | ||
274 | #define varlink_log_errno(v, error, fmt, ...) \ | |
275 | log_debug_errno(error, "%s: " fmt, varlink_description(v), ##__VA_ARGS__) | |
276 | ||
277 | #define varlink_log(v, fmt, ...) \ | |
278 | log_debug("%s: " fmt, varlink_description(v), ##__VA_ARGS__) | |
279 | ||
280 | #define varlink_server_log_errno(s, error, fmt, ...) \ | |
281 | log_debug_errno(error, "%s: " fmt, varlink_server_description(s), ##__VA_ARGS__) | |
282 | ||
283 | #define varlink_server_log(s, fmt, ...) \ | |
284 | log_debug("%s: " fmt, varlink_server_description(s), ##__VA_ARGS__) | |
285 | ||
d37cdac6 | 286 | static int varlink_format_queue(Varlink *v); |
47723340 | 287 | static void varlink_server_test_exit_on_idle(VarlinkServer *s); |
d37cdac6 | 288 | |
cf1ab844 | 289 | static const char *varlink_description(Varlink *v) { |
f35e9b10 | 290 | return (v ? v->description : NULL) ?: "varlink"; |
d41bd96f LP |
291 | } |
292 | ||
cf1ab844 | 293 | static const char *varlink_server_description(VarlinkServer *s) { |
f35e9b10 | 294 | return (s ? s->description : NULL) ?: "varlink"; |
d41bd96f LP |
295 | } |
296 | ||
d37cdac6 LP |
297 | static VarlinkJsonQueueItem *varlink_json_queue_item_free(VarlinkJsonQueueItem *q) { |
298 | if (!q) | |
299 | return NULL; | |
300 | ||
301 | json_variant_unref(q->data); | |
302 | close_many(q->fds, q->n_fds); | |
303 | ||
304 | return mfree(q); | |
305 | } | |
306 | ||
307 | static VarlinkJsonQueueItem *varlink_json_queue_item_new(JsonVariant *m, const int fds[], size_t n_fds) { | |
308 | VarlinkJsonQueueItem *q; | |
309 | ||
310 | assert(m); | |
311 | assert(fds || n_fds == 0); | |
312 | ||
313 | q = malloc(offsetof(VarlinkJsonQueueItem, fds) + sizeof(int) * n_fds); | |
314 | if (!q) | |
315 | return NULL; | |
316 | ||
317 | *q = (VarlinkJsonQueueItem) { | |
318 | .data = json_variant_ref(m), | |
319 | .n_fds = n_fds, | |
320 | }; | |
321 | ||
322 | memcpy_safe(q->fds, fds, n_fds * sizeof(int)); | |
323 | ||
324 | return TAKE_PTR(q); | |
325 | } | |
326 | ||
d41bd96f LP |
327 | static void varlink_set_state(Varlink *v, VarlinkState state) { |
328 | assert(v); | |
77740b59 ZJS |
329 | assert(state >= 0 && state < _VARLINK_STATE_MAX); |
330 | ||
331 | if (v->state < 0) | |
953394e3 | 332 | varlink_log(v, "Setting state %s", |
77740b59 ZJS |
333 | varlink_state_to_string(state)); |
334 | else | |
e2341b6b | 335 | varlink_log(v, "Changing state %s %s %s", |
77740b59 | 336 | varlink_state_to_string(v->state), |
e2341b6b | 337 | special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), |
77740b59 | 338 | varlink_state_to_string(state)); |
d41bd96f LP |
339 | |
340 | v->state = state; | |
341 | } | |
342 | ||
343 | static int varlink_new(Varlink **ret) { | |
344 | Varlink *v; | |
345 | ||
346 | assert(ret); | |
347 | ||
a48481dc | 348 | v = new(Varlink, 1); |
d41bd96f LP |
349 | if (!v) |
350 | return -ENOMEM; | |
351 | ||
352 | *v = (Varlink) { | |
353 | .n_ref = 1, | |
254d1313 | 354 | .fd = -EBADF, |
d41bd96f LP |
355 | |
356 | .state = _VARLINK_STATE_INVALID, | |
357 | ||
a995ce47 | 358 | .ucred = UCRED_INVALID, |
d41bd96f LP |
359 | |
360 | .timestamp = USEC_INFINITY, | |
d37cdac6 LP |
361 | .timeout = VARLINK_DEFAULT_TIMEOUT_USEC, |
362 | ||
363 | .af = -1, | |
d41bd96f LP |
364 | }; |
365 | ||
366 | *ret = v; | |
367 | return 0; | |
368 | } | |
369 | ||
370 | int varlink_connect_address(Varlink **ret, const char *address) { | |
371 | _cleanup_(varlink_unrefp) Varlink *v = NULL; | |
372 | union sockaddr_union sockaddr; | |
373 | int r; | |
374 | ||
375 | assert_return(ret, -EINVAL); | |
376 | assert_return(address, -EINVAL); | |
377 | ||
d41bd96f LP |
378 | r = varlink_new(&v); |
379 | if (r < 0) | |
db3d4222 | 380 | return log_debug_errno(r, "Failed to create varlink object: %m"); |
d41bd96f LP |
381 | |
382 | v->fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
383 | if (v->fd < 0) | |
db3d4222 | 384 | return log_debug_errno(errno, "Failed to create AF_UNIX socket: %m"); |
d41bd96f | 385 | |
a0c41de2 | 386 | v->fd = fd_move_above_stdio(v->fd); |
d37cdac6 | 387 | v->af = AF_UNIX; |
a0c41de2 | 388 | |
1861986a LP |
389 | r = sockaddr_un_set_path(&sockaddr.un, address); |
390 | if (r < 0) { | |
391 | if (r != -ENAMETOOLONG) | |
392 | return log_debug_errno(r, "Failed to set socket address '%s': %m", address); | |
393 | ||
394 | /* This is a file system path, and too long to fit into sockaddr_un. Let's connect via O_PATH | |
395 | * to this socket. */ | |
396 | ||
397 | r = connect_unix_path(v->fd, AT_FDCWD, address); | |
398 | } else | |
399 | r = RET_NERRNO(connect(v->fd, &sockaddr.sa, r)); | |
400 | ||
401 | if (r < 0) { | |
402 | if (!IN_SET(r, -EAGAIN, -EINPROGRESS)) | |
403 | return log_debug_errno(r, "Failed to connect to %s: %m", address); | |
d41bd96f LP |
404 | |
405 | v->connecting = true; /* We are asynchronously connecting, i.e. the connect() is being | |
406 | * processed in the background. As long as that's the case the socket | |
407 | * is in a special state: it's there, we can poll it for EPOLLOUT, but | |
408 | * if we attempt to write() to it before we see EPOLLOUT we'll get | |
409 | * ENOTCONN (and not EAGAIN, like we would for a normal connected | |
410 | * socket that isn't writable at the moment). Since ENOTCONN on write() | |
411 | * hence can mean two different things (i.e. connection not complete | |
412 | * yet vs. already disconnected again), we store as a boolean whether | |
413 | * we are still in connect(). */ | |
414 | } | |
415 | ||
416 | varlink_set_state(v, VARLINK_IDLE_CLIENT); | |
417 | ||
418 | *ret = TAKE_PTR(v); | |
db3d4222 | 419 | return 0; |
d41bd96f LP |
420 | } |
421 | ||
0f274a87 | 422 | int varlink_connect_exec(Varlink **ret, const char *_command, char **_argv) { |
71136404 | 423 | _cleanup_close_pair_ int pair[2] = EBADF_PAIR; |
0f274a87 LP |
424 | _cleanup_(sigkill_waitp) pid_t pid = 0; |
425 | _cleanup_free_ char *command = NULL; | |
426 | _cleanup_strv_free_ char **argv = NULL; | |
427 | int r; | |
428 | ||
429 | assert_return(ret, -EINVAL); | |
430 | assert_return(_command, -EINVAL); | |
431 | ||
432 | /* Copy the strings, in case they point into our own argv[], which we'll invalidate shortly because | |
433 | * we rename the child process */ | |
434 | command = strdup(_command); | |
435 | if (!command) | |
436 | return -ENOMEM; | |
437 | ||
438 | if (strv_isempty(_argv)) | |
439 | argv = strv_new(command); | |
440 | else | |
441 | argv = strv_copy(_argv); | |
442 | if (!argv) | |
443 | return -ENOMEM; | |
444 | ||
445 | log_debug("Forking off Varlink child process '%s'.", command); | |
446 | ||
447 | if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0, pair) < 0) | |
448 | return log_debug_errno(errno, "Failed to allocate AF_UNIX socket pair: %m"); | |
449 | ||
450 | r = safe_fork_full( | |
451 | "(sd-vlexec)", | |
452 | /* stdio_fds= */ NULL, | |
453 | /* except_fds= */ (int[]) { pair[1] }, | |
454 | /* n_except_fds= */ 1, | |
e9ccae31 | 455 | FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE, |
0f274a87 LP |
456 | &pid); |
457 | if (r < 0) | |
458 | return log_debug_errno(r, "Failed to spawn process: %m"); | |
459 | if (r == 0) { | |
460 | char spid[DECIMAL_STR_MAX(pid_t)+1]; | |
461 | const char *setenv_list[] = { | |
462 | "LISTEN_FDS", "1", | |
463 | "LISTEN_PID", spid, | |
464 | "LISTEN_FDNAMES", "varlink", | |
465 | NULL, NULL, | |
466 | }; | |
467 | /* Child */ | |
468 | ||
469 | pair[0] = -EBADF; | |
470 | ||
471 | r = move_fd(pair[1], 3, /* cloexec= */ false); | |
472 | if (r < 0) { | |
473 | log_debug_errno(r, "Failed to move file descriptor to 3: %m"); | |
474 | _exit(EXIT_FAILURE); | |
475 | } | |
476 | ||
477 | xsprintf(spid, PID_FMT, pid); | |
478 | ||
479 | STRV_FOREACH_PAIR(a, b, setenv_list) { | |
480 | if (setenv(*a, *b, /* override= */ true) < 0) { | |
481 | log_debug_errno(errno, "Failed to set environment variable '%s': %m", *a); | |
482 | _exit(EXIT_FAILURE); | |
483 | } | |
484 | } | |
485 | ||
486 | execvp(command, argv); | |
487 | log_debug_errno(r, "Failed to invoke process '%s': %m", command); | |
488 | _exit(EXIT_FAILURE); | |
489 | } | |
490 | ||
491 | pair[1] = safe_close(pair[1]); | |
492 | ||
493 | Varlink *v; | |
494 | r = varlink_new(&v); | |
495 | if (r < 0) | |
496 | return log_debug_errno(r, "Failed to create varlink object: %m"); | |
497 | ||
498 | v->fd = TAKE_FD(pair[0]); | |
499 | v->af = AF_UNIX; | |
500 | v->exec_pid = TAKE_PID(pid); | |
501 | varlink_set_state(v, VARLINK_IDLE_CLIENT); | |
502 | ||
503 | *ret = v; | |
504 | return 0; | |
505 | } | |
506 | ||
507 | int varlink_connect_url(Varlink **ret, const char *url) { | |
508 | _cleanup_free_ char *c = NULL; | |
509 | const char *p; | |
510 | bool exec; | |
cf3d95b2 | 511 | int r; |
0f274a87 LP |
512 | |
513 | assert_return(ret, -EINVAL); | |
514 | assert_return(url, -EINVAL); | |
515 | ||
516 | // FIXME: Add support for vsock:, ssh-exec:, ssh-unix: URL schemes here. (The latter with OpenSSH | |
517 | // 9.4's -W switch for referencing remote AF_UNIX sockets.) | |
518 | ||
519 | /* The Varlink URL scheme is a bit underdefined. We support only the unix: transport for now, plus an | |
520 | * exec: transport we made up ourselves. Strictly speaking this shouldn't even be called URL, since | |
521 | * it has nothing to do with Internet URLs by RFC. */ | |
522 | ||
523 | p = startswith(url, "unix:"); | |
524 | if (p) | |
525 | exec = false; | |
526 | else { | |
527 | p = startswith(url, "exec:"); | |
528 | if (!p) | |
529 | return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "URL scheme not supported."); | |
530 | ||
531 | exec = true; | |
532 | } | |
533 | ||
534 | /* The varlink.org reference C library supports more than just file system paths. We might want to | |
535 | * support that one day too. For now simply refuse that. */ | |
536 | if (p[strcspn(p, ";?#")] != '\0') | |
537 | return log_debug_errno(SYNTHETIC_ERRNO(EPROTONOSUPPORT), "URL parameterization with ';', '?', '#' not supported."); | |
538 | ||
539 | if (exec || p[0] != '@') { /* no validity checks for abstract namespace */ | |
540 | ||
541 | if (!path_is_absolute(p)) | |
542 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Specified path not absolute, refusing."); | |
543 | ||
cf3d95b2 YW |
544 | r = path_simplify_alloc(p, &c); |
545 | if (r < 0) | |
546 | return r; | |
0f274a87 LP |
547 | |
548 | if (!path_is_normalized(c)) | |
549 | return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Specified path is not normalized, refusing."); | |
550 | } | |
551 | ||
552 | if (exec) | |
86f6d32e | 553 | return varlink_connect_exec(ret, c, NULL); |
0f274a87 LP |
554 | |
555 | return varlink_connect_address(ret, c ?: p); | |
556 | } | |
557 | ||
d41bd96f LP |
558 | int varlink_connect_fd(Varlink **ret, int fd) { |
559 | Varlink *v; | |
560 | int r; | |
561 | ||
562 | assert_return(ret, -EINVAL); | |
563 | assert_return(fd >= 0, -EBADF); | |
564 | ||
565 | r = fd_nonblock(fd, true); | |
566 | if (r < 0) | |
db3d4222 | 567 | return log_debug_errno(r, "Failed to make fd %d nonblocking: %m", fd); |
d41bd96f LP |
568 | |
569 | r = varlink_new(&v); | |
570 | if (r < 0) | |
db3d4222 | 571 | return log_debug_errno(r, "Failed to create varlink object: %m"); |
d41bd96f LP |
572 | |
573 | v->fd = fd; | |
d37cdac6 | 574 | v->af = -1, |
d41bd96f LP |
575 | varlink_set_state(v, VARLINK_IDLE_CLIENT); |
576 | ||
577 | /* Note that if this function is called we assume the passed socket (if it is one) is already | |
578 | * properly connected, i.e. any asynchronous connect() done on it already completed. Because of that | |
579 | * we'll not set the 'connecting' boolean here, i.e. we don't need to avoid write()ing to the socket | |
580 | * until the connection is fully set up. Behaviour here is hence a bit different from | |
581 | * varlink_connect_address() above, as there we do handle asynchronous connections ourselves and | |
582 | * avoid doing write() on it before we saw EPOLLOUT for the first time. */ | |
583 | ||
584 | *ret = v; | |
585 | return 0; | |
586 | } | |
587 | ||
588 | static void varlink_detach_event_sources(Varlink *v) { | |
589 | assert(v); | |
590 | ||
1d3fe304 | 591 | v->io_event_source = sd_event_source_disable_unref(v->io_event_source); |
1d3fe304 | 592 | v->time_event_source = sd_event_source_disable_unref(v->time_event_source); |
1d3fe304 | 593 | v->quit_event_source = sd_event_source_disable_unref(v->quit_event_source); |
1d3fe304 | 594 | v->defer_event_source = sd_event_source_disable_unref(v->defer_event_source); |
d41bd96f LP |
595 | } |
596 | ||
790446bd LP |
597 | static void varlink_clear_current(Varlink *v) { |
598 | assert(v); | |
599 | ||
600 | /* Clears the currently processed incoming message */ | |
601 | v->current = json_variant_unref(v->current); | |
7e006b49 | 602 | v->current_method = NULL; |
d37cdac6 LP |
603 | |
604 | close_many(v->input_fds, v->n_input_fds); | |
605 | v->input_fds = mfree(v->input_fds); | |
606 | v->n_input_fds = 0; | |
790446bd LP |
607 | } |
608 | ||
d41bd96f LP |
609 | static void varlink_clear(Varlink *v) { |
610 | assert(v); | |
611 | ||
612 | varlink_detach_event_sources(v); | |
613 | ||
614 | v->fd = safe_close(v->fd); | |
615 | ||
d37cdac6 LP |
616 | varlink_clear_current(v); |
617 | ||
d41bd96f | 618 | v->input_buffer = mfree(v->input_buffer); |
db1f7c84 | 619 | v->output_buffer = v->output_buffer_sensitive ? erase_and_free(v->output_buffer) : mfree(v->output_buffer); |
d41bd96f | 620 | |
3c868058 LP |
621 | v->input_control_buffer = mfree(v->input_control_buffer); |
622 | v->input_control_buffer_size = 0; | |
623 | ||
d37cdac6 LP |
624 | close_many(v->output_fds, v->n_output_fds); |
625 | v->output_fds = mfree(v->output_fds); | |
626 | v->n_output_fds = 0; | |
627 | ||
628 | close_many(v->pushed_fds, v->n_pushed_fds); | |
629 | v->pushed_fds = mfree(v->pushed_fds); | |
630 | v->n_pushed_fds = 0; | |
631 | ||
9aad490e | 632 | LIST_CLEAR(queue, v->output_queue, varlink_json_queue_item_free); |
d37cdac6 LP |
633 | v->output_queue_tail = NULL; |
634 | ||
d41bd96f | 635 | v->event = sd_event_unref(v->event); |
0f274a87 LP |
636 | |
637 | if (v->exec_pid > 0) { | |
638 | sigterm_wait(v->exec_pid); | |
639 | v->exec_pid = 0; | |
640 | } | |
d41bd96f LP |
641 | } |
642 | ||
643 | static Varlink* varlink_destroy(Varlink *v) { | |
644 | if (!v) | |
645 | return NULL; | |
646 | ||
647 | /* If this is called the server object must already been unreffed here. Why that? because when we | |
648 | * linked up the varlink connection with the server object we took one ref in each direction */ | |
649 | assert(!v->server); | |
650 | ||
651 | varlink_clear(v); | |
652 | ||
653 | free(v->description); | |
654 | return mfree(v); | |
655 | } | |
656 | ||
657 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Varlink, varlink, varlink_destroy); | |
658 | ||
659 | static int varlink_test_disconnect(Varlink *v) { | |
660 | assert(v); | |
661 | ||
37b22b3b | 662 | /* Tests whether we the connection has been terminated. We are careful to not stop processing it |
d41bd96f LP |
663 | * prematurely, since we want to handle half-open connections as well as possible and want to flush |
664 | * out and read data before we close down if we can. */ | |
665 | ||
666 | /* Already disconnected? */ | |
667 | if (!VARLINK_STATE_IS_ALIVE(v->state)) | |
668 | return 0; | |
669 | ||
670 | /* Wait until connection setup is complete, i.e. until asynchronous connect() completes */ | |
671 | if (v->connecting) | |
672 | return 0; | |
673 | ||
674 | /* Still something to write and we can write? Stay around */ | |
675 | if (v->output_buffer_size > 0 && !v->write_disconnected) | |
676 | return 0; | |
677 | ||
678 | /* Both sides gone already? Then there's no need to stick around */ | |
679 | if (v->read_disconnected && v->write_disconnected) | |
680 | goto disconnect; | |
681 | ||
682 | /* If we are waiting for incoming data but the read side is shut down, disconnect. */ | |
45a6c965 | 683 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING, VARLINK_IDLE_SERVER) && v->read_disconnected) |
d41bd96f LP |
684 | goto disconnect; |
685 | ||
686 | /* Similar, if are a client that hasn't written anything yet but the write side is dead, also | |
687 | * disconnect. We also explicitly check for POLLHUP here since we likely won't notice the write side | |
688 | * being down if we never wrote anything. */ | |
4f06325c | 689 | if (v->state == VARLINK_IDLE_CLIENT && (v->write_disconnected || v->got_pollhup)) |
d41bd96f LP |
690 | goto disconnect; |
691 | ||
7c26a631 LP |
692 | /* We are on the server side and still want to send out more replies, but we saw POLLHUP already, and |
693 | * either got no buffered bytes to write anymore or already saw a write error. In that case we should | |
694 | * shut down the varlink link. */ | |
695 | 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 |
696 | goto disconnect; |
697 | ||
d41bd96f LP |
698 | return 0; |
699 | ||
700 | disconnect: | |
701 | varlink_set_state(v, VARLINK_PENDING_DISCONNECT); | |
702 | return 1; | |
703 | } | |
704 | ||
705 | static int varlink_write(Varlink *v) { | |
706 | ssize_t n; | |
d37cdac6 | 707 | int r; |
d41bd96f LP |
708 | |
709 | assert(v); | |
710 | ||
711 | if (!VARLINK_STATE_IS_ALIVE(v->state)) | |
712 | return 0; | |
713 | if (v->connecting) /* Writing while we are still wait for a non-blocking connect() to complete will | |
714 | * result in ENOTCONN, hence exit early here */ | |
715 | return 0; | |
d41bd96f LP |
716 | if (v->write_disconnected) |
717 | return 0; | |
718 | ||
d37cdac6 LP |
719 | /* If needed let's convert some output queue json variants into text form */ |
720 | r = varlink_format_queue(v); | |
721 | if (r < 0) | |
722 | return r; | |
723 | ||
724 | if (v->output_buffer_size == 0) | |
725 | return 0; | |
726 | ||
d41bd96f LP |
727 | assert(v->fd >= 0); |
728 | ||
d37cdac6 LP |
729 | if (v->n_output_fds > 0) { /* If we shall send fds along, we must use sendmsg() */ |
730 | struct iovec iov = { | |
731 | .iov_base = v->output_buffer + v->output_buffer_index, | |
732 | .iov_len = v->output_buffer_size, | |
733 | }; | |
734 | struct msghdr mh = { | |
735 | .msg_iov = &iov, | |
736 | .msg_iovlen = 1, | |
737 | .msg_controllen = CMSG_SPACE(sizeof(int) * v->n_output_fds), | |
738 | }; | |
739 | ||
740 | mh.msg_control = alloca0(mh.msg_controllen); | |
741 | ||
742 | struct cmsghdr *control = CMSG_FIRSTHDR(&mh); | |
743 | control->cmsg_len = CMSG_LEN(sizeof(int) * v->n_output_fds); | |
744 | control->cmsg_level = SOL_SOCKET; | |
745 | control->cmsg_type = SCM_RIGHTS; | |
746 | memcpy(CMSG_DATA(control), v->output_fds, sizeof(int) * v->n_output_fds); | |
747 | ||
748 | n = sendmsg(v->fd, &mh, MSG_DONTWAIT|MSG_NOSIGNAL); | |
749 | } else { | |
750 | /* We generally prefer recv()/send() (mostly because of MSG_NOSIGNAL) but also want to be compatible | |
751 | * with non-socket IO, hence fall back automatically. | |
752 | * | |
753 | * Use a local variable to help gcc figure out that we set 'n' in all cases. */ | |
754 | bool prefer_write = v->prefer_read_write; | |
755 | if (!prefer_write) { | |
756 | n = send(v->fd, v->output_buffer + v->output_buffer_index, v->output_buffer_size, MSG_DONTWAIT|MSG_NOSIGNAL); | |
757 | if (n < 0 && errno == ENOTSOCK) | |
758 | prefer_write = v->prefer_read_write = true; | |
759 | } | |
760 | if (prefer_write) | |
761 | n = write(v->fd, v->output_buffer + v->output_buffer_index, v->output_buffer_size); | |
d41bd96f | 762 | } |
d41bd96f LP |
763 | if (n < 0) { |
764 | if (errno == EAGAIN) | |
765 | return 0; | |
766 | ||
767 | if (ERRNO_IS_DISCONNECT(errno)) { | |
768 | /* If we get informed about a disconnect on write, then let's remember that, but not | |
769 | * act on it just yet. Let's wait for read() to report the issue first. */ | |
770 | v->write_disconnected = true; | |
771 | return 1; | |
772 | } | |
773 | ||
774 | return -errno; | |
775 | } | |
776 | ||
db1f7c84 LP |
777 | if (v->output_buffer_sensitive) |
778 | explicit_bzero_safe(v->output_buffer + v->output_buffer_index, n); | |
779 | ||
d41bd96f LP |
780 | v->output_buffer_size -= n; |
781 | ||
db1f7c84 | 782 | if (v->output_buffer_size == 0) { |
d41bd96f | 783 | v->output_buffer_index = 0; |
db1f7c84 LP |
784 | v->output_buffer_sensitive = false; /* We can reset the sensitive flag once the buffer is empty */ |
785 | } else | |
d41bd96f LP |
786 | v->output_buffer_index += n; |
787 | ||
d37cdac6 LP |
788 | close_many(v->output_fds, v->n_output_fds); |
789 | v->n_output_fds = 0; | |
790 | ||
d41bd96f LP |
791 | v->timestamp = now(CLOCK_MONOTONIC); |
792 | return 1; | |
793 | } | |
794 | ||
d37cdac6 LP |
795 | #define VARLINK_FDS_MAX (16U*1024U) |
796 | ||
d41bd96f | 797 | static int varlink_read(Varlink *v) { |
d37cdac6 LP |
798 | struct iovec iov; |
799 | struct msghdr mh; | |
d41bd96f LP |
800 | size_t rs; |
801 | ssize_t n; | |
d37cdac6 | 802 | void *p; |
d41bd96f LP |
803 | |
804 | assert(v); | |
805 | ||
45a6c965 | 806 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING, VARLINK_IDLE_SERVER)) |
d41bd96f LP |
807 | return 0; |
808 | if (v->connecting) /* read() on a socket while we are in connect() will fail with EINVAL, hence exit early here */ | |
809 | return 0; | |
810 | if (v->current) | |
811 | return 0; | |
812 | if (v->input_buffer_unscanned > 0) | |
813 | return 0; | |
814 | if (v->read_disconnected) | |
815 | return 0; | |
816 | ||
817 | if (v->input_buffer_size >= VARLINK_BUFFER_MAX) | |
818 | return -ENOBUFS; | |
819 | ||
820 | assert(v->fd >= 0); | |
821 | ||
319a4f4b | 822 | if (MALLOC_SIZEOF_SAFE(v->input_buffer) <= v->input_buffer_index + v->input_buffer_size) { |
d41bd96f LP |
823 | size_t add; |
824 | ||
825 | add = MIN(VARLINK_BUFFER_MAX - v->input_buffer_size, VARLINK_READ_SIZE); | |
826 | ||
827 | if (v->input_buffer_index == 0) { | |
828 | ||
319a4f4b | 829 | if (!GREEDY_REALLOC(v->input_buffer, v->input_buffer_size + add)) |
d41bd96f LP |
830 | return -ENOMEM; |
831 | ||
832 | } else { | |
833 | char *b; | |
834 | ||
835 | b = new(char, v->input_buffer_size + add); | |
836 | if (!b) | |
837 | return -ENOMEM; | |
838 | ||
839 | memcpy(b, v->input_buffer + v->input_buffer_index, v->input_buffer_size); | |
840 | ||
841 | free_and_replace(v->input_buffer, b); | |
d41bd96f LP |
842 | v->input_buffer_index = 0; |
843 | } | |
844 | } | |
845 | ||
d37cdac6 | 846 | p = v->input_buffer + v->input_buffer_index + v->input_buffer_size; |
319a4f4b | 847 | rs = MALLOC_SIZEOF_SAFE(v->input_buffer) - (v->input_buffer_index + v->input_buffer_size); |
d41bd96f | 848 | |
d37cdac6 | 849 | if (v->allow_fd_passing_input) { |
0347b9fd | 850 | iov = IOVEC_MAKE(p, rs); |
3c868058 LP |
851 | |
852 | /* Allocate the fd buffer on the heap, since we need a lot of space potentially */ | |
853 | if (!v->input_control_buffer) { | |
854 | v->input_control_buffer_size = CMSG_SPACE(sizeof(int) * VARLINK_FDS_MAX); | |
855 | v->input_control_buffer = malloc(v->input_control_buffer_size); | |
856 | if (!v->input_control_buffer) | |
857 | return -ENOMEM; | |
858 | } | |
859 | ||
d37cdac6 LP |
860 | mh = (struct msghdr) { |
861 | .msg_iov = &iov, | |
862 | .msg_iovlen = 1, | |
3c868058 LP |
863 | .msg_control = v->input_control_buffer, |
864 | .msg_controllen = v->input_control_buffer_size, | |
d37cdac6 | 865 | }; |
b456f226 | 866 | |
d37cdac6 LP |
867 | n = recvmsg_safe(v->fd, &mh, MSG_DONTWAIT|MSG_CMSG_CLOEXEC); |
868 | } else { | |
869 | bool prefer_read = v->prefer_read_write; | |
870 | if (!prefer_read) { | |
871 | n = recv(v->fd, p, rs, MSG_DONTWAIT); | |
872 | if (n < 0 && errno == ENOTSOCK) | |
873 | prefer_read = v->prefer_read_write = true; | |
874 | } | |
875 | if (prefer_read) | |
876 | n = read(v->fd, p, rs); | |
d41bd96f | 877 | } |
d41bd96f LP |
878 | if (n < 0) { |
879 | if (errno == EAGAIN) | |
880 | return 0; | |
881 | ||
882 | if (ERRNO_IS_DISCONNECT(errno)) { | |
883 | v->read_disconnected = true; | |
884 | return 1; | |
885 | } | |
886 | ||
887 | return -errno; | |
888 | } | |
889 | if (n == 0) { /* EOF */ | |
d37cdac6 LP |
890 | |
891 | if (v->allow_fd_passing_input) | |
892 | cmsg_close_all(&mh); | |
893 | ||
d41bd96f LP |
894 | v->read_disconnected = true; |
895 | return 1; | |
896 | } | |
897 | ||
d37cdac6 LP |
898 | if (v->allow_fd_passing_input) { |
899 | struct cmsghdr* cmsg; | |
900 | ||
901 | cmsg = cmsg_find(&mh, SOL_SOCKET, SCM_RIGHTS, (socklen_t) -1); | |
902 | if (cmsg) { | |
903 | size_t add; | |
904 | ||
905 | /* We only allow file descriptors to be passed along with the first byte of a | |
906 | * message. If they are passed with any other byte this is a protocol violation. */ | |
907 | if (v->input_buffer_size != 0) { | |
908 | cmsg_close_all(&mh); | |
909 | return -EPROTO; | |
910 | } | |
911 | ||
912 | add = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int); | |
913 | if (add > INT_MAX - v->n_input_fds) { | |
914 | cmsg_close_all(&mh); | |
915 | return -EBADF; | |
916 | } | |
917 | ||
918 | if (!GREEDY_REALLOC(v->input_fds, v->n_input_fds + add)) { | |
919 | cmsg_close_all(&mh); | |
920 | return -ENOMEM; | |
921 | } | |
922 | ||
923 | memcpy_safe(v->input_fds + v->n_input_fds, CMSG_TYPED_DATA(cmsg, int), add * sizeof(int)); | |
924 | v->n_input_fds += add; | |
925 | } | |
926 | } | |
927 | ||
d41bd96f LP |
928 | v->input_buffer_size += n; |
929 | v->input_buffer_unscanned += n; | |
930 | ||
931 | return 1; | |
932 | } | |
933 | ||
934 | static int varlink_parse_message(Varlink *v) { | |
935 | const char *e, *begin; | |
936 | size_t sz; | |
937 | int r; | |
938 | ||
939 | assert(v); | |
940 | ||
941 | if (v->current) | |
942 | return 0; | |
943 | if (v->input_buffer_unscanned <= 0) | |
944 | return 0; | |
945 | ||
946 | assert(v->input_buffer_unscanned <= v->input_buffer_size); | |
319a4f4b | 947 | assert(v->input_buffer_index + v->input_buffer_size <= MALLOC_SIZEOF_SAFE(v->input_buffer)); |
d41bd96f LP |
948 | |
949 | begin = v->input_buffer + v->input_buffer_index; | |
950 | ||
951 | e = memchr(begin + v->input_buffer_size - v->input_buffer_unscanned, 0, v->input_buffer_unscanned); | |
952 | if (!e) { | |
953 | v->input_buffer_unscanned = 0; | |
954 | return 0; | |
955 | } | |
956 | ||
957 | sz = e - begin + 1; | |
958 | ||
77472d06 ZJS |
959 | varlink_log(v, "New incoming message: %s", begin); /* FIXME: should we output the whole message here before validation? |
960 | * This may produce a non-printable journal entry if the message | |
961 | * is invalid. We may also expose privileged information. */ | |
d41bd96f | 962 | |
d642f640 | 963 | r = json_parse(begin, 0, &v->current, NULL, NULL); |
77472d06 ZJS |
964 | if (r < 0) { |
965 | /* If we encounter a parse failure flush all data. We cannot possibly recover from this, | |
966 | * hence drop all buffered data now. */ | |
967 | v->input_buffer_index = v->input_buffer_size = v->input_buffer_unscanned = 0; | |
968 | return varlink_log_errno(v, r, "Failed to parse JSON: %m"); | |
969 | } | |
d41bd96f LP |
970 | |
971 | v->input_buffer_size -= sz; | |
972 | ||
973 | if (v->input_buffer_size == 0) | |
974 | v->input_buffer_index = 0; | |
975 | else | |
976 | v->input_buffer_index += sz; | |
977 | ||
978 | v->input_buffer_unscanned = v->input_buffer_size; | |
979 | return 1; | |
980 | } | |
981 | ||
982 | static int varlink_test_timeout(Varlink *v) { | |
983 | assert(v); | |
984 | ||
45a6c965 | 985 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING)) |
d41bd96f LP |
986 | return 0; |
987 | if (v->timeout == USEC_INFINITY) | |
988 | return 0; | |
989 | ||
990 | if (now(CLOCK_MONOTONIC) < usec_add(v->timestamp, v->timeout)) | |
991 | return 0; | |
992 | ||
993 | varlink_set_state(v, VARLINK_PENDING_TIMEOUT); | |
994 | ||
995 | return 1; | |
996 | } | |
997 | ||
998 | static int varlink_dispatch_local_error(Varlink *v, const char *error) { | |
999 | int r; | |
1000 | ||
1001 | assert(v); | |
1002 | assert(error); | |
1003 | ||
1004 | if (!v->reply_callback) | |
1005 | return 0; | |
1006 | ||
1007 | r = v->reply_callback(v, NULL, error, VARLINK_REPLY_ERROR|VARLINK_REPLY_LOCAL, v->userdata); | |
1008 | if (r < 0) | |
6bcc1232 | 1009 | varlink_log_errno(v, r, "Reply callback returned error, ignoring: %m"); |
d41bd96f LP |
1010 | |
1011 | return 1; | |
1012 | } | |
1013 | ||
1014 | static int varlink_dispatch_timeout(Varlink *v) { | |
1015 | assert(v); | |
1016 | ||
1017 | if (v->state != VARLINK_PENDING_TIMEOUT) | |
1018 | return 0; | |
1019 | ||
1020 | varlink_set_state(v, VARLINK_PROCESSING_TIMEOUT); | |
1021 | varlink_dispatch_local_error(v, VARLINK_ERROR_TIMEOUT); | |
1022 | varlink_close(v); | |
1023 | ||
1024 | return 1; | |
1025 | } | |
1026 | ||
1027 | static int varlink_dispatch_disconnect(Varlink *v) { | |
1028 | assert(v); | |
1029 | ||
1030 | if (v->state != VARLINK_PENDING_DISCONNECT) | |
1031 | return 0; | |
1032 | ||
1033 | varlink_set_state(v, VARLINK_PROCESSING_DISCONNECT); | |
1034 | varlink_dispatch_local_error(v, VARLINK_ERROR_DISCONNECTED); | |
1035 | varlink_close(v); | |
1036 | ||
1037 | return 1; | |
1038 | } | |
1039 | ||
1040 | static int varlink_sanitize_parameters(JsonVariant **v) { | |
f2ad8924 LP |
1041 | int r; |
1042 | ||
d41bd96f LP |
1043 | assert(v); |
1044 | ||
1045 | /* Varlink always wants a parameters list, hence make one if the caller doesn't want any */ | |
1046 | if (!*v) | |
1047 | return json_variant_new_object(v, NULL, 0); | |
f2ad8924 LP |
1048 | if (json_variant_is_null(*v)) { |
1049 | JsonVariant *empty; | |
1050 | ||
1051 | r = json_variant_new_object(&empty, NULL, 0); | |
1052 | if (r < 0) | |
1053 | return r; | |
1054 | ||
1055 | json_variant_unref(*v); | |
1056 | *v = empty; | |
1057 | return 0; | |
1058 | } | |
1059 | if (!json_variant_is_object(*v)) | |
d41bd96f LP |
1060 | return -EINVAL; |
1061 | ||
1062 | return 0; | |
1063 | } | |
1064 | ||
1065 | static int varlink_dispatch_reply(Varlink *v) { | |
1066 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1067 | VarlinkReplyFlags flags = 0; | |
1068 | const char *error = NULL; | |
1069 | JsonVariant *e; | |
1070 | const char *k; | |
1071 | int r; | |
1072 | ||
1073 | assert(v); | |
1074 | ||
45a6c965 | 1075 | if (!IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING)) |
d41bd96f LP |
1076 | return 0; |
1077 | if (!v->current) | |
1078 | return 0; | |
1079 | ||
1080 | assert(v->n_pending > 0); | |
1081 | ||
1082 | if (!json_variant_is_object(v->current)) | |
1083 | goto invalid; | |
1084 | ||
1085 | JSON_VARIANT_OBJECT_FOREACH(k, e, v->current) { | |
1086 | ||
1087 | if (streq(k, "error")) { | |
1088 | if (error) | |
1089 | goto invalid; | |
1090 | if (!json_variant_is_string(e)) | |
1091 | goto invalid; | |
1092 | ||
1093 | error = json_variant_string(e); | |
1094 | flags |= VARLINK_REPLY_ERROR; | |
1095 | ||
1096 | } else if (streq(k, "parameters")) { | |
1097 | if (parameters) | |
1098 | goto invalid; | |
f2ad8924 | 1099 | if (!json_variant_is_object(e) && !json_variant_is_null(e)) |
d41bd96f LP |
1100 | goto invalid; |
1101 | ||
1102 | parameters = json_variant_ref(e); | |
1103 | ||
1104 | } else if (streq(k, "continues")) { | |
1105 | if (FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) | |
1106 | goto invalid; | |
1107 | ||
1108 | if (!json_variant_is_boolean(e)) | |
1109 | goto invalid; | |
1110 | ||
1111 | if (json_variant_boolean(e)) | |
1112 | flags |= VARLINK_REPLY_CONTINUES; | |
1113 | } else | |
1114 | goto invalid; | |
1115 | } | |
1116 | ||
45a6c965 LP |
1117 | /* Replies with 'continue' set are only OK if we set 'more' when the method call was initiated */ |
1118 | if (v->state != VARLINK_AWAITING_REPLY_MORE && FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) | |
1119 | goto invalid; | |
1120 | ||
1121 | /* An error is final */ | |
d41bd96f LP |
1122 | if (error && FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) |
1123 | goto invalid; | |
1124 | ||
1125 | r = varlink_sanitize_parameters(¶meters); | |
1126 | if (r < 0) | |
1127 | goto invalid; | |
1128 | ||
45a6c965 | 1129 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE)) { |
d41bd96f LP |
1130 | varlink_set_state(v, VARLINK_PROCESSING_REPLY); |
1131 | ||
1132 | if (v->reply_callback) { | |
1133 | r = v->reply_callback(v, parameters, error, flags, v->userdata); | |
1134 | if (r < 0) | |
6bcc1232 | 1135 | varlink_log_errno(v, r, "Reply callback returned error, ignoring: %m"); |
d41bd96f LP |
1136 | } |
1137 | ||
790446bd | 1138 | varlink_clear_current(v); |
d41bd96f LP |
1139 | |
1140 | if (v->state == VARLINK_PROCESSING_REPLY) { | |
45a6c965 | 1141 | |
d41bd96f | 1142 | assert(v->n_pending > 0); |
d41bd96f | 1143 | |
45a6c965 LP |
1144 | if (!FLAGS_SET(flags, VARLINK_REPLY_CONTINUES)) |
1145 | v->n_pending--; | |
1146 | ||
1147 | varlink_set_state(v, | |
1148 | FLAGS_SET(flags, VARLINK_REPLY_CONTINUES) ? VARLINK_AWAITING_REPLY_MORE : | |
1149 | v->n_pending == 0 ? VARLINK_IDLE_CLIENT : VARLINK_AWAITING_REPLY); | |
d41bd96f LP |
1150 | } |
1151 | } else { | |
1152 | assert(v->state == VARLINK_CALLING); | |
d41bd96f LP |
1153 | varlink_set_state(v, VARLINK_CALLED); |
1154 | } | |
1155 | ||
1156 | return 1; | |
1157 | ||
1158 | invalid: | |
1159 | varlink_set_state(v, VARLINK_PROCESSING_FAILURE); | |
1160 | varlink_dispatch_local_error(v, VARLINK_ERROR_PROTOCOL); | |
1161 | varlink_close(v); | |
1162 | ||
1163 | return 1; | |
1164 | } | |
1165 | ||
7e006b49 LP |
1166 | static int generic_method_get_info( |
1167 | Varlink *link, | |
1168 | JsonVariant *parameters, | |
1169 | VarlinkMethodFlags flags, | |
1170 | void *userdata) { | |
1171 | ||
1172 | _cleanup_strv_free_ char **interfaces = NULL; | |
1173 | _cleanup_free_ char *product = NULL; | |
1174 | int r; | |
1175 | ||
1176 | assert(link); | |
1177 | ||
1178 | if (json_variant_elements(parameters) != 0) | |
c2fb27ca | 1179 | return varlink_error_invalid_parameter(link, parameters); |
7e006b49 LP |
1180 | |
1181 | product = strjoin("systemd (", program_invocation_short_name, ")"); | |
1182 | if (!product) | |
1183 | return -ENOMEM; | |
1184 | ||
1185 | VarlinkInterface *interface; | |
1186 | HASHMAP_FOREACH(interface, ASSERT_PTR(link->server)->interfaces) { | |
1187 | r = strv_extend(&interfaces, interface->name); | |
1188 | if (r < 0) | |
1189 | return r; | |
1190 | } | |
1191 | ||
1192 | strv_sort(interfaces); | |
1193 | ||
1194 | return varlink_replyb(link, JSON_BUILD_OBJECT( | |
1195 | JSON_BUILD_PAIR_STRING("vendor", "The systemd Project"), | |
1196 | JSON_BUILD_PAIR_STRING("product", product), | |
1197 | JSON_BUILD_PAIR_STRING("version", STRINGIFY(PROJECT_VERSION) " (" GIT_VERSION ")"), | |
1198 | JSON_BUILD_PAIR_STRING("url", "https://systemd.io/"), | |
1199 | JSON_BUILD_PAIR_STRV("interfaces", interfaces))); | |
1200 | } | |
1201 | ||
1202 | static int generic_method_get_interface_description( | |
1203 | Varlink *link, | |
1204 | JsonVariant *parameters, | |
1205 | VarlinkMethodFlags flags, | |
1206 | void *userdata) { | |
1207 | ||
1208 | static const struct JsonDispatch dispatch_table[] = { | |
1209 | { "interface", JSON_VARIANT_STRING, json_dispatch_const_string, 0, JSON_MANDATORY }, | |
1210 | {} | |
1211 | }; | |
1212 | _cleanup_free_ char *text = NULL; | |
1213 | const VarlinkInterface *interface; | |
1214 | const char *name = NULL; | |
1215 | int r; | |
1216 | ||
1217 | assert(link); | |
1218 | ||
f1b622a0 | 1219 | r = json_dispatch(parameters, dispatch_table, 0, &name); |
7e006b49 LP |
1220 | if (r < 0) |
1221 | return r; | |
1222 | ||
1223 | interface = hashmap_get(ASSERT_PTR(link->server)->interfaces, name); | |
1224 | if (!interface) | |
1225 | return varlink_errorb(link, VARLINK_ERROR_INTERFACE_NOT_FOUND, | |
1226 | JSON_BUILD_OBJECT( | |
1227 | JSON_BUILD_PAIR_STRING("interface", name))); | |
1228 | ||
1229 | r = varlink_idl_format(interface, &text); | |
1230 | if (r < 0) | |
1231 | return r; | |
1232 | ||
1233 | return varlink_replyb(link, | |
1234 | JSON_BUILD_OBJECT( | |
1235 | JSON_BUILD_PAIR_STRING("description", text))); | |
1236 | } | |
1237 | ||
d41bd96f LP |
1238 | static int varlink_dispatch_method(Varlink *v) { |
1239 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1240 | VarlinkMethodFlags flags = 0; | |
7e006b49 | 1241 | const char *method = NULL; |
d41bd96f LP |
1242 | JsonVariant *e; |
1243 | VarlinkMethod callback; | |
1244 | const char *k; | |
1245 | int r; | |
1246 | ||
1247 | assert(v); | |
1248 | ||
1249 | if (v->state != VARLINK_IDLE_SERVER) | |
1250 | return 0; | |
1251 | if (!v->current) | |
1252 | return 0; | |
1253 | ||
1254 | if (!json_variant_is_object(v->current)) | |
1255 | goto invalid; | |
1256 | ||
1257 | JSON_VARIANT_OBJECT_FOREACH(k, e, v->current) { | |
1258 | ||
1259 | if (streq(k, "method")) { | |
1260 | if (method) | |
1261 | goto invalid; | |
1262 | if (!json_variant_is_string(e)) | |
1263 | goto invalid; | |
1264 | ||
1265 | method = json_variant_string(e); | |
1266 | ||
1267 | } else if (streq(k, "parameters")) { | |
1268 | if (parameters) | |
1269 | goto invalid; | |
f2ad8924 | 1270 | if (!json_variant_is_object(e) && !json_variant_is_null(e)) |
d41bd96f LP |
1271 | goto invalid; |
1272 | ||
1273 | parameters = json_variant_ref(e); | |
1274 | ||
1275 | } else if (streq(k, "oneway")) { | |
1276 | ||
1277 | if ((flags & (VARLINK_METHOD_ONEWAY|VARLINK_METHOD_MORE)) != 0) | |
1278 | goto invalid; | |
1279 | ||
1280 | if (!json_variant_is_boolean(e)) | |
1281 | goto invalid; | |
1282 | ||
1283 | if (json_variant_boolean(e)) | |
1284 | flags |= VARLINK_METHOD_ONEWAY; | |
1285 | ||
1286 | } else if (streq(k, "more")) { | |
1287 | ||
1288 | if ((flags & (VARLINK_METHOD_ONEWAY|VARLINK_METHOD_MORE)) != 0) | |
1289 | goto invalid; | |
1290 | ||
1291 | if (!json_variant_is_boolean(e)) | |
1292 | goto invalid; | |
1293 | ||
1294 | if (json_variant_boolean(e)) | |
1295 | flags |= VARLINK_METHOD_MORE; | |
1296 | ||
1297 | } else | |
1298 | goto invalid; | |
1299 | } | |
1300 | ||
1301 | if (!method) | |
1302 | goto invalid; | |
1303 | ||
1304 | r = varlink_sanitize_parameters(¶meters); | |
1305 | if (r < 0) | |
1306 | goto fail; | |
1307 | ||
1308 | varlink_set_state(v, (flags & VARLINK_METHOD_MORE) ? VARLINK_PROCESSING_METHOD_MORE : | |
1309 | (flags & VARLINK_METHOD_ONEWAY) ? VARLINK_PROCESSING_METHOD_ONEWAY : | |
1310 | VARLINK_PROCESSING_METHOD); | |
1311 | ||
1312 | assert(v->server); | |
1313 | ||
7e006b49 LP |
1314 | /* First consult user supplied method implementations */ |
1315 | callback = hashmap_get(v->server->methods, method); | |
1316 | if (!callback) { | |
1317 | if (streq(method, "org.varlink.service.GetInfo")) | |
1318 | callback = generic_method_get_info; | |
1319 | else if (streq(method, "org.varlink.service.GetInterfaceDescription")) | |
1320 | callback = generic_method_get_interface_description; | |
d41bd96f LP |
1321 | } |
1322 | ||
1323 | if (callback) { | |
7e006b49 LP |
1324 | bool invalid = false; |
1325 | ||
1326 | v->current_method = hashmap_get(v->server->symbols, method); | |
1327 | if (!v->current_method) | |
6bcc1232 | 1328 | varlink_log(v, "No interface description defined for method '%s', not validating.", method); |
7e006b49 LP |
1329 | else { |
1330 | const char *bad_field; | |
1331 | ||
1332 | r = varlink_idl_validate_method_call(v->current_method, parameters, &bad_field); | |
1333 | if (r < 0) { | |
6bcc1232 | 1334 | varlink_log_errno(v, r, "Parameters for method %s() didn't pass validation on field '%s': %m", method, strna(bad_field)); |
c270fc44 | 1335 | |
7ff96ce3 | 1336 | if (IN_SET(v->state, VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE)) { |
afc50293 | 1337 | r = varlink_error_invalid_parameter_name(v, bad_field); |
c270fc44 LP |
1338 | if (r < 0) |
1339 | return r; | |
1340 | } | |
7e006b49 LP |
1341 | invalid = true; |
1342 | } | |
1343 | } | |
d41bd96f | 1344 | |
7e006b49 LP |
1345 | if (!invalid) { |
1346 | r = callback(v, parameters, flags, v->userdata); | |
1347 | if (r < 0) { | |
6bcc1232 | 1348 | varlink_log_errno(v, r, "Callback for %s returned error: %m", method); |
7e006b49 LP |
1349 | |
1350 | /* We got an error back from the callback. Propagate it to the client if the method call remains unanswered. */ | |
7ff96ce3 | 1351 | if (IN_SET(v->state, VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE)) { |
7e006b49 LP |
1352 | r = varlink_error_errno(v, r); |
1353 | if (r < 0) | |
1354 | return r; | |
1355 | } | |
d41bd96f LP |
1356 | } |
1357 | } | |
7ff96ce3 | 1358 | } else if (IN_SET(v->state, VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE)) { |
7e006b49 | 1359 | r = varlink_errorb(v, VARLINK_ERROR_METHOD_NOT_FOUND, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)))); |
d41bd96f LP |
1360 | if (r < 0) |
1361 | return r; | |
c931c871 | 1362 | } |
d41bd96f LP |
1363 | |
1364 | switch (v->state) { | |
1365 | ||
1366 | case VARLINK_PROCESSED_METHOD: /* Method call is fully processed */ | |
1367 | case VARLINK_PROCESSING_METHOD_ONEWAY: /* ditto */ | |
790446bd | 1368 | varlink_clear_current(v); |
d41bd96f LP |
1369 | varlink_set_state(v, VARLINK_IDLE_SERVER); |
1370 | break; | |
1371 | ||
1372 | case VARLINK_PROCESSING_METHOD: /* Method call wasn't replied to, will be replied to later */ | |
1373 | varlink_set_state(v, VARLINK_PENDING_METHOD); | |
1374 | break; | |
1375 | ||
d41bd96f LP |
1376 | case VARLINK_PROCESSING_METHOD_MORE: /* No reply for a "more" message was sent, more to come */ |
1377 | varlink_set_state(v, VARLINK_PENDING_METHOD_MORE); | |
1378 | break; | |
1379 | ||
1380 | default: | |
04499a70 | 1381 | assert_not_reached(); |
d41bd96f LP |
1382 | } |
1383 | ||
c931c871 | 1384 | return 1; |
d41bd96f LP |
1385 | |
1386 | invalid: | |
1387 | r = -EINVAL; | |
1388 | ||
1389 | fail: | |
1390 | varlink_set_state(v, VARLINK_PROCESSING_FAILURE); | |
1391 | varlink_dispatch_local_error(v, VARLINK_ERROR_PROTOCOL); | |
1392 | varlink_close(v); | |
1393 | ||
1394 | return r; | |
1395 | } | |
1396 | ||
1397 | int varlink_process(Varlink *v) { | |
1398 | int r; | |
1399 | ||
1400 | assert_return(v, -EINVAL); | |
1401 | ||
1402 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 1403 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
d41bd96f LP |
1404 | |
1405 | varlink_ref(v); | |
1406 | ||
1407 | r = varlink_write(v); | |
db3d4222 ZJS |
1408 | if (r < 0) |
1409 | varlink_log_errno(v, r, "Write failed: %m"); | |
d41bd96f LP |
1410 | if (r != 0) |
1411 | goto finish; | |
1412 | ||
1413 | r = varlink_dispatch_reply(v); | |
db3d4222 ZJS |
1414 | if (r < 0) |
1415 | varlink_log_errno(v, r, "Reply dispatch failed: %m"); | |
d41bd96f LP |
1416 | if (r != 0) |
1417 | goto finish; | |
1418 | ||
1419 | r = varlink_dispatch_method(v); | |
db3d4222 ZJS |
1420 | if (r < 0) |
1421 | varlink_log_errno(v, r, "Method dispatch failed: %m"); | |
d41bd96f LP |
1422 | if (r != 0) |
1423 | goto finish; | |
1424 | ||
1425 | r = varlink_parse_message(v); | |
db3d4222 ZJS |
1426 | if (r < 0) |
1427 | varlink_log_errno(v, r, "Message parsing failed: %m"); | |
d41bd96f LP |
1428 | if (r != 0) |
1429 | goto finish; | |
1430 | ||
1431 | r = varlink_read(v); | |
db3d4222 ZJS |
1432 | if (r < 0) |
1433 | varlink_log_errno(v, r, "Read failed: %m"); | |
d41bd96f LP |
1434 | if (r != 0) |
1435 | goto finish; | |
1436 | ||
1437 | r = varlink_test_disconnect(v); | |
db3d4222 | 1438 | assert(r >= 0); |
d41bd96f LP |
1439 | if (r != 0) |
1440 | goto finish; | |
1441 | ||
1442 | r = varlink_dispatch_disconnect(v); | |
db3d4222 | 1443 | assert(r >= 0); |
d41bd96f LP |
1444 | if (r != 0) |
1445 | goto finish; | |
1446 | ||
1447 | r = varlink_test_timeout(v); | |
db3d4222 | 1448 | assert(r >= 0); |
d41bd96f LP |
1449 | if (r != 0) |
1450 | goto finish; | |
1451 | ||
1452 | r = varlink_dispatch_timeout(v); | |
db3d4222 | 1453 | assert(r >= 0); |
d41bd96f LP |
1454 | if (r != 0) |
1455 | goto finish; | |
1456 | ||
1457 | finish: | |
1458 | if (r >= 0 && v->defer_event_source) { | |
1459 | int q; | |
1460 | ||
1461 | /* If we did some processing, make sure we are called again soon */ | |
1462 | q = sd_event_source_set_enabled(v->defer_event_source, r > 0 ? SD_EVENT_ON : SD_EVENT_OFF); | |
1463 | if (q < 0) | |
db3d4222 | 1464 | r = varlink_log_errno(v, q, "Failed to enable deferred event source: %m"); |
d41bd96f LP |
1465 | } |
1466 | ||
1467 | if (r < 0) { | |
1468 | if (VARLINK_STATE_IS_ALIVE(v->state)) | |
1469 | /* Initiate disconnection */ | |
1470 | varlink_set_state(v, VARLINK_PENDING_DISCONNECT); | |
1471 | else | |
1472 | /* We failed while disconnecting, in that case close right away */ | |
1473 | varlink_close(v); | |
1474 | } | |
1475 | ||
1476 | varlink_unref(v); | |
1477 | return r; | |
1478 | } | |
1479 | ||
1480 | static void handle_revents(Varlink *v, int revents) { | |
1481 | assert(v); | |
1482 | ||
1483 | if (v->connecting) { | |
1484 | /* If we have seen POLLOUT or POLLHUP on a socket we are asynchronously waiting a connect() | |
1485 | * to complete on, we know we are ready. We don't read the connection error here though, | |
1486 | * we'll get the error on the next read() or write(). */ | |
1487 | if ((revents & (POLLOUT|POLLHUP)) == 0) | |
1488 | return; | |
1489 | ||
6976bf5c | 1490 | varlink_log(v, "Asynchronous connection completed."); |
d41bd96f LP |
1491 | v->connecting = false; |
1492 | } else { | |
1493 | /* Note that we don't care much about POLLIN/POLLOUT here, we'll just try reading and writing | |
1494 | * what we can. However, we do care about POLLHUP to detect connection termination even if we | |
1495 | * momentarily don't want to read nor write anything. */ | |
1496 | ||
1497 | if (!FLAGS_SET(revents, POLLHUP)) | |
1498 | return; | |
1499 | ||
1500 | varlink_log(v, "Got POLLHUP from socket."); | |
1501 | v->got_pollhup = true; | |
1502 | } | |
1503 | } | |
1504 | ||
1505 | int varlink_wait(Varlink *v, usec_t timeout) { | |
d41bd96f LP |
1506 | int r, fd, events; |
1507 | usec_t t; | |
1508 | ||
1509 | assert_return(v, -EINVAL); | |
d41bd96f LP |
1510 | |
1511 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 1512 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
d41bd96f LP |
1513 | |
1514 | r = varlink_get_timeout(v, &t); | |
1515 | if (r < 0) | |
1516 | return r; | |
1517 | if (t != USEC_INFINITY) { | |
1518 | usec_t n; | |
1519 | ||
1520 | n = now(CLOCK_MONOTONIC); | |
1521 | if (t < n) | |
1522 | t = 0; | |
1523 | else | |
1524 | t = usec_sub_unsigned(t, n); | |
1525 | } | |
1526 | ||
1527 | if (timeout != USEC_INFINITY && | |
1528 | (t == USEC_INFINITY || timeout < t)) | |
1529 | t = timeout; | |
1530 | ||
1531 | fd = varlink_get_fd(v); | |
1532 | if (fd < 0) | |
1533 | return fd; | |
1534 | ||
1535 | events = varlink_get_events(v); | |
1536 | if (events < 0) | |
1537 | return events; | |
1538 | ||
0f2d351f | 1539 | r = fd_wait_for_event(fd, events, t); |
13d84288 ZJS |
1540 | if (ERRNO_IS_NEG_TRANSIENT(r)) /* Treat EINTR as not a timeout, but also nothing happened, and |
1541 | * the caller gets a chance to call back into us */ | |
6976bf5c | 1542 | return 1; |
05827831 | 1543 | if (r <= 0) |
0f2d351f | 1544 | return r; |
d41bd96f | 1545 | |
0f2d351f | 1546 | handle_revents(v, r); |
dad28bff | 1547 | return 1; |
d41bd96f LP |
1548 | } |
1549 | ||
8240fa92 LP |
1550 | int varlink_is_idle(Varlink *v) { |
1551 | assert_return(v, -EINVAL); | |
1552 | ||
1553 | /* Returns true if there's nothing pending on the connection anymore, i.e. we processed all incoming | |
1554 | * or outgoing messages fully, or finished disconnection */ | |
1555 | ||
1556 | return IN_SET(v->state, VARLINK_DISCONNECTED, VARLINK_IDLE_CLIENT, VARLINK_IDLE_SERVER); | |
1557 | } | |
1558 | ||
d41bd96f LP |
1559 | int varlink_get_fd(Varlink *v) { |
1560 | ||
1561 | assert_return(v, -EINVAL); | |
1562 | ||
1563 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 1564 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
d41bd96f | 1565 | if (v->fd < 0) |
db3d4222 | 1566 | return varlink_log_errno(v, SYNTHETIC_ERRNO(EBADF), "No valid fd."); |
d41bd96f LP |
1567 | |
1568 | return v->fd; | |
1569 | } | |
1570 | ||
1571 | int varlink_get_events(Varlink *v) { | |
1572 | int ret = 0; | |
1573 | ||
1574 | assert_return(v, -EINVAL); | |
1575 | ||
1576 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 1577 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
d41bd96f LP |
1578 | |
1579 | if (v->connecting) /* When processing an asynchronous connect(), we only wait for EPOLLOUT, which | |
1580 | * tells us that the connection is now complete. Before that we should neither | |
1581 | * write() or read() from the fd. */ | |
1582 | return EPOLLOUT; | |
1583 | ||
1584 | if (!v->read_disconnected && | |
45a6c965 | 1585 | IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING, VARLINK_IDLE_SERVER) && |
d41bd96f LP |
1586 | !v->current && |
1587 | v->input_buffer_unscanned <= 0) | |
1588 | ret |= EPOLLIN; | |
1589 | ||
1590 | if (!v->write_disconnected && | |
1591 | v->output_buffer_size > 0) | |
1592 | ret |= EPOLLOUT; | |
1593 | ||
1594 | return ret; | |
1595 | } | |
1596 | ||
1597 | int varlink_get_timeout(Varlink *v, usec_t *ret) { | |
1598 | assert_return(v, -EINVAL); | |
1599 | ||
1600 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 1601 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
d41bd96f | 1602 | |
45a6c965 | 1603 | if (IN_SET(v->state, VARLINK_AWAITING_REPLY, VARLINK_AWAITING_REPLY_MORE, VARLINK_CALLING) && |
d41bd96f LP |
1604 | v->timeout != USEC_INFINITY) { |
1605 | if (ret) | |
1606 | *ret = usec_add(v->timestamp, v->timeout); | |
1607 | return 1; | |
1608 | } else { | |
1609 | if (ret) | |
1610 | *ret = USEC_INFINITY; | |
1611 | return 0; | |
1612 | } | |
1613 | } | |
1614 | ||
1615 | int varlink_flush(Varlink *v) { | |
1616 | int ret = 0, r; | |
1617 | ||
1618 | assert_return(v, -EINVAL); | |
1619 | ||
1620 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 1621 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
d41bd96f LP |
1622 | |
1623 | for (;;) { | |
d41bd96f LP |
1624 | if (v->output_buffer_size == 0) |
1625 | break; | |
1626 | if (v->write_disconnected) | |
1627 | return -ECONNRESET; | |
1628 | ||
1629 | r = varlink_write(v); | |
1630 | if (r < 0) | |
1631 | return r; | |
1632 | if (r > 0) { | |
1633 | ret = 1; | |
1634 | continue; | |
1635 | } | |
1636 | ||
0f2d351f | 1637 | r = fd_wait_for_event(v->fd, POLLOUT, USEC_INFINITY); |
bb44fd07 ZJS |
1638 | if (ERRNO_IS_NEG_TRANSIENT(r)) |
1639 | continue; | |
1640 | if (r < 0) | |
db3d4222 | 1641 | return varlink_log_errno(v, r, "Poll failed on fd: %m"); |
bb44fd07 | 1642 | assert(r > 0); |
dad28bff | 1643 | |
0f2d351f | 1644 | handle_revents(v, r); |
d41bd96f LP |
1645 | } |
1646 | ||
1647 | return ret; | |
1648 | } | |
1649 | ||
1650 | static void varlink_detach_server(Varlink *v) { | |
6d4d6002 | 1651 | VarlinkServer *saved_server; |
d41bd96f LP |
1652 | assert(v); |
1653 | ||
1654 | if (!v->server) | |
1655 | return; | |
1656 | ||
1657 | if (v->server->by_uid && | |
1658 | v->ucred_acquired && | |
1659 | uid_is_valid(v->ucred.uid)) { | |
1660 | unsigned c; | |
1661 | ||
1662 | c = PTR_TO_UINT(hashmap_get(v->server->by_uid, UID_TO_PTR(v->ucred.uid))); | |
1663 | assert(c > 0); | |
1664 | ||
1665 | if (c == 1) | |
1666 | (void) hashmap_remove(v->server->by_uid, UID_TO_PTR(v->ucred.uid)); | |
1667 | else | |
1668 | (void) hashmap_replace(v->server->by_uid, UID_TO_PTR(v->ucred.uid), UINT_TO_PTR(c - 1)); | |
1669 | } | |
1670 | ||
1671 | assert(v->server->n_connections > 0); | |
1672 | v->server->n_connections--; | |
1673 | ||
1674 | /* If this is a connection associated to a server, then let's disconnect the server and the | |
6d4d6002 LP |
1675 | * connection from each other. This drops the dangling reference that connect_callback() set up. But |
1676 | * before we release the references, let's call the disconnection callback if it is defined. */ | |
1677 | ||
1678 | saved_server = TAKE_PTR(v->server); | |
1679 | ||
1680 | if (saved_server->disconnect_callback) | |
1681 | saved_server->disconnect_callback(saved_server, v, saved_server->userdata); | |
1682 | ||
47723340 | 1683 | varlink_server_test_exit_on_idle(saved_server); |
6d4d6002 | 1684 | varlink_server_unref(saved_server); |
d41bd96f LP |
1685 | varlink_unref(v); |
1686 | } | |
1687 | ||
1688 | int varlink_close(Varlink *v) { | |
d41bd96f LP |
1689 | assert_return(v, -EINVAL); |
1690 | ||
1691 | if (v->state == VARLINK_DISCONNECTED) | |
1692 | return 0; | |
1693 | ||
1694 | varlink_set_state(v, VARLINK_DISCONNECTED); | |
1695 | ||
cc6b0a18 LP |
1696 | /* Let's take a reference first, since varlink_detach_server() might drop the final (dangling) ref |
1697 | * which would destroy us before we can call varlink_clear() */ | |
d41bd96f LP |
1698 | varlink_ref(v); |
1699 | varlink_detach_server(v); | |
1700 | varlink_clear(v); | |
1701 | varlink_unref(v); | |
1702 | ||
1703 | return 1; | |
1704 | } | |
1705 | ||
9652d740 | 1706 | Varlink* varlink_close_unref(Varlink *v) { |
9652d740 LP |
1707 | if (!v) |
1708 | return NULL; | |
1709 | ||
cc6b0a18 | 1710 | (void) varlink_close(v); |
9652d740 LP |
1711 | return varlink_unref(v); |
1712 | } | |
1713 | ||
d41bd96f | 1714 | Varlink* varlink_flush_close_unref(Varlink *v) { |
cc6b0a18 LP |
1715 | if (!v) |
1716 | return NULL; | |
d41bd96f | 1717 | |
cc6b0a18 | 1718 | (void) varlink_flush(v); |
39ad3f1c | 1719 | return varlink_close_unref(v); |
d41bd96f LP |
1720 | } |
1721 | ||
d37cdac6 | 1722 | static int varlink_format_json(Varlink *v, JsonVariant *m) { |
db1f7c84 | 1723 | _cleanup_(erase_and_freep) char *text = NULL; |
d41bd96f LP |
1724 | int r; |
1725 | ||
1726 | assert(v); | |
1727 | assert(m); | |
1728 | ||
1729 | r = json_variant_format(m, 0, &text); | |
1730 | if (r < 0) | |
1731 | return r; | |
2a04712c | 1732 | assert(text[r] == '\0'); |
d41bd96f LP |
1733 | |
1734 | if (v->output_buffer_size + r + 1 > VARLINK_BUFFER_MAX) | |
1735 | return -ENOBUFS; | |
1736 | ||
1737 | varlink_log(v, "Sending message: %s", text); | |
1738 | ||
1739 | if (v->output_buffer_size == 0) { | |
1740 | ||
1741 | free_and_replace(v->output_buffer, text); | |
1742 | ||
319a4f4b | 1743 | v->output_buffer_size = r + 1; |
d41bd96f LP |
1744 | v->output_buffer_index = 0; |
1745 | ||
1746 | } else if (v->output_buffer_index == 0) { | |
1747 | ||
319a4f4b | 1748 | if (!GREEDY_REALLOC(v->output_buffer, v->output_buffer_size + r + 1)) |
d41bd96f LP |
1749 | return -ENOMEM; |
1750 | ||
1751 | memcpy(v->output_buffer + v->output_buffer_size, text, r + 1); | |
1752 | v->output_buffer_size += r + 1; | |
d41bd96f LP |
1753 | } else { |
1754 | char *n; | |
be44e091 | 1755 | const size_t new_size = v->output_buffer_size + r + 1; |
d41bd96f | 1756 | |
be44e091 | 1757 | n = new(char, new_size); |
d41bd96f LP |
1758 | if (!n) |
1759 | return -ENOMEM; | |
1760 | ||
1761 | memcpy(mempcpy(n, v->output_buffer + v->output_buffer_index, v->output_buffer_size), text, r + 1); | |
1762 | ||
1763 | free_and_replace(v->output_buffer, n); | |
319a4f4b | 1764 | v->output_buffer_size = new_size; |
d41bd96f LP |
1765 | v->output_buffer_index = 0; |
1766 | } | |
1767 | ||
db1f7c84 LP |
1768 | if (json_variant_is_sensitive(m)) |
1769 | v->output_buffer_sensitive = true; /* Propagate sensitive flag */ | |
1770 | else | |
1771 | text = mfree(text); /* No point in the erase_and_free() destructor declared above */ | |
1772 | ||
d41bd96f LP |
1773 | return 0; |
1774 | } | |
1775 | ||
d37cdac6 LP |
1776 | static int varlink_enqueue_json(Varlink *v, JsonVariant *m) { |
1777 | VarlinkJsonQueueItem *q; | |
1778 | ||
1779 | assert(v); | |
1780 | assert(m); | |
1781 | ||
94d82b59 | 1782 | /* If there are no file descriptors to be queued and no queue entries yet we can shortcut things and |
d37cdac6 LP |
1783 | * append this entry directly to the output buffer */ |
1784 | if (v->n_pushed_fds == 0 && !v->output_queue) | |
1785 | return varlink_format_json(v, m); | |
1786 | ||
1787 | /* Otherwise add a queue entry for this */ | |
1788 | q = varlink_json_queue_item_new(m, v->pushed_fds, v->n_pushed_fds); | |
1789 | if (!q) | |
1790 | return -ENOMEM; | |
1791 | ||
1792 | v->n_pushed_fds = 0; /* fds now belong to the queue entry */ | |
1793 | ||
1794 | LIST_INSERT_AFTER(queue, v->output_queue, v->output_queue_tail, q); | |
1795 | v->output_queue_tail = q; | |
1796 | return 0; | |
1797 | } | |
1798 | ||
1799 | static int varlink_format_queue(Varlink *v) { | |
1800 | int r; | |
1801 | ||
1802 | assert(v); | |
1803 | ||
1804 | /* Takes entries out of the output queue and formats them into the output buffer. But only if this | |
1805 | * would not corrupt our fd message boundaries */ | |
1806 | ||
1807 | while (v->output_queue) { | |
1808 | _cleanup_free_ int *array = NULL; | |
1809 | VarlinkJsonQueueItem *q = v->output_queue; | |
1810 | ||
1811 | if (v->n_output_fds > 0) /* unwritten fds? if we'd add more we'd corrupt the fd message boundaries, hence wait */ | |
1812 | return 0; | |
1813 | ||
1814 | if (q->n_fds > 0) { | |
1815 | array = newdup(int, q->fds, q->n_fds); | |
1816 | if (!array) | |
1817 | return -ENOMEM; | |
1818 | } | |
1819 | ||
1820 | r = varlink_format_json(v, q->data); | |
1821 | if (r < 0) | |
1822 | return r; | |
1823 | ||
1824 | /* Take possession of the queue element's fds */ | |
1825 | free(v->output_fds); | |
1826 | v->output_fds = TAKE_PTR(array); | |
1827 | v->n_output_fds = q->n_fds; | |
1828 | q->n_fds = 0; | |
1829 | ||
1830 | LIST_REMOVE(queue, v->output_queue, q); | |
1831 | if (!v->output_queue) | |
1832 | v->output_queue_tail = NULL; | |
1833 | ||
1834 | varlink_json_queue_item_free(q); | |
1835 | } | |
1836 | ||
1837 | return 0; | |
1838 | } | |
1839 | ||
d41bd96f LP |
1840 | int varlink_send(Varlink *v, const char *method, JsonVariant *parameters) { |
1841 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1842 | int r; | |
1843 | ||
1844 | assert_return(v, -EINVAL); | |
1845 | assert_return(method, -EINVAL); | |
1846 | ||
1847 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 1848 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
45a6c965 LP |
1849 | |
1850 | /* We allow enqueuing multiple method calls at once! */ | |
d41bd96f | 1851 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT, VARLINK_AWAITING_REPLY)) |
db3d4222 | 1852 | return varlink_log_errno(v, SYNTHETIC_ERRNO(EBUSY), "Connection busy."); |
d41bd96f LP |
1853 | |
1854 | r = varlink_sanitize_parameters(¶meters); | |
1855 | if (r < 0) | |
db3d4222 | 1856 | return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); |
d41bd96f LP |
1857 | |
1858 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1859 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1860 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
1861 | JSON_BUILD_PAIR("oneway", JSON_BUILD_BOOLEAN(true)))); | |
1862 | if (r < 0) | |
db3d4222 | 1863 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f LP |
1864 | |
1865 | r = varlink_enqueue_json(v, m); | |
1866 | if (r < 0) | |
db3d4222 | 1867 | return varlink_log_errno(v, r, "Failed to enqueue json message: %m"); |
d41bd96f LP |
1868 | |
1869 | /* No state change here, this is one-way only after all */ | |
1870 | v->timestamp = now(CLOCK_MONOTONIC); | |
1871 | return 0; | |
1872 | } | |
1873 | ||
1874 | int varlink_sendb(Varlink *v, const char *method, ...) { | |
1875 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1876 | va_list ap; | |
1877 | int r; | |
1878 | ||
1879 | assert_return(v, -EINVAL); | |
1880 | ||
1881 | va_start(ap, method); | |
1882 | r = json_buildv(¶meters, ap); | |
1883 | va_end(ap); | |
1884 | ||
1885 | if (r < 0) | |
db3d4222 | 1886 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f LP |
1887 | |
1888 | return varlink_send(v, method, parameters); | |
1889 | } | |
1890 | ||
1891 | int varlink_invoke(Varlink *v, const char *method, JsonVariant *parameters) { | |
1892 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1893 | int r; | |
1894 | ||
1895 | assert_return(v, -EINVAL); | |
1896 | assert_return(method, -EINVAL); | |
1897 | ||
1898 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 1899 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
45a6c965 | 1900 | |
86b52a39 | 1901 | /* We allow enqueuing multiple method calls at once! */ |
d41bd96f | 1902 | if (!IN_SET(v->state, VARLINK_IDLE_CLIENT, VARLINK_AWAITING_REPLY)) |
db3d4222 | 1903 | return varlink_log_errno(v, SYNTHETIC_ERRNO(EBUSY), "Connection busy."); |
d41bd96f LP |
1904 | |
1905 | r = varlink_sanitize_parameters(¶meters); | |
1906 | if (r < 0) | |
db3d4222 | 1907 | return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); |
d41bd96f LP |
1908 | |
1909 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1910 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1911 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
1912 | if (r < 0) | |
db3d4222 | 1913 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f LP |
1914 | |
1915 | r = varlink_enqueue_json(v, m); | |
1916 | if (r < 0) | |
db3d4222 | 1917 | return varlink_log_errno(v, r, "Failed to enqueue json message: %m"); |
d41bd96f LP |
1918 | |
1919 | varlink_set_state(v, VARLINK_AWAITING_REPLY); | |
1920 | v->n_pending++; | |
1921 | v->timestamp = now(CLOCK_MONOTONIC); | |
1922 | ||
1923 | return 0; | |
1924 | } | |
1925 | ||
1926 | int varlink_invokeb(Varlink *v, const char *method, ...) { | |
1927 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1928 | va_list ap; | |
1929 | int r; | |
1930 | ||
1931 | assert_return(v, -EINVAL); | |
1932 | ||
1933 | va_start(ap, method); | |
1934 | r = json_buildv(¶meters, ap); | |
1935 | va_end(ap); | |
1936 | ||
1937 | if (r < 0) | |
db3d4222 | 1938 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f LP |
1939 | |
1940 | return varlink_invoke(v, method, parameters); | |
45a6c965 LP |
1941 | } |
1942 | ||
1943 | int varlink_observe(Varlink *v, const char *method, JsonVariant *parameters) { | |
1944 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
1945 | int r; | |
1946 | ||
1947 | assert_return(v, -EINVAL); | |
1948 | assert_return(method, -EINVAL); | |
1949 | ||
1950 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 ZJS |
1951 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
1952 | ||
45a6c965 LP |
1953 | /* Note that we don't allow enqueuing multiple method calls when we are in more/continues mode! We |
1954 | * thus insist on an idle client here. */ | |
1955 | if (v->state != VARLINK_IDLE_CLIENT) | |
db3d4222 | 1956 | return varlink_log_errno(v, SYNTHETIC_ERRNO(EBUSY), "Connection busy."); |
45a6c965 LP |
1957 | |
1958 | r = varlink_sanitize_parameters(¶meters); | |
1959 | if (r < 0) | |
db3d4222 | 1960 | return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); |
45a6c965 LP |
1961 | |
1962 | r = json_build(&m, JSON_BUILD_OBJECT( | |
1963 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
1964 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
1965 | JSON_BUILD_PAIR("more", JSON_BUILD_BOOLEAN(true)))); | |
1966 | if (r < 0) | |
db3d4222 | 1967 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
45a6c965 LP |
1968 | |
1969 | r = varlink_enqueue_json(v, m); | |
1970 | if (r < 0) | |
db3d4222 | 1971 | return varlink_log_errno(v, r, "Failed to enqueue json message: %m"); |
45a6c965 LP |
1972 | |
1973 | varlink_set_state(v, VARLINK_AWAITING_REPLY_MORE); | |
1974 | v->n_pending++; | |
1975 | v->timestamp = now(CLOCK_MONOTONIC); | |
1976 | ||
1977 | return 0; | |
1978 | } | |
1979 | ||
1980 | int varlink_observeb(Varlink *v, const char *method, ...) { | |
1981 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
1982 | va_list ap; | |
1983 | int r; | |
1984 | ||
1985 | assert_return(v, -EINVAL); | |
1986 | ||
1987 | va_start(ap, method); | |
1988 | r = json_buildv(¶meters, ap); | |
1989 | va_end(ap); | |
1990 | ||
1991 | if (r < 0) | |
db3d4222 | 1992 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
45a6c965 LP |
1993 | |
1994 | return varlink_observe(v, method, parameters); | |
d41bd96f LP |
1995 | } |
1996 | ||
1997 | int varlink_call( | |
1998 | Varlink *v, | |
1999 | const char *method, | |
2000 | JsonVariant *parameters, | |
2001 | JsonVariant **ret_parameters, | |
2002 | const char **ret_error_id, | |
2003 | VarlinkReplyFlags *ret_flags) { | |
2004 | ||
2005 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
2006 | int r; | |
2007 | ||
2008 | assert_return(v, -EINVAL); | |
2009 | assert_return(method, -EINVAL); | |
2010 | ||
2011 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 2012 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
4f06325c | 2013 | if (v->state != VARLINK_IDLE_CLIENT) |
db3d4222 | 2014 | return varlink_log_errno(v, SYNTHETIC_ERRNO(EBUSY), "Connection busy."); |
d41bd96f LP |
2015 | |
2016 | assert(v->n_pending == 0); /* n_pending can't be > 0 if we are in VARLINK_IDLE_CLIENT state */ | |
2017 | ||
d37cdac6 LP |
2018 | /* If there was still a reply pinned from a previous call, now it's the time to get rid of it, so |
2019 | * that we can assign a new reply shortly. */ | |
790446bd | 2020 | varlink_clear_current(v); |
85316317 | 2021 | |
d41bd96f LP |
2022 | r = varlink_sanitize_parameters(¶meters); |
2023 | if (r < 0) | |
db3d4222 | 2024 | return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); |
d41bd96f LP |
2025 | |
2026 | r = json_build(&m, JSON_BUILD_OBJECT( | |
2027 | JSON_BUILD_PAIR("method", JSON_BUILD_STRING(method)), | |
2028 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
2029 | if (r < 0) | |
db3d4222 | 2030 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f LP |
2031 | |
2032 | r = varlink_enqueue_json(v, m); | |
2033 | if (r < 0) | |
db3d4222 | 2034 | return varlink_log_errno(v, r, "Failed to enqueue json message: %m"); |
d41bd96f LP |
2035 | |
2036 | varlink_set_state(v, VARLINK_CALLING); | |
2037 | v->n_pending++; | |
2038 | v->timestamp = now(CLOCK_MONOTONIC); | |
2039 | ||
2040 | while (v->state == VARLINK_CALLING) { | |
2041 | ||
2042 | r = varlink_process(v); | |
2043 | if (r < 0) | |
2044 | return r; | |
2045 | if (r > 0) | |
2046 | continue; | |
2047 | ||
2048 | r = varlink_wait(v, USEC_INFINITY); | |
2049 | if (r < 0) | |
2050 | return r; | |
2051 | } | |
2052 | ||
2053 | switch (v->state) { | |
2054 | ||
2055 | case VARLINK_CALLED: | |
2056 | assert(v->current); | |
2057 | ||
d41bd96f LP |
2058 | varlink_set_state(v, VARLINK_IDLE_CLIENT); |
2059 | assert(v->n_pending == 1); | |
2060 | v->n_pending--; | |
2061 | ||
2062 | if (ret_parameters) | |
85316317 | 2063 | *ret_parameters = json_variant_by_key(v->current, "parameters"); |
d41bd96f | 2064 | if (ret_error_id) |
85316317 | 2065 | *ret_error_id = json_variant_string(json_variant_by_key(v->current, "error")); |
d41bd96f LP |
2066 | if (ret_flags) |
2067 | *ret_flags = 0; | |
2068 | ||
2069 | return 1; | |
2070 | ||
2071 | case VARLINK_PENDING_DISCONNECT: | |
2072 | case VARLINK_DISCONNECTED: | |
db3d4222 | 2073 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ECONNRESET), "Connection was closed."); |
d41bd96f LP |
2074 | |
2075 | case VARLINK_PENDING_TIMEOUT: | |
db3d4222 | 2076 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ETIME), "Connection timed out."); |
d41bd96f LP |
2077 | |
2078 | default: | |
04499a70 | 2079 | assert_not_reached(); |
d41bd96f LP |
2080 | } |
2081 | } | |
2082 | ||
2083 | int varlink_callb( | |
2084 | Varlink *v, | |
2085 | const char *method, | |
2086 | JsonVariant **ret_parameters, | |
2087 | const char **ret_error_id, | |
2088 | VarlinkReplyFlags *ret_flags, ...) { | |
2089 | ||
2090 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
2091 | va_list ap; | |
2092 | int r; | |
2093 | ||
2094 | assert_return(v, -EINVAL); | |
2095 | ||
2096 | va_start(ap, ret_flags); | |
2097 | r = json_buildv(¶meters, ap); | |
2098 | va_end(ap); | |
2099 | ||
2100 | if (r < 0) | |
db3d4222 | 2101 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f LP |
2102 | |
2103 | return varlink_call(v, method, parameters, ret_parameters, ret_error_id, ret_flags); | |
2104 | } | |
2105 | ||
1bd0b9c0 AS |
2106 | static void varlink_collect_context_free(VarlinkCollectContext *cc) { |
2107 | assert(cc); | |
2108 | ||
2109 | json_variant_unref(cc->parameters); | |
2110 | free((char *)cc->error_id); | |
2111 | } | |
2112 | ||
2113 | static int collect_callback( | |
2114 | Varlink *v, | |
2115 | JsonVariant *parameters, | |
2116 | const char *error_id, | |
2117 | VarlinkReplyFlags flags, | |
2118 | void *userdata) { | |
2119 | ||
2120 | VarlinkCollectContext *context = ASSERT_PTR(userdata); | |
2121 | int r; | |
2122 | ||
2123 | assert(v); | |
2124 | ||
2125 | context->flags = flags; | |
2126 | /* If we hit an error, we will drop all collected replies and just return the error_id and flags in varlink_collect() */ | |
2127 | if (error_id) { | |
2128 | context->error_id = error_id; | |
2129 | return 0; | |
2130 | } | |
2131 | ||
2132 | r = json_variant_append_array(&context->parameters, parameters); | |
2133 | if (r < 0) | |
2134 | return varlink_log_errno(v, r, "Failed to append JSON object to array: %m"); | |
2135 | ||
2136 | return 1; | |
2137 | } | |
2138 | ||
2139 | int varlink_collect( | |
2140 | Varlink *v, | |
2141 | const char *method, | |
2142 | JsonVariant *parameters, | |
2143 | JsonVariant **ret_parameters, | |
2144 | const char **ret_error_id, | |
2145 | VarlinkReplyFlags *ret_flags) { | |
2146 | ||
2147 | _cleanup_(varlink_collect_context_free) VarlinkCollectContext context = {}; | |
2148 | int r; | |
2149 | ||
2150 | assert_return(v, -EINVAL); | |
2151 | assert_return(method, -EINVAL); | |
2152 | ||
2153 | if (v->state == VARLINK_DISCONNECTED) | |
2154 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); | |
2155 | if (v->state != VARLINK_IDLE_CLIENT) | |
2156 | return varlink_log_errno(v, SYNTHETIC_ERRNO(EBUSY), "Connection busy."); | |
2157 | ||
2158 | assert(v->n_pending == 0); /* n_pending can't be > 0 if we are in VARLINK_IDLE_CLIENT state */ | |
2159 | ||
2160 | /* If there was still a reply pinned from a previous call, now it's the time to get rid of it, so | |
2161 | * that we can assign a new reply shortly. */ | |
2162 | varlink_clear_current(v); | |
2163 | ||
2164 | r = varlink_bind_reply(v, collect_callback); | |
2165 | if (r < 0) | |
2166 | return varlink_log_errno(v, r, "Failed to bind collect callback"); | |
2167 | ||
2168 | varlink_set_userdata(v, &context); | |
2169 | r = varlink_observe(v, method, parameters); | |
2170 | if (r < 0) | |
2171 | return varlink_log_errno(v, r, "Failed to collect varlink method: %m"); | |
2172 | ||
2173 | while (v->state == VARLINK_AWAITING_REPLY_MORE) { | |
2174 | ||
2175 | r = varlink_process(v); | |
2176 | if (r < 0) | |
2177 | return r; | |
2178 | ||
2179 | /* If we get an error from any of the replies, return immediately with just the error_id and flags*/ | |
2180 | if (context.error_id) { | |
2181 | if (ret_error_id) | |
2182 | *ret_error_id = TAKE_PTR(context.error_id); | |
2183 | if (ret_flags) | |
2184 | *ret_flags = context.flags; | |
2185 | return 0; | |
2186 | } | |
2187 | ||
2188 | if (r > 0) | |
2189 | continue; | |
2190 | ||
2191 | r = varlink_wait(v, USEC_INFINITY); | |
2192 | if (r < 0) | |
2193 | return r; | |
2194 | } | |
2195 | ||
2196 | switch (v->state) { | |
2197 | ||
2198 | case VARLINK_IDLE_CLIENT: | |
2199 | break; | |
2200 | ||
2201 | case VARLINK_PENDING_DISCONNECT: | |
2202 | case VARLINK_DISCONNECTED: | |
2203 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ECONNRESET), "Connection was closed."); | |
2204 | ||
2205 | case VARLINK_PENDING_TIMEOUT: | |
2206 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ETIME), "Connection timed out."); | |
2207 | ||
2208 | default: | |
2209 | assert_not_reached(); | |
2210 | } | |
2211 | ||
2212 | if (ret_parameters) | |
2213 | *ret_parameters = TAKE_PTR(context.parameters); | |
2214 | if (ret_error_id) | |
2215 | *ret_error_id = TAKE_PTR(context.error_id); | |
2216 | if (ret_flags) | |
2217 | *ret_flags = context.flags; | |
2218 | return 1; | |
2219 | } | |
2220 | ||
2221 | int varlink_collectb( | |
2222 | Varlink *v, | |
2223 | const char *method, | |
2224 | JsonVariant **ret_parameters, | |
2225 | const char **ret_error_id, | |
2226 | VarlinkReplyFlags *ret_flags, ...) { | |
2227 | ||
2228 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
2229 | va_list ap; | |
2230 | int r; | |
2231 | ||
2232 | assert_return(v, -EINVAL); | |
2233 | ||
2234 | va_start(ap, ret_flags); | |
2235 | r = json_buildv(¶meters, ap); | |
2236 | va_end(ap); | |
2237 | ||
2238 | if (r < 0) | |
2239 | return varlink_log_errno(v, r, "Failed to build json message: %m"); | |
2240 | ||
2241 | return varlink_collect(v, method, parameters, ret_parameters, ret_error_id, ret_flags); | |
2242 | } | |
2243 | ||
d41bd96f LP |
2244 | int varlink_reply(Varlink *v, JsonVariant *parameters) { |
2245 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
2246 | int r; | |
2247 | ||
2248 | assert_return(v, -EINVAL); | |
2249 | ||
2250 | if (v->state == VARLINK_DISCONNECTED) | |
2251 | return -ENOTCONN; | |
2252 | if (!IN_SET(v->state, | |
2253 | VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE, | |
2254 | VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) | |
2255 | return -EBUSY; | |
2256 | ||
2257 | r = varlink_sanitize_parameters(¶meters); | |
2258 | if (r < 0) | |
db3d4222 | 2259 | return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); |
d41bd96f LP |
2260 | |
2261 | r = json_build(&m, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
2262 | if (r < 0) | |
db3d4222 | 2263 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f | 2264 | |
7e006b49 LP |
2265 | if (v->current_method) { |
2266 | const char *bad_field = NULL; | |
2267 | ||
2268 | r = varlink_idl_validate_method_reply(v->current_method, parameters, &bad_field); | |
2269 | if (r < 0) | |
6bcc1232 | 2270 | varlink_log_errno(v, r, "Return parameters for method reply %s() didn't pass validation on field '%s', ignoring: %m", v->current_method->name, strna(bad_field)); |
7e006b49 LP |
2271 | } |
2272 | ||
d41bd96f LP |
2273 | r = varlink_enqueue_json(v, m); |
2274 | if (r < 0) | |
db3d4222 | 2275 | return varlink_log_errno(v, r, "Failed to enqueue json message: %m"); |
d41bd96f LP |
2276 | |
2277 | if (IN_SET(v->state, VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) { | |
2278 | /* We just replied to a method call that was let hanging for a while (i.e. we were outside of | |
2279 | * the varlink_dispatch_method() stack frame), which means with this reply we are ready to | |
2280 | * process further messages. */ | |
790446bd | 2281 | varlink_clear_current(v); |
d41bd96f LP |
2282 | varlink_set_state(v, VARLINK_IDLE_SERVER); |
2283 | } else | |
2284 | /* We replied to a method call from within the varlink_dispatch_method() stack frame), which | |
2285 | * means we should it handle the rest of the state engine. */ | |
2286 | varlink_set_state(v, VARLINK_PROCESSED_METHOD); | |
2287 | ||
2288 | return 1; | |
2289 | } | |
2290 | ||
2291 | int varlink_replyb(Varlink *v, ...) { | |
2292 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
2293 | va_list ap; | |
2294 | int r; | |
2295 | ||
2296 | assert_return(v, -EINVAL); | |
2297 | ||
2298 | va_start(ap, v); | |
2299 | r = json_buildv(¶meters, ap); | |
2300 | va_end(ap); | |
2301 | ||
2302 | if (r < 0) | |
2303 | return r; | |
2304 | ||
2305 | return varlink_reply(v, parameters); | |
2306 | } | |
2307 | ||
2308 | int varlink_error(Varlink *v, const char *error_id, JsonVariant *parameters) { | |
2309 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
2310 | int r; | |
2311 | ||
2312 | assert_return(v, -EINVAL); | |
2313 | assert_return(error_id, -EINVAL); | |
2314 | ||
2315 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 2316 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
d41bd96f LP |
2317 | if (!IN_SET(v->state, |
2318 | VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE, | |
2319 | VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) | |
db3d4222 | 2320 | return varlink_log_errno(v, SYNTHETIC_ERRNO(EBUSY), "Connection busy."); |
d41bd96f | 2321 | |
d37cdac6 LP |
2322 | /* Reset the list of pushed file descriptors before sending an error reply. We do this here to |
2323 | * simplify code that puts together a complex reply message with fds, and half-way something | |
2324 | * fails. In that case the pushed fds need to be flushed out again. Under the assumption that it | |
2325 | * never makes sense to send fds along with errors we simply flush them out here beforehand, so that | |
2326 | * the callers don't need to do this explicitly. */ | |
2327 | varlink_reset_fds(v); | |
2328 | ||
d41bd96f LP |
2329 | r = varlink_sanitize_parameters(¶meters); |
2330 | if (r < 0) | |
db3d4222 | 2331 | return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); |
d41bd96f LP |
2332 | |
2333 | r = json_build(&m, JSON_BUILD_OBJECT( | |
2334 | JSON_BUILD_PAIR("error", JSON_BUILD_STRING(error_id)), | |
2335 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)))); | |
2336 | if (r < 0) | |
db3d4222 | 2337 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f | 2338 | |
7e006b49 LP |
2339 | VarlinkSymbol *symbol = hashmap_get(v->server->symbols, error_id); |
2340 | if (!symbol) | |
6bcc1232 | 2341 | varlink_log(v, "No interface description defined for error '%s', not validating.", error_id); |
7e006b49 LP |
2342 | else { |
2343 | const char *bad_field = NULL; | |
2344 | ||
80f6507c | 2345 | r = varlink_idl_validate_error(symbol, parameters, &bad_field); |
7e006b49 | 2346 | if (r < 0) |
6bcc1232 | 2347 | varlink_log_errno(v, r, "Parameters for error %s didn't pass validation on field '%s', ignoring: %m", error_id, strna(bad_field)); |
7e006b49 LP |
2348 | } |
2349 | ||
d41bd96f LP |
2350 | r = varlink_enqueue_json(v, m); |
2351 | if (r < 0) | |
db3d4222 | 2352 | return varlink_log_errno(v, r, "Failed to enqueue json message: %m"); |
d41bd96f LP |
2353 | |
2354 | if (IN_SET(v->state, VARLINK_PENDING_METHOD, VARLINK_PENDING_METHOD_MORE)) { | |
790446bd | 2355 | varlink_clear_current(v); |
d41bd96f LP |
2356 | varlink_set_state(v, VARLINK_IDLE_SERVER); |
2357 | } else | |
2358 | varlink_set_state(v, VARLINK_PROCESSED_METHOD); | |
2359 | ||
2360 | return 1; | |
2361 | } | |
2362 | ||
2363 | int varlink_errorb(Varlink *v, const char *error_id, ...) { | |
2364 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
2365 | va_list ap; | |
2366 | int r; | |
2367 | ||
2368 | assert_return(v, -EINVAL); | |
2369 | assert_return(error_id, -EINVAL); | |
2370 | ||
2371 | va_start(ap, error_id); | |
2372 | r = json_buildv(¶meters, ap); | |
2373 | va_end(ap); | |
2374 | ||
2375 | if (r < 0) | |
db3d4222 | 2376 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f LP |
2377 | |
2378 | return varlink_error(v, error_id, parameters); | |
2379 | } | |
2380 | ||
2381 | int varlink_error_invalid_parameter(Varlink *v, JsonVariant *parameters) { | |
e8aba093 | 2382 | int r; |
d41bd96f LP |
2383 | |
2384 | assert_return(v, -EINVAL); | |
2385 | assert_return(parameters, -EINVAL); | |
2386 | ||
2387 | /* We expect to be called in one of two ways: the 'parameters' argument is a string variant in which | |
2388 | * case it is the parameter key name that is invalid. Or the 'parameters' argument is an object | |
2389 | * variant in which case we'll pull out the first key. The latter mode is useful in functions that | |
2390 | * don't expect any arguments. */ | |
2391 | ||
e8aba093 VCS |
2392 | /* varlink_error(...) expects a json object as the third parameter. Passing a string variant causes |
2393 | * parameter sanitization to fail, and it returns -EINVAL. */ | |
2394 | ||
2395 | if (json_variant_is_string(parameters)) { | |
2396 | _cleanup_(json_variant_unrefp) JsonVariant *parameters_obj = NULL; | |
2397 | ||
2398 | r = json_build(¶meters_obj, | |
2399 | JSON_BUILD_OBJECT( | |
2400 | JSON_BUILD_PAIR("parameter", JSON_BUILD_VARIANT(parameters)))); | |
2401 | if (r < 0) | |
2402 | return r; | |
2403 | ||
2404 | return varlink_error(v, VARLINK_ERROR_INVALID_PARAMETER, parameters_obj); | |
2405 | } | |
d41bd96f LP |
2406 | |
2407 | if (json_variant_is_object(parameters) && | |
e8aba093 VCS |
2408 | json_variant_elements(parameters) > 0) { |
2409 | _cleanup_(json_variant_unrefp) JsonVariant *parameters_obj = NULL; | |
2410 | ||
2411 | r = json_build(¶meters_obj, | |
2412 | JSON_BUILD_OBJECT( | |
2413 | JSON_BUILD_PAIR("parameter", JSON_BUILD_VARIANT(json_variant_by_index(parameters, 0))))); | |
2414 | if (r < 0) | |
2415 | return r; | |
2416 | ||
2417 | return varlink_error(v, VARLINK_ERROR_INVALID_PARAMETER, parameters_obj); | |
2418 | } | |
d41bd96f LP |
2419 | |
2420 | return -EINVAL; | |
2421 | } | |
2422 | ||
afc50293 LP |
2423 | int varlink_error_invalid_parameter_name(Varlink *v, const char *name) { |
2424 | return varlink_errorb( | |
2425 | v, | |
2426 | VARLINK_ERROR_INVALID_PARAMETER, | |
2427 | JSON_BUILD_OBJECT(JSON_BUILD_PAIR("parameter", JSON_BUILD_STRING(name)))); | |
2428 | } | |
2429 | ||
7466e94f LP |
2430 | int varlink_error_errno(Varlink *v, int error) { |
2431 | return varlink_errorb( | |
2432 | v, | |
2433 | VARLINK_ERROR_SYSTEM, | |
2434 | JSON_BUILD_OBJECT(JSON_BUILD_PAIR("errno", JSON_BUILD_INTEGER(abs(error))))); | |
2435 | } | |
2436 | ||
d41bd96f LP |
2437 | int varlink_notify(Varlink *v, JsonVariant *parameters) { |
2438 | _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; | |
2439 | int r; | |
2440 | ||
2441 | assert_return(v, -EINVAL); | |
2442 | ||
2443 | if (v->state == VARLINK_DISCONNECTED) | |
db3d4222 | 2444 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENOTCONN), "Not connected."); |
47c9bbb1 LP |
2445 | |
2446 | /* If we want to reply with a notify connection but the caller didn't set "more", then return an | |
2447 | * error indicating that we expected to be called with "more" set */ | |
2448 | if (IN_SET(v->state, VARLINK_PROCESSING_METHOD, VARLINK_PENDING_METHOD)) | |
2449 | return varlink_error(v, VARLINK_ERROR_EXPECTED_MORE, NULL); | |
2450 | ||
d41bd96f | 2451 | if (!IN_SET(v->state, VARLINK_PROCESSING_METHOD_MORE, VARLINK_PENDING_METHOD_MORE)) |
db3d4222 | 2452 | return varlink_log_errno(v, SYNTHETIC_ERRNO(EBUSY), "Connection busy."); |
d41bd96f LP |
2453 | |
2454 | r = varlink_sanitize_parameters(¶meters); | |
2455 | if (r < 0) | |
db3d4222 | 2456 | return varlink_log_errno(v, r, "Failed to sanitize parameters: %m"); |
d41bd96f LP |
2457 | |
2458 | r = json_build(&m, JSON_BUILD_OBJECT( | |
2459 | JSON_BUILD_PAIR("parameters", JSON_BUILD_VARIANT(parameters)), | |
2460 | JSON_BUILD_PAIR("continues", JSON_BUILD_BOOLEAN(true)))); | |
2461 | if (r < 0) | |
db3d4222 | 2462 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f | 2463 | |
7e006b49 LP |
2464 | if (v->current_method) { |
2465 | const char *bad_field = NULL; | |
2466 | ||
2467 | r = varlink_idl_validate_method_reply(v->current_method, parameters, &bad_field); | |
2468 | if (r < 0) | |
6bcc1232 | 2469 | varlink_log_errno(v, r, "Return parameters for method reply %s() didn't pass validation on field '%s', ignoring: %m", v->current_method->name, strna(bad_field)); |
7e006b49 LP |
2470 | } |
2471 | ||
d41bd96f LP |
2472 | r = varlink_enqueue_json(v, m); |
2473 | if (r < 0) | |
db3d4222 | 2474 | return varlink_log_errno(v, r, "Failed to enqueue json message: %m"); |
d41bd96f LP |
2475 | |
2476 | /* No state change, as more is coming */ | |
2477 | return 1; | |
2478 | } | |
2479 | ||
2480 | int varlink_notifyb(Varlink *v, ...) { | |
2481 | _cleanup_(json_variant_unrefp) JsonVariant *parameters = NULL; | |
2482 | va_list ap; | |
2483 | int r; | |
2484 | ||
2485 | assert_return(v, -EINVAL); | |
2486 | ||
2487 | va_start(ap, v); | |
2488 | r = json_buildv(¶meters, ap); | |
2489 | va_end(ap); | |
2490 | ||
2491 | if (r < 0) | |
db3d4222 | 2492 | return varlink_log_errno(v, r, "Failed to build json message: %m"); |
d41bd96f LP |
2493 | |
2494 | return varlink_notify(v, parameters); | |
2495 | } | |
2496 | ||
f1b622a0 LP |
2497 | int varlink_dispatch(Varlink *v, JsonVariant *parameters, const JsonDispatch table[], void *userdata) { |
2498 | const char *bad_field = NULL; | |
2499 | int r; | |
2500 | ||
2501 | assert_return(v, -EINVAL); | |
2502 | assert_return(table, -EINVAL); | |
2503 | ||
2504 | /* A wrapper around json_dispatch_full() that returns a nice InvalidParameter error if we hit a problem with some field. */ | |
2505 | ||
2506 | r = json_dispatch_full(parameters, table, /* bad= */ NULL, /* flags= */ 0, userdata, &bad_field); | |
2507 | if (r < 0) { | |
2508 | if (bad_field) | |
afc50293 | 2509 | return varlink_error_invalid_parameter_name(v, bad_field); |
f1b622a0 LP |
2510 | return r; |
2511 | } | |
2512 | ||
2513 | return 0; | |
2514 | } | |
2515 | ||
d41bd96f LP |
2516 | int varlink_bind_reply(Varlink *v, VarlinkReply callback) { |
2517 | assert_return(v, -EINVAL); | |
2518 | ||
2519 | if (callback && v->reply_callback && callback != v->reply_callback) | |
db3d4222 | 2520 | return varlink_log_errno(v, SYNTHETIC_ERRNO(EBUSY), "A different callback was already set."); |
d41bd96f LP |
2521 | |
2522 | v->reply_callback = callback; | |
2523 | ||
2524 | return 0; | |
2525 | } | |
2526 | ||
2527 | void* varlink_set_userdata(Varlink *v, void *userdata) { | |
2528 | void *old; | |
2529 | ||
2530 | assert_return(v, NULL); | |
2531 | ||
2532 | old = v->userdata; | |
2533 | v->userdata = userdata; | |
2534 | ||
2535 | return old; | |
2536 | } | |
2537 | ||
2538 | void* varlink_get_userdata(Varlink *v) { | |
2539 | assert_return(v, NULL); | |
2540 | ||
2541 | return v->userdata; | |
2542 | } | |
2543 | ||
2544 | static int varlink_acquire_ucred(Varlink *v) { | |
2545 | int r; | |
2546 | ||
2547 | assert(v); | |
2548 | ||
2549 | if (v->ucred_acquired) | |
2550 | return 0; | |
2551 | ||
2552 | r = getpeercred(v->fd, &v->ucred); | |
2553 | if (r < 0) | |
2554 | return r; | |
2555 | ||
2556 | v->ucred_acquired = true; | |
2557 | return 0; | |
2558 | } | |
2559 | ||
2560 | int varlink_get_peer_uid(Varlink *v, uid_t *ret) { | |
2561 | int r; | |
2562 | ||
2563 | assert_return(v, -EINVAL); | |
2564 | assert_return(ret, -EINVAL); | |
2565 | ||
2566 | r = varlink_acquire_ucred(v); | |
2567 | if (r < 0) | |
db3d4222 | 2568 | return varlink_log_errno(v, r, "Failed to acquire credentials: %m"); |
d41bd96f LP |
2569 | |
2570 | if (!uid_is_valid(v->ucred.uid)) | |
db3d4222 | 2571 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENODATA), "Peer uid is invalid."); |
d41bd96f LP |
2572 | |
2573 | *ret = v->ucred.uid; | |
2574 | return 0; | |
2575 | } | |
2576 | ||
2577 | int varlink_get_peer_pid(Varlink *v, pid_t *ret) { | |
2578 | int r; | |
2579 | ||
2580 | assert_return(v, -EINVAL); | |
2581 | assert_return(ret, -EINVAL); | |
2582 | ||
2583 | r = varlink_acquire_ucred(v); | |
2584 | if (r < 0) | |
db3d4222 | 2585 | return varlink_log_errno(v, r, "Failed to acquire credentials: %m"); |
d41bd96f LP |
2586 | |
2587 | if (!pid_is_valid(v->ucred.pid)) | |
db3d4222 | 2588 | return varlink_log_errno(v, SYNTHETIC_ERRNO(ENODATA), "Peer uid is invalid."); |
d41bd96f LP |
2589 | |
2590 | *ret = v->ucred.pid; | |
2591 | return 0; | |
2592 | } | |
2593 | ||
2594 | int varlink_set_relative_timeout(Varlink *v, usec_t timeout) { | |
2595 | assert_return(v, -EINVAL); | |
2596 | assert_return(timeout > 0, -EINVAL); | |
2597 | ||
2598 | v->timeout = timeout; | |
2599 | return 0; | |
2600 | } | |
2601 | ||
2602 | VarlinkServer *varlink_get_server(Varlink *v) { | |
2603 | assert_return(v, NULL); | |
2604 | ||
2605 | return v->server; | |
2606 | } | |
2607 | ||
2608 | int varlink_set_description(Varlink *v, const char *description) { | |
2609 | assert_return(v, -EINVAL); | |
2610 | ||
2611 | return free_and_strdup(&v->description, description); | |
2612 | } | |
2613 | ||
2614 | static int io_callback(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
99534007 | 2615 | Varlink *v = ASSERT_PTR(userdata); |
d41bd96f LP |
2616 | |
2617 | assert(s); | |
d41bd96f LP |
2618 | |
2619 | handle_revents(v, revents); | |
2620 | (void) varlink_process(v); | |
2621 | ||
2622 | return 1; | |
2623 | } | |
2624 | ||
2625 | static int time_callback(sd_event_source *s, uint64_t usec, void *userdata) { | |
99534007 | 2626 | Varlink *v = ASSERT_PTR(userdata); |
d41bd96f LP |
2627 | |
2628 | assert(s); | |
d41bd96f LP |
2629 | |
2630 | (void) varlink_process(v); | |
2631 | return 1; | |
2632 | } | |
2633 | ||
2634 | static int defer_callback(sd_event_source *s, void *userdata) { | |
99534007 | 2635 | Varlink *v = ASSERT_PTR(userdata); |
d41bd96f LP |
2636 | |
2637 | assert(s); | |
d41bd96f LP |
2638 | |
2639 | (void) varlink_process(v); | |
2640 | return 1; | |
2641 | } | |
2642 | ||
2643 | static int prepare_callback(sd_event_source *s, void *userdata) { | |
99534007 | 2644 | Varlink *v = ASSERT_PTR(userdata); |
d41bd96f LP |
2645 | int r, e; |
2646 | usec_t until; | |
f1194f5d | 2647 | bool have_timeout; |
d41bd96f LP |
2648 | |
2649 | assert(s); | |
d41bd96f LP |
2650 | |
2651 | e = varlink_get_events(v); | |
2652 | if (e < 0) | |
2653 | return e; | |
2654 | ||
2655 | r = sd_event_source_set_io_events(v->io_event_source, e); | |
2656 | if (r < 0) | |
db3d4222 | 2657 | return varlink_log_errno(v, r, "Failed to set source events: %m"); |
d41bd96f LP |
2658 | |
2659 | r = varlink_get_timeout(v, &until); | |
2660 | if (r < 0) | |
2661 | return r; | |
f1194f5d LP |
2662 | have_timeout = r > 0; |
2663 | ||
2664 | if (have_timeout) { | |
d41bd96f LP |
2665 | r = sd_event_source_set_time(v->time_event_source, until); |
2666 | if (r < 0) | |
db3d4222 | 2667 | return varlink_log_errno(v, r, "Failed to set source time: %m"); |
d41bd96f LP |
2668 | } |
2669 | ||
f1194f5d | 2670 | r = sd_event_source_set_enabled(v->time_event_source, have_timeout ? SD_EVENT_ON : SD_EVENT_OFF); |
d41bd96f | 2671 | if (r < 0) |
db3d4222 | 2672 | return varlink_log_errno(v, r, "Failed to enable event source: %m"); |
d41bd96f LP |
2673 | |
2674 | return 1; | |
2675 | } | |
2676 | ||
2677 | static int quit_callback(sd_event_source *event, void *userdata) { | |
99534007 | 2678 | Varlink *v = ASSERT_PTR(userdata); |
d41bd96f LP |
2679 | |
2680 | assert(event); | |
d41bd96f LP |
2681 | |
2682 | varlink_flush(v); | |
2683 | varlink_close(v); | |
2684 | ||
2685 | return 1; | |
2686 | } | |
2687 | ||
2688 | int varlink_attach_event(Varlink *v, sd_event *e, int64_t priority) { | |
2689 | int r; | |
2690 | ||
2691 | assert_return(v, -EINVAL); | |
2692 | assert_return(!v->event, -EBUSY); | |
2693 | ||
2694 | if (e) | |
2695 | v->event = sd_event_ref(e); | |
2696 | else { | |
2697 | r = sd_event_default(&v->event); | |
2698 | if (r < 0) | |
db3d4222 | 2699 | return varlink_log_errno(v, r, "Failed to create event source: %m"); |
d41bd96f LP |
2700 | } |
2701 | ||
2702 | r = sd_event_add_time(v->event, &v->time_event_source, CLOCK_MONOTONIC, 0, 0, time_callback, v); | |
2703 | if (r < 0) | |
2704 | goto fail; | |
2705 | ||
2706 | r = sd_event_source_set_priority(v->time_event_source, priority); | |
2707 | if (r < 0) | |
2708 | goto fail; | |
2709 | ||
2710 | (void) sd_event_source_set_description(v->time_event_source, "varlink-time"); | |
2711 | ||
2712 | r = sd_event_add_exit(v->event, &v->quit_event_source, quit_callback, v); | |
2713 | if (r < 0) | |
2714 | goto fail; | |
2715 | ||
2716 | r = sd_event_source_set_priority(v->quit_event_source, priority); | |
2717 | if (r < 0) | |
2718 | goto fail; | |
2719 | ||
2720 | (void) sd_event_source_set_description(v->quit_event_source, "varlink-quit"); | |
2721 | ||
2722 | r = sd_event_add_io(v->event, &v->io_event_source, v->fd, 0, io_callback, v); | |
2723 | if (r < 0) | |
2724 | goto fail; | |
2725 | ||
2726 | r = sd_event_source_set_prepare(v->io_event_source, prepare_callback); | |
2727 | if (r < 0) | |
2728 | goto fail; | |
2729 | ||
2730 | r = sd_event_source_set_priority(v->io_event_source, priority); | |
2731 | if (r < 0) | |
2732 | goto fail; | |
2733 | ||
2734 | (void) sd_event_source_set_description(v->io_event_source, "varlink-io"); | |
2735 | ||
2736 | r = sd_event_add_defer(v->event, &v->defer_event_source, defer_callback, v); | |
2737 | if (r < 0) | |
2738 | goto fail; | |
2739 | ||
2740 | r = sd_event_source_set_priority(v->defer_event_source, priority); | |
2741 | if (r < 0) | |
2742 | goto fail; | |
2743 | ||
2744 | (void) sd_event_source_set_description(v->defer_event_source, "varlink-defer"); | |
2745 | ||
2746 | return 0; | |
2747 | ||
2748 | fail: | |
db3d4222 | 2749 | varlink_log_errno(v, r, "Failed to setup event source: %m"); |
d41bd96f LP |
2750 | varlink_detach_event(v); |
2751 | return r; | |
2752 | } | |
2753 | ||
d41bd96f LP |
2754 | void varlink_detach_event(Varlink *v) { |
2755 | if (!v) | |
2756 | return; | |
2757 | ||
2758 | varlink_detach_event_sources(v); | |
2759 | ||
2760 | v->event = sd_event_unref(v->event); | |
2761 | } | |
2762 | ||
2763 | sd_event *varlink_get_event(Varlink *v) { | |
2764 | assert_return(v, NULL); | |
2765 | ||
2766 | return v->event; | |
2767 | } | |
2768 | ||
d37cdac6 LP |
2769 | int varlink_push_fd(Varlink *v, int fd) { |
2770 | int i; | |
2771 | ||
2772 | assert_return(v, -EINVAL); | |
2773 | assert_return(fd >= 0, -EBADF); | |
2774 | ||
2775 | /* Takes an fd to send along with the *next* varlink message sent via this varlink connection. This | |
2776 | * takes ownership of the specified fd. Use varlink_dup_fd() below to duplicate the fd first. */ | |
2777 | ||
2778 | if (!v->allow_fd_passing_output) | |
2779 | return -EPERM; | |
2780 | ||
2781 | if (v->n_pushed_fds >= INT_MAX) | |
2782 | return -ENOMEM; | |
2783 | ||
2784 | if (!GREEDY_REALLOC(v->pushed_fds, v->n_pushed_fds + 1)) | |
2785 | return -ENOMEM; | |
2786 | ||
2787 | i = (int) v->n_pushed_fds; | |
2788 | v->pushed_fds[v->n_pushed_fds++] = fd; | |
2789 | return i; | |
2790 | } | |
2791 | ||
2792 | int varlink_dup_fd(Varlink *v, int fd) { | |
2793 | _cleanup_close_ int dp = -1; | |
2794 | int r; | |
2795 | ||
2796 | assert_return(v, -EINVAL); | |
2797 | assert_return(fd >= 0, -EBADF); | |
2798 | ||
2799 | /* Like varlink_push_fd() but duplicates the specified fd instead of taking possession of it */ | |
2800 | ||
2801 | dp = fcntl(fd, F_DUPFD_CLOEXEC, 3); | |
2802 | if (dp < 0) | |
2803 | return -errno; | |
2804 | ||
2805 | r = varlink_push_fd(v, dp); | |
2806 | if (r < 0) | |
2807 | return r; | |
2808 | ||
2809 | TAKE_FD(dp); | |
2810 | return r; | |
2811 | } | |
2812 | ||
2813 | int varlink_reset_fds(Varlink *v) { | |
2814 | assert_return(v, -EINVAL); | |
2815 | ||
2816 | /* Closes all currently pending fds to send. This may be used whenever the caller is in the process | |
2817 | * of putting together a message with fds, and then eventually something fails and they need to | |
2818 | * rollback the fds. Note that this is implicitly called whenever an error reply is sent, see above. */ | |
2819 | ||
2820 | close_many(v->output_fds, v->n_output_fds); | |
2821 | v->n_output_fds = 0; | |
2822 | return 0; | |
2823 | } | |
2824 | ||
2825 | int varlink_peek_fd(Varlink *v, size_t i) { | |
2826 | assert_return(v, -EINVAL); | |
2827 | ||
94d82b59 | 2828 | /* Returns one of the file descriptors that were received along with the current message. This does |
d37cdac6 LP |
2829 | * not duplicate the fd nor invalidate it, it hence remains in our possession. */ |
2830 | ||
2831 | if (!v->allow_fd_passing_input) | |
2832 | return -EPERM; | |
2833 | ||
2834 | if (i >= v->n_input_fds) | |
2835 | return -ENXIO; | |
2836 | ||
2837 | return v->input_fds[i]; | |
2838 | } | |
2839 | ||
2840 | int varlink_take_fd(Varlink *v, size_t i) { | |
2841 | assert_return(v, -EINVAL); | |
2842 | ||
2843 | /* Similar to varlink_peek_fd() but the file descriptor's ownership is passed to the caller, and | |
2844 | * we'll invalidate the reference to it under our possession. If called twice in a row will return | |
2845 | * -EBADF */ | |
2846 | ||
2847 | if (!v->allow_fd_passing_input) | |
2848 | return -EPERM; | |
2849 | ||
2850 | if (i >= v->n_input_fds) | |
2851 | return -ENXIO; | |
2852 | ||
2853 | return TAKE_FD(v->input_fds[i]); | |
2854 | } | |
2855 | ||
2856 | static int verify_unix_socket(Varlink *v) { | |
2857 | assert(v); | |
2858 | ||
2859 | if (v->af < 0) { | |
2860 | struct stat st; | |
2861 | ||
2862 | if (fstat(v->fd, &st) < 0) | |
2863 | return -errno; | |
2864 | if (!S_ISSOCK(st.st_mode)) { | |
2865 | v->af = AF_UNSPEC; | |
2866 | return -ENOTSOCK; | |
2867 | } | |
2868 | ||
2869 | v->af = socket_get_family(v->fd); | |
2870 | if (v->af < 0) | |
2871 | return v->af; | |
2872 | } | |
2873 | ||
2874 | return v->af == AF_UNIX ? 0 : -ENOMEDIUM; | |
2875 | } | |
2876 | ||
2877 | int varlink_set_allow_fd_passing_input(Varlink *v, bool b) { | |
2878 | int r; | |
2879 | ||
2880 | assert_return(v, -EINVAL); | |
2881 | ||
2882 | if (v->allow_fd_passing_input == b) | |
2883 | return 0; | |
2884 | ||
2885 | if (!b) { | |
2886 | v->allow_fd_passing_input = false; | |
2887 | return 1; | |
2888 | } | |
2889 | ||
2890 | r = verify_unix_socket(v); | |
2891 | if (r < 0) | |
2892 | return r; | |
2893 | ||
2894 | v->allow_fd_passing_input = true; | |
2895 | return 0; | |
2896 | } | |
2897 | ||
2898 | int varlink_set_allow_fd_passing_output(Varlink *v, bool b) { | |
2899 | int r; | |
2900 | ||
2901 | assert_return(v, -EINVAL); | |
2902 | ||
2903 | if (v->allow_fd_passing_output == b) | |
2904 | return 0; | |
2905 | ||
2906 | if (!b) { | |
2907 | v->allow_fd_passing_output = false; | |
2908 | return 1; | |
2909 | } | |
2910 | ||
2911 | r = verify_unix_socket(v); | |
2912 | if (r < 0) | |
2913 | return r; | |
2914 | ||
2915 | v->allow_fd_passing_output = true; | |
2916 | return 0; | |
2917 | } | |
2918 | ||
d41bd96f | 2919 | int varlink_server_new(VarlinkServer **ret, VarlinkServerFlags flags) { |
7e006b49 LP |
2920 | _cleanup_(varlink_server_unrefp) VarlinkServer *s = NULL; |
2921 | int r; | |
d41bd96f LP |
2922 | |
2923 | assert_return(ret, -EINVAL); | |
2924 | assert_return((flags & ~_VARLINK_SERVER_FLAGS_ALL) == 0, -EINVAL); | |
2925 | ||
2926 | s = new(VarlinkServer, 1); | |
2927 | if (!s) | |
db3d4222 | 2928 | return log_oom_debug(); |
d41bd96f LP |
2929 | |
2930 | *s = (VarlinkServer) { | |
2931 | .n_ref = 1, | |
2932 | .flags = flags, | |
2933 | .connections_max = varlink_server_connections_max(NULL), | |
2934 | .connections_per_uid_max = varlink_server_connections_per_uid_max(NULL), | |
2935 | }; | |
2936 | ||
7e006b49 LP |
2937 | r = varlink_server_add_interface_many( |
2938 | s, | |
2939 | &vl_interface_io_systemd, | |
2940 | &vl_interface_org_varlink_service); | |
2941 | if (r < 0) | |
2942 | return r; | |
2943 | ||
2944 | *ret = TAKE_PTR(s); | |
d41bd96f LP |
2945 | return 0; |
2946 | } | |
2947 | ||
2948 | static VarlinkServer* varlink_server_destroy(VarlinkServer *s) { | |
2949 | char *m; | |
2950 | ||
2951 | if (!s) | |
2952 | return NULL; | |
2953 | ||
2954 | varlink_server_shutdown(s); | |
2955 | ||
2956 | while ((m = hashmap_steal_first_key(s->methods))) | |
2957 | free(m); | |
2958 | ||
2959 | hashmap_free(s->methods); | |
9fc843ed | 2960 | hashmap_free(s->interfaces); |
7e006b49 | 2961 | hashmap_free(s->symbols); |
d41bd96f LP |
2962 | hashmap_free(s->by_uid); |
2963 | ||
2964 | sd_event_unref(s->event); | |
2965 | ||
2966 | free(s->description); | |
2967 | ||
2968 | return mfree(s); | |
2969 | } | |
2970 | ||
2971 | DEFINE_TRIVIAL_REF_UNREF_FUNC(VarlinkServer, varlink_server, varlink_server_destroy); | |
2972 | ||
2973 | static int validate_connection(VarlinkServer *server, const struct ucred *ucred) { | |
2974 | int allowed = -1; | |
2975 | ||
2976 | assert(server); | |
2977 | assert(ucred); | |
2978 | ||
2979 | if (FLAGS_SET(server->flags, VARLINK_SERVER_ROOT_ONLY)) | |
2980 | allowed = ucred->uid == 0; | |
2981 | ||
2982 | if (FLAGS_SET(server->flags, VARLINK_SERVER_MYSELF_ONLY)) | |
2983 | allowed = allowed > 0 || ucred->uid == getuid(); | |
2984 | ||
2985 | if (allowed == 0) { /* Allow access when it is explicitly allowed or when neither | |
2986 | * VARLINK_SERVER_ROOT_ONLY nor VARLINK_SERVER_MYSELF_ONLY are specified. */ | |
2987 | varlink_server_log(server, "Unprivileged client attempted connection, refusing."); | |
2988 | return 0; | |
2989 | } | |
2990 | ||
2991 | if (server->n_connections >= server->connections_max) { | |
2992 | varlink_server_log(server, "Connection limit of %u reached, refusing.", server->connections_max); | |
2993 | return 0; | |
2994 | } | |
2995 | ||
2996 | if (FLAGS_SET(server->flags, VARLINK_SERVER_ACCOUNT_UID)) { | |
2997 | unsigned c; | |
2998 | ||
2999 | if (!uid_is_valid(ucred->uid)) { | |
3000 | varlink_server_log(server, "Client with invalid UID attempted connection, refusing."); | |
3001 | return 0; | |
3002 | } | |
3003 | ||
3004 | c = PTR_TO_UINT(hashmap_get(server->by_uid, UID_TO_PTR(ucred->uid))); | |
3005 | if (c >= server->connections_per_uid_max) { | |
3006 | varlink_server_log(server, "Per-UID connection limit of %u reached, refusing.", | |
3007 | server->connections_per_uid_max); | |
3008 | return 0; | |
3009 | } | |
3010 | } | |
3011 | ||
3012 | return 1; | |
3013 | } | |
3014 | ||
678ca213 | 3015 | static int count_connection(VarlinkServer *server, const struct ucred *ucred) { |
d41bd96f LP |
3016 | unsigned c; |
3017 | int r; | |
3018 | ||
3019 | assert(server); | |
3020 | assert(ucred); | |
3021 | ||
3022 | server->n_connections++; | |
3023 | ||
3024 | if (FLAGS_SET(server->flags, VARLINK_SERVER_ACCOUNT_UID)) { | |
a1351823 LP |
3025 | assert(uid_is_valid(ucred->uid)); |
3026 | ||
d41bd96f LP |
3027 | r = hashmap_ensure_allocated(&server->by_uid, NULL); |
3028 | if (r < 0) | |
6bcc1232 | 3029 | return varlink_server_log_errno(server, r, "Failed to allocate UID hash table: %m"); |
d41bd96f LP |
3030 | |
3031 | c = PTR_TO_UINT(hashmap_get(server->by_uid, UID_TO_PTR(ucred->uid))); | |
3032 | ||
3033 | varlink_server_log(server, "Connections of user " UID_FMT ": %u (of %u max)", | |
3034 | ucred->uid, c, server->connections_per_uid_max); | |
3035 | ||
3036 | r = hashmap_replace(server->by_uid, UID_TO_PTR(ucred->uid), UINT_TO_PTR(c + 1)); | |
3037 | if (r < 0) | |
6bcc1232 | 3038 | return varlink_server_log_errno(server, r, "Failed to increment counter in UID hash table: %m"); |
d41bd96f LP |
3039 | } |
3040 | ||
3041 | return 0; | |
3042 | } | |
3043 | ||
3044 | int varlink_server_add_connection(VarlinkServer *server, int fd, Varlink **ret) { | |
3045 | _cleanup_(varlink_unrefp) Varlink *v = NULL; | |
a995ce47 | 3046 | struct ucred ucred = UCRED_INVALID; |
d41bd96f | 3047 | bool ucred_acquired; |
d41bd96f LP |
3048 | int r; |
3049 | ||
3050 | assert_return(server, -EINVAL); | |
3051 | assert_return(fd >= 0, -EBADF); | |
3052 | ||
3053 | if ((server->flags & (VARLINK_SERVER_ROOT_ONLY|VARLINK_SERVER_ACCOUNT_UID)) != 0) { | |
3054 | r = getpeercred(fd, &ucred); | |
3055 | if (r < 0) | |
3056 | return varlink_server_log_errno(server, r, "Failed to acquire peer credentials of incoming socket, refusing: %m"); | |
3057 | ||
3058 | ucred_acquired = true; | |
3059 | ||
3060 | r = validate_connection(server, &ucred); | |
3061 | if (r < 0) | |
3062 | return r; | |
3063 | if (r == 0) | |
3064 | return -EPERM; | |
3065 | } else | |
3066 | ucred_acquired = false; | |
3067 | ||
3068 | r = varlink_new(&v); | |
3069 | if (r < 0) | |
3070 | return varlink_server_log_errno(server, r, "Failed to allocate connection object: %m"); | |
3071 | ||
3072 | r = count_connection(server, &ucred); | |
3073 | if (r < 0) | |
3074 | return r; | |
3075 | ||
3076 | v->fd = fd; | |
9807fdc1 LP |
3077 | if (server->flags & VARLINK_SERVER_INHERIT_USERDATA) |
3078 | v->userdata = server->userdata; | |
3079 | ||
d41bd96f LP |
3080 | if (ucred_acquired) { |
3081 | v->ucred = ucred; | |
3082 | v->ucred_acquired = true; | |
3083 | } | |
3084 | ||
12619d0a ZJS |
3085 | _cleanup_free_ char *desc = NULL; |
3086 | if (asprintf(&desc, "%s-%i", server->description ?: "varlink", v->fd) >= 0) | |
3087 | v->description = TAKE_PTR(desc); | |
d41bd96f LP |
3088 | |
3089 | /* Link up the server and the connection, and take reference in both directions. Note that the | |
3090 | * reference on the connection is left dangling. It will be dropped when the connection is closed, | |
3091 | * which happens in varlink_close(), including in the event loop quit callback. */ | |
3092 | v->server = varlink_server_ref(server); | |
3093 | varlink_ref(v); | |
3094 | ||
3095 | varlink_set_state(v, VARLINK_IDLE_SERVER); | |
3096 | ||
7e69d90c LP |
3097 | if (server->event) { |
3098 | r = varlink_attach_event(v, server->event, server->event_priority); | |
3099 | if (r < 0) { | |
3100 | varlink_log_errno(v, r, "Failed to attach new connection: %m"); | |
254d1313 | 3101 | v->fd = -EBADF; /* take the fd out of the connection again */ |
7e69d90c LP |
3102 | varlink_close(v); |
3103 | return r; | |
3104 | } | |
d41bd96f LP |
3105 | } |
3106 | ||
3107 | if (ret) | |
3108 | *ret = v; | |
3109 | ||
3110 | return 0; | |
3111 | } | |
3112 | ||
c14e841f AZ |
3113 | static VarlinkServerSocket *varlink_server_socket_free(VarlinkServerSocket *ss) { |
3114 | if (!ss) | |
3115 | return NULL; | |
3116 | ||
3117 | free(ss->address); | |
3118 | return mfree(ss); | |
3119 | } | |
3120 | ||
3121 | DEFINE_TRIVIAL_CLEANUP_FUNC(VarlinkServerSocket *, varlink_server_socket_free); | |
3122 | ||
d41bd96f | 3123 | static int connect_callback(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
99534007 | 3124 | VarlinkServerSocket *ss = ASSERT_PTR(userdata); |
254d1313 | 3125 | _cleanup_close_ int cfd = -EBADF; |
d41bd96f LP |
3126 | Varlink *v = NULL; |
3127 | int r; | |
3128 | ||
3129 | assert(source); | |
d41bd96f LP |
3130 | |
3131 | varlink_server_log(ss->server, "New incoming connection."); | |
3132 | ||
3133 | cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); | |
3134 | if (cfd < 0) { | |
3135 | if (ERRNO_IS_ACCEPT_AGAIN(errno)) | |
3136 | return 0; | |
3137 | ||
3138 | return varlink_server_log_errno(ss->server, errno, "Failed to accept incoming socket: %m"); | |
3139 | } | |
3140 | ||
3141 | r = varlink_server_add_connection(ss->server, cfd, &v); | |
3142 | if (r < 0) | |
3143 | return 0; | |
3144 | ||
3145 | TAKE_FD(cfd); | |
3146 | ||
3147 | if (ss->server->connect_callback) { | |
3148 | r = ss->server->connect_callback(ss->server, v, ss->server->userdata); | |
3149 | if (r < 0) { | |
3150 | varlink_log_errno(v, r, "Connection callback returned error, disconnecting client: %m"); | |
3151 | varlink_close(v); | |
3152 | return 0; | |
3153 | } | |
3154 | } | |
3155 | ||
3156 | return 0; | |
3157 | } | |
3158 | ||
c14e841f AZ |
3159 | static int varlink_server_create_listen_fd_socket(VarlinkServer *s, int fd, VarlinkServerSocket **ret_ss) { |
3160 | _cleanup_(varlink_server_socket_freep) VarlinkServerSocket *ss = NULL; | |
d41bd96f LP |
3161 | int r; |
3162 | ||
c14e841f AZ |
3163 | assert(s); |
3164 | assert(fd >= 0); | |
3165 | assert(ret_ss); | |
d41bd96f | 3166 | |
d41bd96f LP |
3167 | ss = new(VarlinkServerSocket, 1); |
3168 | if (!ss) | |
db3d4222 | 3169 | return log_oom_debug(); |
d41bd96f LP |
3170 | |
3171 | *ss = (VarlinkServerSocket) { | |
3172 | .server = s, | |
3173 | .fd = fd, | |
3174 | }; | |
3175 | ||
3176 | if (s->event) { | |
8d91b220 | 3177 | r = sd_event_add_io(s->event, &ss->event_source, fd, EPOLLIN, connect_callback, ss); |
d41bd96f LP |
3178 | if (r < 0) |
3179 | return r; | |
3180 | ||
3181 | r = sd_event_source_set_priority(ss->event_source, s->event_priority); | |
3182 | if (r < 0) | |
3183 | return r; | |
3184 | } | |
3185 | ||
c14e841f AZ |
3186 | *ret_ss = TAKE_PTR(ss); |
3187 | return 0; | |
3188 | } | |
3189 | ||
3190 | int varlink_server_listen_fd(VarlinkServer *s, int fd) { | |
3191 | _cleanup_(varlink_server_socket_freep) VarlinkServerSocket *ss = NULL; | |
3192 | int r; | |
3193 | ||
3194 | assert_return(s, -EINVAL); | |
3195 | assert_return(fd >= 0, -EBADF); | |
3196 | ||
a4edf033 LP |
3197 | r = fd_nonblock(fd, true); |
3198 | if (r < 0) | |
3199 | return r; | |
3200 | ||
3201 | r = fd_cloexec(fd, true); | |
3202 | if (r < 0) | |
3203 | return r; | |
3204 | ||
c14e841f AZ |
3205 | r = varlink_server_create_listen_fd_socket(s, fd, &ss); |
3206 | if (r < 0) | |
3207 | return r; | |
3208 | ||
d41bd96f LP |
3209 | LIST_PREPEND(sockets, s->sockets, TAKE_PTR(ss)); |
3210 | return 0; | |
3211 | } | |
3212 | ||
3213 | int varlink_server_listen_address(VarlinkServer *s, const char *address, mode_t m) { | |
c14e841f | 3214 | _cleanup_(varlink_server_socket_freep) VarlinkServerSocket *ss = NULL; |
d41bd96f | 3215 | union sockaddr_union sockaddr; |
f36a9d59 | 3216 | socklen_t sockaddr_len; |
254d1313 | 3217 | _cleanup_close_ int fd = -EBADF; |
d41bd96f LP |
3218 | int r; |
3219 | ||
3220 | assert_return(s, -EINVAL); | |
3221 | assert_return(address, -EINVAL); | |
3222 | assert_return((m & ~0777) == 0, -EINVAL); | |
3223 | ||
3224 | r = sockaddr_un_set_path(&sockaddr.un, address); | |
3225 | if (r < 0) | |
3226 | return r; | |
f36a9d59 | 3227 | sockaddr_len = r; |
d41bd96f LP |
3228 | |
3229 | fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
3230 | if (fd < 0) | |
3231 | return -errno; | |
3232 | ||
a0c41de2 LP |
3233 | fd = fd_move_above_stdio(fd); |
3234 | ||
d41bd96f LP |
3235 | (void) sockaddr_un_unlink(&sockaddr.un); |
3236 | ||
2053593f | 3237 | WITH_UMASK(~m & 0777) { |
63e00ccd CG |
3238 | r = mac_selinux_bind(fd, &sockaddr.sa, sockaddr_len); |
3239 | if (r < 0) | |
3240 | return r; | |
3241 | } | |
d41bd96f | 3242 | |
768fcd77 | 3243 | if (listen(fd, SOMAXCONN_DELUXE) < 0) |
d41bd96f LP |
3244 | return -errno; |
3245 | ||
c14e841f | 3246 | r = varlink_server_create_listen_fd_socket(s, fd, &ss); |
d41bd96f LP |
3247 | if (r < 0) |
3248 | return r; | |
3249 | ||
c14e841f AZ |
3250 | r = free_and_strdup(&ss->address, address); |
3251 | if (r < 0) | |
3252 | return r; | |
3253 | ||
3254 | LIST_PREPEND(sockets, s->sockets, TAKE_PTR(ss)); | |
d41bd96f LP |
3255 | TAKE_FD(fd); |
3256 | return 0; | |
3257 | } | |
3258 | ||
206504a5 LP |
3259 | int varlink_server_listen_auto(VarlinkServer *s) { |
3260 | _cleanup_strv_free_ char **names = NULL; | |
3261 | int r, n = 0; | |
3262 | ||
3263 | assert_return(s, -EINVAL); | |
3264 | ||
3265 | /* Adds all passed fds marked as "varlink" to our varlink server. These fds can either refer to a | |
3266 | * listening socket or to a connection socket. | |
3267 | * | |
3268 | * See https://varlink.org/#activation for the environment variables this is backed by and the | |
3269 | * recommended "varlink" identifier in $LISTEN_FDNAMES. */ | |
3270 | ||
3271 | r = sd_listen_fds_with_names(/* unset_environment= */ false, &names); | |
3272 | if (r < 0) | |
3273 | return r; | |
3274 | ||
3275 | for (int i = 0; i < r; i++) { | |
3276 | int b, fd; | |
3277 | socklen_t l = sizeof(b); | |
3278 | ||
3279 | if (!streq(names[i], "varlink")) | |
3280 | continue; | |
3281 | ||
3282 | fd = SD_LISTEN_FDS_START + i; | |
3283 | ||
3284 | if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &b, &l) < 0) | |
3285 | return -errno; | |
3286 | ||
3287 | assert(l == sizeof(b)); | |
3288 | ||
3289 | if (b) /* Listening socket? */ | |
3290 | r = varlink_server_listen_fd(s, fd); | |
3291 | else /* Otherwise assume connection socket */ | |
3292 | r = varlink_server_add_connection(s, fd, NULL); | |
3293 | if (r < 0) | |
3294 | return r; | |
3295 | ||
3296 | n++; | |
3297 | } | |
3298 | ||
3299 | return n; | |
3300 | } | |
3301 | ||
d41bd96f LP |
3302 | void* varlink_server_set_userdata(VarlinkServer *s, void *userdata) { |
3303 | void *ret; | |
3304 | ||
3305 | assert_return(s, NULL); | |
3306 | ||
3307 | ret = s->userdata; | |
3308 | s->userdata = userdata; | |
3309 | ||
3310 | return ret; | |
3311 | } | |
3312 | ||
3313 | void* varlink_server_get_userdata(VarlinkServer *s) { | |
3314 | assert_return(s, NULL); | |
3315 | ||
3316 | return s->userdata; | |
3317 | } | |
3318 | ||
2e5edb93 LP |
3319 | int varlink_server_loop_auto(VarlinkServer *server) { |
3320 | _cleanup_(sd_event_unrefp) sd_event *event = NULL; | |
3321 | int r; | |
3322 | ||
3323 | assert_return(server, -EINVAL); | |
3324 | assert_return(!server->event, -EBUSY); | |
3325 | ||
3326 | /* Runs a Varlink service event loop populated with a passed fd. Exits on the last connection. */ | |
3327 | ||
3328 | r = sd_event_new(&event); | |
3329 | if (r < 0) | |
3330 | return r; | |
3331 | ||
3332 | r = varlink_server_set_exit_on_idle(server, true); | |
3333 | if (r < 0) | |
3334 | return r; | |
3335 | ||
3336 | r = varlink_server_attach_event(server, event, 0); | |
3337 | if (r < 0) | |
3338 | return r; | |
3339 | ||
3340 | r = varlink_server_listen_auto(server); | |
3341 | if (r < 0) | |
3342 | return r; | |
3343 | ||
3344 | return sd_event_loop(event); | |
3345 | } | |
3346 | ||
d41bd96f LP |
3347 | static VarlinkServerSocket* varlink_server_socket_destroy(VarlinkServerSocket *ss) { |
3348 | if (!ss) | |
3349 | return NULL; | |
3350 | ||
3351 | if (ss->server) | |
3352 | LIST_REMOVE(sockets, ss->server->sockets, ss); | |
3353 | ||
1d3fe304 | 3354 | sd_event_source_disable_unref(ss->event_source); |
d41bd96f LP |
3355 | |
3356 | free(ss->address); | |
3357 | safe_close(ss->fd); | |
3358 | ||
3359 | return mfree(ss); | |
3360 | } | |
3361 | ||
3362 | int varlink_server_shutdown(VarlinkServer *s) { | |
3363 | assert_return(s, -EINVAL); | |
3364 | ||
3365 | while (s->sockets) | |
3366 | varlink_server_socket_destroy(s->sockets); | |
3367 | ||
3368 | return 0; | |
3369 | } | |
3370 | ||
47723340 LP |
3371 | static void varlink_server_test_exit_on_idle(VarlinkServer *s) { |
3372 | assert(s); | |
3373 | ||
3374 | if (s->exit_on_idle && s->event && s->n_connections == 0) | |
3375 | (void) sd_event_exit(s->event, 0); | |
3376 | } | |
3377 | ||
3378 | int varlink_server_set_exit_on_idle(VarlinkServer *s, bool b) { | |
3379 | assert_return(s, -EINVAL); | |
3380 | ||
3381 | s->exit_on_idle = b; | |
3382 | varlink_server_test_exit_on_idle(s); | |
3383 | return 0; | |
3384 | } | |
3385 | ||
536827e0 AZ |
3386 | static int varlink_server_add_socket_event_source(VarlinkServer *s, VarlinkServerSocket *ss, int64_t priority) { |
3387 | _cleanup_(sd_event_source_unrefp) sd_event_source *es = NULL; | |
536827e0 AZ |
3388 | int r; |
3389 | ||
3390 | assert(s); | |
3391 | assert(s->event); | |
3392 | assert(ss); | |
3393 | assert(ss->fd >= 0); | |
3394 | assert(!ss->event_source); | |
3395 | ||
3396 | r = sd_event_add_io(s->event, &es, ss->fd, EPOLLIN, connect_callback, ss); | |
3397 | if (r < 0) | |
3398 | return r; | |
3399 | ||
3400 | r = sd_event_source_set_priority(es, priority); | |
3401 | if (r < 0) | |
3402 | return r; | |
3403 | ||
3404 | ss->event_source = TAKE_PTR(es); | |
3405 | return 0; | |
3406 | } | |
3407 | ||
d41bd96f | 3408 | int varlink_server_attach_event(VarlinkServer *s, sd_event *e, int64_t priority) { |
d41bd96f LP |
3409 | int r; |
3410 | ||
3411 | assert_return(s, -EINVAL); | |
3412 | assert_return(!s->event, -EBUSY); | |
3413 | ||
3414 | if (e) | |
3415 | s->event = sd_event_ref(e); | |
3416 | else { | |
3417 | r = sd_event_default(&s->event); | |
3418 | if (r < 0) | |
3419 | return r; | |
3420 | } | |
3421 | ||
3422 | LIST_FOREACH(sockets, ss, s->sockets) { | |
536827e0 | 3423 | r = varlink_server_add_socket_event_source(s, ss, priority); |
d41bd96f LP |
3424 | if (r < 0) |
3425 | goto fail; | |
3426 | } | |
3427 | ||
3428 | s->event_priority = priority; | |
3429 | return 0; | |
3430 | ||
3431 | fail: | |
3432 | varlink_server_detach_event(s); | |
3433 | return r; | |
3434 | } | |
3435 | ||
3436 | int varlink_server_detach_event(VarlinkServer *s) { | |
d41bd96f LP |
3437 | assert_return(s, -EINVAL); |
3438 | ||
4f538d7b LP |
3439 | LIST_FOREACH(sockets, ss, s->sockets) |
3440 | ss->event_source = sd_event_source_disable_unref(ss->event_source); | |
d41bd96f LP |
3441 | |
3442 | sd_event_unref(s->event); | |
3443 | return 0; | |
3444 | } | |
3445 | ||
3446 | sd_event *varlink_server_get_event(VarlinkServer *s) { | |
3447 | assert_return(s, NULL); | |
3448 | ||
3449 | return s->event; | |
3450 | } | |
3451 | ||
7e006b49 LP |
3452 | static bool varlink_symbol_in_interface(const char *method, const char *interface) { |
3453 | const char *p; | |
3454 | ||
3455 | assert(method); | |
3456 | assert(interface); | |
3457 | ||
3458 | p = startswith(method, interface); | |
3459 | if (!p) | |
3460 | return false; | |
3461 | ||
3462 | if (*p != '.') | |
3463 | return false; | |
3464 | ||
3465 | return !strchr(p+1, '.'); | |
3466 | } | |
3467 | ||
d41bd96f | 3468 | int varlink_server_bind_method(VarlinkServer *s, const char *method, VarlinkMethod callback) { |
db3d4222 | 3469 | _cleanup_free_ char *m = NULL; |
d41bd96f LP |
3470 | int r; |
3471 | ||
3472 | assert_return(s, -EINVAL); | |
3473 | assert_return(method, -EINVAL); | |
3474 | assert_return(callback, -EINVAL); | |
3475 | ||
7e006b49 LP |
3476 | if (varlink_symbol_in_interface(method, "org.varlink.service") || |
3477 | varlink_symbol_in_interface(method, "io.systemd")) | |
6bcc1232 | 3478 | return varlink_server_log_errno(s, SYNTHETIC_ERRNO(EEXIST), "Cannot bind server to '%s'.", method); |
d41bd96f | 3479 | |
d41bd96f LP |
3480 | m = strdup(method); |
3481 | if (!m) | |
db3d4222 | 3482 | return log_oom_debug(); |
d41bd96f | 3483 | |
1d2d1654 SS |
3484 | r = hashmap_ensure_put(&s->methods, &string_hash_ops, m, callback); |
3485 | if (r == -ENOMEM) | |
3486 | return log_oom_debug(); | |
db3d4222 | 3487 | if (r < 0) |
6bcc1232 | 3488 | return varlink_server_log_errno(s, r, "Failed to register callback: %m"); |
db3d4222 ZJS |
3489 | if (r > 0) |
3490 | TAKE_PTR(m); | |
d41bd96f LP |
3491 | |
3492 | return 0; | |
3493 | } | |
3494 | ||
3495 | int varlink_server_bind_method_many_internal(VarlinkServer *s, ...) { | |
3496 | va_list ap; | |
e7b93f97 | 3497 | int r = 0; |
d41bd96f LP |
3498 | |
3499 | assert_return(s, -EINVAL); | |
3500 | ||
3501 | va_start(ap, s); | |
3502 | for (;;) { | |
3503 | VarlinkMethod callback; | |
3504 | const char *method; | |
3505 | ||
3506 | method = va_arg(ap, const char *); | |
3507 | if (!method) | |
3508 | break; | |
3509 | ||
3510 | callback = va_arg(ap, VarlinkMethod); | |
3511 | ||
3512 | r = varlink_server_bind_method(s, method, callback); | |
3513 | if (r < 0) | |
e7b93f97 | 3514 | break; |
d41bd96f | 3515 | } |
e7b93f97 | 3516 | va_end(ap); |
d41bd96f | 3517 | |
e7b93f97 | 3518 | return r; |
d41bd96f LP |
3519 | } |
3520 | ||
3521 | int varlink_server_bind_connect(VarlinkServer *s, VarlinkConnect callback) { | |
3522 | assert_return(s, -EINVAL); | |
3523 | ||
3524 | if (callback && s->connect_callback && callback != s->connect_callback) | |
6bcc1232 | 3525 | return varlink_server_log_errno(s, SYNTHETIC_ERRNO(EBUSY), "A different callback was already set."); |
d41bd96f LP |
3526 | |
3527 | s->connect_callback = callback; | |
3528 | return 0; | |
3529 | } | |
3530 | ||
6d4d6002 LP |
3531 | int varlink_server_bind_disconnect(VarlinkServer *s, VarlinkDisconnect callback) { |
3532 | assert_return(s, -EINVAL); | |
3533 | ||
3534 | if (callback && s->disconnect_callback && callback != s->disconnect_callback) | |
6bcc1232 | 3535 | return varlink_server_log_errno(s, SYNTHETIC_ERRNO(EBUSY), "A different callback was already set."); |
6d4d6002 LP |
3536 | |
3537 | s->disconnect_callback = callback; | |
3538 | return 0; | |
3539 | } | |
3540 | ||
9fc843ed | 3541 | int varlink_server_add_interface(VarlinkServer *s, const VarlinkInterface *interface) { |
7e006b49 LP |
3542 | int r; |
3543 | ||
9fc843ed LP |
3544 | assert_return(s, -EINVAL); |
3545 | assert_return(interface, -EINVAL); | |
3546 | assert_return(interface->name, -EINVAL); | |
3547 | ||
3548 | if (hashmap_contains(s->interfaces, interface->name)) | |
6bcc1232 | 3549 | return varlink_server_log_errno(s, SYNTHETIC_ERRNO(EEXIST), "Duplicate registration of interface '%s'.", interface->name); |
9fc843ed | 3550 | |
7e006b49 LP |
3551 | r = hashmap_ensure_put(&s->interfaces, &string_hash_ops, interface->name, (void*) interface); |
3552 | if (r < 0) | |
3553 | return r; | |
3554 | ||
3555 | for (const VarlinkSymbol *const*symbol = interface->symbols; *symbol; symbol++) { | |
3556 | _cleanup_free_ char *j = NULL; | |
3557 | ||
3558 | /* We only ever want to validate method calls/replies and errors against the interface | |
3559 | * definitions, hence don't bother with the type symbols */ | |
3560 | if (!IN_SET((*symbol)->symbol_type, VARLINK_METHOD, VARLINK_ERROR)) | |
3561 | continue; | |
3562 | ||
3563 | j = strjoin(interface->name, ".", (*symbol)->name); | |
3564 | if (!j) | |
3565 | return -ENOMEM; | |
3566 | ||
3567 | r = hashmap_ensure_put(&s->symbols, &string_hash_ops_free, j, (void*) *symbol); | |
3568 | if (r < 0) | |
3569 | return r; | |
3570 | ||
3571 | TAKE_PTR(j); | |
3572 | } | |
3573 | ||
3574 | return 0; | |
9fc843ed LP |
3575 | } |
3576 | ||
3577 | int varlink_server_add_interface_many_internal(VarlinkServer *s, ...) { | |
3578 | va_list ap; | |
3579 | int r = 0; | |
3580 | ||
3581 | assert_return(s, -EINVAL); | |
3582 | ||
3583 | va_start(ap, s); | |
3584 | for (;;) { | |
3585 | const VarlinkInterface *interface = va_arg(ap, const VarlinkInterface*); | |
3586 | if (!interface) | |
3587 | break; | |
3588 | ||
3589 | r = varlink_server_add_interface(s, interface); | |
3590 | if (r < 0) | |
3591 | break; | |
3592 | } | |
3593 | va_end(ap); | |
3594 | ||
3595 | return r; | |
3596 | } | |
3597 | ||
d41bd96f | 3598 | unsigned varlink_server_connections_max(VarlinkServer *s) { |
88a36d36 | 3599 | int dts; |
d41bd96f LP |
3600 | |
3601 | /* If a server is specified, return the setting for that server, otherwise the default value */ | |
3602 | if (s) | |
3603 | return s->connections_max; | |
3604 | ||
88a36d36 LP |
3605 | dts = getdtablesize(); |
3606 | assert_se(dts > 0); | |
d41bd96f LP |
3607 | |
3608 | /* Make sure we never use up more than ¾th of RLIMIT_NOFILE for IPC */ | |
88a36d36 LP |
3609 | if (VARLINK_DEFAULT_CONNECTIONS_MAX > (unsigned) dts / 4 * 3) |
3610 | return dts / 4 * 3; | |
d41bd96f LP |
3611 | |
3612 | return VARLINK_DEFAULT_CONNECTIONS_MAX; | |
3613 | } | |
3614 | ||
3615 | unsigned varlink_server_connections_per_uid_max(VarlinkServer *s) { | |
3616 | unsigned m; | |
3617 | ||
3618 | if (s) | |
3619 | return s->connections_per_uid_max; | |
3620 | ||
3621 | /* Make sure to never use up more than ¾th of available connections for a single user */ | |
3622 | m = varlink_server_connections_max(NULL); | |
3623 | if (VARLINK_DEFAULT_CONNECTIONS_PER_UID_MAX > m) | |
3624 | return m / 4 * 3; | |
3625 | ||
3626 | return VARLINK_DEFAULT_CONNECTIONS_PER_UID_MAX; | |
3627 | } | |
3628 | ||
3629 | int varlink_server_set_connections_per_uid_max(VarlinkServer *s, unsigned m) { | |
3630 | assert_return(s, -EINVAL); | |
3631 | assert_return(m > 0, -EINVAL); | |
3632 | ||
3633 | s->connections_per_uid_max = m; | |
3634 | return 0; | |
3635 | } | |
3636 | ||
3637 | int varlink_server_set_connections_max(VarlinkServer *s, unsigned m) { | |
3638 | assert_return(s, -EINVAL); | |
3639 | assert_return(m > 0, -EINVAL); | |
3640 | ||
3641 | s->connections_max = m; | |
3642 | return 0; | |
3643 | } | |
3644 | ||
c4f601f2 LP |
3645 | unsigned varlink_server_current_connections(VarlinkServer *s) { |
3646 | assert_return(s, UINT_MAX); | |
3647 | ||
3648 | return s->n_connections; | |
3649 | } | |
3650 | ||
d41bd96f LP |
3651 | int varlink_server_set_description(VarlinkServer *s, const char *description) { |
3652 | assert_return(s, -EINVAL); | |
3653 | ||
3654 | return free_and_strdup(&s->description, description); | |
3655 | } | |
008798e9 AZ |
3656 | |
3657 | int varlink_server_serialize(VarlinkServer *s, FILE *f, FDSet *fds) { | |
3658 | assert(f); | |
3659 | assert(fds); | |
3660 | ||
3661 | if (!s) | |
3662 | return 0; | |
3663 | ||
3664 | LIST_FOREACH(sockets, ss, s->sockets) { | |
3665 | int copy; | |
3666 | ||
3667 | assert(ss->address); | |
3668 | assert(ss->fd >= 0); | |
3669 | ||
3670 | fprintf(f, "varlink-server-socket-address=%s", ss->address); | |
3671 | ||
3672 | /* If we fail to serialize the fd, it will be considered an error during deserialization */ | |
3673 | copy = fdset_put_dup(fds, ss->fd); | |
3674 | if (copy < 0) | |
3675 | return copy; | |
3676 | ||
3677 | fprintf(f, " varlink-server-socket-fd=%i", copy); | |
3678 | ||
3679 | fputc('\n', f); | |
3680 | } | |
3681 | ||
3682 | return 0; | |
3683 | } | |
3684 | ||
3685 | int varlink_server_deserialize_one(VarlinkServer *s, const char *value, FDSet *fds) { | |
3686 | _cleanup_(varlink_server_socket_freep) VarlinkServerSocket *ss = NULL; | |
3687 | _cleanup_free_ char *address = NULL; | |
3688 | const char *v = ASSERT_PTR(value); | |
254d1313 | 3689 | int r, fd = -EBADF; |
008798e9 AZ |
3690 | char *buf; |
3691 | size_t n; | |
3692 | ||
3693 | assert(s); | |
3694 | assert(fds); | |
3695 | ||
3696 | n = strcspn(v, " "); | |
3697 | address = strndup(v, n); | |
3698 | if (!address) | |
3699 | return log_oom_debug(); | |
3700 | ||
3701 | if (v[n] != ' ') | |
6bcc1232 | 3702 | return varlink_server_log_errno(s, SYNTHETIC_ERRNO(EINVAL), |
008798e9 AZ |
3703 | "Failed to deserialize VarlinkServerSocket: %s: %m", value); |
3704 | v = startswith(v + n + 1, "varlink-server-socket-fd="); | |
3705 | if (!v) | |
6bcc1232 | 3706 | return varlink_server_log_errno(s, SYNTHETIC_ERRNO(EINVAL), |
008798e9 AZ |
3707 | "Failed to deserialize VarlinkServerSocket fd %s: %m", value); |
3708 | ||
3709 | n = strcspn(v, " "); | |
3710 | buf = strndupa_safe(v, n); | |
3711 | ||
e652663a | 3712 | fd = parse_fd(buf); |
e652663a | 3713 | if (fd < 0) |
6bcc1232 | 3714 | return varlink_server_log_errno(s, fd, "Unable to parse VarlinkServerSocket varlink-server-socket-fd=%s: %m", buf); |
008798e9 | 3715 | if (!fdset_contains(fds, fd)) |
6bcc1232 | 3716 | return varlink_server_log_errno(s, SYNTHETIC_ERRNO(EBADF), |
008798e9 AZ |
3717 | "VarlinkServerSocket varlink-server-socket-fd= has unknown fd %d: %m", fd); |
3718 | ||
3719 | ss = new(VarlinkServerSocket, 1); | |
3720 | if (!ss) | |
3721 | return log_oom_debug(); | |
3722 | ||
3723 | *ss = (VarlinkServerSocket) { | |
3724 | .server = s, | |
3725 | .address = TAKE_PTR(address), | |
3726 | .fd = fdset_remove(fds, fd), | |
3727 | }; | |
3728 | ||
3729 | r = varlink_server_add_socket_event_source(s, ss, SD_EVENT_PRIORITY_NORMAL); | |
3730 | if (r < 0) | |
6bcc1232 | 3731 | return varlink_server_log_errno(s, r, "Failed to add VarlinkServerSocket event source to the event loop: %m"); |
008798e9 AZ |
3732 | |
3733 | LIST_PREPEND(sockets, s->sockets, TAKE_PTR(ss)); | |
3734 | return 0; | |
3735 | } | |
02b0d24d LP |
3736 | |
3737 | int varlink_invocation(VarlinkInvocationFlags flags) { | |
3738 | _cleanup_strv_free_ char **names = NULL; | |
3739 | int r, b; | |
3740 | socklen_t l = sizeof(b); | |
3741 | ||
3742 | /* Returns true if this is a "pure" varlink server invocation, i.e. with one fd passed. */ | |
3743 | ||
3744 | r = sd_listen_fds_with_names(/* unset_environment= */ false, &names); | |
3745 | if (r < 0) | |
3746 | return r; | |
3747 | if (r == 0) | |
3748 | return false; | |
3749 | if (r > 1) | |
3750 | return -ETOOMANYREFS; | |
3751 | ||
3752 | if (!strv_equal(names, STRV_MAKE("varlink"))) | |
3753 | return false; | |
3754 | ||
3755 | if (FLAGS_SET(flags, VARLINK_ALLOW_LISTEN|VARLINK_ALLOW_ACCEPT)) /* Both flags set? Then allow everything */ | |
3756 | return true; | |
3757 | ||
3758 | if ((flags & (VARLINK_ALLOW_LISTEN|VARLINK_ALLOW_ACCEPT)) == 0) /* Neither is set, then fail */ | |
3759 | return -EISCONN; | |
3760 | ||
3761 | if (getsockopt(SD_LISTEN_FDS_START, SOL_SOCKET, SO_ACCEPTCONN, &b, &l) < 0) | |
3762 | return -errno; | |
3763 | ||
3764 | assert(l == sizeof(b)); | |
3765 | ||
3766 | if (!FLAGS_SET(flags, b ? VARLINK_ALLOW_LISTEN : VARLINK_ALLOW_ACCEPT)) | |
3767 | return -EISCONN; | |
3768 | ||
3769 | return true; | |
3770 | } |