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
73 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
74 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
76 static void socket_init(Unit
*u
) {
77 Socket
*s
= SOCKET(u
);
80 assert(u
->load_state
== UNIT_STUB
);
82 s
->backlog
= SOMAXCONN
;
83 s
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
84 s
->directory_mode
= 0755;
85 s
->socket_mode
= 0666;
87 s
->max_connections
= 64;
94 s
->exec_context
.std_output
= u
->manager
->default_std_output
;
95 s
->exec_context
.std_error
= u
->manager
->default_std_error
;
97 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
99 s
->trigger_limit
.interval
= USEC_INFINITY
;
100 s
->trigger_limit
.burst
= (unsigned) -1;
103 static void socket_unwatch_control_pid(Socket
*s
) {
106 if (s
->control_pid
<= 0)
109 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
113 static void socket_cleanup_fd_list(SocketPort
*p
) {
116 close_many(p
->auxiliary_fds
, p
->n_auxiliary_fds
);
117 p
->auxiliary_fds
= mfree(p
->auxiliary_fds
);
118 p
->n_auxiliary_fds
= 0;
121 void socket_free_ports(Socket
*s
) {
126 while ((p
= s
->ports
)) {
127 LIST_REMOVE(port
, s
->ports
, p
);
129 sd_event_source_unref(p
->event_source
);
131 socket_cleanup_fd_list(p
);
138 static void socket_done(Unit
*u
) {
139 Socket
*s
= SOCKET(u
);
144 socket_free_ports(s
);
146 while ((p
= set_steal_first(s
->peers_by_address
)))
149 s
->peers_by_address
= set_free(s
->peers_by_address
);
151 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
, false);
152 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
153 s
->control_command
= NULL
;
155 dynamic_creds_unref(&s
->dynamic_creds
);
157 socket_unwatch_control_pid(s
);
159 unit_ref_unset(&s
->service
);
161 s
->tcp_congestion
= mfree(s
->tcp_congestion
);
162 s
->bind_to_device
= mfree(s
->bind_to_device
);
164 s
->smack
= mfree(s
->smack
);
165 s
->smack_ip_in
= mfree(s
->smack_ip_in
);
166 s
->smack_ip_out
= mfree(s
->smack_ip_out
);
168 strv_free(s
->symlinks
);
170 s
->user
= mfree(s
->user
);
171 s
->group
= mfree(s
->group
);
173 s
->fdname
= mfree(s
->fdname
);
175 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
178 static int socket_arm_timer(Socket
*s
, usec_t usec
) {
183 if (s
->timer_event_source
) {
184 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
188 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
191 if (usec
== USEC_INFINITY
)
194 r
= sd_event_add_time(
195 UNIT(s
)->manager
->event
,
196 &s
->timer_event_source
,
199 socket_dispatch_timer
, s
);
203 (void) sd_event_source_set_description(s
->timer_event_source
, "socket-timer");
208 int socket_instantiate_service(Socket
*s
) {
209 _cleanup_free_
char *prefix
= NULL
, *name
= NULL
;
215 /* This fills in s->service if it isn't filled in yet. For
216 * Accept=yes sockets we create the next connection service
217 * here. For Accept=no this is mostly a NOP since the service
218 * is figured out at load time anyway. */
220 if (UNIT_DEREF(s
->service
))
226 r
= unit_name_to_prefix(UNIT(s
)->id
, &prefix
);
230 if (asprintf(&name
, "%s@%u.service", prefix
, s
->n_accepted
) < 0)
233 r
= manager_load_unit(UNIT(s
)->manager
, name
, NULL
, NULL
, &u
);
237 unit_ref_set(&s
->service
, UNIT(s
), u
);
239 return unit_add_two_dependencies(UNIT(s
), UNIT_BEFORE
, UNIT_TRIGGERS
, u
, false, UNIT_DEPENDENCY_IMPLICIT
);
242 static bool have_non_accept_socket(Socket
*s
) {
250 LIST_FOREACH(port
, p
, s
->ports
) {
252 if (p
->type
!= SOCKET_SOCKET
)
255 if (!socket_address_can_accept(&p
->address
))
262 static int socket_add_mount_dependencies(Socket
*s
) {
268 LIST_FOREACH(port
, p
, s
->ports
) {
269 const char *path
= NULL
;
271 if (p
->type
== SOCKET_SOCKET
)
272 path
= socket_address_get_path(&p
->address
);
273 else if (IN_SET(p
->type
, SOCKET_FIFO
, SOCKET_SPECIAL
, SOCKET_USB_FUNCTION
))
279 r
= unit_require_mounts_for(UNIT(s
), path
, UNIT_DEPENDENCY_FILE
);
287 static int socket_add_device_dependencies(Socket
*s
) {
292 if (!s
->bind_to_device
|| streq(s
->bind_to_device
, "lo"))
295 t
= strjoina("/sys/subsystem/net/devices/", s
->bind_to_device
);
296 return unit_add_node_dependency(UNIT(s
), t
, false, UNIT_BINDS_TO
, UNIT_DEPENDENCY_FILE
);
299 static int socket_add_default_dependencies(Socket
*s
) {
303 if (!UNIT(s
)->default_dependencies
)
306 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_BEFORE
, SPECIAL_SOCKETS_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
310 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
311 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
316 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
319 _pure_
static bool socket_has_exec(Socket
*s
) {
323 for (i
= 0; i
< _SOCKET_EXEC_COMMAND_MAX
; i
++)
324 if (s
->exec_command
[i
])
330 static int socket_add_extras(Socket
*s
) {
336 /* Pick defaults for the trigger limit, if nothing was explicitly configured. We pick a relatively high limit
337 * in Accept=yes mode, and a lower limit for Accept=no. Reason: in Accept=yes mode we are invoking accept()
338 * ourselves before the trigger limit can hit, thus incoming connections are taken off the socket queue quickly
339 * and reliably. This is different for Accept=no, where the spawned service has to take the incoming traffic
340 * off the queues, which it might not necessarily do. Moreover, while Accept=no services are supposed to
341 * process whatever is queued in one go, and thus should normally never have to be started frequently. This is
342 * different for Accept=yes where each connection is processed by a new service instance, and thus frequent
343 * service starts are typical. */
345 if (s
->trigger_limit
.interval
== USEC_INFINITY
)
346 s
->trigger_limit
.interval
= 2 * USEC_PER_SEC
;
348 if (s
->trigger_limit
.burst
== (unsigned) -1) {
350 s
->trigger_limit
.burst
= 200;
352 s
->trigger_limit
.burst
= 20;
355 if (have_non_accept_socket(s
)) {
357 if (!UNIT_DEREF(s
->service
)) {
360 r
= unit_load_related_unit(u
, ".service", &x
);
364 unit_ref_set(&s
->service
, u
, x
);
367 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT_DEREF(s
->service
), true, UNIT_DEPENDENCY_IMPLICIT
);
372 r
= socket_add_mount_dependencies(s
);
376 r
= socket_add_device_dependencies(s
);
380 r
= unit_patch_contexts(u
);
384 if (socket_has_exec(s
)) {
385 r
= unit_add_exec_dependencies(u
, &s
->exec_context
);
390 r
= unit_set_default_slice(u
);
394 r
= socket_add_default_dependencies(s
);
401 static const char *socket_find_symlink_target(Socket
*s
) {
402 const char *found
= NULL
;
405 LIST_FOREACH(port
, p
, s
->ports
) {
406 const char *f
= NULL
;
415 f
= socket_address_get_path(&p
->address
);
433 static int socket_verify(Socket
*s
) {
436 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
440 log_unit_error(UNIT(s
), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing.");
444 if (s
->accept
&& have_non_accept_socket(s
)) {
445 log_unit_error(UNIT(s
), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing.");
449 if (s
->accept
&& s
->max_connections
<= 0) {
450 log_unit_error(UNIT(s
), "MaxConnection= setting too small. Refusing.");
454 if (s
->accept
&& UNIT_DEREF(s
->service
)) {
455 log_unit_error(UNIT(s
), "Explicit service configuration for accepting socket units not supported. Refusing.");
459 if (s
->exec_context
.pam_name
&& s
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
460 log_unit_error(UNIT(s
), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing.");
464 if (!strv_isempty(s
->symlinks
) && !socket_find_symlink_target(s
)) {
465 log_unit_error(UNIT(s
), "Unit has symlinks set but none or more than one node in the file system. Refusing.");
472 static void peer_address_hash_func(const SocketPeer
*s
, struct siphash
*state
) {
475 if (s
->peer
.sa
.sa_family
== AF_INET
)
476 siphash24_compress(&s
->peer
.in
.sin_addr
, sizeof(s
->peer
.in
.sin_addr
), state
);
477 else if (s
->peer
.sa
.sa_family
== AF_INET6
)
478 siphash24_compress(&s
->peer
.in6
.sin6_addr
, sizeof(s
->peer
.in6
.sin6_addr
), state
);
479 else if (s
->peer
.sa
.sa_family
== AF_VSOCK
)
480 siphash24_compress(&s
->peer
.vm
.svm_cid
, sizeof(s
->peer
.vm
.svm_cid
), state
);
482 assert_not_reached("Unknown address family.");
485 static int peer_address_compare_func(const SocketPeer
*x
, const SocketPeer
*y
) {
488 r
= CMP(x
->peer
.sa
.sa_family
, y
->peer
.sa
.sa_family
);
492 switch(x
->peer
.sa
.sa_family
) {
494 return memcmp(&x
->peer
.in
.sin_addr
, &y
->peer
.in
.sin_addr
, sizeof(x
->peer
.in
.sin_addr
));
496 return memcmp(&x
->peer
.in6
.sin6_addr
, &y
->peer
.in6
.sin6_addr
, sizeof(x
->peer
.in6
.sin6_addr
));
498 return CMP(x
->peer
.vm
.svm_cid
, y
->peer
.vm
.svm_cid
);
500 assert_not_reached("Black sheep in the family!");
503 DEFINE_PRIVATE_HASH_OPS(peer_address_hash_ops
, SocketPeer
, peer_address_hash_func
, peer_address_compare_func
);
505 static int socket_load(Unit
*u
) {
506 Socket
*s
= SOCKET(u
);
510 assert(u
->load_state
== UNIT_STUB
);
512 r
= set_ensure_allocated(&s
->peers_by_address
, &peer_address_hash_ops
);
516 r
= unit_load_fragment_and_dropin(u
);
520 if (u
->load_state
== UNIT_LOADED
) {
521 /* This is a new unit? Then let's add in some extras */
522 r
= socket_add_extras(s
);
527 return socket_verify(s
);
530 static SocketPeer
*socket_peer_new(void) {
533 p
= new0(SocketPeer
, 1);
542 static SocketPeer
*socket_peer_free(SocketPeer
*p
) {
546 set_remove(p
->socket
->peers_by_address
, p
);
551 DEFINE_TRIVIAL_REF_UNREF_FUNC(SocketPeer
, socket_peer
, socket_peer_free
);
553 int socket_acquire_peer(Socket
*s
, int fd
, SocketPeer
**p
) {
554 _cleanup_(socket_peer_unrefp
) SocketPeer
*remote
= NULL
;
555 SocketPeer sa
= {}, *i
;
556 socklen_t salen
= sizeof(sa
.peer
);
562 r
= getpeername(fd
, &sa
.peer
.sa
, &salen
);
564 return log_unit_error_errno(UNIT(s
), errno
, "getpeername failed: %m");
566 if (!IN_SET(sa
.peer
.sa
.sa_family
, AF_INET
, AF_INET6
, AF_VSOCK
)) {
571 i
= set_get(s
->peers_by_address
, &sa
);
573 *p
= socket_peer_ref(i
);
577 remote
= socket_peer_new();
581 remote
->peer
= sa
.peer
;
582 remote
->peer_salen
= salen
;
584 r
= set_put(s
->peers_by_address
, remote
);
590 *p
= TAKE_PTR(remote
);
595 _const_
static const char* listen_lookup(int family
, int type
) {
597 if (family
== AF_NETLINK
)
598 return "ListenNetlink";
600 if (type
== SOCK_STREAM
)
601 return "ListenStream";
602 else if (type
== SOCK_DGRAM
)
603 return "ListenDatagram";
604 else if (type
== SOCK_SEQPACKET
)
605 return "ListenSequentialPacket";
607 assert_not_reached("Unknown socket type");
611 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
612 char time_string
[FORMAT_TIMESPAN_MAX
];
614 Socket
*s
= SOCKET(u
);
616 const char *prefix2
, *str
;
621 prefix
= strempty(prefix
);
622 prefix2
= strjoina(prefix
, "\t");
625 "%sSocket State: %s\n"
627 "%sBindIPv6Only: %s\n"
629 "%sSocketMode: %04o\n"
630 "%sDirectoryMode: %04o\n"
634 "%sTransparent: %s\n"
636 "%sPassCredentials: %s\n"
637 "%sPassSecurity: %s\n"
638 "%sTCPCongestion: %s\n"
639 "%sRemoveOnStop: %s\n"
641 "%sFileDescriptorName: %s\n"
642 "%sSELinuxContextFromNet: %s\n",
643 prefix
, socket_state_to_string(s
->state
),
644 prefix
, socket_result_to_string(s
->result
),
645 prefix
, socket_address_bind_ipv6_only_to_string(s
->bind_ipv6_only
),
647 prefix
, s
->socket_mode
,
648 prefix
, s
->directory_mode
,
649 prefix
, yes_no(s
->keep_alive
),
650 prefix
, yes_no(s
->no_delay
),
651 prefix
, yes_no(s
->free_bind
),
652 prefix
, yes_no(s
->transparent
),
653 prefix
, yes_no(s
->broadcast
),
654 prefix
, yes_no(s
->pass_cred
),
655 prefix
, yes_no(s
->pass_sec
),
656 prefix
, strna(s
->tcp_congestion
),
657 prefix
, yes_no(s
->remove_on_stop
),
658 prefix
, yes_no(s
->writable
),
659 prefix
, socket_fdname(s
),
660 prefix
, yes_no(s
->selinux_context_from_net
));
662 if (s
->control_pid
> 0)
664 "%sControl PID: "PID_FMT
"\n",
665 prefix
, s
->control_pid
);
667 if (s
->bind_to_device
)
669 "%sBindToDevice: %s\n",
670 prefix
, s
->bind_to_device
);
675 "%sNConnections: %u\n"
676 "%sMaxConnections: %u\n"
677 "%sMaxConnectionsPerSource: %u\n",
678 prefix
, s
->n_accepted
,
679 prefix
, s
->n_connections
,
680 prefix
, s
->max_connections
,
681 prefix
, s
->max_connections_per_source
);
683 if (s
->priority
>= 0)
686 prefix
, s
->priority
);
688 if (s
->receive_buffer
> 0)
690 "%sReceiveBuffer: %zu\n",
691 prefix
, s
->receive_buffer
);
693 if (s
->send_buffer
> 0)
695 "%sSendBuffer: %zu\n",
696 prefix
, s
->send_buffer
);
708 if (s
->pipe_size
> 0)
711 prefix
, s
->pipe_size
);
718 if (s
->mq_maxmsg
> 0)
720 "%sMessageQueueMaxMessages: %li\n",
721 prefix
, s
->mq_maxmsg
);
723 if (s
->mq_msgsize
> 0)
725 "%sMessageQueueMessageSize: %li\n",
726 prefix
, s
->mq_msgsize
);
731 prefix
, yes_no(s
->reuse_port
));
735 "%sSmackLabel: %s\n",
740 "%sSmackLabelIPIn: %s\n",
741 prefix
, s
->smack_ip_in
);
745 "%sSmackLabelIPOut: %s\n",
746 prefix
, s
->smack_ip_out
);
748 if (!isempty(s
->user
) || !isempty(s
->group
))
751 "%sSocketGroup: %s\n",
752 prefix
, strna(s
->user
),
753 prefix
, strna(s
->group
));
755 if (s
->keep_alive_time
> 0)
757 "%sKeepAliveTimeSec: %s\n",
758 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->keep_alive_time
, USEC_PER_SEC
));
760 if (s
->keep_alive_interval
> 0)
762 "%sKeepAliveIntervalSec: %s\n",
763 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->keep_alive_interval
, USEC_PER_SEC
));
765 if (s
->keep_alive_cnt
> 0)
767 "%sKeepAliveProbes: %u\n",
768 prefix
, s
->keep_alive_cnt
);
770 if (s
->defer_accept
> 0)
772 "%sDeferAcceptSec: %s\n",
773 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->defer_accept
, USEC_PER_SEC
));
775 LIST_FOREACH(port
, p
, s
->ports
) {
778 case SOCKET_SOCKET
: {
779 _cleanup_free_
char *k
= NULL
;
783 r
= socket_address_print(&p
->address
, &k
);
789 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(socket_address_family(&p
->address
), p
->address
.type
), t
);
793 fprintf(f
, "%sListenSpecial: %s\n", prefix
, p
->path
);
795 case SOCKET_USB_FUNCTION
:
796 fprintf(f
, "%sListenUSBFunction: %s\n", prefix
, p
->path
);
799 fprintf(f
, "%sListenMessageQueue: %s\n", prefix
, p
->path
);
802 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
807 "%sTriggerLimitIntervalSec: %s\n"
808 "%sTriggerLimitBurst: %u\n",
809 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->trigger_limit
.interval
, USEC_PER_SEC
),
810 prefix
, s
->trigger_limit
.burst
);
812 str
= ip_protocol_to_name(s
->socket_protocol
);
814 fprintf(f
, "%sSocketProtocol: %s\n", prefix
, str
);
816 if (!strv_isempty(s
->symlinks
)) {
819 fprintf(f
, "%sSymlinks:", prefix
);
820 STRV_FOREACH(q
, s
->symlinks
)
821 fprintf(f
, " %s", *q
);
827 "%sTimeoutSec: %s\n",
828 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->timeout_usec
, USEC_PER_SEC
));
830 exec_context_dump(&s
->exec_context
, f
, prefix
);
831 kill_context_dump(&s
->kill_context
, f
, prefix
);
833 for (c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
834 if (!s
->exec_command
[c
])
837 fprintf(f
, "%s-> %s:\n",
838 prefix
, socket_exec_command_to_string(c
));
840 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
843 cgroup_context_dump(&s
->cgroup_context
, f
, prefix
);
846 static int instance_from_socket(int fd
, unsigned nr
, char **instance
) {
849 union sockaddr_union local
, remote
;
855 if (getsockname(fd
, &local
.sa
, &l
) < 0)
859 if (getpeername(fd
, &remote
.sa
, &l
) < 0)
862 switch (local
.sa
.sa_family
) {
866 a
= be32toh(local
.in
.sin_addr
.s_addr
),
867 b
= be32toh(remote
.in
.sin_addr
.s_addr
);
870 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
872 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
873 be16toh(local
.in
.sin_port
),
874 b
>> 24, (b
>> 16) & 0xFF, (b
>> 8) & 0xFF, b
& 0xFF,
875 be16toh(remote
.in
.sin_port
)) < 0)
882 static const unsigned char ipv4_prefix
[] = {
883 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
886 if (memcmp(&local
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0 &&
887 memcmp(&remote
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
889 *a
= local
.in6
.sin6_addr
.s6_addr
+12,
890 *b
= remote
.in6
.sin6_addr
.s6_addr
+12;
893 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
895 a
[0], a
[1], a
[2], a
[3],
896 be16toh(local
.in6
.sin6_port
),
897 b
[0], b
[1], b
[2], b
[3],
898 be16toh(remote
.in6
.sin6_port
)) < 0)
901 char a
[INET6_ADDRSTRLEN
], b
[INET6_ADDRSTRLEN
];
906 inet_ntop(AF_INET6
, &local
.in6
.sin6_addr
, a
, sizeof(a
)),
907 be16toh(local
.in6
.sin6_port
),
908 inet_ntop(AF_INET6
, &remote
.in6
.sin6_addr
, b
, sizeof(b
)),
909 be16toh(remote
.in6
.sin6_port
)) < 0)
920 k
= getpeercred(fd
, &ucred
);
923 "%u-"PID_FMT
"-"UID_FMT
,
924 nr
, ucred
.pid
, ucred
.uid
) < 0)
926 } else if (k
== -ENODATA
) {
927 /* This handles the case where somebody is
928 * connecting from another pid/uid namespace
929 * (e.g. from outside of our container). */
944 local
.vm
.svm_cid
, local
.vm
.svm_port
,
945 remote
.vm
.svm_cid
, remote
.vm
.svm_port
) < 0)
951 assert_not_reached("Unhandled socket type.");
958 static void socket_close_fds(Socket
*s
) {
964 LIST_FOREACH(port
, p
, s
->ports
) {
967 was_open
= p
->fd
>= 0;
969 p
->event_source
= sd_event_source_unref(p
->event_source
);
970 p
->fd
= safe_close(p
->fd
);
971 socket_cleanup_fd_list(p
);
973 /* One little note: we should normally not delete any sockets in the file system here! After all some
974 * other process we spawned might still have a reference of this fd and wants to continue to use
975 * it. Therefore we normally delete sockets in the file system before we create a new one, not after we
976 * stopped using one! That all said, if the user explicitly requested this, we'll delete them here
977 * anyway, but only then. */
979 if (!was_open
|| !s
->remove_on_stop
)
985 (void) unlink(p
->path
);
989 (void) mq_unlink(p
->path
);
993 (void) socket_address_unlink(&p
->address
);
1001 if (s
->remove_on_stop
)
1002 STRV_FOREACH(i
, s
->symlinks
)
1006 static void socket_apply_socket_options(Socket
*s
, int fd
) {
1012 if (s
->keep_alive
) {
1013 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_KEEPALIVE
, true);
1015 log_unit_warning_errno(UNIT(s
), r
, "SO_KEEPALIVE failed: %m");
1018 if (s
->keep_alive_time
> 0) {
1019 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPIDLE
, s
->keep_alive_time
/ USEC_PER_SEC
);
1021 log_unit_warning_errno(UNIT(s
), r
, "TCP_KEEPIDLE failed: %m");
1024 if (s
->keep_alive_interval
> 0) {
1025 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPINTVL
, s
->keep_alive_interval
/ USEC_PER_SEC
);
1027 log_unit_warning_errno(UNIT(s
), r
, "TCP_KEEPINTVL failed: %m");
1030 if (s
->keep_alive_cnt
> 0) {
1031 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPCNT
, s
->keep_alive_cnt
);
1033 log_unit_warning_errno(UNIT(s
), r
, "TCP_KEEPCNT failed: %m");
1036 if (s
->defer_accept
> 0) {
1037 r
= setsockopt_int(fd
, SOL_TCP
, TCP_DEFER_ACCEPT
, s
->defer_accept
/ USEC_PER_SEC
);
1039 log_unit_warning_errno(UNIT(s
), r
, "TCP_DEFER_ACCEPT failed: %m");
1043 if (s
->socket_protocol
== IPPROTO_SCTP
) {
1044 r
= setsockopt_int(fd
, SOL_SCTP
, SCTP_NODELAY
, true);
1046 log_unit_warning_errno(UNIT(s
), r
, "SCTP_NODELAY failed: %m");
1048 r
= setsockopt_int(fd
, SOL_TCP
, TCP_NODELAY
, true);
1050 log_unit_warning_errno(UNIT(s
), r
, "TCP_NODELAY failed: %m");
1055 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_BROADCAST
, true);
1057 log_unit_warning_errno(UNIT(s
), r
, "SO_BROADCAST failed: %m");
1061 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSCRED
, true);
1063 log_unit_warning_errno(UNIT(s
), r
, "SO_PASSCRED failed: %m");
1067 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSSEC
, true);
1069 log_unit_warning_errno(UNIT(s
), r
, "SO_PASSSEC failed: %m");
1072 if (s
->priority
>= 0) {
1073 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PRIORITY
, s
->priority
);
1075 log_unit_warning_errno(UNIT(s
), r
, "SO_PRIORITY failed: %m");
1078 if (s
->receive_buffer
> 0) {
1079 /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
1080 if (setsockopt_int(fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, s
->receive_buffer
) < 0) {
1081 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_RCVBUF
, s
->receive_buffer
);
1083 log_unit_warning_errno(UNIT(s
), r
, "SO_RCVBUF failed: %m");
1087 if (s
->send_buffer
> 0) {
1088 if (setsockopt_int(fd
, SOL_SOCKET
, SO_SNDBUFFORCE
, s
->send_buffer
) < 0) {
1089 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_SNDBUF
, s
->send_buffer
);
1091 log_unit_warning_errno(UNIT(s
), r
, "SO_SNDBUF failed: %m");
1096 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_MARK
, s
->mark
);
1098 log_unit_warning_errno(UNIT(s
), r
, "SO_MARK failed: %m");
1101 if (s
->ip_tos
>= 0) {
1102 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_TOS
, s
->ip_tos
);
1104 log_unit_warning_errno(UNIT(s
), r
, "IP_TOS failed: %m");
1107 if (s
->ip_ttl
>= 0) {
1110 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_TTL
, s
->ip_ttl
);
1112 if (socket_ipv6_is_supported())
1113 x
= setsockopt_int(fd
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, s
->ip_ttl
);
1118 log_unit_warning_errno(UNIT(s
), r
, "IP_TTL/IPV6_UNICAST_HOPS failed: %m");
1121 if (s
->tcp_congestion
)
1122 if (setsockopt(fd
, SOL_TCP
, TCP_CONGESTION
, s
->tcp_congestion
, strlen(s
->tcp_congestion
)+1) < 0)
1123 log_unit_warning_errno(UNIT(s
), errno
, "TCP_CONGESTION failed: %m");
1125 if (s
->smack_ip_in
) {
1126 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_IPIN
, s
->smack_ip_in
);
1128 log_unit_error_errno(UNIT(s
), r
, "mac_smack_apply_ip_in_fd: %m");
1131 if (s
->smack_ip_out
) {
1132 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_IPOUT
, s
->smack_ip_out
);
1134 log_unit_error_errno(UNIT(s
), r
, "mac_smack_apply_ip_out_fd: %m");
1138 static void socket_apply_fifo_options(Socket
*s
, int fd
) {
1144 if (s
->pipe_size
> 0)
1145 if (fcntl(fd
, F_SETPIPE_SZ
, s
->pipe_size
) < 0)
1146 log_unit_warning_errno(UNIT(s
), errno
, "Setting pipe size failed, ignoring: %m");
1149 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_ACCESS
, s
->smack
);
1151 log_unit_error_errno(UNIT(s
), r
, "SMACK relabelling failed, ignoring: %m");
1155 static int fifo_address_create(
1157 mode_t directory_mode
,
1158 mode_t socket_mode
) {
1160 _cleanup_close_
int fd
= -1;
1167 (void) mkdir_parents_label(path
, directory_mode
);
1169 r
= mac_selinux_create_file_prepare(path
, S_IFIFO
);
1173 /* Enforce the right access mode for the fifo */
1174 old_mask
= umask(~socket_mode
);
1176 /* Include the original umask in our mask */
1177 (void) umask(~socket_mode
| old_mask
);
1179 r
= mkfifo(path
, socket_mode
);
1180 (void) umask(old_mask
);
1182 if (r
< 0 && errno
!= EEXIST
) {
1187 fd
= open(path
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
| O_NOFOLLOW
);
1193 mac_selinux_create_file_clear();
1195 if (fstat(fd
, &st
) < 0) {
1200 if (!S_ISFIFO(st
.st_mode
) ||
1201 (st
.st_mode
& 0777) != (socket_mode
& ~old_mask
) ||
1202 st
.st_uid
!= getuid() ||
1203 st
.st_gid
!= getgid()) {
1211 mac_selinux_create_file_clear();
1215 static int special_address_create(const char *path
, bool writable
) {
1216 _cleanup_close_
int fd
= -1;
1221 fd
= open(path
, (writable
? O_RDWR
: O_RDONLY
)|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
);
1225 if (fstat(fd
, &st
) < 0)
1228 /* Check whether this is a /proc, /sys or /dev file or char device */
1229 if (!S_ISREG(st
.st_mode
) && !S_ISCHR(st
.st_mode
))
1235 static int usbffs_address_create(const char *path
) {
1236 _cleanup_close_
int fd
= -1;
1241 fd
= open(path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
);
1245 if (fstat(fd
, &st
) < 0)
1248 /* Check whether this is a regular file (ffs endpoint) */
1249 if (!S_ISREG(st
.st_mode
))
1255 static int mq_address_create(
1261 _cleanup_close_
int fd
= -1;
1264 struct mq_attr _attr
, *attr
= NULL
;
1268 if (maxmsg
> 0 && msgsize
> 0) {
1269 _attr
= (struct mq_attr
) {
1270 .mq_flags
= O_NONBLOCK
,
1271 .mq_maxmsg
= maxmsg
,
1272 .mq_msgsize
= msgsize
,
1277 /* Enforce the right access mode for the mq */
1278 old_mask
= umask(~mq_mode
);
1280 /* Include the original umask in our mask */
1281 (void) umask(~mq_mode
| old_mask
);
1282 fd
= mq_open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_CREAT
, mq_mode
, attr
);
1283 (void) umask(old_mask
);
1288 if (fstat(fd
, &st
) < 0)
1291 if ((st
.st_mode
& 0777) != (mq_mode
& ~old_mask
) ||
1292 st
.st_uid
!= getuid() ||
1293 st
.st_gid
!= getgid())
1299 static int socket_symlink(Socket
*s
) {
1306 p
= socket_find_symlink_target(s
);
1310 STRV_FOREACH(i
, s
->symlinks
) {
1311 (void) mkdir_parents_label(*i
, s
->directory_mode
);
1313 r
= symlink_idempotent(p
, *i
, false);
1315 if (r
== -EEXIST
&& s
->remove_on_stop
) {
1316 /* If there's already something where we want to create the symlink, and the destructive
1317 * RemoveOnStop= mode is set, then we might as well try to remove what already exists and try
1320 if (unlink(*i
) >= 0)
1321 r
= symlink_idempotent(p
, *i
, false);
1325 log_unit_warning_errno(UNIT(s
), r
, "Failed to create symlink %s → %s, ignoring: %m", p
, *i
);
1331 static int usbffs_write_descs(int fd
, Service
*s
) {
1334 if (!s
->usb_function_descriptors
|| !s
->usb_function_strings
)
1337 r
= copy_file_fd(s
->usb_function_descriptors
, fd
, 0);
1341 return copy_file_fd(s
->usb_function_strings
, fd
, 0);
1344 static int usbffs_select_ep(const struct dirent
*d
) {
1345 return d
->d_name
[0] != '.' && !streq(d
->d_name
, "ep0");
1348 static int usbffs_dispatch_eps(SocketPort
*p
) {
1349 _cleanup_free_
struct dirent
**ent
= NULL
;
1353 r
= scandir(p
->path
, &ent
, usbffs_select_ep
, alphasort
);
1358 p
->auxiliary_fds
= new(int, n
);
1359 if (!p
->auxiliary_fds
) {
1364 p
->n_auxiliary_fds
= n
;
1367 for (i
= 0; i
< n
; ++i
) {
1368 _cleanup_free_
char *ep
= NULL
;
1370 ep
= path_make_absolute(ent
[i
]->d_name
, p
->path
);
1376 path_simplify(ep
, false);
1378 r
= usbffs_address_create(ep
);
1382 p
->auxiliary_fds
[k
++] = r
;
1389 close_many(p
->auxiliary_fds
, k
);
1390 p
->auxiliary_fds
= mfree(p
->auxiliary_fds
);
1391 p
->n_auxiliary_fds
= 0;
1394 for (i
= 0; i
< n
; ++i
)
1400 static int socket_determine_selinux_label(Socket
*s
, char **ret
) {
1403 _cleanup_free_
char *path
= NULL
;
1409 if (s
->selinux_context_from_net
) {
1410 /* If this is requested, get label from the network label */
1412 r
= mac_selinux_get_our_label(ret
);
1413 if (r
== -EOPNOTSUPP
)
1417 /* Otherwise, get it from the executable we are about to start */
1418 r
= socket_instantiate_service(s
);
1422 if (!UNIT_ISSET(s
->service
))
1425 service
= SERVICE(UNIT_DEREF(s
->service
));
1426 c
= service
->exec_command
[SERVICE_EXEC_START
];
1430 r
= chase_symlinks(c
->path
, service
->exec_context
.root_directory
, CHASE_PREFIX_ROOT
, &path
);
1434 r
= mac_selinux_get_create_label_from_exe(path
, ret
);
1435 if (IN_SET(r
, -EPERM
, -EOPNOTSUPP
))
1446 static int socket_address_listen_do(
1448 const SocketAddress
*address
,
1449 const char *label
) {
1454 return socket_address_listen(
1456 SOCK_CLOEXEC
|SOCK_NONBLOCK
,
1468 #define log_address_error_errno(u, address, error, fmt) \
1470 _cleanup_free_ char *_t = NULL; \
1472 (void) socket_address_print(address, &_t); \
1473 log_unit_error_errno(u, error, fmt, strna(_t)); \
1476 static int fork_needed(const SocketAddress
*address
, const ExecContext
*context
) {
1482 /* Check if we need to do the cgroup or netns stuff. If not we can do things much simpler. */
1484 if (IN_SET(address
->sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1485 r
= bpf_firewall_supported();
1488 if (r
!= BPF_FIREWALL_UNSUPPORTED
) /* If BPF firewalling isn't supported anyway — there's no point in this forking complexity */
1492 return context
->private_network
|| context
->network_namespace_path
;
1495 static int socket_address_listen_in_cgroup(
1497 const SocketAddress
*address
,
1498 const char *label
) {
1500 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1507 /* This is a wrapper around socket_address_listen(), that forks off a helper process inside the
1508 * socket's cgroup and network namespace in which the socket is actually created. This way we ensure
1509 * the socket is actually properly attached to the unit's cgroup for the purpose of BPF filtering and
1512 r
= fork_needed(address
, &s
->exec_context
);
1516 /* Shortcut things... */
1517 fd
= socket_address_listen_do(s
, address
, label
);
1519 return log_address_error_errno(UNIT(s
), address
, fd
, "Failed to create listening socket (%s): %m");
1524 r
= unit_setup_exec_runtime(UNIT(s
));
1526 return log_unit_error_errno(UNIT(s
), r
, "Failed acquire runtime: %m");
1528 if (s
->exec_context
.network_namespace_path
&&
1530 s
->exec_runtime
->netns_storage_socket
[0] >= 0) {
1531 r
= open_netns_path(s
->exec_runtime
->netns_storage_socket
, s
->exec_context
.network_namespace_path
);
1533 return log_unit_error_errno(UNIT(s
), r
, "Failed to open network namespace path %s: %m", s
->exec_context
.network_namespace_path
);
1536 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
1537 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
1539 r
= unit_fork_helper_process(UNIT(s
), "(sd-listen)", &pid
);
1541 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off listener stub process: %m");
1545 pair
[0] = safe_close(pair
[0]);
1547 if ((s
->exec_context
.private_network
|| s
->exec_context
.network_namespace_path
) &&
1549 s
->exec_runtime
->netns_storage_socket
[0] >= 0) {
1551 if (ns_type_supported(NAMESPACE_NET
)) {
1552 r
= setup_netns(s
->exec_runtime
->netns_storage_socket
);
1554 log_unit_error_errno(UNIT(s
), r
, "Failed to join network namespace: %m");
1555 _exit(EXIT_NETWORK
);
1557 } else if (s
->exec_context
.network_namespace_path
) {
1558 log_unit_error(UNIT(s
), "Network namespace path configured but network namespaces not supported.");
1559 _exit(EXIT_NETWORK
);
1561 log_unit_warning(UNIT(s
), "PrivateNetwork=yes is configured, but the kernel does not support network namespaces, ignoring.");
1564 fd
= socket_address_listen_do(s
, address
, label
);
1566 log_address_error_errno(UNIT(s
), address
, fd
, "Failed to create listening socket (%s): %m");
1567 _exit(EXIT_FAILURE
);
1570 r
= send_one_fd(pair
[1], fd
, 0);
1572 log_address_error_errno(UNIT(s
), address
, r
, "Failed to send listening socket (%s) to parent: %m");
1573 _exit(EXIT_FAILURE
);
1576 _exit(EXIT_SUCCESS
);
1579 pair
[1] = safe_close(pair
[1]);
1580 fd
= receive_one_fd(pair
[0], 0);
1582 /* We synchronously wait for the helper, as it shouldn't be slow */
1583 r
= wait_for_terminate_and_check("(sd-listen)", pid
, WAIT_LOG_ABNORMAL
);
1590 return log_address_error_errno(UNIT(s
), address
, fd
, "Failed to receive listening socket (%s): %m");
1595 DEFINE_TRIVIAL_CLEANUP_FUNC(Socket
*, socket_close_fds
);
1597 static int socket_open_fds(Socket
*_s
) {
1598 _cleanup_(socket_close_fdsp
) Socket
*s
= _s
;
1599 _cleanup_(mac_selinux_freep
) char *label
= NULL
;
1600 bool know_label
= false;
1606 LIST_FOREACH(port
, p
, s
->ports
) {
1616 /* Figure out label, if we don't it know yet. We do it once, for the first socket where
1617 * we need this and remember it for the rest. */
1619 r
= socket_determine_selinux_label(s
, &label
);
1621 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine SELinux label: %m");
1626 /* Apply the socket protocol */
1627 switch (p
->address
.type
) {
1630 case SOCK_SEQPACKET
:
1631 if (s
->socket_protocol
== IPPROTO_SCTP
)
1632 p
->address
.protocol
= s
->socket_protocol
;
1636 if (s
->socket_protocol
== IPPROTO_UDPLITE
)
1637 p
->address
.protocol
= s
->socket_protocol
;
1641 p
->fd
= socket_address_listen_in_cgroup(s
, &p
->address
, label
);
1645 socket_apply_socket_options(s
, p
->fd
);
1649 case SOCKET_SPECIAL
:
1651 p
->fd
= special_address_create(p
->path
, s
->writable
);
1653 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open special file %s: %m", p
->path
);
1658 p
->fd
= fifo_address_create(
1663 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open FIFO %s: %m", p
->path
);
1665 socket_apply_fifo_options(s
, p
->fd
);
1671 p
->fd
= mq_address_create(
1677 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open message queue %s: %m", p
->path
);
1680 case SOCKET_USB_FUNCTION
: {
1681 _cleanup_free_
char *ep
= NULL
;
1683 ep
= path_make_absolute("ep0", p
->path
);
1685 p
->fd
= usbffs_address_create(ep
);
1689 r
= usbffs_write_descs(p
->fd
, SERVICE(UNIT_DEREF(s
->service
)));
1693 r
= usbffs_dispatch_eps(p
);
1700 assert_not_reached("Unknown port type");
1708 static void socket_unwatch_fds(Socket
*s
) {
1714 LIST_FOREACH(port
, p
, s
->ports
) {
1718 if (!p
->event_source
)
1721 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_OFF
);
1723 log_unit_debug_errno(UNIT(s
), r
, "Failed to disable event source: %m");
1727 static int socket_watch_fds(Socket
*s
) {
1733 LIST_FOREACH(port
, p
, s
->ports
) {
1737 if (p
->event_source
) {
1738 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_ON
);
1742 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &p
->event_source
, p
->fd
, EPOLLIN
, socket_dispatch_io
, p
);
1746 (void) sd_event_source_set_description(p
->event_source
, "socket-port-io");
1753 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch listening fds: %m");
1754 socket_unwatch_fds(s
);
1764 static int socket_check_open(Socket
*s
) {
1765 bool have_open
= false, have_closed
= false;
1770 LIST_FOREACH(port
, p
, s
->ports
) {
1776 if (have_open
&& have_closed
)
1777 return SOCKET_OPEN_SOME
;
1781 return SOCKET_OPEN_ALL
;
1783 return SOCKET_OPEN_NONE
;
1786 static void socket_set_state(Socket
*s
, SocketState state
) {
1787 SocketState old_state
;
1790 if (s
->state
!= state
)
1791 bus_unit_send_pending_change_signal(UNIT(s
), false);
1793 old_state
= s
->state
;
1801 SOCKET_STOP_PRE_SIGTERM
,
1802 SOCKET_STOP_PRE_SIGKILL
,
1804 SOCKET_FINAL_SIGTERM
,
1805 SOCKET_FINAL_SIGKILL
)) {
1807 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1808 socket_unwatch_control_pid(s
);
1809 s
->control_command
= NULL
;
1810 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1813 if (state
!= SOCKET_LISTENING
)
1814 socket_unwatch_fds(s
);
1822 SOCKET_STOP_PRE_SIGTERM
,
1823 SOCKET_STOP_PRE_SIGKILL
))
1824 socket_close_fds(s
);
1826 if (state
!= old_state
)
1827 log_unit_debug(UNIT(s
), "Changed %s -> %s", socket_state_to_string(old_state
), socket_state_to_string(state
));
1829 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
1832 static int socket_coldplug(Unit
*u
) {
1833 Socket
*s
= SOCKET(u
);
1837 assert(s
->state
== SOCKET_DEAD
);
1839 if (s
->deserialized_state
== s
->state
)
1842 if (s
->control_pid
> 0 &&
1843 pid_is_unwaited(s
->control_pid
) &&
1844 IN_SET(s
->deserialized_state
,
1849 SOCKET_STOP_PRE_SIGTERM
,
1850 SOCKET_STOP_PRE_SIGKILL
,
1852 SOCKET_FINAL_SIGTERM
,
1853 SOCKET_FINAL_SIGKILL
)) {
1855 r
= unit_watch_pid(UNIT(s
), s
->control_pid
, false);
1859 r
= socket_arm_timer(s
, usec_add(u
->state_change_timestamp
.monotonic
, s
->timeout_usec
));
1864 if (IN_SET(s
->deserialized_state
,
1870 /* Originally, we used to simply reopen all sockets here that we didn't have file descriptors
1871 * for. However, this is problematic, as we won't traverse throught the SOCKET_START_CHOWN state for
1872 * them, and thus the UID/GID wouldn't be right. Hence, instead simply check if we have all fds open,
1873 * and if there's a mismatch, warn loudly. */
1875 r
= socket_check_open(s
);
1876 if (r
== SOCKET_OPEN_NONE
)
1877 log_unit_warning(UNIT(s
),
1878 "Socket unit configuration has changed while unit has been running, "
1879 "no open socket file descriptor left. "
1880 "The socket unit is not functional until restarted.");
1881 else if (r
== SOCKET_OPEN_SOME
)
1882 log_unit_warning(UNIT(s
),
1883 "Socket unit configuration has changed while unit has been running, "
1884 "and some socket file descriptors have not been opened yet. "
1885 "The socket unit is not fully functional until restarted.");
1888 if (s
->deserialized_state
== SOCKET_LISTENING
) {
1889 r
= socket_watch_fds(s
);
1894 if (!IN_SET(s
->deserialized_state
, SOCKET_DEAD
, SOCKET_FAILED
)) {
1895 (void) unit_setup_dynamic_creds(u
);
1896 (void) unit_setup_exec_runtime(u
);
1899 socket_set_state(s
, s
->deserialized_state
);
1903 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
1905 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
1906 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
1919 r
= unit_prepare_exec(UNIT(s
));
1923 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
1927 r
= unit_set_exec_params(UNIT(s
), &exec_params
);
1931 r
= exec_spawn(UNIT(s
),
1941 r
= unit_watch_pid(UNIT(s
), pid
, true);
1950 static int socket_chown(Socket
*s
, pid_t
*_pid
) {
1954 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
1958 /* We have to resolve the user names out-of-process, hence
1959 * let's fork here. It's messy, but well, what can we do? */
1961 r
= unit_fork_helper_process(UNIT(s
), "(sd-chown)", &pid
);
1965 uid_t uid
= UID_INVALID
;
1966 gid_t gid
= GID_INVALID
;
1971 if (!isempty(s
->user
)) {
1972 const char *user
= s
->user
;
1974 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
, 0);
1976 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve user %s: %m", user
);
1981 if (!isempty(s
->group
)) {
1982 const char *group
= s
->group
;
1984 r
= get_group_creds(&group
, &gid
, 0);
1986 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve group %s: %m", group
);
1991 LIST_FOREACH(port
, p
, s
->ports
) {
1992 const char *path
= NULL
;
1994 if (p
->type
== SOCKET_SOCKET
)
1995 path
= socket_address_get_path(&p
->address
);
1996 else if (p
->type
== SOCKET_FIFO
)
2002 if (chown(path
, uid
, gid
) < 0) {
2003 log_unit_error_errno(UNIT(s
), errno
, "Failed to chown(): %m");
2008 _exit(EXIT_SUCCESS
);
2011 r
= unit_watch_pid(UNIT(s
), pid
, true);
2019 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
2023 static void socket_enter_dead(Socket
*s
, SocketResult f
) {
2026 if (s
->result
== SOCKET_SUCCESS
)
2029 if (s
->result
== SOCKET_SUCCESS
)
2030 unit_log_success(UNIT(s
));
2032 unit_log_failure(UNIT(s
), socket_result_to_string(s
->result
));
2034 socket_set_state(s
, s
->result
!= SOCKET_SUCCESS
? SOCKET_FAILED
: SOCKET_DEAD
);
2036 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
, true);
2038 exec_context_destroy_runtime_directory(&s
->exec_context
, UNIT(s
)->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
2040 unit_unref_uid_gid(UNIT(s
), true);
2042 dynamic_creds_destroy(&s
->dynamic_creds
);
2045 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
);
2047 static void socket_enter_stop_post(Socket
*s
, SocketResult f
) {
2051 if (s
->result
== SOCKET_SUCCESS
)
2054 socket_unwatch_control_pid(s
);
2055 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
2056 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
];
2058 if (s
->control_command
) {
2059 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2063 socket_set_state(s
, SOCKET_STOP_POST
);
2065 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_SUCCESS
);
2070 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
2071 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2074 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
) {
2079 if (s
->result
== SOCKET_SUCCESS
)
2082 r
= unit_kill_context(
2085 !IN_SET(state
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FINAL_SIGTERM
) ?
2086 KILL_KILL
: KILL_TERMINATE
,
2094 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
2098 socket_set_state(s
, state
);
2099 } else if (state
== SOCKET_STOP_PRE_SIGTERM
)
2100 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_SUCCESS
);
2101 else if (state
== SOCKET_STOP_PRE_SIGKILL
)
2102 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2103 else if (state
== SOCKET_FINAL_SIGTERM
)
2104 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_SUCCESS
);
2106 socket_enter_dead(s
, SOCKET_SUCCESS
);
2111 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
2113 if (IN_SET(state
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_STOP_PRE_SIGKILL
))
2114 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2116 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2119 static void socket_enter_stop_pre(Socket
*s
, SocketResult f
) {
2123 if (s
->result
== SOCKET_SUCCESS
)
2126 socket_unwatch_control_pid(s
);
2127 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
2128 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
];
2130 if (s
->control_command
) {
2131 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2135 socket_set_state(s
, SOCKET_STOP_PRE
);
2137 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2142 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-pre' task: %m");
2143 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2146 static void socket_enter_listening(Socket
*s
) {
2150 r
= socket_watch_fds(s
);
2152 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch sockets: %m");
2156 socket_set_state(s
, SOCKET_LISTENING
);
2160 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2163 static void socket_enter_start_post(Socket
*s
) {
2167 socket_unwatch_control_pid(s
);
2168 s
->control_command_id
= SOCKET_EXEC_START_POST
;
2169 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
];
2171 if (s
->control_command
) {
2172 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2174 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
2178 socket_set_state(s
, SOCKET_START_POST
);
2180 socket_enter_listening(s
);
2185 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2188 static void socket_enter_start_chown(Socket
*s
) {
2193 r
= socket_open_fds(s
);
2195 log_unit_warning_errno(UNIT(s
), r
, "Failed to listen on sockets: %m");
2199 if (!isempty(s
->user
) || !isempty(s
->group
)) {
2201 socket_unwatch_control_pid(s
);
2202 s
->control_command_id
= SOCKET_EXEC_START_CHOWN
;
2203 s
->control_command
= NULL
;
2205 r
= socket_chown(s
, &s
->control_pid
);
2207 log_unit_warning_errno(UNIT(s
), r
, "Failed to fork 'start-chown' task: %m");
2211 socket_set_state(s
, SOCKET_START_CHOWN
);
2213 socket_enter_start_post(s
);
2218 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2221 static void socket_enter_start_pre(Socket
*s
) {
2225 socket_unwatch_control_pid(s
);
2227 unit_warn_leftover_processes(UNIT(s
));
2229 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
2230 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
];
2232 if (s
->control_command
) {
2233 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2235 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
2239 socket_set_state(s
, SOCKET_START_PRE
);
2241 socket_enter_start_chown(s
);
2246 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2249 static void flush_ports(Socket
*s
) {
2252 /* Flush all incoming traffic, regardless if actual bytes or new connections, so that this socket isn't busy
2255 LIST_FOREACH(port
, p
, s
->ports
) {
2259 (void) flush_accept(p
->fd
);
2260 (void) flush_fd(p
->fd
);
2264 static void socket_enter_running(Socket
*s
, int cfd
) {
2265 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2268 /* Note that this call takes possession of the connection fd passed. It either has to assign it somewhere or
2273 /* We don't take connections anymore if we are supposed to shut down anyway */
2274 if (unit_stop_pending(UNIT(s
))) {
2276 log_unit_debug(UNIT(s
), "Suppressing connection request since unit stop is scheduled.");
2286 if (!ratelimit_below(&s
->trigger_limit
)) {
2287 log_unit_warning(UNIT(s
), "Trigger limit hit, refusing further activation.");
2288 socket_enter_stop_pre(s
, SOCKET_FAILURE_TRIGGER_LIMIT_HIT
);
2293 bool pending
= false;
2298 /* If there's already a start pending don't bother to
2300 HASHMAP_FOREACH_KEY(v
, other
, UNIT(s
)->dependencies
[UNIT_TRIGGERS
], i
)
2301 if (unit_active_or_pending(other
)) {
2307 if (!UNIT_ISSET(s
->service
)) {
2308 log_unit_error(UNIT(s
), "Service to activate vanished, refusing activation.");
2313 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT_DEREF(s
->service
), JOB_REPLACE
, NULL
, &error
, NULL
);
2318 socket_set_state(s
, SOCKET_RUNNING
);
2320 _cleanup_free_
char *prefix
= NULL
, *instance
= NULL
, *name
= NULL
;
2321 _cleanup_(socket_peer_unrefp
) SocketPeer
*p
= NULL
;
2324 if (s
->n_connections
>= s
->max_connections
) {
2325 log_unit_warning(UNIT(s
), "Too many incoming connections (%u), dropping connection.",
2330 if (s
->max_connections_per_source
> 0) {
2331 r
= socket_acquire_peer(s
, cfd
, &p
);
2334 } else if (r
> 0 && p
->n_ref
> s
->max_connections_per_source
) {
2335 _cleanup_free_
char *t
= NULL
;
2337 (void) sockaddr_pretty(&p
->peer
.sa
, p
->peer_salen
, true, false, &t
);
2339 log_unit_warning(UNIT(s
),
2340 "Too many incoming connections (%u) from source %s, dropping connection.",
2341 p
->n_ref
, strnull(t
));
2346 r
= socket_instantiate_service(s
);
2350 r
= instance_from_socket(cfd
, s
->n_accepted
, &instance
);
2355 /* ENOTCONN is legitimate if TCP RST was received.
2356 * This connection is over, but the socket unit lives on. */
2357 log_unit_debug(UNIT(s
), "Got ENOTCONN on incoming socket, assuming aborted connection attempt, ignoring.");
2361 r
= unit_name_to_prefix(UNIT(s
)->id
, &prefix
);
2365 r
= unit_name_build(prefix
, instance
, ".service", &name
);
2369 r
= unit_add_name(UNIT_DEREF(s
->service
), name
);
2373 service
= SERVICE(UNIT_DEREF(s
->service
));
2374 unit_ref_unset(&s
->service
);
2377 unit_choose_id(UNIT(service
), name
);
2379 r
= service_set_socket_fd(service
, cfd
, s
, s
->selinux_context_from_net
);
2383 cfd
= -1; /* We passed ownership of the fd to the service now. Forget it here. */
2386 service
->peer
= TAKE_PTR(p
); /* Pass ownership of the peer reference */
2388 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(service
), JOB_REPLACE
, NULL
, &error
, NULL
);
2390 /* We failed to activate the new service, but it still exists. Let's make sure the service
2391 * closes and forgets the connection fd again, immediately. */
2392 service_close_socket_fd(service
);
2396 /* Notify clients about changed counters */
2397 unit_add_to_dbus_queue(UNIT(s
));
2408 log_unit_warning(UNIT(s
), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
2409 cfd
>= 0 ? "template" : "non-template",
2410 bus_error_message(&error
, r
));
2412 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2416 static void socket_run_next(Socket
*s
) {
2420 assert(s
->control_command
);
2421 assert(s
->control_command
->command_next
);
2423 socket_unwatch_control_pid(s
);
2425 s
->control_command
= s
->control_command
->command_next
;
2427 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2434 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next task: %m");
2436 if (s
->state
== SOCKET_START_POST
)
2437 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2438 else if (s
->state
== SOCKET_STOP_POST
)
2439 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2441 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2444 static int socket_start(Unit
*u
) {
2445 Socket
*s
= SOCKET(u
);
2450 /* We cannot fulfill this request right now, try again later
2452 if (IN_SET(s
->state
,
2454 SOCKET_STOP_PRE_SIGKILL
,
2455 SOCKET_STOP_PRE_SIGTERM
,
2457 SOCKET_FINAL_SIGTERM
,
2458 SOCKET_FINAL_SIGKILL
))
2461 /* Already on it! */
2462 if (IN_SET(s
->state
,
2468 /* Cannot run this without the service being around */
2469 if (UNIT_ISSET(s
->service
)) {
2472 service
= SERVICE(UNIT_DEREF(s
->service
));
2474 if (UNIT(service
)->load_state
!= UNIT_LOADED
) {
2475 log_unit_error(u
, "Socket service %s not loaded, refusing.", UNIT(service
)->id
);
2479 /* If the service is already active we cannot start the
2481 if (!IN_SET(service
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
)) {
2482 log_unit_error(u
, "Socket service %s already active, refusing.", UNIT(service
)->id
);
2487 assert(IN_SET(s
->state
, SOCKET_DEAD
, SOCKET_FAILED
));
2489 r
= unit_test_start_limit(u
);
2491 socket_enter_dead(s
, SOCKET_FAILURE_START_LIMIT_HIT
);
2495 r
= unit_acquire_invocation_id(u
);
2499 s
->result
= SOCKET_SUCCESS
;
2500 exec_command_reset_status_list_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
2502 u
->reset_accounting
= true;
2504 socket_enter_start_pre(s
);
2508 static int socket_stop(Unit
*u
) {
2509 Socket
*s
= SOCKET(u
);
2514 if (IN_SET(s
->state
,
2516 SOCKET_STOP_PRE_SIGTERM
,
2517 SOCKET_STOP_PRE_SIGKILL
,
2519 SOCKET_FINAL_SIGTERM
,
2520 SOCKET_FINAL_SIGKILL
))
2523 /* If there's already something running we go directly into
2525 if (IN_SET(s
->state
,
2528 SOCKET_START_POST
)) {
2529 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_SUCCESS
);
2533 assert(IN_SET(s
->state
, SOCKET_LISTENING
, SOCKET_RUNNING
));
2535 socket_enter_stop_pre(s
, SOCKET_SUCCESS
);
2539 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2540 Socket
*s
= SOCKET(u
);
2548 (void) serialize_item(f
, "state", socket_state_to_string(s
->state
));
2549 (void) serialize_item(f
, "result", socket_result_to_string(s
->result
));
2550 (void) serialize_item_format(f
, "n-accepted", "%u", s
->n_accepted
);
2551 (void) serialize_item_format(f
, "n-refused", "%u", s
->n_refused
);
2553 if (s
->control_pid
> 0)
2554 (void) serialize_item_format(f
, "control-pid", PID_FMT
, s
->control_pid
);
2556 if (s
->control_command_id
>= 0)
2557 (void) serialize_item(f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
2559 LIST_FOREACH(port
, p
, s
->ports
) {
2565 copy
= fdset_put_dup(fds
, p
->fd
);
2567 return log_unit_warning_errno(u
, copy
, "Failed to serialize socket fd: %m");
2569 if (p
->type
== SOCKET_SOCKET
) {
2570 _cleanup_free_
char *t
= NULL
;
2572 r
= socket_address_print(&p
->address
, &t
);
2574 return log_unit_error_errno(u
, r
, "Failed to format socket address: %m");
2576 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2577 (void) serialize_item_format(f
, "netlink", "%i %s", copy
, t
);
2579 (void) serialize_item_format(f
, "socket", "%i %i %s", copy
, p
->address
.type
, t
);
2580 } else if (p
->type
== SOCKET_SPECIAL
)
2581 (void) serialize_item_format(f
, "special", "%i %s", copy
, p
->path
);
2582 else if (p
->type
== SOCKET_MQUEUE
)
2583 (void) serialize_item_format(f
, "mqueue", "%i %s", copy
, p
->path
);
2584 else if (p
->type
== SOCKET_USB_FUNCTION
)
2585 (void) serialize_item_format(f
, "ffs", "%i %s", copy
, p
->path
);
2587 assert(p
->type
== SOCKET_FIFO
);
2588 (void) serialize_item_format(f
, "fifo", "%i %s", copy
, p
->path
);
2595 static void socket_port_take_fd(SocketPort
*p
, FDSet
*fds
, int fd
) {
2599 p
->fd
= fdset_remove(fds
, fd
);
2602 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2603 Socket
*s
= SOCKET(u
);
2609 if (streq(key
, "state")) {
2612 state
= socket_state_from_string(value
);
2614 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2616 s
->deserialized_state
= state
;
2617 } else if (streq(key
, "result")) {
2620 f
= socket_result_from_string(value
);
2622 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2623 else if (f
!= SOCKET_SUCCESS
)
2626 } else if (streq(key
, "n-accepted")) {
2629 if (safe_atou(value
, &k
) < 0)
2630 log_unit_debug(u
, "Failed to parse n-accepted value: %s", value
);
2633 } else if (streq(key
, "n-refused")) {
2636 if (safe_atou(value
, &k
) < 0)
2637 log_unit_debug(u
, "Failed to parse n-refused value: %s", value
);
2640 } else if (streq(key
, "control-pid")) {
2643 if (parse_pid(value
, &pid
) < 0)
2644 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2646 s
->control_pid
= pid
;
2647 } else if (streq(key
, "control-command")) {
2648 SocketExecCommand id
;
2650 id
= socket_exec_command_from_string(value
);
2652 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2654 s
->control_command_id
= id
;
2655 s
->control_command
= s
->exec_command
[id
];
2657 } else if (streq(key
, "fifo")) {
2661 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2662 log_unit_debug(u
, "Failed to parse fifo value: %s", value
);
2664 LIST_FOREACH(port
, p
, s
->ports
)
2665 if (p
->type
== SOCKET_FIFO
&&
2666 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2667 socket_port_take_fd(p
, fds
, fd
);
2671 } else if (streq(key
, "special")) {
2675 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2676 log_unit_debug(u
, "Failed to parse special value: %s", value
);
2678 LIST_FOREACH(port
, p
, s
->ports
)
2679 if (p
->type
== SOCKET_SPECIAL
&&
2680 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2681 socket_port_take_fd(p
, fds
, fd
);
2685 } else if (streq(key
, "mqueue")) {
2689 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2690 log_unit_debug(u
, "Failed to parse mqueue value: %s", value
);
2692 LIST_FOREACH(port
, p
, s
->ports
)
2693 if (p
->type
== SOCKET_MQUEUE
&&
2694 streq(p
->path
, value
+skip
)) {
2695 socket_port_take_fd(p
, fds
, fd
);
2699 } else if (streq(key
, "socket")) {
2700 int fd
, type
, skip
= 0;
2703 if (sscanf(value
, "%i %i %n", &fd
, &type
, &skip
) < 2 || fd
< 0 || type
< 0 || !fdset_contains(fds
, fd
))
2704 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2706 LIST_FOREACH(port
, p
, s
->ports
)
2707 if (socket_address_is(&p
->address
, value
+skip
, type
)) {
2708 socket_port_take_fd(p
, fds
, fd
);
2712 } else if (streq(key
, "netlink")) {
2716 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2717 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2719 LIST_FOREACH(port
, p
, s
->ports
)
2720 if (socket_address_is_netlink(&p
->address
, value
+skip
)) {
2721 socket_port_take_fd(p
, fds
, fd
);
2725 } else if (streq(key
, "ffs")) {
2729 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2730 log_unit_debug(u
, "Failed to parse ffs value: %s", value
);
2732 LIST_FOREACH(port
, p
, s
->ports
)
2733 if (p
->type
== SOCKET_USB_FUNCTION
&&
2734 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2735 socket_port_take_fd(p
, fds
, fd
);
2740 log_unit_debug(UNIT(s
), "Unknown serialization key: %s", key
);
2745 static void socket_distribute_fds(Unit
*u
, FDSet
*fds
) {
2746 Socket
*s
= SOCKET(u
);
2751 LIST_FOREACH(port
, p
, s
->ports
) {
2755 if (p
->type
!= SOCKET_SOCKET
)
2761 FDSET_FOREACH(fd
, fds
, i
) {
2762 if (socket_address_matches_fd(&p
->address
, fd
)) {
2763 p
->fd
= fdset_remove(fds
, fd
);
2764 s
->deserialized_state
= SOCKET_LISTENING
;
2771 _pure_
static UnitActiveState
socket_active_state(Unit
*u
) {
2774 return state_translation_table
[SOCKET(u
)->state
];
2777 _pure_
static const char *socket_sub_state_to_string(Unit
*u
) {
2780 return socket_state_to_string(SOCKET(u
)->state
);
2783 const char* socket_port_type_to_string(SocketPort
*p
) {
2791 switch (p
->address
.type
) {
2799 case SOCK_SEQPACKET
:
2800 return "SequentialPacket";
2803 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2811 case SOCKET_SPECIAL
:
2815 return "MessageQueue";
2820 case SOCKET_USB_FUNCTION
:
2821 return "USBFunction";
2828 SocketType
socket_port_type_from_string(const char *s
) {
2831 if (STR_IN_SET(s
, "Stream", "Datagram", "SequentialPacket", "Netlink"))
2832 return SOCKET_SOCKET
;
2833 else if (streq(s
, "Special"))
2834 return SOCKET_SPECIAL
;
2835 else if (streq(s
, "MessageQueue"))
2836 return SOCKET_MQUEUE
;
2837 else if (streq(s
, "FIFO"))
2839 else if (streq(s
, "USBFunction"))
2840 return SOCKET_USB_FUNCTION
;
2842 return _SOCKET_TYPE_INVALID
;
2845 _pure_
static bool socket_may_gc(Unit
*u
) {
2846 Socket
*s
= SOCKET(u
);
2850 return s
->n_connections
== 0;
2853 static int socket_accept_do(Socket
*s
, int fd
) {
2859 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
2861 /* Convert transient network errors into clean and well-defined EAGAIN */
2862 return ERRNO_IS_ACCEPT_AGAIN(errno
) ? -EAGAIN
: -errno
;
2867 static int socket_accept_in_cgroup(Socket
*s
, SocketPort
*p
, int fd
) {
2868 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
2876 /* Similar to socket_address_listen_in_cgroup(), but for accept() rathern than socket(): make sure that any
2877 * connection socket is also properly associated with the cgroup. */
2879 if (!IN_SET(p
->address
.sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
2882 r
= bpf_firewall_supported();
2885 if (r
== BPF_FIREWALL_UNSUPPORTED
)
2888 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
2889 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
2891 r
= unit_fork_helper_process(UNIT(s
), "(sd-accept)", &pid
);
2893 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off accept stub process: %m");
2897 pair
[0] = safe_close(pair
[0]);
2899 cfd
= socket_accept_do(s
, fd
);
2900 if (cfd
== -EAGAIN
) /* spurious accept() */
2901 _exit(EXIT_SUCCESS
);
2903 log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
2904 _exit(EXIT_FAILURE
);
2907 r
= send_one_fd(pair
[1], cfd
, 0);
2909 log_unit_error_errno(UNIT(s
), r
, "Failed to send connection socket to parent: %m");
2910 _exit(EXIT_FAILURE
);
2913 _exit(EXIT_SUCCESS
);
2916 pair
[1] = safe_close(pair
[1]);
2917 cfd
= receive_one_fd(pair
[0], 0);
2919 /* We synchronously wait for the helper, as it shouldn't be slow */
2920 r
= wait_for_terminate_and_check("(sd-accept)", pid
, WAIT_LOG_ABNORMAL
);
2926 /* If we received no fd, we got EIO here. If this happens with a process exit code of EXIT_SUCCESS
2927 * this is a spurious accept(), let's convert that back to EAGAIN here. */
2931 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to receive connection socket: %m");
2936 cfd
= socket_accept_do(s
, fd
);
2937 if (cfd
== -EAGAIN
) /* spurious accept(), skip it silently */
2940 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
2945 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
2946 SocketPort
*p
= userdata
;
2952 if (p
->socket
->state
!= SOCKET_LISTENING
)
2955 log_unit_debug(UNIT(p
->socket
), "Incoming traffic");
2957 if (revents
!= EPOLLIN
) {
2958 if (revents
& EPOLLHUP
)
2959 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.");
2961 log_unit_error(UNIT(p
->socket
), "Got unexpected poll event (0x%x) on socket.", revents
);
2965 if (p
->socket
->accept
&&
2966 p
->type
== SOCKET_SOCKET
&&
2967 socket_address_can_accept(&p
->address
)) {
2969 cfd
= socket_accept_in_cgroup(p
->socket
, p
, fd
);
2970 if (cfd
== -EAGAIN
) /* Spurious accept() */
2975 socket_apply_socket_options(p
->socket
, cfd
);
2978 socket_enter_running(p
->socket
, cfd
);
2982 socket_enter_stop_pre(p
->socket
, SOCKET_FAILURE_RESOURCES
);
2986 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2987 Socket
*s
= SOCKET(u
);
2993 if (pid
!= s
->control_pid
)
2998 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
3000 else if (code
== CLD_EXITED
)
3001 f
= SOCKET_FAILURE_EXIT_CODE
;
3002 else if (code
== CLD_KILLED
)
3003 f
= SOCKET_FAILURE_SIGNAL
;
3004 else if (code
== CLD_DUMPED
)
3005 f
= SOCKET_FAILURE_CORE_DUMP
;
3007 assert_not_reached("Unknown sigchld code");
3009 if (s
->control_command
) {
3010 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
3012 if (s
->control_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3016 unit_log_process_exit(
3017 u
, f
== SOCKET_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
3019 socket_exec_command_to_string(s
->control_command_id
),
3022 if (s
->result
== SOCKET_SUCCESS
)
3025 if (s
->control_command
&&
3026 s
->control_command
->command_next
&&
3027 f
== SOCKET_SUCCESS
) {
3029 log_unit_debug(u
, "Running next command for state %s", socket_state_to_string(s
->state
));
3032 s
->control_command
= NULL
;
3033 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
3035 /* No further commands for this step, so let's figure
3036 * out what to do next */
3038 log_unit_debug(u
, "Got final SIGCHLD for state %s", socket_state_to_string(s
->state
));
3042 case SOCKET_START_PRE
:
3043 if (f
== SOCKET_SUCCESS
)
3044 socket_enter_start_chown(s
);
3046 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, f
);
3049 case SOCKET_START_CHOWN
:
3050 if (f
== SOCKET_SUCCESS
)
3051 socket_enter_start_post(s
);
3053 socket_enter_stop_pre(s
, f
);
3056 case SOCKET_START_POST
:
3057 if (f
== SOCKET_SUCCESS
)
3058 socket_enter_listening(s
);
3060 socket_enter_stop_pre(s
, f
);
3063 case SOCKET_STOP_PRE
:
3064 case SOCKET_STOP_PRE_SIGTERM
:
3065 case SOCKET_STOP_PRE_SIGKILL
:
3066 socket_enter_stop_post(s
, f
);
3069 case SOCKET_STOP_POST
:
3070 case SOCKET_FINAL_SIGTERM
:
3071 case SOCKET_FINAL_SIGKILL
:
3072 socket_enter_dead(s
, f
);
3076 assert_not_reached("Uh, control process died at wrong time.");
3080 /* Notify clients about changed exit status */
3081 unit_add_to_dbus_queue(u
);
3084 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
3085 Socket
*s
= SOCKET(userdata
);
3088 assert(s
->timer_event_source
== source
);
3092 case SOCKET_START_PRE
:
3093 log_unit_warning(UNIT(s
), "Starting timed out. Terminating.");
3094 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3097 case SOCKET_START_CHOWN
:
3098 case SOCKET_START_POST
:
3099 log_unit_warning(UNIT(s
), "Starting timed out. Stopping.");
3100 socket_enter_stop_pre(s
, SOCKET_FAILURE_TIMEOUT
);
3103 case SOCKET_STOP_PRE
:
3104 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
3105 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3108 case SOCKET_STOP_PRE_SIGTERM
:
3109 if (s
->kill_context
.send_sigkill
) {
3110 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
3111 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3113 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL. Ignoring.");
3114 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3118 case SOCKET_STOP_PRE_SIGKILL
:
3119 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
3120 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3123 case SOCKET_STOP_POST
:
3124 log_unit_warning(UNIT(s
), "Stopping timed out (2). Terminating.");
3125 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3128 case SOCKET_FINAL_SIGTERM
:
3129 if (s
->kill_context
.send_sigkill
) {
3130 log_unit_warning(UNIT(s
), "Stopping timed out (2). Killing.");
3131 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3133 log_unit_warning(UNIT(s
), "Stopping timed out (2). Skipping SIGKILL. Ignoring.");
3134 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3138 case SOCKET_FINAL_SIGKILL
:
3139 log_unit_warning(UNIT(s
), "Still around after SIGKILL (2). Entering failed mode.");
3140 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3144 assert_not_reached("Timeout at wrong time.");
3150 int socket_collect_fds(Socket
*s
, int **fds
) {
3151 size_t k
= 0, n
= 0;
3158 /* Called from the service code for requesting our fds */
3160 LIST_FOREACH(port
, p
, s
->ports
) {
3163 n
+= p
->n_auxiliary_fds
;
3175 LIST_FOREACH(port
, p
, s
->ports
) {
3180 for (i
= 0; i
< p
->n_auxiliary_fds
; ++i
)
3181 rfds
[k
++] = p
->auxiliary_fds
[i
];
3190 static void socket_reset_failed(Unit
*u
) {
3191 Socket
*s
= SOCKET(u
);
3195 if (s
->state
== SOCKET_FAILED
)
3196 socket_set_state(s
, SOCKET_DEAD
);
3198 s
->result
= SOCKET_SUCCESS
;
3201 void socket_connection_unref(Socket
*s
) {
3204 /* The service is dead. Yay!
3206 * This is strictly for one-instance-per-connection
3209 assert(s
->n_connections
> 0);
3212 log_unit_debug(UNIT(s
), "One connection closed, %u left.", s
->n_connections
);
3215 static void socket_trigger_notify(Unit
*u
, Unit
*other
) {
3216 Socket
*s
= SOCKET(u
);
3221 /* Filter out invocations with bogus state */
3222 if (other
->load_state
!= UNIT_LOADED
|| other
->type
!= UNIT_SERVICE
)
3225 /* Don't propagate state changes from the service if we are already down */
3226 if (!IN_SET(s
->state
, SOCKET_RUNNING
, SOCKET_LISTENING
))
3229 /* We don't care for the service state if we are in Accept=yes mode */
3233 /* Propagate start limit hit state */
3234 if (other
->start_limit_hit
) {
3235 socket_enter_stop_pre(s
, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
);
3239 /* Don't propagate anything if there's still a job queued */
3243 if (IN_SET(SERVICE(other
)->state
,
3244 SERVICE_DEAD
, SERVICE_FAILED
,
3245 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
3246 SERVICE_AUTO_RESTART
))
3247 socket_enter_listening(s
);
3249 if (SERVICE(other
)->state
== SERVICE_RUNNING
)
3250 socket_set_state(s
, SOCKET_RUNNING
);
3253 static int socket_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3254 return unit_kill_common(u
, who
, signo
, -1, SOCKET(u
)->control_pid
, error
);
3257 static int socket_get_timeout(Unit
*u
, usec_t
*timeout
) {
3258 Socket
*s
= SOCKET(u
);
3262 if (!s
->timer_event_source
)
3265 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3268 if (t
== USEC_INFINITY
)
3275 char *socket_fdname(Socket
*s
) {
3278 /* Returns the name to use for $LISTEN_NAMES. If the user
3279 * didn't specify anything specifically, use the socket unit's
3280 * name as fallback. */
3282 return s
->fdname
?: UNIT(s
)->id
;
3285 static int socket_control_pid(Unit
*u
) {
3286 Socket
*s
= SOCKET(u
);
3290 return s
->control_pid
;
3293 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
3294 [SOCKET_EXEC_START_PRE
] = "ExecStartPre",
3295 [SOCKET_EXEC_START_CHOWN
] = "ExecStartChown",
3296 [SOCKET_EXEC_START_POST
] = "ExecStartPost",
3297 [SOCKET_EXEC_STOP_PRE
] = "ExecStopPre",
3298 [SOCKET_EXEC_STOP_POST
] = "ExecStopPost"
3301 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
3303 static const char* const socket_result_table
[_SOCKET_RESULT_MAX
] = {
3304 [SOCKET_SUCCESS
] = "success",
3305 [SOCKET_FAILURE_RESOURCES
] = "resources",
3306 [SOCKET_FAILURE_TIMEOUT
] = "timeout",
3307 [SOCKET_FAILURE_EXIT_CODE
] = "exit-code",
3308 [SOCKET_FAILURE_SIGNAL
] = "signal",
3309 [SOCKET_FAILURE_CORE_DUMP
] = "core-dump",
3310 [SOCKET_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
3311 [SOCKET_FAILURE_TRIGGER_LIMIT_HIT
] = "trigger-limit-hit",
3312 [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
] = "service-start-limit-hit"
3315 DEFINE_STRING_TABLE_LOOKUP(socket_result
, SocketResult
);
3317 const UnitVTable socket_vtable
= {
3318 .object_size
= sizeof(Socket
),
3319 .exec_context_offset
= offsetof(Socket
, exec_context
),
3320 .cgroup_context_offset
= offsetof(Socket
, cgroup_context
),
3321 .kill_context_offset
= offsetof(Socket
, kill_context
),
3322 .exec_runtime_offset
= offsetof(Socket
, exec_runtime
),
3323 .dynamic_creds_offset
= offsetof(Socket
, dynamic_creds
),
3329 .private_section
= "Socket",
3331 .can_transient
= true,
3333 .init
= socket_init
,
3334 .done
= socket_done
,
3335 .load
= socket_load
,
3337 .coldplug
= socket_coldplug
,
3339 .dump
= socket_dump
,
3341 .start
= socket_start
,
3342 .stop
= socket_stop
,
3344 .kill
= socket_kill
,
3346 .get_timeout
= socket_get_timeout
,
3348 .serialize
= socket_serialize
,
3349 .deserialize_item
= socket_deserialize_item
,
3350 .distribute_fds
= socket_distribute_fds
,
3352 .active_state
= socket_active_state
,
3353 .sub_state_to_string
= socket_sub_state_to_string
,
3355 .may_gc
= socket_may_gc
,
3357 .sigchld_event
= socket_sigchld_event
,
3359 .trigger_notify
= socket_trigger_notify
,
3361 .reset_failed
= socket_reset_failed
,
3363 .control_pid
= socket_control_pid
,
3365 .bus_vtable
= bus_socket_vtable
,
3366 .bus_set_property
= bus_socket_set_property
,
3367 .bus_commit_properties
= bus_socket_commit_properties
,
3369 .status_message_formats
= {
3370 /*.starting_stopping = {
3371 [0] = "Starting socket %s...",
3372 [1] = "Stopping socket %s...",
3374 .finished_start_job
= {
3375 [JOB_DONE
] = "Listening on %s.",
3376 [JOB_FAILED
] = "Failed to listen on %s.",
3377 [JOB_TIMEOUT
] = "Timed out starting %s.",
3379 .finished_stop_job
= {
3380 [JOB_DONE
] = "Closed %s.",
3381 [JOB_FAILED
] = "Failed stopping %s.",
3382 [JOB_TIMEOUT
] = "Timed out stopping %s.",