1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <netinet/tcp.h>
12 #include <linux/sctp.h>
14 #include "alloc-util.h"
15 #include "bpf-firewall.h"
16 #include "bus-error.h"
19 #include "dbus-socket.h"
20 #include "dbus-unit.h"
22 #include "exit-status.h"
24 #include "format-util.h"
26 #include "in-addr-util.h"
28 #include "ip-protocol-list.h"
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "process-util.h"
36 #include "selinux-util.h"
37 #include "serialize.h"
38 #include "signal-util.h"
39 #include "smack-util.h"
42 #include "string-table.h"
43 #include "string-util.h"
45 #include "unit-name.h"
47 #include "user-util.h"
53 union sockaddr_union peer
;
57 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
58 [SOCKET_DEAD
] = UNIT_INACTIVE
,
59 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
60 [SOCKET_START_CHOWN
] = UNIT_ACTIVATING
,
61 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
62 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
63 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
64 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
65 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
66 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
67 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
68 [SOCKET_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
69 [SOCKET_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
70 [SOCKET_FAILED
] = UNIT_FAILED
,
71 [SOCKET_CLEANING
] = UNIT_MAINTENANCE
,
74 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
75 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
77 static void socket_init(Unit
*u
) {
78 Socket
*s
= SOCKET(u
);
81 assert(u
->load_state
== UNIT_STUB
);
83 s
->backlog
= SOMAXCONN
;
84 s
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
85 s
->directory_mode
= 0755;
86 s
->socket_mode
= 0666;
88 s
->max_connections
= 64;
95 s
->exec_context
.std_output
= u
->manager
->default_std_output
;
96 s
->exec_context
.std_error
= u
->manager
->default_std_error
;
98 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
100 s
->trigger_limit
.interval
= USEC_INFINITY
;
101 s
->trigger_limit
.burst
= (unsigned) -1;
104 static void socket_unwatch_control_pid(Socket
*s
) {
107 if (s
->control_pid
<= 0)
110 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
114 static void socket_cleanup_fd_list(SocketPort
*p
) {
117 close_many(p
->auxiliary_fds
, p
->n_auxiliary_fds
);
118 p
->auxiliary_fds
= mfree(p
->auxiliary_fds
);
119 p
->n_auxiliary_fds
= 0;
122 void socket_free_ports(Socket
*s
) {
127 while ((p
= s
->ports
)) {
128 LIST_REMOVE(port
, s
->ports
, p
);
130 sd_event_source_unref(p
->event_source
);
132 socket_cleanup_fd_list(p
);
139 static void socket_done(Unit
*u
) {
140 Socket
*s
= SOCKET(u
);
145 socket_free_ports(s
);
147 while ((p
= set_steal_first(s
->peers_by_address
)))
150 s
->peers_by_address
= set_free(s
->peers_by_address
);
152 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
, false);
153 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
154 s
->control_command
= NULL
;
156 dynamic_creds_unref(&s
->dynamic_creds
);
158 socket_unwatch_control_pid(s
);
160 unit_ref_unset(&s
->service
);
162 s
->tcp_congestion
= mfree(s
->tcp_congestion
);
163 s
->bind_to_device
= mfree(s
->bind_to_device
);
165 s
->smack
= mfree(s
->smack
);
166 s
->smack_ip_in
= mfree(s
->smack_ip_in
);
167 s
->smack_ip_out
= mfree(s
->smack_ip_out
);
169 strv_free(s
->symlinks
);
171 s
->user
= mfree(s
->user
);
172 s
->group
= mfree(s
->group
);
174 s
->fdname
= mfree(s
->fdname
);
176 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
179 static int socket_arm_timer(Socket
*s
, usec_t usec
) {
184 if (s
->timer_event_source
) {
185 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
189 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
192 if (usec
== USEC_INFINITY
)
195 r
= sd_event_add_time(
196 UNIT(s
)->manager
->event
,
197 &s
->timer_event_source
,
200 socket_dispatch_timer
, s
);
204 (void) sd_event_source_set_description(s
->timer_event_source
, "socket-timer");
209 int socket_instantiate_service(Socket
*s
) {
210 _cleanup_free_
char *prefix
= NULL
, *name
= NULL
;
216 /* This fills in s->service if it isn't filled in yet. For
217 * Accept=yes sockets we create the next connection service
218 * here. For Accept=no this is mostly a NOP since the service
219 * is figured out at load time anyway. */
221 if (UNIT_DEREF(s
->service
))
227 r
= unit_name_to_prefix(UNIT(s
)->id
, &prefix
);
231 if (asprintf(&name
, "%s@%u.service", prefix
, s
->n_accepted
) < 0)
234 r
= manager_load_unit(UNIT(s
)->manager
, name
, NULL
, NULL
, &u
);
238 unit_ref_set(&s
->service
, UNIT(s
), u
);
240 return unit_add_two_dependencies(UNIT(s
), UNIT_BEFORE
, UNIT_TRIGGERS
, u
, false, UNIT_DEPENDENCY_IMPLICIT
);
243 static bool have_non_accept_socket(Socket
*s
) {
251 LIST_FOREACH(port
, p
, s
->ports
) {
253 if (p
->type
!= SOCKET_SOCKET
)
256 if (!socket_address_can_accept(&p
->address
))
263 static int socket_add_mount_dependencies(Socket
*s
) {
269 LIST_FOREACH(port
, p
, s
->ports
) {
270 const char *path
= NULL
;
272 if (p
->type
== SOCKET_SOCKET
)
273 path
= socket_address_get_path(&p
->address
);
274 else if (IN_SET(p
->type
, SOCKET_FIFO
, SOCKET_SPECIAL
, SOCKET_USB_FUNCTION
))
280 r
= unit_require_mounts_for(UNIT(s
), path
, UNIT_DEPENDENCY_FILE
);
288 static int socket_add_device_dependencies(Socket
*s
) {
293 if (!s
->bind_to_device
|| streq(s
->bind_to_device
, "lo"))
296 t
= strjoina("/sys/subsystem/net/devices/", s
->bind_to_device
);
297 return unit_add_node_dependency(UNIT(s
), t
, false, UNIT_BINDS_TO
, UNIT_DEPENDENCY_FILE
);
300 static int socket_add_default_dependencies(Socket
*s
) {
304 if (!UNIT(s
)->default_dependencies
)
307 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_BEFORE
, SPECIAL_SOCKETS_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
311 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
312 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
317 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
320 _pure_
static bool socket_has_exec(Socket
*s
) {
324 for (i
= 0; i
< _SOCKET_EXEC_COMMAND_MAX
; i
++)
325 if (s
->exec_command
[i
])
331 static int socket_add_extras(Socket
*s
) {
337 /* Pick defaults for the trigger limit, if nothing was explicitly configured. We pick a relatively high limit
338 * in Accept=yes mode, and a lower limit for Accept=no. Reason: in Accept=yes mode we are invoking accept()
339 * ourselves before the trigger limit can hit, thus incoming connections are taken off the socket queue quickly
340 * and reliably. This is different for Accept=no, where the spawned service has to take the incoming traffic
341 * off the queues, which it might not necessarily do. Moreover, while Accept=no services are supposed to
342 * process whatever is queued in one go, and thus should normally never have to be started frequently. This is
343 * different for Accept=yes where each connection is processed by a new service instance, and thus frequent
344 * service starts are typical. */
346 if (s
->trigger_limit
.interval
== USEC_INFINITY
)
347 s
->trigger_limit
.interval
= 2 * USEC_PER_SEC
;
349 if (s
->trigger_limit
.burst
== (unsigned) -1) {
351 s
->trigger_limit
.burst
= 200;
353 s
->trigger_limit
.burst
= 20;
356 if (have_non_accept_socket(s
)) {
358 if (!UNIT_DEREF(s
->service
)) {
361 r
= unit_load_related_unit(u
, ".service", &x
);
365 unit_ref_set(&s
->service
, u
, x
);
368 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT_DEREF(s
->service
), true, UNIT_DEPENDENCY_IMPLICIT
);
373 r
= socket_add_mount_dependencies(s
);
377 r
= socket_add_device_dependencies(s
);
381 r
= unit_patch_contexts(u
);
385 if (socket_has_exec(s
)) {
386 r
= unit_add_exec_dependencies(u
, &s
->exec_context
);
391 r
= unit_set_default_slice(u
);
395 r
= socket_add_default_dependencies(s
);
402 static const char *socket_find_symlink_target(Socket
*s
) {
403 const char *found
= NULL
;
406 LIST_FOREACH(port
, p
, s
->ports
) {
407 const char *f
= NULL
;
416 f
= socket_address_get_path(&p
->address
);
434 static int socket_verify(Socket
*s
) {
437 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
441 log_unit_error(UNIT(s
), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing.");
445 if (s
->accept
&& have_non_accept_socket(s
)) {
446 log_unit_error(UNIT(s
), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing.");
450 if (s
->accept
&& s
->max_connections
<= 0) {
451 log_unit_error(UNIT(s
), "MaxConnection= setting too small. Refusing.");
455 if (s
->accept
&& UNIT_DEREF(s
->service
)) {
456 log_unit_error(UNIT(s
), "Explicit service configuration for accepting socket units not supported. Refusing.");
460 if (s
->exec_context
.pam_name
&& s
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
461 log_unit_error(UNIT(s
), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing.");
465 if (!strv_isempty(s
->symlinks
) && !socket_find_symlink_target(s
)) {
466 log_unit_error(UNIT(s
), "Unit has symlinks set but none or more than one node in the file system. Refusing.");
473 static void peer_address_hash_func(const SocketPeer
*s
, struct siphash
*state
) {
476 if (s
->peer
.sa
.sa_family
== AF_INET
)
477 siphash24_compress(&s
->peer
.in
.sin_addr
, sizeof(s
->peer
.in
.sin_addr
), state
);
478 else if (s
->peer
.sa
.sa_family
== AF_INET6
)
479 siphash24_compress(&s
->peer
.in6
.sin6_addr
, sizeof(s
->peer
.in6
.sin6_addr
), state
);
480 else if (s
->peer
.sa
.sa_family
== AF_VSOCK
)
481 siphash24_compress(&s
->peer
.vm
.svm_cid
, sizeof(s
->peer
.vm
.svm_cid
), state
);
483 assert_not_reached("Unknown address family.");
486 static int peer_address_compare_func(const SocketPeer
*x
, const SocketPeer
*y
) {
489 r
= CMP(x
->peer
.sa
.sa_family
, y
->peer
.sa
.sa_family
);
493 switch(x
->peer
.sa
.sa_family
) {
495 return memcmp(&x
->peer
.in
.sin_addr
, &y
->peer
.in
.sin_addr
, sizeof(x
->peer
.in
.sin_addr
));
497 return memcmp(&x
->peer
.in6
.sin6_addr
, &y
->peer
.in6
.sin6_addr
, sizeof(x
->peer
.in6
.sin6_addr
));
499 return CMP(x
->peer
.vm
.svm_cid
, y
->peer
.vm
.svm_cid
);
501 assert_not_reached("Black sheep in the family!");
504 DEFINE_PRIVATE_HASH_OPS(peer_address_hash_ops
, SocketPeer
, peer_address_hash_func
, peer_address_compare_func
);
506 static int socket_load(Unit
*u
) {
507 Socket
*s
= SOCKET(u
);
511 assert(u
->load_state
== UNIT_STUB
);
513 r
= set_ensure_allocated(&s
->peers_by_address
, &peer_address_hash_ops
);
517 r
= unit_load_fragment_and_dropin(u
, true);
521 if (u
->load_state
== UNIT_LOADED
) {
522 /* This is a new unit? Then let's add in some extras */
523 r
= socket_add_extras(s
);
528 return socket_verify(s
);
531 static SocketPeer
*socket_peer_new(void) {
534 p
= new0(SocketPeer
, 1);
543 static SocketPeer
*socket_peer_free(SocketPeer
*p
) {
547 set_remove(p
->socket
->peers_by_address
, p
);
552 DEFINE_TRIVIAL_REF_UNREF_FUNC(SocketPeer
, socket_peer
, socket_peer_free
);
554 int socket_acquire_peer(Socket
*s
, int fd
, SocketPeer
**p
) {
555 _cleanup_(socket_peer_unrefp
) SocketPeer
*remote
= NULL
;
556 SocketPeer sa
= {}, *i
;
557 socklen_t salen
= sizeof(sa
.peer
);
563 r
= getpeername(fd
, &sa
.peer
.sa
, &salen
);
565 return log_unit_error_errno(UNIT(s
), errno
, "getpeername failed: %m");
567 if (!IN_SET(sa
.peer
.sa
.sa_family
, AF_INET
, AF_INET6
, AF_VSOCK
)) {
572 i
= set_get(s
->peers_by_address
, &sa
);
574 *p
= socket_peer_ref(i
);
578 remote
= socket_peer_new();
582 remote
->peer
= sa
.peer
;
583 remote
->peer_salen
= salen
;
585 r
= set_put(s
->peers_by_address
, remote
);
591 *p
= TAKE_PTR(remote
);
596 _const_
static const char* listen_lookup(int family
, int type
) {
598 if (family
== AF_NETLINK
)
599 return "ListenNetlink";
601 if (type
== SOCK_STREAM
)
602 return "ListenStream";
603 else if (type
== SOCK_DGRAM
)
604 return "ListenDatagram";
605 else if (type
== SOCK_SEQPACKET
)
606 return "ListenSequentialPacket";
608 assert_not_reached("Unknown socket type");
612 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
613 char time_string
[FORMAT_TIMESPAN_MAX
];
615 Socket
*s
= SOCKET(u
);
617 const char *prefix2
, *str
;
622 prefix
= strempty(prefix
);
623 prefix2
= strjoina(prefix
, "\t");
626 "%sSocket State: %s\n"
628 "%sClean Result: %s\n"
629 "%sBindIPv6Only: %s\n"
631 "%sSocketMode: %04o\n"
632 "%sDirectoryMode: %04o\n"
636 "%sTransparent: %s\n"
638 "%sPassCredentials: %s\n"
639 "%sPassSecurity: %s\n"
640 "%sTCPCongestion: %s\n"
641 "%sRemoveOnStop: %s\n"
643 "%sFileDescriptorName: %s\n"
644 "%sSELinuxContextFromNet: %s\n",
645 prefix
, socket_state_to_string(s
->state
),
646 prefix
, socket_result_to_string(s
->result
),
647 prefix
, socket_result_to_string(s
->clean_result
),
648 prefix
, socket_address_bind_ipv6_only_to_string(s
->bind_ipv6_only
),
650 prefix
, s
->socket_mode
,
651 prefix
, s
->directory_mode
,
652 prefix
, yes_no(s
->keep_alive
),
653 prefix
, yes_no(s
->no_delay
),
654 prefix
, yes_no(s
->free_bind
),
655 prefix
, yes_no(s
->transparent
),
656 prefix
, yes_no(s
->broadcast
),
657 prefix
, yes_no(s
->pass_cred
),
658 prefix
, yes_no(s
->pass_sec
),
659 prefix
, strna(s
->tcp_congestion
),
660 prefix
, yes_no(s
->remove_on_stop
),
661 prefix
, yes_no(s
->writable
),
662 prefix
, socket_fdname(s
),
663 prefix
, yes_no(s
->selinux_context_from_net
));
665 if (s
->control_pid
> 0)
667 "%sControl PID: "PID_FMT
"\n",
668 prefix
, s
->control_pid
);
670 if (s
->bind_to_device
)
672 "%sBindToDevice: %s\n",
673 prefix
, s
->bind_to_device
);
678 "%sNConnections: %u\n"
679 "%sMaxConnections: %u\n"
680 "%sMaxConnectionsPerSource: %u\n",
681 prefix
, s
->n_accepted
,
682 prefix
, s
->n_connections
,
683 prefix
, s
->max_connections
,
684 prefix
, s
->max_connections_per_source
);
686 if (s
->priority
>= 0)
689 prefix
, s
->priority
);
691 if (s
->receive_buffer
> 0)
693 "%sReceiveBuffer: %zu\n",
694 prefix
, s
->receive_buffer
);
696 if (s
->send_buffer
> 0)
698 "%sSendBuffer: %zu\n",
699 prefix
, s
->send_buffer
);
711 if (s
->pipe_size
> 0)
714 prefix
, s
->pipe_size
);
721 if (s
->mq_maxmsg
> 0)
723 "%sMessageQueueMaxMessages: %li\n",
724 prefix
, s
->mq_maxmsg
);
726 if (s
->mq_msgsize
> 0)
728 "%sMessageQueueMessageSize: %li\n",
729 prefix
, s
->mq_msgsize
);
734 prefix
, yes_no(s
->reuse_port
));
738 "%sSmackLabel: %s\n",
743 "%sSmackLabelIPIn: %s\n",
744 prefix
, s
->smack_ip_in
);
748 "%sSmackLabelIPOut: %s\n",
749 prefix
, s
->smack_ip_out
);
751 if (!isempty(s
->user
) || !isempty(s
->group
))
754 "%sSocketGroup: %s\n",
755 prefix
, strna(s
->user
),
756 prefix
, strna(s
->group
));
758 if (s
->keep_alive_time
> 0)
760 "%sKeepAliveTimeSec: %s\n",
761 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->keep_alive_time
, USEC_PER_SEC
));
763 if (s
->keep_alive_interval
> 0)
765 "%sKeepAliveIntervalSec: %s\n",
766 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->keep_alive_interval
, USEC_PER_SEC
));
768 if (s
->keep_alive_cnt
> 0)
770 "%sKeepAliveProbes: %u\n",
771 prefix
, s
->keep_alive_cnt
);
773 if (s
->defer_accept
> 0)
775 "%sDeferAcceptSec: %s\n",
776 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->defer_accept
, USEC_PER_SEC
));
778 LIST_FOREACH(port
, p
, s
->ports
) {
781 case SOCKET_SOCKET
: {
782 _cleanup_free_
char *k
= NULL
;
786 r
= socket_address_print(&p
->address
, &k
);
788 t
= strerror_safe(r
);
792 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(socket_address_family(&p
->address
), p
->address
.type
), t
);
796 fprintf(f
, "%sListenSpecial: %s\n", prefix
, p
->path
);
798 case SOCKET_USB_FUNCTION
:
799 fprintf(f
, "%sListenUSBFunction: %s\n", prefix
, p
->path
);
802 fprintf(f
, "%sListenMessageQueue: %s\n", prefix
, p
->path
);
805 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
810 "%sTriggerLimitIntervalSec: %s\n"
811 "%sTriggerLimitBurst: %u\n",
812 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->trigger_limit
.interval
, USEC_PER_SEC
),
813 prefix
, s
->trigger_limit
.burst
);
815 str
= ip_protocol_to_name(s
->socket_protocol
);
817 fprintf(f
, "%sSocketProtocol: %s\n", prefix
, str
);
819 if (!strv_isempty(s
->symlinks
)) {
822 fprintf(f
, "%sSymlinks:", prefix
);
823 STRV_FOREACH(q
, s
->symlinks
)
824 fprintf(f
, " %s", *q
);
830 "%sTimeoutSec: %s\n",
831 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->timeout_usec
, USEC_PER_SEC
));
833 exec_context_dump(&s
->exec_context
, f
, prefix
);
834 kill_context_dump(&s
->kill_context
, f
, prefix
);
836 for (c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
837 if (!s
->exec_command
[c
])
840 fprintf(f
, "%s-> %s:\n",
841 prefix
, socket_exec_command_to_string(c
));
843 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
846 cgroup_context_dump(UNIT(s
), f
, prefix
);
849 static int instance_from_socket(int fd
, unsigned nr
, char **instance
) {
852 union sockaddr_union local
, remote
;
858 if (getsockname(fd
, &local
.sa
, &l
) < 0)
862 if (getpeername(fd
, &remote
.sa
, &l
) < 0)
865 switch (local
.sa
.sa_family
) {
869 a
= be32toh(local
.in
.sin_addr
.s_addr
),
870 b
= be32toh(remote
.in
.sin_addr
.s_addr
);
873 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
875 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
876 be16toh(local
.in
.sin_port
),
877 b
>> 24, (b
>> 16) & 0xFF, (b
>> 8) & 0xFF, b
& 0xFF,
878 be16toh(remote
.in
.sin_port
)) < 0)
885 static const unsigned char ipv4_prefix
[] = {
886 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
889 if (memcmp(&local
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0 &&
890 memcmp(&remote
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
892 *a
= local
.in6
.sin6_addr
.s6_addr
+12,
893 *b
= remote
.in6
.sin6_addr
.s6_addr
+12;
896 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
898 a
[0], a
[1], a
[2], a
[3],
899 be16toh(local
.in6
.sin6_port
),
900 b
[0], b
[1], b
[2], b
[3],
901 be16toh(remote
.in6
.sin6_port
)) < 0)
904 char a
[INET6_ADDRSTRLEN
], b
[INET6_ADDRSTRLEN
];
909 inet_ntop(AF_INET6
, &local
.in6
.sin6_addr
, a
, sizeof(a
)),
910 be16toh(local
.in6
.sin6_port
),
911 inet_ntop(AF_INET6
, &remote
.in6
.sin6_addr
, b
, sizeof(b
)),
912 be16toh(remote
.in6
.sin6_port
)) < 0)
923 k
= getpeercred(fd
, &ucred
);
926 "%u-"PID_FMT
"-"UID_FMT
,
927 nr
, ucred
.pid
, ucred
.uid
) < 0)
929 } else if (k
== -ENODATA
) {
930 /* This handles the case where somebody is
931 * connecting from another pid/uid namespace
932 * (e.g. from outside of our container). */
947 local
.vm
.svm_cid
, local
.vm
.svm_port
,
948 remote
.vm
.svm_cid
, remote
.vm
.svm_port
) < 0)
954 assert_not_reached("Unhandled socket type.");
961 static void socket_close_fds(Socket
*s
) {
967 LIST_FOREACH(port
, p
, s
->ports
) {
970 was_open
= p
->fd
>= 0;
972 p
->event_source
= sd_event_source_unref(p
->event_source
);
973 p
->fd
= safe_close(p
->fd
);
974 socket_cleanup_fd_list(p
);
976 /* One little note: we should normally not delete any sockets in the file system here! After all some
977 * other process we spawned might still have a reference of this fd and wants to continue to use
978 * it. Therefore we normally delete sockets in the file system before we create a new one, not after we
979 * stopped using one! That all said, if the user explicitly requested this, we'll delete them here
980 * anyway, but only then. */
982 if (!was_open
|| !s
->remove_on_stop
)
988 (void) unlink(p
->path
);
992 (void) mq_unlink(p
->path
);
996 (void) socket_address_unlink(&p
->address
);
1004 if (s
->remove_on_stop
)
1005 STRV_FOREACH(i
, s
->symlinks
)
1009 static void socket_apply_socket_options(Socket
*s
, int fd
) {
1015 if (s
->keep_alive
) {
1016 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_KEEPALIVE
, true);
1018 log_unit_warning_errno(UNIT(s
), r
, "SO_KEEPALIVE failed: %m");
1021 if (s
->keep_alive_time
> 0) {
1022 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPIDLE
, s
->keep_alive_time
/ USEC_PER_SEC
);
1024 log_unit_warning_errno(UNIT(s
), r
, "TCP_KEEPIDLE failed: %m");
1027 if (s
->keep_alive_interval
> 0) {
1028 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPINTVL
, s
->keep_alive_interval
/ USEC_PER_SEC
);
1030 log_unit_warning_errno(UNIT(s
), r
, "TCP_KEEPINTVL failed: %m");
1033 if (s
->keep_alive_cnt
> 0) {
1034 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPCNT
, s
->keep_alive_cnt
);
1036 log_unit_warning_errno(UNIT(s
), r
, "TCP_KEEPCNT failed: %m");
1039 if (s
->defer_accept
> 0) {
1040 r
= setsockopt_int(fd
, SOL_TCP
, TCP_DEFER_ACCEPT
, s
->defer_accept
/ USEC_PER_SEC
);
1042 log_unit_warning_errno(UNIT(s
), r
, "TCP_DEFER_ACCEPT failed: %m");
1046 if (s
->socket_protocol
== IPPROTO_SCTP
) {
1047 r
= setsockopt_int(fd
, SOL_SCTP
, SCTP_NODELAY
, true);
1049 log_unit_warning_errno(UNIT(s
), r
, "SCTP_NODELAY failed: %m");
1051 r
= setsockopt_int(fd
, SOL_TCP
, TCP_NODELAY
, true);
1053 log_unit_warning_errno(UNIT(s
), r
, "TCP_NODELAY failed: %m");
1058 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_BROADCAST
, true);
1060 log_unit_warning_errno(UNIT(s
), r
, "SO_BROADCAST failed: %m");
1064 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSCRED
, true);
1066 log_unit_warning_errno(UNIT(s
), r
, "SO_PASSCRED failed: %m");
1070 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSSEC
, true);
1072 log_unit_warning_errno(UNIT(s
), r
, "SO_PASSSEC failed: %m");
1075 if (s
->priority
>= 0) {
1076 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PRIORITY
, s
->priority
);
1078 log_unit_warning_errno(UNIT(s
), r
, "SO_PRIORITY failed: %m");
1081 if (s
->receive_buffer
> 0) {
1082 /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
1083 if (setsockopt_int(fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, s
->receive_buffer
) < 0) {
1084 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_RCVBUF
, s
->receive_buffer
);
1086 log_unit_warning_errno(UNIT(s
), r
, "SO_RCVBUF failed: %m");
1090 if (s
->send_buffer
> 0) {
1091 if (setsockopt_int(fd
, SOL_SOCKET
, SO_SNDBUFFORCE
, s
->send_buffer
) < 0) {
1092 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_SNDBUF
, s
->send_buffer
);
1094 log_unit_warning_errno(UNIT(s
), r
, "SO_SNDBUF failed: %m");
1099 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_MARK
, s
->mark
);
1101 log_unit_warning_errno(UNIT(s
), r
, "SO_MARK failed: %m");
1104 if (s
->ip_tos
>= 0) {
1105 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_TOS
, s
->ip_tos
);
1107 log_unit_warning_errno(UNIT(s
), r
, "IP_TOS failed: %m");
1110 if (s
->ip_ttl
>= 0) {
1113 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_TTL
, s
->ip_ttl
);
1115 if (socket_ipv6_is_supported())
1116 x
= setsockopt_int(fd
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, s
->ip_ttl
);
1121 log_unit_warning_errno(UNIT(s
), r
, "IP_TTL/IPV6_UNICAST_HOPS failed: %m");
1124 if (s
->tcp_congestion
)
1125 if (setsockopt(fd
, SOL_TCP
, TCP_CONGESTION
, s
->tcp_congestion
, strlen(s
->tcp_congestion
)+1) < 0)
1126 log_unit_warning_errno(UNIT(s
), errno
, "TCP_CONGESTION failed: %m");
1128 if (s
->smack_ip_in
) {
1129 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_IPIN
, s
->smack_ip_in
);
1131 log_unit_error_errno(UNIT(s
), r
, "mac_smack_apply_ip_in_fd: %m");
1134 if (s
->smack_ip_out
) {
1135 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_IPOUT
, s
->smack_ip_out
);
1137 log_unit_error_errno(UNIT(s
), r
, "mac_smack_apply_ip_out_fd: %m");
1141 static void socket_apply_fifo_options(Socket
*s
, int fd
) {
1147 if (s
->pipe_size
> 0)
1148 if (fcntl(fd
, F_SETPIPE_SZ
, s
->pipe_size
) < 0)
1149 log_unit_warning_errno(UNIT(s
), errno
, "Setting pipe size failed, ignoring: %m");
1152 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_ACCESS
, s
->smack
);
1154 log_unit_error_errno(UNIT(s
), r
, "SMACK relabelling failed, ignoring: %m");
1158 static int fifo_address_create(
1160 mode_t directory_mode
,
1161 mode_t socket_mode
) {
1163 _cleanup_close_
int fd
= -1;
1170 (void) mkdir_parents_label(path
, directory_mode
);
1172 r
= mac_selinux_create_file_prepare(path
, S_IFIFO
);
1176 /* Enforce the right access mode for the fifo */
1177 old_mask
= umask(~socket_mode
);
1179 /* Include the original umask in our mask */
1180 (void) umask(~socket_mode
| old_mask
);
1182 r
= mkfifo(path
, socket_mode
);
1183 (void) umask(old_mask
);
1185 if (r
< 0 && errno
!= EEXIST
) {
1190 fd
= open(path
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
| O_NOFOLLOW
);
1196 mac_selinux_create_file_clear();
1198 if (fstat(fd
, &st
) < 0) {
1203 if (!S_ISFIFO(st
.st_mode
) ||
1204 (st
.st_mode
& 0777) != (socket_mode
& ~old_mask
) ||
1205 st
.st_uid
!= getuid() ||
1206 st
.st_gid
!= getgid()) {
1214 mac_selinux_create_file_clear();
1218 static int special_address_create(const char *path
, bool writable
) {
1219 _cleanup_close_
int fd
= -1;
1224 fd
= open(path
, (writable
? O_RDWR
: O_RDONLY
)|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
);
1228 if (fstat(fd
, &st
) < 0)
1231 /* Check whether this is a /proc, /sys or /dev file or char device */
1232 if (!S_ISREG(st
.st_mode
) && !S_ISCHR(st
.st_mode
))
1238 static int usbffs_address_create(const char *path
) {
1239 _cleanup_close_
int fd
= -1;
1244 fd
= open(path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
);
1248 if (fstat(fd
, &st
) < 0)
1251 /* Check whether this is a regular file (ffs endpoint) */
1252 if (!S_ISREG(st
.st_mode
))
1258 static int mq_address_create(
1264 _cleanup_close_
int fd
= -1;
1267 struct mq_attr _attr
, *attr
= NULL
;
1271 if (maxmsg
> 0 && msgsize
> 0) {
1272 _attr
= (struct mq_attr
) {
1273 .mq_flags
= O_NONBLOCK
,
1274 .mq_maxmsg
= maxmsg
,
1275 .mq_msgsize
= msgsize
,
1280 /* Enforce the right access mode for the mq */
1281 old_mask
= umask(~mq_mode
);
1283 /* Include the original umask in our mask */
1284 (void) umask(~mq_mode
| old_mask
);
1285 fd
= mq_open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_CREAT
, mq_mode
, attr
);
1286 (void) umask(old_mask
);
1291 if (fstat(fd
, &st
) < 0)
1294 if ((st
.st_mode
& 0777) != (mq_mode
& ~old_mask
) ||
1295 st
.st_uid
!= getuid() ||
1296 st
.st_gid
!= getgid())
1302 static int socket_symlink(Socket
*s
) {
1309 p
= socket_find_symlink_target(s
);
1313 STRV_FOREACH(i
, s
->symlinks
) {
1314 (void) mkdir_parents_label(*i
, s
->directory_mode
);
1316 r
= symlink_idempotent(p
, *i
, false);
1318 if (r
== -EEXIST
&& s
->remove_on_stop
) {
1319 /* If there's already something where we want to create the symlink, and the destructive
1320 * RemoveOnStop= mode is set, then we might as well try to remove what already exists and try
1323 if (unlink(*i
) >= 0)
1324 r
= symlink_idempotent(p
, *i
, false);
1328 log_unit_warning_errno(UNIT(s
), r
, "Failed to create symlink %s → %s, ignoring: %m", p
, *i
);
1334 static int usbffs_write_descs(int fd
, Service
*s
) {
1337 if (!s
->usb_function_descriptors
|| !s
->usb_function_strings
)
1340 r
= copy_file_fd(s
->usb_function_descriptors
, fd
, 0);
1344 return copy_file_fd(s
->usb_function_strings
, fd
, 0);
1347 static int usbffs_select_ep(const struct dirent
*d
) {
1348 return d
->d_name
[0] != '.' && !streq(d
->d_name
, "ep0");
1351 static int usbffs_dispatch_eps(SocketPort
*p
) {
1352 _cleanup_free_
struct dirent
**ent
= NULL
;
1356 r
= scandir(p
->path
, &ent
, usbffs_select_ep
, alphasort
);
1361 p
->auxiliary_fds
= new(int, n
);
1362 if (!p
->auxiliary_fds
) {
1367 p
->n_auxiliary_fds
= n
;
1370 for (i
= 0; i
< n
; ++i
) {
1371 _cleanup_free_
char *ep
= NULL
;
1373 ep
= path_make_absolute(ent
[i
]->d_name
, p
->path
);
1379 path_simplify(ep
, false);
1381 r
= usbffs_address_create(ep
);
1385 p
->auxiliary_fds
[k
++] = r
;
1392 close_many(p
->auxiliary_fds
, k
);
1393 p
->auxiliary_fds
= mfree(p
->auxiliary_fds
);
1394 p
->n_auxiliary_fds
= 0;
1397 for (i
= 0; i
< n
; ++i
)
1403 static int socket_determine_selinux_label(Socket
*s
, char **ret
) {
1406 _cleanup_free_
char *path
= NULL
;
1412 if (s
->selinux_context_from_net
) {
1413 /* If this is requested, get label from the network label */
1415 r
= mac_selinux_get_our_label(ret
);
1416 if (r
== -EOPNOTSUPP
)
1420 /* Otherwise, get it from the executable we are about to start */
1421 r
= socket_instantiate_service(s
);
1425 if (!UNIT_ISSET(s
->service
))
1428 service
= SERVICE(UNIT_DEREF(s
->service
));
1429 c
= service
->exec_command
[SERVICE_EXEC_START
];
1433 r
= chase_symlinks(c
->path
, service
->exec_context
.root_directory
, CHASE_PREFIX_ROOT
, &path
);
1437 r
= mac_selinux_get_create_label_from_exe(path
, ret
);
1438 if (IN_SET(r
, -EPERM
, -EOPNOTSUPP
))
1449 static int socket_address_listen_do(
1451 const SocketAddress
*address
,
1452 const char *label
) {
1457 return socket_address_listen(
1459 SOCK_CLOEXEC
|SOCK_NONBLOCK
,
1471 #define log_address_error_errno(u, address, error, fmt) \
1473 _cleanup_free_ char *_t = NULL; \
1475 (void) socket_address_print(address, &_t); \
1476 log_unit_error_errno(u, error, fmt, strna(_t)); \
1479 static int fork_needed(const SocketAddress
*address
, const ExecContext
*context
) {
1485 /* Check if we need to do the cgroup or netns stuff. If not we can do things much simpler. */
1487 if (IN_SET(address
->sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1488 r
= bpf_firewall_supported();
1491 if (r
!= BPF_FIREWALL_UNSUPPORTED
) /* If BPF firewalling isn't supported anyway — there's no point in this forking complexity */
1495 return context
->private_network
|| context
->network_namespace_path
;
1498 static int socket_address_listen_in_cgroup(
1500 const SocketAddress
*address
,
1501 const char *label
) {
1503 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1510 /* This is a wrapper around socket_address_listen(), that forks off a helper process inside the
1511 * socket's cgroup and network namespace in which the socket is actually created. This way we ensure
1512 * the socket is actually properly attached to the unit's cgroup for the purpose of BPF filtering and
1515 r
= fork_needed(address
, &s
->exec_context
);
1519 /* Shortcut things... */
1520 fd
= socket_address_listen_do(s
, address
, label
);
1522 return log_address_error_errno(UNIT(s
), address
, fd
, "Failed to create listening socket (%s): %m");
1527 r
= unit_setup_exec_runtime(UNIT(s
));
1529 return log_unit_error_errno(UNIT(s
), r
, "Failed acquire runtime: %m");
1531 if (s
->exec_context
.network_namespace_path
&&
1533 s
->exec_runtime
->netns_storage_socket
[0] >= 0) {
1534 r
= open_netns_path(s
->exec_runtime
->netns_storage_socket
, s
->exec_context
.network_namespace_path
);
1536 return log_unit_error_errno(UNIT(s
), r
, "Failed to open network namespace path %s: %m", s
->exec_context
.network_namespace_path
);
1539 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
1540 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
1542 r
= unit_fork_helper_process(UNIT(s
), "(sd-listen)", &pid
);
1544 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off listener stub process: %m");
1548 pair
[0] = safe_close(pair
[0]);
1550 if ((s
->exec_context
.private_network
|| s
->exec_context
.network_namespace_path
) &&
1552 s
->exec_runtime
->netns_storage_socket
[0] >= 0) {
1554 if (ns_type_supported(NAMESPACE_NET
)) {
1555 r
= setup_netns(s
->exec_runtime
->netns_storage_socket
);
1557 log_unit_error_errno(UNIT(s
), r
, "Failed to join network namespace: %m");
1558 _exit(EXIT_NETWORK
);
1560 } else if (s
->exec_context
.network_namespace_path
) {
1561 log_unit_error(UNIT(s
), "Network namespace path configured but network namespaces not supported.");
1562 _exit(EXIT_NETWORK
);
1564 log_unit_warning(UNIT(s
), "PrivateNetwork=yes is configured, but the kernel does not support network namespaces, ignoring.");
1567 fd
= socket_address_listen_do(s
, address
, label
);
1569 log_address_error_errno(UNIT(s
), address
, fd
, "Failed to create listening socket (%s): %m");
1570 _exit(EXIT_FAILURE
);
1573 r
= send_one_fd(pair
[1], fd
, 0);
1575 log_address_error_errno(UNIT(s
), address
, r
, "Failed to send listening socket (%s) to parent: %m");
1576 _exit(EXIT_FAILURE
);
1579 _exit(EXIT_SUCCESS
);
1582 pair
[1] = safe_close(pair
[1]);
1583 fd
= receive_one_fd(pair
[0], 0);
1585 /* We synchronously wait for the helper, as it shouldn't be slow */
1586 r
= wait_for_terminate_and_check("(sd-listen)", pid
, WAIT_LOG_ABNORMAL
);
1593 return log_address_error_errno(UNIT(s
), address
, fd
, "Failed to receive listening socket (%s): %m");
1598 DEFINE_TRIVIAL_CLEANUP_FUNC(Socket
*, socket_close_fds
);
1600 static int socket_open_fds(Socket
*_s
) {
1601 _cleanup_(socket_close_fdsp
) Socket
*s
= _s
;
1602 _cleanup_(mac_selinux_freep
) char *label
= NULL
;
1603 bool know_label
= false;
1609 LIST_FOREACH(port
, p
, s
->ports
) {
1619 /* Figure out label, if we don't it know yet. We do it once, for the first socket where
1620 * we need this and remember it for the rest. */
1622 r
= socket_determine_selinux_label(s
, &label
);
1624 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine SELinux label: %m");
1629 /* Apply the socket protocol */
1630 switch (p
->address
.type
) {
1633 case SOCK_SEQPACKET
:
1634 if (s
->socket_protocol
== IPPROTO_SCTP
)
1635 p
->address
.protocol
= s
->socket_protocol
;
1639 if (s
->socket_protocol
== IPPROTO_UDPLITE
)
1640 p
->address
.protocol
= s
->socket_protocol
;
1644 p
->fd
= socket_address_listen_in_cgroup(s
, &p
->address
, label
);
1648 socket_apply_socket_options(s
, p
->fd
);
1652 case SOCKET_SPECIAL
:
1654 p
->fd
= special_address_create(p
->path
, s
->writable
);
1656 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open special file %s: %m", p
->path
);
1661 p
->fd
= fifo_address_create(
1666 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open FIFO %s: %m", p
->path
);
1668 socket_apply_fifo_options(s
, p
->fd
);
1674 p
->fd
= mq_address_create(
1680 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open message queue %s: %m", p
->path
);
1683 case SOCKET_USB_FUNCTION
: {
1684 _cleanup_free_
char *ep
= NULL
;
1686 ep
= path_make_absolute("ep0", p
->path
);
1688 p
->fd
= usbffs_address_create(ep
);
1692 r
= usbffs_write_descs(p
->fd
, SERVICE(UNIT_DEREF(s
->service
)));
1696 r
= usbffs_dispatch_eps(p
);
1703 assert_not_reached("Unknown port type");
1711 static void socket_unwatch_fds(Socket
*s
) {
1717 LIST_FOREACH(port
, p
, s
->ports
) {
1721 if (!p
->event_source
)
1724 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_OFF
);
1726 log_unit_debug_errno(UNIT(s
), r
, "Failed to disable event source: %m");
1730 static int socket_watch_fds(Socket
*s
) {
1736 LIST_FOREACH(port
, p
, s
->ports
) {
1740 if (p
->event_source
) {
1741 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_ON
);
1745 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &p
->event_source
, p
->fd
, EPOLLIN
, socket_dispatch_io
, p
);
1749 (void) sd_event_source_set_description(p
->event_source
, "socket-port-io");
1756 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch listening fds: %m");
1757 socket_unwatch_fds(s
);
1767 static int socket_check_open(Socket
*s
) {
1768 bool have_open
= false, have_closed
= false;
1773 LIST_FOREACH(port
, p
, s
->ports
) {
1779 if (have_open
&& have_closed
)
1780 return SOCKET_OPEN_SOME
;
1784 return SOCKET_OPEN_ALL
;
1786 return SOCKET_OPEN_NONE
;
1789 static void socket_set_state(Socket
*s
, SocketState state
) {
1790 SocketState old_state
;
1793 if (s
->state
!= state
)
1794 bus_unit_send_pending_change_signal(UNIT(s
), false);
1796 old_state
= s
->state
;
1804 SOCKET_STOP_PRE_SIGTERM
,
1805 SOCKET_STOP_PRE_SIGKILL
,
1807 SOCKET_FINAL_SIGTERM
,
1808 SOCKET_FINAL_SIGKILL
,
1811 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1812 socket_unwatch_control_pid(s
);
1813 s
->control_command
= NULL
;
1814 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1817 if (state
!= SOCKET_LISTENING
)
1818 socket_unwatch_fds(s
);
1826 SOCKET_STOP_PRE_SIGTERM
,
1827 SOCKET_STOP_PRE_SIGKILL
,
1829 socket_close_fds(s
);
1831 if (state
!= old_state
)
1832 log_unit_debug(UNIT(s
), "Changed %s -> %s", socket_state_to_string(old_state
), socket_state_to_string(state
));
1834 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
1837 static int socket_coldplug(Unit
*u
) {
1838 Socket
*s
= SOCKET(u
);
1842 assert(s
->state
== SOCKET_DEAD
);
1844 if (s
->deserialized_state
== s
->state
)
1847 if (s
->control_pid
> 0 &&
1848 pid_is_unwaited(s
->control_pid
) &&
1849 IN_SET(s
->deserialized_state
,
1854 SOCKET_STOP_PRE_SIGTERM
,
1855 SOCKET_STOP_PRE_SIGKILL
,
1857 SOCKET_FINAL_SIGTERM
,
1858 SOCKET_FINAL_SIGKILL
,
1861 r
= unit_watch_pid(UNIT(s
), s
->control_pid
, false);
1865 r
= socket_arm_timer(s
, usec_add(u
->state_change_timestamp
.monotonic
, s
->timeout_usec
));
1870 if (IN_SET(s
->deserialized_state
,
1876 /* Originally, we used to simply reopen all sockets here that we didn't have file descriptors
1877 * for. However, this is problematic, as we won't traverse through the SOCKET_START_CHOWN state for
1878 * them, and thus the UID/GID wouldn't be right. Hence, instead simply check if we have all fds open,
1879 * and if there's a mismatch, warn loudly. */
1881 r
= socket_check_open(s
);
1882 if (r
== SOCKET_OPEN_NONE
)
1883 log_unit_warning(UNIT(s
),
1884 "Socket unit configuration has changed while unit has been running, "
1885 "no open socket file descriptor left. "
1886 "The socket unit is not functional until restarted.");
1887 else if (r
== SOCKET_OPEN_SOME
)
1888 log_unit_warning(UNIT(s
),
1889 "Socket unit configuration has changed while unit has been running, "
1890 "and some socket file descriptors have not been opened yet. "
1891 "The socket unit is not fully functional until restarted.");
1894 if (s
->deserialized_state
== SOCKET_LISTENING
) {
1895 r
= socket_watch_fds(s
);
1900 if (!IN_SET(s
->deserialized_state
, SOCKET_DEAD
, SOCKET_FAILED
, SOCKET_CLEANING
)) {
1901 (void) unit_setup_dynamic_creds(u
);
1902 (void) unit_setup_exec_runtime(u
);
1905 socket_set_state(s
, s
->deserialized_state
);
1909 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
1911 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
1912 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
1925 r
= unit_prepare_exec(UNIT(s
));
1929 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
1933 r
= unit_set_exec_params(UNIT(s
), &exec_params
);
1937 r
= exec_spawn(UNIT(s
),
1947 r
= unit_watch_pid(UNIT(s
), pid
, true);
1956 static int socket_chown(Socket
*s
, pid_t
*_pid
) {
1960 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
1964 /* We have to resolve the user names out-of-process, hence
1965 * let's fork here. It's messy, but well, what can we do? */
1967 r
= unit_fork_helper_process(UNIT(s
), "(sd-chown)", &pid
);
1971 uid_t uid
= UID_INVALID
;
1972 gid_t gid
= GID_INVALID
;
1977 if (!isempty(s
->user
)) {
1978 const char *user
= s
->user
;
1980 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
, 0);
1982 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve user %s: %m", user
);
1987 if (!isempty(s
->group
)) {
1988 const char *group
= s
->group
;
1990 r
= get_group_creds(&group
, &gid
, 0);
1992 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve group %s: %m", group
);
1997 LIST_FOREACH(port
, p
, s
->ports
) {
1998 const char *path
= NULL
;
2000 if (p
->type
== SOCKET_SOCKET
)
2001 path
= socket_address_get_path(&p
->address
);
2002 else if (p
->type
== SOCKET_FIFO
)
2008 if (chown(path
, uid
, gid
) < 0) {
2009 log_unit_error_errno(UNIT(s
), errno
, "Failed to chown(): %m");
2014 _exit(EXIT_SUCCESS
);
2017 r
= unit_watch_pid(UNIT(s
), pid
, true);
2025 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
2029 static void socket_enter_dead(Socket
*s
, SocketResult f
) {
2032 if (s
->result
== SOCKET_SUCCESS
)
2035 if (s
->result
== SOCKET_SUCCESS
)
2036 unit_log_success(UNIT(s
));
2038 unit_log_failure(UNIT(s
), socket_result_to_string(s
->result
));
2040 socket_set_state(s
, s
->result
!= SOCKET_SUCCESS
? SOCKET_FAILED
: SOCKET_DEAD
);
2042 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
, true);
2044 unit_destroy_runtime_directory(UNIT(s
), &s
->exec_context
);
2046 unit_unref_uid_gid(UNIT(s
), true);
2048 dynamic_creds_destroy(&s
->dynamic_creds
);
2051 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
);
2053 static void socket_enter_stop_post(Socket
*s
, SocketResult f
) {
2057 if (s
->result
== SOCKET_SUCCESS
)
2060 socket_unwatch_control_pid(s
);
2061 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
2062 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
];
2064 if (s
->control_command
) {
2065 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2069 socket_set_state(s
, SOCKET_STOP_POST
);
2071 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_SUCCESS
);
2076 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
2077 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2080 static int state_to_kill_operation(Socket
*s
, SocketState state
) {
2081 if (state
== SOCKET_STOP_PRE_SIGTERM
&& unit_has_job_type(UNIT(s
), JOB_RESTART
))
2082 return KILL_RESTART
;
2084 if (state
== SOCKET_FINAL_SIGTERM
)
2085 return KILL_TERMINATE
;
2090 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
) {
2095 if (s
->result
== SOCKET_SUCCESS
)
2098 r
= unit_kill_context(
2101 state_to_kill_operation(s
, state
),
2109 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
2113 socket_set_state(s
, state
);
2114 } else if (state
== SOCKET_STOP_PRE_SIGTERM
)
2115 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_SUCCESS
);
2116 else if (state
== SOCKET_STOP_PRE_SIGKILL
)
2117 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2118 else if (state
== SOCKET_FINAL_SIGTERM
)
2119 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_SUCCESS
);
2121 socket_enter_dead(s
, SOCKET_SUCCESS
);
2126 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
2128 if (IN_SET(state
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_STOP_PRE_SIGKILL
))
2129 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2131 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2134 static void socket_enter_stop_pre(Socket
*s
, SocketResult f
) {
2138 if (s
->result
== SOCKET_SUCCESS
)
2141 socket_unwatch_control_pid(s
);
2142 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
2143 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
];
2145 if (s
->control_command
) {
2146 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2150 socket_set_state(s
, SOCKET_STOP_PRE
);
2152 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2157 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-pre' task: %m");
2158 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2161 static void socket_enter_listening(Socket
*s
) {
2165 r
= socket_watch_fds(s
);
2167 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch sockets: %m");
2171 socket_set_state(s
, SOCKET_LISTENING
);
2175 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2178 static void socket_enter_start_post(Socket
*s
) {
2182 socket_unwatch_control_pid(s
);
2183 s
->control_command_id
= SOCKET_EXEC_START_POST
;
2184 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
];
2186 if (s
->control_command
) {
2187 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2189 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
2193 socket_set_state(s
, SOCKET_START_POST
);
2195 socket_enter_listening(s
);
2200 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2203 static void socket_enter_start_chown(Socket
*s
) {
2208 r
= socket_open_fds(s
);
2210 log_unit_warning_errno(UNIT(s
), r
, "Failed to listen on sockets: %m");
2214 if (!isempty(s
->user
) || !isempty(s
->group
)) {
2216 socket_unwatch_control_pid(s
);
2217 s
->control_command_id
= SOCKET_EXEC_START_CHOWN
;
2218 s
->control_command
= NULL
;
2220 r
= socket_chown(s
, &s
->control_pid
);
2222 log_unit_warning_errno(UNIT(s
), r
, "Failed to fork 'start-chown' task: %m");
2226 socket_set_state(s
, SOCKET_START_CHOWN
);
2228 socket_enter_start_post(s
);
2233 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2236 static void socket_enter_start_pre(Socket
*s
) {
2240 socket_unwatch_control_pid(s
);
2242 unit_warn_leftover_processes(UNIT(s
));
2244 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
2245 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
];
2247 if (s
->control_command
) {
2248 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2250 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
2254 socket_set_state(s
, SOCKET_START_PRE
);
2256 socket_enter_start_chown(s
);
2261 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2264 static void flush_ports(Socket
*s
) {
2267 /* Flush all incoming traffic, regardless if actual bytes or new connections, so that this socket isn't busy
2270 LIST_FOREACH(port
, p
, s
->ports
) {
2274 (void) flush_accept(p
->fd
);
2275 (void) flush_fd(p
->fd
);
2279 static void socket_enter_running(Socket
*s
, int cfd
) {
2280 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2283 /* Note that this call takes possession of the connection fd passed. It either has to assign it somewhere or
2288 /* We don't take connections anymore if we are supposed to shut down anyway */
2289 if (unit_stop_pending(UNIT(s
))) {
2291 log_unit_debug(UNIT(s
), "Suppressing connection request since unit stop is scheduled.");
2301 if (!ratelimit_below(&s
->trigger_limit
)) {
2302 log_unit_warning(UNIT(s
), "Trigger limit hit, refusing further activation.");
2303 socket_enter_stop_pre(s
, SOCKET_FAILURE_TRIGGER_LIMIT_HIT
);
2308 bool pending
= false;
2313 /* If there's already a start pending don't bother to
2315 HASHMAP_FOREACH_KEY(v
, other
, UNIT(s
)->dependencies
[UNIT_TRIGGERS
], i
)
2316 if (unit_active_or_pending(other
)) {
2322 if (!UNIT_ISSET(s
->service
)) {
2323 log_unit_error(UNIT(s
), "Service to activate vanished, refusing activation.");
2328 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT_DEREF(s
->service
), JOB_REPLACE
, NULL
, &error
, NULL
);
2333 socket_set_state(s
, SOCKET_RUNNING
);
2335 _cleanup_free_
char *prefix
= NULL
, *instance
= NULL
, *name
= NULL
;
2336 _cleanup_(socket_peer_unrefp
) SocketPeer
*p
= NULL
;
2339 if (s
->n_connections
>= s
->max_connections
) {
2340 log_unit_warning(UNIT(s
), "Too many incoming connections (%u), dropping connection.",
2345 if (s
->max_connections_per_source
> 0) {
2346 r
= socket_acquire_peer(s
, cfd
, &p
);
2349 } else if (r
> 0 && p
->n_ref
> s
->max_connections_per_source
) {
2350 _cleanup_free_
char *t
= NULL
;
2352 (void) sockaddr_pretty(&p
->peer
.sa
, p
->peer_salen
, true, false, &t
);
2354 log_unit_warning(UNIT(s
),
2355 "Too many incoming connections (%u) from source %s, dropping connection.",
2356 p
->n_ref
, strnull(t
));
2361 r
= socket_instantiate_service(s
);
2365 r
= instance_from_socket(cfd
, s
->n_accepted
, &instance
);
2370 /* ENOTCONN is legitimate if TCP RST was received.
2371 * This connection is over, but the socket unit lives on. */
2372 log_unit_debug(UNIT(s
), "Got ENOTCONN on incoming socket, assuming aborted connection attempt, ignoring.");
2376 r
= unit_name_to_prefix(UNIT(s
)->id
, &prefix
);
2380 r
= unit_name_build(prefix
, instance
, ".service", &name
);
2384 r
= unit_add_name(UNIT_DEREF(s
->service
), name
);
2388 service
= SERVICE(UNIT_DEREF(s
->service
));
2389 unit_ref_unset(&s
->service
);
2392 unit_choose_id(UNIT(service
), name
);
2394 r
= service_set_socket_fd(service
, cfd
, s
, s
->selinux_context_from_net
);
2398 cfd
= -1; /* We passed ownership of the fd to the service now. Forget it here. */
2401 service
->peer
= TAKE_PTR(p
); /* Pass ownership of the peer reference */
2403 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(service
), JOB_REPLACE
, NULL
, &error
, NULL
);
2405 /* We failed to activate the new service, but it still exists. Let's make sure the service
2406 * closes and forgets the connection fd again, immediately. */
2407 service_close_socket_fd(service
);
2411 /* Notify clients about changed counters */
2412 unit_add_to_dbus_queue(UNIT(s
));
2423 log_unit_warning(UNIT(s
), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
2424 cfd
>= 0 ? "template" : "non-template",
2425 bus_error_message(&error
, r
));
2427 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2431 static void socket_run_next(Socket
*s
) {
2435 assert(s
->control_command
);
2436 assert(s
->control_command
->command_next
);
2438 socket_unwatch_control_pid(s
);
2440 s
->control_command
= s
->control_command
->command_next
;
2442 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2449 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next task: %m");
2451 if (s
->state
== SOCKET_START_POST
)
2452 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2453 else if (s
->state
== SOCKET_STOP_POST
)
2454 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2456 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2459 static int socket_start(Unit
*u
) {
2460 Socket
*s
= SOCKET(u
);
2465 /* We cannot fulfill this request right now, try again later
2467 if (IN_SET(s
->state
,
2469 SOCKET_STOP_PRE_SIGKILL
,
2470 SOCKET_STOP_PRE_SIGTERM
,
2472 SOCKET_FINAL_SIGTERM
,
2473 SOCKET_FINAL_SIGKILL
,
2477 /* Already on it! */
2478 if (IN_SET(s
->state
,
2484 /* Cannot run this without the service being around */
2485 if (UNIT_ISSET(s
->service
)) {
2488 service
= SERVICE(UNIT_DEREF(s
->service
));
2490 if (UNIT(service
)->load_state
!= UNIT_LOADED
) {
2491 log_unit_error(u
, "Socket service %s not loaded, refusing.", UNIT(service
)->id
);
2495 /* If the service is already active we cannot start the
2497 if (!IN_SET(service
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
)) {
2498 log_unit_error(u
, "Socket service %s already active, refusing.", UNIT(service
)->id
);
2503 assert(IN_SET(s
->state
, SOCKET_DEAD
, SOCKET_FAILED
));
2505 r
= unit_test_start_limit(u
);
2507 socket_enter_dead(s
, SOCKET_FAILURE_START_LIMIT_HIT
);
2511 r
= unit_acquire_invocation_id(u
);
2515 s
->result
= SOCKET_SUCCESS
;
2516 exec_command_reset_status_list_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
2518 u
->reset_accounting
= true;
2520 socket_enter_start_pre(s
);
2524 static int socket_stop(Unit
*u
) {
2525 Socket
*s
= SOCKET(u
);
2530 if (IN_SET(s
->state
,
2532 SOCKET_STOP_PRE_SIGTERM
,
2533 SOCKET_STOP_PRE_SIGKILL
,
2535 SOCKET_FINAL_SIGTERM
,
2536 SOCKET_FINAL_SIGKILL
))
2539 /* If there's already something running we go directly into
2541 if (IN_SET(s
->state
,
2544 SOCKET_START_POST
)) {
2545 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_SUCCESS
);
2549 /* If we are currently cleaning, then abort it, brutally. */
2550 if (s
->state
== SOCKET_CLEANING
) {
2551 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_SUCCESS
);
2555 assert(IN_SET(s
->state
, SOCKET_LISTENING
, SOCKET_RUNNING
));
2557 socket_enter_stop_pre(s
, SOCKET_SUCCESS
);
2561 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2562 Socket
*s
= SOCKET(u
);
2570 (void) serialize_item(f
, "state", socket_state_to_string(s
->state
));
2571 (void) serialize_item(f
, "result", socket_result_to_string(s
->result
));
2572 (void) serialize_item_format(f
, "n-accepted", "%u", s
->n_accepted
);
2573 (void) serialize_item_format(f
, "n-refused", "%u", s
->n_refused
);
2575 if (s
->control_pid
> 0)
2576 (void) serialize_item_format(f
, "control-pid", PID_FMT
, s
->control_pid
);
2578 if (s
->control_command_id
>= 0)
2579 (void) serialize_item(f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
2581 LIST_FOREACH(port
, p
, s
->ports
) {
2587 copy
= fdset_put_dup(fds
, p
->fd
);
2589 return log_unit_warning_errno(u
, copy
, "Failed to serialize socket fd: %m");
2591 if (p
->type
== SOCKET_SOCKET
) {
2592 _cleanup_free_
char *t
= NULL
;
2594 r
= socket_address_print(&p
->address
, &t
);
2596 return log_unit_error_errno(u
, r
, "Failed to format socket address: %m");
2598 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2599 (void) serialize_item_format(f
, "netlink", "%i %s", copy
, t
);
2601 (void) serialize_item_format(f
, "socket", "%i %i %s", copy
, p
->address
.type
, t
);
2602 } else if (p
->type
== SOCKET_SPECIAL
)
2603 (void) serialize_item_format(f
, "special", "%i %s", copy
, p
->path
);
2604 else if (p
->type
== SOCKET_MQUEUE
)
2605 (void) serialize_item_format(f
, "mqueue", "%i %s", copy
, p
->path
);
2606 else if (p
->type
== SOCKET_USB_FUNCTION
)
2607 (void) serialize_item_format(f
, "ffs", "%i %s", copy
, p
->path
);
2609 assert(p
->type
== SOCKET_FIFO
);
2610 (void) serialize_item_format(f
, "fifo", "%i %s", copy
, p
->path
);
2617 static void socket_port_take_fd(SocketPort
*p
, FDSet
*fds
, int fd
) {
2621 p
->fd
= fdset_remove(fds
, fd
);
2624 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2625 Socket
*s
= SOCKET(u
);
2631 if (streq(key
, "state")) {
2634 state
= socket_state_from_string(value
);
2636 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2638 s
->deserialized_state
= state
;
2639 } else if (streq(key
, "result")) {
2642 f
= socket_result_from_string(value
);
2644 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2645 else if (f
!= SOCKET_SUCCESS
)
2648 } else if (streq(key
, "n-accepted")) {
2651 if (safe_atou(value
, &k
) < 0)
2652 log_unit_debug(u
, "Failed to parse n-accepted value: %s", value
);
2655 } else if (streq(key
, "n-refused")) {
2658 if (safe_atou(value
, &k
) < 0)
2659 log_unit_debug(u
, "Failed to parse n-refused value: %s", value
);
2662 } else if (streq(key
, "control-pid")) {
2665 if (parse_pid(value
, &pid
) < 0)
2666 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2668 s
->control_pid
= pid
;
2669 } else if (streq(key
, "control-command")) {
2670 SocketExecCommand id
;
2672 id
= socket_exec_command_from_string(value
);
2674 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2676 s
->control_command_id
= id
;
2677 s
->control_command
= s
->exec_command
[id
];
2679 } else if (streq(key
, "fifo")) {
2683 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2684 log_unit_debug(u
, "Failed to parse fifo value: %s", value
);
2686 LIST_FOREACH(port
, p
, s
->ports
)
2687 if (p
->type
== SOCKET_FIFO
&&
2688 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2689 socket_port_take_fd(p
, fds
, fd
);
2693 } else if (streq(key
, "special")) {
2697 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2698 log_unit_debug(u
, "Failed to parse special value: %s", value
);
2700 LIST_FOREACH(port
, p
, s
->ports
)
2701 if (p
->type
== SOCKET_SPECIAL
&&
2702 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2703 socket_port_take_fd(p
, fds
, fd
);
2707 } else if (streq(key
, "mqueue")) {
2711 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2712 log_unit_debug(u
, "Failed to parse mqueue value: %s", value
);
2714 LIST_FOREACH(port
, p
, s
->ports
)
2715 if (p
->type
== SOCKET_MQUEUE
&&
2716 streq(p
->path
, value
+skip
)) {
2717 socket_port_take_fd(p
, fds
, fd
);
2721 } else if (streq(key
, "socket")) {
2722 int fd
, type
, skip
= 0;
2725 if (sscanf(value
, "%i %i %n", &fd
, &type
, &skip
) < 2 || fd
< 0 || type
< 0 || !fdset_contains(fds
, fd
))
2726 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2728 LIST_FOREACH(port
, p
, s
->ports
)
2729 if (socket_address_is(&p
->address
, value
+skip
, type
)) {
2730 socket_port_take_fd(p
, fds
, fd
);
2734 } else if (streq(key
, "netlink")) {
2738 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2739 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2741 LIST_FOREACH(port
, p
, s
->ports
)
2742 if (socket_address_is_netlink(&p
->address
, value
+skip
)) {
2743 socket_port_take_fd(p
, fds
, fd
);
2747 } else if (streq(key
, "ffs")) {
2751 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2752 log_unit_debug(u
, "Failed to parse ffs value: %s", value
);
2754 LIST_FOREACH(port
, p
, s
->ports
)
2755 if (p
->type
== SOCKET_USB_FUNCTION
&&
2756 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2757 socket_port_take_fd(p
, fds
, fd
);
2762 log_unit_debug(UNIT(s
), "Unknown serialization key: %s", key
);
2767 static void socket_distribute_fds(Unit
*u
, FDSet
*fds
) {
2768 Socket
*s
= SOCKET(u
);
2773 LIST_FOREACH(port
, p
, s
->ports
) {
2777 if (p
->type
!= SOCKET_SOCKET
)
2783 FDSET_FOREACH(fd
, fds
, i
) {
2784 if (socket_address_matches_fd(&p
->address
, fd
)) {
2785 p
->fd
= fdset_remove(fds
, fd
);
2786 s
->deserialized_state
= SOCKET_LISTENING
;
2793 _pure_
static UnitActiveState
socket_active_state(Unit
*u
) {
2796 return state_translation_table
[SOCKET(u
)->state
];
2799 _pure_
static const char *socket_sub_state_to_string(Unit
*u
) {
2802 return socket_state_to_string(SOCKET(u
)->state
);
2805 const char* socket_port_type_to_string(SocketPort
*p
) {
2813 switch (p
->address
.type
) {
2821 case SOCK_SEQPACKET
:
2822 return "SequentialPacket";
2825 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2833 case SOCKET_SPECIAL
:
2837 return "MessageQueue";
2842 case SOCKET_USB_FUNCTION
:
2843 return "USBFunction";
2850 SocketType
socket_port_type_from_string(const char *s
) {
2853 if (STR_IN_SET(s
, "Stream", "Datagram", "SequentialPacket", "Netlink"))
2854 return SOCKET_SOCKET
;
2855 else if (streq(s
, "Special"))
2856 return SOCKET_SPECIAL
;
2857 else if (streq(s
, "MessageQueue"))
2858 return SOCKET_MQUEUE
;
2859 else if (streq(s
, "FIFO"))
2861 else if (streq(s
, "USBFunction"))
2862 return SOCKET_USB_FUNCTION
;
2864 return _SOCKET_TYPE_INVALID
;
2867 _pure_
static bool socket_may_gc(Unit
*u
) {
2868 Socket
*s
= SOCKET(u
);
2872 return s
->n_connections
== 0;
2875 static int socket_accept_do(Socket
*s
, int fd
) {
2881 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
2883 /* Convert transient network errors into clean and well-defined EAGAIN */
2884 return ERRNO_IS_ACCEPT_AGAIN(errno
) ? -EAGAIN
: -errno
;
2889 static int socket_accept_in_cgroup(Socket
*s
, SocketPort
*p
, int fd
) {
2890 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
2898 /* Similar to socket_address_listen_in_cgroup(), but for accept() rather than socket(): make sure that any
2899 * connection socket is also properly associated with the cgroup. */
2901 if (!IN_SET(p
->address
.sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
2904 r
= bpf_firewall_supported();
2907 if (r
== BPF_FIREWALL_UNSUPPORTED
)
2910 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
2911 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
2913 r
= unit_fork_helper_process(UNIT(s
), "(sd-accept)", &pid
);
2915 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off accept stub process: %m");
2919 pair
[0] = safe_close(pair
[0]);
2921 cfd
= socket_accept_do(s
, fd
);
2922 if (cfd
== -EAGAIN
) /* spurious accept() */
2923 _exit(EXIT_SUCCESS
);
2925 log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
2926 _exit(EXIT_FAILURE
);
2929 r
= send_one_fd(pair
[1], cfd
, 0);
2931 log_unit_error_errno(UNIT(s
), r
, "Failed to send connection socket to parent: %m");
2932 _exit(EXIT_FAILURE
);
2935 _exit(EXIT_SUCCESS
);
2938 pair
[1] = safe_close(pair
[1]);
2939 cfd
= receive_one_fd(pair
[0], 0);
2941 /* We synchronously wait for the helper, as it shouldn't be slow */
2942 r
= wait_for_terminate_and_check("(sd-accept)", pid
, WAIT_LOG_ABNORMAL
);
2948 /* If we received no fd, we got EIO here. If this happens with a process exit code of EXIT_SUCCESS
2949 * this is a spurious accept(), let's convert that back to EAGAIN here. */
2953 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to receive connection socket: %m");
2958 cfd
= socket_accept_do(s
, fd
);
2959 if (cfd
== -EAGAIN
) /* spurious accept(), skip it silently */
2962 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
2967 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
2968 SocketPort
*p
= userdata
;
2974 if (p
->socket
->state
!= SOCKET_LISTENING
)
2977 log_unit_debug(UNIT(p
->socket
), "Incoming traffic");
2979 if (revents
!= EPOLLIN
) {
2980 if (revents
& EPOLLHUP
)
2981 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.");
2983 log_unit_error(UNIT(p
->socket
), "Got unexpected poll event (0x%x) on socket.", revents
);
2987 if (p
->socket
->accept
&&
2988 p
->type
== SOCKET_SOCKET
&&
2989 socket_address_can_accept(&p
->address
)) {
2991 cfd
= socket_accept_in_cgroup(p
->socket
, p
, fd
);
2992 if (cfd
== -EAGAIN
) /* Spurious accept() */
2997 socket_apply_socket_options(p
->socket
, cfd
);
3000 socket_enter_running(p
->socket
, cfd
);
3004 socket_enter_stop_pre(p
->socket
, SOCKET_FAILURE_RESOURCES
);
3008 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
3009 Socket
*s
= SOCKET(u
);
3015 if (pid
!= s
->control_pid
)
3020 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
3022 else if (code
== CLD_EXITED
)
3023 f
= SOCKET_FAILURE_EXIT_CODE
;
3024 else if (code
== CLD_KILLED
)
3025 f
= SOCKET_FAILURE_SIGNAL
;
3026 else if (code
== CLD_DUMPED
)
3027 f
= SOCKET_FAILURE_CORE_DUMP
;
3029 assert_not_reached("Unknown sigchld code");
3031 if (s
->control_command
) {
3032 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
3034 if (s
->control_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3038 unit_log_process_exit(
3041 socket_exec_command_to_string(s
->control_command_id
),
3042 f
== SOCKET_SUCCESS
,
3045 if (s
->result
== SOCKET_SUCCESS
)
3048 if (s
->control_command
&&
3049 s
->control_command
->command_next
&&
3050 f
== SOCKET_SUCCESS
) {
3052 log_unit_debug(u
, "Running next command for state %s", socket_state_to_string(s
->state
));
3055 s
->control_command
= NULL
;
3056 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
3058 /* No further commands for this step, so let's figure
3059 * out what to do next */
3061 log_unit_debug(u
, "Got final SIGCHLD for state %s", socket_state_to_string(s
->state
));
3065 case SOCKET_START_PRE
:
3066 if (f
== SOCKET_SUCCESS
)
3067 socket_enter_start_chown(s
);
3069 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, f
);
3072 case SOCKET_START_CHOWN
:
3073 if (f
== SOCKET_SUCCESS
)
3074 socket_enter_start_post(s
);
3076 socket_enter_stop_pre(s
, f
);
3079 case SOCKET_START_POST
:
3080 if (f
== SOCKET_SUCCESS
)
3081 socket_enter_listening(s
);
3083 socket_enter_stop_pre(s
, f
);
3086 case SOCKET_STOP_PRE
:
3087 case SOCKET_STOP_PRE_SIGTERM
:
3088 case SOCKET_STOP_PRE_SIGKILL
:
3089 socket_enter_stop_post(s
, f
);
3092 case SOCKET_STOP_POST
:
3093 case SOCKET_FINAL_SIGTERM
:
3094 case SOCKET_FINAL_SIGKILL
:
3095 socket_enter_dead(s
, f
);
3098 case SOCKET_CLEANING
:
3100 if (s
->clean_result
== SOCKET_SUCCESS
)
3101 s
->clean_result
= f
;
3103 socket_enter_dead(s
, SOCKET_SUCCESS
);
3107 assert_not_reached("Uh, control process died at wrong time.");
3111 /* Notify clients about changed exit status */
3112 unit_add_to_dbus_queue(u
);
3115 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
3116 Socket
*s
= SOCKET(userdata
);
3119 assert(s
->timer_event_source
== source
);
3123 case SOCKET_START_PRE
:
3124 log_unit_warning(UNIT(s
), "Starting timed out. Terminating.");
3125 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3128 case SOCKET_START_CHOWN
:
3129 case SOCKET_START_POST
:
3130 log_unit_warning(UNIT(s
), "Starting timed out. Stopping.");
3131 socket_enter_stop_pre(s
, SOCKET_FAILURE_TIMEOUT
);
3134 case SOCKET_STOP_PRE
:
3135 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
3136 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3139 case SOCKET_STOP_PRE_SIGTERM
:
3140 if (s
->kill_context
.send_sigkill
) {
3141 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
3142 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3144 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL. Ignoring.");
3145 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3149 case SOCKET_STOP_PRE_SIGKILL
:
3150 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
3151 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3154 case SOCKET_STOP_POST
:
3155 log_unit_warning(UNIT(s
), "Stopping timed out (2). Terminating.");
3156 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3159 case SOCKET_FINAL_SIGTERM
:
3160 if (s
->kill_context
.send_sigkill
) {
3161 log_unit_warning(UNIT(s
), "Stopping timed out (2). Killing.");
3162 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3164 log_unit_warning(UNIT(s
), "Stopping timed out (2). Skipping SIGKILL. Ignoring.");
3165 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3169 case SOCKET_FINAL_SIGKILL
:
3170 log_unit_warning(UNIT(s
), "Still around after SIGKILL (2). Entering failed mode.");
3171 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3174 case SOCKET_CLEANING
:
3175 log_unit_warning(UNIT(s
), "Cleaning timed out. killing.");
3177 if (s
->clean_result
== SOCKET_SUCCESS
)
3178 s
->clean_result
= SOCKET_FAILURE_TIMEOUT
;
3180 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, 0);
3184 assert_not_reached("Timeout at wrong time.");
3190 int socket_collect_fds(Socket
*s
, int **fds
) {
3191 size_t k
= 0, n
= 0;
3198 /* Called from the service code for requesting our fds */
3200 LIST_FOREACH(port
, p
, s
->ports
) {
3203 n
+= p
->n_auxiliary_fds
;
3215 LIST_FOREACH(port
, p
, s
->ports
) {
3220 for (i
= 0; i
< p
->n_auxiliary_fds
; ++i
)
3221 rfds
[k
++] = p
->auxiliary_fds
[i
];
3230 static void socket_reset_failed(Unit
*u
) {
3231 Socket
*s
= SOCKET(u
);
3235 if (s
->state
== SOCKET_FAILED
)
3236 socket_set_state(s
, SOCKET_DEAD
);
3238 s
->result
= SOCKET_SUCCESS
;
3239 s
->clean_result
= SOCKET_SUCCESS
;
3242 void socket_connection_unref(Socket
*s
) {
3245 /* The service is dead. Yay!
3247 * This is strictly for one-instance-per-connection
3250 assert(s
->n_connections
> 0);
3253 log_unit_debug(UNIT(s
), "One connection closed, %u left.", s
->n_connections
);
3256 static void socket_trigger_notify(Unit
*u
, Unit
*other
) {
3257 Socket
*s
= SOCKET(u
);
3262 /* Filter out invocations with bogus state */
3263 if (other
->load_state
!= UNIT_LOADED
|| other
->type
!= UNIT_SERVICE
)
3266 /* Don't propagate state changes from the service if we are already down */
3267 if (!IN_SET(s
->state
, SOCKET_RUNNING
, SOCKET_LISTENING
))
3270 /* We don't care for the service state if we are in Accept=yes mode */
3274 /* Propagate start limit hit state */
3275 if (other
->start_limit_hit
) {
3276 socket_enter_stop_pre(s
, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
);
3280 /* Don't propagate anything if there's still a job queued */
3284 if (IN_SET(SERVICE(other
)->state
,
3285 SERVICE_DEAD
, SERVICE_FAILED
,
3286 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
3287 SERVICE_AUTO_RESTART
))
3288 socket_enter_listening(s
);
3290 if (SERVICE(other
)->state
== SERVICE_RUNNING
)
3291 socket_set_state(s
, SOCKET_RUNNING
);
3294 static int socket_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3295 return unit_kill_common(u
, who
, signo
, -1, SOCKET(u
)->control_pid
, error
);
3298 static int socket_get_timeout(Unit
*u
, usec_t
*timeout
) {
3299 Socket
*s
= SOCKET(u
);
3303 if (!s
->timer_event_source
)
3306 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3309 if (t
== USEC_INFINITY
)
3316 char *socket_fdname(Socket
*s
) {
3319 /* Returns the name to use for $LISTEN_NAMES. If the user
3320 * didn't specify anything specifically, use the socket unit's
3321 * name as fallback. */
3323 return s
->fdname
?: UNIT(s
)->id
;
3326 static int socket_control_pid(Unit
*u
) {
3327 Socket
*s
= SOCKET(u
);
3331 return s
->control_pid
;
3334 static int socket_clean(Unit
*u
, ExecCleanMask mask
) {
3335 _cleanup_strv_free_
char **l
= NULL
;
3336 Socket
*s
= SOCKET(u
);
3342 if (s
->state
!= SOCKET_DEAD
)
3345 r
= exec_context_get_clean_directories(&s
->exec_context
, u
->manager
->prefix
, mask
, &l
);
3349 if (strv_isempty(l
))
3352 socket_unwatch_control_pid(s
);
3353 s
->clean_result
= SOCKET_SUCCESS
;
3354 s
->control_command
= NULL
;
3355 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
3357 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->exec_context
.timeout_clean_usec
));
3361 r
= unit_fork_and_watch_rm_rf(u
, l
, &s
->control_pid
);
3365 socket_set_state(s
, SOCKET_CLEANING
);
3370 log_unit_warning_errno(u
, r
, "Failed to initiate cleaning: %m");
3371 s
->clean_result
= SOCKET_FAILURE_RESOURCES
;
3372 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
3376 static int socket_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
3377 Socket
*s
= SOCKET(u
);
3381 return exec_context_get_clean_mask(&s
->exec_context
, ret
);
3384 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
3385 [SOCKET_EXEC_START_PRE
] = "ExecStartPre",
3386 [SOCKET_EXEC_START_CHOWN
] = "ExecStartChown",
3387 [SOCKET_EXEC_START_POST
] = "ExecStartPost",
3388 [SOCKET_EXEC_STOP_PRE
] = "ExecStopPre",
3389 [SOCKET_EXEC_STOP_POST
] = "ExecStopPost"
3392 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
3394 static const char* const socket_result_table
[_SOCKET_RESULT_MAX
] = {
3395 [SOCKET_SUCCESS
] = "success",
3396 [SOCKET_FAILURE_RESOURCES
] = "resources",
3397 [SOCKET_FAILURE_TIMEOUT
] = "timeout",
3398 [SOCKET_FAILURE_EXIT_CODE
] = "exit-code",
3399 [SOCKET_FAILURE_SIGNAL
] = "signal",
3400 [SOCKET_FAILURE_CORE_DUMP
] = "core-dump",
3401 [SOCKET_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
3402 [SOCKET_FAILURE_TRIGGER_LIMIT_HIT
] = "trigger-limit-hit",
3403 [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
] = "service-start-limit-hit"
3406 DEFINE_STRING_TABLE_LOOKUP(socket_result
, SocketResult
);
3408 const UnitVTable socket_vtable
= {
3409 .object_size
= sizeof(Socket
),
3410 .exec_context_offset
= offsetof(Socket
, exec_context
),
3411 .cgroup_context_offset
= offsetof(Socket
, cgroup_context
),
3412 .kill_context_offset
= offsetof(Socket
, kill_context
),
3413 .exec_runtime_offset
= offsetof(Socket
, exec_runtime
),
3414 .dynamic_creds_offset
= offsetof(Socket
, dynamic_creds
),
3420 .private_section
= "Socket",
3422 .can_transient
= true,
3424 .init
= socket_init
,
3425 .done
= socket_done
,
3426 .load
= socket_load
,
3428 .coldplug
= socket_coldplug
,
3430 .dump
= socket_dump
,
3432 .start
= socket_start
,
3433 .stop
= socket_stop
,
3435 .kill
= socket_kill
,
3436 .clean
= socket_clean
,
3437 .can_clean
= socket_can_clean
,
3439 .get_timeout
= socket_get_timeout
,
3441 .serialize
= socket_serialize
,
3442 .deserialize_item
= socket_deserialize_item
,
3443 .distribute_fds
= socket_distribute_fds
,
3445 .active_state
= socket_active_state
,
3446 .sub_state_to_string
= socket_sub_state_to_string
,
3448 .will_restart
= unit_will_restart_default
,
3450 .may_gc
= socket_may_gc
,
3452 .sigchld_event
= socket_sigchld_event
,
3454 .trigger_notify
= socket_trigger_notify
,
3456 .reset_failed
= socket_reset_failed
,
3458 .control_pid
= socket_control_pid
,
3460 .bus_vtable
= bus_socket_vtable
,
3461 .bus_set_property
= bus_socket_set_property
,
3462 .bus_commit_properties
= bus_socket_commit_properties
,
3464 .status_message_formats
= {
3465 /*.starting_stopping = {
3466 [0] = "Starting socket %s...",
3467 [1] = "Stopping socket %s...",
3469 .finished_start_job
= {
3470 [JOB_DONE
] = "Listening on %s.",
3471 [JOB_FAILED
] = "Failed to listen on %s.",
3472 [JOB_TIMEOUT
] = "Timed out starting %s.",
3474 .finished_stop_job
= {
3475 [JOB_DONE
] = "Closed %s.",
3476 [JOB_FAILED
] = "Failed stopping %s.",
3477 [JOB_TIMEOUT
] = "Timed out stopping %s.",