]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <arpa/inet.h> | |
4 | #include <fcntl.h> | |
5 | #include <linux/sctp.h> | |
6 | #include <mqueue.h> | |
7 | #include <netinet/tcp.h> | |
8 | #include <sys/stat.h> | |
9 | #include <unistd.h> | |
10 | ||
11 | #include "sd-bus.h" | |
12 | ||
13 | #include "alloc-util.h" | |
14 | #include "bpf-program.h" | |
15 | #include "bus-error.h" | |
16 | #include "copy.h" | |
17 | #include "dbus-socket.h" | |
18 | #include "dbus-unit.h" | |
19 | #include "errno-list.h" | |
20 | #include "errno-util.h" | |
21 | #include "exit-status.h" | |
22 | #include "extract-word.h" | |
23 | #include "fd-util.h" | |
24 | #include "fdset.h" | |
25 | #include "format-util.h" | |
26 | #include "fs-util.h" | |
27 | #include "glyph-util.h" | |
28 | #include "in-addr-util.h" | |
29 | #include "io-util.h" | |
30 | #include "ip-protocol-list.h" | |
31 | #include "log.h" | |
32 | #include "manager.h" | |
33 | #include "mkdir-label.h" | |
34 | #include "namespace-util.h" | |
35 | #include "parse-util.h" | |
36 | #include "path-util.h" | |
37 | #include "pidfd-util.h" | |
38 | #include "process-util.h" | |
39 | #include "recurse-dir.h" | |
40 | #include "selinux-util.h" | |
41 | #include "serialize.h" | |
42 | #include "service.h" | |
43 | #include "set.h" | |
44 | #include "siphash24.h" | |
45 | #include "smack-util.h" | |
46 | #include "socket.h" | |
47 | #include "socket-netlink.h" | |
48 | #include "special.h" | |
49 | #include "string-table.h" | |
50 | #include "string-util.h" | |
51 | #include "strv.h" | |
52 | #include "unit.h" | |
53 | #include "unit-name.h" | |
54 | #include "user-util.h" | |
55 | ||
56 | typedef struct SocketPeer { | |
57 | unsigned n_ref; | |
58 | ||
59 | Socket *socket; | |
60 | union sockaddr_union peer; | |
61 | socklen_t peer_salen; | |
62 | struct ucred peer_cred; | |
63 | } SocketPeer; | |
64 | ||
65 | static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = { | |
66 | [SOCKET_DEAD] = UNIT_INACTIVE, | |
67 | [SOCKET_START_PRE] = UNIT_ACTIVATING, | |
68 | [SOCKET_START_OPEN] = UNIT_ACTIVATING, | |
69 | [SOCKET_START_CHOWN] = UNIT_ACTIVATING, | |
70 | [SOCKET_START_POST] = UNIT_ACTIVATING, | |
71 | [SOCKET_LISTENING] = UNIT_ACTIVE, | |
72 | [SOCKET_RUNNING] = UNIT_ACTIVE, | |
73 | [SOCKET_STOP_PRE] = UNIT_DEACTIVATING, | |
74 | [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING, | |
75 | [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING, | |
76 | [SOCKET_STOP_POST] = UNIT_DEACTIVATING, | |
77 | [SOCKET_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
78 | [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
79 | [SOCKET_FAILED] = UNIT_FAILED, | |
80 | [SOCKET_CLEANING] = UNIT_MAINTENANCE, | |
81 | }; | |
82 | ||
83 | static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata); | |
84 | static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); | |
85 | ||
86 | static bool SOCKET_STATE_WITH_PROCESS(SocketState state) { | |
87 | return IN_SET(state, | |
88 | SOCKET_START_PRE, | |
89 | SOCKET_START_CHOWN, | |
90 | SOCKET_START_POST, | |
91 | SOCKET_STOP_PRE, | |
92 | SOCKET_STOP_PRE_SIGTERM, | |
93 | SOCKET_STOP_PRE_SIGKILL, | |
94 | SOCKET_STOP_POST, | |
95 | SOCKET_FINAL_SIGTERM, | |
96 | SOCKET_FINAL_SIGKILL, | |
97 | SOCKET_CLEANING); | |
98 | } | |
99 | ||
100 | static bool SOCKET_SERVICE_IS_ACTIVE(Service *s, bool allow_finalize) { | |
101 | assert(s); | |
102 | ||
103 | /* If unit_active_state() reports inactive/failed then it's all good, otherwise we need to | |
104 | * manually exclude SERVICE_AUTO_RESTART and SERVICE_AUTO_RESTART_QUEUED, in which cases | |
105 | * the start job hasn't been enqueued/run, but are only placeholders in order to allow | |
106 | * canceling auto restart. */ | |
107 | ||
108 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(UNIT(s)))) | |
109 | return false; | |
110 | ||
111 | if (IN_SET(s->state, SERVICE_AUTO_RESTART, SERVICE_AUTO_RESTART_QUEUED)) | |
112 | return false; | |
113 | ||
114 | if (allow_finalize && IN_SET(s->state, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, SERVICE_CLEANING)) | |
115 | return false; | |
116 | ||
117 | return true; | |
118 | } | |
119 | ||
120 | static void socket_init(Unit *u) { | |
121 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
122 | ||
123 | assert(u->load_state == UNIT_STUB); | |
124 | ||
125 | s->backlog = SOMAXCONN_DELUXE; | |
126 | s->timeout_usec = u->manager->defaults.timeout_start_usec; | |
127 | s->directory_mode = 0755; | |
128 | s->socket_mode = 0666; | |
129 | ||
130 | s->max_connections = 64; | |
131 | ||
132 | s->pass_rights = true; /* defaults to enabled in kernel */ | |
133 | s->priority = -1; | |
134 | s->ip_tos = -1; | |
135 | s->ip_ttl = -1; | |
136 | s->mark = -1; | |
137 | ||
138 | s->exec_context.std_output = u->manager->defaults.std_output; | |
139 | s->exec_context.std_error = u->manager->defaults.std_error; | |
140 | ||
141 | s->control_pid = PIDREF_NULL; | |
142 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
143 | ||
144 | s->trigger_limit = RATELIMIT_OFF; | |
145 | ||
146 | s->poll_limit = RATELIMIT_OFF; | |
147 | } | |
148 | ||
149 | static void socket_unwatch_control_pid(Socket *s) { | |
150 | assert(s); | |
151 | unit_unwatch_pidref_done(UNIT(s), &s->control_pid); | |
152 | } | |
153 | ||
154 | static void socket_port_close_auxiliary_fds(SocketPort *p) { | |
155 | assert(p); | |
156 | ||
157 | close_many(p->auxiliary_fds, p->n_auxiliary_fds); | |
158 | p->auxiliary_fds = mfree(p->auxiliary_fds); | |
159 | p->n_auxiliary_fds = 0; | |
160 | } | |
161 | ||
162 | SocketPort* socket_port_free(SocketPort *p) { | |
163 | if (!p) | |
164 | return NULL; | |
165 | ||
166 | sd_event_source_unref(p->event_source); | |
167 | ||
168 | socket_port_close_auxiliary_fds(p); | |
169 | safe_close(p->fd); | |
170 | free(p->path); | |
171 | ||
172 | return mfree(p); | |
173 | } | |
174 | ||
175 | void socket_free_ports(Socket *s) { | |
176 | assert(s); | |
177 | ||
178 | LIST_CLEAR(port, s->ports, socket_port_free); | |
179 | } | |
180 | ||
181 | static void socket_done(Unit *u) { | |
182 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
183 | SocketPeer *p; | |
184 | ||
185 | socket_free_ports(s); | |
186 | ||
187 | while ((p = set_steal_first(s->peers_by_address))) | |
188 | p->socket = NULL; | |
189 | ||
190 | s->peers_by_address = set_free(s->peers_by_address); | |
191 | ||
192 | s->exec_runtime = exec_runtime_free(s->exec_runtime); | |
193 | ||
194 | exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX); | |
195 | s->control_command = NULL; | |
196 | ||
197 | socket_unwatch_control_pid(s); | |
198 | ||
199 | unit_ref_unset(&s->service); | |
200 | ||
201 | s->tcp_congestion = mfree(s->tcp_congestion); | |
202 | s->bind_to_device = mfree(s->bind_to_device); | |
203 | ||
204 | s->smack = mfree(s->smack); | |
205 | s->smack_ip_in = mfree(s->smack_ip_in); | |
206 | s->smack_ip_out = mfree(s->smack_ip_out); | |
207 | ||
208 | strv_free(s->symlinks); | |
209 | ||
210 | s->user = mfree(s->user); | |
211 | s->group = mfree(s->group); | |
212 | ||
213 | s->fdname = mfree(s->fdname); | |
214 | ||
215 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); | |
216 | } | |
217 | ||
218 | static int socket_arm_timer(Socket *s, bool relative, usec_t usec) { | |
219 | assert(s); | |
220 | ||
221 | return unit_arm_timer(UNIT(s), &s->timer_event_source, relative, usec, socket_dispatch_timer); | |
222 | } | |
223 | ||
224 | static bool have_non_accept_socket(Socket *s) { | |
225 | assert(s); | |
226 | ||
227 | if (!s->accept) | |
228 | return true; | |
229 | ||
230 | LIST_FOREACH(port, p, s->ports) { | |
231 | ||
232 | if (p->type != SOCKET_SOCKET) | |
233 | return true; | |
234 | ||
235 | if (!socket_address_can_accept(&p->address)) | |
236 | return true; | |
237 | } | |
238 | ||
239 | return false; | |
240 | } | |
241 | ||
242 | static int socket_add_mount_dependencies(Socket *s) { | |
243 | int r; | |
244 | ||
245 | assert(s); | |
246 | ||
247 | LIST_FOREACH(port, p, s->ports) { | |
248 | const char *path = NULL; | |
249 | ||
250 | if (p->type == SOCKET_SOCKET) | |
251 | path = socket_address_get_path(&p->address); | |
252 | else if (IN_SET(p->type, SOCKET_FIFO, SOCKET_SPECIAL, SOCKET_USB_FUNCTION)) | |
253 | path = p->path; | |
254 | ||
255 | if (!path) | |
256 | continue; | |
257 | ||
258 | r = unit_add_mounts_for(UNIT(s), path, UNIT_DEPENDENCY_FILE, UNIT_MOUNT_REQUIRES); | |
259 | if (r < 0) | |
260 | return r; | |
261 | } | |
262 | ||
263 | return 0; | |
264 | } | |
265 | ||
266 | static int socket_add_device_dependencies(Socket *s) { | |
267 | char *t; | |
268 | ||
269 | assert(s); | |
270 | ||
271 | if (!s->bind_to_device || streq(s->bind_to_device, "lo")) | |
272 | return 0; | |
273 | ||
274 | t = strjoina("/sys/subsystem/net/devices/", s->bind_to_device); | |
275 | return unit_add_node_dependency(UNIT(s), t, UNIT_BINDS_TO, UNIT_DEPENDENCY_FILE); | |
276 | } | |
277 | ||
278 | static int socket_add_default_dependencies(Socket *s) { | |
279 | int r; | |
280 | ||
281 | assert(s); | |
282 | ||
283 | if (!UNIT(s)->default_dependencies) | |
284 | return 0; | |
285 | ||
286 | r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, true, UNIT_DEPENDENCY_DEFAULT); | |
287 | if (r < 0) | |
288 | return r; | |
289 | ||
290 | if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) { | |
291 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); | |
292 | if (r < 0) | |
293 | return r; | |
294 | } | |
295 | ||
296 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); | |
297 | } | |
298 | ||
299 | static bool socket_has_exec(Socket *s) { | |
300 | assert(s); | |
301 | ||
302 | FOREACH_ARRAY(i, s->exec_command, _SOCKET_EXEC_COMMAND_MAX) | |
303 | if (*i) | |
304 | return true; | |
305 | ||
306 | return false; | |
307 | } | |
308 | ||
309 | static int socket_add_extras(Socket *s) { | |
310 | Unit *u = UNIT(ASSERT_PTR(s)); | |
311 | int r; | |
312 | ||
313 | /* Pick defaults for the trigger limit, if nothing was explicitly configured. We pick a relatively high limit | |
314 | * in Accept=yes mode, and a lower limit for Accept=no. Reason: in Accept=yes mode we are invoking accept() | |
315 | * ourselves before the trigger limit can hit, thus incoming connections are taken off the socket queue quickly | |
316 | * and reliably. This is different for Accept=no, where the spawned service has to take the incoming traffic | |
317 | * off the queues, which it might not necessarily do. Moreover, while Accept=no services are supposed to | |
318 | * process whatever is queued in one go, and thus should normally never have to be started frequently. This is | |
319 | * different for Accept=yes where each connection is processed by a new service instance, and thus frequent | |
320 | * service starts are typical. | |
321 | * | |
322 | * For the poll limit we follow a similar rule, but use 3/4th of the trigger limit parameters, to | |
323 | * trigger this earlier. */ | |
324 | ||
325 | if (s->trigger_limit.interval == USEC_INFINITY) | |
326 | s->trigger_limit.interval = 2 * USEC_PER_SEC; | |
327 | if (s->trigger_limit.burst == UINT_MAX) | |
328 | s->trigger_limit.burst = s->accept ? 200 : 20; | |
329 | ||
330 | if (s->poll_limit.interval == USEC_INFINITY) | |
331 | s->poll_limit.interval = 2 * USEC_PER_SEC; | |
332 | if (s->poll_limit.burst == UINT_MAX) | |
333 | s->poll_limit.burst = s->accept ? 150 : 15; | |
334 | ||
335 | if (have_non_accept_socket(s)) { | |
336 | ||
337 | if (!UNIT_ISSET(s->service)) { | |
338 | Unit *x; | |
339 | ||
340 | r = unit_load_related_unit(u, ".service", &x); | |
341 | if (r < 0) | |
342 | return r; | |
343 | ||
344 | unit_ref_set(&s->service, u, x); | |
345 | } | |
346 | ||
347 | r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true, UNIT_DEPENDENCY_IMPLICIT); | |
348 | if (r < 0) | |
349 | return r; | |
350 | } | |
351 | ||
352 | r = socket_add_mount_dependencies(s); | |
353 | if (r < 0) | |
354 | return r; | |
355 | ||
356 | r = socket_add_device_dependencies(s); | |
357 | if (r < 0) | |
358 | return r; | |
359 | ||
360 | r = unit_patch_contexts(u); | |
361 | if (r < 0) | |
362 | return r; | |
363 | ||
364 | if (socket_has_exec(s)) { | |
365 | r = unit_add_exec_dependencies(u, &s->exec_context); | |
366 | if (r < 0) | |
367 | return r; | |
368 | } | |
369 | ||
370 | r = unit_set_default_slice(u); | |
371 | if (r < 0) | |
372 | return r; | |
373 | ||
374 | r = socket_add_default_dependencies(s); | |
375 | if (r < 0) | |
376 | return r; | |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
381 | static const char* socket_find_symlink_target(Socket *s) { | |
382 | const char *found = NULL; | |
383 | ||
384 | assert(s); | |
385 | ||
386 | LIST_FOREACH(port, p, s->ports) { | |
387 | const char *f; | |
388 | ||
389 | switch (p->type) { | |
390 | ||
391 | case SOCKET_FIFO: | |
392 | f = p->path; | |
393 | break; | |
394 | ||
395 | case SOCKET_SOCKET: | |
396 | f = socket_address_get_path(&p->address); | |
397 | break; | |
398 | ||
399 | default: | |
400 | f = NULL; | |
401 | } | |
402 | ||
403 | if (f) { | |
404 | if (found) | |
405 | return NULL; | |
406 | ||
407 | found = f; | |
408 | } | |
409 | } | |
410 | ||
411 | return found; | |
412 | } | |
413 | ||
414 | static int socket_verify(Socket *s) { | |
415 | assert(s); | |
416 | assert(UNIT(s)->load_state == UNIT_LOADED); | |
417 | ||
418 | if (!s->ports) | |
419 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing."); | |
420 | ||
421 | if (s->max_connections <= 0) | |
422 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "MaxConnection= setting too small. Refusing."); | |
423 | ||
424 | if (s->accept && have_non_accept_socket(s)) | |
425 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing."); | |
426 | ||
427 | if (s->accept && UNIT_ISSET(s->service)) | |
428 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Explicit service configuration for accepting socket units not supported. Refusing."); | |
429 | ||
430 | if (!strv_isempty(s->symlinks) && !socket_find_symlink_target(s)) | |
431 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Unit has symlinks set but none or more than one node in the file system. Refusing."); | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
436 | static void peer_address_hash_func(const SocketPeer *s, struct siphash *state) { | |
437 | assert(s); | |
438 | ||
439 | if (s->peer.sa.sa_family == AF_INET) | |
440 | siphash24_compress_typesafe(s->peer.in.sin_addr, state); | |
441 | else if (s->peer.sa.sa_family == AF_INET6) | |
442 | siphash24_compress_typesafe(s->peer.in6.sin6_addr, state); | |
443 | else if (s->peer.sa.sa_family == AF_VSOCK) | |
444 | siphash24_compress_typesafe(s->peer.vm.svm_cid, state); | |
445 | else if (s->peer.sa.sa_family == AF_UNIX) | |
446 | siphash24_compress_typesafe(s->peer_cred.uid, state); | |
447 | else | |
448 | assert_not_reached(); | |
449 | } | |
450 | ||
451 | static int peer_address_compare_func(const SocketPeer *x, const SocketPeer *y) { | |
452 | int r; | |
453 | ||
454 | r = CMP(x->peer.sa.sa_family, y->peer.sa.sa_family); | |
455 | if (r != 0) | |
456 | return r; | |
457 | ||
458 | switch (x->peer.sa.sa_family) { | |
459 | case AF_INET: | |
460 | return memcmp(&x->peer.in.sin_addr, &y->peer.in.sin_addr, sizeof(x->peer.in.sin_addr)); | |
461 | case AF_INET6: | |
462 | return memcmp(&x->peer.in6.sin6_addr, &y->peer.in6.sin6_addr, sizeof(x->peer.in6.sin6_addr)); | |
463 | case AF_VSOCK: | |
464 | return CMP(x->peer.vm.svm_cid, y->peer.vm.svm_cid); | |
465 | case AF_UNIX: | |
466 | return CMP(x->peer_cred.uid, y->peer_cred.uid); | |
467 | } | |
468 | assert_not_reached(); | |
469 | } | |
470 | ||
471 | DEFINE_PRIVATE_HASH_OPS(peer_address_hash_ops, SocketPeer, peer_address_hash_func, peer_address_compare_func); | |
472 | ||
473 | static int socket_load(Unit *u) { | |
474 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
475 | int r; | |
476 | ||
477 | assert(u->load_state == UNIT_STUB); | |
478 | ||
479 | r = unit_load_fragment_and_dropin(u, true); | |
480 | if (r < 0) | |
481 | return r; | |
482 | ||
483 | if (u->load_state != UNIT_LOADED) | |
484 | return 0; | |
485 | ||
486 | /* This is a new unit? Then let's add in some extras */ | |
487 | r = socket_add_extras(s); | |
488 | if (r < 0) | |
489 | return r; | |
490 | ||
491 | return socket_verify(s); | |
492 | } | |
493 | ||
494 | static SocketPeer* socket_peer_dup(const SocketPeer *q) { | |
495 | SocketPeer *p; | |
496 | ||
497 | assert(q); | |
498 | ||
499 | p = new(SocketPeer, 1); | |
500 | if (!p) | |
501 | return NULL; | |
502 | ||
503 | *p = (SocketPeer) { | |
504 | .n_ref = 1, | |
505 | .peer = q->peer, | |
506 | .peer_salen = q->peer_salen, | |
507 | .peer_cred = q->peer_cred, | |
508 | }; | |
509 | ||
510 | return p; | |
511 | } | |
512 | ||
513 | static SocketPeer* socket_peer_free(SocketPeer *p) { | |
514 | assert(p); | |
515 | ||
516 | if (p->socket) | |
517 | set_remove(p->socket->peers_by_address, p); | |
518 | ||
519 | return mfree(p); | |
520 | } | |
521 | ||
522 | DEFINE_TRIVIAL_REF_UNREF_FUNC(SocketPeer, socket_peer, socket_peer_free); | |
523 | ||
524 | int socket_acquire_peer(Socket *s, int fd, SocketPeer **ret) { | |
525 | _cleanup_(socket_peer_unrefp) SocketPeer *remote = NULL; | |
526 | SocketPeer key = { | |
527 | .peer_salen = sizeof(union sockaddr_union), | |
528 | .peer_cred = UCRED_INVALID, | |
529 | }, *i; | |
530 | int r; | |
531 | ||
532 | assert(s); | |
533 | assert(fd >= 0); | |
534 | assert(ret); | |
535 | ||
536 | if (getpeername(fd, &key.peer.sa, &key.peer_salen) < 0) | |
537 | return log_unit_error_errno(UNIT(s), errno, "getpeername() failed: %m"); | |
538 | ||
539 | switch (key.peer.sa.sa_family) { | |
540 | case AF_INET: | |
541 | case AF_INET6: | |
542 | case AF_VSOCK: | |
543 | break; | |
544 | ||
545 | case AF_UNIX: | |
546 | r = getpeercred(fd, &key.peer_cred); | |
547 | if (r < 0) | |
548 | return log_unit_error_errno(UNIT(s), r, "Failed to get peer credentials of socket: %m"); | |
549 | break; | |
550 | ||
551 | default: | |
552 | *ret = NULL; | |
553 | return 0; | |
554 | } | |
555 | ||
556 | i = set_get(s->peers_by_address, &key); | |
557 | if (i) { | |
558 | *ret = socket_peer_ref(i); | |
559 | return 1; | |
560 | } | |
561 | ||
562 | remote = socket_peer_dup(&key); | |
563 | if (!remote) | |
564 | return log_oom(); | |
565 | ||
566 | r = set_ensure_put(&s->peers_by_address, &peer_address_hash_ops, remote); | |
567 | if (r < 0) | |
568 | return log_unit_error_errno(UNIT(s), r, "Failed to insert peer info into hash table: %m"); | |
569 | ||
570 | remote->socket = s; | |
571 | ||
572 | *ret = TAKE_PTR(remote); | |
573 | return 1; | |
574 | } | |
575 | ||
576 | static const char* listen_lookup(int family, int type) { | |
577 | ||
578 | if (family == AF_NETLINK) | |
579 | return "ListenNetlink"; | |
580 | ||
581 | if (type == SOCK_STREAM) | |
582 | return "ListenStream"; | |
583 | else if (type == SOCK_DGRAM) | |
584 | return "ListenDatagram"; | |
585 | else if (type == SOCK_SEQPACKET) | |
586 | return "ListenSequentialPacket"; | |
587 | ||
588 | assert_not_reached(); | |
589 | } | |
590 | ||
591 | static void socket_dump(Unit *u, FILE *f, const char *prefix) { | |
592 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
593 | const char *prefix2, *str; | |
594 | ||
595 | assert(f); | |
596 | ||
597 | prefix = strempty(prefix); | |
598 | prefix2 = strjoina(prefix, "\t"); | |
599 | ||
600 | fprintf(f, | |
601 | "%sSocket State: %s\n" | |
602 | "%sResult: %s\n" | |
603 | "%sClean Result: %s\n" | |
604 | "%sBindIPv6Only: %s\n" | |
605 | "%sBacklog: %u\n" | |
606 | "%sSocketMode: %04o\n" | |
607 | "%sDirectoryMode: %04o\n" | |
608 | "%sKeepAlive: %s\n" | |
609 | "%sNoDelay: %s\n" | |
610 | "%sFreeBind: %s\n" | |
611 | "%sTransparent: %s\n" | |
612 | "%sBroadcast: %s\n" | |
613 | "%sPassCredentials: %s\n" | |
614 | "%sPassPIDFD: %s\n" | |
615 | "%sPassSecurity: %s\n" | |
616 | "%sPassPacketInfo: %s\n" | |
617 | "%sAcceptFileDescriptors: %s\n" | |
618 | "%sTCPCongestion: %s\n" | |
619 | "%sRemoveOnStop: %s\n" | |
620 | "%sWritable: %s\n" | |
621 | "%sFileDescriptorName: %s\n" | |
622 | "%sPassFileDescriptorsToExec: %s\n" | |
623 | "%sSELinuxContextFromNet: %s\n", | |
624 | prefix, socket_state_to_string(s->state), | |
625 | prefix, socket_result_to_string(s->result), | |
626 | prefix, socket_result_to_string(s->clean_result), | |
627 | prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only), | |
628 | prefix, s->backlog, | |
629 | prefix, s->socket_mode, | |
630 | prefix, s->directory_mode, | |
631 | prefix, yes_no(s->keep_alive), | |
632 | prefix, yes_no(s->no_delay), | |
633 | prefix, yes_no(s->free_bind), | |
634 | prefix, yes_no(s->transparent), | |
635 | prefix, yes_no(s->broadcast), | |
636 | prefix, yes_no(s->pass_cred), | |
637 | prefix, yes_no(s->pass_pidfd), | |
638 | prefix, yes_no(s->pass_sec), | |
639 | prefix, yes_no(s->pass_pktinfo), | |
640 | prefix, yes_no(s->pass_rights), | |
641 | prefix, strna(s->tcp_congestion), | |
642 | prefix, yes_no(s->remove_on_stop), | |
643 | prefix, yes_no(s->writable), | |
644 | prefix, socket_fdname(s), | |
645 | prefix, yes_no(s->pass_fds_to_exec), | |
646 | prefix, yes_no(s->selinux_context_from_net)); | |
647 | ||
648 | if (s->timestamping != SOCKET_TIMESTAMPING_OFF) | |
649 | fprintf(f, | |
650 | "%sTimestamping: %s\n", | |
651 | prefix, socket_timestamping_to_string(s->timestamping)); | |
652 | ||
653 | if (pidref_is_set(&s->control_pid)) | |
654 | fprintf(f, | |
655 | "%sControl PID: "PID_FMT"\n", | |
656 | prefix, s->control_pid.pid); | |
657 | ||
658 | if (s->bind_to_device) | |
659 | fprintf(f, | |
660 | "%sBindToDevice: %s\n", | |
661 | prefix, s->bind_to_device); | |
662 | ||
663 | if (s->accept) | |
664 | fprintf(f, | |
665 | "%sAccepted: %u\n" | |
666 | "%sNConnections: %u\n" | |
667 | "%sMaxConnections: %u\n" | |
668 | "%sMaxConnectionsPerSource: %u\n", | |
669 | prefix, s->n_accepted, | |
670 | prefix, s->n_connections, | |
671 | prefix, s->max_connections, | |
672 | prefix, s->max_connections_per_source); | |
673 | else | |
674 | fprintf(f, | |
675 | "%sFlushPending: %s\n", | |
676 | prefix, yes_no(s->flush_pending)); | |
677 | ||
678 | if (s->priority >= 0) | |
679 | fprintf(f, | |
680 | "%sPriority: %i\n", | |
681 | prefix, s->priority); | |
682 | ||
683 | if (s->receive_buffer > 0) | |
684 | fprintf(f, | |
685 | "%sReceiveBuffer: %zu\n", | |
686 | prefix, s->receive_buffer); | |
687 | ||
688 | if (s->send_buffer > 0) | |
689 | fprintf(f, | |
690 | "%sSendBuffer: %zu\n", | |
691 | prefix, s->send_buffer); | |
692 | ||
693 | if (s->ip_tos >= 0) | |
694 | fprintf(f, | |
695 | "%sIPTOS: %i\n", | |
696 | prefix, s->ip_tos); | |
697 | ||
698 | if (s->ip_ttl >= 0) | |
699 | fprintf(f, | |
700 | "%sIPTTL: %i\n", | |
701 | prefix, s->ip_ttl); | |
702 | ||
703 | if (s->pipe_size > 0) | |
704 | fprintf(f, | |
705 | "%sPipeSize: %zu\n", | |
706 | prefix, s->pipe_size); | |
707 | ||
708 | if (s->mark >= 0) | |
709 | fprintf(f, | |
710 | "%sMark: %i\n", | |
711 | prefix, s->mark); | |
712 | ||
713 | if (s->mq_maxmsg > 0) | |
714 | fprintf(f, | |
715 | "%sMessageQueueMaxMessages: %li\n", | |
716 | prefix, s->mq_maxmsg); | |
717 | ||
718 | if (s->mq_msgsize > 0) | |
719 | fprintf(f, | |
720 | "%sMessageQueueMessageSize: %li\n", | |
721 | prefix, s->mq_msgsize); | |
722 | ||
723 | if (s->reuse_port) | |
724 | fprintf(f, | |
725 | "%sReusePort: %s\n", | |
726 | prefix, yes_no(s->reuse_port)); | |
727 | ||
728 | if (s->smack) | |
729 | fprintf(f, | |
730 | "%sSmackLabel: %s\n", | |
731 | prefix, s->smack); | |
732 | ||
733 | if (s->smack_ip_in) | |
734 | fprintf(f, | |
735 | "%sSmackLabelIPIn: %s\n", | |
736 | prefix, s->smack_ip_in); | |
737 | ||
738 | if (s->smack_ip_out) | |
739 | fprintf(f, | |
740 | "%sSmackLabelIPOut: %s\n", | |
741 | prefix, s->smack_ip_out); | |
742 | ||
743 | if (!isempty(s->user) || !isempty(s->group)) | |
744 | fprintf(f, | |
745 | "%sSocketUser: %s\n" | |
746 | "%sSocketGroup: %s\n", | |
747 | prefix, strna(s->user), | |
748 | prefix, strna(s->group)); | |
749 | ||
750 | if (timestamp_is_set(s->keep_alive_time)) | |
751 | fprintf(f, | |
752 | "%sKeepAliveTimeSec: %s\n", | |
753 | prefix, FORMAT_TIMESPAN(s->keep_alive_time, USEC_PER_SEC)); | |
754 | ||
755 | if (s->keep_alive_interval > 0) | |
756 | fprintf(f, | |
757 | "%sKeepAliveIntervalSec: %s\n", | |
758 | prefix, FORMAT_TIMESPAN(s->keep_alive_interval, USEC_PER_SEC)); | |
759 | ||
760 | if (s->keep_alive_cnt > 0) | |
761 | fprintf(f, | |
762 | "%sKeepAliveProbes: %u\n", | |
763 | prefix, s->keep_alive_cnt); | |
764 | ||
765 | if (s->defer_accept > 0) | |
766 | fprintf(f, | |
767 | "%sDeferAcceptSec: %s\n", | |
768 | prefix, FORMAT_TIMESPAN(s->defer_accept, USEC_PER_SEC)); | |
769 | ||
770 | LIST_FOREACH(port, p, s->ports) { | |
771 | ||
772 | switch (p->type) { | |
773 | case SOCKET_SOCKET: { | |
774 | _cleanup_free_ char *k = NULL; | |
775 | int r; | |
776 | ||
777 | r = socket_address_print(&p->address, &k); | |
778 | if (r < 0) { | |
779 | errno = -r; | |
780 | fprintf(f, "%s%s: %m\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type)); | |
781 | } else | |
782 | fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), k); | |
783 | break; | |
784 | } | |
785 | case SOCKET_SPECIAL: | |
786 | fprintf(f, "%sListenSpecial: %s\n", prefix, p->path); | |
787 | break; | |
788 | case SOCKET_USB_FUNCTION: | |
789 | fprintf(f, "%sListenUSBFunction: %s\n", prefix, p->path); | |
790 | break; | |
791 | case SOCKET_MQUEUE: | |
792 | fprintf(f, "%sListenMessageQueue: %s\n", prefix, p->path); | |
793 | break; | |
794 | default: | |
795 | fprintf(f, "%sListenFIFO: %s\n", prefix, p->path); | |
796 | } | |
797 | } | |
798 | ||
799 | fprintf(f, | |
800 | "%sTriggerLimitIntervalSec: %s\n" | |
801 | "%sTriggerLimitBurst: %u\n" | |
802 | "%sPollLimitIntervalSec: %s\n" | |
803 | "%sPollLimitBurst: %u\n", | |
804 | prefix, FORMAT_TIMESPAN(s->trigger_limit.interval, USEC_PER_SEC), | |
805 | prefix, s->trigger_limit.burst, | |
806 | prefix, FORMAT_TIMESPAN(s->poll_limit.interval, USEC_PER_SEC), | |
807 | prefix, s->poll_limit.burst); | |
808 | ||
809 | str = ip_protocol_to_name(s->socket_protocol); | |
810 | if (str) | |
811 | fprintf(f, "%sSocketProtocol: %s\n", prefix, str); | |
812 | ||
813 | if (!strv_isempty(s->symlinks)) { | |
814 | fprintf(f, "%sSymlinks:", prefix); | |
815 | STRV_FOREACH(q, s->symlinks) | |
816 | fprintf(f, " %s", *q); | |
817 | ||
818 | fprintf(f, "\n"); | |
819 | } | |
820 | ||
821 | fprintf(f, | |
822 | "%sTimeoutSec: %s\n", | |
823 | prefix, FORMAT_TIMESPAN(s->timeout_usec, USEC_PER_SEC)); | |
824 | ||
825 | exec_context_dump(&s->exec_context, f, prefix); | |
826 | kill_context_dump(&s->kill_context, f, prefix); | |
827 | ||
828 | for (SocketExecCommand c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) { | |
829 | if (!s->exec_command[c]) | |
830 | continue; | |
831 | ||
832 | fprintf(f, "%s%s %s:\n", | |
833 | prefix, glyph(GLYPH_ARROW_RIGHT), socket_exec_command_to_string(c)); | |
834 | ||
835 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
836 | } | |
837 | ||
838 | cgroup_context_dump(UNIT(s), f, prefix); | |
839 | } | |
840 | ||
841 | static int instance_from_socket( | |
842 | int fd, | |
843 | unsigned nr, | |
844 | char **ret) { | |
845 | ||
846 | union sockaddr_union local, remote; | |
847 | socklen_t l; | |
848 | int r; | |
849 | ||
850 | assert(fd >= 0); | |
851 | assert(ret); | |
852 | ||
853 | l = sizeof(local); | |
854 | if (getsockname(fd, &local.sa, &l) < 0) | |
855 | return -errno; | |
856 | ||
857 | l = sizeof(remote); | |
858 | if (getpeername(fd, &remote.sa, &l) < 0) | |
859 | return -errno; | |
860 | ||
861 | uint64_t cookie; | |
862 | r = socket_get_cookie(fd, &cookie); | |
863 | if (r < 0) | |
864 | return r; | |
865 | ||
866 | char *s; | |
867 | ||
868 | switch (local.sa.sa_family) { | |
869 | ||
870 | case AF_INET: { | |
871 | uint32_t | |
872 | a = be32toh(local.in.sin_addr.s_addr), | |
873 | b = be32toh(remote.in.sin_addr.s_addr); | |
874 | ||
875 | if (asprintf(&s, | |
876 | "%u-%" PRIu64 "-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", | |
877 | nr, | |
878 | cookie, | |
879 | a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF, | |
880 | be16toh(local.in.sin_port), | |
881 | b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF, | |
882 | be16toh(remote.in.sin_port)) < 0) | |
883 | return -ENOMEM; | |
884 | ||
885 | break; | |
886 | } | |
887 | ||
888 | case AF_INET6: { | |
889 | static const unsigned char ipv4_prefix[] = { | |
890 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF | |
891 | }; | |
892 | ||
893 | if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 && | |
894 | memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) { | |
895 | const uint8_t | |
896 | *a = local.in6.sin6_addr.s6_addr+12, | |
897 | *b = remote.in6.sin6_addr.s6_addr+12; | |
898 | ||
899 | if (asprintf(&s, | |
900 | "%u-%" PRIu64 "-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u", | |
901 | nr, | |
902 | cookie, | |
903 | a[0], a[1], a[2], a[3], | |
904 | be16toh(local.in6.sin6_port), | |
905 | b[0], b[1], b[2], b[3], | |
906 | be16toh(remote.in6.sin6_port)) < 0) | |
907 | return -ENOMEM; | |
908 | } else { | |
909 | if (asprintf(&s, | |
910 | "%u-%" PRIu64 "-%s:%u-%s:%u", | |
911 | nr, | |
912 | cookie, | |
913 | IN6_ADDR_TO_STRING(&local.in6.sin6_addr), | |
914 | be16toh(local.in6.sin6_port), | |
915 | IN6_ADDR_TO_STRING(&remote.in6.sin6_addr), | |
916 | be16toh(remote.in6.sin6_port)) < 0) | |
917 | return -ENOMEM; | |
918 | } | |
919 | ||
920 | break; | |
921 | } | |
922 | ||
923 | case AF_UNIX: { | |
924 | struct ucred ucred; | |
925 | ||
926 | r = getpeercred(fd, &ucred); | |
927 | if (r >= 0) { | |
928 | _cleanup_close_ int pidfd = getpeerpidfd(fd); | |
929 | uint64_t pidfd_id; | |
930 | ||
931 | if (pidfd >= 0 && pidfd_get_inode_id(pidfd, &pidfd_id) >= 0) | |
932 | r = asprintf(&s, "%u-%" PRIu64 "-" PID_FMT "_%" PRIu64 "-" UID_FMT, | |
933 | nr, cookie, ucred.pid, pidfd_id, ucred.uid); | |
934 | else | |
935 | r = asprintf(&s, "%u-%" PRIu64 "-" PID_FMT "-" UID_FMT, | |
936 | nr, cookie, ucred.pid, ucred.uid); | |
937 | if (r < 0) | |
938 | return -ENOMEM; | |
939 | } else if (r == -ENODATA) { | |
940 | /* This handles the case where somebody is connecting from another pid/uid namespace | |
941 | * (e.g. from outside of our container). */ | |
942 | if (asprintf(&s, | |
943 | "%u-%" PRIu64 "-unknown", | |
944 | nr, | |
945 | cookie) < 0) | |
946 | return -ENOMEM; | |
947 | } else | |
948 | return r; | |
949 | ||
950 | break; | |
951 | } | |
952 | ||
953 | case AF_VSOCK: | |
954 | if (asprintf(&s, | |
955 | "%u-%" PRIu64 "-%u:%u-%u:%u", | |
956 | nr, | |
957 | cookie, | |
958 | local.vm.svm_cid, local.vm.svm_port, | |
959 | remote.vm.svm_cid, remote.vm.svm_port) < 0) | |
960 | return -ENOMEM; | |
961 | ||
962 | break; | |
963 | ||
964 | default: | |
965 | assert_not_reached(); | |
966 | } | |
967 | ||
968 | *ret = s; | |
969 | return 0; | |
970 | } | |
971 | ||
972 | static void socket_close_fds(Socket *s) { | |
973 | assert(s); | |
974 | ||
975 | LIST_FOREACH(port, p, s->ports) { | |
976 | bool was_open = p->fd >= 0; | |
977 | ||
978 | p->event_source = sd_event_source_disable_unref(p->event_source); | |
979 | p->fd = safe_close(p->fd); | |
980 | socket_port_close_auxiliary_fds(p); | |
981 | ||
982 | /* One little note: we should normally not delete any sockets in the file system here! After all some | |
983 | * other process we spawned might still have a reference of this fd and wants to continue to use | |
984 | * it. Therefore we normally delete sockets in the file system before we create a new one, not after we | |
985 | * stopped using one! That all said, if the user explicitly requested this, we'll delete them here | |
986 | * anyway, but only then. */ | |
987 | ||
988 | if (!was_open || !s->remove_on_stop) | |
989 | continue; | |
990 | ||
991 | switch (p->type) { | |
992 | ||
993 | case SOCKET_FIFO: | |
994 | (void) unlink(p->path); | |
995 | break; | |
996 | ||
997 | case SOCKET_MQUEUE: | |
998 | (void) mq_unlink(p->path); | |
999 | break; | |
1000 | ||
1001 | case SOCKET_SOCKET: | |
1002 | (void) socket_address_unlink(&p->address); | |
1003 | break; | |
1004 | ||
1005 | default: | |
1006 | ; | |
1007 | } | |
1008 | } | |
1009 | ||
1010 | if (s->remove_on_stop) | |
1011 | STRV_FOREACH(i, s->symlinks) | |
1012 | (void) unlink(*i); | |
1013 | ||
1014 | /* Note that we don't return NULL here, since s has not been freed. */ | |
1015 | } | |
1016 | ||
1017 | DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(Socket*, socket_close_fds, NULL); | |
1018 | ||
1019 | #define SOCKET_OPTION_WARNING_FORMAT_STR "Failed to set %s socket option, ignoring: %m" | |
1020 | #define log_socket_option_warning_errno(s, error, option) \ | |
1021 | log_unit_warning_errno(UNIT(s), (error), SOCKET_OPTION_WARNING_FORMAT_STR, STRINGIFY(option)) | |
1022 | ||
1023 | static void socket_apply_socket_options(Socket *s, SocketPort *p, int fd) { | |
1024 | int r; | |
1025 | ||
1026 | assert(s); | |
1027 | assert(p); | |
1028 | assert(fd >= 0); | |
1029 | ||
1030 | if (s->keep_alive) { | |
1031 | r = setsockopt_int(fd, SOL_SOCKET, SO_KEEPALIVE, true); | |
1032 | if (r < 0) | |
1033 | log_socket_option_warning_errno(s, r, SO_KEEPALIVE); | |
1034 | } | |
1035 | ||
1036 | if (timestamp_is_set(s->keep_alive_time)) { | |
1037 | r = setsockopt_int(fd, SOL_TCP, TCP_KEEPIDLE, s->keep_alive_time / USEC_PER_SEC); | |
1038 | if (r < 0) | |
1039 | log_socket_option_warning_errno(s, r, TCP_KEEPIDLE); | |
1040 | } | |
1041 | ||
1042 | if (s->keep_alive_interval > 0) { | |
1043 | r = setsockopt_int(fd, SOL_TCP, TCP_KEEPINTVL, s->keep_alive_interval / USEC_PER_SEC); | |
1044 | if (r < 0) | |
1045 | log_socket_option_warning_errno(s, r, TCP_KEEPINTVL); | |
1046 | } | |
1047 | ||
1048 | if (s->keep_alive_cnt > 0) { | |
1049 | r = setsockopt_int(fd, SOL_TCP, TCP_KEEPCNT, s->keep_alive_cnt); | |
1050 | if (r < 0) | |
1051 | log_socket_option_warning_errno(s, r, TCP_KEEPCNT); | |
1052 | } | |
1053 | ||
1054 | if (s->defer_accept > 0) { | |
1055 | r = setsockopt_int(fd, SOL_TCP, TCP_DEFER_ACCEPT, s->defer_accept / USEC_PER_SEC); | |
1056 | if (r < 0) | |
1057 | log_socket_option_warning_errno(s, r, TCP_DEFER_ACCEPT); | |
1058 | } | |
1059 | ||
1060 | if (s->no_delay) { | |
1061 | if (s->socket_protocol == IPPROTO_SCTP) { | |
1062 | r = setsockopt_int(fd, SOL_SCTP, SCTP_NODELAY, true); | |
1063 | if (r < 0) | |
1064 | log_socket_option_warning_errno(s, r, SCTP_NODELAY); | |
1065 | } else { | |
1066 | r = setsockopt_int(fd, SOL_TCP, TCP_NODELAY, true); | |
1067 | if (r < 0) | |
1068 | log_socket_option_warning_errno(s, r, TCP_NODELAY); | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | if (s->broadcast) { | |
1073 | r = setsockopt_int(fd, SOL_SOCKET, SO_BROADCAST, true); | |
1074 | if (r < 0) | |
1075 | log_socket_option_warning_errno(s, r, SO_BROADCAST); | |
1076 | } | |
1077 | ||
1078 | if (s->pass_cred) { | |
1079 | r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true); | |
1080 | if (r < 0) | |
1081 | log_socket_option_warning_errno(s, r, SO_PASSCRED); | |
1082 | } | |
1083 | ||
1084 | if (s->pass_pidfd) { | |
1085 | r = setsockopt_int(fd, SOL_SOCKET, SO_PASSPIDFD, true); | |
1086 | if (r < 0) | |
1087 | log_unit_full_errno(UNIT(s), ERRNO_IS_NEG_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r, | |
1088 | SOCKET_OPTION_WARNING_FORMAT_STR, "SO_PASSPIDFD"); | |
1089 | } | |
1090 | ||
1091 | if (s->pass_sec) { | |
1092 | r = setsockopt_int(fd, SOL_SOCKET, SO_PASSSEC, true); | |
1093 | if (r < 0) | |
1094 | log_unit_full_errno(UNIT(s), ERRNO_IS_NEG_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r, | |
1095 | SOCKET_OPTION_WARNING_FORMAT_STR, "SO_PASSSEC"); | |
1096 | } | |
1097 | ||
1098 | if (s->pass_pktinfo) { | |
1099 | r = socket_set_recvpktinfo(fd, socket_address_family(&p->address), true); | |
1100 | if (r < 0) | |
1101 | log_unit_warning_errno(UNIT(s), r, SOCKET_OPTION_WARNING_FORMAT_STR, "packet info"); | |
1102 | } | |
1103 | ||
1104 | if (!s->pass_rights) { | |
1105 | r = setsockopt_int(fd, SOL_SOCKET, SO_PASSRIGHTS, false); | |
1106 | if (r < 0) | |
1107 | log_unit_full_errno(UNIT(s), ERRNO_IS_NEG_NOT_SUPPORTED(r) ? LOG_DEBUG : LOG_WARNING, r, | |
1108 | SOCKET_OPTION_WARNING_FORMAT_STR, "SO_PASSRIGHTS"); | |
1109 | } | |
1110 | ||
1111 | if (s->timestamping != SOCKET_TIMESTAMPING_OFF) { | |
1112 | r = setsockopt_int(fd, SOL_SOCKET, | |
1113 | s->timestamping == SOCKET_TIMESTAMPING_NS ? SO_TIMESTAMPNS : SO_TIMESTAMP, | |
1114 | true); | |
1115 | if (r < 0) | |
1116 | log_unit_warning_errno(UNIT(s), r, SOCKET_OPTION_WARNING_FORMAT_STR, "timestamping"); | |
1117 | } | |
1118 | ||
1119 | if (s->priority >= 0) { | |
1120 | r = setsockopt_int(fd, SOL_SOCKET, SO_PRIORITY, s->priority); | |
1121 | if (r < 0) | |
1122 | log_socket_option_warning_errno(s, r, SO_PRIORITY); | |
1123 | } | |
1124 | ||
1125 | if (s->receive_buffer > 0) { | |
1126 | r = fd_set_rcvbuf(fd, s->receive_buffer, false); | |
1127 | if (r < 0) | |
1128 | log_unit_full_errno(UNIT(s), ERRNO_IS_NEG_PRIVILEGE(r) ? LOG_DEBUG : LOG_WARNING, r, | |
1129 | SOCKET_OPTION_WARNING_FORMAT_STR, "SO_RCVBUF/SO_RCVBUFFORCE"); | |
1130 | } | |
1131 | ||
1132 | if (s->send_buffer > 0) { | |
1133 | r = fd_set_sndbuf(fd, s->send_buffer, false); | |
1134 | if (r < 0) | |
1135 | log_unit_full_errno(UNIT(s), ERRNO_IS_NEG_PRIVILEGE(r) ? LOG_DEBUG : LOG_WARNING, r, | |
1136 | SOCKET_OPTION_WARNING_FORMAT_STR, "SO_SNDBUF/SO_SNDBUFFORCE"); | |
1137 | } | |
1138 | ||
1139 | if (s->mark >= 0) { | |
1140 | r = setsockopt_int(fd, SOL_SOCKET, SO_MARK, s->mark); | |
1141 | if (r < 0) | |
1142 | log_socket_option_warning_errno(s, r, SO_MARK); | |
1143 | } | |
1144 | ||
1145 | if (s->ip_tos >= 0) { | |
1146 | r = setsockopt_int(fd, IPPROTO_IP, IP_TOS, s->ip_tos); | |
1147 | if (r < 0) | |
1148 | log_socket_option_warning_errno(s, r, IP_TOS); | |
1149 | } | |
1150 | ||
1151 | if (s->ip_ttl >= 0) { | |
1152 | r = socket_set_ttl(fd, socket_address_family(&p->address), s->ip_ttl); | |
1153 | if (r < 0) | |
1154 | log_unit_warning_errno(UNIT(s), r, SOCKET_OPTION_WARNING_FORMAT_STR, "IP_TTL/IPV6_UNICAST_HOPS"); | |
1155 | } | |
1156 | ||
1157 | if (s->tcp_congestion) | |
1158 | if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0) | |
1159 | log_socket_option_warning_errno(s, errno, TCP_CONGESTION); | |
1160 | ||
1161 | if (s->smack_ip_in) { | |
1162 | r = mac_smack_apply_fd(fd, SMACK_ATTR_IPIN, s->smack_ip_in); | |
1163 | if (r < 0) | |
1164 | log_unit_error_errno(UNIT(s), r, "Failed to apply SMACK label for IP input, ignoring: %m"); | |
1165 | } | |
1166 | ||
1167 | if (s->smack_ip_out) { | |
1168 | r = mac_smack_apply_fd(fd, SMACK_ATTR_IPOUT, s->smack_ip_out); | |
1169 | if (r < 0) | |
1170 | log_unit_error_errno(UNIT(s), r, "Failed to apply SMACK label for IP output, ignoring: %m"); | |
1171 | } | |
1172 | } | |
1173 | ||
1174 | static void socket_apply_fifo_options(Socket *s, int fd) { | |
1175 | int r; | |
1176 | ||
1177 | assert(s); | |
1178 | assert(fd >= 0); | |
1179 | ||
1180 | if (s->pipe_size > 0) | |
1181 | if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0) | |
1182 | log_unit_warning_errno(UNIT(s), errno, "Setting pipe size failed, ignoring: %m"); | |
1183 | ||
1184 | if (s->smack) { | |
1185 | r = mac_smack_apply_fd(fd, SMACK_ATTR_ACCESS, s->smack); | |
1186 | if (r < 0) | |
1187 | log_unit_error_errno(UNIT(s), r, "SMACK relabelling failed, ignoring: %m"); | |
1188 | } | |
1189 | } | |
1190 | ||
1191 | static int fifo_address_create( | |
1192 | const char *path, | |
1193 | mode_t directory_mode, | |
1194 | mode_t socket_mode) { | |
1195 | ||
1196 | _cleanup_close_ int fd = -EBADF; | |
1197 | mode_t old_mask; | |
1198 | struct stat st; | |
1199 | int r; | |
1200 | ||
1201 | assert(path); | |
1202 | ||
1203 | (void) mkdir_parents_label(path, directory_mode); | |
1204 | ||
1205 | r = mac_selinux_create_file_prepare(path, S_IFIFO); | |
1206 | if (r < 0) | |
1207 | return r; | |
1208 | ||
1209 | /* Enforce the right access mode for the fifo */ | |
1210 | old_mask = umask(~socket_mode); | |
1211 | ||
1212 | /* Include the original umask in our mask */ | |
1213 | (void) umask(~socket_mode | old_mask); | |
1214 | ||
1215 | r = mkfifo(path, socket_mode); | |
1216 | (void) umask(old_mask); | |
1217 | ||
1218 | if (r < 0 && errno != EEXIST) { | |
1219 | r = -errno; | |
1220 | goto fail; | |
1221 | } | |
1222 | ||
1223 | fd = open(path, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW); | |
1224 | if (fd < 0) { | |
1225 | r = -errno; | |
1226 | goto fail; | |
1227 | } | |
1228 | ||
1229 | mac_selinux_create_file_clear(); | |
1230 | ||
1231 | if (fstat(fd, &st) < 0) { | |
1232 | r = -errno; | |
1233 | goto fail; | |
1234 | } | |
1235 | ||
1236 | if (!S_ISFIFO(st.st_mode) || | |
1237 | (st.st_mode & 0777) != (socket_mode & ~old_mask) || | |
1238 | st.st_uid != getuid() || | |
1239 | st.st_gid != getgid()) { | |
1240 | r = -EEXIST; | |
1241 | goto fail; | |
1242 | } | |
1243 | ||
1244 | return TAKE_FD(fd); | |
1245 | ||
1246 | fail: | |
1247 | mac_selinux_create_file_clear(); | |
1248 | return r; | |
1249 | } | |
1250 | ||
1251 | static int special_address_create(const char *path, bool writable) { | |
1252 | _cleanup_close_ int fd = -EBADF; | |
1253 | struct stat st; | |
1254 | ||
1255 | assert(path); | |
1256 | ||
1257 | fd = open(path, (writable ? O_RDWR : O_RDONLY)|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW); | |
1258 | if (fd < 0) | |
1259 | return -errno; | |
1260 | ||
1261 | if (fstat(fd, &st) < 0) | |
1262 | return -errno; | |
1263 | ||
1264 | /* Check whether this is a /proc, /sys or /dev file or char device */ | |
1265 | if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) | |
1266 | return -EEXIST; | |
1267 | ||
1268 | return TAKE_FD(fd); | |
1269 | } | |
1270 | ||
1271 | static int usbffs_address_create_at(int dfd, const char *name) { | |
1272 | _cleanup_close_ int fd = -EBADF; | |
1273 | struct stat st; | |
1274 | ||
1275 | assert(dfd >= 0); | |
1276 | assert(name); | |
1277 | ||
1278 | fd = openat(dfd, name, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW); | |
1279 | if (fd < 0) | |
1280 | return -errno; | |
1281 | ||
1282 | if (fstat(fd, &st) < 0) | |
1283 | return -errno; | |
1284 | ||
1285 | /* Check whether this is a regular file (ffs endpoint) */ | |
1286 | if (!S_ISREG(st.st_mode)) | |
1287 | return -EEXIST; | |
1288 | ||
1289 | return TAKE_FD(fd); | |
1290 | } | |
1291 | ||
1292 | static int mq_address_create( | |
1293 | const char *path, | |
1294 | mode_t mq_mode, | |
1295 | long maxmsg, | |
1296 | long msgsize) { | |
1297 | ||
1298 | _cleanup_close_ int fd = -EBADF; | |
1299 | struct stat st; | |
1300 | mode_t old_mask; | |
1301 | struct mq_attr _attr, *attr = NULL; | |
1302 | ||
1303 | assert(path); | |
1304 | ||
1305 | if (maxmsg > 0 && msgsize > 0) { | |
1306 | _attr = (struct mq_attr) { | |
1307 | .mq_flags = O_NONBLOCK, | |
1308 | .mq_maxmsg = maxmsg, | |
1309 | .mq_msgsize = msgsize, | |
1310 | }; | |
1311 | attr = &_attr; | |
1312 | } | |
1313 | ||
1314 | /* Enforce the right access mode for the mq */ | |
1315 | old_mask = umask(~mq_mode); | |
1316 | ||
1317 | /* Include the original umask in our mask */ | |
1318 | (void) umask(~mq_mode | old_mask); | |
1319 | fd = mq_open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_CREAT, mq_mode, attr); | |
1320 | (void) umask(old_mask); | |
1321 | ||
1322 | if (fd < 0) | |
1323 | return -errno; | |
1324 | ||
1325 | if (fstat(fd, &st) < 0) | |
1326 | return -errno; | |
1327 | ||
1328 | if ((st.st_mode & 0777) != (mq_mode & ~old_mask) || | |
1329 | st.st_uid != getuid() || | |
1330 | st.st_gid != getgid()) | |
1331 | return -EEXIST; | |
1332 | ||
1333 | return TAKE_FD(fd); | |
1334 | } | |
1335 | ||
1336 | static int socket_symlink(Socket *s) { | |
1337 | const char *p; | |
1338 | int r; | |
1339 | ||
1340 | assert(s); | |
1341 | ||
1342 | p = socket_find_symlink_target(s); | |
1343 | if (!p) | |
1344 | return 0; | |
1345 | ||
1346 | STRV_FOREACH(i, s->symlinks) { | |
1347 | (void) mkdir_parents_label(*i, s->directory_mode); | |
1348 | ||
1349 | r = symlink_idempotent(p, *i, false); | |
1350 | if (r == -EEXIST && s->remove_on_stop) { | |
1351 | /* If there's already something where we want to create the symlink, and the destructive | |
1352 | * RemoveOnStop= mode is set, then we might as well try to remove what already exists and try | |
1353 | * again. */ | |
1354 | ||
1355 | if (unlink(*i) >= 0) | |
1356 | r = symlink_idempotent(p, *i, false); | |
1357 | } | |
1358 | if (r < 0) | |
1359 | log_unit_warning_errno(UNIT(s), r, "Failed to create symlink %s %s %s, ignoring: %m", | |
1360 | p, glyph(GLYPH_ARROW_RIGHT), *i); | |
1361 | } | |
1362 | ||
1363 | return 0; | |
1364 | } | |
1365 | ||
1366 | static int usbffs_write_descs(int fd, Service *s) { | |
1367 | int r; | |
1368 | ||
1369 | assert(fd >= 0); | |
1370 | assert(s); | |
1371 | ||
1372 | if (!s->usb_function_descriptors || !s->usb_function_strings) | |
1373 | return -EINVAL; | |
1374 | ||
1375 | r = copy_file_fd(s->usb_function_descriptors, fd, 0); | |
1376 | if (r < 0) | |
1377 | return r; | |
1378 | ||
1379 | return copy_file_fd(s->usb_function_strings, fd, 0); | |
1380 | } | |
1381 | ||
1382 | static int usbffs_dispatch_eps(SocketPort *p, int dfd) { | |
1383 | _cleanup_free_ DirectoryEntries *des = NULL; | |
1384 | int r; | |
1385 | ||
1386 | assert(p); | |
1387 | assert(dfd >= 0); | |
1388 | ||
1389 | r = readdir_all(dfd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &des); | |
1390 | if (r < 0) | |
1391 | return r; | |
1392 | ||
1393 | p->auxiliary_fds = new(int, des->n_entries); | |
1394 | if (!p->auxiliary_fds) | |
1395 | return -ENOMEM; | |
1396 | ||
1397 | FOREACH_ARRAY(i, des->entries, des->n_entries) { | |
1398 | const struct dirent *de = *i; | |
1399 | ||
1400 | if (streq(de->d_name, "ep0")) | |
1401 | continue; | |
1402 | ||
1403 | r = usbffs_address_create_at(dfd, de->d_name); | |
1404 | if (r < 0) | |
1405 | goto fail; | |
1406 | ||
1407 | p->auxiliary_fds[p->n_auxiliary_fds++] = r; | |
1408 | } | |
1409 | ||
1410 | assert(p->n_auxiliary_fds < des->n_entries); | |
1411 | ||
1412 | return 0; | |
1413 | ||
1414 | fail: | |
1415 | socket_port_close_auxiliary_fds(p); | |
1416 | return r; | |
1417 | } | |
1418 | ||
1419 | int socket_load_service_unit(Socket *s, int cfd, Unit **ret) { | |
1420 | int r; | |
1421 | ||
1422 | /* Figure out what the unit that will be used to handle the connections on the socket looks like. | |
1423 | * | |
1424 | * If cfd < 0, then we don't have a connection yet. In case of Accept=yes sockets, use a fake | |
1425 | * instance name. | |
1426 | */ | |
1427 | ||
1428 | assert(s); | |
1429 | assert(ret); | |
1430 | ||
1431 | if (UNIT_ISSET(s->service)) { | |
1432 | *ret = UNIT_DEREF(s->service); | |
1433 | return 0; | |
1434 | } | |
1435 | ||
1436 | if (!s->accept) | |
1437 | return -ENODATA; | |
1438 | ||
1439 | /* Build the instance name and load the unit */ | |
1440 | _cleanup_free_ char *prefix = NULL, *instance = NULL, *name = NULL; | |
1441 | ||
1442 | r = unit_name_to_prefix(UNIT(s)->id, &prefix); | |
1443 | if (r < 0) | |
1444 | return r; | |
1445 | ||
1446 | if (cfd >= 0) { | |
1447 | r = instance_from_socket(cfd, s->n_accepted, &instance); | |
1448 | if (ERRNO_IS_NEG_DISCONNECT(r)) | |
1449 | /* ENOTCONN is legitimate if TCP RST was received. Other socket families might return | |
1450 | * different errors. This connection is over, but the socket unit lives on. */ | |
1451 | return log_unit_debug_errno(UNIT(s), r, | |
1452 | "Got %s on incoming socket, assuming aborted connection attempt, ignoring.", | |
1453 | errno_to_name(r)); | |
1454 | if (r < 0) | |
1455 | return r; | |
1456 | } | |
1457 | ||
1458 | /* For accepting sockets, we don't know how the instance will be called until we get a connection and | |
1459 | * can figure out what the peer name is. So let's use "internal" as the instance to make it clear | |
1460 | * that this is not an actual peer name. We use "unknown" when we cannot figure out the peer. */ | |
1461 | r = unit_name_build(prefix, instance ?: "internal", ".service", &name); | |
1462 | if (r < 0) | |
1463 | return r; | |
1464 | ||
1465 | return manager_load_unit(UNIT(s)->manager, name, NULL, NULL, ret); | |
1466 | } | |
1467 | ||
1468 | static int socket_determine_selinux_label(Socket *s, char **ret) { | |
1469 | Unit *service; | |
1470 | int r; | |
1471 | ||
1472 | assert(s); | |
1473 | assert(ret); | |
1474 | ||
1475 | r = socket_load_service_unit(s, /* cfd= */ -EBADF, &service); | |
1476 | if (r == -ENODATA) { | |
1477 | *ret = NULL; | |
1478 | return 0; | |
1479 | } | |
1480 | if (r < 0) | |
1481 | return r; | |
1482 | ||
1483 | r = service_determine_exec_selinux_label(SERVICE(service), ret); | |
1484 | if (r == -ENODATA) { | |
1485 | *ret = NULL; | |
1486 | return 0; | |
1487 | } | |
1488 | return r; | |
1489 | } | |
1490 | ||
1491 | static int socket_address_listen_do( | |
1492 | Socket *s, | |
1493 | const SocketAddress *address, | |
1494 | const char *label) { | |
1495 | ||
1496 | assert(s); | |
1497 | assert(address); | |
1498 | ||
1499 | return socket_address_listen( | |
1500 | address, | |
1501 | SOCK_CLOEXEC|SOCK_NONBLOCK, | |
1502 | s->backlog, | |
1503 | s->bind_ipv6_only, | |
1504 | s->bind_to_device, | |
1505 | s->reuse_port, | |
1506 | s->free_bind, | |
1507 | s->transparent, | |
1508 | s->directory_mode, | |
1509 | s->socket_mode, | |
1510 | label); | |
1511 | } | |
1512 | ||
1513 | #define log_address_error_errno(u, address, error, fmt) \ | |
1514 | ({ \ | |
1515 | _cleanup_free_ char *_t = NULL; \ | |
1516 | \ | |
1517 | (void) socket_address_print(address, &_t); \ | |
1518 | log_unit_error_errno(u, error, fmt, strna(_t)); \ | |
1519 | }) | |
1520 | ||
1521 | static bool fork_needed(const SocketAddress *address, Socket *s) { | |
1522 | assert(address); | |
1523 | assert(s); | |
1524 | ||
1525 | /* Check if we need to do the cgroup or netns stuff. If not we can do things much simpler. */ | |
1526 | ||
1527 | /* If there are any NFTSet= directives with cgroup source, we need the cgroup */ | |
1528 | Unit *u = UNIT(s); | |
1529 | CGroupContext *c = unit_get_cgroup_context(u); | |
1530 | if (c) | |
1531 | FOREACH_ARRAY(nft_set, c->nft_set_context.sets, c->nft_set_context.n_sets) | |
1532 | if (nft_set->source == NFT_SET_SOURCE_CGROUP) | |
1533 | return true; | |
1534 | ||
1535 | if (IN_SET(address->sockaddr.sa.sa_family, AF_INET, AF_INET6) && | |
1536 | bpf_program_supported() > 0) /* If BPF firewalling isn't supported anyway — there's no point in this forking complexity */ | |
1537 | return true; | |
1538 | ||
1539 | return exec_needs_network_namespace(&s->exec_context); | |
1540 | } | |
1541 | ||
1542 | static int socket_address_listen_in_cgroup( | |
1543 | Socket *s, | |
1544 | const SocketAddress *address, | |
1545 | const char *label) { | |
1546 | ||
1547 | _cleanup_(pidref_done) PidRef pid = PIDREF_NULL; | |
1548 | _cleanup_close_pair_ int pair[2] = EBADF_PAIR; | |
1549 | int fd, r; | |
1550 | ||
1551 | assert(s); | |
1552 | assert(address); | |
1553 | ||
1554 | /* This is a wrapper around socket_address_listen(), that forks off a helper process inside the | |
1555 | * socket's cgroup and network namespace in which the socket is actually created. This way we ensure | |
1556 | * the socket is actually properly attached to the unit's cgroup for the purpose of BPF filtering and | |
1557 | * such. */ | |
1558 | ||
1559 | if (!fork_needed(address, s)) { | |
1560 | /* Shortcut things... */ | |
1561 | fd = socket_address_listen_do(s, address, label); | |
1562 | if (fd < 0) | |
1563 | return log_address_error_errno(UNIT(s), address, fd, "Failed to create listening socket (%s): %m"); | |
1564 | ||
1565 | return fd; | |
1566 | } | |
1567 | ||
1568 | r = unit_setup_exec_runtime(UNIT(s)); | |
1569 | if (r < 0) | |
1570 | return log_unit_error_errno(UNIT(s), r, "Failed acquire runtime: %m"); | |
1571 | ||
1572 | if (s->exec_context.network_namespace_path && | |
1573 | s->exec_runtime && | |
1574 | s->exec_runtime->shared && | |
1575 | s->exec_runtime->shared->netns_storage_socket[0] >= 0) { | |
1576 | r = open_shareable_ns_path(s->exec_runtime->shared->netns_storage_socket, s->exec_context.network_namespace_path, CLONE_NEWNET); | |
1577 | if (r < 0) | |
1578 | return log_unit_error_errno(UNIT(s), r, "Failed to open network namespace path %s: %m", s->exec_context.network_namespace_path); | |
1579 | } | |
1580 | ||
1581 | if (s->exec_context.ipc_namespace_path && | |
1582 | s->exec_runtime && | |
1583 | s->exec_runtime->shared && | |
1584 | s->exec_runtime->shared->ipcns_storage_socket[0] >= 0) { | |
1585 | r = open_shareable_ns_path(s->exec_runtime->shared->ipcns_storage_socket, s->exec_context.ipc_namespace_path, CLONE_NEWIPC); | |
1586 | if (r < 0) | |
1587 | return log_unit_error_errno(UNIT(s), r, "Failed to open IPC namespace path %s: %m", s->exec_context.ipc_namespace_path); | |
1588 | } | |
1589 | ||
1590 | if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0) | |
1591 | return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m"); | |
1592 | ||
1593 | r = unit_fork_helper_process(UNIT(s), "(sd-listen)", /* into_cgroup= */ true, &pid); | |
1594 | if (r < 0) | |
1595 | return log_unit_error_errno(UNIT(s), r, "Failed to fork off listener stub process: %m"); | |
1596 | if (r == 0) { | |
1597 | /* Child */ | |
1598 | ||
1599 | pair[0] = safe_close(pair[0]); | |
1600 | ||
1601 | if (exec_needs_network_namespace(&s->exec_context) && | |
1602 | s->exec_runtime && | |
1603 | s->exec_runtime->shared && | |
1604 | s->exec_runtime->shared->netns_storage_socket[0] >= 0) { | |
1605 | ||
1606 | if (namespace_type_supported(NAMESPACE_NET)) { | |
1607 | r = setup_shareable_ns(s->exec_runtime->shared->netns_storage_socket, CLONE_NEWNET); | |
1608 | if (r < 0) { | |
1609 | log_unit_error_errno(UNIT(s), r, "Failed to join network namespace: %m"); | |
1610 | _exit(EXIT_NETWORK); | |
1611 | } | |
1612 | } else if (s->exec_context.network_namespace_path) { | |
1613 | log_unit_error(UNIT(s), "Network namespace path configured but network namespaces not supported."); | |
1614 | _exit(EXIT_NETWORK); | |
1615 | } else | |
1616 | log_unit_warning(UNIT(s), "PrivateNetwork=yes is configured, but the kernel does not support network namespaces, ignoring."); | |
1617 | } | |
1618 | ||
1619 | fd = socket_address_listen_do(s, address, label); | |
1620 | if (fd < 0) { | |
1621 | log_address_error_errno(UNIT(s), address, fd, "Failed to create listening socket (%s): %m"); | |
1622 | _exit(EXIT_FAILURE); | |
1623 | } | |
1624 | ||
1625 | r = send_one_fd(pair[1], fd, 0); | |
1626 | if (r < 0) { | |
1627 | log_address_error_errno(UNIT(s), address, r, "Failed to send listening socket (%s) to parent: %m"); | |
1628 | _exit(EXIT_FAILURE); | |
1629 | } | |
1630 | ||
1631 | _exit(EXIT_SUCCESS); | |
1632 | } | |
1633 | ||
1634 | pair[1] = safe_close(pair[1]); | |
1635 | fd = receive_one_fd(pair[0], 0); | |
1636 | ||
1637 | /* We synchronously wait for the helper, as it shouldn't be slow */ | |
1638 | r = wait_for_terminate_and_check("(sd-listen)", pid.pid, WAIT_LOG_ABNORMAL); | |
1639 | if (r < 0) { | |
1640 | safe_close(fd); | |
1641 | return r; | |
1642 | } | |
1643 | ||
1644 | if (fd < 0) | |
1645 | return log_address_error_errno(UNIT(s), address, fd, "Failed to receive listening socket (%s): %m"); | |
1646 | ||
1647 | return fd; | |
1648 | } | |
1649 | ||
1650 | static int socket_open_fds(Socket *orig_s) { | |
1651 | _cleanup_(socket_close_fdsp) Socket *s = orig_s; | |
1652 | _cleanup_freecon_ char *label = NULL; | |
1653 | bool know_label = false; | |
1654 | int r; | |
1655 | ||
1656 | assert(s); | |
1657 | ||
1658 | LIST_FOREACH(port, p, s->ports) { | |
1659 | ||
1660 | if (p->fd >= 0) | |
1661 | continue; | |
1662 | ||
1663 | switch (p->type) { | |
1664 | ||
1665 | case SOCKET_SOCKET: | |
1666 | ||
1667 | if (!know_label) { | |
1668 | /* Figure out the label, if we don't it know yet. We do it once for the first | |
1669 | * socket where we need this and remember it for the rest. */ | |
1670 | ||
1671 | r = socket_determine_selinux_label(s, &label); | |
1672 | if (r < 0) | |
1673 | return log_unit_error_errno(UNIT(s), r, "Failed to determine SELinux label: %m"); | |
1674 | ||
1675 | know_label = true; | |
1676 | } | |
1677 | ||
1678 | /* Apply the socket protocol */ | |
1679 | switch (p->address.type) { | |
1680 | ||
1681 | case SOCK_STREAM: | |
1682 | if (IN_SET(s->socket_protocol, IPPROTO_SCTP, IPPROTO_MPTCP)) | |
1683 | p->address.protocol = s->socket_protocol; | |
1684 | break; | |
1685 | ||
1686 | case SOCK_SEQPACKET: | |
1687 | if (s->socket_protocol == IPPROTO_SCTP) | |
1688 | p->address.protocol = s->socket_protocol; | |
1689 | break; | |
1690 | ||
1691 | case SOCK_DGRAM: | |
1692 | if (s->socket_protocol == IPPROTO_UDPLITE) | |
1693 | p->address.protocol = s->socket_protocol; | |
1694 | break; | |
1695 | } | |
1696 | ||
1697 | p->fd = socket_address_listen_in_cgroup(s, &p->address, label); | |
1698 | if (p->fd < 0) | |
1699 | return p->fd; | |
1700 | ||
1701 | socket_apply_socket_options(s, p, p->fd); | |
1702 | socket_symlink(s); | |
1703 | break; | |
1704 | ||
1705 | case SOCKET_SPECIAL: | |
1706 | ||
1707 | p->fd = special_address_create(p->path, s->writable); | |
1708 | if (p->fd < 0) | |
1709 | return log_unit_error_errno(UNIT(s), p->fd, "Failed to open special file '%s': %m", p->path); | |
1710 | break; | |
1711 | ||
1712 | case SOCKET_FIFO: | |
1713 | ||
1714 | p->fd = fifo_address_create( | |
1715 | p->path, | |
1716 | s->directory_mode, | |
1717 | s->socket_mode); | |
1718 | if (p->fd < 0) | |
1719 | return log_unit_error_errno(UNIT(s), p->fd, "Failed to open FIFO '%s': %m", p->path); | |
1720 | ||
1721 | socket_apply_fifo_options(s, p->fd); | |
1722 | socket_symlink(s); | |
1723 | break; | |
1724 | ||
1725 | case SOCKET_MQUEUE: | |
1726 | ||
1727 | p->fd = mq_address_create( | |
1728 | p->path, | |
1729 | s->socket_mode, | |
1730 | s->mq_maxmsg, | |
1731 | s->mq_msgsize); | |
1732 | if (p->fd < 0) | |
1733 | return log_unit_error_errno(UNIT(s), p->fd, "Failed to open message queue '%s': %m", p->path); | |
1734 | break; | |
1735 | ||
1736 | case SOCKET_USB_FUNCTION: { | |
1737 | _cleanup_close_ int dfd = -EBADF; | |
1738 | ||
1739 | dfd = open(p->path, O_DIRECTORY|O_CLOEXEC); | |
1740 | if (dfd < 0) | |
1741 | return log_unit_error_errno(UNIT(s), errno, | |
1742 | "Failed to open USB FunctionFS dir '%s': %m", p->path); | |
1743 | ||
1744 | p->fd = usbffs_address_create_at(dfd, "ep0"); | |
1745 | if (p->fd < 0) | |
1746 | return log_unit_error_errno(UNIT(s), p->fd, "Failed to open USB FunctionFS ep0: %m"); | |
1747 | ||
1748 | r = usbffs_write_descs(p->fd, SERVICE(UNIT_DEREF(s->service))); | |
1749 | if (r < 0) | |
1750 | return log_unit_error_errno(UNIT(s), r, "Failed to write to USB FunctionFS ep0: %m"); | |
1751 | ||
1752 | r = usbffs_dispatch_eps(p, dfd); | |
1753 | if (r < 0) | |
1754 | return log_unit_error_errno(UNIT(s), r, "Failed to dispatch USB FunctionFS eps: %m"); | |
1755 | ||
1756 | break; | |
1757 | } | |
1758 | ||
1759 | default: | |
1760 | assert_not_reached(); | |
1761 | } | |
1762 | } | |
1763 | ||
1764 | TAKE_PTR(s); | |
1765 | return 0; | |
1766 | } | |
1767 | ||
1768 | static void socket_unwatch_fds(Socket *s) { | |
1769 | int r; | |
1770 | ||
1771 | assert(s); | |
1772 | ||
1773 | LIST_FOREACH(port, p, s->ports) { | |
1774 | if (p->fd < 0) | |
1775 | continue; | |
1776 | ||
1777 | r = sd_event_source_set_enabled(p->event_source, SD_EVENT_OFF); | |
1778 | if (r < 0) | |
1779 | log_unit_debug_errno(UNIT(s), r, "Failed to disable event source: %m"); | |
1780 | } | |
1781 | } | |
1782 | ||
1783 | static int socket_watch_fds(Socket *s) { | |
1784 | int r; | |
1785 | ||
1786 | assert(s); | |
1787 | ||
1788 | LIST_FOREACH(port, p, s->ports) { | |
1789 | if (p->fd < 0) | |
1790 | continue; | |
1791 | ||
1792 | if (p->event_source) { | |
1793 | r = sd_event_source_set_enabled(p->event_source, SD_EVENT_ON); | |
1794 | if (r < 0) | |
1795 | goto fail; | |
1796 | } else { | |
1797 | r = sd_event_add_io(UNIT(s)->manager->event, &p->event_source, p->fd, EPOLLIN, socket_dispatch_io, p); | |
1798 | if (r < 0) | |
1799 | goto fail; | |
1800 | ||
1801 | (void) sd_event_source_set_description(p->event_source, "socket-port-io"); | |
1802 | } | |
1803 | ||
1804 | r = sd_event_source_set_ratelimit(p->event_source, s->poll_limit.interval, s->poll_limit.burst); | |
1805 | if (r < 0) | |
1806 | log_unit_debug_errno(UNIT(s), r, "Failed to set poll limit on I/O event source, ignoring: %m"); | |
1807 | } | |
1808 | ||
1809 | return 0; | |
1810 | ||
1811 | fail: | |
1812 | log_unit_warning_errno(UNIT(s), r, "Failed to watch listening fds: %m"); | |
1813 | socket_unwatch_fds(s); | |
1814 | return r; | |
1815 | } | |
1816 | ||
1817 | enum { | |
1818 | SOCKET_OPEN_NONE, | |
1819 | SOCKET_OPEN_SOME, | |
1820 | SOCKET_OPEN_ALL, | |
1821 | }; | |
1822 | ||
1823 | static int socket_check_open(Socket *s) { | |
1824 | bool have_open = false, have_closed = false; | |
1825 | ||
1826 | assert(s); | |
1827 | ||
1828 | LIST_FOREACH(port, p, s->ports) { | |
1829 | if (p->fd < 0) | |
1830 | have_closed = true; | |
1831 | else | |
1832 | have_open = true; | |
1833 | ||
1834 | if (have_open && have_closed) | |
1835 | return SOCKET_OPEN_SOME; | |
1836 | } | |
1837 | ||
1838 | if (have_open) | |
1839 | return SOCKET_OPEN_ALL; | |
1840 | ||
1841 | return SOCKET_OPEN_NONE; | |
1842 | } | |
1843 | ||
1844 | static void socket_set_state(Socket *s, SocketState state) { | |
1845 | SocketState old_state; | |
1846 | ||
1847 | assert(s); | |
1848 | ||
1849 | if (s->state != state) | |
1850 | bus_unit_send_pending_change_signal(UNIT(s), false); | |
1851 | ||
1852 | old_state = s->state; | |
1853 | s->state = state; | |
1854 | ||
1855 | if (!SOCKET_STATE_WITH_PROCESS(state)) { | |
1856 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); | |
1857 | socket_unwatch_control_pid(s); | |
1858 | s->control_command = NULL; | |
1859 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
1860 | } | |
1861 | ||
1862 | if (state != SOCKET_LISTENING) | |
1863 | socket_unwatch_fds(s); | |
1864 | ||
1865 | if (!IN_SET(state, | |
1866 | SOCKET_START_OPEN, | |
1867 | SOCKET_START_CHOWN, | |
1868 | SOCKET_START_POST, | |
1869 | SOCKET_LISTENING, | |
1870 | SOCKET_RUNNING, | |
1871 | SOCKET_STOP_PRE, | |
1872 | SOCKET_STOP_PRE_SIGTERM, | |
1873 | SOCKET_STOP_PRE_SIGKILL)) | |
1874 | socket_close_fds(s); | |
1875 | ||
1876 | if (state != old_state) | |
1877 | log_unit_debug(UNIT(s), "Changed %s -> %s", socket_state_to_string(old_state), socket_state_to_string(state)); | |
1878 | ||
1879 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true); | |
1880 | } | |
1881 | ||
1882 | static int socket_coldplug(Unit *u) { | |
1883 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
1884 | int r; | |
1885 | ||
1886 | assert(s->state == SOCKET_DEAD); | |
1887 | ||
1888 | if (s->deserialized_state == s->state) | |
1889 | return 0; | |
1890 | ||
1891 | if (pidref_is_set(&s->control_pid) && | |
1892 | pidref_is_unwaited(&s->control_pid) > 0 && | |
1893 | SOCKET_STATE_WITH_PROCESS(s->deserialized_state)) { | |
1894 | ||
1895 | r = unit_watch_pidref(UNIT(s), &s->control_pid, /* exclusive= */ false); | |
1896 | if (r < 0) | |
1897 | return r; | |
1898 | ||
1899 | r = socket_arm_timer(s, /* relative= */ false, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec)); | |
1900 | if (r < 0) | |
1901 | return r; | |
1902 | } | |
1903 | ||
1904 | if (IN_SET(s->deserialized_state, | |
1905 | SOCKET_START_OPEN, | |
1906 | SOCKET_START_CHOWN, | |
1907 | SOCKET_START_POST, | |
1908 | SOCKET_LISTENING, | |
1909 | SOCKET_RUNNING)) { | |
1910 | ||
1911 | /* Originally, we used to simply reopen all sockets here that we didn't have file descriptors | |
1912 | * for. However, this is problematic, as we won't traverse through the SOCKET_START_CHOWN state for | |
1913 | * them, and thus the UID/GID wouldn't be right. Hence, instead simply check if we have all fds open, | |
1914 | * and if there's a mismatch, warn loudly. | |
1915 | * | |
1916 | * Note that SOCKET_START_OPEN requires no special treatment, as it's only intermediate | |
1917 | * between SOCKET_START_PRE and SOCKET_START_CHOWN and shall otherwise not be observed. | |
1918 | * It's listed only for consistency. */ | |
1919 | ||
1920 | r = socket_check_open(s); | |
1921 | if (r == SOCKET_OPEN_NONE) | |
1922 | log_unit_warning(UNIT(s), | |
1923 | "Socket unit configuration has changed while unit has been running, " | |
1924 | "no open socket file descriptor left. " | |
1925 | "The socket unit is not functional until restarted."); | |
1926 | else if (r == SOCKET_OPEN_SOME) | |
1927 | log_unit_warning(UNIT(s), | |
1928 | "Socket unit configuration has changed while unit has been running, " | |
1929 | "and some socket file descriptors have not been opened yet. " | |
1930 | "The socket unit is not fully functional until restarted."); | |
1931 | } | |
1932 | ||
1933 | if (s->deserialized_state == SOCKET_LISTENING) { | |
1934 | r = socket_watch_fds(s); | |
1935 | if (r < 0) | |
1936 | return r; | |
1937 | } | |
1938 | ||
1939 | if (!IN_SET(s->deserialized_state, SOCKET_DEAD, SOCKET_FAILED, SOCKET_CLEANING)) | |
1940 | (void) unit_setup_exec_runtime(u); | |
1941 | ||
1942 | socket_set_state(s, s->deserialized_state); | |
1943 | return 0; | |
1944 | } | |
1945 | ||
1946 | static int socket_spawn(Socket *s, ExecCommand *c, PidRef *ret_pid) { | |
1947 | _cleanup_(exec_params_shallow_clear) ExecParameters exec_params = EXEC_PARAMETERS_INIT( | |
1948 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN); | |
1949 | _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; | |
1950 | int r; | |
1951 | ||
1952 | assert(s); | |
1953 | assert(c); | |
1954 | assert(ret_pid); | |
1955 | ||
1956 | r = unit_prepare_exec(UNIT(s)); | |
1957 | if (r < 0) | |
1958 | return r; | |
1959 | ||
1960 | r = socket_arm_timer(s, /* relative= */ true, s->timeout_usec); | |
1961 | if (r < 0) | |
1962 | return r; | |
1963 | ||
1964 | r = unit_set_exec_params(UNIT(s), &exec_params); | |
1965 | if (r < 0) | |
1966 | return r; | |
1967 | ||
1968 | /* Note that ExecStartPre= command doesn't inherit any FDs. It runs before we open listen FDs. */ | |
1969 | if (s->pass_fds_to_exec) { | |
1970 | _cleanup_strv_free_ char **fd_names = NULL; | |
1971 | _cleanup_free_ int *fds = NULL; | |
1972 | int n_fds; | |
1973 | ||
1974 | n_fds = socket_collect_fds(s, &fds); | |
1975 | if (n_fds < 0) | |
1976 | return n_fds; | |
1977 | ||
1978 | r = strv_extend_n(&fd_names, socket_fdname(s), n_fds); | |
1979 | if (r < 0) | |
1980 | return r; | |
1981 | ||
1982 | exec_params.flags |= EXEC_PASS_FDS; | |
1983 | exec_params.fds = TAKE_PTR(fds); | |
1984 | exec_params.fd_names = TAKE_PTR(fd_names); | |
1985 | exec_params.n_socket_fds = n_fds; | |
1986 | } | |
1987 | ||
1988 | r = exec_spawn(UNIT(s), | |
1989 | c, | |
1990 | &s->exec_context, | |
1991 | &exec_params, | |
1992 | s->exec_runtime, | |
1993 | &s->cgroup_context, | |
1994 | &pidref); | |
1995 | if (r < 0) | |
1996 | return r; | |
1997 | ||
1998 | r = unit_watch_pidref(UNIT(s), &pidref, /* exclusive= */ true); | |
1999 | if (r < 0) | |
2000 | return r; | |
2001 | ||
2002 | *ret_pid = TAKE_PIDREF(pidref); | |
2003 | return 0; | |
2004 | } | |
2005 | ||
2006 | static int socket_chown(Socket *s, PidRef *ret_pid) { | |
2007 | _cleanup_(pidref_done) PidRef pid = PIDREF_NULL; | |
2008 | int r; | |
2009 | ||
2010 | assert(s); | |
2011 | ||
2012 | r = socket_arm_timer(s, /* relative= */ true, s->timeout_usec); | |
2013 | if (r < 0) | |
2014 | return r; | |
2015 | ||
2016 | /* We have to resolve the user names out-of-process, hence | |
2017 | * let's fork here. It's messy, but well, what can we do? */ | |
2018 | ||
2019 | r = unit_fork_helper_process(UNIT(s), "(sd-chown)", /* into_cgroup= */ true, &pid); | |
2020 | if (r < 0) | |
2021 | return r; | |
2022 | if (r == 0) { | |
2023 | uid_t uid = UID_INVALID; | |
2024 | gid_t gid = GID_INVALID; | |
2025 | ||
2026 | /* Child */ | |
2027 | ||
2028 | if (!isempty(s->user)) { | |
2029 | const char *user = s->user; | |
2030 | ||
2031 | r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0); | |
2032 | if (r < 0) { | |
2033 | log_unit_error_errno(UNIT(s), r, "Failed to resolve user %s: %m", user); | |
2034 | _exit(EXIT_USER); | |
2035 | } | |
2036 | } | |
2037 | ||
2038 | if (!isempty(s->group)) { | |
2039 | const char *group = s->group; | |
2040 | ||
2041 | r = get_group_creds(&group, &gid, 0); | |
2042 | if (r < 0) { | |
2043 | log_unit_error_errno(UNIT(s), r, "Failed to resolve group %s: %m", group); | |
2044 | _exit(EXIT_GROUP); | |
2045 | } | |
2046 | } | |
2047 | ||
2048 | LIST_FOREACH(port, p, s->ports) { | |
2049 | const char *path = NULL; | |
2050 | ||
2051 | if (p->type == SOCKET_SOCKET) | |
2052 | path = socket_address_get_path(&p->address); | |
2053 | else if (p->type == SOCKET_FIFO) | |
2054 | path = p->path; | |
2055 | else if (p->type == SOCKET_MQUEUE) { | |
2056 | /* Use fchown on the fd since /dev/mqueue might not be mounted. */ | |
2057 | if (fchown(p->fd, uid, gid) < 0) { | |
2058 | log_unit_error_errno(UNIT(s), errno, "Failed to fchown(): %m"); | |
2059 | _exit(EXIT_CHOWN); | |
2060 | } | |
2061 | continue; | |
2062 | } | |
2063 | ||
2064 | if (!path) | |
2065 | continue; | |
2066 | ||
2067 | if (chown(path, uid, gid) < 0) { | |
2068 | log_unit_error_errno(UNIT(s), errno, "Failed to chown(): %m"); | |
2069 | _exit(EXIT_CHOWN); | |
2070 | } | |
2071 | } | |
2072 | ||
2073 | _exit(EXIT_SUCCESS); | |
2074 | } | |
2075 | ||
2076 | r = unit_watch_pidref(UNIT(s), &pid, /* exclusive= */ true); | |
2077 | if (r < 0) | |
2078 | return r; | |
2079 | ||
2080 | *ret_pid = TAKE_PIDREF(pid); | |
2081 | return 0; | |
2082 | } | |
2083 | ||
2084 | static void socket_enter_dead(Socket *s, SocketResult f) { | |
2085 | assert(s); | |
2086 | ||
2087 | if (s->result == SOCKET_SUCCESS) | |
2088 | s->result = f; | |
2089 | ||
2090 | if (s->result == SOCKET_SUCCESS) | |
2091 | unit_log_success(UNIT(s)); | |
2092 | else | |
2093 | unit_log_failure(UNIT(s), socket_result_to_string(s->result)); | |
2094 | ||
2095 | unit_warn_leftover_processes(UNIT(s), /* start = */ false); | |
2096 | ||
2097 | socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD); | |
2098 | ||
2099 | s->exec_runtime = exec_runtime_destroy(s->exec_runtime); | |
2100 | ||
2101 | unit_destroy_runtime_data(UNIT(s), &s->exec_context, /* destroy_runtime_dir = */ true); | |
2102 | ||
2103 | unit_unref_uid_gid(UNIT(s), true); | |
2104 | } | |
2105 | ||
2106 | static void socket_enter_signal(Socket *s, SocketState state, SocketResult f); | |
2107 | ||
2108 | static void socket_enter_stop_post(Socket *s, SocketResult f) { | |
2109 | int r; | |
2110 | ||
2111 | assert(s); | |
2112 | ||
2113 | if (s->result == SOCKET_SUCCESS) | |
2114 | s->result = f; | |
2115 | ||
2116 | socket_unwatch_control_pid(s); | |
2117 | s->control_command_id = SOCKET_EXEC_STOP_POST; | |
2118 | s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST]; | |
2119 | ||
2120 | if (s->control_command) { | |
2121 | pidref_done(&s->control_pid); | |
2122 | ||
2123 | r = socket_spawn(s, s->control_command, &s->control_pid); | |
2124 | if (r < 0) { | |
2125 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'stop-post' task: %m"); | |
2126 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES); | |
2127 | return; | |
2128 | } | |
2129 | ||
2130 | socket_set_state(s, SOCKET_STOP_POST); | |
2131 | } else | |
2132 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_SUCCESS); | |
2133 | } | |
2134 | ||
2135 | static int state_to_kill_operation(Socket *s, SocketState state) { | |
2136 | assert(s); | |
2137 | ||
2138 | if (state == SOCKET_STOP_PRE_SIGTERM) | |
2139 | return unit_has_job_type(UNIT(s), JOB_RESTART) ? KILL_RESTART : KILL_TERMINATE; | |
2140 | ||
2141 | if (state == SOCKET_FINAL_SIGTERM) | |
2142 | return KILL_TERMINATE; | |
2143 | ||
2144 | return KILL_KILL; | |
2145 | } | |
2146 | ||
2147 | static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) { | |
2148 | int r; | |
2149 | ||
2150 | assert(s); | |
2151 | ||
2152 | if (s->result == SOCKET_SUCCESS) | |
2153 | s->result = f; | |
2154 | ||
2155 | r = unit_kill_context(UNIT(s), state_to_kill_operation(s, state)); | |
2156 | if (r < 0) { | |
2157 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); | |
2158 | goto fail; | |
2159 | } | |
2160 | if (r > 0) { | |
2161 | r = socket_arm_timer(s, /* relative= */ true, s->timeout_usec); | |
2162 | if (r < 0) { | |
2163 | log_unit_warning_errno(UNIT(s), r, "Failed to install timer: %m"); | |
2164 | goto fail; | |
2165 | } | |
2166 | ||
2167 | socket_set_state(s, state); | |
2168 | } else if (state == SOCKET_STOP_PRE_SIGTERM) | |
2169 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_SUCCESS); | |
2170 | else if (state == SOCKET_STOP_PRE_SIGKILL) | |
2171 | socket_enter_stop_post(s, SOCKET_SUCCESS); | |
2172 | else if (state == SOCKET_FINAL_SIGTERM) | |
2173 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS); | |
2174 | else | |
2175 | socket_enter_dead(s, SOCKET_SUCCESS); | |
2176 | ||
2177 | return; | |
2178 | ||
2179 | fail: | |
2180 | if (IN_SET(state, SOCKET_STOP_PRE_SIGTERM, SOCKET_STOP_PRE_SIGKILL)) | |
2181 | socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES); | |
2182 | else | |
2183 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); | |
2184 | } | |
2185 | ||
2186 | static void socket_enter_stop_pre(Socket *s, SocketResult f) { | |
2187 | int r; | |
2188 | ||
2189 | assert(s); | |
2190 | ||
2191 | if (s->result == SOCKET_SUCCESS) | |
2192 | s->result = f; | |
2193 | ||
2194 | socket_unwatch_control_pid(s); | |
2195 | s->control_command_id = SOCKET_EXEC_STOP_PRE; | |
2196 | s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE]; | |
2197 | ||
2198 | if (s->control_command) { | |
2199 | pidref_done(&s->control_pid); | |
2200 | ||
2201 | r = socket_spawn(s, s->control_command, &s->control_pid); | |
2202 | if (r < 0) { | |
2203 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'stop-pre' task: %m"); | |
2204 | socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES); | |
2205 | return; | |
2206 | } | |
2207 | ||
2208 | socket_set_state(s, SOCKET_STOP_PRE); | |
2209 | } else | |
2210 | socket_enter_stop_post(s, SOCKET_SUCCESS); | |
2211 | } | |
2212 | ||
2213 | static void flush_ports(Socket *s) { | |
2214 | assert(s); | |
2215 | ||
2216 | /* Flush all incoming traffic, regardless if actual bytes or new connections, so that this socket isn't busy | |
2217 | * anymore */ | |
2218 | ||
2219 | LIST_FOREACH(port, p, s->ports) { | |
2220 | if (p->fd < 0) | |
2221 | continue; | |
2222 | ||
2223 | if (p->type == SOCKET_MQUEUE) | |
2224 | (void) flush_mqueue(p->fd); | |
2225 | else { | |
2226 | (void) flush_accept(p->fd); | |
2227 | (void) flush_fd(p->fd); | |
2228 | } | |
2229 | } | |
2230 | } | |
2231 | ||
2232 | static void socket_enter_listening(Socket *s) { | |
2233 | int r; | |
2234 | ||
2235 | assert(s); | |
2236 | ||
2237 | if (!s->accept && s->flush_pending) { | |
2238 | log_unit_debug(UNIT(s), "Flushing socket before listening."); | |
2239 | flush_ports(s); | |
2240 | } | |
2241 | ||
2242 | r = socket_watch_fds(s); | |
2243 | if (r < 0) { | |
2244 | log_unit_warning_errno(UNIT(s), r, "Failed to watch sockets: %m"); | |
2245 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); | |
2246 | return; | |
2247 | } | |
2248 | ||
2249 | socket_set_state(s, SOCKET_LISTENING); | |
2250 | } | |
2251 | ||
2252 | static void socket_enter_start_post(Socket *s) { | |
2253 | int r; | |
2254 | ||
2255 | assert(s); | |
2256 | ||
2257 | socket_unwatch_control_pid(s); | |
2258 | s->control_command_id = SOCKET_EXEC_START_POST; | |
2259 | s->control_command = s->exec_command[SOCKET_EXEC_START_POST]; | |
2260 | ||
2261 | if (s->control_command) { | |
2262 | pidref_done(&s->control_pid); | |
2263 | ||
2264 | r = socket_spawn(s, s->control_command, &s->control_pid); | |
2265 | if (r < 0) { | |
2266 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'start-post' task: %m"); | |
2267 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); | |
2268 | return; | |
2269 | } | |
2270 | ||
2271 | socket_set_state(s, SOCKET_START_POST); | |
2272 | } else | |
2273 | socket_enter_listening(s); | |
2274 | } | |
2275 | ||
2276 | static void socket_enter_start_chown(Socket *s) { | |
2277 | int r; | |
2278 | ||
2279 | assert(s); | |
2280 | assert(s->state == SOCKET_START_OPEN); | |
2281 | ||
2282 | if (!isempty(s->user) || !isempty(s->group)) { | |
2283 | ||
2284 | socket_unwatch_control_pid(s); | |
2285 | s->control_command_id = SOCKET_EXEC_START_CHOWN; | |
2286 | s->control_command = NULL; | |
2287 | ||
2288 | r = socket_chown(s, &s->control_pid); | |
2289 | if (r < 0) { | |
2290 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'start-chown' task: %m"); | |
2291 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); | |
2292 | return; | |
2293 | } | |
2294 | ||
2295 | socket_set_state(s, SOCKET_START_CHOWN); | |
2296 | } else | |
2297 | socket_enter_start_post(s); | |
2298 | } | |
2299 | ||
2300 | static void socket_enter_start_open(Socket *s) { | |
2301 | int r; | |
2302 | ||
2303 | assert(s); | |
2304 | assert(IN_SET(s->state, SOCKET_DEAD, SOCKET_FAILED, SOCKET_START_PRE)); | |
2305 | ||
2306 | /* We force a state transition here even though we're not spawning any process (i.e. the state is purely | |
2307 | * intermediate), so that failure of socket_open_fds() always causes a state change in unit_notify(). | |
2308 | * Otherwise, if no Exec*= is defined, we might go from previous SOCKET_FAILED to SOCKET_FAILED, | |
2309 | * meaning the OnFailure= deps are unexpectedly skipped (#35635). */ | |
2310 | ||
2311 | socket_set_state(s, SOCKET_START_OPEN); | |
2312 | ||
2313 | r = socket_open_fds(s); | |
2314 | if (r < 0) { | |
2315 | log_unit_error_errno(UNIT(s), r, "Failed to listen on sockets: %m"); | |
2316 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); | |
2317 | return; | |
2318 | } | |
2319 | ||
2320 | socket_enter_start_chown(s); | |
2321 | } | |
2322 | ||
2323 | static void socket_enter_start_pre(Socket *s) { | |
2324 | int r; | |
2325 | ||
2326 | assert(s); | |
2327 | ||
2328 | socket_unwatch_control_pid(s); | |
2329 | ||
2330 | unit_warn_leftover_processes(UNIT(s), /* start = */ true); | |
2331 | ||
2332 | s->control_command_id = SOCKET_EXEC_START_PRE; | |
2333 | s->control_command = s->exec_command[SOCKET_EXEC_START_PRE]; | |
2334 | ||
2335 | if (s->control_command) { | |
2336 | pidref_done(&s->control_pid); | |
2337 | ||
2338 | r = socket_spawn(s, s->control_command, &s->control_pid); | |
2339 | if (r < 0) { | |
2340 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn 'start-pre' task: %m"); | |
2341 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); | |
2342 | return; | |
2343 | } | |
2344 | ||
2345 | socket_set_state(s, SOCKET_START_PRE); | |
2346 | } else | |
2347 | socket_enter_start_open(s); | |
2348 | } | |
2349 | ||
2350 | static void socket_enter_running(Socket *s, int cfd_in) { | |
2351 | /* Note that this call takes possession of the connection fd passed. It either has to assign it | |
2352 | * somewhere or close it. */ | |
2353 | _cleanup_close_ int cfd = cfd_in; | |
2354 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2355 | int r; | |
2356 | ||
2357 | assert(s); | |
2358 | ||
2359 | /* We don't take connections anymore if we are supposed to shut down anyway */ | |
2360 | if (unit_stop_pending(UNIT(s))) { | |
2361 | ||
2362 | log_unit_debug(UNIT(s), "Suppressing connection request since unit stop is scheduled."); | |
2363 | ||
2364 | if (cfd >= 0) | |
2365 | goto refuse; | |
2366 | ||
2367 | flush_ports(s); | |
2368 | return; | |
2369 | } | |
2370 | ||
2371 | if (!ratelimit_below(&s->trigger_limit)) { | |
2372 | log_unit_warning(UNIT(s), "Trigger limit hit, refusing further activation."); | |
2373 | socket_enter_stop_pre(s, SOCKET_FAILURE_TRIGGER_LIMIT_HIT); | |
2374 | goto refuse; | |
2375 | } | |
2376 | ||
2377 | if (cfd < 0) { /* Accept=no case */ | |
2378 | bool pending = false; | |
2379 | Unit *other; | |
2380 | ||
2381 | /* If there's already a start pending don't bother to do anything */ | |
2382 | UNIT_FOREACH_DEPENDENCY(other, UNIT(s), UNIT_ATOM_TRIGGERS) | |
2383 | if (unit_active_or_pending(other)) { | |
2384 | pending = true; | |
2385 | break; | |
2386 | } | |
2387 | ||
2388 | if (!pending) { | |
2389 | if (!UNIT_ISSET(s->service)) { | |
2390 | log_unit_warning(UNIT(s), | |
2391 | "Service to activate vanished, refusing activation."); | |
2392 | goto fail; | |
2393 | } | |
2394 | ||
2395 | r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, &error, /* ret = */ NULL); | |
2396 | if (r == -EDEADLK) | |
2397 | return (void) log_unit_debug_errno(UNIT(s), r, "Failed to queue service startup job, ignoring: %s", bus_error_message(&error, r)); | |
2398 | if (r < 0) | |
2399 | goto queue_error; | |
2400 | } | |
2401 | ||
2402 | socket_set_state(s, SOCKET_RUNNING); | |
2403 | } else { /* Accept=yes case */ | |
2404 | _cleanup_(socket_peer_unrefp) SocketPeer *p = NULL; | |
2405 | Unit *service; | |
2406 | ||
2407 | if (s->n_connections >= s->max_connections) { | |
2408 | log_unit_warning(UNIT(s), "Too many incoming connections (%u), dropping connection.", | |
2409 | s->n_connections); | |
2410 | goto refuse; | |
2411 | } | |
2412 | ||
2413 | if (s->max_connections_per_source > 0) { | |
2414 | r = socket_acquire_peer(s, cfd, &p); | |
2415 | if (r < 0) { | |
2416 | if (ERRNO_IS_DISCONNECT(r)) | |
2417 | return; | |
2418 | /* We didn't have enough resources to acquire peer information, let's fail. */ | |
2419 | goto fail; | |
2420 | } | |
2421 | if (r > 0 && p->n_ref > s->max_connections_per_source) { | |
2422 | _cleanup_free_ char *t = NULL; | |
2423 | ||
2424 | if (p->peer.sa.sa_family == AF_UNIX) | |
2425 | (void) asprintf(&t, "UID " UID_FMT, p->peer_cred.uid); | |
2426 | else | |
2427 | (void) sockaddr_pretty(&p->peer.sa, p->peer_salen, /* translate_ipv6= */ true, /* include_port= */ false, &t); | |
2428 | ||
2429 | log_unit_warning(UNIT(s), | |
2430 | "Too many incoming connections (%u) from source %s, dropping connection.", | |
2431 | p->n_ref, strnull(t)); | |
2432 | goto refuse; | |
2433 | } | |
2434 | } | |
2435 | ||
2436 | r = socket_load_service_unit(s, cfd, &service); | |
2437 | if (ERRNO_IS_NEG_DISCONNECT(r)) | |
2438 | return; | |
2439 | if (r < 0 || UNIT_IS_LOAD_ERROR(service->load_state)) { | |
2440 | log_unit_warning_errno(UNIT(s), r < 0 ? r : service->load_error, | |
2441 | "Failed to load connection service unit: %m"); | |
2442 | goto fail; | |
2443 | } | |
2444 | if (service->load_state == UNIT_MASKED) { | |
2445 | log_unit_warning(UNIT(s), "Connection service unit is masked, refusing."); | |
2446 | goto fail; | |
2447 | } | |
2448 | ||
2449 | s->n_accepted++; | |
2450 | ||
2451 | r = service_set_socket_fd(SERVICE(service), cfd, s, p, s->selinux_context_from_net); | |
2452 | if (ERRNO_IS_NEG_DISCONNECT(r)) | |
2453 | return; | |
2454 | if (r < 0) { | |
2455 | log_unit_warning_errno(UNIT(s), r, "Failed to set socket on service: %m"); | |
2456 | goto fail; | |
2457 | } | |
2458 | ||
2459 | /* We passed ownership of the fd and socket peer to the service now. */ | |
2460 | TAKE_FD(cfd); | |
2461 | TAKE_PTR(p); | |
2462 | ||
2463 | s->n_connections++; | |
2464 | ||
2465 | r = manager_add_job(UNIT(s)->manager, JOB_START, service, JOB_REPLACE, &error, /* ret = */ NULL); | |
2466 | if (r < 0) { | |
2467 | /* We failed to activate the new service, but it still exists. Let's make sure the | |
2468 | * service closes and forgets the connection fd again, immediately. */ | |
2469 | service_release_socket_fd(SERVICE(service)); | |
2470 | goto queue_error; | |
2471 | } | |
2472 | ||
2473 | /* Notify clients about changed counters */ | |
2474 | unit_add_to_dbus_queue(UNIT(s)); | |
2475 | } | |
2476 | ||
2477 | return; | |
2478 | ||
2479 | refuse: | |
2480 | s->n_refused++; | |
2481 | return; | |
2482 | ||
2483 | queue_error: | |
2484 | log_unit_warning_errno(UNIT(s), r, "Failed to queue service startup job%s: %s", | |
2485 | cfd >= 0 && !ERRNO_IS_RESOURCE(r) ? " (Maybe the service is missing or is a template unit?)" : "", | |
2486 | bus_error_message(&error, r)); | |
2487 | ||
2488 | fail: | |
2489 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); | |
2490 | } | |
2491 | ||
2492 | static void socket_run_next(Socket *s) { | |
2493 | int r; | |
2494 | ||
2495 | assert(s); | |
2496 | assert(s->control_command); | |
2497 | assert(s->control_command->command_next); | |
2498 | ||
2499 | socket_unwatch_control_pid(s); | |
2500 | ||
2501 | s->control_command = s->control_command->command_next; | |
2502 | ||
2503 | pidref_done(&s->control_pid); | |
2504 | ||
2505 | r = socket_spawn(s, s->control_command, &s->control_pid); | |
2506 | if (r < 0) { | |
2507 | log_unit_warning_errno(UNIT(s), r, "Failed to spawn next task: %m"); | |
2508 | ||
2509 | if (s->state == SOCKET_START_POST) | |
2510 | socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES); | |
2511 | else if (s->state == SOCKET_STOP_POST) | |
2512 | socket_enter_dead(s, SOCKET_FAILURE_RESOURCES); | |
2513 | else | |
2514 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES); | |
2515 | } | |
2516 | } | |
2517 | ||
2518 | static int socket_start(Unit *u) { | |
2519 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
2520 | int r; | |
2521 | ||
2522 | /* We cannot fulfill this request right now, try again later | |
2523 | * please! */ | |
2524 | if (IN_SET(s->state, | |
2525 | SOCKET_STOP_PRE, | |
2526 | SOCKET_STOP_PRE_SIGKILL, | |
2527 | SOCKET_STOP_PRE_SIGTERM, | |
2528 | SOCKET_STOP_POST, | |
2529 | SOCKET_FINAL_SIGTERM, | |
2530 | SOCKET_FINAL_SIGKILL, | |
2531 | SOCKET_CLEANING)) | |
2532 | return -EAGAIN; | |
2533 | ||
2534 | /* Already on it! */ | |
2535 | if (IN_SET(s->state, | |
2536 | SOCKET_START_PRE, | |
2537 | SOCKET_START_OPEN, | |
2538 | SOCKET_START_CHOWN, | |
2539 | SOCKET_START_POST)) | |
2540 | return 0; | |
2541 | ||
2542 | /* Cannot run this without the service being around */ | |
2543 | if (UNIT_ISSET(s->service)) { | |
2544 | Service *service = ASSERT_PTR(SERVICE(UNIT_DEREF(s->service))); | |
2545 | ||
2546 | if (UNIT(service)->load_state != UNIT_LOADED) | |
2547 | return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT), | |
2548 | "Socket service %s not loaded, refusing.", UNIT(service)->id); | |
2549 | ||
2550 | /* If the service is already active we cannot start the socket */ | |
2551 | if (SOCKET_SERVICE_IS_ACTIVE(service, /* allow_finalize = */ false)) | |
2552 | return log_unit_error_errno(u, SYNTHETIC_ERRNO(EBUSY), | |
2553 | "Socket service %s already active, refusing.", UNIT(service)->id); | |
2554 | } | |
2555 | ||
2556 | assert(IN_SET(s->state, SOCKET_DEAD, SOCKET_FAILED)); | |
2557 | ||
2558 | r = unit_acquire_invocation_id(u); | |
2559 | if (r < 0) | |
2560 | return r; | |
2561 | ||
2562 | s->result = SOCKET_SUCCESS; | |
2563 | exec_command_reset_status_list_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX); | |
2564 | ||
2565 | if (s->cgroup_runtime) | |
2566 | s->cgroup_runtime->reset_accounting = true; | |
2567 | ||
2568 | socket_enter_start_pre(s); | |
2569 | return 1; | |
2570 | } | |
2571 | ||
2572 | static int socket_stop(Unit *u) { | |
2573 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
2574 | ||
2575 | /* Already on it */ | |
2576 | if (IN_SET(s->state, | |
2577 | SOCKET_STOP_PRE, | |
2578 | SOCKET_STOP_PRE_SIGTERM, | |
2579 | SOCKET_STOP_PRE_SIGKILL, | |
2580 | SOCKET_STOP_POST, | |
2581 | SOCKET_FINAL_SIGTERM, | |
2582 | SOCKET_FINAL_SIGKILL)) | |
2583 | return 0; | |
2584 | ||
2585 | /* If there's already something running we go directly into | |
2586 | * kill mode. */ | |
2587 | if (IN_SET(s->state, | |
2588 | SOCKET_START_PRE, | |
2589 | SOCKET_START_OPEN, | |
2590 | SOCKET_START_CHOWN, | |
2591 | SOCKET_START_POST)) { | |
2592 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_SUCCESS); | |
2593 | return -EAGAIN; | |
2594 | } | |
2595 | ||
2596 | /* If we are currently cleaning, then abort it, brutally. */ | |
2597 | if (s->state == SOCKET_CLEANING) { | |
2598 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS); | |
2599 | return 0; | |
2600 | } | |
2601 | ||
2602 | assert(IN_SET(s->state, SOCKET_LISTENING, SOCKET_RUNNING)); | |
2603 | ||
2604 | socket_enter_stop_pre(s, SOCKET_SUCCESS); | |
2605 | return 1; | |
2606 | } | |
2607 | ||
2608 | static int socket_serialize(Unit *u, FILE *f, FDSet *fds) { | |
2609 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
2610 | int r; | |
2611 | ||
2612 | assert(f); | |
2613 | assert(fds); | |
2614 | ||
2615 | (void) serialize_item(f, "state", socket_state_to_string(s->state)); | |
2616 | (void) serialize_item(f, "result", socket_result_to_string(s->result)); | |
2617 | (void) serialize_item_format(f, "n-accepted", "%u", s->n_accepted); | |
2618 | (void) serialize_item_format(f, "n-refused", "%u", s->n_refused); | |
2619 | (void) serialize_pidref(f, fds, "control-pid", &s->control_pid); | |
2620 | ||
2621 | if (s->control_command_id >= 0) | |
2622 | (void) serialize_item(f, "control-command", socket_exec_command_to_string(s->control_command_id)); | |
2623 | ||
2624 | LIST_FOREACH(port, p, s->ports) { | |
2625 | int copy; | |
2626 | ||
2627 | if (p->fd < 0) | |
2628 | continue; | |
2629 | ||
2630 | copy = fdset_put_dup(fds, p->fd); | |
2631 | if (copy < 0) | |
2632 | return log_unit_warning_errno(u, copy, "Failed to serialize socket fd: %m"); | |
2633 | ||
2634 | if (p->type == SOCKET_SOCKET) { | |
2635 | _cleanup_free_ char *t = NULL; | |
2636 | ||
2637 | r = socket_address_print(&p->address, &t); | |
2638 | if (r < 0) | |
2639 | return log_unit_error_errno(u, r, "Failed to format socket address: %m"); | |
2640 | ||
2641 | if (socket_address_family(&p->address) == AF_NETLINK) | |
2642 | (void) serialize_item_format(f, "netlink", "%i %s", copy, t); | |
2643 | else | |
2644 | (void) serialize_item_format(f, "socket", "%i %i %s", copy, p->address.type, t); | |
2645 | } else if (p->type == SOCKET_SPECIAL) | |
2646 | (void) serialize_item_format(f, "special", "%i %s", copy, p->path); | |
2647 | else if (p->type == SOCKET_MQUEUE) | |
2648 | (void) serialize_item_format(f, "mqueue", "%i %s", copy, p->path); | |
2649 | else if (p->type == SOCKET_USB_FUNCTION) | |
2650 | (void) serialize_item_format(f, "ffs", "%i %s", copy, p->path); | |
2651 | else { | |
2652 | assert(p->type == SOCKET_FIFO); | |
2653 | (void) serialize_item_format(f, "fifo", "%i %s", copy, p->path); | |
2654 | } | |
2655 | } | |
2656 | ||
2657 | (void) serialize_ratelimit(f, "trigger-ratelimit", &s->trigger_limit); | |
2658 | ||
2659 | return 0; | |
2660 | } | |
2661 | ||
2662 | static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
2663 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
2664 | int r; | |
2665 | ||
2666 | assert(key); | |
2667 | assert(value); | |
2668 | ||
2669 | if (streq(key, "state")) { | |
2670 | SocketState state; | |
2671 | ||
2672 | state = socket_state_from_string(value); | |
2673 | if (state < 0) | |
2674 | log_unit_debug(u, "Failed to parse state value: %s", value); | |
2675 | else | |
2676 | s->deserialized_state = state; | |
2677 | } else if (streq(key, "result")) { | |
2678 | SocketResult f; | |
2679 | ||
2680 | f = socket_result_from_string(value); | |
2681 | if (f < 0) | |
2682 | log_unit_debug(u, "Failed to parse result value: %s", value); | |
2683 | else if (f != SOCKET_SUCCESS) | |
2684 | s->result = f; | |
2685 | ||
2686 | } else if (streq(key, "n-accepted")) { | |
2687 | unsigned k; | |
2688 | ||
2689 | if (safe_atou(value, &k) < 0) | |
2690 | log_unit_debug(u, "Failed to parse n-accepted value: %s", value); | |
2691 | else | |
2692 | s->n_accepted += k; | |
2693 | } else if (streq(key, "n-refused")) { | |
2694 | unsigned k; | |
2695 | ||
2696 | if (safe_atou(value, &k) < 0) | |
2697 | log_unit_debug(u, "Failed to parse n-refused value: %s", value); | |
2698 | else | |
2699 | s->n_refused += k; | |
2700 | } else if (streq(key, "control-pid")) { | |
2701 | ||
2702 | if (!pidref_is_set(&s->control_pid)) | |
2703 | (void) deserialize_pidref(fds, value, &s->control_pid); | |
2704 | ||
2705 | } else if (streq(key, "control-command")) { | |
2706 | SocketExecCommand id; | |
2707 | ||
2708 | id = socket_exec_command_from_string(value); | |
2709 | if (id < 0) | |
2710 | log_unit_debug(u, "Failed to parse exec-command value: %s", value); | |
2711 | else { | |
2712 | s->control_command_id = id; | |
2713 | s->control_command = s->exec_command[id]; | |
2714 | } | |
2715 | } else if (streq(key, "fifo")) { | |
2716 | _cleanup_free_ char *fdv = NULL; | |
2717 | bool found = false; | |
2718 | int fd; | |
2719 | ||
2720 | r = extract_first_word(&value, &fdv, NULL, 0); | |
2721 | if (r <= 0) { | |
2722 | log_unit_debug(u, "Failed to parse fifo value: %s", value); | |
2723 | return 0; | |
2724 | } | |
2725 | ||
2726 | fd = parse_fd(fdv); | |
2727 | if (fd < 0 || !fdset_contains(fds, fd)) { | |
2728 | log_unit_debug(u, "Invalid fifo value: %s", fdv); | |
2729 | return 0; | |
2730 | } | |
2731 | ||
2732 | LIST_FOREACH(port, p, s->ports) | |
2733 | if (p->fd < 0 && | |
2734 | p->type == SOCKET_FIFO && | |
2735 | path_equal_or_inode_same(p->path, value, 0)) { | |
2736 | p->fd = fdset_remove(fds, fd); | |
2737 | found = true; | |
2738 | break; | |
2739 | } | |
2740 | if (!found) | |
2741 | log_unit_debug(u, "No matching fifo socket found: %s", value); | |
2742 | ||
2743 | } else if (streq(key, "special")) { | |
2744 | _cleanup_free_ char *fdv = NULL; | |
2745 | bool found = false; | |
2746 | int fd; | |
2747 | ||
2748 | r = extract_first_word(&value, &fdv, NULL, 0); | |
2749 | if (r <= 0) { | |
2750 | log_unit_debug(u, "Failed to parse special value: %s", value); | |
2751 | return 0; | |
2752 | } | |
2753 | ||
2754 | fd = parse_fd(fdv); | |
2755 | if (fd < 0 || !fdset_contains(fds, fd)) { | |
2756 | log_unit_debug(u, "Invalid special value: %s", fdv); | |
2757 | return 0; | |
2758 | } | |
2759 | ||
2760 | LIST_FOREACH(port, p, s->ports) | |
2761 | if (p->fd < 0 && | |
2762 | p->type == SOCKET_SPECIAL && | |
2763 | path_equal_or_inode_same(p->path, value, 0)) { | |
2764 | p->fd = fdset_remove(fds, fd); | |
2765 | found = true; | |
2766 | break; | |
2767 | } | |
2768 | if (!found) | |
2769 | log_unit_debug(u, "No matching special socket found: %s", value); | |
2770 | ||
2771 | } else if (streq(key, "mqueue")) { | |
2772 | _cleanup_free_ char *fdv = NULL; | |
2773 | bool found = false; | |
2774 | int fd; | |
2775 | ||
2776 | r = extract_first_word(&value, &fdv, NULL, 0); | |
2777 | if (r <= 0) { | |
2778 | log_unit_debug(u, "Failed to parse mqueue value: %s", value); | |
2779 | return 0; | |
2780 | } | |
2781 | ||
2782 | fd = parse_fd(fdv); | |
2783 | if (fd < 0 || !fdset_contains(fds, fd)) { | |
2784 | log_unit_debug(u, "Invalid mqueue value: %s", fdv); | |
2785 | return 0; | |
2786 | } | |
2787 | ||
2788 | LIST_FOREACH(port, p, s->ports) | |
2789 | if (p->fd < 0 && | |
2790 | p->type == SOCKET_MQUEUE && | |
2791 | streq(p->path, value)) { | |
2792 | p->fd = fdset_remove(fds, fd); | |
2793 | found = true; | |
2794 | break; | |
2795 | } | |
2796 | if (!found) | |
2797 | log_unit_debug(u, "No matching mqueue socket found: %s", value); | |
2798 | ||
2799 | } else if (streq(key, "socket")) { | |
2800 | _cleanup_free_ char *fdv = NULL, *typev = NULL; | |
2801 | bool found = false; | |
2802 | int fd, type; | |
2803 | ||
2804 | r = extract_first_word(&value, &fdv, NULL, 0); | |
2805 | if (r <= 0) { | |
2806 | log_unit_debug(u, "Failed to parse socket fd from value: %s", value); | |
2807 | return 0; | |
2808 | } | |
2809 | ||
2810 | fd = parse_fd(fdv); | |
2811 | if (fd < 0 || !fdset_contains(fds, fd)) { | |
2812 | log_unit_debug(u, "Invalid socket fd: %s", fdv); | |
2813 | return 0; | |
2814 | } | |
2815 | ||
2816 | r = extract_first_word(&value, &typev, NULL, 0); | |
2817 | if (r <= 0) { | |
2818 | log_unit_debug(u, "Failed to parse socket type from value: %s", value); | |
2819 | return 0; | |
2820 | } | |
2821 | ||
2822 | if (safe_atoi(typev, &type) < 0 || type < 0) { | |
2823 | log_unit_debug(u, "Invalid socket type: %s", typev); | |
2824 | return 0; | |
2825 | } | |
2826 | ||
2827 | LIST_FOREACH(port, p, s->ports) | |
2828 | if (p->fd < 0 && | |
2829 | socket_address_is(&p->address, value, type)) { | |
2830 | p->fd = fdset_remove(fds, fd); | |
2831 | found = true; | |
2832 | break; | |
2833 | } | |
2834 | if (!found) | |
2835 | log_unit_debug(u, "No matching %s socket found: %s", | |
2836 | socket_address_type_to_string(type), value); | |
2837 | ||
2838 | } else if (streq(key, "netlink")) { | |
2839 | _cleanup_free_ char *fdv = NULL; | |
2840 | bool found = false; | |
2841 | int fd; | |
2842 | ||
2843 | r = extract_first_word(&value, &fdv, NULL, 0); | |
2844 | if (r <= 0) { | |
2845 | log_unit_debug(u, "Failed to parse socket value: %s", value); | |
2846 | return 0; | |
2847 | } | |
2848 | ||
2849 | fd = parse_fd(fdv); | |
2850 | if (fd < 0 || !fdset_contains(fds, fd)) { | |
2851 | log_unit_debug(u, "Invalid socket value: %s", fdv); | |
2852 | return 0; | |
2853 | } | |
2854 | ||
2855 | LIST_FOREACH(port, p, s->ports) | |
2856 | if (p->fd < 0 && | |
2857 | socket_address_is_netlink(&p->address, value)) { | |
2858 | p->fd = fdset_remove(fds, fd); | |
2859 | found = true; | |
2860 | break; | |
2861 | } | |
2862 | if (!found) | |
2863 | log_unit_debug(u, "No matching netlink socket found: %s", value); | |
2864 | ||
2865 | } else if (streq(key, "ffs")) { | |
2866 | _cleanup_free_ char *fdv = NULL; | |
2867 | bool found = false; | |
2868 | int fd; | |
2869 | ||
2870 | r = extract_first_word(&value, &fdv, NULL, 0); | |
2871 | if (r <= 0) { | |
2872 | log_unit_debug(u, "Failed to parse ffs value: %s", value); | |
2873 | return 0; | |
2874 | } | |
2875 | ||
2876 | fd = parse_fd(fdv); | |
2877 | if (fd < 0 || !fdset_contains(fds, fd)) { | |
2878 | log_unit_debug(u, "Invalid ffs value: %s", fdv); | |
2879 | return 0; | |
2880 | } | |
2881 | ||
2882 | LIST_FOREACH(port, p, s->ports) | |
2883 | if (p->fd < 0 && | |
2884 | p->type == SOCKET_USB_FUNCTION && | |
2885 | path_equal_or_inode_same(p->path, value, 0)) { | |
2886 | p->fd = fdset_remove(fds, fd); | |
2887 | found = true; | |
2888 | break; | |
2889 | } | |
2890 | if (!found) | |
2891 | log_unit_debug(u, "No matching ffs socket found: %s", value); | |
2892 | ||
2893 | } else if (streq(key, "trigger-ratelimit")) | |
2894 | (void) deserialize_ratelimit(&s->trigger_limit, key, value); | |
2895 | else | |
2896 | log_unit_debug(UNIT(s), "Unknown serialization key: %s", key); | |
2897 | ||
2898 | return 0; | |
2899 | } | |
2900 | ||
2901 | static void socket_distribute_fds(Unit *u, FDSet *fds) { | |
2902 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
2903 | ||
2904 | LIST_FOREACH(port, p, s->ports) { | |
2905 | int fd; | |
2906 | ||
2907 | if (p->type != SOCKET_SOCKET) | |
2908 | continue; | |
2909 | ||
2910 | if (p->fd >= 0) | |
2911 | continue; | |
2912 | ||
2913 | FDSET_FOREACH(fd, fds) { | |
2914 | if (socket_address_matches_fd(&p->address, fd)) { | |
2915 | p->fd = fdset_remove(fds, fd); | |
2916 | s->deserialized_state = SOCKET_LISTENING; | |
2917 | break; | |
2918 | } | |
2919 | } | |
2920 | } | |
2921 | } | |
2922 | ||
2923 | static UnitActiveState socket_active_state(Unit *u) { | |
2924 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
2925 | ||
2926 | return state_translation_table[s->state]; | |
2927 | } | |
2928 | ||
2929 | static const char *socket_sub_state_to_string(Unit *u) { | |
2930 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
2931 | ||
2932 | return socket_state_to_string(s->state); | |
2933 | } | |
2934 | ||
2935 | int socket_port_to_address(const SocketPort *p, char **ret) { | |
2936 | _cleanup_free_ char *address = NULL; | |
2937 | int r; | |
2938 | ||
2939 | assert(p); | |
2940 | assert(ret); | |
2941 | ||
2942 | switch (p->type) { | |
2943 | case SOCKET_SOCKET: { | |
2944 | r = socket_address_print(&p->address, &address); | |
2945 | if (r < 0) | |
2946 | return r; | |
2947 | ||
2948 | break; | |
2949 | } | |
2950 | ||
2951 | case SOCKET_SPECIAL: | |
2952 | case SOCKET_MQUEUE: | |
2953 | case SOCKET_FIFO: | |
2954 | case SOCKET_USB_FUNCTION: | |
2955 | address = strdup(p->path); | |
2956 | if (!address) | |
2957 | return -ENOMEM; | |
2958 | break; | |
2959 | ||
2960 | default: | |
2961 | assert_not_reached(); | |
2962 | } | |
2963 | ||
2964 | *ret = TAKE_PTR(address); | |
2965 | ||
2966 | return 0; | |
2967 | } | |
2968 | ||
2969 | const char* socket_port_type_to_string(SocketPort *p) { | |
2970 | assert(p); | |
2971 | ||
2972 | switch (p->type) { | |
2973 | ||
2974 | case SOCKET_SOCKET: | |
2975 | ||
2976 | switch (p->address.type) { | |
2977 | ||
2978 | case SOCK_STREAM: | |
2979 | return "Stream"; | |
2980 | ||
2981 | case SOCK_DGRAM: | |
2982 | return "Datagram"; | |
2983 | ||
2984 | case SOCK_SEQPACKET: | |
2985 | return "SequentialPacket"; | |
2986 | ||
2987 | case SOCK_RAW: | |
2988 | if (socket_address_family(&p->address) == AF_NETLINK) | |
2989 | return "Netlink"; | |
2990 | ||
2991 | _fallthrough_; | |
2992 | default: | |
2993 | return NULL; | |
2994 | } | |
2995 | ||
2996 | case SOCKET_SPECIAL: | |
2997 | return "Special"; | |
2998 | ||
2999 | case SOCKET_MQUEUE: | |
3000 | return "MessageQueue"; | |
3001 | ||
3002 | case SOCKET_FIFO: | |
3003 | return "FIFO"; | |
3004 | ||
3005 | case SOCKET_USB_FUNCTION: | |
3006 | return "USBFunction"; | |
3007 | ||
3008 | default: | |
3009 | return NULL; | |
3010 | } | |
3011 | } | |
3012 | ||
3013 | SocketType socket_port_type_from_string(const char *s) { | |
3014 | assert(s); | |
3015 | ||
3016 | if (STR_IN_SET(s, "Stream", "Datagram", "SequentialPacket", "Netlink")) | |
3017 | return SOCKET_SOCKET; | |
3018 | else if (streq(s, "Special")) | |
3019 | return SOCKET_SPECIAL; | |
3020 | else if (streq(s, "MessageQueue")) | |
3021 | return SOCKET_MQUEUE; | |
3022 | else if (streq(s, "FIFO")) | |
3023 | return SOCKET_FIFO; | |
3024 | else if (streq(s, "USBFunction")) | |
3025 | return SOCKET_USB_FUNCTION; | |
3026 | else | |
3027 | return _SOCKET_TYPE_INVALID; | |
3028 | } | |
3029 | ||
3030 | static bool socket_may_gc(Unit *u) { | |
3031 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
3032 | ||
3033 | return s->n_connections == 0; | |
3034 | } | |
3035 | ||
3036 | static int socket_accept_do(Socket *s, int fd) { | |
3037 | int cfd; | |
3038 | ||
3039 | assert(s); | |
3040 | assert(fd >= 0); | |
3041 | ||
3042 | cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); | |
3043 | if (cfd < 0) | |
3044 | /* Convert transient network errors into clean and well-defined EAGAIN */ | |
3045 | return ERRNO_IS_ACCEPT_AGAIN(errno) ? -EAGAIN : -errno; | |
3046 | ||
3047 | return cfd; | |
3048 | } | |
3049 | ||
3050 | static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) { | |
3051 | _cleanup_(pidref_done) PidRef pid = PIDREF_NULL; | |
3052 | _cleanup_close_pair_ int pair[2] = EBADF_PAIR; | |
3053 | int cfd, r; | |
3054 | ||
3055 | assert(s); | |
3056 | assert(p); | |
3057 | assert(fd >= 0); | |
3058 | ||
3059 | /* Similar to socket_address_listen_in_cgroup(), but for accept() rather than socket(): make sure that any | |
3060 | * connection socket is also properly associated with the cgroup. */ | |
3061 | ||
3062 | if (!IN_SET(p->address.sockaddr.sa.sa_family, AF_INET, AF_INET6)) | |
3063 | goto shortcut; | |
3064 | ||
3065 | if (bpf_program_supported() <= 0) | |
3066 | goto shortcut; | |
3067 | ||
3068 | if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0) | |
3069 | return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m"); | |
3070 | ||
3071 | r = unit_fork_helper_process(UNIT(s), "(sd-accept)", /* into_cgroup= */ true, &pid); | |
3072 | if (r < 0) | |
3073 | return log_unit_error_errno(UNIT(s), r, "Failed to fork off accept stub process: %m"); | |
3074 | if (r == 0) { | |
3075 | /* Child */ | |
3076 | ||
3077 | pair[0] = safe_close(pair[0]); | |
3078 | ||
3079 | cfd = socket_accept_do(s, fd); | |
3080 | if (cfd == -EAGAIN) /* spurious accept() */ | |
3081 | _exit(EXIT_SUCCESS); | |
3082 | if (cfd < 0) { | |
3083 | log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m"); | |
3084 | _exit(EXIT_FAILURE); | |
3085 | } | |
3086 | ||
3087 | r = send_one_fd(pair[1], cfd, 0); | |
3088 | if (r < 0) { | |
3089 | log_unit_error_errno(UNIT(s), r, "Failed to send connection socket to parent: %m"); | |
3090 | _exit(EXIT_FAILURE); | |
3091 | } | |
3092 | ||
3093 | _exit(EXIT_SUCCESS); | |
3094 | } | |
3095 | ||
3096 | pair[1] = safe_close(pair[1]); | |
3097 | cfd = receive_one_fd(pair[0], 0); | |
3098 | ||
3099 | /* We synchronously wait for the helper, as it shouldn't be slow */ | |
3100 | r = wait_for_terminate_and_check("(sd-accept)", pid.pid, WAIT_LOG_ABNORMAL); | |
3101 | if (r < 0) { | |
3102 | safe_close(cfd); | |
3103 | return r; | |
3104 | } | |
3105 | ||
3106 | /* If we received no fd, we got EIO here. If this happens with a process exit code of EXIT_SUCCESS | |
3107 | * this is a spurious accept(), let's convert that back to EAGAIN here. */ | |
3108 | if (cfd == -EIO) | |
3109 | return -EAGAIN; | |
3110 | if (cfd < 0) | |
3111 | return log_unit_error_errno(UNIT(s), cfd, "Failed to receive connection socket: %m"); | |
3112 | ||
3113 | return cfd; | |
3114 | ||
3115 | shortcut: | |
3116 | cfd = socket_accept_do(s, fd); | |
3117 | if (cfd == -EAGAIN) /* spurious accept(), skip it silently */ | |
3118 | return -EAGAIN; | |
3119 | if (cfd < 0) | |
3120 | return log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m"); | |
3121 | ||
3122 | return cfd; | |
3123 | } | |
3124 | ||
3125 | static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { | |
3126 | SocketPort *p = ASSERT_PTR(userdata); | |
3127 | int cfd = -EBADF; | |
3128 | ||
3129 | assert(fd >= 0); | |
3130 | ||
3131 | if (p->socket->state != SOCKET_LISTENING) | |
3132 | return 0; | |
3133 | ||
3134 | log_unit_debug(UNIT(p->socket), "Incoming traffic"); | |
3135 | ||
3136 | if (revents != EPOLLIN) { | |
3137 | if (revents & EPOLLHUP) | |
3138 | log_unit_error(UNIT(p->socket), "Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that."); | |
3139 | else | |
3140 | log_unit_error(UNIT(p->socket), "Got unexpected poll event (0x%x) on socket.", revents); | |
3141 | goto fail; | |
3142 | } | |
3143 | ||
3144 | if (p->socket->accept && | |
3145 | p->type == SOCKET_SOCKET && | |
3146 | socket_address_can_accept(&p->address)) { | |
3147 | ||
3148 | cfd = socket_accept_in_cgroup(p->socket, p, fd); | |
3149 | if (cfd == -EAGAIN) /* Spurious accept() */ | |
3150 | return 0; | |
3151 | if (cfd < 0) | |
3152 | goto fail; | |
3153 | ||
3154 | socket_apply_socket_options(p->socket, p, cfd); | |
3155 | } | |
3156 | ||
3157 | socket_enter_running(p->socket, cfd); | |
3158 | return 0; | |
3159 | ||
3160 | fail: | |
3161 | socket_enter_stop_pre(p->socket, SOCKET_FAILURE_RESOURCES); | |
3162 | return 0; | |
3163 | } | |
3164 | ||
3165 | static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) { | |
3166 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
3167 | SocketResult f; | |
3168 | ||
3169 | assert(pid >= 0); | |
3170 | ||
3171 | if (pid != s->control_pid.pid) | |
3172 | return; | |
3173 | ||
3174 | pidref_done(&s->control_pid); | |
3175 | ||
3176 | if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL)) | |
3177 | f = SOCKET_SUCCESS; | |
3178 | else if (code == CLD_EXITED) | |
3179 | f = SOCKET_FAILURE_EXIT_CODE; | |
3180 | else if (code == CLD_KILLED) | |
3181 | f = SOCKET_FAILURE_SIGNAL; | |
3182 | else if (code == CLD_DUMPED) | |
3183 | f = SOCKET_FAILURE_CORE_DUMP; | |
3184 | else | |
3185 | assert_not_reached(); | |
3186 | ||
3187 | if (s->control_command) { | |
3188 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); | |
3189 | ||
3190 | if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE) | |
3191 | f = SOCKET_SUCCESS; | |
3192 | } | |
3193 | ||
3194 | unit_log_process_exit( | |
3195 | u, | |
3196 | "Control process", | |
3197 | socket_exec_command_to_string(s->control_command_id), | |
3198 | f == SOCKET_SUCCESS, | |
3199 | code, status); | |
3200 | ||
3201 | if (s->result == SOCKET_SUCCESS) | |
3202 | s->result = f; | |
3203 | ||
3204 | if (s->control_command && | |
3205 | s->control_command->command_next && | |
3206 | f == SOCKET_SUCCESS) { | |
3207 | ||
3208 | log_unit_debug(u, "Running next command for state %s", socket_state_to_string(s->state)); | |
3209 | socket_run_next(s); | |
3210 | } else { | |
3211 | s->control_command = NULL; | |
3212 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
3213 | ||
3214 | /* No further commands for this step, so let's figure | |
3215 | * out what to do next */ | |
3216 | ||
3217 | log_unit_debug(u, "Got final SIGCHLD for state %s", socket_state_to_string(s->state)); | |
3218 | ||
3219 | switch (s->state) { | |
3220 | ||
3221 | case SOCKET_START_PRE: | |
3222 | if (f == SOCKET_SUCCESS) | |
3223 | socket_enter_start_open(s); | |
3224 | else | |
3225 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, f); | |
3226 | break; | |
3227 | ||
3228 | case SOCKET_START_CHOWN: | |
3229 | if (f == SOCKET_SUCCESS) | |
3230 | socket_enter_start_post(s); | |
3231 | else | |
3232 | socket_enter_stop_pre(s, f); | |
3233 | break; | |
3234 | ||
3235 | case SOCKET_START_POST: | |
3236 | if (f == SOCKET_SUCCESS) | |
3237 | socket_enter_listening(s); | |
3238 | else | |
3239 | socket_enter_stop_pre(s, f); | |
3240 | break; | |
3241 | ||
3242 | case SOCKET_STOP_PRE: | |
3243 | case SOCKET_STOP_PRE_SIGTERM: | |
3244 | case SOCKET_STOP_PRE_SIGKILL: | |
3245 | socket_enter_stop_post(s, f); | |
3246 | break; | |
3247 | ||
3248 | case SOCKET_STOP_POST: | |
3249 | case SOCKET_FINAL_SIGTERM: | |
3250 | case SOCKET_FINAL_SIGKILL: | |
3251 | socket_enter_dead(s, f); | |
3252 | break; | |
3253 | ||
3254 | case SOCKET_CLEANING: | |
3255 | ||
3256 | if (s->clean_result == SOCKET_SUCCESS) | |
3257 | s->clean_result = f; | |
3258 | ||
3259 | socket_enter_dead(s, SOCKET_SUCCESS); | |
3260 | break; | |
3261 | ||
3262 | default: | |
3263 | assert_not_reached(); | |
3264 | } | |
3265 | } | |
3266 | ||
3267 | /* Notify clients about changed exit status */ | |
3268 | unit_add_to_dbus_queue(u); | |
3269 | } | |
3270 | ||
3271 | static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { | |
3272 | Socket *s = ASSERT_PTR(SOCKET(userdata)); | |
3273 | ||
3274 | assert(s->timer_event_source == source); | |
3275 | ||
3276 | switch (s->state) { | |
3277 | ||
3278 | case SOCKET_START_PRE: | |
3279 | log_unit_warning(UNIT(s), "Starting timed out. Terminating."); | |
3280 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT); | |
3281 | break; | |
3282 | ||
3283 | case SOCKET_START_CHOWN: | |
3284 | case SOCKET_START_POST: | |
3285 | log_unit_warning(UNIT(s), "Starting timed out. Stopping."); | |
3286 | socket_enter_stop_pre(s, SOCKET_FAILURE_TIMEOUT); | |
3287 | break; | |
3288 | ||
3289 | case SOCKET_STOP_PRE: | |
3290 | log_unit_warning(UNIT(s), "Stopping timed out. Terminating."); | |
3291 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_FAILURE_TIMEOUT); | |
3292 | break; | |
3293 | ||
3294 | case SOCKET_STOP_PRE_SIGTERM: | |
3295 | if (s->kill_context.send_sigkill) { | |
3296 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); | |
3297 | socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_FAILURE_TIMEOUT); | |
3298 | } else { | |
3299 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL. Ignoring."); | |
3300 | socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT); | |
3301 | } | |
3302 | break; | |
3303 | ||
3304 | case SOCKET_STOP_PRE_SIGKILL: | |
3305 | log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring."); | |
3306 | socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT); | |
3307 | break; | |
3308 | ||
3309 | case SOCKET_STOP_POST: | |
3310 | log_unit_warning(UNIT(s), "Stopping timed out (2). Terminating."); | |
3311 | socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT); | |
3312 | break; | |
3313 | ||
3314 | case SOCKET_FINAL_SIGTERM: | |
3315 | if (s->kill_context.send_sigkill) { | |
3316 | log_unit_warning(UNIT(s), "Stopping timed out (2). Killing."); | |
3317 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_FAILURE_TIMEOUT); | |
3318 | } else { | |
3319 | log_unit_warning(UNIT(s), "Stopping timed out (2). Skipping SIGKILL. Ignoring."); | |
3320 | socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT); | |
3321 | } | |
3322 | break; | |
3323 | ||
3324 | case SOCKET_FINAL_SIGKILL: | |
3325 | log_unit_warning(UNIT(s), "Still around after SIGKILL (2). Entering failed mode."); | |
3326 | socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT); | |
3327 | break; | |
3328 | ||
3329 | case SOCKET_CLEANING: | |
3330 | log_unit_warning(UNIT(s), "Cleaning timed out. killing."); | |
3331 | ||
3332 | if (s->clean_result == SOCKET_SUCCESS) | |
3333 | s->clean_result = SOCKET_FAILURE_TIMEOUT; | |
3334 | ||
3335 | socket_enter_signal(s, SOCKET_FINAL_SIGKILL, 0); | |
3336 | break; | |
3337 | ||
3338 | default: | |
3339 | assert_not_reached(); | |
3340 | } | |
3341 | ||
3342 | return 0; | |
3343 | } | |
3344 | ||
3345 | int socket_collect_fds(Socket *s, int **ret) { | |
3346 | size_t n = 0, k = 0; | |
3347 | ||
3348 | assert(s); | |
3349 | assert(ret); | |
3350 | ||
3351 | /* Called from the service code for requesting our fds */ | |
3352 | ||
3353 | LIST_FOREACH(port, p, s->ports) { | |
3354 | if (p->fd >= 0) | |
3355 | n++; | |
3356 | n += p->n_auxiliary_fds; | |
3357 | } | |
3358 | ||
3359 | if (n == 0) { | |
3360 | *ret = NULL; | |
3361 | return 0; | |
3362 | } | |
3363 | ||
3364 | int *fds = new(int, n); | |
3365 | if (!fds) | |
3366 | return -ENOMEM; | |
3367 | ||
3368 | LIST_FOREACH(port, p, s->ports) { | |
3369 | if (p->fd >= 0) | |
3370 | fds[k++] = p->fd; | |
3371 | FOREACH_ARRAY(i, p->auxiliary_fds, p->n_auxiliary_fds) | |
3372 | fds[k++] = *i; | |
3373 | } | |
3374 | ||
3375 | assert(k == n); | |
3376 | ||
3377 | *ret = fds; | |
3378 | return (int) n; | |
3379 | } | |
3380 | ||
3381 | static void socket_reset_failed(Unit *u) { | |
3382 | Socket *s = SOCKET(u); | |
3383 | ||
3384 | assert(s); | |
3385 | ||
3386 | if (s->state == SOCKET_FAILED) | |
3387 | socket_set_state(s, SOCKET_DEAD); | |
3388 | ||
3389 | s->result = SOCKET_SUCCESS; | |
3390 | s->clean_result = SOCKET_SUCCESS; | |
3391 | } | |
3392 | ||
3393 | void socket_connection_unref(Socket *s) { | |
3394 | assert(s); | |
3395 | ||
3396 | /* The service is dead. Yay! | |
3397 | * | |
3398 | * This is strictly for one-instance-per-connection | |
3399 | * services. */ | |
3400 | ||
3401 | assert(s->n_connections > 0); | |
3402 | s->n_connections--; | |
3403 | ||
3404 | log_unit_debug(UNIT(s), "One connection closed, %u left.", s->n_connections); | |
3405 | } | |
3406 | ||
3407 | static void socket_trigger_notify(Unit *u, Unit *other) { | |
3408 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
3409 | ||
3410 | assert(other); | |
3411 | ||
3412 | /* Filter out invocations with bogus state */ | |
3413 | assert(UNIT_IS_LOAD_COMPLETE(other->load_state)); | |
3414 | ||
3415 | Service *service = ASSERT_PTR(SERVICE(other)); | |
3416 | ||
3417 | /* Don't propagate state changes from the service if we are already down */ | |
3418 | if (!IN_SET(s->state, SOCKET_RUNNING, SOCKET_LISTENING)) | |
3419 | return; | |
3420 | ||
3421 | /* We don't care for the service state if we are in Accept=yes mode */ | |
3422 | if (s->accept) | |
3423 | return; | |
3424 | ||
3425 | /* Propagate start limit hit state */ | |
3426 | if (other->start_limit_hit) { | |
3427 | socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT); | |
3428 | return; | |
3429 | } | |
3430 | ||
3431 | /* Don't propagate anything if there's still a job queued */ | |
3432 | if (other->job) | |
3433 | return; | |
3434 | ||
3435 | if (!SOCKET_SERVICE_IS_ACTIVE(service, /* allow_finalize = */ true)) | |
3436 | socket_enter_listening(s); | |
3437 | ||
3438 | if (SERVICE(other)->state == SERVICE_RUNNING) | |
3439 | socket_set_state(s, SOCKET_RUNNING); | |
3440 | } | |
3441 | ||
3442 | static void socket_handoff_timestamp( | |
3443 | Unit *u, | |
3444 | const struct ucred *ucred, | |
3445 | const dual_timestamp *ts) { | |
3446 | ||
3447 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
3448 | ||
3449 | assert(ucred); | |
3450 | assert(ts); | |
3451 | ||
3452 | if (s->control_pid.pid == ucred->pid && s->control_command) { | |
3453 | exec_status_handoff(&s->control_command->exec_status, ucred, ts); | |
3454 | unit_add_to_dbus_queue(u); | |
3455 | } | |
3456 | } | |
3457 | ||
3458 | static int socket_get_timeout(Unit *u, usec_t *timeout) { | |
3459 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
3460 | usec_t t; | |
3461 | int r; | |
3462 | ||
3463 | if (!s->timer_event_source) | |
3464 | return 0; | |
3465 | ||
3466 | r = sd_event_source_get_time(s->timer_event_source, &t); | |
3467 | if (r < 0) | |
3468 | return r; | |
3469 | if (t == USEC_INFINITY) | |
3470 | return 0; | |
3471 | ||
3472 | *timeout = t; | |
3473 | return 1; | |
3474 | } | |
3475 | ||
3476 | const char* socket_fdname(Socket *s) { | |
3477 | assert(s); | |
3478 | ||
3479 | /* Returns the name to use for $LISTEN_FDNAMES. If the user didn't specify anything specifically, | |
3480 | * use the socket unit's name as fallback for Accept=no sockets, "connection" otherwise. */ | |
3481 | ||
3482 | if (s->fdname) | |
3483 | return s->fdname; | |
3484 | ||
3485 | if (s->accept) | |
3486 | return "connection"; | |
3487 | ||
3488 | return UNIT(s)->id; | |
3489 | } | |
3490 | ||
3491 | static PidRef* socket_control_pid(Unit *u) { | |
3492 | return &ASSERT_PTR(SOCKET(u))->control_pid; | |
3493 | } | |
3494 | ||
3495 | static int socket_clean(Unit *u, ExecCleanMask mask) { | |
3496 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
3497 | _cleanup_strv_free_ char **l = NULL; | |
3498 | int r; | |
3499 | ||
3500 | assert(mask != 0); | |
3501 | ||
3502 | if (s->state != SOCKET_DEAD) | |
3503 | return -EBUSY; | |
3504 | ||
3505 | r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l); | |
3506 | if (r < 0) | |
3507 | return r; | |
3508 | ||
3509 | if (strv_isempty(l)) | |
3510 | return -EUNATCH; | |
3511 | ||
3512 | socket_unwatch_control_pid(s); | |
3513 | s->clean_result = SOCKET_SUCCESS; | |
3514 | s->control_command = NULL; | |
3515 | s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID; | |
3516 | ||
3517 | r = socket_arm_timer(s, /* relative= */ true, s->exec_context.timeout_clean_usec); | |
3518 | if (r < 0) { | |
3519 | log_unit_warning_errno(u, r, "Failed to install timer: %m"); | |
3520 | goto fail; | |
3521 | } | |
3522 | ||
3523 | r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); | |
3524 | if (r < 0) { | |
3525 | log_unit_warning_errno(u, r, "Failed to spawn cleaning task: %m"); | |
3526 | goto fail; | |
3527 | } | |
3528 | ||
3529 | socket_set_state(s, SOCKET_CLEANING); | |
3530 | return 0; | |
3531 | ||
3532 | fail: | |
3533 | s->clean_result = SOCKET_FAILURE_RESOURCES; | |
3534 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); | |
3535 | return r; | |
3536 | } | |
3537 | ||
3538 | static int socket_can_clean(Unit *u, ExecCleanMask *ret) { | |
3539 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
3540 | ||
3541 | return exec_context_get_clean_mask(&s->exec_context, ret); | |
3542 | } | |
3543 | ||
3544 | static int socket_can_start(Unit *u) { | |
3545 | Socket *s = ASSERT_PTR(SOCKET(u)); | |
3546 | int r; | |
3547 | ||
3548 | r = unit_test_start_limit(u); | |
3549 | if (r < 0) { | |
3550 | socket_enter_dead(s, SOCKET_FAILURE_START_LIMIT_HIT); | |
3551 | return r; | |
3552 | } | |
3553 | ||
3554 | return 1; | |
3555 | } | |
3556 | ||
3557 | static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = { | |
3558 | [SOCKET_EXEC_START_PRE] = "ExecStartPre", | |
3559 | [SOCKET_EXEC_START_CHOWN] = "ExecStartChown", | |
3560 | [SOCKET_EXEC_START_POST] = "ExecStartPost", | |
3561 | [SOCKET_EXEC_STOP_PRE] = "ExecStopPre", | |
3562 | [SOCKET_EXEC_STOP_POST] = "ExecStopPost", | |
3563 | }; | |
3564 | ||
3565 | DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand); | |
3566 | ||
3567 | static const char* const socket_result_table[_SOCKET_RESULT_MAX] = { | |
3568 | [SOCKET_SUCCESS] = "success", | |
3569 | [SOCKET_FAILURE_RESOURCES] = "resources", | |
3570 | [SOCKET_FAILURE_TIMEOUT] = "timeout", | |
3571 | [SOCKET_FAILURE_EXIT_CODE] = "exit-code", | |
3572 | [SOCKET_FAILURE_SIGNAL] = "signal", | |
3573 | [SOCKET_FAILURE_CORE_DUMP] = "core-dump", | |
3574 | [SOCKET_FAILURE_START_LIMIT_HIT] = "start-limit-hit", | |
3575 | [SOCKET_FAILURE_TRIGGER_LIMIT_HIT] = "trigger-limit-hit", | |
3576 | [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT] = "service-start-limit-hit", | |
3577 | }; | |
3578 | ||
3579 | DEFINE_STRING_TABLE_LOOKUP(socket_result, SocketResult); | |
3580 | ||
3581 | static const char* const socket_timestamping_table[_SOCKET_TIMESTAMPING_MAX] = { | |
3582 | [SOCKET_TIMESTAMPING_OFF] = "off", | |
3583 | [SOCKET_TIMESTAMPING_US] = "us", | |
3584 | [SOCKET_TIMESTAMPING_NS] = "ns", | |
3585 | }; | |
3586 | ||
3587 | DEFINE_STRING_TABLE_LOOKUP(socket_timestamping, SocketTimestamping); | |
3588 | ||
3589 | SocketTimestamping socket_timestamping_from_string_harder(const char *p) { | |
3590 | SocketTimestamping t; | |
3591 | int r; | |
3592 | ||
3593 | if (!p) | |
3594 | return _SOCKET_TIMESTAMPING_INVALID; | |
3595 | ||
3596 | t = socket_timestamping_from_string(p); | |
3597 | if (t >= 0) | |
3598 | return t; | |
3599 | ||
3600 | /* Let's alternatively support the various other aliases parse_time() accepts for ns and µs here, | |
3601 | * too. */ | |
3602 | if (streq(p, "nsec")) | |
3603 | return SOCKET_TIMESTAMPING_NS; | |
3604 | if (STR_IN_SET(p, "usec", "µs", "μs")) /* Accept both small greek letter mu + micro sign unicode codepoints */ | |
3605 | return SOCKET_TIMESTAMPING_US; | |
3606 | ||
3607 | r = parse_boolean(p); | |
3608 | if (r < 0) | |
3609 | return _SOCKET_TIMESTAMPING_INVALID; | |
3610 | ||
3611 | return r ? SOCKET_TIMESTAMPING_NS : SOCKET_TIMESTAMPING_OFF; /* If boolean yes, default to ns accuracy */ | |
3612 | } | |
3613 | ||
3614 | const UnitVTable socket_vtable = { | |
3615 | .object_size = sizeof(Socket), | |
3616 | .exec_context_offset = offsetof(Socket, exec_context), | |
3617 | .cgroup_context_offset = offsetof(Socket, cgroup_context), | |
3618 | .kill_context_offset = offsetof(Socket, kill_context), | |
3619 | .exec_runtime_offset = offsetof(Socket, exec_runtime), | |
3620 | .cgroup_runtime_offset = offsetof(Socket, cgroup_runtime), | |
3621 | ||
3622 | .sections = | |
3623 | "Unit\0" | |
3624 | "Socket\0" | |
3625 | "Install\0", | |
3626 | .private_section = "Socket", | |
3627 | ||
3628 | .can_transient = true, | |
3629 | .can_trigger = true, | |
3630 | .can_fail = true, | |
3631 | ||
3632 | .init = socket_init, | |
3633 | .done = socket_done, | |
3634 | .load = socket_load, | |
3635 | ||
3636 | .coldplug = socket_coldplug, | |
3637 | ||
3638 | .dump = socket_dump, | |
3639 | ||
3640 | .start = socket_start, | |
3641 | .stop = socket_stop, | |
3642 | ||
3643 | .clean = socket_clean, | |
3644 | .can_clean = socket_can_clean, | |
3645 | ||
3646 | .get_timeout = socket_get_timeout, | |
3647 | ||
3648 | .serialize = socket_serialize, | |
3649 | .deserialize_item = socket_deserialize_item, | |
3650 | .distribute_fds = socket_distribute_fds, | |
3651 | ||
3652 | .active_state = socket_active_state, | |
3653 | .sub_state_to_string = socket_sub_state_to_string, | |
3654 | ||
3655 | .will_restart = unit_will_restart_default, | |
3656 | ||
3657 | .may_gc = socket_may_gc, | |
3658 | ||
3659 | .sigchld_event = socket_sigchld_event, | |
3660 | ||
3661 | .trigger_notify = socket_trigger_notify, | |
3662 | ||
3663 | .reset_failed = socket_reset_failed, | |
3664 | ||
3665 | .notify_handoff_timestamp = socket_handoff_timestamp, | |
3666 | ||
3667 | .control_pid = socket_control_pid, | |
3668 | ||
3669 | .bus_set_property = bus_socket_set_property, | |
3670 | .bus_commit_properties = bus_socket_commit_properties, | |
3671 | ||
3672 | .status_message_formats = { | |
3673 | .finished_start_job = { | |
3674 | [JOB_DONE] = "Listening on %s.", | |
3675 | [JOB_FAILED] = "Failed to listen on %s.", | |
3676 | [JOB_TIMEOUT] = "Timed out starting %s.", | |
3677 | }, | |
3678 | .finished_stop_job = { | |
3679 | [JOB_DONE] = "Closed %s.", | |
3680 | [JOB_FAILED] = "Failed stopping %s.", | |
3681 | [JOB_TIMEOUT] = "Timed out stopping %s.", | |
3682 | }, | |
3683 | }, | |
3684 | ||
3685 | .can_start = socket_can_start, | |
3686 | }; |