1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <linux/sctp.h>
7 #include <netinet/tcp.h>
13 #include "alloc-util.h"
14 #include "bpf-program.h"
15 #include "bus-error.h"
17 #include "dbus-socket.h"
18 #include "dbus-unit.h"
19 #include "errno-list.h"
20 #include "errno-util.h"
21 #include "exit-status.h"
22 #include "extract-word.h"
25 #include "format-util.h"
27 #include "glyph-util.h"
28 #include "in-addr-util.h"
30 #include "ip-protocol-list.h"
33 #include "mkdir-label.h"
34 #include "namespace-util.h"
35 #include "parse-util.h"
36 #include "path-util.h"
37 #include "pidfd-util.h"
38 #include "process-util.h"
39 #include "recurse-dir.h"
40 #include "selinux-util.h"
41 #include "serialize.h"
44 #include "siphash24.h"
45 #include "smack-util.h"
47 #include "socket-netlink.h"
49 #include "string-table.h"
50 #include "string-util.h"
53 #include "unit-name.h"
54 #include "user-util.h"
56 typedef struct SocketPeer
{
60 union sockaddr_union peer
;
62 struct ucred peer_cred
;
65 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
66 [SOCKET_DEAD
] = UNIT_INACTIVE
,
67 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
68 [SOCKET_START_OPEN
] = UNIT_ACTIVATING
,
69 [SOCKET_START_CHOWN
] = UNIT_ACTIVATING
,
70 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
71 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
72 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
73 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
74 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
75 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
76 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
77 [SOCKET_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
78 [SOCKET_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
79 [SOCKET_FAILED
] = UNIT_FAILED
,
80 [SOCKET_CLEANING
] = UNIT_MAINTENANCE
,
83 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
84 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
86 static bool SOCKET_STATE_WITH_PROCESS(SocketState state
) {
92 SOCKET_STOP_PRE_SIGTERM
,
93 SOCKET_STOP_PRE_SIGKILL
,
100 static bool SOCKET_SERVICE_IS_ACTIVE(Service
*s
, bool allow_finalize
) {
103 /* If unit_active_state() reports inactive/failed then it's all good, otherwise we need to
104 * manually exclude SERVICE_AUTO_RESTART and SERVICE_AUTO_RESTART_QUEUED, in which cases
105 * the start job hasn't been enqueued/run, but are only placeholders in order to allow
106 * canceling auto restart. */
108 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(UNIT(s
))))
111 if (IN_SET(s
->state
, SERVICE_AUTO_RESTART
, SERVICE_AUTO_RESTART_QUEUED
))
114 if (allow_finalize
&& IN_SET(s
->state
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
, SERVICE_CLEANING
))
120 static void socket_init(Unit
*u
) {
121 Socket
*s
= ASSERT_PTR(SOCKET(u
));
123 assert(u
->load_state
== UNIT_STUB
);
125 s
->backlog
= SOMAXCONN_DELUXE
;
126 s
->timeout_usec
= u
->manager
->defaults
.timeout_start_usec
;
127 s
->directory_mode
= 0755;
128 s
->socket_mode
= 0666;
130 s
->max_connections
= 64;
132 s
->pass_rights
= true; /* defaults to enabled in kernel */
138 s
->exec_context
.std_output
= u
->manager
->defaults
.std_output
;
139 s
->exec_context
.std_error
= u
->manager
->defaults
.std_error
;
141 s
->control_pid
= PIDREF_NULL
;
142 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
144 s
->trigger_limit
= RATELIMIT_OFF
;
146 s
->poll_limit
= RATELIMIT_OFF
;
149 static void socket_unwatch_control_pid(Socket
*s
) {
151 unit_unwatch_pidref_done(UNIT(s
), &s
->control_pid
);
154 static void socket_port_close_auxiliary_fds(SocketPort
*p
) {
157 close_many(p
->auxiliary_fds
, p
->n_auxiliary_fds
);
158 p
->auxiliary_fds
= mfree(p
->auxiliary_fds
);
159 p
->n_auxiliary_fds
= 0;
162 SocketPort
* socket_port_free(SocketPort
*p
) {
166 sd_event_source_unref(p
->event_source
);
168 socket_port_close_auxiliary_fds(p
);
175 void socket_free_ports(Socket
*s
) {
178 LIST_CLEAR(port
, s
->ports
, socket_port_free
);
181 static void socket_done(Unit
*u
) {
182 Socket
*s
= ASSERT_PTR(SOCKET(u
));
185 socket_free_ports(s
);
187 while ((p
= set_steal_first(s
->peers_by_address
)))
190 s
->peers_by_address
= set_free(s
->peers_by_address
);
192 s
->exec_runtime
= exec_runtime_free(s
->exec_runtime
);
194 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
195 s
->control_command
= NULL
;
197 socket_unwatch_control_pid(s
);
199 unit_ref_unset(&s
->service
);
201 s
->tcp_congestion
= mfree(s
->tcp_congestion
);
202 s
->bind_to_device
= mfree(s
->bind_to_device
);
204 s
->smack
= mfree(s
->smack
);
205 s
->smack_ip_in
= mfree(s
->smack_ip_in
);
206 s
->smack_ip_out
= mfree(s
->smack_ip_out
);
208 strv_free(s
->symlinks
);
210 s
->user
= mfree(s
->user
);
211 s
->group
= mfree(s
->group
);
213 s
->fdname
= mfree(s
->fdname
);
215 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
218 static int socket_arm_timer(Socket
*s
, bool relative
, usec_t usec
) {
221 return unit_arm_timer(UNIT(s
), &s
->timer_event_source
, relative
, usec
, socket_dispatch_timer
);
224 static bool have_non_accept_socket(Socket
*s
) {
230 LIST_FOREACH(port
, p
, s
->ports
) {
232 if (p
->type
!= SOCKET_SOCKET
)
235 if (!socket_address_can_accept(&p
->address
))
242 static int socket_add_mount_dependencies(Socket
*s
) {
247 LIST_FOREACH(port
, p
, s
->ports
) {
248 const char *path
= NULL
;
250 if (p
->type
== SOCKET_SOCKET
)
251 path
= socket_address_get_path(&p
->address
);
252 else if (IN_SET(p
->type
, SOCKET_FIFO
, SOCKET_SPECIAL
, SOCKET_USB_FUNCTION
))
258 r
= unit_add_mounts_for(UNIT(s
), path
, UNIT_DEPENDENCY_FILE
, UNIT_MOUNT_REQUIRES
);
266 static int socket_add_device_dependencies(Socket
*s
) {
271 if (!s
->bind_to_device
|| streq(s
->bind_to_device
, "lo"))
274 t
= strjoina("/sys/subsystem/net/devices/", s
->bind_to_device
);
275 return unit_add_node_dependency(UNIT(s
), t
, UNIT_BINDS_TO
, UNIT_DEPENDENCY_FILE
);
278 static int socket_add_default_dependencies(Socket
*s
) {
283 if (!UNIT(s
)->default_dependencies
)
286 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_BEFORE
, SPECIAL_SOCKETS_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
290 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
291 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
296 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
299 static bool socket_has_exec(Socket
*s
) {
302 FOREACH_ARRAY(i
, s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
)
309 static int socket_add_extras(Socket
*s
) {
310 Unit
*u
= UNIT(ASSERT_PTR(s
));
313 /* Pick defaults for the trigger limit, if nothing was explicitly configured. We pick a relatively high limit
314 * in Accept=yes mode, and a lower limit for Accept=no. Reason: in Accept=yes mode we are invoking accept()
315 * ourselves before the trigger limit can hit, thus incoming connections are taken off the socket queue quickly
316 * and reliably. This is different for Accept=no, where the spawned service has to take the incoming traffic
317 * off the queues, which it might not necessarily do. Moreover, while Accept=no services are supposed to
318 * process whatever is queued in one go, and thus should normally never have to be started frequently. This is
319 * different for Accept=yes where each connection is processed by a new service instance, and thus frequent
320 * service starts are typical.
322 * For the poll limit we follow a similar rule, but use 3/4th of the trigger limit parameters, to
323 * trigger this earlier. */
325 if (s
->trigger_limit
.interval
== USEC_INFINITY
)
326 s
->trigger_limit
.interval
= 2 * USEC_PER_SEC
;
327 if (s
->trigger_limit
.burst
== UINT_MAX
)
328 s
->trigger_limit
.burst
= s
->accept
? 200 : 20;
330 if (s
->poll_limit
.interval
== USEC_INFINITY
)
331 s
->poll_limit
.interval
= 2 * USEC_PER_SEC
;
332 if (s
->poll_limit
.burst
== UINT_MAX
)
333 s
->poll_limit
.burst
= s
->accept
? 150 : 15;
335 if (have_non_accept_socket(s
)) {
337 if (!UNIT_ISSET(s
->service
)) {
340 r
= unit_load_related_unit(u
, ".service", &x
);
344 unit_ref_set(&s
->service
, u
, x
);
347 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT_DEREF(s
->service
), true, UNIT_DEPENDENCY_IMPLICIT
);
352 r
= socket_add_mount_dependencies(s
);
356 r
= socket_add_device_dependencies(s
);
360 r
= unit_patch_contexts(u
);
364 if (socket_has_exec(s
)) {
365 r
= unit_add_exec_dependencies(u
, &s
->exec_context
);
370 r
= unit_set_default_slice(u
);
374 r
= socket_add_default_dependencies(s
);
381 static const char* socket_find_symlink_target(Socket
*s
) {
382 const char *found
= NULL
;
386 LIST_FOREACH(port
, p
, s
->ports
) {
396 f
= socket_address_get_path(&p
->address
);
414 static int socket_verify(Socket
*s
) {
416 assert(UNIT(s
)->load_state
== UNIT_LOADED
);
419 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing.");
421 if (s
->max_connections
<= 0)
422 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "MaxConnection= setting too small. Refusing.");
424 if (s
->accept
&& have_non_accept_socket(s
))
425 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing.");
427 if (s
->accept
&& UNIT_ISSET(s
->service
))
428 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Explicit service configuration for accepting socket units not supported. Refusing.");
430 if (!strv_isempty(s
->symlinks
) && !socket_find_symlink_target(s
))
431 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Unit has symlinks set but none or more than one node in the file system. Refusing.");
436 static void peer_address_hash_func(const SocketPeer
*s
, struct siphash
*state
) {
439 if (s
->peer
.sa
.sa_family
== AF_INET
)
440 siphash24_compress_typesafe(s
->peer
.in
.sin_addr
, state
);
441 else if (s
->peer
.sa
.sa_family
== AF_INET6
)
442 siphash24_compress_typesafe(s
->peer
.in6
.sin6_addr
, state
);
443 else if (s
->peer
.sa
.sa_family
== AF_VSOCK
)
444 siphash24_compress_typesafe(s
->peer
.vm
.svm_cid
, state
);
445 else if (s
->peer
.sa
.sa_family
== AF_UNIX
)
446 siphash24_compress_typesafe(s
->peer_cred
.uid
, state
);
448 assert_not_reached();
451 static int peer_address_compare_func(const SocketPeer
*x
, const SocketPeer
*y
) {
454 r
= CMP(x
->peer
.sa
.sa_family
, y
->peer
.sa
.sa_family
);
458 switch (x
->peer
.sa
.sa_family
) {
460 return memcmp(&x
->peer
.in
.sin_addr
, &y
->peer
.in
.sin_addr
, sizeof(x
->peer
.in
.sin_addr
));
462 return memcmp(&x
->peer
.in6
.sin6_addr
, &y
->peer
.in6
.sin6_addr
, sizeof(x
->peer
.in6
.sin6_addr
));
464 return CMP(x
->peer
.vm
.svm_cid
, y
->peer
.vm
.svm_cid
);
466 return CMP(x
->peer_cred
.uid
, y
->peer_cred
.uid
);
468 assert_not_reached();
471 DEFINE_PRIVATE_HASH_OPS(peer_address_hash_ops
, SocketPeer
, peer_address_hash_func
, peer_address_compare_func
);
473 static int socket_load(Unit
*u
) {
474 Socket
*s
= ASSERT_PTR(SOCKET(u
));
477 assert(u
->load_state
== UNIT_STUB
);
479 r
= unit_load_fragment_and_dropin(u
, true);
483 if (u
->load_state
!= UNIT_LOADED
)
486 /* This is a new unit? Then let's add in some extras */
487 r
= socket_add_extras(s
);
491 return socket_verify(s
);
494 static SocketPeer
* socket_peer_dup(const SocketPeer
*q
) {
499 p
= new(SocketPeer
, 1);
506 .peer_salen
= q
->peer_salen
,
507 .peer_cred
= q
->peer_cred
,
513 static SocketPeer
* socket_peer_free(SocketPeer
*p
) {
517 set_remove(p
->socket
->peers_by_address
, p
);
522 DEFINE_TRIVIAL_REF_UNREF_FUNC(SocketPeer
, socket_peer
, socket_peer_free
);
524 int socket_acquire_peer(Socket
*s
, int fd
, SocketPeer
**ret
) {
525 _cleanup_(socket_peer_unrefp
) SocketPeer
*remote
= NULL
;
527 .peer_salen
= sizeof(union sockaddr_union
),
528 .peer_cred
= UCRED_INVALID
,
536 if (getpeername(fd
, &key
.peer
.sa
, &key
.peer_salen
) < 0)
537 return log_unit_error_errno(UNIT(s
), errno
, "getpeername() failed: %m");
539 switch (key
.peer
.sa
.sa_family
) {
546 r
= getpeercred(fd
, &key
.peer_cred
);
548 return log_unit_error_errno(UNIT(s
), r
, "Failed to get peer credentials of socket: %m");
556 i
= set_get(s
->peers_by_address
, &key
);
558 *ret
= socket_peer_ref(i
);
562 remote
= socket_peer_dup(&key
);
566 r
= set_ensure_put(&s
->peers_by_address
, &peer_address_hash_ops
, remote
);
568 return log_unit_error_errno(UNIT(s
), r
, "Failed to insert peer info into hash table: %m");
572 *ret
= TAKE_PTR(remote
);
576 static const char* listen_lookup(int family
, int type
) {
578 if (family
== AF_NETLINK
)
579 return "ListenNetlink";
581 if (type
== SOCK_STREAM
)
582 return "ListenStream";
583 else if (type
== SOCK_DGRAM
)
584 return "ListenDatagram";
585 else if (type
== SOCK_SEQPACKET
)
586 return "ListenSequentialPacket";
588 assert_not_reached();
591 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
592 Socket
*s
= ASSERT_PTR(SOCKET(u
));
593 const char *prefix2
, *str
;
597 prefix
= strempty(prefix
);
598 prefix2
= strjoina(prefix
, "\t");
601 "%sSocket State: %s\n"
603 "%sClean Result: %s\n"
604 "%sBindIPv6Only: %s\n"
606 "%sSocketMode: %04o\n"
607 "%sDirectoryMode: %04o\n"
611 "%sTransparent: %s\n"
613 "%sPassCredentials: %s\n"
615 "%sPassSecurity: %s\n"
616 "%sPassPacketInfo: %s\n"
617 "%sAcceptFileDescriptors: %s\n"
618 "%sTCPCongestion: %s\n"
619 "%sRemoveOnStop: %s\n"
621 "%sFileDescriptorName: %s\n"
622 "%sPassFileDescriptorsToExec: %s\n"
623 "%sSELinuxContextFromNet: %s\n",
624 prefix
, socket_state_to_string(s
->state
),
625 prefix
, socket_result_to_string(s
->result
),
626 prefix
, socket_result_to_string(s
->clean_result
),
627 prefix
, socket_address_bind_ipv6_only_to_string(s
->bind_ipv6_only
),
629 prefix
, s
->socket_mode
,
630 prefix
, s
->directory_mode
,
631 prefix
, yes_no(s
->keep_alive
),
632 prefix
, yes_no(s
->no_delay
),
633 prefix
, yes_no(s
->free_bind
),
634 prefix
, yes_no(s
->transparent
),
635 prefix
, yes_no(s
->broadcast
),
636 prefix
, yes_no(s
->pass_cred
),
637 prefix
, yes_no(s
->pass_pidfd
),
638 prefix
, yes_no(s
->pass_sec
),
639 prefix
, yes_no(s
->pass_pktinfo
),
640 prefix
, yes_no(s
->pass_rights
),
641 prefix
, strna(s
->tcp_congestion
),
642 prefix
, yes_no(s
->remove_on_stop
),
643 prefix
, yes_no(s
->writable
),
644 prefix
, socket_fdname(s
),
645 prefix
, yes_no(s
->pass_fds_to_exec
),
646 prefix
, yes_no(s
->selinux_context_from_net
));
648 if (s
->timestamping
!= SOCKET_TIMESTAMPING_OFF
)
650 "%sTimestamping: %s\n",
651 prefix
, socket_timestamping_to_string(s
->timestamping
));
653 if (pidref_is_set(&s
->control_pid
))
655 "%sControl PID: "PID_FMT
"\n",
656 prefix
, s
->control_pid
.pid
);
658 if (s
->bind_to_device
)
660 "%sBindToDevice: %s\n",
661 prefix
, s
->bind_to_device
);
666 "%sNConnections: %u\n"
667 "%sMaxConnections: %u\n"
668 "%sMaxConnectionsPerSource: %u\n",
669 prefix
, s
->n_accepted
,
670 prefix
, s
->n_connections
,
671 prefix
, s
->max_connections
,
672 prefix
, s
->max_connections_per_source
);
675 "%sFlushPending: %s\n",
676 prefix
, yes_no(s
->flush_pending
));
678 if (s
->priority
>= 0)
681 prefix
, s
->priority
);
683 if (s
->receive_buffer
> 0)
685 "%sReceiveBuffer: %zu\n",
686 prefix
, s
->receive_buffer
);
688 if (s
->send_buffer
> 0)
690 "%sSendBuffer: %zu\n",
691 prefix
, s
->send_buffer
);
703 if (s
->pipe_size
> 0)
706 prefix
, s
->pipe_size
);
713 if (s
->mq_maxmsg
> 0)
715 "%sMessageQueueMaxMessages: %li\n",
716 prefix
, s
->mq_maxmsg
);
718 if (s
->mq_msgsize
> 0)
720 "%sMessageQueueMessageSize: %li\n",
721 prefix
, s
->mq_msgsize
);
726 prefix
, yes_no(s
->reuse_port
));
730 "%sSmackLabel: %s\n",
735 "%sSmackLabelIPIn: %s\n",
736 prefix
, s
->smack_ip_in
);
740 "%sSmackLabelIPOut: %s\n",
741 prefix
, s
->smack_ip_out
);
743 if (!isempty(s
->user
) || !isempty(s
->group
))
746 "%sSocketGroup: %s\n",
747 prefix
, strna(s
->user
),
748 prefix
, strna(s
->group
));
750 if (timestamp_is_set(s
->keep_alive_time
))
752 "%sKeepAliveTimeSec: %s\n",
753 prefix
, FORMAT_TIMESPAN(s
->keep_alive_time
, USEC_PER_SEC
));
755 if (s
->keep_alive_interval
> 0)
757 "%sKeepAliveIntervalSec: %s\n",
758 prefix
, FORMAT_TIMESPAN(s
->keep_alive_interval
, USEC_PER_SEC
));
760 if (s
->keep_alive_cnt
> 0)
762 "%sKeepAliveProbes: %u\n",
763 prefix
, s
->keep_alive_cnt
);
765 if (s
->defer_accept
> 0)
767 "%sDeferAcceptSec: %s\n",
768 prefix
, FORMAT_TIMESPAN(s
->defer_accept
, USEC_PER_SEC
));
770 LIST_FOREACH(port
, p
, s
->ports
) {
773 case SOCKET_SOCKET
: {
774 _cleanup_free_
char *k
= NULL
;
777 r
= socket_address_print(&p
->address
, &k
);
780 fprintf(f
, "%s%s: %m\n", prefix
, listen_lookup(socket_address_family(&p
->address
), p
->address
.type
));
782 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(socket_address_family(&p
->address
), p
->address
.type
), k
);
786 fprintf(f
, "%sListenSpecial: %s\n", prefix
, p
->path
);
788 case SOCKET_USB_FUNCTION
:
789 fprintf(f
, "%sListenUSBFunction: %s\n", prefix
, p
->path
);
792 fprintf(f
, "%sListenMessageQueue: %s\n", prefix
, p
->path
);
795 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
800 "%sTriggerLimitIntervalSec: %s\n"
801 "%sTriggerLimitBurst: %u\n"
802 "%sPollLimitIntervalSec: %s\n"
803 "%sPollLimitBurst: %u\n",
804 prefix
, FORMAT_TIMESPAN(s
->trigger_limit
.interval
, USEC_PER_SEC
),
805 prefix
, s
->trigger_limit
.burst
,
806 prefix
, FORMAT_TIMESPAN(s
->poll_limit
.interval
, USEC_PER_SEC
),
807 prefix
, s
->poll_limit
.burst
);
809 str
= ip_protocol_to_name(s
->socket_protocol
);
811 fprintf(f
, "%sSocketProtocol: %s\n", prefix
, str
);
813 if (!strv_isempty(s
->symlinks
)) {
814 fprintf(f
, "%sSymlinks:", prefix
);
815 STRV_FOREACH(q
, s
->symlinks
)
816 fprintf(f
, " %s", *q
);
822 "%sTimeoutSec: %s\n",
823 prefix
, FORMAT_TIMESPAN(s
->timeout_usec
, USEC_PER_SEC
));
825 exec_context_dump(&s
->exec_context
, f
, prefix
);
826 kill_context_dump(&s
->kill_context
, f
, prefix
);
828 for (SocketExecCommand c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
829 if (!s
->exec_command
[c
])
832 fprintf(f
, "%s%s %s:\n",
833 prefix
, glyph(GLYPH_ARROW_RIGHT
), socket_exec_command_to_string(c
));
835 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
838 cgroup_context_dump(UNIT(s
), f
, prefix
);
841 static int instance_from_socket(
846 union sockaddr_union local
, remote
;
854 if (getsockname(fd
, &local
.sa
, &l
) < 0)
858 if (getpeername(fd
, &remote
.sa
, &l
) < 0)
862 r
= socket_get_cookie(fd
, &cookie
);
868 switch (local
.sa
.sa_family
) {
872 a
= be32toh(local
.in
.sin_addr
.s_addr
),
873 b
= be32toh(remote
.in
.sin_addr
.s_addr
);
876 "%u-%" PRIu64
"-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
879 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
880 be16toh(local
.in
.sin_port
),
881 b
>> 24, (b
>> 16) & 0xFF, (b
>> 8) & 0xFF, b
& 0xFF,
882 be16toh(remote
.in
.sin_port
)) < 0)
889 static const unsigned char ipv4_prefix
[] = {
890 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
893 if (memcmp(&local
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0 &&
894 memcmp(&remote
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
896 *a
= local
.in6
.sin6_addr
.s6_addr
+12,
897 *b
= remote
.in6
.sin6_addr
.s6_addr
+12;
900 "%u-%" PRIu64
"-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
903 a
[0], a
[1], a
[2], a
[3],
904 be16toh(local
.in6
.sin6_port
),
905 b
[0], b
[1], b
[2], b
[3],
906 be16toh(remote
.in6
.sin6_port
)) < 0)
910 "%u-%" PRIu64
"-%s:%u-%s:%u",
913 IN6_ADDR_TO_STRING(&local
.in6
.sin6_addr
),
914 be16toh(local
.in6
.sin6_port
),
915 IN6_ADDR_TO_STRING(&remote
.in6
.sin6_addr
),
916 be16toh(remote
.in6
.sin6_port
)) < 0)
926 r
= getpeercred(fd
, &ucred
);
928 _cleanup_close_
int pidfd
= getpeerpidfd(fd
);
931 if (pidfd
>= 0 && pidfd_get_inode_id(pidfd
, &pidfd_id
) >= 0)
932 r
= asprintf(&s
, "%u-%" PRIu64
"-" PID_FMT
"_%" PRIu64
"-" UID_FMT
,
933 nr
, cookie
, ucred
.pid
, pidfd_id
, ucred
.uid
);
935 r
= asprintf(&s
, "%u-%" PRIu64
"-" PID_FMT
"-" UID_FMT
,
936 nr
, cookie
, ucred
.pid
, ucred
.uid
);
939 } else if (r
== -ENODATA
) {
940 /* This handles the case where somebody is connecting from another pid/uid namespace
941 * (e.g. from outside of our container). */
943 "%u-%" PRIu64
"-unknown",
955 "%u-%" PRIu64
"-%u:%u-%u:%u",
958 local
.vm
.svm_cid
, local
.vm
.svm_port
,
959 remote
.vm
.svm_cid
, remote
.vm
.svm_port
) < 0)
965 assert_not_reached();
972 static void socket_close_fds(Socket
*s
) {
975 LIST_FOREACH(port
, p
, s
->ports
) {
976 bool was_open
= p
->fd
>= 0;
978 p
->event_source
= sd_event_source_disable_unref(p
->event_source
);
979 p
->fd
= safe_close(p
->fd
);
980 socket_port_close_auxiliary_fds(p
);
982 /* One little note: we should normally not delete any sockets in the file system here! After all some
983 * other process we spawned might still have a reference of this fd and wants to continue to use
984 * it. Therefore we normally delete sockets in the file system before we create a new one, not after we
985 * stopped using one! That all said, if the user explicitly requested this, we'll delete them here
986 * anyway, but only then. */
988 if (!was_open
|| !s
->remove_on_stop
)
994 (void) unlink(p
->path
);
998 (void) mq_unlink(p
->path
);
1002 (void) socket_address_unlink(&p
->address
);
1010 if (s
->remove_on_stop
)
1011 STRV_FOREACH(i
, s
->symlinks
)
1014 /* Note that we don't return NULL here, since s has not been freed. */
1017 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(Socket
*, socket_close_fds
, NULL
);
1019 #define SOCKET_OPTION_WARNING_FORMAT_STR "Failed to set %s socket option, ignoring: %m"
1020 #define log_socket_option_warning_errno(s, error, option) \
1021 log_unit_warning_errno(UNIT(s), (error), SOCKET_OPTION_WARNING_FORMAT_STR, STRINGIFY(option))
1023 static void socket_apply_socket_options(Socket
*s
, SocketPort
*p
, int fd
) {
1030 if (s
->keep_alive
) {
1031 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_KEEPALIVE
, true);
1033 log_socket_option_warning_errno(s
, r
, SO_KEEPALIVE
);
1036 if (timestamp_is_set(s
->keep_alive_time
)) {
1037 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPIDLE
, s
->keep_alive_time
/ USEC_PER_SEC
);
1039 log_socket_option_warning_errno(s
, r
, TCP_KEEPIDLE
);
1042 if (s
->keep_alive_interval
> 0) {
1043 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPINTVL
, s
->keep_alive_interval
/ USEC_PER_SEC
);
1045 log_socket_option_warning_errno(s
, r
, TCP_KEEPINTVL
);
1048 if (s
->keep_alive_cnt
> 0) {
1049 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPCNT
, s
->keep_alive_cnt
);
1051 log_socket_option_warning_errno(s
, r
, TCP_KEEPCNT
);
1054 if (s
->defer_accept
> 0) {
1055 r
= setsockopt_int(fd
, SOL_TCP
, TCP_DEFER_ACCEPT
, s
->defer_accept
/ USEC_PER_SEC
);
1057 log_socket_option_warning_errno(s
, r
, TCP_DEFER_ACCEPT
);
1061 if (s
->socket_protocol
== IPPROTO_SCTP
) {
1062 r
= setsockopt_int(fd
, SOL_SCTP
, SCTP_NODELAY
, true);
1064 log_socket_option_warning_errno(s
, r
, SCTP_NODELAY
);
1066 r
= setsockopt_int(fd
, SOL_TCP
, TCP_NODELAY
, true);
1068 log_socket_option_warning_errno(s
, r
, TCP_NODELAY
);
1073 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_BROADCAST
, true);
1075 log_socket_option_warning_errno(s
, r
, SO_BROADCAST
);
1079 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSCRED
, true);
1081 log_socket_option_warning_errno(s
, r
, SO_PASSCRED
);
1084 if (s
->pass_pidfd
) {
1085 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSPIDFD
, true);
1087 log_unit_full_errno(UNIT(s
), ERRNO_IS_NEG_NOT_SUPPORTED(r
) ? LOG_DEBUG
: LOG_WARNING
, r
,
1088 SOCKET_OPTION_WARNING_FORMAT_STR
, "SO_PASSPIDFD");
1092 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSSEC
, true);
1094 log_unit_full_errno(UNIT(s
), ERRNO_IS_NEG_NOT_SUPPORTED(r
) ? LOG_DEBUG
: LOG_WARNING
, r
,
1095 SOCKET_OPTION_WARNING_FORMAT_STR
, "SO_PASSSEC");
1098 if (s
->pass_pktinfo
) {
1099 r
= socket_set_recvpktinfo(fd
, socket_address_family(&p
->address
), true);
1101 log_unit_warning_errno(UNIT(s
), r
, SOCKET_OPTION_WARNING_FORMAT_STR
, "packet info");
1104 if (!s
->pass_rights
) {
1105 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSRIGHTS
, false);
1107 log_unit_full_errno(UNIT(s
), ERRNO_IS_NEG_NOT_SUPPORTED(r
) ? LOG_DEBUG
: LOG_WARNING
, r
,
1108 SOCKET_OPTION_WARNING_FORMAT_STR
, "SO_PASSRIGHTS");
1111 if (s
->timestamping
!= SOCKET_TIMESTAMPING_OFF
) {
1112 r
= setsockopt_int(fd
, SOL_SOCKET
,
1113 s
->timestamping
== SOCKET_TIMESTAMPING_NS
? SO_TIMESTAMPNS
: SO_TIMESTAMP
,
1116 log_unit_warning_errno(UNIT(s
), r
, SOCKET_OPTION_WARNING_FORMAT_STR
, "timestamping");
1119 if (s
->priority
>= 0) {
1120 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PRIORITY
, s
->priority
);
1122 log_socket_option_warning_errno(s
, r
, SO_PRIORITY
);
1125 if (s
->receive_buffer
> 0) {
1126 r
= fd_set_rcvbuf(fd
, s
->receive_buffer
, false);
1128 log_unit_full_errno(UNIT(s
), ERRNO_IS_NEG_PRIVILEGE(r
) ? LOG_DEBUG
: LOG_WARNING
, r
,
1129 SOCKET_OPTION_WARNING_FORMAT_STR
, "SO_RCVBUF/SO_RCVBUFFORCE");
1132 if (s
->send_buffer
> 0) {
1133 r
= fd_set_sndbuf(fd
, s
->send_buffer
, false);
1135 log_unit_full_errno(UNIT(s
), ERRNO_IS_NEG_PRIVILEGE(r
) ? LOG_DEBUG
: LOG_WARNING
, r
,
1136 SOCKET_OPTION_WARNING_FORMAT_STR
, "SO_SNDBUF/SO_SNDBUFFORCE");
1140 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_MARK
, s
->mark
);
1142 log_socket_option_warning_errno(s
, r
, SO_MARK
);
1145 if (s
->ip_tos
>= 0) {
1146 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_TOS
, s
->ip_tos
);
1148 log_socket_option_warning_errno(s
, r
, IP_TOS
);
1151 if (s
->ip_ttl
>= 0) {
1152 r
= socket_set_ttl(fd
, socket_address_family(&p
->address
), s
->ip_ttl
);
1154 log_unit_warning_errno(UNIT(s
), r
, SOCKET_OPTION_WARNING_FORMAT_STR
, "IP_TTL/IPV6_UNICAST_HOPS");
1157 if (s
->tcp_congestion
)
1158 if (setsockopt(fd
, SOL_TCP
, TCP_CONGESTION
, s
->tcp_congestion
, strlen(s
->tcp_congestion
)+1) < 0)
1159 log_socket_option_warning_errno(s
, errno
, TCP_CONGESTION
);
1161 if (s
->smack_ip_in
) {
1162 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_IPIN
, s
->smack_ip_in
);
1164 log_unit_error_errno(UNIT(s
), r
, "Failed to apply SMACK label for IP input, ignoring: %m");
1167 if (s
->smack_ip_out
) {
1168 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_IPOUT
, s
->smack_ip_out
);
1170 log_unit_error_errno(UNIT(s
), r
, "Failed to apply SMACK label for IP output, ignoring: %m");
1174 static void socket_apply_fifo_options(Socket
*s
, int fd
) {
1180 if (s
->pipe_size
> 0)
1181 if (fcntl(fd
, F_SETPIPE_SZ
, s
->pipe_size
) < 0)
1182 log_unit_warning_errno(UNIT(s
), errno
, "Setting pipe size failed, ignoring: %m");
1185 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_ACCESS
, s
->smack
);
1187 log_unit_error_errno(UNIT(s
), r
, "SMACK relabelling failed, ignoring: %m");
1191 static int fifo_address_create(
1193 mode_t directory_mode
,
1194 mode_t socket_mode
) {
1196 _cleanup_close_
int fd
= -EBADF
;
1203 (void) mkdir_parents_label(path
, directory_mode
);
1205 r
= mac_selinux_create_file_prepare(path
, S_IFIFO
);
1209 /* Enforce the right access mode for the fifo */
1210 old_mask
= umask(~socket_mode
);
1212 /* Include the original umask in our mask */
1213 (void) umask(~socket_mode
| old_mask
);
1215 r
= mkfifo(path
, socket_mode
);
1216 (void) umask(old_mask
);
1218 if (r
< 0 && errno
!= EEXIST
) {
1223 fd
= open(path
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
| O_NOFOLLOW
);
1229 mac_selinux_create_file_clear();
1231 if (fstat(fd
, &st
) < 0) {
1236 if (!S_ISFIFO(st
.st_mode
) ||
1237 (st
.st_mode
& 0777) != (socket_mode
& ~old_mask
) ||
1238 st
.st_uid
!= getuid() ||
1239 st
.st_gid
!= getgid()) {
1247 mac_selinux_create_file_clear();
1251 static int special_address_create(const char *path
, bool writable
) {
1252 _cleanup_close_
int fd
= -EBADF
;
1257 fd
= open(path
, (writable
? O_RDWR
: O_RDONLY
)|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
);
1261 if (fstat(fd
, &st
) < 0)
1264 /* Check whether this is a /proc, /sys or /dev file or char device */
1265 if (!S_ISREG(st
.st_mode
) && !S_ISCHR(st
.st_mode
))
1271 static int usbffs_address_create_at(int dfd
, const char *name
) {
1272 _cleanup_close_
int fd
= -EBADF
;
1278 fd
= openat(dfd
, name
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
);
1282 if (fstat(fd
, &st
) < 0)
1285 /* Check whether this is a regular file (ffs endpoint) */
1286 if (!S_ISREG(st
.st_mode
))
1292 static int mq_address_create(
1298 _cleanup_close_
int fd
= -EBADF
;
1301 struct mq_attr _attr
, *attr
= NULL
;
1305 if (maxmsg
> 0 && msgsize
> 0) {
1306 _attr
= (struct mq_attr
) {
1307 .mq_flags
= O_NONBLOCK
,
1308 .mq_maxmsg
= maxmsg
,
1309 .mq_msgsize
= msgsize
,
1314 /* Enforce the right access mode for the mq */
1315 old_mask
= umask(~mq_mode
);
1317 /* Include the original umask in our mask */
1318 (void) umask(~mq_mode
| old_mask
);
1319 fd
= mq_open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_CREAT
, mq_mode
, attr
);
1320 (void) umask(old_mask
);
1325 if (fstat(fd
, &st
) < 0)
1328 if ((st
.st_mode
& 0777) != (mq_mode
& ~old_mask
) ||
1329 st
.st_uid
!= getuid() ||
1330 st
.st_gid
!= getgid())
1336 static int socket_symlink(Socket
*s
) {
1342 p
= socket_find_symlink_target(s
);
1346 STRV_FOREACH(i
, s
->symlinks
) {
1347 (void) mkdir_parents_label(*i
, s
->directory_mode
);
1349 r
= symlink_idempotent(p
, *i
, false);
1350 if (r
== -EEXIST
&& s
->remove_on_stop
) {
1351 /* If there's already something where we want to create the symlink, and the destructive
1352 * RemoveOnStop= mode is set, then we might as well try to remove what already exists and try
1355 if (unlink(*i
) >= 0)
1356 r
= symlink_idempotent(p
, *i
, false);
1359 log_unit_warning_errno(UNIT(s
), r
, "Failed to create symlink %s %s %s, ignoring: %m",
1360 p
, glyph(GLYPH_ARROW_RIGHT
), *i
);
1366 static int usbffs_write_descs(int fd
, Service
*s
) {
1372 if (!s
->usb_function_descriptors
|| !s
->usb_function_strings
)
1375 r
= copy_file_fd(s
->usb_function_descriptors
, fd
, 0);
1379 return copy_file_fd(s
->usb_function_strings
, fd
, 0);
1382 static int usbffs_dispatch_eps(SocketPort
*p
, int dfd
) {
1383 _cleanup_free_ DirectoryEntries
*des
= NULL
;
1389 r
= readdir_all(dfd
, RECURSE_DIR_SORT
|RECURSE_DIR_IGNORE_DOT
, &des
);
1393 p
->auxiliary_fds
= new(int, des
->n_entries
);
1394 if (!p
->auxiliary_fds
)
1397 FOREACH_ARRAY(i
, des
->entries
, des
->n_entries
) {
1398 const struct dirent
*de
= *i
;
1400 if (streq(de
->d_name
, "ep0"))
1403 r
= usbffs_address_create_at(dfd
, de
->d_name
);
1407 p
->auxiliary_fds
[p
->n_auxiliary_fds
++] = r
;
1410 assert(p
->n_auxiliary_fds
< des
->n_entries
);
1415 socket_port_close_auxiliary_fds(p
);
1419 int socket_load_service_unit(Socket
*s
, int cfd
, Unit
**ret
) {
1422 /* Figure out what the unit that will be used to handle the connections on the socket looks like.
1424 * If cfd < 0, then we don't have a connection yet. In case of Accept=yes sockets, use a fake
1431 if (UNIT_ISSET(s
->service
)) {
1432 *ret
= UNIT_DEREF(s
->service
);
1439 /* Build the instance name and load the unit */
1440 _cleanup_free_
char *prefix
= NULL
, *instance
= NULL
, *name
= NULL
;
1442 r
= unit_name_to_prefix(UNIT(s
)->id
, &prefix
);
1447 r
= instance_from_socket(cfd
, s
->n_accepted
, &instance
);
1448 if (ERRNO_IS_NEG_DISCONNECT(r
))
1449 /* ENOTCONN is legitimate if TCP RST was received. Other socket families might return
1450 * different errors. This connection is over, but the socket unit lives on. */
1451 return log_unit_debug_errno(UNIT(s
), r
,
1452 "Got %s on incoming socket, assuming aborted connection attempt, ignoring.",
1458 /* For accepting sockets, we don't know how the instance will be called until we get a connection and
1459 * can figure out what the peer name is. So let's use "internal" as the instance to make it clear
1460 * that this is not an actual peer name. We use "unknown" when we cannot figure out the peer. */
1461 r
= unit_name_build(prefix
, instance
?: "internal", ".service", &name
);
1465 return manager_load_unit(UNIT(s
)->manager
, name
, NULL
, NULL
, ret
);
1468 static int socket_determine_selinux_label(Socket
*s
, char **ret
) {
1475 r
= socket_load_service_unit(s
, /* cfd= */ -EBADF
, &service
);
1476 if (r
== -ENODATA
) {
1483 r
= service_determine_exec_selinux_label(SERVICE(service
), ret
);
1484 if (r
== -ENODATA
) {
1491 static int socket_address_listen_do(
1493 const SocketAddress
*address
,
1494 const char *label
) {
1499 return socket_address_listen(
1501 SOCK_CLOEXEC
|SOCK_NONBLOCK
,
1513 #define log_address_error_errno(u, address, error, fmt) \
1515 _cleanup_free_ char *_t = NULL; \
1517 (void) socket_address_print(address, &_t); \
1518 log_unit_error_errno(u, error, fmt, strna(_t)); \
1521 static bool fork_needed(const SocketAddress
*address
, Socket
*s
) {
1525 /* Check if we need to do the cgroup or netns stuff. If not we can do things much simpler. */
1527 /* If there are any NFTSet= directives with cgroup source, we need the cgroup */
1529 CGroupContext
*c
= unit_get_cgroup_context(u
);
1531 FOREACH_ARRAY(nft_set
, c
->nft_set_context
.sets
, c
->nft_set_context
.n_sets
)
1532 if (nft_set
->source
== NFT_SET_SOURCE_CGROUP
)
1535 if (IN_SET(address
->sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
) &&
1536 bpf_program_supported() > 0) /* If BPF firewalling isn't supported anyway — there's no point in this forking complexity */
1539 return exec_needs_network_namespace(&s
->exec_context
);
1542 static int socket_address_listen_in_cgroup(
1544 const SocketAddress
*address
,
1545 const char *label
) {
1547 _cleanup_(pidref_done
) PidRef pid
= PIDREF_NULL
;
1548 _cleanup_close_pair_
int pair
[2] = EBADF_PAIR
;
1554 /* This is a wrapper around socket_address_listen(), that forks off a helper process inside the
1555 * socket's cgroup and network namespace in which the socket is actually created. This way we ensure
1556 * the socket is actually properly attached to the unit's cgroup for the purpose of BPF filtering and
1559 if (!fork_needed(address
, s
)) {
1560 /* Shortcut things... */
1561 fd
= socket_address_listen_do(s
, address
, label
);
1563 return log_address_error_errno(UNIT(s
), address
, fd
, "Failed to create listening socket (%s): %m");
1568 r
= unit_setup_exec_runtime(UNIT(s
));
1570 return log_unit_error_errno(UNIT(s
), r
, "Failed acquire runtime: %m");
1572 if (s
->exec_context
.network_namespace_path
&&
1574 s
->exec_runtime
->shared
&&
1575 s
->exec_runtime
->shared
->netns_storage_socket
[0] >= 0) {
1576 r
= open_shareable_ns_path(s
->exec_runtime
->shared
->netns_storage_socket
, s
->exec_context
.network_namespace_path
, CLONE_NEWNET
);
1578 return log_unit_error_errno(UNIT(s
), r
, "Failed to open network namespace path %s: %m", s
->exec_context
.network_namespace_path
);
1581 if (s
->exec_context
.ipc_namespace_path
&&
1583 s
->exec_runtime
->shared
&&
1584 s
->exec_runtime
->shared
->ipcns_storage_socket
[0] >= 0) {
1585 r
= open_shareable_ns_path(s
->exec_runtime
->shared
->ipcns_storage_socket
, s
->exec_context
.ipc_namespace_path
, CLONE_NEWIPC
);
1587 return log_unit_error_errno(UNIT(s
), r
, "Failed to open IPC namespace path %s: %m", s
->exec_context
.ipc_namespace_path
);
1590 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
1591 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
1593 r
= unit_fork_helper_process(UNIT(s
), "(sd-listen)", /* into_cgroup= */ true, &pid
);
1595 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off listener stub process: %m");
1599 pair
[0] = safe_close(pair
[0]);
1601 if (exec_needs_network_namespace(&s
->exec_context
) &&
1603 s
->exec_runtime
->shared
&&
1604 s
->exec_runtime
->shared
->netns_storage_socket
[0] >= 0) {
1606 if (namespace_type_supported(NAMESPACE_NET
)) {
1607 r
= setup_shareable_ns(s
->exec_runtime
->shared
->netns_storage_socket
, CLONE_NEWNET
);
1609 log_unit_error_errno(UNIT(s
), r
, "Failed to join network namespace: %m");
1610 _exit(EXIT_NETWORK
);
1612 } else if (s
->exec_context
.network_namespace_path
) {
1613 log_unit_error(UNIT(s
), "Network namespace path configured but network namespaces not supported.");
1614 _exit(EXIT_NETWORK
);
1616 log_unit_warning(UNIT(s
), "PrivateNetwork=yes is configured, but the kernel does not support network namespaces, ignoring.");
1619 fd
= socket_address_listen_do(s
, address
, label
);
1621 log_address_error_errno(UNIT(s
), address
, fd
, "Failed to create listening socket (%s): %m");
1622 _exit(EXIT_FAILURE
);
1625 r
= send_one_fd(pair
[1], fd
, 0);
1627 log_address_error_errno(UNIT(s
), address
, r
, "Failed to send listening socket (%s) to parent: %m");
1628 _exit(EXIT_FAILURE
);
1631 _exit(EXIT_SUCCESS
);
1634 pair
[1] = safe_close(pair
[1]);
1635 fd
= receive_one_fd(pair
[0], 0);
1637 /* We synchronously wait for the helper, as it shouldn't be slow */
1638 r
= wait_for_terminate_and_check("(sd-listen)", pid
.pid
, WAIT_LOG_ABNORMAL
);
1645 return log_address_error_errno(UNIT(s
), address
, fd
, "Failed to receive listening socket (%s): %m");
1650 static int socket_open_fds(Socket
*orig_s
) {
1651 _cleanup_(socket_close_fdsp
) Socket
*s
= orig_s
;
1652 _cleanup_freecon_
char *label
= NULL
;
1653 bool know_label
= false;
1658 LIST_FOREACH(port
, p
, s
->ports
) {
1668 /* Figure out the label, if we don't it know yet. We do it once for the first
1669 * socket where we need this and remember it for the rest. */
1671 r
= socket_determine_selinux_label(s
, &label
);
1673 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine SELinux label: %m");
1678 /* Apply the socket protocol */
1679 switch (p
->address
.type
) {
1682 if (IN_SET(s
->socket_protocol
, IPPROTO_SCTP
, IPPROTO_MPTCP
))
1683 p
->address
.protocol
= s
->socket_protocol
;
1686 case SOCK_SEQPACKET
:
1687 if (s
->socket_protocol
== IPPROTO_SCTP
)
1688 p
->address
.protocol
= s
->socket_protocol
;
1692 if (s
->socket_protocol
== IPPROTO_UDPLITE
)
1693 p
->address
.protocol
= s
->socket_protocol
;
1697 p
->fd
= socket_address_listen_in_cgroup(s
, &p
->address
, label
);
1701 socket_apply_socket_options(s
, p
, p
->fd
);
1705 case SOCKET_SPECIAL
:
1707 p
->fd
= special_address_create(p
->path
, s
->writable
);
1709 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open special file '%s': %m", p
->path
);
1714 p
->fd
= fifo_address_create(
1719 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open FIFO '%s': %m", p
->path
);
1721 socket_apply_fifo_options(s
, p
->fd
);
1727 p
->fd
= mq_address_create(
1733 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open message queue '%s': %m", p
->path
);
1736 case SOCKET_USB_FUNCTION
: {
1737 _cleanup_close_
int dfd
= -EBADF
;
1739 dfd
= open(p
->path
, O_DIRECTORY
|O_CLOEXEC
);
1741 return log_unit_error_errno(UNIT(s
), errno
,
1742 "Failed to open USB FunctionFS dir '%s': %m", p
->path
);
1744 p
->fd
= usbffs_address_create_at(dfd
, "ep0");
1746 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open USB FunctionFS ep0: %m");
1748 r
= usbffs_write_descs(p
->fd
, SERVICE(UNIT_DEREF(s
->service
)));
1750 return log_unit_error_errno(UNIT(s
), r
, "Failed to write to USB FunctionFS ep0: %m");
1752 r
= usbffs_dispatch_eps(p
, dfd
);
1754 return log_unit_error_errno(UNIT(s
), r
, "Failed to dispatch USB FunctionFS eps: %m");
1760 assert_not_reached();
1768 static void socket_unwatch_fds(Socket
*s
) {
1773 LIST_FOREACH(port
, p
, s
->ports
) {
1777 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_OFF
);
1779 log_unit_debug_errno(UNIT(s
), r
, "Failed to disable event source: %m");
1783 static int socket_watch_fds(Socket
*s
) {
1788 LIST_FOREACH(port
, p
, s
->ports
) {
1792 if (p
->event_source
) {
1793 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_ON
);
1797 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &p
->event_source
, p
->fd
, EPOLLIN
, socket_dispatch_io
, p
);
1801 (void) sd_event_source_set_description(p
->event_source
, "socket-port-io");
1804 r
= sd_event_source_set_ratelimit(p
->event_source
, s
->poll_limit
.interval
, s
->poll_limit
.burst
);
1806 log_unit_debug_errno(UNIT(s
), r
, "Failed to set poll limit on I/O event source, ignoring: %m");
1812 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch listening fds: %m");
1813 socket_unwatch_fds(s
);
1823 static int socket_check_open(Socket
*s
) {
1824 bool have_open
= false, have_closed
= false;
1828 LIST_FOREACH(port
, p
, s
->ports
) {
1834 if (have_open
&& have_closed
)
1835 return SOCKET_OPEN_SOME
;
1839 return SOCKET_OPEN_ALL
;
1841 return SOCKET_OPEN_NONE
;
1844 static void socket_set_state(Socket
*s
, SocketState state
) {
1845 SocketState old_state
;
1849 if (s
->state
!= state
)
1850 bus_unit_send_pending_change_signal(UNIT(s
), false);
1852 old_state
= s
->state
;
1855 if (!SOCKET_STATE_WITH_PROCESS(state
)) {
1856 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
1857 socket_unwatch_control_pid(s
);
1858 s
->control_command
= NULL
;
1859 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1862 if (state
!= SOCKET_LISTENING
)
1863 socket_unwatch_fds(s
);
1872 SOCKET_STOP_PRE_SIGTERM
,
1873 SOCKET_STOP_PRE_SIGKILL
))
1874 socket_close_fds(s
);
1876 if (state
!= old_state
)
1877 log_unit_debug(UNIT(s
), "Changed %s -> %s", socket_state_to_string(old_state
), socket_state_to_string(state
));
1879 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], /* reload_success = */ true);
1882 static int socket_coldplug(Unit
*u
) {
1883 Socket
*s
= ASSERT_PTR(SOCKET(u
));
1886 assert(s
->state
== SOCKET_DEAD
);
1888 if (s
->deserialized_state
== s
->state
)
1891 if (pidref_is_set(&s
->control_pid
) &&
1892 pidref_is_unwaited(&s
->control_pid
) > 0 &&
1893 SOCKET_STATE_WITH_PROCESS(s
->deserialized_state
)) {
1895 r
= unit_watch_pidref(UNIT(s
), &s
->control_pid
, /* exclusive= */ false);
1899 r
= socket_arm_timer(s
, /* relative= */ false, usec_add(u
->state_change_timestamp
.monotonic
, s
->timeout_usec
));
1904 if (IN_SET(s
->deserialized_state
,
1911 /* Originally, we used to simply reopen all sockets here that we didn't have file descriptors
1912 * for. However, this is problematic, as we won't traverse through the SOCKET_START_CHOWN state for
1913 * them, and thus the UID/GID wouldn't be right. Hence, instead simply check if we have all fds open,
1914 * and if there's a mismatch, warn loudly.
1916 * Note that SOCKET_START_OPEN requires no special treatment, as it's only intermediate
1917 * between SOCKET_START_PRE and SOCKET_START_CHOWN and shall otherwise not be observed.
1918 * It's listed only for consistency. */
1920 r
= socket_check_open(s
);
1921 if (r
== SOCKET_OPEN_NONE
)
1922 log_unit_warning(UNIT(s
),
1923 "Socket unit configuration has changed while unit has been running, "
1924 "no open socket file descriptor left. "
1925 "The socket unit is not functional until restarted.");
1926 else if (r
== SOCKET_OPEN_SOME
)
1927 log_unit_warning(UNIT(s
),
1928 "Socket unit configuration has changed while unit has been running, "
1929 "and some socket file descriptors have not been opened yet. "
1930 "The socket unit is not fully functional until restarted.");
1933 if (s
->deserialized_state
== SOCKET_LISTENING
) {
1934 r
= socket_watch_fds(s
);
1939 if (!IN_SET(s
->deserialized_state
, SOCKET_DEAD
, SOCKET_FAILED
, SOCKET_CLEANING
))
1940 (void) unit_setup_exec_runtime(u
);
1942 socket_set_state(s
, s
->deserialized_state
);
1946 static int socket_spawn(Socket
*s
, ExecCommand
*c
, PidRef
*ret_pid
) {
1947 _cleanup_(exec_params_shallow_clear
) ExecParameters exec_params
= EXEC_PARAMETERS_INIT(
1948 EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
);
1949 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
1956 r
= unit_prepare_exec(UNIT(s
));
1960 r
= socket_arm_timer(s
, /* relative= */ true, s
->timeout_usec
);
1964 r
= unit_set_exec_params(UNIT(s
), &exec_params
);
1968 /* Note that ExecStartPre= command doesn't inherit any FDs. It runs before we open listen FDs. */
1969 if (s
->pass_fds_to_exec
) {
1970 _cleanup_strv_free_
char **fd_names
= NULL
;
1971 _cleanup_free_
int *fds
= NULL
;
1974 n_fds
= socket_collect_fds(s
, &fds
);
1978 r
= strv_extend_n(&fd_names
, socket_fdname(s
), n_fds
);
1982 exec_params
.flags
|= EXEC_PASS_FDS
;
1983 exec_params
.fds
= TAKE_PTR(fds
);
1984 exec_params
.fd_names
= TAKE_PTR(fd_names
);
1985 exec_params
.n_socket_fds
= n_fds
;
1988 r
= exec_spawn(UNIT(s
),
1998 r
= unit_watch_pidref(UNIT(s
), &pidref
, /* exclusive= */ true);
2002 *ret_pid
= TAKE_PIDREF(pidref
);
2006 static int socket_chown(Socket
*s
, PidRef
*ret_pid
) {
2007 _cleanup_(pidref_done
) PidRef pid
= PIDREF_NULL
;
2012 r
= socket_arm_timer(s
, /* relative= */ true, s
->timeout_usec
);
2016 /* We have to resolve the user names out-of-process, hence
2017 * let's fork here. It's messy, but well, what can we do? */
2019 r
= unit_fork_helper_process(UNIT(s
), "(sd-chown)", /* into_cgroup= */ true, &pid
);
2023 uid_t uid
= UID_INVALID
;
2024 gid_t gid
= GID_INVALID
;
2028 if (!isempty(s
->user
)) {
2029 const char *user
= s
->user
;
2031 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
, 0);
2033 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve user %s: %m", user
);
2038 if (!isempty(s
->group
)) {
2039 const char *group
= s
->group
;
2041 r
= get_group_creds(&group
, &gid
, 0);
2043 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve group %s: %m", group
);
2048 LIST_FOREACH(port
, p
, s
->ports
) {
2049 const char *path
= NULL
;
2051 if (p
->type
== SOCKET_SOCKET
)
2052 path
= socket_address_get_path(&p
->address
);
2053 else if (p
->type
== SOCKET_FIFO
)
2055 else if (p
->type
== SOCKET_MQUEUE
) {
2056 /* Use fchown on the fd since /dev/mqueue might not be mounted. */
2057 if (fchown(p
->fd
, uid
, gid
) < 0) {
2058 log_unit_error_errno(UNIT(s
), errno
, "Failed to fchown(): %m");
2067 if (chown(path
, uid
, gid
) < 0) {
2068 log_unit_error_errno(UNIT(s
), errno
, "Failed to chown(): %m");
2073 _exit(EXIT_SUCCESS
);
2076 r
= unit_watch_pidref(UNIT(s
), &pid
, /* exclusive= */ true);
2080 *ret_pid
= TAKE_PIDREF(pid
);
2084 static void socket_enter_dead(Socket
*s
, SocketResult f
) {
2087 if (s
->result
== SOCKET_SUCCESS
)
2090 if (s
->result
== SOCKET_SUCCESS
)
2091 unit_log_success(UNIT(s
));
2093 unit_log_failure(UNIT(s
), socket_result_to_string(s
->result
));
2095 unit_warn_leftover_processes(UNIT(s
), /* start = */ false);
2097 socket_set_state(s
, s
->result
!= SOCKET_SUCCESS
? SOCKET_FAILED
: SOCKET_DEAD
);
2099 s
->exec_runtime
= exec_runtime_destroy(s
->exec_runtime
);
2101 unit_destroy_runtime_data(UNIT(s
), &s
->exec_context
, /* destroy_runtime_dir = */ true);
2103 unit_unref_uid_gid(UNIT(s
), true);
2106 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
);
2108 static void socket_enter_stop_post(Socket
*s
, SocketResult f
) {
2113 if (s
->result
== SOCKET_SUCCESS
)
2116 socket_unwatch_control_pid(s
);
2117 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
2118 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
];
2120 if (s
->control_command
) {
2121 pidref_done(&s
->control_pid
);
2123 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2125 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'stop-post' task: %m");
2126 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2130 socket_set_state(s
, SOCKET_STOP_POST
);
2132 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_SUCCESS
);
2135 static int state_to_kill_operation(Socket
*s
, SocketState state
) {
2138 if (state
== SOCKET_STOP_PRE_SIGTERM
)
2139 return unit_has_job_type(UNIT(s
), JOB_RESTART
) ? KILL_RESTART
: KILL_TERMINATE
;
2141 if (state
== SOCKET_FINAL_SIGTERM
)
2142 return KILL_TERMINATE
;
2147 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
) {
2152 if (s
->result
== SOCKET_SUCCESS
)
2155 r
= unit_kill_context(UNIT(s
), state_to_kill_operation(s
, state
));
2157 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
2161 r
= socket_arm_timer(s
, /* relative= */ true, s
->timeout_usec
);
2163 log_unit_warning_errno(UNIT(s
), r
, "Failed to install timer: %m");
2167 socket_set_state(s
, state
);
2168 } else if (state
== SOCKET_STOP_PRE_SIGTERM
)
2169 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_SUCCESS
);
2170 else if (state
== SOCKET_STOP_PRE_SIGKILL
)
2171 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2172 else if (state
== SOCKET_FINAL_SIGTERM
)
2173 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_SUCCESS
);
2175 socket_enter_dead(s
, SOCKET_SUCCESS
);
2180 if (IN_SET(state
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_STOP_PRE_SIGKILL
))
2181 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2183 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2186 static void socket_enter_stop_pre(Socket
*s
, SocketResult f
) {
2191 if (s
->result
== SOCKET_SUCCESS
)
2194 socket_unwatch_control_pid(s
);
2195 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
2196 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
];
2198 if (s
->control_command
) {
2199 pidref_done(&s
->control_pid
);
2201 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2203 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'stop-pre' task: %m");
2204 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2208 socket_set_state(s
, SOCKET_STOP_PRE
);
2210 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2213 static void flush_ports(Socket
*s
) {
2216 /* Flush all incoming traffic, regardless if actual bytes or new connections, so that this socket isn't busy
2219 LIST_FOREACH(port
, p
, s
->ports
) {
2223 if (p
->type
== SOCKET_MQUEUE
)
2224 (void) flush_mqueue(p
->fd
);
2226 (void) flush_accept(p
->fd
);
2227 (void) flush_fd(p
->fd
);
2232 static void socket_enter_listening(Socket
*s
) {
2237 if (!s
->accept
&& s
->flush_pending
) {
2238 log_unit_debug(UNIT(s
), "Flushing socket before listening.");
2242 r
= socket_watch_fds(s
);
2244 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch sockets: %m");
2245 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2249 socket_set_state(s
, SOCKET_LISTENING
);
2252 static void socket_enter_start_post(Socket
*s
) {
2257 socket_unwatch_control_pid(s
);
2258 s
->control_command_id
= SOCKET_EXEC_START_POST
;
2259 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
];
2261 if (s
->control_command
) {
2262 pidref_done(&s
->control_pid
);
2264 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2266 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'start-post' task: %m");
2267 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2271 socket_set_state(s
, SOCKET_START_POST
);
2273 socket_enter_listening(s
);
2276 static void socket_enter_start_chown(Socket
*s
) {
2280 assert(s
->state
== SOCKET_START_OPEN
);
2282 if (!isempty(s
->user
) || !isempty(s
->group
)) {
2284 socket_unwatch_control_pid(s
);
2285 s
->control_command_id
= SOCKET_EXEC_START_CHOWN
;
2286 s
->control_command
= NULL
;
2288 r
= socket_chown(s
, &s
->control_pid
);
2290 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'start-chown' task: %m");
2291 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2295 socket_set_state(s
, SOCKET_START_CHOWN
);
2297 socket_enter_start_post(s
);
2300 static void socket_enter_start_open(Socket
*s
) {
2304 assert(IN_SET(s
->state
, SOCKET_DEAD
, SOCKET_FAILED
, SOCKET_START_PRE
));
2306 /* We force a state transition here even though we're not spawning any process (i.e. the state is purely
2307 * intermediate), so that failure of socket_open_fds() always causes a state change in unit_notify().
2308 * Otherwise, if no Exec*= is defined, we might go from previous SOCKET_FAILED to SOCKET_FAILED,
2309 * meaning the OnFailure= deps are unexpectedly skipped (#35635). */
2311 socket_set_state(s
, SOCKET_START_OPEN
);
2313 r
= socket_open_fds(s
);
2315 log_unit_error_errno(UNIT(s
), r
, "Failed to listen on sockets: %m");
2316 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2320 socket_enter_start_chown(s
);
2323 static void socket_enter_start_pre(Socket
*s
) {
2328 socket_unwatch_control_pid(s
);
2330 unit_warn_leftover_processes(UNIT(s
), /* start = */ true);
2332 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
2333 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
];
2335 if (s
->control_command
) {
2336 pidref_done(&s
->control_pid
);
2338 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2340 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn 'start-pre' task: %m");
2341 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2345 socket_set_state(s
, SOCKET_START_PRE
);
2347 socket_enter_start_open(s
);
2350 static void socket_enter_running(Socket
*s
, int cfd_in
) {
2351 /* Note that this call takes possession of the connection fd passed. It either has to assign it
2352 * somewhere or close it. */
2353 _cleanup_close_
int cfd
= cfd_in
;
2354 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2359 /* We don't take connections anymore if we are supposed to shut down anyway */
2360 if (unit_stop_pending(UNIT(s
))) {
2362 log_unit_debug(UNIT(s
), "Suppressing connection request since unit stop is scheduled.");
2371 if (!ratelimit_below(&s
->trigger_limit
)) {
2372 log_unit_warning(UNIT(s
), "Trigger limit hit, refusing further activation.");
2373 socket_enter_stop_pre(s
, SOCKET_FAILURE_TRIGGER_LIMIT_HIT
);
2377 if (cfd
< 0) { /* Accept=no case */
2378 bool pending
= false;
2381 /* If there's already a start pending don't bother to do anything */
2382 UNIT_FOREACH_DEPENDENCY(other
, UNIT(s
), UNIT_ATOM_TRIGGERS
)
2383 if (unit_active_or_pending(other
)) {
2389 if (!UNIT_ISSET(s
->service
)) {
2390 log_unit_warning(UNIT(s
),
2391 "Service to activate vanished, refusing activation.");
2395 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT_DEREF(s
->service
), JOB_REPLACE
, &error
, /* ret = */ NULL
);
2397 return (void) log_unit_debug_errno(UNIT(s
), r
, "Failed to queue service startup job, ignoring: %s", bus_error_message(&error
, r
));
2402 socket_set_state(s
, SOCKET_RUNNING
);
2403 } else { /* Accept=yes case */
2404 _cleanup_(socket_peer_unrefp
) SocketPeer
*p
= NULL
;
2407 if (s
->n_connections
>= s
->max_connections
) {
2408 log_unit_warning(UNIT(s
), "Too many incoming connections (%u), dropping connection.",
2413 if (s
->max_connections_per_source
> 0) {
2414 r
= socket_acquire_peer(s
, cfd
, &p
);
2416 if (ERRNO_IS_DISCONNECT(r
))
2418 /* We didn't have enough resources to acquire peer information, let's fail. */
2421 if (r
> 0 && p
->n_ref
> s
->max_connections_per_source
) {
2422 _cleanup_free_
char *t
= NULL
;
2424 if (p
->peer
.sa
.sa_family
== AF_UNIX
)
2425 (void) asprintf(&t
, "UID " UID_FMT
, p
->peer_cred
.uid
);
2427 (void) sockaddr_pretty(&p
->peer
.sa
, p
->peer_salen
, /* translate_ipv6= */ true, /* include_port= */ false, &t
);
2429 log_unit_warning(UNIT(s
),
2430 "Too many incoming connections (%u) from source %s, dropping connection.",
2431 p
->n_ref
, strnull(t
));
2436 r
= socket_load_service_unit(s
, cfd
, &service
);
2437 if (ERRNO_IS_NEG_DISCONNECT(r
))
2439 if (r
< 0 || UNIT_IS_LOAD_ERROR(service
->load_state
)) {
2440 log_unit_warning_errno(UNIT(s
), r
< 0 ? r
: service
->load_error
,
2441 "Failed to load connection service unit: %m");
2444 if (service
->load_state
== UNIT_MASKED
) {
2445 log_unit_warning(UNIT(s
), "Connection service unit is masked, refusing.");
2451 r
= service_set_socket_fd(SERVICE(service
), cfd
, s
, p
, s
->selinux_context_from_net
);
2452 if (ERRNO_IS_NEG_DISCONNECT(r
))
2455 log_unit_warning_errno(UNIT(s
), r
, "Failed to set socket on service: %m");
2459 /* We passed ownership of the fd and socket peer to the service now. */
2465 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, service
, JOB_REPLACE
, &error
, /* ret = */ NULL
);
2467 /* We failed to activate the new service, but it still exists. Let's make sure the
2468 * service closes and forgets the connection fd again, immediately. */
2469 service_release_socket_fd(SERVICE(service
));
2473 /* Notify clients about changed counters */
2474 unit_add_to_dbus_queue(UNIT(s
));
2484 log_unit_warning_errno(UNIT(s
), r
, "Failed to queue service startup job%s: %s",
2485 cfd
>= 0 && !ERRNO_IS_RESOURCE(r
) ? " (Maybe the service is missing or is a template unit?)" : "",
2486 bus_error_message(&error
, r
));
2489 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2492 static void socket_run_next(Socket
*s
) {
2496 assert(s
->control_command
);
2497 assert(s
->control_command
->command_next
);
2499 socket_unwatch_control_pid(s
);
2501 s
->control_command
= s
->control_command
->command_next
;
2503 pidref_done(&s
->control_pid
);
2505 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2507 log_unit_warning_errno(UNIT(s
), r
, "Failed to spawn next task: %m");
2509 if (s
->state
== SOCKET_START_POST
)
2510 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2511 else if (s
->state
== SOCKET_STOP_POST
)
2512 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2514 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2518 static int socket_start(Unit
*u
) {
2519 Socket
*s
= ASSERT_PTR(SOCKET(u
));
2522 /* We cannot fulfill this request right now, try again later
2524 if (IN_SET(s
->state
,
2526 SOCKET_STOP_PRE_SIGKILL
,
2527 SOCKET_STOP_PRE_SIGTERM
,
2529 SOCKET_FINAL_SIGTERM
,
2530 SOCKET_FINAL_SIGKILL
,
2534 /* Already on it! */
2535 if (IN_SET(s
->state
,
2542 /* Cannot run this without the service being around */
2543 if (UNIT_ISSET(s
->service
)) {
2544 Service
*service
= ASSERT_PTR(SERVICE(UNIT_DEREF(s
->service
)));
2546 if (UNIT(service
)->load_state
!= UNIT_LOADED
)
2547 return log_unit_error_errno(u
, SYNTHETIC_ERRNO(ENOENT
),
2548 "Socket service %s not loaded, refusing.", UNIT(service
)->id
);
2550 /* If the service is already active we cannot start the socket */
2551 if (SOCKET_SERVICE_IS_ACTIVE(service
, /* allow_finalize = */ false))
2552 return log_unit_error_errno(u
, SYNTHETIC_ERRNO(EBUSY
),
2553 "Socket service %s already active, refusing.", UNIT(service
)->id
);
2556 assert(IN_SET(s
->state
, SOCKET_DEAD
, SOCKET_FAILED
));
2558 r
= unit_acquire_invocation_id(u
);
2562 s
->result
= SOCKET_SUCCESS
;
2563 exec_command_reset_status_list_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
2565 if (s
->cgroup_runtime
)
2566 s
->cgroup_runtime
->reset_accounting
= true;
2568 socket_enter_start_pre(s
);
2572 static int socket_stop(Unit
*u
) {
2573 Socket
*s
= ASSERT_PTR(SOCKET(u
));
2576 if (IN_SET(s
->state
,
2578 SOCKET_STOP_PRE_SIGTERM
,
2579 SOCKET_STOP_PRE_SIGKILL
,
2581 SOCKET_FINAL_SIGTERM
,
2582 SOCKET_FINAL_SIGKILL
))
2585 /* If there's already something running we go directly into
2587 if (IN_SET(s
->state
,
2591 SOCKET_START_POST
)) {
2592 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_SUCCESS
);
2596 /* If we are currently cleaning, then abort it, brutally. */
2597 if (s
->state
== SOCKET_CLEANING
) {
2598 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_SUCCESS
);
2602 assert(IN_SET(s
->state
, SOCKET_LISTENING
, SOCKET_RUNNING
));
2604 socket_enter_stop_pre(s
, SOCKET_SUCCESS
);
2608 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2609 Socket
*s
= ASSERT_PTR(SOCKET(u
));
2615 (void) serialize_item(f
, "state", socket_state_to_string(s
->state
));
2616 (void) serialize_item(f
, "result", socket_result_to_string(s
->result
));
2617 (void) serialize_item_format(f
, "n-accepted", "%u", s
->n_accepted
);
2618 (void) serialize_item_format(f
, "n-refused", "%u", s
->n_refused
);
2619 (void) serialize_pidref(f
, fds
, "control-pid", &s
->control_pid
);
2621 if (s
->control_command_id
>= 0)
2622 (void) serialize_item(f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
2624 LIST_FOREACH(port
, p
, s
->ports
) {
2630 copy
= fdset_put_dup(fds
, p
->fd
);
2632 return log_unit_warning_errno(u
, copy
, "Failed to serialize socket fd: %m");
2634 if (p
->type
== SOCKET_SOCKET
) {
2635 _cleanup_free_
char *t
= NULL
;
2637 r
= socket_address_print(&p
->address
, &t
);
2639 return log_unit_error_errno(u
, r
, "Failed to format socket address: %m");
2641 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2642 (void) serialize_item_format(f
, "netlink", "%i %s", copy
, t
);
2644 (void) serialize_item_format(f
, "socket", "%i %i %s", copy
, p
->address
.type
, t
);
2645 } else if (p
->type
== SOCKET_SPECIAL
)
2646 (void) serialize_item_format(f
, "special", "%i %s", copy
, p
->path
);
2647 else if (p
->type
== SOCKET_MQUEUE
)
2648 (void) serialize_item_format(f
, "mqueue", "%i %s", copy
, p
->path
);
2649 else if (p
->type
== SOCKET_USB_FUNCTION
)
2650 (void) serialize_item_format(f
, "ffs", "%i %s", copy
, p
->path
);
2652 assert(p
->type
== SOCKET_FIFO
);
2653 (void) serialize_item_format(f
, "fifo", "%i %s", copy
, p
->path
);
2657 (void) serialize_ratelimit(f
, "trigger-ratelimit", &s
->trigger_limit
);
2662 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2663 Socket
*s
= ASSERT_PTR(SOCKET(u
));
2669 if (streq(key
, "state")) {
2672 state
= socket_state_from_string(value
);
2674 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2676 s
->deserialized_state
= state
;
2677 } else if (streq(key
, "result")) {
2680 f
= socket_result_from_string(value
);
2682 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2683 else if (f
!= SOCKET_SUCCESS
)
2686 } else if (streq(key
, "n-accepted")) {
2689 if (safe_atou(value
, &k
) < 0)
2690 log_unit_debug(u
, "Failed to parse n-accepted value: %s", value
);
2693 } else if (streq(key
, "n-refused")) {
2696 if (safe_atou(value
, &k
) < 0)
2697 log_unit_debug(u
, "Failed to parse n-refused value: %s", value
);
2700 } else if (streq(key
, "control-pid")) {
2702 if (!pidref_is_set(&s
->control_pid
))
2703 (void) deserialize_pidref(fds
, value
, &s
->control_pid
);
2705 } else if (streq(key
, "control-command")) {
2706 SocketExecCommand id
;
2708 id
= socket_exec_command_from_string(value
);
2710 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2712 s
->control_command_id
= id
;
2713 s
->control_command
= s
->exec_command
[id
];
2715 } else if (streq(key
, "fifo")) {
2716 _cleanup_free_
char *fdv
= NULL
;
2720 r
= extract_first_word(&value
, &fdv
, NULL
, 0);
2722 log_unit_debug(u
, "Failed to parse fifo value: %s", value
);
2727 if (fd
< 0 || !fdset_contains(fds
, fd
)) {
2728 log_unit_debug(u
, "Invalid fifo value: %s", fdv
);
2732 LIST_FOREACH(port
, p
, s
->ports
)
2734 p
->type
== SOCKET_FIFO
&&
2735 path_equal_or_inode_same(p
->path
, value
, 0)) {
2736 p
->fd
= fdset_remove(fds
, fd
);
2741 log_unit_debug(u
, "No matching fifo socket found: %s", value
);
2743 } else if (streq(key
, "special")) {
2744 _cleanup_free_
char *fdv
= NULL
;
2748 r
= extract_first_word(&value
, &fdv
, NULL
, 0);
2750 log_unit_debug(u
, "Failed to parse special value: %s", value
);
2755 if (fd
< 0 || !fdset_contains(fds
, fd
)) {
2756 log_unit_debug(u
, "Invalid special value: %s", fdv
);
2760 LIST_FOREACH(port
, p
, s
->ports
)
2762 p
->type
== SOCKET_SPECIAL
&&
2763 path_equal_or_inode_same(p
->path
, value
, 0)) {
2764 p
->fd
= fdset_remove(fds
, fd
);
2769 log_unit_debug(u
, "No matching special socket found: %s", value
);
2771 } else if (streq(key
, "mqueue")) {
2772 _cleanup_free_
char *fdv
= NULL
;
2776 r
= extract_first_word(&value
, &fdv
, NULL
, 0);
2778 log_unit_debug(u
, "Failed to parse mqueue value: %s", value
);
2783 if (fd
< 0 || !fdset_contains(fds
, fd
)) {
2784 log_unit_debug(u
, "Invalid mqueue value: %s", fdv
);
2788 LIST_FOREACH(port
, p
, s
->ports
)
2790 p
->type
== SOCKET_MQUEUE
&&
2791 streq(p
->path
, value
)) {
2792 p
->fd
= fdset_remove(fds
, fd
);
2797 log_unit_debug(u
, "No matching mqueue socket found: %s", value
);
2799 } else if (streq(key
, "socket")) {
2800 _cleanup_free_
char *fdv
= NULL
, *typev
= NULL
;
2804 r
= extract_first_word(&value
, &fdv
, NULL
, 0);
2806 log_unit_debug(u
, "Failed to parse socket fd from value: %s", value
);
2811 if (fd
< 0 || !fdset_contains(fds
, fd
)) {
2812 log_unit_debug(u
, "Invalid socket fd: %s", fdv
);
2816 r
= extract_first_word(&value
, &typev
, NULL
, 0);
2818 log_unit_debug(u
, "Failed to parse socket type from value: %s", value
);
2822 if (safe_atoi(typev
, &type
) < 0 || type
< 0) {
2823 log_unit_debug(u
, "Invalid socket type: %s", typev
);
2827 LIST_FOREACH(port
, p
, s
->ports
)
2829 socket_address_is(&p
->address
, value
, type
)) {
2830 p
->fd
= fdset_remove(fds
, fd
);
2835 log_unit_debug(u
, "No matching %s socket found: %s",
2836 socket_address_type_to_string(type
), value
);
2838 } else if (streq(key
, "netlink")) {
2839 _cleanup_free_
char *fdv
= NULL
;
2843 r
= extract_first_word(&value
, &fdv
, NULL
, 0);
2845 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2850 if (fd
< 0 || !fdset_contains(fds
, fd
)) {
2851 log_unit_debug(u
, "Invalid socket value: %s", fdv
);
2855 LIST_FOREACH(port
, p
, s
->ports
)
2857 socket_address_is_netlink(&p
->address
, value
)) {
2858 p
->fd
= fdset_remove(fds
, fd
);
2863 log_unit_debug(u
, "No matching netlink socket found: %s", value
);
2865 } else if (streq(key
, "ffs")) {
2866 _cleanup_free_
char *fdv
= NULL
;
2870 r
= extract_first_word(&value
, &fdv
, NULL
, 0);
2872 log_unit_debug(u
, "Failed to parse ffs value: %s", value
);
2877 if (fd
< 0 || !fdset_contains(fds
, fd
)) {
2878 log_unit_debug(u
, "Invalid ffs value: %s", fdv
);
2882 LIST_FOREACH(port
, p
, s
->ports
)
2884 p
->type
== SOCKET_USB_FUNCTION
&&
2885 path_equal_or_inode_same(p
->path
, value
, 0)) {
2886 p
->fd
= fdset_remove(fds
, fd
);
2891 log_unit_debug(u
, "No matching ffs socket found: %s", value
);
2893 } else if (streq(key
, "trigger-ratelimit"))
2894 (void) deserialize_ratelimit(&s
->trigger_limit
, key
, value
);
2896 log_unit_debug(UNIT(s
), "Unknown serialization key: %s", key
);
2901 static void socket_distribute_fds(Unit
*u
, FDSet
*fds
) {
2902 Socket
*s
= ASSERT_PTR(SOCKET(u
));
2904 LIST_FOREACH(port
, p
, s
->ports
) {
2907 if (p
->type
!= SOCKET_SOCKET
)
2913 FDSET_FOREACH(fd
, fds
) {
2914 if (socket_address_matches_fd(&p
->address
, fd
)) {
2915 p
->fd
= fdset_remove(fds
, fd
);
2916 s
->deserialized_state
= SOCKET_LISTENING
;
2923 static UnitActiveState
socket_active_state(Unit
*u
) {
2924 Socket
*s
= ASSERT_PTR(SOCKET(u
));
2926 return state_translation_table
[s
->state
];
2929 static const char *socket_sub_state_to_string(Unit
*u
) {
2930 Socket
*s
= ASSERT_PTR(SOCKET(u
));
2932 return socket_state_to_string(s
->state
);
2935 int socket_port_to_address(const SocketPort
*p
, char **ret
) {
2936 _cleanup_free_
char *address
= NULL
;
2943 case SOCKET_SOCKET
: {
2944 r
= socket_address_print(&p
->address
, &address
);
2951 case SOCKET_SPECIAL
:
2954 case SOCKET_USB_FUNCTION
:
2955 address
= strdup(p
->path
);
2961 assert_not_reached();
2964 *ret
= TAKE_PTR(address
);
2969 const char* socket_port_type_to_string(SocketPort
*p
) {
2976 switch (p
->address
.type
) {
2984 case SOCK_SEQPACKET
:
2985 return "SequentialPacket";
2988 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2996 case SOCKET_SPECIAL
:
3000 return "MessageQueue";
3005 case SOCKET_USB_FUNCTION
:
3006 return "USBFunction";
3013 SocketType
socket_port_type_from_string(const char *s
) {
3016 if (STR_IN_SET(s
, "Stream", "Datagram", "SequentialPacket", "Netlink"))
3017 return SOCKET_SOCKET
;
3018 else if (streq(s
, "Special"))
3019 return SOCKET_SPECIAL
;
3020 else if (streq(s
, "MessageQueue"))
3021 return SOCKET_MQUEUE
;
3022 else if (streq(s
, "FIFO"))
3024 else if (streq(s
, "USBFunction"))
3025 return SOCKET_USB_FUNCTION
;
3027 return _SOCKET_TYPE_INVALID
;
3030 static bool socket_may_gc(Unit
*u
) {
3031 Socket
*s
= ASSERT_PTR(SOCKET(u
));
3033 return s
->n_connections
== 0;
3036 static int socket_accept_do(Socket
*s
, int fd
) {
3042 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
3044 /* Convert transient network errors into clean and well-defined EAGAIN */
3045 return ERRNO_IS_ACCEPT_AGAIN(errno
) ? -EAGAIN
: -errno
;
3050 static int socket_accept_in_cgroup(Socket
*s
, SocketPort
*p
, int fd
) {
3051 _cleanup_(pidref_done
) PidRef pid
= PIDREF_NULL
;
3052 _cleanup_close_pair_
int pair
[2] = EBADF_PAIR
;
3059 /* Similar to socket_address_listen_in_cgroup(), but for accept() rather than socket(): make sure that any
3060 * connection socket is also properly associated with the cgroup. */
3062 if (!IN_SET(p
->address
.sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
3065 if (bpf_program_supported() <= 0)
3068 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
3069 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
3071 r
= unit_fork_helper_process(UNIT(s
), "(sd-accept)", /* into_cgroup= */ true, &pid
);
3073 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off accept stub process: %m");
3077 pair
[0] = safe_close(pair
[0]);
3079 cfd
= socket_accept_do(s
, fd
);
3080 if (cfd
== -EAGAIN
) /* spurious accept() */
3081 _exit(EXIT_SUCCESS
);
3083 log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
3084 _exit(EXIT_FAILURE
);
3087 r
= send_one_fd(pair
[1], cfd
, 0);
3089 log_unit_error_errno(UNIT(s
), r
, "Failed to send connection socket to parent: %m");
3090 _exit(EXIT_FAILURE
);
3093 _exit(EXIT_SUCCESS
);
3096 pair
[1] = safe_close(pair
[1]);
3097 cfd
= receive_one_fd(pair
[0], 0);
3099 /* We synchronously wait for the helper, as it shouldn't be slow */
3100 r
= wait_for_terminate_and_check("(sd-accept)", pid
.pid
, WAIT_LOG_ABNORMAL
);
3106 /* If we received no fd, we got EIO here. If this happens with a process exit code of EXIT_SUCCESS
3107 * this is a spurious accept(), let's convert that back to EAGAIN here. */
3111 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to receive connection socket: %m");
3116 cfd
= socket_accept_do(s
, fd
);
3117 if (cfd
== -EAGAIN
) /* spurious accept(), skip it silently */
3120 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
3125 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
3126 SocketPort
*p
= ASSERT_PTR(userdata
);
3131 if (p
->socket
->state
!= SOCKET_LISTENING
)
3134 log_unit_debug(UNIT(p
->socket
), "Incoming traffic");
3136 if (revents
!= EPOLLIN
) {
3137 if (revents
& EPOLLHUP
)
3138 log_unit_error(UNIT(p
->socket
), "Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.");
3140 log_unit_error(UNIT(p
->socket
), "Got unexpected poll event (0x%x) on socket.", revents
);
3144 if (p
->socket
->accept
&&
3145 p
->type
== SOCKET_SOCKET
&&
3146 socket_address_can_accept(&p
->address
)) {
3148 cfd
= socket_accept_in_cgroup(p
->socket
, p
, fd
);
3149 if (cfd
== -EAGAIN
) /* Spurious accept() */
3154 socket_apply_socket_options(p
->socket
, p
, cfd
);
3157 socket_enter_running(p
->socket
, cfd
);
3161 socket_enter_stop_pre(p
->socket
, SOCKET_FAILURE_RESOURCES
);
3165 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
3166 Socket
*s
= ASSERT_PTR(SOCKET(u
));
3171 if (pid
!= s
->control_pid
.pid
)
3174 pidref_done(&s
->control_pid
);
3176 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
3178 else if (code
== CLD_EXITED
)
3179 f
= SOCKET_FAILURE_EXIT_CODE
;
3180 else if (code
== CLD_KILLED
)
3181 f
= SOCKET_FAILURE_SIGNAL
;
3182 else if (code
== CLD_DUMPED
)
3183 f
= SOCKET_FAILURE_CORE_DUMP
;
3185 assert_not_reached();
3187 if (s
->control_command
) {
3188 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
3190 if (s
->control_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3194 unit_log_process_exit(
3197 socket_exec_command_to_string(s
->control_command_id
),
3198 f
== SOCKET_SUCCESS
,
3201 if (s
->result
== SOCKET_SUCCESS
)
3204 if (s
->control_command
&&
3205 s
->control_command
->command_next
&&
3206 f
== SOCKET_SUCCESS
) {
3208 log_unit_debug(u
, "Running next command for state %s", socket_state_to_string(s
->state
));
3211 s
->control_command
= NULL
;
3212 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
3214 /* No further commands for this step, so let's figure
3215 * out what to do next */
3217 log_unit_debug(u
, "Got final SIGCHLD for state %s", socket_state_to_string(s
->state
));
3221 case SOCKET_START_PRE
:
3222 if (f
== SOCKET_SUCCESS
)
3223 socket_enter_start_open(s
);
3225 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, f
);
3228 case SOCKET_START_CHOWN
:
3229 if (f
== SOCKET_SUCCESS
)
3230 socket_enter_start_post(s
);
3232 socket_enter_stop_pre(s
, f
);
3235 case SOCKET_START_POST
:
3236 if (f
== SOCKET_SUCCESS
)
3237 socket_enter_listening(s
);
3239 socket_enter_stop_pre(s
, f
);
3242 case SOCKET_STOP_PRE
:
3243 case SOCKET_STOP_PRE_SIGTERM
:
3244 case SOCKET_STOP_PRE_SIGKILL
:
3245 socket_enter_stop_post(s
, f
);
3248 case SOCKET_STOP_POST
:
3249 case SOCKET_FINAL_SIGTERM
:
3250 case SOCKET_FINAL_SIGKILL
:
3251 socket_enter_dead(s
, f
);
3254 case SOCKET_CLEANING
:
3256 if (s
->clean_result
== SOCKET_SUCCESS
)
3257 s
->clean_result
= f
;
3259 socket_enter_dead(s
, SOCKET_SUCCESS
);
3263 assert_not_reached();
3267 /* Notify clients about changed exit status */
3268 unit_add_to_dbus_queue(u
);
3271 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
3272 Socket
*s
= ASSERT_PTR(SOCKET(userdata
));
3274 assert(s
->timer_event_source
== source
);
3278 case SOCKET_START_PRE
:
3279 log_unit_warning(UNIT(s
), "Starting timed out. Terminating.");
3280 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3283 case SOCKET_START_CHOWN
:
3284 case SOCKET_START_POST
:
3285 log_unit_warning(UNIT(s
), "Starting timed out. Stopping.");
3286 socket_enter_stop_pre(s
, SOCKET_FAILURE_TIMEOUT
);
3289 case SOCKET_STOP_PRE
:
3290 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
3291 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3294 case SOCKET_STOP_PRE_SIGTERM
:
3295 if (s
->kill_context
.send_sigkill
) {
3296 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
3297 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3299 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL. Ignoring.");
3300 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3304 case SOCKET_STOP_PRE_SIGKILL
:
3305 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
3306 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3309 case SOCKET_STOP_POST
:
3310 log_unit_warning(UNIT(s
), "Stopping timed out (2). Terminating.");
3311 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3314 case SOCKET_FINAL_SIGTERM
:
3315 if (s
->kill_context
.send_sigkill
) {
3316 log_unit_warning(UNIT(s
), "Stopping timed out (2). Killing.");
3317 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3319 log_unit_warning(UNIT(s
), "Stopping timed out (2). Skipping SIGKILL. Ignoring.");
3320 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3324 case SOCKET_FINAL_SIGKILL
:
3325 log_unit_warning(UNIT(s
), "Still around after SIGKILL (2). Entering failed mode.");
3326 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3329 case SOCKET_CLEANING
:
3330 log_unit_warning(UNIT(s
), "Cleaning timed out. killing.");
3332 if (s
->clean_result
== SOCKET_SUCCESS
)
3333 s
->clean_result
= SOCKET_FAILURE_TIMEOUT
;
3335 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, 0);
3339 assert_not_reached();
3345 int socket_collect_fds(Socket
*s
, int **ret
) {
3346 size_t n
= 0, k
= 0;
3351 /* Called from the service code for requesting our fds */
3353 LIST_FOREACH(port
, p
, s
->ports
) {
3356 n
+= p
->n_auxiliary_fds
;
3364 int *fds
= new(int, n
);
3368 LIST_FOREACH(port
, p
, s
->ports
) {
3371 FOREACH_ARRAY(i
, p
->auxiliary_fds
, p
->n_auxiliary_fds
)
3381 static void socket_reset_failed(Unit
*u
) {
3382 Socket
*s
= SOCKET(u
);
3386 if (s
->state
== SOCKET_FAILED
)
3387 socket_set_state(s
, SOCKET_DEAD
);
3389 s
->result
= SOCKET_SUCCESS
;
3390 s
->clean_result
= SOCKET_SUCCESS
;
3393 void socket_connection_unref(Socket
*s
) {
3396 /* The service is dead. Yay!
3398 * This is strictly for one-instance-per-connection
3401 assert(s
->n_connections
> 0);
3404 log_unit_debug(UNIT(s
), "One connection closed, %u left.", s
->n_connections
);
3407 static void socket_trigger_notify(Unit
*u
, Unit
*other
) {
3408 Socket
*s
= ASSERT_PTR(SOCKET(u
));
3412 /* Filter out invocations with bogus state */
3413 assert(UNIT_IS_LOAD_COMPLETE(other
->load_state
));
3415 Service
*service
= ASSERT_PTR(SERVICE(other
));
3417 /* Don't propagate state changes from the service if we are already down */
3418 if (!IN_SET(s
->state
, SOCKET_RUNNING
, SOCKET_LISTENING
))
3421 /* We don't care for the service state if we are in Accept=yes mode */
3425 /* Propagate start limit hit state */
3426 if (other
->start_limit_hit
) {
3427 socket_enter_stop_pre(s
, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
);
3431 /* Don't propagate anything if there's still a job queued */
3435 if (!SOCKET_SERVICE_IS_ACTIVE(service
, /* allow_finalize = */ true))
3436 socket_enter_listening(s
);
3438 if (SERVICE(other
)->state
== SERVICE_RUNNING
)
3439 socket_set_state(s
, SOCKET_RUNNING
);
3442 static void socket_handoff_timestamp(
3444 const struct ucred
*ucred
,
3445 const dual_timestamp
*ts
) {
3447 Socket
*s
= ASSERT_PTR(SOCKET(u
));
3452 if (s
->control_pid
.pid
== ucred
->pid
&& s
->control_command
) {
3453 exec_status_handoff(&s
->control_command
->exec_status
, ucred
, ts
);
3454 unit_add_to_dbus_queue(u
);
3458 static int socket_get_timeout(Unit
*u
, usec_t
*timeout
) {
3459 Socket
*s
= ASSERT_PTR(SOCKET(u
));
3463 if (!s
->timer_event_source
)
3466 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3469 if (t
== USEC_INFINITY
)
3476 const char* socket_fdname(Socket
*s
) {
3479 /* Returns the name to use for $LISTEN_FDNAMES. If the user didn't specify anything specifically,
3480 * use the socket unit's name as fallback for Accept=no sockets, "connection" otherwise. */
3486 return "connection";
3491 static PidRef
* socket_control_pid(Unit
*u
) {
3492 return &ASSERT_PTR(SOCKET(u
))->control_pid
;
3495 static int socket_clean(Unit
*u
, ExecCleanMask mask
) {
3496 Socket
*s
= ASSERT_PTR(SOCKET(u
));
3497 _cleanup_strv_free_
char **l
= NULL
;
3502 if (s
->state
!= SOCKET_DEAD
)
3505 r
= exec_context_get_clean_directories(&s
->exec_context
, u
->manager
->prefix
, mask
, &l
);
3509 if (strv_isempty(l
))
3512 socket_unwatch_control_pid(s
);
3513 s
->clean_result
= SOCKET_SUCCESS
;
3514 s
->control_command
= NULL
;
3515 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
3517 r
= socket_arm_timer(s
, /* relative= */ true, s
->exec_context
.timeout_clean_usec
);
3519 log_unit_warning_errno(u
, r
, "Failed to install timer: %m");
3523 r
= unit_fork_and_watch_rm_rf(u
, l
, &s
->control_pid
);
3525 log_unit_warning_errno(u
, r
, "Failed to spawn cleaning task: %m");
3529 socket_set_state(s
, SOCKET_CLEANING
);
3533 s
->clean_result
= SOCKET_FAILURE_RESOURCES
;
3534 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
3538 static int socket_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
3539 Socket
*s
= ASSERT_PTR(SOCKET(u
));
3541 return exec_context_get_clean_mask(&s
->exec_context
, ret
);
3544 static int socket_can_start(Unit
*u
) {
3545 Socket
*s
= ASSERT_PTR(SOCKET(u
));
3548 r
= unit_test_start_limit(u
);
3550 socket_enter_dead(s
, SOCKET_FAILURE_START_LIMIT_HIT
);
3557 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
3558 [SOCKET_EXEC_START_PRE
] = "ExecStartPre",
3559 [SOCKET_EXEC_START_CHOWN
] = "ExecStartChown",
3560 [SOCKET_EXEC_START_POST
] = "ExecStartPost",
3561 [SOCKET_EXEC_STOP_PRE
] = "ExecStopPre",
3562 [SOCKET_EXEC_STOP_POST
] = "ExecStopPost",
3565 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
3567 static const char* const socket_result_table
[_SOCKET_RESULT_MAX
] = {
3568 [SOCKET_SUCCESS
] = "success",
3569 [SOCKET_FAILURE_RESOURCES
] = "resources",
3570 [SOCKET_FAILURE_TIMEOUT
] = "timeout",
3571 [SOCKET_FAILURE_EXIT_CODE
] = "exit-code",
3572 [SOCKET_FAILURE_SIGNAL
] = "signal",
3573 [SOCKET_FAILURE_CORE_DUMP
] = "core-dump",
3574 [SOCKET_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
3575 [SOCKET_FAILURE_TRIGGER_LIMIT_HIT
] = "trigger-limit-hit",
3576 [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
] = "service-start-limit-hit",
3579 DEFINE_STRING_TABLE_LOOKUP(socket_result
, SocketResult
);
3581 static const char* const socket_timestamping_table
[_SOCKET_TIMESTAMPING_MAX
] = {
3582 [SOCKET_TIMESTAMPING_OFF
] = "off",
3583 [SOCKET_TIMESTAMPING_US
] = "us",
3584 [SOCKET_TIMESTAMPING_NS
] = "ns",
3587 DEFINE_STRING_TABLE_LOOKUP(socket_timestamping
, SocketTimestamping
);
3589 SocketTimestamping
socket_timestamping_from_string_harder(const char *p
) {
3590 SocketTimestamping t
;
3594 return _SOCKET_TIMESTAMPING_INVALID
;
3596 t
= socket_timestamping_from_string(p
);
3600 /* Let's alternatively support the various other aliases parse_time() accepts for ns and µs here,
3602 if (streq(p
, "nsec"))
3603 return SOCKET_TIMESTAMPING_NS
;
3604 if (STR_IN_SET(p
, "usec", "µs", "μs")) /* Accept both small greek letter mu + micro sign unicode codepoints */
3605 return SOCKET_TIMESTAMPING_US
;
3607 r
= parse_boolean(p
);
3609 return _SOCKET_TIMESTAMPING_INVALID
;
3611 return r
? SOCKET_TIMESTAMPING_NS
: SOCKET_TIMESTAMPING_OFF
; /* If boolean yes, default to ns accuracy */
3614 const UnitVTable socket_vtable
= {
3615 .object_size
= sizeof(Socket
),
3616 .exec_context_offset
= offsetof(Socket
, exec_context
),
3617 .cgroup_context_offset
= offsetof(Socket
, cgroup_context
),
3618 .kill_context_offset
= offsetof(Socket
, kill_context
),
3619 .exec_runtime_offset
= offsetof(Socket
, exec_runtime
),
3620 .cgroup_runtime_offset
= offsetof(Socket
, cgroup_runtime
),
3626 .private_section
= "Socket",
3628 .can_transient
= true,
3629 .can_trigger
= true,
3632 .init
= socket_init
,
3633 .done
= socket_done
,
3634 .load
= socket_load
,
3636 .coldplug
= socket_coldplug
,
3638 .dump
= socket_dump
,
3640 .start
= socket_start
,
3641 .stop
= socket_stop
,
3643 .clean
= socket_clean
,
3644 .can_clean
= socket_can_clean
,
3646 .get_timeout
= socket_get_timeout
,
3648 .serialize
= socket_serialize
,
3649 .deserialize_item
= socket_deserialize_item
,
3650 .distribute_fds
= socket_distribute_fds
,
3652 .active_state
= socket_active_state
,
3653 .sub_state_to_string
= socket_sub_state_to_string
,
3655 .will_restart
= unit_will_restart_default
,
3657 .may_gc
= socket_may_gc
,
3659 .sigchld_event
= socket_sigchld_event
,
3661 .trigger_notify
= socket_trigger_notify
,
3663 .reset_failed
= socket_reset_failed
,
3665 .notify_handoff_timestamp
= socket_handoff_timestamp
,
3667 .control_pid
= socket_control_pid
,
3669 .bus_set_property
= bus_socket_set_property
,
3670 .bus_commit_properties
= bus_socket_commit_properties
,
3672 .status_message_formats
= {
3673 .finished_start_job
= {
3674 [JOB_DONE
] = "Listening on %s.",
3675 [JOB_FAILED
] = "Failed to listen on %s.",
3676 [JOB_TIMEOUT
] = "Timed out starting %s.",
3678 .finished_stop_job
= {
3679 [JOB_DONE
] = "Closed %s.",
3680 [JOB_FAILED
] = "Failed stopping %s.",
3681 [JOB_TIMEOUT
] = "Timed out stopping %s.",
3685 .can_start
= socket_can_start
,