1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <netinet/tcp.h>
11 #include <linux/sctp.h>
13 #include "alloc-util.h"
14 #include "bpf-firewall.h"
15 #include "bus-error.h"
18 #include "dbus-socket.h"
19 #include "dbus-unit.h"
21 #include "exit-status.h"
23 #include "format-util.h"
25 #include "in-addr-util.h"
27 #include "ip-protocol-list.h"
31 #include "parse-util.h"
32 #include "path-util.h"
33 #include "process-util.h"
34 #include "selinux-util.h"
35 #include "serialize.h"
36 #include "signal-util.h"
37 #include "smack-util.h"
40 #include "string-table.h"
41 #include "string-util.h"
43 #include "unit-name.h"
45 #include "user-util.h"
51 union sockaddr_union peer;
55 static const UnitActiveState state_translation_table[_SOCKET_STATE_MAX] = {
56 [SOCKET_DEAD] = UNIT_INACTIVE,
57 [SOCKET_START_PRE] = UNIT_ACTIVATING,
58 [SOCKET_START_CHOWN] = UNIT_ACTIVATING,
59 [SOCKET_START_POST] = UNIT_ACTIVATING,
60 [SOCKET_LISTENING] = UNIT_ACTIVE,
61 [SOCKET_RUNNING] = UNIT_ACTIVE,
62 [SOCKET_STOP_PRE] = UNIT_DEACTIVATING,
63 [SOCKET_STOP_PRE_SIGTERM] = UNIT_DEACTIVATING,
64 [SOCKET_STOP_PRE_SIGKILL] = UNIT_DEACTIVATING,
65 [SOCKET_STOP_POST] = UNIT_DEACTIVATING,
66 [SOCKET_FINAL_SIGTERM] = UNIT_DEACTIVATING,
67 [SOCKET_FINAL_SIGKILL] = UNIT_DEACTIVATING,
68 [SOCKET_FAILED] = UNIT_FAILED,
69 [SOCKET_CLEANING] = UNIT_MAINTENANCE,
72 static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata);
73 static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
75 static void socket_init(Unit *u) {
76 Socket *s = SOCKET(u);
79 assert(u->load_state == UNIT_STUB);
81 s->backlog = SOMAXCONN;
82 s->timeout_usec = u->manager->default_timeout_start_usec;
83 s->directory_mode = 0755;
84 s->socket_mode = 0666;
86 s->max_connections = 64;
93 s->exec_context.std_output = u->manager->default_std_output;
94 s->exec_context.std_error = u->manager->default_std_error;
96 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
98 s->trigger_limit.interval = USEC_INFINITY;
99 s->trigger_limit.burst = (unsigned) -1;
102 static void socket_unwatch_control_pid(Socket *s) {
105 if (s->control_pid <= 0)
108 unit_unwatch_pid(UNIT(s), s->control_pid);
112 static void socket_cleanup_fd_list(SocketPort *p) {
115 close_many(p->auxiliary_fds, p->n_auxiliary_fds);
116 p->auxiliary_fds = mfree(p->auxiliary_fds);
117 p->n_auxiliary_fds = 0;
120 void socket_free_ports(Socket *s) {
125 while ((p = s->ports)) {
126 LIST_REMOVE(port, s->ports, p);
128 sd_event_source_unref(p->event_source);
130 socket_cleanup_fd_list(p);
137 static void socket_done(Unit *u) {
138 Socket *s = SOCKET(u);
143 socket_free_ports(s);
145 while ((p = set_steal_first(s->peers_by_address)))
148 s->peers_by_address = set_free(s->peers_by_address);
150 s->exec_runtime = exec_runtime_unref(s->exec_runtime, false);
151 exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
152 s->control_command = NULL;
154 dynamic_creds_unref(&s->dynamic_creds);
156 socket_unwatch_control_pid(s);
158 unit_ref_unset(&s->service);
160 s->tcp_congestion = mfree(s->tcp_congestion);
161 s->bind_to_device = mfree(s->bind_to_device);
163 s->smack = mfree(s->smack);
164 s->smack_ip_in = mfree(s->smack_ip_in);
165 s->smack_ip_out = mfree(s->smack_ip_out);
167 strv_free(s->symlinks);
169 s->user = mfree(s->user);
170 s->group = mfree(s->group);
172 s->fdname = mfree(s->fdname);
174 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
177 static int socket_arm_timer(Socket *s, usec_t usec) {
182 if (s->timer_event_source) {
183 r = sd_event_source_set_time(s->timer_event_source, usec);
187 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
190 if (usec == USEC_INFINITY)
193 r = sd_event_add_time(
194 UNIT(s)->manager->event,
195 &s->timer_event_source,
198 socket_dispatch_timer, s);
202 (void) sd_event_source_set_description(s->timer_event_source, "socket-timer");
207 int socket_instantiate_service(Socket *s) {
208 _cleanup_free_ char *prefix = NULL, *name = NULL;
214 /* This fills in s->service if it isn't filled in yet. For
215 * Accept=yes sockets we create the next connection service
216 * here. For Accept=no this is mostly a NOP since the service
217 * is figured out at load time anyway. */
219 if (UNIT_DEREF(s->service))
225 r = unit_name_to_prefix(UNIT(s)->id, &prefix);
229 if (asprintf(&name, "%s@%u.service", prefix, s->n_accepted) < 0)
232 r = manager_load_unit(UNIT(s)->manager, name, NULL, NULL, &u);
236 unit_ref_set(&s->service, UNIT(s), u);
238 return unit_add_two_dependencies(UNIT(s), UNIT_BEFORE, UNIT_TRIGGERS, u, false, UNIT_DEPENDENCY_IMPLICIT);
241 static bool have_non_accept_socket(Socket *s) {
249 LIST_FOREACH(port, p, s->ports) {
251 if (p->type != SOCKET_SOCKET)
254 if (!socket_address_can_accept(&p->address))
261 static int socket_add_mount_dependencies(Socket *s) {
267 LIST_FOREACH(port, p, s->ports) {
268 const char *path = NULL;
270 if (p->type == SOCKET_SOCKET)
271 path = socket_address_get_path(&p->address);
272 else if (IN_SET(p->type, SOCKET_FIFO, SOCKET_SPECIAL, SOCKET_USB_FUNCTION))
278 r = unit_require_mounts_for(UNIT(s), path, UNIT_DEPENDENCY_FILE);
286 static int socket_add_device_dependencies(Socket *s) {
291 if (!s->bind_to_device || streq(s->bind_to_device, "lo"))
294 t = strjoina("/sys/subsystem/net/devices/", s->bind_to_device);
295 return unit_add_node_dependency(UNIT(s), t, UNIT_BINDS_TO, UNIT_DEPENDENCY_FILE);
298 static int socket_add_default_dependencies(Socket *s) {
302 if (!UNIT(s)->default_dependencies)
305 r = unit_add_dependency_by_name(UNIT(s), UNIT_BEFORE, SPECIAL_SOCKETS_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
309 if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) {
310 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
315 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
318 _pure_ static bool socket_has_exec(Socket *s) {
322 for (i = 0; i < _SOCKET_EXEC_COMMAND_MAX; i++)
323 if (s->exec_command[i])
329 static int socket_add_extras(Socket *s) {
335 /* Pick defaults for the trigger limit, if nothing was explicitly configured. We pick a relatively high limit
336 * in Accept=yes mode, and a lower limit for Accept=no. Reason: in Accept=yes mode we are invoking accept()
337 * ourselves before the trigger limit can hit, thus incoming connections are taken off the socket queue quickly
338 * and reliably. This is different for Accept=no, where the spawned service has to take the incoming traffic
339 * off the queues, which it might not necessarily do. Moreover, while Accept=no services are supposed to
340 * process whatever is queued in one go, and thus should normally never have to be started frequently. This is
341 * different for Accept=yes where each connection is processed by a new service instance, and thus frequent
342 * service starts are typical. */
344 if (s->trigger_limit.interval == USEC_INFINITY)
345 s->trigger_limit.interval = 2 * USEC_PER_SEC;
347 if (s->trigger_limit.burst == (unsigned) -1) {
349 s->trigger_limit.burst = 200;
351 s->trigger_limit.burst = 20;
354 if (have_non_accept_socket(s)) {
356 if (!UNIT_DEREF(s->service)) {
359 r = unit_load_related_unit(u, ".service", &x);
363 unit_ref_set(&s->service, u, x);
366 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true, UNIT_DEPENDENCY_IMPLICIT);
371 r = socket_add_mount_dependencies(s);
375 r = socket_add_device_dependencies(s);
379 r = unit_patch_contexts(u);
383 if (socket_has_exec(s)) {
384 r = unit_add_exec_dependencies(u, &s->exec_context);
389 r = unit_set_default_slice(u);
393 r = socket_add_default_dependencies(s);
400 static const char *socket_find_symlink_target(Socket *s) {
401 const char *found = NULL;
404 LIST_FOREACH(port, p, s->ports) {
405 const char *f = NULL;
414 f = socket_address_get_path(&p->address);
432 static int socket_verify(Socket *s) {
434 assert(UNIT(s)->load_state == UNIT_LOADED);
437 log_unit_error(UNIT(s), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing.");
441 if (s->accept && have_non_accept_socket(s)) {
442 log_unit_error(UNIT(s), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing.");
446 if (s->accept && s->max_connections <= 0) {
447 log_unit_error(UNIT(s), "MaxConnection= setting too small. Refusing.");
451 if (s->accept && UNIT_DEREF(s->service)) {
452 log_unit_error(UNIT(s), "Explicit service configuration for accepting socket units not supported. Refusing.");
456 if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) {
457 log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing.");
461 if (!strv_isempty(s->symlinks) && !socket_find_symlink_target(s)) {
462 log_unit_error(UNIT(s), "Unit has symlinks set but none or more than one node in the file system. Refusing.");
469 static void peer_address_hash_func(const SocketPeer *s, struct siphash *state) {
472 if (s->peer.sa.sa_family == AF_INET)
473 siphash24_compress(&s->peer.in.sin_addr, sizeof(s->peer.in.sin_addr), state);
474 else if (s->peer.sa.sa_family == AF_INET6)
475 siphash24_compress(&s->peer.in6.sin6_addr, sizeof(s->peer.in6.sin6_addr), state);
476 else if (s->peer.sa.sa_family == AF_VSOCK)
477 siphash24_compress(&s->peer.vm.svm_cid, sizeof(s->peer.vm.svm_cid), state);
479 assert_not_reached("Unknown address family.");
482 static int peer_address_compare_func(const SocketPeer *x, const SocketPeer *y) {
485 r = CMP(x->peer.sa.sa_family, y->peer.sa.sa_family);
489 switch(x->peer.sa.sa_family) {
491 return memcmp(&x->peer.in.sin_addr, &y->peer.in.sin_addr, sizeof(x->peer.in.sin_addr));
493 return memcmp(&x->peer.in6.sin6_addr, &y->peer.in6.sin6_addr, sizeof(x->peer.in6.sin6_addr));
495 return CMP(x->peer.vm.svm_cid, y->peer.vm.svm_cid);
497 assert_not_reached("Black sheep in the family!");
500 DEFINE_PRIVATE_HASH_OPS(peer_address_hash_ops, SocketPeer, peer_address_hash_func, peer_address_compare_func);
502 static int socket_load(Unit *u) {
503 Socket *s = SOCKET(u);
507 assert(u->load_state == UNIT_STUB);
509 r = set_ensure_allocated(&s->peers_by_address, &peer_address_hash_ops);
513 r = unit_load_fragment_and_dropin(u, true);
517 if (u->load_state != UNIT_LOADED)
520 /* This is a new unit? Then let's add in some extras */
521 r = socket_add_extras(s);
525 return socket_verify(s);
528 static SocketPeer *socket_peer_new(void) {
531 p = new0(SocketPeer, 1);
540 static SocketPeer *socket_peer_free(SocketPeer *p) {
544 set_remove(p->socket->peers_by_address, p);
549 DEFINE_TRIVIAL_REF_UNREF_FUNC(SocketPeer, socket_peer, socket_peer_free);
551 int socket_acquire_peer(Socket *s, int fd, SocketPeer **p) {
552 _cleanup_(socket_peer_unrefp) SocketPeer *remote = NULL;
553 SocketPeer sa = {}, *i;
554 socklen_t salen = sizeof(sa.peer);
560 r = getpeername(fd, &sa.peer.sa, &salen);
562 return log_unit_error_errno(UNIT(s), errno, "getpeername failed: %m");
564 if (!IN_SET(sa.peer.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) {
569 i = set_get(s->peers_by_address, &sa);
571 *p = socket_peer_ref(i);
575 remote = socket_peer_new();
579 remote->peer = sa.peer;
580 remote->peer_salen = salen;
582 r = set_put(s->peers_by_address, remote);
588 *p = TAKE_PTR(remote);
593 _const_ static const char* listen_lookup(int family, int type) {
595 if (family == AF_NETLINK)
596 return "ListenNetlink";
598 if (type == SOCK_STREAM)
599 return "ListenStream";
600 else if (type == SOCK_DGRAM)
601 return "ListenDatagram";
602 else if (type == SOCK_SEQPACKET)
603 return "ListenSequentialPacket";
605 assert_not_reached("Unknown socket type");
609 static void socket_dump(Unit *u, FILE *f, const char *prefix) {
610 char time_string[FORMAT_TIMESPAN_MAX];
612 Socket *s = SOCKET(u);
614 const char *prefix2, *str;
619 prefix = strempty(prefix);
620 prefix2 = strjoina(prefix, "\t");
623 "%sSocket State: %s\n"
625 "%sClean Result: %s\n"
626 "%sBindIPv6Only: %s\n"
628 "%sSocketMode: %04o\n"
629 "%sDirectoryMode: %04o\n"
633 "%sTransparent: %s\n"
635 "%sPassCredentials: %s\n"
636 "%sPassSecurity: %s\n"
637 "%sTCPCongestion: %s\n"
638 "%sRemoveOnStop: %s\n"
640 "%sFileDescriptorName: %s\n"
641 "%sSELinuxContextFromNet: %s\n",
642 prefix, socket_state_to_string(s->state),
643 prefix, socket_result_to_string(s->result),
644 prefix, socket_result_to_string(s->clean_result),
645 prefix, socket_address_bind_ipv6_only_to_string(s->bind_ipv6_only),
647 prefix, s->socket_mode,
648 prefix, s->directory_mode,
649 prefix, yes_no(s->keep_alive),
650 prefix, yes_no(s->no_delay),
651 prefix, yes_no(s->free_bind),
652 prefix, yes_no(s->transparent),
653 prefix, yes_no(s->broadcast),
654 prefix, yes_no(s->pass_cred),
655 prefix, yes_no(s->pass_sec),
656 prefix, strna(s->tcp_congestion),
657 prefix, yes_no(s->remove_on_stop),
658 prefix, yes_no(s->writable),
659 prefix, socket_fdname(s),
660 prefix, yes_no(s->selinux_context_from_net));
662 if (s->control_pid > 0)
664 "%sControl PID: "PID_FMT"\n",
665 prefix, s->control_pid);
667 if (s->bind_to_device)
669 "%sBindToDevice: %s\n",
670 prefix, s->bind_to_device);
675 "%sNConnections: %u\n"
676 "%sMaxConnections: %u\n"
677 "%sMaxConnectionsPerSource: %u\n",
678 prefix, s->n_accepted,
679 prefix, s->n_connections,
680 prefix, s->max_connections,
681 prefix, s->max_connections_per_source);
683 if (s->priority >= 0)
686 prefix, s->priority);
688 if (s->receive_buffer > 0)
690 "%sReceiveBuffer: %zu\n",
691 prefix, s->receive_buffer);
693 if (s->send_buffer > 0)
695 "%sSendBuffer: %zu\n",
696 prefix, s->send_buffer);
708 if (s->pipe_size > 0)
711 prefix, s->pipe_size);
718 if (s->mq_maxmsg > 0)
720 "%sMessageQueueMaxMessages: %li\n",
721 prefix, s->mq_maxmsg);
723 if (s->mq_msgsize > 0)
725 "%sMessageQueueMessageSize: %li\n",
726 prefix, s->mq_msgsize);
731 prefix, yes_no(s->reuse_port));
735 "%sSmackLabel: %s\n",
740 "%sSmackLabelIPIn: %s\n",
741 prefix, s->smack_ip_in);
745 "%sSmackLabelIPOut: %s\n",
746 prefix, s->smack_ip_out);
748 if (!isempty(s->user) || !isempty(s->group))
751 "%sSocketGroup: %s\n",
752 prefix, strna(s->user),
753 prefix, strna(s->group));
755 if (s->keep_alive_time > 0)
757 "%sKeepAliveTimeSec: %s\n",
758 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->keep_alive_time, USEC_PER_SEC));
760 if (s->keep_alive_interval > 0)
762 "%sKeepAliveIntervalSec: %s\n",
763 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->keep_alive_interval, USEC_PER_SEC));
765 if (s->keep_alive_cnt > 0)
767 "%sKeepAliveProbes: %u\n",
768 prefix, s->keep_alive_cnt);
770 if (s->defer_accept > 0)
772 "%sDeferAcceptSec: %s\n",
773 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->defer_accept, USEC_PER_SEC));
775 LIST_FOREACH(port, p, s->ports) {
778 case SOCKET_SOCKET: {
779 _cleanup_free_ char *k = NULL;
783 r = socket_address_print(&p->address, &k);
785 t = strerror_safe(r);
789 fprintf(f, "%s%s: %s\n", prefix, listen_lookup(socket_address_family(&p->address), p->address.type), t);
793 fprintf(f, "%sListenSpecial: %s\n", prefix, p->path);
795 case SOCKET_USB_FUNCTION:
796 fprintf(f, "%sListenUSBFunction: %s\n", prefix, p->path);
799 fprintf(f, "%sListenMessageQueue: %s\n", prefix, p->path);
802 fprintf(f, "%sListenFIFO: %s\n", prefix, p->path);
807 "%sTriggerLimitIntervalSec: %s\n"
808 "%sTriggerLimitBurst: %u\n",
809 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->trigger_limit.interval, USEC_PER_SEC),
810 prefix, s->trigger_limit.burst);
812 str = ip_protocol_to_name(s->socket_protocol);
814 fprintf(f, "%sSocketProtocol: %s\n", prefix, str);
816 if (!strv_isempty(s->symlinks)) {
819 fprintf(f, "%sSymlinks:", prefix);
820 STRV_FOREACH(q, s->symlinks)
821 fprintf(f, " %s", *q);
827 "%sTimeoutSec: %s\n",
828 prefix, format_timespan(time_string, FORMAT_TIMESPAN_MAX, s->timeout_usec, USEC_PER_SEC));
830 exec_context_dump(&s->exec_context, f, prefix);
831 kill_context_dump(&s->kill_context, f, prefix);
833 for (c = 0; c < _SOCKET_EXEC_COMMAND_MAX; c++) {
834 if (!s->exec_command[c])
837 fprintf(f, "%s-> %s:\n",
838 prefix, socket_exec_command_to_string(c));
840 exec_command_dump_list(s->exec_command[c], f, prefix2);
843 cgroup_context_dump(UNIT(s), f, prefix);
846 static int instance_from_socket(int fd, unsigned nr, char **instance) {
849 union sockaddr_union local, remote;
855 if (getsockname(fd, &local.sa, &l) < 0)
859 if (getpeername(fd, &remote.sa, &l) < 0)
862 switch (local.sa.sa_family) {
866 a = be32toh(local.in.sin_addr.s_addr),
867 b = be32toh(remote.in.sin_addr.s_addr);
870 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
872 a >> 24, (a >> 16) & 0xFF, (a >> 8) & 0xFF, a & 0xFF,
873 be16toh(local.in.sin_port),
874 b >> 24, (b >> 16) & 0xFF, (b >> 8) & 0xFF, b & 0xFF,
875 be16toh(remote.in.sin_port)) < 0)
882 static const unsigned char ipv4_prefix[] = {
883 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
886 if (memcmp(&local.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0 &&
887 memcmp(&remote.in6.sin6_addr, ipv4_prefix, sizeof(ipv4_prefix)) == 0) {
889 *a = local.in6.sin6_addr.s6_addr+12,
890 *b = remote.in6.sin6_addr.s6_addr+12;
893 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
895 a[0], a[1], a[2], a[3],
896 be16toh(local.in6.sin6_port),
897 b[0], b[1], b[2], b[3],
898 be16toh(remote.in6.sin6_port)) < 0)
901 char a[INET6_ADDRSTRLEN], b[INET6_ADDRSTRLEN];
906 inet_ntop(AF_INET6, &local.in6.sin6_addr, a, sizeof(a)),
907 be16toh(local.in6.sin6_port),
908 inet_ntop(AF_INET6, &remote.in6.sin6_addr, b, sizeof(b)),
909 be16toh(remote.in6.sin6_port)) < 0)
920 k = getpeercred(fd, &ucred);
923 "%u-"PID_FMT"-"UID_FMT,
924 nr, ucred.pid, ucred.uid) < 0)
926 } else if (k == -ENODATA) {
927 /* This handles the case where somebody is
928 * connecting from another pid/uid namespace
929 * (e.g. from outside of our container). */
944 local.vm.svm_cid, local.vm.svm_port,
945 remote.vm.svm_cid, remote.vm.svm_port) < 0)
951 assert_not_reached("Unhandled socket type.");
958 static void socket_close_fds(Socket *s) {
964 LIST_FOREACH(port, p, s->ports) {
967 was_open = p->fd >= 0;
969 p->event_source = sd_event_source_unref(p->event_source);
970 p->fd = safe_close(p->fd);
971 socket_cleanup_fd_list(p);
973 /* One little note: we should normally not delete any sockets in the file system here! After all some
974 * other process we spawned might still have a reference of this fd and wants to continue to use
975 * it. Therefore we normally delete sockets in the file system before we create a new one, not after we
976 * stopped using one! That all said, if the user explicitly requested this, we'll delete them here
977 * anyway, but only then. */
979 if (!was_open || !s->remove_on_stop)
985 (void) unlink(p->path);
989 (void) mq_unlink(p->path);
993 (void) socket_address_unlink(&p->address);
1001 if (s->remove_on_stop)
1002 STRV_FOREACH(i, s->symlinks)
1006 static void socket_apply_socket_options(Socket *s, int fd) {
1012 if (s->keep_alive) {
1013 r = setsockopt_int(fd, SOL_SOCKET, SO_KEEPALIVE, true);
1015 log_unit_warning_errno(UNIT(s), r, "SO_KEEPALIVE failed: %m");
1018 if (s->keep_alive_time > 0) {
1019 r = setsockopt_int(fd, SOL_TCP, TCP_KEEPIDLE, s->keep_alive_time / USEC_PER_SEC);
1021 log_unit_warning_errno(UNIT(s), r, "TCP_KEEPIDLE failed: %m");
1024 if (s->keep_alive_interval > 0) {
1025 r = setsockopt_int(fd, SOL_TCP, TCP_KEEPINTVL, s->keep_alive_interval / USEC_PER_SEC);
1027 log_unit_warning_errno(UNIT(s), r, "TCP_KEEPINTVL failed: %m");
1030 if (s->keep_alive_cnt > 0) {
1031 r = setsockopt_int(fd, SOL_TCP, TCP_KEEPCNT, s->keep_alive_cnt);
1033 log_unit_warning_errno(UNIT(s), r, "TCP_KEEPCNT failed: %m");
1036 if (s->defer_accept > 0) {
1037 r = setsockopt_int(fd, SOL_TCP, TCP_DEFER_ACCEPT, s->defer_accept / USEC_PER_SEC);
1039 log_unit_warning_errno(UNIT(s), r, "TCP_DEFER_ACCEPT failed: %m");
1043 if (s->socket_protocol == IPPROTO_SCTP) {
1044 r = setsockopt_int(fd, SOL_SCTP, SCTP_NODELAY, true);
1046 log_unit_warning_errno(UNIT(s), r, "SCTP_NODELAY failed: %m");
1048 r = setsockopt_int(fd, SOL_TCP, TCP_NODELAY, true);
1050 log_unit_warning_errno(UNIT(s), r, "TCP_NODELAY failed: %m");
1055 r = setsockopt_int(fd, SOL_SOCKET, SO_BROADCAST, true);
1057 log_unit_warning_errno(UNIT(s), r, "SO_BROADCAST failed: %m");
1061 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSCRED, true);
1063 log_unit_warning_errno(UNIT(s), r, "SO_PASSCRED failed: %m");
1067 r = setsockopt_int(fd, SOL_SOCKET, SO_PASSSEC, true);
1069 log_unit_warning_errno(UNIT(s), r, "SO_PASSSEC failed: %m");
1072 if (s->priority >= 0) {
1073 r = setsockopt_int(fd, SOL_SOCKET, SO_PRIORITY, s->priority);
1075 log_unit_warning_errno(UNIT(s), r, "SO_PRIORITY failed: %m");
1078 if (s->receive_buffer > 0) {
1079 /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
1080 if (setsockopt_int(fd, SOL_SOCKET, SO_RCVBUFFORCE, s->receive_buffer) < 0) {
1081 r = setsockopt_int(fd, SOL_SOCKET, SO_RCVBUF, s->receive_buffer);
1083 log_unit_warning_errno(UNIT(s), r, "SO_RCVBUF failed: %m");
1087 if (s->send_buffer > 0) {
1088 if (setsockopt_int(fd, SOL_SOCKET, SO_SNDBUFFORCE, s->send_buffer) < 0) {
1089 r = setsockopt_int(fd, SOL_SOCKET, SO_SNDBUF, s->send_buffer);
1091 log_unit_warning_errno(UNIT(s), r, "SO_SNDBUF failed: %m");
1096 r = setsockopt_int(fd, SOL_SOCKET, SO_MARK, s->mark);
1098 log_unit_warning_errno(UNIT(s), r, "SO_MARK failed: %m");
1101 if (s->ip_tos >= 0) {
1102 r = setsockopt_int(fd, IPPROTO_IP, IP_TOS, s->ip_tos);
1104 log_unit_warning_errno(UNIT(s), r, "IP_TOS failed: %m");
1107 if (s->ip_ttl >= 0) {
1110 r = setsockopt_int(fd, IPPROTO_IP, IP_TTL, s->ip_ttl);
1112 if (socket_ipv6_is_supported())
1113 x = setsockopt_int(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, s->ip_ttl);
1118 log_unit_warning_errno(UNIT(s), r, "IP_TTL/IPV6_UNICAST_HOPS failed: %m");
1121 if (s->tcp_congestion)
1122 if (setsockopt(fd, SOL_TCP, TCP_CONGESTION, s->tcp_congestion, strlen(s->tcp_congestion)+1) < 0)
1123 log_unit_warning_errno(UNIT(s), errno, "TCP_CONGESTION failed: %m");
1125 if (s->smack_ip_in) {
1126 r = mac_smack_apply_fd(fd, SMACK_ATTR_IPIN, s->smack_ip_in);
1128 log_unit_error_errno(UNIT(s), r, "mac_smack_apply_ip_in_fd: %m");
1131 if (s->smack_ip_out) {
1132 r = mac_smack_apply_fd(fd, SMACK_ATTR_IPOUT, s->smack_ip_out);
1134 log_unit_error_errno(UNIT(s), r, "mac_smack_apply_ip_out_fd: %m");
1138 static void socket_apply_fifo_options(Socket *s, int fd) {
1144 if (s->pipe_size > 0)
1145 if (fcntl(fd, F_SETPIPE_SZ, s->pipe_size) < 0)
1146 log_unit_warning_errno(UNIT(s), errno, "Setting pipe size failed, ignoring: %m");
1149 r = mac_smack_apply_fd(fd, SMACK_ATTR_ACCESS, s->smack);
1151 log_unit_error_errno(UNIT(s), r, "SMACK relabelling failed, ignoring: %m");
1155 static int fifo_address_create(
1157 mode_t directory_mode,
1158 mode_t socket_mode) {
1160 _cleanup_close_ int fd = -1;
1167 (void) mkdir_parents_label(path, directory_mode);
1169 r = mac_selinux_create_file_prepare(path, S_IFIFO);
1173 /* Enforce the right access mode for the fifo */
1174 old_mask = umask(~socket_mode);
1176 /* Include the original umask in our mask */
1177 (void) umask(~socket_mode | old_mask);
1179 r = mkfifo(path, socket_mode);
1180 (void) umask(old_mask);
1182 if (r < 0 && errno != EEXIST) {
1187 fd = open(path, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK | O_NOFOLLOW);
1193 mac_selinux_create_file_clear();
1195 if (fstat(fd, &st) < 0) {
1200 if (!S_ISFIFO(st.st_mode) ||
1201 (st.st_mode & 0777) != (socket_mode & ~old_mask) ||
1202 st.st_uid != getuid() ||
1203 st.st_gid != getgid()) {
1211 mac_selinux_create_file_clear();
1215 static int special_address_create(const char *path, bool writable) {
1216 _cleanup_close_ int fd = -1;
1221 fd = open(path, (writable ? O_RDWR : O_RDONLY)|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW);
1225 if (fstat(fd, &st) < 0)
1228 /* Check whether this is a /proc, /sys or /dev file or char device */
1229 if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode))
1235 static int usbffs_address_create(const char *path) {
1236 _cleanup_close_ int fd = -1;
1241 fd = open(path, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK|O_NOFOLLOW);
1245 if (fstat(fd, &st) < 0)
1248 /* Check whether this is a regular file (ffs endpoint) */
1249 if (!S_ISREG(st.st_mode))
1255 static int mq_address_create(
1261 _cleanup_close_ int fd = -1;
1264 struct mq_attr _attr, *attr = NULL;
1268 if (maxmsg > 0 && msgsize > 0) {
1269 _attr = (struct mq_attr) {
1270 .mq_flags = O_NONBLOCK,
1271 .mq_maxmsg = maxmsg,
1272 .mq_msgsize = msgsize,
1277 /* Enforce the right access mode for the mq */
1278 old_mask = umask(~mq_mode);
1280 /* Include the original umask in our mask */
1281 (void) umask(~mq_mode | old_mask);
1282 fd = mq_open(path, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_CREAT, mq_mode, attr);
1283 (void) umask(old_mask);
1288 if (fstat(fd, &st) < 0)
1291 if ((st.st_mode & 0777) != (mq_mode & ~old_mask) ||
1292 st.st_uid != getuid() ||
1293 st.st_gid != getgid())
1299 static int socket_symlink(Socket *s) {
1306 p = socket_find_symlink_target(s);
1310 STRV_FOREACH(i, s->symlinks) {
1311 (void) mkdir_parents_label(*i, s->directory_mode);
1313 r = symlink_idempotent(p, *i, false);
1315 if (r == -EEXIST && s->remove_on_stop) {
1316 /* If there's already something where we want to create the symlink, and the destructive
1317 * RemoveOnStop= mode is set, then we might as well try to remove what already exists and try
1320 if (unlink(*i) >= 0)
1321 r = symlink_idempotent(p, *i, false);
1325 log_unit_warning_errno(UNIT(s), r, "Failed to create symlink %s → %s, ignoring: %m", p, *i);
1331 static int usbffs_write_descs(int fd, Service *s) {
1334 if (!s->usb_function_descriptors || !s->usb_function_strings)
1337 r = copy_file_fd(s->usb_function_descriptors, fd, 0);
1341 return copy_file_fd(s->usb_function_strings, fd, 0);
1344 static int usbffs_select_ep(const struct dirent *d) {
1345 return d->d_name[0] != '.' && !streq(d->d_name, "ep0");
1348 static int usbffs_dispatch_eps(SocketPort *p) {
1349 _cleanup_free_ struct dirent **ent = NULL;
1353 r = scandir(p->path, &ent, usbffs_select_ep, alphasort);
1358 p->auxiliary_fds = new(int, n);
1359 if (!p->auxiliary_fds) {
1364 p->n_auxiliary_fds = n;
1367 for (i = 0; i < n; ++i) {
1368 _cleanup_free_ char *ep = NULL;
1370 ep = path_make_absolute(ent[i]->d_name, p->path);
1376 path_simplify(ep, false);
1378 r = usbffs_address_create(ep);
1382 p->auxiliary_fds[k++] = r;
1389 close_many(p->auxiliary_fds, k);
1390 p->auxiliary_fds = mfree(p->auxiliary_fds);
1391 p->n_auxiliary_fds = 0;
1394 for (i = 0; i < n; ++i)
1400 static int socket_determine_selinux_label(Socket *s, char **ret) {
1403 _cleanup_free_ char *path = NULL;
1409 if (s->selinux_context_from_net) {
1410 /* If this is requested, get label from the network label */
1412 r = mac_selinux_get_our_label(ret);
1413 if (r == -EOPNOTSUPP)
1417 /* Otherwise, get it from the executable we are about to start */
1418 r = socket_instantiate_service(s);
1422 if (!UNIT_ISSET(s->service))
1425 service = SERVICE(UNIT_DEREF(s->service));
1426 c = service->exec_command[SERVICE_EXEC_START];
1430 r = chase_symlinks(c->path, service->exec_context.root_directory, CHASE_PREFIX_ROOT, &path, NULL);
1434 r = mac_selinux_get_create_label_from_exe(path, ret);
1435 if (IN_SET(r, -EPERM, -EOPNOTSUPP))
1446 static int socket_address_listen_do(
1448 const SocketAddress *address,
1449 const char *label) {
1454 return socket_address_listen(
1456 SOCK_CLOEXEC|SOCK_NONBLOCK,
1468 #define log_address_error_errno(u, address, error, fmt) \
1470 _cleanup_free_ char *_t = NULL; \
1472 (void) socket_address_print(address, &_t); \
1473 log_unit_error_errno(u, error, fmt, strna(_t)); \
1476 static int fork_needed(const SocketAddress *address, const ExecContext *context) {
1482 /* Check if we need to do the cgroup or netns stuff. If not we can do things much simpler. */
1484 if (IN_SET(address->sockaddr.sa.sa_family, AF_INET, AF_INET6)) {
1485 r = bpf_firewall_supported();
1488 if (r != BPF_FIREWALL_UNSUPPORTED) /* If BPF firewalling isn't supported anyway — there's no point in this forking complexity */
1492 return context->private_network || context->network_namespace_path;
1495 static int socket_address_listen_in_cgroup(
1497 const SocketAddress *address,
1498 const char *label) {
1500 _cleanup_close_pair_ int pair[2] = { -1, -1 };
1507 /* This is a wrapper around socket_address_listen(), that forks off a helper process inside the
1508 * socket's cgroup and network namespace in which the socket is actually created. This way we ensure
1509 * the socket is actually properly attached to the unit's cgroup for the purpose of BPF filtering and
1512 r = fork_needed(address, &s->exec_context);
1516 /* Shortcut things... */
1517 fd = socket_address_listen_do(s, address, label);
1519 return log_address_error_errno(UNIT(s), address, fd, "Failed to create listening socket (%s): %m");
1524 r = unit_setup_exec_runtime(UNIT(s));
1526 return log_unit_error_errno(UNIT(s), r, "Failed acquire runtime: %m");
1528 if (s->exec_context.network_namespace_path &&
1530 s->exec_runtime->netns_storage_socket[0] >= 0) {
1531 r = open_netns_path(s->exec_runtime->netns_storage_socket, s->exec_context.network_namespace_path);
1533 return log_unit_error_errno(UNIT(s), r, "Failed to open network namespace path %s: %m", s->exec_context.network_namespace_path);
1536 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0)
1537 return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m");
1539 r = unit_fork_helper_process(UNIT(s), "(sd-listen)", &pid);
1541 return log_unit_error_errno(UNIT(s), r, "Failed to fork off listener stub process: %m");
1545 pair[0] = safe_close(pair[0]);
1547 if ((s->exec_context.private_network || s->exec_context.network_namespace_path) &&
1549 s->exec_runtime->netns_storage_socket[0] >= 0) {
1551 if (ns_type_supported(NAMESPACE_NET)) {
1552 r = setup_netns(s->exec_runtime->netns_storage_socket);
1554 log_unit_error_errno(UNIT(s), r, "Failed to join network namespace: %m");
1555 _exit(EXIT_NETWORK);
1557 } else if (s->exec_context.network_namespace_path) {
1558 log_unit_error(UNIT(s), "Network namespace path configured but network namespaces not supported.");
1559 _exit(EXIT_NETWORK);
1561 log_unit_warning(UNIT(s), "PrivateNetwork=yes is configured, but the kernel does not support network namespaces, ignoring.");
1564 fd = socket_address_listen_do(s, address, label);
1566 log_address_error_errno(UNIT(s), address, fd, "Failed to create listening socket (%s): %m");
1567 _exit(EXIT_FAILURE);
1570 r = send_one_fd(pair[1], fd, 0);
1572 log_address_error_errno(UNIT(s), address, r, "Failed to send listening socket (%s) to parent: %m");
1573 _exit(EXIT_FAILURE);
1576 _exit(EXIT_SUCCESS);
1579 pair[1] = safe_close(pair[1]);
1580 fd = receive_one_fd(pair[0], 0);
1582 /* We synchronously wait for the helper, as it shouldn't be slow */
1583 r = wait_for_terminate_and_check("(sd-listen)", pid, WAIT_LOG_ABNORMAL);
1590 return log_address_error_errno(UNIT(s), address, fd, "Failed to receive listening socket (%s): %m");
1595 DEFINE_TRIVIAL_CLEANUP_FUNC(Socket *, socket_close_fds);
1597 static int socket_open_fds(Socket *_s) {
1598 _cleanup_(socket_close_fdsp) Socket *s = _s;
1599 _cleanup_(mac_selinux_freep) char *label = NULL;
1600 bool know_label = false;
1606 LIST_FOREACH(port, p, s->ports) {
1616 /* Figure out label, if we don't it know yet. We do it once, for the first socket where
1617 * we need this and remember it for the rest. */
1619 r = socket_determine_selinux_label(s, &label);
1621 return log_unit_error_errno(UNIT(s), r, "Failed to determine SELinux label: %m");
1626 /* Apply the socket protocol */
1627 switch (p->address.type) {
1630 case SOCK_SEQPACKET:
1631 if (s->socket_protocol == IPPROTO_SCTP)
1632 p->address.protocol = s->socket_protocol;
1636 if (s->socket_protocol == IPPROTO_UDPLITE)
1637 p->address.protocol = s->socket_protocol;
1641 p->fd = socket_address_listen_in_cgroup(s, &p->address, label);
1645 socket_apply_socket_options(s, p->fd);
1649 case SOCKET_SPECIAL:
1651 p->fd = special_address_create(p->path, s->writable);
1653 return log_unit_error_errno(UNIT(s), p->fd, "Failed to open special file %s: %m", p->path);
1658 p->fd = fifo_address_create(
1663 return log_unit_error_errno(UNIT(s), p->fd, "Failed to open FIFO %s: %m", p->path);
1665 socket_apply_fifo_options(s, p->fd);
1671 p->fd = mq_address_create(
1677 return log_unit_error_errno(UNIT(s), p->fd, "Failed to open message queue %s: %m", p->path);
1680 case SOCKET_USB_FUNCTION: {
1681 _cleanup_free_ char *ep = NULL;
1683 ep = path_make_absolute("ep0", p->path);
1685 p->fd = usbffs_address_create(ep);
1689 r = usbffs_write_descs(p->fd, SERVICE(UNIT_DEREF(s->service)));
1693 r = usbffs_dispatch_eps(p);
1700 assert_not_reached("Unknown port type");
1708 static void socket_unwatch_fds(Socket *s) {
1714 LIST_FOREACH(port, p, s->ports) {
1718 if (!p->event_source)
1721 r = sd_event_source_set_enabled(p->event_source, SD_EVENT_OFF);
1723 log_unit_debug_errno(UNIT(s), r, "Failed to disable event source: %m");
1727 static int socket_watch_fds(Socket *s) {
1733 LIST_FOREACH(port, p, s->ports) {
1737 if (p->event_source) {
1738 r = sd_event_source_set_enabled(p->event_source, SD_EVENT_ON);
1742 r = sd_event_add_io(UNIT(s)->manager->event, &p->event_source, p->fd, EPOLLIN, socket_dispatch_io, p);
1746 (void) sd_event_source_set_description(p->event_source, "socket-port-io");
1753 log_unit_warning_errno(UNIT(s), r, "Failed to watch listening fds: %m");
1754 socket_unwatch_fds(s);
1764 static int socket_check_open(Socket *s) {
1765 bool have_open = false, have_closed = false;
1770 LIST_FOREACH(port, p, s->ports) {
1776 if (have_open && have_closed)
1777 return SOCKET_OPEN_SOME;
1781 return SOCKET_OPEN_ALL;
1783 return SOCKET_OPEN_NONE;
1786 static void socket_set_state(Socket *s, SocketState state) {
1787 SocketState old_state;
1790 if (s->state != state)
1791 bus_unit_send_pending_change_signal(UNIT(s), false);
1793 old_state = s->state;
1801 SOCKET_STOP_PRE_SIGTERM,
1802 SOCKET_STOP_PRE_SIGKILL,
1804 SOCKET_FINAL_SIGTERM,
1805 SOCKET_FINAL_SIGKILL,
1808 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
1809 socket_unwatch_control_pid(s);
1810 s->control_command = NULL;
1811 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
1814 if (state != SOCKET_LISTENING)
1815 socket_unwatch_fds(s);
1823 SOCKET_STOP_PRE_SIGTERM,
1824 SOCKET_STOP_PRE_SIGKILL,
1826 socket_close_fds(s);
1828 if (state != old_state)
1829 log_unit_debug(UNIT(s), "Changed %s -> %s", socket_state_to_string(old_state), socket_state_to_string(state));
1831 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], 0);
1834 static int socket_coldplug(Unit *u) {
1835 Socket *s = SOCKET(u);
1839 assert(s->state == SOCKET_DEAD);
1841 if (s->deserialized_state == s->state)
1844 if (s->control_pid > 0 &&
1845 pid_is_unwaited(s->control_pid) &&
1846 IN_SET(s->deserialized_state,
1851 SOCKET_STOP_PRE_SIGTERM,
1852 SOCKET_STOP_PRE_SIGKILL,
1854 SOCKET_FINAL_SIGTERM,
1855 SOCKET_FINAL_SIGKILL,
1858 r = unit_watch_pid(UNIT(s), s->control_pid, false);
1862 r = socket_arm_timer(s, usec_add(u->state_change_timestamp.monotonic, s->timeout_usec));
1867 if (IN_SET(s->deserialized_state,
1873 /* Originally, we used to simply reopen all sockets here that we didn't have file descriptors
1874 * for. However, this is problematic, as we won't traverse through the SOCKET_START_CHOWN state for
1875 * them, and thus the UID/GID wouldn't be right. Hence, instead simply check if we have all fds open,
1876 * and if there's a mismatch, warn loudly. */
1878 r = socket_check_open(s);
1879 if (r == SOCKET_OPEN_NONE)
1880 log_unit_warning(UNIT(s),
1881 "Socket unit configuration has changed while unit has been running, "
1882 "no open socket file descriptor left. "
1883 "The socket unit is not functional until restarted.");
1884 else if (r == SOCKET_OPEN_SOME)
1885 log_unit_warning(UNIT(s),
1886 "Socket unit configuration has changed while unit has been running, "
1887 "and some socket file descriptors have not been opened yet. "
1888 "The socket unit is not fully functional until restarted.");
1891 if (s->deserialized_state == SOCKET_LISTENING) {
1892 r = socket_watch_fds(s);
1897 if (!IN_SET(s->deserialized_state, SOCKET_DEAD, SOCKET_FAILED, SOCKET_CLEANING)) {
1898 (void) unit_setup_dynamic_creds(u);
1899 (void) unit_setup_exec_runtime(u);
1902 socket_set_state(s, s->deserialized_state);
1906 static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
1908 _cleanup_(exec_params_clear) ExecParameters exec_params = {
1909 .flags = EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN,
1922 r = unit_prepare_exec(UNIT(s));
1926 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
1930 r = unit_set_exec_params(UNIT(s), &exec_params);
1934 r = exec_spawn(UNIT(s),
1944 r = unit_watch_pid(UNIT(s), pid, true);
1953 static int socket_chown(Socket *s, pid_t *_pid) {
1957 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
1961 /* We have to resolve the user names out-of-process, hence
1962 * let's fork here. It's messy, but well, what can we do? */
1964 r = unit_fork_helper_process(UNIT(s), "(sd-chown)", &pid);
1968 uid_t uid = UID_INVALID;
1969 gid_t gid = GID_INVALID;
1974 if (!isempty(s->user)) {
1975 const char *user = s->user;
1977 r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
1979 log_unit_error_errno(UNIT(s), r, "Failed to resolve user %s: %m", user);
1984 if (!isempty(s->group)) {
1985 const char *group = s->group;
1987 r = get_group_creds(&group, &gid, 0);
1989 log_unit_error_errno(UNIT(s), r, "Failed to resolve group %s: %m", group);
1994 LIST_FOREACH(port, p, s->ports) {
1995 const char *path = NULL;
1997 if (p->type == SOCKET_SOCKET)
1998 path = socket_address_get_path(&p->address);
1999 else if (p->type == SOCKET_FIFO)
2005 if (chown(path, uid, gid) < 0) {
2006 log_unit_error_errno(UNIT(s), errno, "Failed to chown(): %m");
2011 _exit(EXIT_SUCCESS);
2014 r = unit_watch_pid(UNIT(s), pid, true);
2022 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
2026 static void socket_enter_dead(Socket *s, SocketResult f) {
2029 if (s->result == SOCKET_SUCCESS)
2032 if (s->result == SOCKET_SUCCESS)
2033 unit_log_success(UNIT(s));
2035 unit_log_failure(UNIT(s), socket_result_to_string(s->result));
2037 socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD);
2039 s->exec_runtime = exec_runtime_unref(s->exec_runtime, true);
2041 unit_destroy_runtime_directory(UNIT(s), &s->exec_context);
2043 unit_unref_uid_gid(UNIT(s), true);
2045 dynamic_creds_destroy(&s->dynamic_creds);
2048 static void socket_enter_signal(Socket *s, SocketState state, SocketResult f);
2050 static void socket_enter_stop_post(Socket *s, SocketResult f) {
2054 if (s->result == SOCKET_SUCCESS)
2057 socket_unwatch_control_pid(s);
2058 s->control_command_id = SOCKET_EXEC_STOP_POST;
2059 s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST];
2061 if (s->control_command) {
2062 r = socket_spawn(s, s->control_command, &s->control_pid);
2066 socket_set_state(s, SOCKET_STOP_POST);
2068 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_SUCCESS);
2073 log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m");
2074 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES);
2077 static int state_to_kill_operation(Socket *s, SocketState state) {
2078 if (state == SOCKET_STOP_PRE_SIGTERM && unit_has_job_type(UNIT(s), JOB_RESTART))
2079 return KILL_RESTART;
2081 if (state == SOCKET_FINAL_SIGTERM)
2082 return KILL_TERMINATE;
2087 static void socket_enter_signal(Socket *s, SocketState state, SocketResult f) {
2092 if (s->result == SOCKET_SUCCESS)
2095 r = unit_kill_context(
2098 state_to_kill_operation(s, state),
2106 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_usec));
2110 socket_set_state(s, state);
2111 } else if (state == SOCKET_STOP_PRE_SIGTERM)
2112 socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_SUCCESS);
2113 else if (state == SOCKET_STOP_PRE_SIGKILL)
2114 socket_enter_stop_post(s, SOCKET_SUCCESS);
2115 else if (state == SOCKET_FINAL_SIGTERM)
2116 socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS);
2118 socket_enter_dead(s, SOCKET_SUCCESS);
2123 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
2125 if (IN_SET(state, SOCKET_STOP_PRE_SIGTERM, SOCKET_STOP_PRE_SIGKILL))
2126 socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
2128 socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
2131 static void socket_enter_stop_pre(Socket *s, SocketResult f) {
2135 if (s->result == SOCKET_SUCCESS)
2138 socket_unwatch_control_pid(s);
2139 s->control_command_id = SOCKET_EXEC_STOP_PRE;
2140 s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE];
2142 if (s->control_command) {
2143 r = socket_spawn(s, s->control_command, &s->control_pid);
2147 socket_set_state(s, SOCKET_STOP_PRE);
2149 socket_enter_stop_post(s, SOCKET_SUCCESS);
2154 log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-pre' task: %m");
2155 socket_enter_stop_post(s, SOCKET_FAILURE_RESOURCES);
2158 static void socket_enter_listening(Socket *s) {
2162 r = socket_watch_fds(s);
2164 log_unit_warning_errno(UNIT(s), r, "Failed to watch sockets: %m");
2168 socket_set_state(s, SOCKET_LISTENING);
2172 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
2175 static void socket_enter_start_post(Socket *s) {
2179 socket_unwatch_control_pid(s);
2180 s->control_command_id = SOCKET_EXEC_START_POST;
2181 s->control_command = s->exec_command[SOCKET_EXEC_START_POST];
2183 if (s->control_command) {
2184 r = socket_spawn(s, s->control_command, &s->control_pid);
2186 log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m");
2190 socket_set_state(s, SOCKET_START_POST);
2192 socket_enter_listening(s);
2197 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
2200 static void socket_enter_start_chown(Socket *s) {
2205 r = socket_open_fds(s);
2207 log_unit_warning_errno(UNIT(s), r, "Failed to listen on sockets: %m");
2211 if (!isempty(s->user) || !isempty(s->group)) {
2213 socket_unwatch_control_pid(s);
2214 s->control_command_id = SOCKET_EXEC_START_CHOWN;
2215 s->control_command = NULL;
2217 r = socket_chown(s, &s->control_pid);
2219 log_unit_warning_errno(UNIT(s), r, "Failed to fork 'start-chown' task: %m");
2223 socket_set_state(s, SOCKET_START_CHOWN);
2225 socket_enter_start_post(s);
2230 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
2233 static void socket_enter_start_pre(Socket *s) {
2237 socket_unwatch_control_pid(s);
2239 unit_warn_leftover_processes(UNIT(s));
2241 s->control_command_id = SOCKET_EXEC_START_PRE;
2242 s->control_command = s->exec_command[SOCKET_EXEC_START_PRE];
2244 if (s->control_command) {
2245 r = socket_spawn(s, s->control_command, &s->control_pid);
2247 log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m");
2251 socket_set_state(s, SOCKET_START_PRE);
2253 socket_enter_start_chown(s);
2258 socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
2261 static void flush_ports(Socket *s) {
2264 /* Flush all incoming traffic, regardless if actual bytes or new connections, so that this socket isn't busy
2267 LIST_FOREACH(port, p, s->ports) {
2271 (void) flush_accept(p->fd);
2272 (void) flush_fd(p->fd);
2276 static void socket_enter_running(Socket *s, int cfd) {
2277 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2280 /* Note that this call takes possession of the connection fd passed. It either has to assign it somewhere or
2285 /* We don't take connections anymore if we are supposed to shut down anyway */
2286 if (unit_stop_pending(UNIT(s))) {
2288 log_unit_debug(UNIT(s), "Suppressing connection request since unit stop is scheduled.");
2298 if (!ratelimit_below(&s->trigger_limit)) {
2299 log_unit_warning(UNIT(s), "Trigger limit hit, refusing further activation.");
2300 socket_enter_stop_pre(s, SOCKET_FAILURE_TRIGGER_LIMIT_HIT);
2305 bool pending = false;
2310 /* If there's already a start pending don't bother to
2312 HASHMAP_FOREACH_KEY(v, other, UNIT(s)->dependencies[UNIT_TRIGGERS], i)
2313 if (unit_active_or_pending(other)) {
2319 if (!UNIT_ISSET(s->service)) {
2320 log_unit_error(UNIT(s), "Service to activate vanished, refusing activation.");
2325 r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT_DEREF(s->service), JOB_REPLACE, NULL, &error, NULL);
2330 socket_set_state(s, SOCKET_RUNNING);
2332 _cleanup_free_ char *prefix = NULL, *instance = NULL, *name = NULL;
2333 _cleanup_(socket_peer_unrefp) SocketPeer *p = NULL;
2336 if (s->n_connections >= s->max_connections) {
2337 log_unit_warning(UNIT(s), "Too many incoming connections (%u), dropping connection.",
2342 if (s->max_connections_per_source > 0) {
2343 r = socket_acquire_peer(s, cfd, &p);
2346 } else if (r > 0 && p->n_ref > s->max_connections_per_source) {
2347 _cleanup_free_ char *t = NULL;
2349 (void) sockaddr_pretty(&p->peer.sa, p->peer_salen, true, false, &t);
2351 log_unit_warning(UNIT(s),
2352 "Too many incoming connections (%u) from source %s, dropping connection.",
2353 p->n_ref, strnull(t));
2358 r = socket_instantiate_service(s);
2362 r = instance_from_socket(cfd, s->n_accepted, &instance);
2367 /* ENOTCONN is legitimate if TCP RST was received.
2368 * This connection is over, but the socket unit lives on. */
2369 log_unit_debug(UNIT(s), "Got ENOTCONN on incoming socket, assuming aborted connection attempt, ignoring.");
2373 r = unit_name_to_prefix(UNIT(s)->id, &prefix);
2377 r = unit_name_build(prefix, instance, ".service", &name);
2381 r = unit_add_name(UNIT_DEREF(s->service), name);
2385 service = SERVICE(UNIT_DEREF(s->service));
2386 unit_ref_unset(&s->service);
2389 unit_choose_id(UNIT(service), name);
2391 r = service_set_socket_fd(service, cfd, s, s->selinux_context_from_net);
2395 cfd = -1; /* We passed ownership of the fd to the service now. Forget it here. */
2398 service->peer = TAKE_PTR(p); /* Pass ownership of the peer reference */
2400 r = manager_add_job(UNIT(s)->manager, JOB_START, UNIT(service), JOB_REPLACE, NULL, &error, NULL);
2402 /* We failed to activate the new service, but it still exists. Let's make sure the service
2403 * closes and forgets the connection fd again, immediately. */
2404 service_close_socket_fd(service);
2408 /* Notify clients about changed counters */
2409 unit_add_to_dbus_queue(UNIT(s));
2420 log_unit_warning(UNIT(s), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
2421 cfd >= 0 ? "template" : "non-template",
2422 bus_error_message(&error, r));
2424 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
2428 static void socket_run_next(Socket *s) {
2432 assert(s->control_command);
2433 assert(s->control_command->command_next);
2435 socket_unwatch_control_pid(s);
2437 s->control_command = s->control_command->command_next;
2439 r = socket_spawn(s, s->control_command, &s->control_pid);
2446 log_unit_warning_errno(UNIT(s), r, "Failed to run next task: %m");
2448 if (s->state == SOCKET_START_POST)
2449 socket_enter_stop_pre(s, SOCKET_FAILURE_RESOURCES);
2450 else if (s->state == SOCKET_STOP_POST)
2451 socket_enter_dead(s, SOCKET_FAILURE_RESOURCES);
2453 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_RESOURCES);
2456 static int socket_start(Unit *u) {
2457 Socket *s = SOCKET(u);
2462 /* We cannot fulfill this request right now, try again later
2464 if (IN_SET(s->state,
2466 SOCKET_STOP_PRE_SIGKILL,
2467 SOCKET_STOP_PRE_SIGTERM,
2469 SOCKET_FINAL_SIGTERM,
2470 SOCKET_FINAL_SIGKILL,
2474 /* Already on it! */
2475 if (IN_SET(s->state,
2481 /* Cannot run this without the service being around */
2482 if (UNIT_ISSET(s->service)) {
2485 service = SERVICE(UNIT_DEREF(s->service));
2487 if (UNIT(service)->load_state != UNIT_LOADED) {
2488 log_unit_error(u, "Socket service %s not loaded, refusing.", UNIT(service)->id);
2492 /* If the service is already active we cannot start the
2494 if (!IN_SET(service->state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) {
2495 log_unit_error(u, "Socket service %s already active, refusing.", UNIT(service)->id);
2500 assert(IN_SET(s->state, SOCKET_DEAD, SOCKET_FAILED));
2502 r = unit_test_start_limit(u);
2504 socket_enter_dead(s, SOCKET_FAILURE_START_LIMIT_HIT);
2508 r = unit_acquire_invocation_id(u);
2512 s->result = SOCKET_SUCCESS;
2513 exec_command_reset_status_list_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
2515 u->reset_accounting = true;
2517 socket_enter_start_pre(s);
2521 static int socket_stop(Unit *u) {
2522 Socket *s = SOCKET(u);
2527 if (IN_SET(s->state,
2529 SOCKET_STOP_PRE_SIGTERM,
2530 SOCKET_STOP_PRE_SIGKILL,
2532 SOCKET_FINAL_SIGTERM,
2533 SOCKET_FINAL_SIGKILL))
2536 /* If there's already something running we go directly into
2538 if (IN_SET(s->state,
2541 SOCKET_START_POST)) {
2542 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_SUCCESS);
2546 /* If we are currently cleaning, then abort it, brutally. */
2547 if (s->state == SOCKET_CLEANING) {
2548 socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_SUCCESS);
2552 assert(IN_SET(s->state, SOCKET_LISTENING, SOCKET_RUNNING));
2554 socket_enter_stop_pre(s, SOCKET_SUCCESS);
2558 static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
2559 Socket *s = SOCKET(u);
2567 (void) serialize_item(f, "state", socket_state_to_string(s->state));
2568 (void) serialize_item(f, "result", socket_result_to_string(s->result));
2569 (void) serialize_item_format(f, "n-accepted", "%u", s->n_accepted);
2570 (void) serialize_item_format(f, "n-refused", "%u", s->n_refused);
2572 if (s->control_pid > 0)
2573 (void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid);
2575 if (s->control_command_id >= 0)
2576 (void) serialize_item(f, "control-command", socket_exec_command_to_string(s->control_command_id));
2578 LIST_FOREACH(port, p, s->ports) {
2585 copy = fdset_put_dup(fds, p->fd);
2587 return log_unit_warning_errno(u, copy, "Failed to serialize socket fd: %m");
2589 if (p->type == SOCKET_SOCKET) {
2590 _cleanup_free_ char *t = NULL;
2592 r = socket_address_print(&p->address, &t);
2594 return log_unit_error_errno(u, r, "Failed to format socket address: %m");
2596 if (socket_address_family(&p->address) == AF_NETLINK)
2597 (void) serialize_item_format(f, "netlink", "%i %s", copy, t);
2599 (void) serialize_item_format(f, "socket", "%i %i %s", copy, p->address.type, t);
2600 } else if (p->type == SOCKET_SPECIAL)
2601 (void) serialize_item_format(f, "special", "%i %s", copy, p->path);
2602 else if (p->type == SOCKET_MQUEUE)
2603 (void) serialize_item_format(f, "mqueue", "%i %s", copy, p->path);
2604 else if (p->type == SOCKET_USB_FUNCTION)
2605 (void) serialize_item_format(f, "ffs", "%i %zu %s", copy, p->n_auxiliary_fds, p->path);
2607 assert(p->type == SOCKET_FIFO);
2608 (void) serialize_item_format(f, "fifo", "%i %s", copy, p->path);
2611 for (i = 0; i < p->n_auxiliary_fds; ++i) {
2612 copy = fdset_put_dup(fds, p->auxiliary_fds[i]);
2616 (void) serialize_item_format(f, "aux", "%i %i %s", i, copy, p->path);
2623 static void socket_port_take_fd(SocketPort *p, FDSet *fds, int fd) {
2627 p->fd = fdset_remove(fds, fd);
2630 static int socket_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2631 Socket *s = SOCKET(u);
2637 if (streq(key, "state")) {
2640 state = socket_state_from_string(value);
2642 log_unit_debug(u, "Failed to parse state value: %s", value);
2644 s->deserialized_state = state;
2645 } else if (streq(key, "result")) {
2648 f = socket_result_from_string(value);
2650 log_unit_debug(u, "Failed to parse result value: %s", value);
2651 else if (f != SOCKET_SUCCESS)
2654 } else if (streq(key, "n-accepted")) {
2657 if (safe_atou(value, &k) < 0)
2658 log_unit_debug(u, "Failed to parse n-accepted value: %s", value);
2661 } else if (streq(key, "n-refused")) {
2664 if (safe_atou(value, &k) < 0)
2665 log_unit_debug(u, "Failed to parse n-refused value: %s", value);
2668 } else if (streq(key, "control-pid")) {
2671 if (parse_pid(value, &pid) < 0)
2672 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
2674 s->control_pid = pid;
2675 } else if (streq(key, "control-command")) {
2676 SocketExecCommand id;
2678 id = socket_exec_command_from_string(value);
2680 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
2682 s->control_command_id = id;
2683 s->control_command = s->exec_command[id];
2685 } else if (streq(key, "fifo")) {
2689 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
2690 log_unit_debug(u, "Failed to parse fifo value: %s", value);
2692 LIST_FOREACH(port, p, s->ports)
2693 if (p->type == SOCKET_FIFO &&
2694 path_equal_or_files_same(p->path, value+skip, 0)) {
2695 socket_port_take_fd(p, fds, fd);
2699 } else if (streq(key, "special")) {
2703 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
2704 log_unit_debug(u, "Failed to parse special value: %s", value);
2706 LIST_FOREACH(port, p, s->ports)
2707 if (p->type == SOCKET_SPECIAL &&
2708 path_equal_or_files_same(p->path, value+skip, 0)) {
2709 socket_port_take_fd(p, fds, fd);
2713 } else if (streq(key, "mqueue")) {
2717 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
2718 log_unit_debug(u, "Failed to parse mqueue value: %s", value);
2720 LIST_FOREACH(port, p, s->ports)
2721 if (p->type == SOCKET_MQUEUE &&
2722 streq(p->path, value+skip)) {
2723 socket_port_take_fd(p, fds, fd);
2727 } else if (streq(key, "socket")) {
2728 int fd, type, skip = 0;
2731 if (sscanf(value, "%i %i %n", &fd, &type, &skip) < 2 || fd < 0 || type < 0 || !fdset_contains(fds, fd))
2732 log_unit_debug(u, "Failed to parse socket value: %s", value);
2734 LIST_FOREACH(port, p, s->ports)
2735 if (socket_address_is(&p->address, value+skip, type)) {
2736 socket_port_take_fd(p, fds, fd);
2740 } else if (streq(key, "netlink")) {
2744 if (sscanf(value, "%i %n", &fd, &skip) < 1 || fd < 0 || !fdset_contains(fds, fd))
2745 log_unit_debug(u, "Failed to parse socket value: %s", value);
2747 LIST_FOREACH(port, p, s->ports)
2748 if (socket_address_is_netlink(&p->address, value+skip)) {
2749 socket_port_take_fd(p, fds, fd);
2753 } else if (streq(key, "ffs")) {
2754 int fd, skip = 0, n_auxiliary_fds;
2757 if (sscanf(value, "%i %i %n", &fd, &n_auxiliary_fds, &skip) < 2 || fd < 0 || !fdset_contains(fds, fd))
2758 log_unit_debug(u, "Failed to parse ffs value: %s", value);
2760 LIST_FOREACH(port, p, s->ports)
2761 if (p->type == SOCKET_USB_FUNCTION &&
2762 path_equal_or_files_same(p->path, value+skip, 0)) {
2763 socket_port_take_fd(p, fds, fd);
2764 socket_cleanup_fd_list(p);
2765 p->n_auxiliary_fds = n_auxiliary_fds;
2766 p->auxiliary_fds = new(int, n_auxiliary_fds);
2769 } else if (streq(key, "aux")) {
2770 int fd, skip = 0, idx;
2773 if (sscanf(value, "%i %i %n", &idx, &fd, &skip) < 2 || fd < 0 || !fdset_contains(fds, fd))
2774 log_unit_debug(u, "Failed to parse ffs value: %s", value);
2777 LIST_FOREACH(port, p, s->ports)
2778 if (p->type == SOCKET_USB_FUNCTION &&
2779 path_equal_or_files_same(p->path, value+skip, 0))
2783 if (p->n_auxiliary_fds <= idx)
2784 log_unit_debug(u, "Id of auxiliary fd out of bounds");
2786 p->auxiliary_fds[idx] = fdset_remove(fds, fd);
2791 log_unit_debug(UNIT(s), "Unknown serialization key: %s", key);
2796 static void socket_distribute_fds(Unit *u, FDSet *fds) {
2797 Socket *s = SOCKET(u);
2802 LIST_FOREACH(port, p, s->ports) {
2806 if (p->type != SOCKET_SOCKET)
2812 FDSET_FOREACH(fd, fds, i) {
2813 if (socket_address_matches_fd(&p->address, fd)) {
2814 p->fd = fdset_remove(fds, fd);
2815 s->deserialized_state = SOCKET_LISTENING;
2822 _pure_ static UnitActiveState socket_active_state(Unit *u) {
2825 return state_translation_table[SOCKET(u)->state];
2828 _pure_ static const char *socket_sub_state_to_string(Unit *u) {
2831 return socket_state_to_string(SOCKET(u)->state);
2834 const char* socket_port_type_to_string(SocketPort *p) {
2842 switch (p->address.type) {
2850 case SOCK_SEQPACKET:
2851 return "SequentialPacket";
2854 if (socket_address_family(&p->address) == AF_NETLINK)
2862 case SOCKET_SPECIAL:
2866 return "MessageQueue";
2871 case SOCKET_USB_FUNCTION:
2872 return "USBFunction";
2879 SocketType socket_port_type_from_string(const char *s) {
2882 if (STR_IN_SET(s, "Stream", "Datagram", "SequentialPacket", "Netlink"))
2883 return SOCKET_SOCKET;
2884 else if (streq(s, "Special"))
2885 return SOCKET_SPECIAL;
2886 else if (streq(s, "MessageQueue"))
2887 return SOCKET_MQUEUE;
2888 else if (streq(s, "FIFO"))
2890 else if (streq(s, "USBFunction"))
2891 return SOCKET_USB_FUNCTION;
2893 return _SOCKET_TYPE_INVALID;
2896 _pure_ static bool socket_may_gc(Unit *u) {
2897 Socket *s = SOCKET(u);
2901 return s->n_connections == 0;
2904 static int socket_accept_do(Socket *s, int fd) {
2910 cfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
2912 /* Convert transient network errors into clean and well-defined EAGAIN */
2913 return ERRNO_IS_ACCEPT_AGAIN(errno) ? -EAGAIN : -errno;
2918 static int socket_accept_in_cgroup(Socket *s, SocketPort *p, int fd) {
2919 _cleanup_close_pair_ int pair[2] = { -1, -1 };
2927 /* Similar to socket_address_listen_in_cgroup(), but for accept() rather than socket(): make sure that any
2928 * connection socket is also properly associated with the cgroup. */
2930 if (!IN_SET(p->address.sockaddr.sa.sa_family, AF_INET, AF_INET6))
2933 r = bpf_firewall_supported();
2936 if (r == BPF_FIREWALL_UNSUPPORTED)
2939 if (socketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CLOEXEC, 0, pair) < 0)
2940 return log_unit_error_errno(UNIT(s), errno, "Failed to create communication channel: %m");
2942 r = unit_fork_helper_process(UNIT(s), "(sd-accept)", &pid);
2944 return log_unit_error_errno(UNIT(s), r, "Failed to fork off accept stub process: %m");
2948 pair[0] = safe_close(pair[0]);
2950 cfd = socket_accept_do(s, fd);
2951 if (cfd == -EAGAIN) /* spurious accept() */
2952 _exit(EXIT_SUCCESS);
2954 log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m");
2955 _exit(EXIT_FAILURE);
2958 r = send_one_fd(pair[1], cfd, 0);
2960 log_unit_error_errno(UNIT(s), r, "Failed to send connection socket to parent: %m");
2961 _exit(EXIT_FAILURE);
2964 _exit(EXIT_SUCCESS);
2967 pair[1] = safe_close(pair[1]);
2968 cfd = receive_one_fd(pair[0], 0);
2970 /* We synchronously wait for the helper, as it shouldn't be slow */
2971 r = wait_for_terminate_and_check("(sd-accept)", pid, WAIT_LOG_ABNORMAL);
2977 /* If we received no fd, we got EIO here. If this happens with a process exit code of EXIT_SUCCESS
2978 * this is a spurious accept(), let's convert that back to EAGAIN here. */
2982 return log_unit_error_errno(UNIT(s), cfd, "Failed to receive connection socket: %m");
2987 cfd = socket_accept_do(s, fd);
2988 if (cfd == -EAGAIN) /* spurious accept(), skip it silently */
2991 return log_unit_error_errno(UNIT(s), cfd, "Failed to accept connection socket: %m");
2996 static int socket_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
2997 SocketPort *p = userdata;
3003 if (p->socket->state != SOCKET_LISTENING)
3006 log_unit_debug(UNIT(p->socket), "Incoming traffic");
3008 if (revents != EPOLLIN) {
3009 if (revents & EPOLLHUP)
3010 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.");
3012 log_unit_error(UNIT(p->socket), "Got unexpected poll event (0x%x) on socket.", revents);
3016 if (p->socket->accept &&
3017 p->type == SOCKET_SOCKET &&
3018 socket_address_can_accept(&p->address)) {
3020 cfd = socket_accept_in_cgroup(p->socket, p, fd);
3021 if (cfd == -EAGAIN) /* Spurious accept() */
3026 socket_apply_socket_options(p->socket, cfd);
3029 socket_enter_running(p->socket, cfd);
3033 socket_enter_stop_pre(p->socket, SOCKET_FAILURE_RESOURCES);
3037 static void socket_sigchld_event(Unit *u, pid_t pid, int code, int status) {
3038 Socket *s = SOCKET(u);
3044 if (pid != s->control_pid)
3049 if (is_clean_exit(code, status, EXIT_CLEAN_COMMAND, NULL))
3051 else if (code == CLD_EXITED)
3052 f = SOCKET_FAILURE_EXIT_CODE;
3053 else if (code == CLD_KILLED)
3054 f = SOCKET_FAILURE_SIGNAL;
3055 else if (code == CLD_DUMPED)
3056 f = SOCKET_FAILURE_CORE_DUMP;
3058 assert_not_reached("Unknown sigchld code");
3060 if (s->control_command) {
3061 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
3063 if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE)
3067 unit_log_process_exit(
3070 socket_exec_command_to_string(s->control_command_id),
3071 f == SOCKET_SUCCESS,
3074 if (s->result == SOCKET_SUCCESS)
3077 if (s->control_command &&
3078 s->control_command->command_next &&
3079 f == SOCKET_SUCCESS) {
3081 log_unit_debug(u, "Running next command for state %s", socket_state_to_string(s->state));
3084 s->control_command = NULL;
3085 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
3087 /* No further commands for this step, so let's figure
3088 * out what to do next */
3090 log_unit_debug(u, "Got final SIGCHLD for state %s", socket_state_to_string(s->state));
3094 case SOCKET_START_PRE:
3095 if (f == SOCKET_SUCCESS)
3096 socket_enter_start_chown(s);
3098 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, f);
3101 case SOCKET_START_CHOWN:
3102 if (f == SOCKET_SUCCESS)
3103 socket_enter_start_post(s);
3105 socket_enter_stop_pre(s, f);
3108 case SOCKET_START_POST:
3109 if (f == SOCKET_SUCCESS)
3110 socket_enter_listening(s);
3112 socket_enter_stop_pre(s, f);
3115 case SOCKET_STOP_PRE:
3116 case SOCKET_STOP_PRE_SIGTERM:
3117 case SOCKET_STOP_PRE_SIGKILL:
3118 socket_enter_stop_post(s, f);
3121 case SOCKET_STOP_POST:
3122 case SOCKET_FINAL_SIGTERM:
3123 case SOCKET_FINAL_SIGKILL:
3124 socket_enter_dead(s, f);
3127 case SOCKET_CLEANING:
3129 if (s->clean_result == SOCKET_SUCCESS)
3130 s->clean_result = f;
3132 socket_enter_dead(s, SOCKET_SUCCESS);
3136 assert_not_reached("Uh, control process died at wrong time.");
3140 /* Notify clients about changed exit status */
3141 unit_add_to_dbus_queue(u);
3144 static int socket_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
3145 Socket *s = SOCKET(userdata);
3148 assert(s->timer_event_source == source);
3152 case SOCKET_START_PRE:
3153 log_unit_warning(UNIT(s), "Starting timed out. Terminating.");
3154 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT);
3157 case SOCKET_START_CHOWN:
3158 case SOCKET_START_POST:
3159 log_unit_warning(UNIT(s), "Starting timed out. Stopping.");
3160 socket_enter_stop_pre(s, SOCKET_FAILURE_TIMEOUT);
3163 case SOCKET_STOP_PRE:
3164 log_unit_warning(UNIT(s), "Stopping timed out. Terminating.");
3165 socket_enter_signal(s, SOCKET_STOP_PRE_SIGTERM, SOCKET_FAILURE_TIMEOUT);
3168 case SOCKET_STOP_PRE_SIGTERM:
3169 if (s->kill_context.send_sigkill) {
3170 log_unit_warning(UNIT(s), "Stopping timed out. Killing.");
3171 socket_enter_signal(s, SOCKET_STOP_PRE_SIGKILL, SOCKET_FAILURE_TIMEOUT);
3173 log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL. Ignoring.");
3174 socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT);
3178 case SOCKET_STOP_PRE_SIGKILL:
3179 log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring.");
3180 socket_enter_stop_post(s, SOCKET_FAILURE_TIMEOUT);
3183 case SOCKET_STOP_POST:
3184 log_unit_warning(UNIT(s), "Stopping timed out (2). Terminating.");
3185 socket_enter_signal(s, SOCKET_FINAL_SIGTERM, SOCKET_FAILURE_TIMEOUT);
3188 case SOCKET_FINAL_SIGTERM:
3189 if (s->kill_context.send_sigkill) {
3190 log_unit_warning(UNIT(s), "Stopping timed out (2). Killing.");
3191 socket_enter_signal(s, SOCKET_FINAL_SIGKILL, SOCKET_FAILURE_TIMEOUT);
3193 log_unit_warning(UNIT(s), "Stopping timed out (2). Skipping SIGKILL. Ignoring.");
3194 socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT);
3198 case SOCKET_FINAL_SIGKILL:
3199 log_unit_warning(UNIT(s), "Still around after SIGKILL (2). Entering failed mode.");
3200 socket_enter_dead(s, SOCKET_FAILURE_TIMEOUT);
3203 case SOCKET_CLEANING:
3204 log_unit_warning(UNIT(s), "Cleaning timed out. killing.");
3206 if (s->clean_result == SOCKET_SUCCESS)
3207 s->clean_result = SOCKET_FAILURE_TIMEOUT;
3209 socket_enter_signal(s, SOCKET_FINAL_SIGKILL, 0);
3213 assert_not_reached("Timeout at wrong time.");
3219 int socket_collect_fds(Socket *s, int **fds) {
3220 size_t k = 0, n = 0;
3227 /* Called from the service code for requesting our fds */
3229 LIST_FOREACH(port, p, s->ports) {
3232 n += p->n_auxiliary_fds;
3244 LIST_FOREACH(port, p, s->ports) {
3249 for (i = 0; i < p->n_auxiliary_fds; ++i)
3250 rfds[k++] = p->auxiliary_fds[i];
3259 static void socket_reset_failed(Unit *u) {
3260 Socket *s = SOCKET(u);
3264 if (s->state == SOCKET_FAILED)
3265 socket_set_state(s, SOCKET_DEAD);
3267 s->result = SOCKET_SUCCESS;
3268 s->clean_result = SOCKET_SUCCESS;
3271 void socket_connection_unref(Socket *s) {
3274 /* The service is dead. Yay!
3276 * This is strictly for one-instance-per-connection
3279 assert(s->n_connections > 0);
3282 log_unit_debug(UNIT(s), "One connection closed, %u left.", s->n_connections);
3285 static void socket_trigger_notify(Unit *u, Unit *other) {
3286 Socket *s = SOCKET(u);
3291 /* Filter out invocations with bogus state */
3292 if (other->load_state != UNIT_LOADED || other->type != UNIT_SERVICE)
3295 /* Don't propagate state changes from the service if we are already down */
3296 if (!IN_SET(s->state, SOCKET_RUNNING, SOCKET_LISTENING))
3299 /* We don't care for the service state if we are in Accept=yes mode */
3303 /* Propagate start limit hit state */
3304 if (other->start_limit_hit) {
3305 socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT);
3309 /* Don't propagate anything if there's still a job queued */
3313 if (IN_SET(SERVICE(other)->state,
3314 SERVICE_DEAD, SERVICE_FAILED,
3315 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
3316 SERVICE_AUTO_RESTART))
3317 socket_enter_listening(s);
3319 if (SERVICE(other)->state == SERVICE_RUNNING)
3320 socket_set_state(s, SOCKET_RUNNING);
3323 static int socket_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
3324 return unit_kill_common(u, who, signo, -1, SOCKET(u)->control_pid, error);
3327 static int socket_get_timeout(Unit *u, usec_t *timeout) {
3328 Socket *s = SOCKET(u);
3332 if (!s->timer_event_source)
3335 r = sd_event_source_get_time(s->timer_event_source, &t);
3338 if (t == USEC_INFINITY)
3345 char *socket_fdname(Socket *s) {
3348 /* Returns the name to use for $LISTEN_NAMES. If the user
3349 * didn't specify anything specifically, use the socket unit's
3350 * name as fallback. */
3352 return s->fdname ?: UNIT(s)->id;
3355 static int socket_control_pid(Unit *u) {
3356 Socket *s = SOCKET(u);
3360 return s->control_pid;
3363 static int socket_clean(Unit *u, ExecCleanMask mask) {
3364 _cleanup_strv_free_ char **l = NULL;
3365 Socket *s = SOCKET(u);
3371 if (s->state != SOCKET_DEAD)
3374 r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l);
3378 if (strv_isempty(l))
3381 socket_unwatch_control_pid(s);
3382 s->clean_result = SOCKET_SUCCESS;
3383 s->control_command = NULL;
3384 s->control_command_id = _SOCKET_EXEC_COMMAND_INVALID;
3386 r = socket_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->exec_context.timeout_clean_usec));
3390 r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid);
3394 socket_set_state(s, SOCKET_CLEANING);
3399 log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m");
3400 s->clean_result = SOCKET_FAILURE_RESOURCES;
3401 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
3405 static int socket_can_clean(Unit *u, ExecCleanMask *ret) {
3406 Socket *s = SOCKET(u);
3410 return exec_context_get_clean_mask(&s->exec_context, ret);
3413 static const char* const socket_exec_command_table[_SOCKET_EXEC_COMMAND_MAX] = {
3414 [SOCKET_EXEC_START_PRE] = "ExecStartPre",
3415 [SOCKET_EXEC_START_CHOWN] = "ExecStartChown",
3416 [SOCKET_EXEC_START_POST] = "ExecStartPost",
3417 [SOCKET_EXEC_STOP_PRE] = "ExecStopPre",
3418 [SOCKET_EXEC_STOP_POST] = "ExecStopPost"
3421 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command, SocketExecCommand);
3423 static const char* const socket_result_table[_SOCKET_RESULT_MAX] = {
3424 [SOCKET_SUCCESS] = "success",
3425 [SOCKET_FAILURE_RESOURCES] = "resources",
3426 [SOCKET_FAILURE_TIMEOUT] = "timeout",
3427 [SOCKET_FAILURE_EXIT_CODE] = "exit-code",
3428 [SOCKET_FAILURE_SIGNAL] = "signal",
3429 [SOCKET_FAILURE_CORE_DUMP] = "core-dump",
3430 [SOCKET_FAILURE_START_LIMIT_HIT] = "start-limit-hit",
3431 [SOCKET_FAILURE_TRIGGER_LIMIT_HIT] = "trigger-limit-hit",
3432 [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT] = "service-start-limit-hit"
3435 DEFINE_STRING_TABLE_LOOKUP(socket_result, SocketResult);
3437 const UnitVTable socket_vtable = {
3438 .object_size = sizeof(Socket),
3439 .exec_context_offset = offsetof(Socket, exec_context),
3440 .cgroup_context_offset = offsetof(Socket, cgroup_context),
3441 .kill_context_offset = offsetof(Socket, kill_context),
3442 .exec_runtime_offset = offsetof(Socket, exec_runtime),
3443 .dynamic_creds_offset = offsetof(Socket, dynamic_creds),
3449 .private_section = "Socket",
3451 .can_transient = true,
3453 .init = socket_init,
3454 .done = socket_done,
3455 .load = socket_load,
3457 .coldplug = socket_coldplug,
3459 .dump = socket_dump,
3461 .start = socket_start,
3462 .stop = socket_stop,
3464 .kill = socket_kill,
3465 .clean = socket_clean,
3466 .can_clean = socket_can_clean,
3468 .get_timeout = socket_get_timeout,
3470 .serialize = socket_serialize,
3471 .deserialize_item = socket_deserialize_item,
3472 .distribute_fds = socket_distribute_fds,
3474 .active_state = socket_active_state,
3475 .sub_state_to_string = socket_sub_state_to_string,
3477 .will_restart = unit_will_restart_default,
3479 .may_gc = socket_may_gc,
3481 .sigchld_event = socket_sigchld_event,
3483 .trigger_notify = socket_trigger_notify,
3485 .reset_failed = socket_reset_failed,
3487 .control_pid = socket_control_pid,
3489 .bus_vtable = bus_socket_vtable,
3490 .bus_set_property = bus_socket_set_property,
3491 .bus_commit_properties = bus_socket_commit_properties,
3493 .status_message_formats = {
3494 /*.starting_stopping = {
3495 [0] = "Starting socket %s...",
3496 [1] = "Stopping socket %s...",
3498 .finished_start_job = {
3499 [JOB_DONE] = "Listening on %s.",
3500 [JOB_FAILED] = "Failed to listen on %s.",
3501 [JOB_TIMEOUT] = "Timed out starting %s.",
3503 .finished_stop_job = {
3504 [JOB_DONE] = "Closed %s.",
3505 [JOB_FAILED] = "Failed stopping %s.",
3506 [JOB_TIMEOUT] = "Timed out stopping %s.",