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 socket_address_listen_in_cgroup(
1478 const SocketAddress
*address
,
1479 const char *label
) {
1481 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1488 /* This is a wrapper around socket_address_listen(), that forks off a helper process inside the socket's cgroup
1489 * in which the socket is actually created. This way we ensure the socket is actually properly attached to the
1490 * unit's cgroup for the purpose of BPF filtering and such. */
1492 if (!IN_SET(address
->sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
1493 goto shortcut
; /* BPF filtering only applies to IPv4 + IPv6, shortcut things for other protocols */
1495 r
= bpf_firewall_supported();
1498 if (r
== BPF_FIREWALL_UNSUPPORTED
) /* If BPF firewalling isn't supported anyway — there's no point in this forking complexity */
1501 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
1502 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
1504 r
= unit_fork_helper_process(UNIT(s
), "(sd-listen)", &pid
);
1506 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off listener stub process: %m");
1510 pair
[0] = safe_close(pair
[0]);
1512 fd
= socket_address_listen_do(s
, address
, label
);
1514 log_address_error_errno(UNIT(s
), address
, fd
, "Failed to create listening socket (%s): %m");
1515 _exit(EXIT_FAILURE
);
1518 r
= send_one_fd(pair
[1], fd
, 0);
1520 log_address_error_errno(UNIT(s
), address
, r
, "Failed to send listening socket (%s) to parent: %m");
1521 _exit(EXIT_FAILURE
);
1524 _exit(EXIT_SUCCESS
);
1527 pair
[1] = safe_close(pair
[1]);
1528 fd
= receive_one_fd(pair
[0], 0);
1530 /* We synchronously wait for the helper, as it shouldn't be slow */
1531 r
= wait_for_terminate_and_check("(sd-listen)", pid
, WAIT_LOG_ABNORMAL
);
1538 return log_address_error_errno(UNIT(s
), address
, fd
, "Failed to receive listening socket (%s): %m");
1543 fd
= socket_address_listen_do(s
, address
, label
);
1545 return log_address_error_errno(UNIT(s
), address
, fd
, "Failed to create listening socket (%s): %m");
1550 DEFINE_TRIVIAL_CLEANUP_FUNC(Socket
*, socket_close_fds
);
1552 static int socket_open_fds(Socket
*_s
) {
1553 _cleanup_(socket_close_fdsp
) Socket
*s
= _s
;
1554 _cleanup_(mac_selinux_freep
) char *label
= NULL
;
1555 bool know_label
= false;
1561 LIST_FOREACH(port
, p
, s
->ports
) {
1571 /* Figure out label, if we don't it know yet. We do it once, for the first socket where
1572 * we need this and remember it for the rest. */
1574 r
= socket_determine_selinux_label(s
, &label
);
1576 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine SELinux label: %m");
1581 /* Apply the socket protocol */
1582 switch (p
->address
.type
) {
1585 case SOCK_SEQPACKET
:
1586 if (s
->socket_protocol
== IPPROTO_SCTP
)
1587 p
->address
.protocol
= s
->socket_protocol
;
1591 if (s
->socket_protocol
== IPPROTO_UDPLITE
)
1592 p
->address
.protocol
= s
->socket_protocol
;
1596 r
= socket_address_listen_in_cgroup(s
, &p
->address
, label
);
1601 socket_apply_socket_options(s
, p
->fd
);
1605 case SOCKET_SPECIAL
:
1607 r
= special_address_create(p
->path
, s
->writable
);
1609 return log_unit_error_errno(UNIT(s
), r
, "Failed to open special file %s: %m", p
->path
);
1616 r
= fifo_address_create(
1621 return log_unit_error_errno(UNIT(s
), r
, "Failed to open FIFO %s: %m", p
->path
);
1624 socket_apply_fifo_options(s
, p
->fd
);
1630 r
= mq_address_create(
1636 return log_unit_error_errno(UNIT(s
), r
, "Failed to open message queue %s: %m", p
->path
);
1641 case SOCKET_USB_FUNCTION
: {
1642 _cleanup_free_
char *ep
= NULL
;
1644 ep
= path_make_absolute("ep0", p
->path
);
1646 r
= usbffs_address_create(ep
);
1652 r
= usbffs_write_descs(p
->fd
, SERVICE(UNIT_DEREF(s
->service
)));
1656 r
= usbffs_dispatch_eps(p
);
1663 assert_not_reached("Unknown port type");
1671 static void socket_unwatch_fds(Socket
*s
) {
1677 LIST_FOREACH(port
, p
, s
->ports
) {
1681 if (!p
->event_source
)
1684 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_OFF
);
1686 log_unit_debug_errno(UNIT(s
), r
, "Failed to disable event source: %m");
1690 static int socket_watch_fds(Socket
*s
) {
1696 LIST_FOREACH(port
, p
, s
->ports
) {
1700 if (p
->event_source
) {
1701 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_ON
);
1705 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &p
->event_source
, p
->fd
, EPOLLIN
, socket_dispatch_io
, p
);
1709 (void) sd_event_source_set_description(p
->event_source
, "socket-port-io");
1716 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch listening fds: %m");
1717 socket_unwatch_fds(s
);
1727 static int socket_check_open(Socket
*s
) {
1728 bool have_open
= false, have_closed
= false;
1733 LIST_FOREACH(port
, p
, s
->ports
) {
1739 if (have_open
&& have_closed
)
1740 return SOCKET_OPEN_SOME
;
1744 return SOCKET_OPEN_ALL
;
1746 return SOCKET_OPEN_NONE
;
1749 static void socket_set_state(Socket
*s
, SocketState state
) {
1750 SocketState old_state
;
1753 if (s
->state
!= state
)
1754 bus_unit_send_pending_change_signal(UNIT(s
), false);
1756 old_state
= s
->state
;
1764 SOCKET_STOP_PRE_SIGTERM
,
1765 SOCKET_STOP_PRE_SIGKILL
,
1767 SOCKET_FINAL_SIGTERM
,
1768 SOCKET_FINAL_SIGKILL
)) {
1770 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1771 socket_unwatch_control_pid(s
);
1772 s
->control_command
= NULL
;
1773 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1776 if (state
!= SOCKET_LISTENING
)
1777 socket_unwatch_fds(s
);
1785 SOCKET_STOP_PRE_SIGTERM
,
1786 SOCKET_STOP_PRE_SIGKILL
))
1787 socket_close_fds(s
);
1789 if (state
!= old_state
)
1790 log_unit_debug(UNIT(s
), "Changed %s -> %s", socket_state_to_string(old_state
), socket_state_to_string(state
));
1792 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
1795 static int socket_coldplug(Unit
*u
) {
1796 Socket
*s
= SOCKET(u
);
1800 assert(s
->state
== SOCKET_DEAD
);
1802 if (s
->deserialized_state
== s
->state
)
1805 if (s
->control_pid
> 0 &&
1806 pid_is_unwaited(s
->control_pid
) &&
1807 IN_SET(s
->deserialized_state
,
1812 SOCKET_STOP_PRE_SIGTERM
,
1813 SOCKET_STOP_PRE_SIGKILL
,
1815 SOCKET_FINAL_SIGTERM
,
1816 SOCKET_FINAL_SIGKILL
)) {
1818 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
1822 r
= socket_arm_timer(s
, usec_add(u
->state_change_timestamp
.monotonic
, s
->timeout_usec
));
1827 if (IN_SET(s
->deserialized_state
,
1833 /* Originally, we used to simply reopen all sockets here that we didn't have file descriptors
1834 * for. However, this is problematic, as we won't traverse throught the SOCKET_START_CHOWN state for
1835 * them, and thus the UID/GID wouldn't be right. Hence, instead simply check if we have all fds open,
1836 * and if there's a mismatch, warn loudly. */
1838 r
= socket_check_open(s
);
1839 if (r
== SOCKET_OPEN_NONE
)
1840 log_unit_warning(UNIT(s
),
1841 "Socket unit configuration has changed while unit has been running, "
1842 "no open socket file descriptor left. "
1843 "The socket unit is not functional until restarted.");
1844 else if (r
== SOCKET_OPEN_SOME
)
1845 log_unit_warning(UNIT(s
),
1846 "Socket unit configuration has changed while unit has been running, "
1847 "and some socket file descriptors have not been opened yet. "
1848 "The socket unit is not fully functional until restarted.");
1851 if (s
->deserialized_state
== SOCKET_LISTENING
) {
1852 r
= socket_watch_fds(s
);
1857 if (!IN_SET(s
->deserialized_state
, SOCKET_DEAD
, SOCKET_FAILED
)) {
1858 (void) unit_setup_dynamic_creds(u
);
1859 (void) unit_setup_exec_runtime(u
);
1862 socket_set_state(s
, s
->deserialized_state
);
1866 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
1868 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
1869 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
1882 r
= unit_prepare_exec(UNIT(s
));
1886 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
1890 r
= unit_set_exec_params(UNIT(s
), &exec_params
);
1894 r
= exec_spawn(UNIT(s
),
1904 r
= unit_watch_pid(UNIT(s
), pid
);
1906 /* FIXME: we need to do something here */
1914 static int socket_chown(Socket
*s
, pid_t
*_pid
) {
1918 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
1922 /* We have to resolve the user names out-of-process, hence
1923 * let's fork here. It's messy, but well, what can we do? */
1925 r
= unit_fork_helper_process(UNIT(s
), "(sd-chown)", &pid
);
1929 uid_t uid
= UID_INVALID
;
1930 gid_t gid
= GID_INVALID
;
1935 if (!isempty(s
->user
)) {
1936 const char *user
= s
->user
;
1938 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
, 0);
1940 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve user %s: %m", user
);
1945 if (!isempty(s
->group
)) {
1946 const char *group
= s
->group
;
1948 r
= get_group_creds(&group
, &gid
, 0);
1950 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve group %s: %m", group
);
1955 LIST_FOREACH(port
, p
, s
->ports
) {
1956 const char *path
= NULL
;
1958 if (p
->type
== SOCKET_SOCKET
)
1959 path
= socket_address_get_path(&p
->address
);
1960 else if (p
->type
== SOCKET_FIFO
)
1966 if (chown(path
, uid
, gid
) < 0) {
1967 log_unit_error_errno(UNIT(s
), errno
, "Failed to chown(): %m");
1972 _exit(EXIT_SUCCESS
);
1975 r
= unit_watch_pid(UNIT(s
), pid
);
1983 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1987 static void socket_enter_dead(Socket
*s
, SocketResult f
) {
1990 if (s
->result
== SOCKET_SUCCESS
)
1993 if (s
->result
== SOCKET_SUCCESS
)
1994 unit_log_success(UNIT(s
));
1996 unit_log_failure(UNIT(s
), socket_result_to_string(s
->result
));
1998 socket_set_state(s
, s
->result
!= SOCKET_SUCCESS
? SOCKET_FAILED
: SOCKET_DEAD
);
2000 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
, true);
2002 exec_context_destroy_runtime_directory(&s
->exec_context
, UNIT(s
)->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
2004 unit_unref_uid_gid(UNIT(s
), true);
2006 dynamic_creds_destroy(&s
->dynamic_creds
);
2009 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
);
2011 static void socket_enter_stop_post(Socket
*s
, SocketResult f
) {
2015 if (s
->result
== SOCKET_SUCCESS
)
2018 socket_unwatch_control_pid(s
);
2019 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
2020 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
];
2022 if (s
->control_command
) {
2023 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2027 socket_set_state(s
, SOCKET_STOP_POST
);
2029 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_SUCCESS
);
2034 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
2035 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2038 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
) {
2043 if (s
->result
== SOCKET_SUCCESS
)
2046 r
= unit_kill_context(
2049 !IN_SET(state
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FINAL_SIGTERM
) ?
2050 KILL_KILL
: KILL_TERMINATE
,
2058 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
2062 socket_set_state(s
, state
);
2063 } else if (state
== SOCKET_STOP_PRE_SIGTERM
)
2064 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_SUCCESS
);
2065 else if (state
== SOCKET_STOP_PRE_SIGKILL
)
2066 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2067 else if (state
== SOCKET_FINAL_SIGTERM
)
2068 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_SUCCESS
);
2070 socket_enter_dead(s
, SOCKET_SUCCESS
);
2075 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
2077 if (IN_SET(state
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_STOP_PRE_SIGKILL
))
2078 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2080 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2083 static void socket_enter_stop_pre(Socket
*s
, SocketResult f
) {
2087 if (s
->result
== SOCKET_SUCCESS
)
2090 socket_unwatch_control_pid(s
);
2091 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
2092 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
];
2094 if (s
->control_command
) {
2095 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2099 socket_set_state(s
, SOCKET_STOP_PRE
);
2101 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2106 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-pre' task: %m");
2107 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2110 static void socket_enter_listening(Socket
*s
) {
2114 r
= socket_watch_fds(s
);
2116 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch sockets: %m");
2120 socket_set_state(s
, SOCKET_LISTENING
);
2124 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2127 static void socket_enter_start_post(Socket
*s
) {
2131 socket_unwatch_control_pid(s
);
2132 s
->control_command_id
= SOCKET_EXEC_START_POST
;
2133 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
];
2135 if (s
->control_command
) {
2136 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2138 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
2142 socket_set_state(s
, SOCKET_START_POST
);
2144 socket_enter_listening(s
);
2149 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2152 static void socket_enter_start_chown(Socket
*s
) {
2157 r
= socket_open_fds(s
);
2159 log_unit_warning_errno(UNIT(s
), r
, "Failed to listen on sockets: %m");
2163 if (!isempty(s
->user
) || !isempty(s
->group
)) {
2165 socket_unwatch_control_pid(s
);
2166 s
->control_command_id
= SOCKET_EXEC_START_CHOWN
;
2167 s
->control_command
= NULL
;
2169 r
= socket_chown(s
, &s
->control_pid
);
2171 log_unit_warning_errno(UNIT(s
), r
, "Failed to fork 'start-chown' task: %m");
2175 socket_set_state(s
, SOCKET_START_CHOWN
);
2177 socket_enter_start_post(s
);
2182 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2185 static void socket_enter_start_pre(Socket
*s
) {
2189 socket_unwatch_control_pid(s
);
2191 unit_warn_leftover_processes(UNIT(s
));
2193 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
2194 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
];
2196 if (s
->control_command
) {
2197 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2199 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
2203 socket_set_state(s
, SOCKET_START_PRE
);
2205 socket_enter_start_chown(s
);
2210 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
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 (void) flush_accept(p
->fd
);
2224 (void) flush_fd(p
->fd
);
2228 static void socket_enter_running(Socket
*s
, int cfd
) {
2229 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2232 /* Note that this call takes possession of the connection fd passed. It either has to assign it somewhere or
2237 /* We don't take connections anymore if we are supposed to shut down anyway */
2238 if (unit_stop_pending(UNIT(s
))) {
2240 log_unit_debug(UNIT(s
), "Suppressing connection request since unit stop is scheduled.");
2250 if (!ratelimit_below(&s
->trigger_limit
)) {
2251 log_unit_warning(UNIT(s
), "Trigger limit hit, refusing further activation.");
2252 socket_enter_stop_pre(s
, SOCKET_FAILURE_TRIGGER_LIMIT_HIT
);
2257 bool pending
= false;
2262 /* If there's already a start pending don't bother to
2264 HASHMAP_FOREACH_KEY(v
, other
, UNIT(s
)->dependencies
[UNIT_TRIGGERS
], i
)
2265 if (unit_active_or_pending(other
)) {
2271 if (!UNIT_ISSET(s
->service
)) {
2272 log_unit_error(UNIT(s
), "Service to activate vanished, refusing activation.");
2277 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT_DEREF(s
->service
), JOB_REPLACE
, &error
, NULL
);
2282 socket_set_state(s
, SOCKET_RUNNING
);
2284 _cleanup_free_
char *prefix
= NULL
, *instance
= NULL
, *name
= NULL
;
2285 _cleanup_(socket_peer_unrefp
) SocketPeer
*p
= NULL
;
2288 if (s
->n_connections
>= s
->max_connections
) {
2289 log_unit_warning(UNIT(s
), "Too many incoming connections (%u), dropping connection.",
2294 if (s
->max_connections_per_source
> 0) {
2295 r
= socket_acquire_peer(s
, cfd
, &p
);
2298 } else if (r
> 0 && p
->n_ref
> s
->max_connections_per_source
) {
2299 _cleanup_free_
char *t
= NULL
;
2301 (void) sockaddr_pretty(&p
->peer
.sa
, p
->peer_salen
, true, false, &t
);
2303 log_unit_warning(UNIT(s
),
2304 "Too many incoming connections (%u) from source %s, dropping connection.",
2305 p
->n_ref
, strnull(t
));
2310 r
= socket_instantiate_service(s
);
2314 r
= instance_from_socket(cfd
, s
->n_accepted
, &instance
);
2319 /* ENOTCONN is legitimate if TCP RST was received.
2320 * This connection is over, but the socket unit lives on. */
2321 log_unit_debug(UNIT(s
), "Got ENOTCONN on incoming socket, assuming aborted connection attempt, ignoring.");
2325 r
= unit_name_to_prefix(UNIT(s
)->id
, &prefix
);
2329 r
= unit_name_build(prefix
, instance
, ".service", &name
);
2333 r
= unit_add_name(UNIT_DEREF(s
->service
), name
);
2337 service
= SERVICE(UNIT_DEREF(s
->service
));
2338 unit_ref_unset(&s
->service
);
2341 unit_choose_id(UNIT(service
), name
);
2343 r
= service_set_socket_fd(service
, cfd
, s
, s
->selinux_context_from_net
);
2347 cfd
= -1; /* We passed ownership of the fd to the service now. Forget it here. */
2350 service
->peer
= TAKE_PTR(p
); /* Pass ownership of the peer reference */
2352 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(service
), JOB_REPLACE
, &error
, NULL
);
2354 /* We failed to activate the new service, but it still exists. Let's make sure the service
2355 * closes and forgets the connection fd again, immediately. */
2356 service_close_socket_fd(service
);
2360 /* Notify clients about changed counters */
2361 unit_add_to_dbus_queue(UNIT(s
));
2372 log_unit_warning(UNIT(s
), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
2373 cfd
>= 0 ? "template" : "non-template",
2374 bus_error_message(&error
, r
));
2376 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2380 static void socket_run_next(Socket
*s
) {
2384 assert(s
->control_command
);
2385 assert(s
->control_command
->command_next
);
2387 socket_unwatch_control_pid(s
);
2389 s
->control_command
= s
->control_command
->command_next
;
2391 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2398 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next task: %m");
2400 if (s
->state
== SOCKET_START_POST
)
2401 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2402 else if (s
->state
== SOCKET_STOP_POST
)
2403 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2405 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2408 static int socket_start(Unit
*u
) {
2409 Socket
*s
= SOCKET(u
);
2414 /* We cannot fulfill this request right now, try again later
2416 if (IN_SET(s
->state
,
2418 SOCKET_STOP_PRE_SIGKILL
,
2419 SOCKET_STOP_PRE_SIGTERM
,
2421 SOCKET_FINAL_SIGTERM
,
2422 SOCKET_FINAL_SIGKILL
))
2425 /* Already on it! */
2426 if (IN_SET(s
->state
,
2432 /* Cannot run this without the service being around */
2433 if (UNIT_ISSET(s
->service
)) {
2436 service
= SERVICE(UNIT_DEREF(s
->service
));
2438 if (UNIT(service
)->load_state
!= UNIT_LOADED
) {
2439 log_unit_error(u
, "Socket service %s not loaded, refusing.", UNIT(service
)->id
);
2443 /* If the service is already active we cannot start the
2445 if (!IN_SET(service
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
)) {
2446 log_unit_error(u
, "Socket service %s already active, refusing.", UNIT(service
)->id
);
2451 assert(IN_SET(s
->state
, SOCKET_DEAD
, SOCKET_FAILED
));
2453 r
= unit_start_limit_test(u
);
2455 socket_enter_dead(s
, SOCKET_FAILURE_START_LIMIT_HIT
);
2459 r
= unit_acquire_invocation_id(u
);
2463 s
->result
= SOCKET_SUCCESS
;
2464 exec_command_reset_status_list_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
2466 u
->reset_accounting
= true;
2468 socket_enter_start_pre(s
);
2472 static int socket_stop(Unit
*u
) {
2473 Socket
*s
= SOCKET(u
);
2478 if (IN_SET(s
->state
,
2480 SOCKET_STOP_PRE_SIGTERM
,
2481 SOCKET_STOP_PRE_SIGKILL
,
2483 SOCKET_FINAL_SIGTERM
,
2484 SOCKET_FINAL_SIGKILL
))
2487 /* If there's already something running we go directly into
2489 if (IN_SET(s
->state
,
2492 SOCKET_START_POST
)) {
2493 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_SUCCESS
);
2497 assert(IN_SET(s
->state
, SOCKET_LISTENING
, SOCKET_RUNNING
));
2499 socket_enter_stop_pre(s
, SOCKET_SUCCESS
);
2503 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2504 Socket
*s
= SOCKET(u
);
2512 (void) serialize_item(f
, "state", socket_state_to_string(s
->state
));
2513 (void) serialize_item(f
, "result", socket_result_to_string(s
->result
));
2514 (void) serialize_item_format(f
, "n-accepted", "%u", s
->n_accepted
);
2515 (void) serialize_item_format(f
, "n-refused", "%u", s
->n_refused
);
2517 if (s
->control_pid
> 0)
2518 (void) serialize_item_format(f
, "control-pid", PID_FMT
, s
->control_pid
);
2520 if (s
->control_command_id
>= 0)
2521 (void) serialize_item(f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
2523 LIST_FOREACH(port
, p
, s
->ports
) {
2529 copy
= fdset_put_dup(fds
, p
->fd
);
2531 return log_unit_warning_errno(u
, copy
, "Failed to serialize socket fd: %m");
2533 if (p
->type
== SOCKET_SOCKET
) {
2534 _cleanup_free_
char *t
= NULL
;
2536 r
= socket_address_print(&p
->address
, &t
);
2538 return log_unit_error_errno(u
, r
, "Failed to format socket address: %m");
2540 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2541 (void) serialize_item_format(f
, "netlink", "%i %s", copy
, t
);
2543 (void) serialize_item_format(f
, "socket", "%i %i %s", copy
, p
->address
.type
, t
);
2544 } else if (p
->type
== SOCKET_SPECIAL
)
2545 (void) serialize_item_format(f
, "special", "%i %s", copy
, p
->path
);
2546 else if (p
->type
== SOCKET_MQUEUE
)
2547 (void) serialize_item_format(f
, "mqueue", "%i %s", copy
, p
->path
);
2548 else if (p
->type
== SOCKET_USB_FUNCTION
)
2549 (void) serialize_item_format(f
, "ffs", "%i %s", copy
, p
->path
);
2551 assert(p
->type
== SOCKET_FIFO
);
2552 (void) serialize_item_format(f
, "fifo", "%i %s", copy
, p
->path
);
2559 static void socket_port_take_fd(SocketPort
*p
, FDSet
*fds
, int fd
) {
2563 p
->fd
= fdset_remove(fds
, fd
);
2566 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2567 Socket
*s
= SOCKET(u
);
2573 if (streq(key
, "state")) {
2576 state
= socket_state_from_string(value
);
2578 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2580 s
->deserialized_state
= state
;
2581 } else if (streq(key
, "result")) {
2584 f
= socket_result_from_string(value
);
2586 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2587 else if (f
!= SOCKET_SUCCESS
)
2590 } else if (streq(key
, "n-accepted")) {
2593 if (safe_atou(value
, &k
) < 0)
2594 log_unit_debug(u
, "Failed to parse n-accepted value: %s", value
);
2597 } else if (streq(key
, "n-refused")) {
2600 if (safe_atou(value
, &k
) < 0)
2601 log_unit_debug(u
, "Failed to parse n-refused value: %s", value
);
2604 } else if (streq(key
, "control-pid")) {
2607 if (parse_pid(value
, &pid
) < 0)
2608 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2610 s
->control_pid
= pid
;
2611 } else if (streq(key
, "control-command")) {
2612 SocketExecCommand id
;
2614 id
= socket_exec_command_from_string(value
);
2616 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2618 s
->control_command_id
= id
;
2619 s
->control_command
= s
->exec_command
[id
];
2621 } else if (streq(key
, "fifo")) {
2625 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2626 log_unit_debug(u
, "Failed to parse fifo value: %s", value
);
2628 LIST_FOREACH(port
, p
, s
->ports
)
2629 if (p
->type
== SOCKET_FIFO
&&
2630 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2631 socket_port_take_fd(p
, fds
, fd
);
2635 } else if (streq(key
, "special")) {
2639 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2640 log_unit_debug(u
, "Failed to parse special value: %s", value
);
2642 LIST_FOREACH(port
, p
, s
->ports
)
2643 if (p
->type
== SOCKET_SPECIAL
&&
2644 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2645 socket_port_take_fd(p
, fds
, fd
);
2649 } else if (streq(key
, "mqueue")) {
2653 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2654 log_unit_debug(u
, "Failed to parse mqueue value: %s", value
);
2656 LIST_FOREACH(port
, p
, s
->ports
)
2657 if (p
->type
== SOCKET_MQUEUE
&&
2658 streq(p
->path
, value
+skip
)) {
2659 socket_port_take_fd(p
, fds
, fd
);
2663 } else if (streq(key
, "socket")) {
2664 int fd
, type
, skip
= 0;
2667 if (sscanf(value
, "%i %i %n", &fd
, &type
, &skip
) < 2 || fd
< 0 || type
< 0 || !fdset_contains(fds
, fd
))
2668 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2670 LIST_FOREACH(port
, p
, s
->ports
)
2671 if (socket_address_is(&p
->address
, value
+skip
, type
)) {
2672 socket_port_take_fd(p
, fds
, fd
);
2676 } else if (streq(key
, "netlink")) {
2680 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2681 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2683 LIST_FOREACH(port
, p
, s
->ports
)
2684 if (socket_address_is_netlink(&p
->address
, value
+skip
)) {
2685 socket_port_take_fd(p
, fds
, fd
);
2689 } else if (streq(key
, "ffs")) {
2693 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2694 log_unit_debug(u
, "Failed to parse ffs value: %s", value
);
2696 LIST_FOREACH(port
, p
, s
->ports
)
2697 if (p
->type
== SOCKET_USB_FUNCTION
&&
2698 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2699 socket_port_take_fd(p
, fds
, fd
);
2704 log_unit_debug(UNIT(s
), "Unknown serialization key: %s", key
);
2709 static void socket_distribute_fds(Unit
*u
, FDSet
*fds
) {
2710 Socket
*s
= SOCKET(u
);
2715 LIST_FOREACH(port
, p
, s
->ports
) {
2719 if (p
->type
!= SOCKET_SOCKET
)
2725 FDSET_FOREACH(fd
, fds
, i
) {
2726 if (socket_address_matches_fd(&p
->address
, fd
)) {
2727 p
->fd
= fdset_remove(fds
, fd
);
2728 s
->deserialized_state
= SOCKET_LISTENING
;
2735 _pure_
static UnitActiveState
socket_active_state(Unit
*u
) {
2738 return state_translation_table
[SOCKET(u
)->state
];
2741 _pure_
static const char *socket_sub_state_to_string(Unit
*u
) {
2744 return socket_state_to_string(SOCKET(u
)->state
);
2747 const char* socket_port_type_to_string(SocketPort
*p
) {
2755 switch (p
->address
.type
) {
2763 case SOCK_SEQPACKET
:
2764 return "SequentialPacket";
2767 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2775 case SOCKET_SPECIAL
:
2779 return "MessageQueue";
2784 case SOCKET_USB_FUNCTION
:
2785 return "USBFunction";
2792 SocketType
socket_port_type_from_string(const char *s
) {
2795 if (STR_IN_SET(s
, "Stream", "Datagram", "SequentialPacket", "Netlink"))
2796 return SOCKET_SOCKET
;
2797 else if (streq(s
, "Special"))
2798 return SOCKET_SPECIAL
;
2799 else if (streq(s
, "MessageQueue"))
2800 return SOCKET_MQUEUE
;
2801 else if (streq(s
, "FIFO"))
2803 else if (streq(s
, "USBFunction"))
2804 return SOCKET_USB_FUNCTION
;
2806 return _SOCKET_TYPE_INVALID
;
2809 _pure_
static bool socket_may_gc(Unit
*u
) {
2810 Socket
*s
= SOCKET(u
);
2814 return s
->n_connections
== 0;
2817 static int socket_accept_do(Socket
*s
, int fd
) {
2824 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
);
2838 static int socket_accept_in_cgroup(Socket
*s
, SocketPort
*p
, int fd
) {
2839 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
2847 /* Similar to socket_address_listen_in_cgroup(), but for accept() rathern than socket(): make sure that any
2848 * connection socket is also properly associated with the cgroup. */
2850 if (!IN_SET(p
->address
.sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
2853 r
= bpf_firewall_supported();
2856 if (r
== BPF_FIREWALL_UNSUPPORTED
)
2859 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
2860 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
2862 r
= unit_fork_helper_process(UNIT(s
), "(sd-accept)", &pid
);
2864 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off accept stub process: %m");
2868 pair
[0] = safe_close(pair
[0]);
2870 cfd
= socket_accept_do(s
, fd
);
2872 log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
2873 _exit(EXIT_FAILURE
);
2876 r
= send_one_fd(pair
[1], cfd
, 0);
2878 log_unit_error_errno(UNIT(s
), r
, "Failed to send connection socket to parent: %m");
2879 _exit(EXIT_FAILURE
);
2882 _exit(EXIT_SUCCESS
);
2885 pair
[1] = safe_close(pair
[1]);
2886 cfd
= receive_one_fd(pair
[0], 0);
2888 /* We synchronously wait for the helper, as it shouldn't be slow */
2889 r
= wait_for_terminate_and_check("(sd-accept)", pid
, WAIT_LOG_ABNORMAL
);
2896 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to receive connection socket: %m");
2901 cfd
= socket_accept_do(s
, fd
);
2903 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
2908 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
2909 SocketPort
*p
= userdata
;
2915 if (p
->socket
->state
!= SOCKET_LISTENING
)
2918 log_unit_debug(UNIT(p
->socket
), "Incoming traffic");
2920 if (revents
!= EPOLLIN
) {
2922 if (revents
& EPOLLHUP
)
2923 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.");
2925 log_unit_error(UNIT(p
->socket
), "Got unexpected poll event (0x%x) on socket.", revents
);
2929 if (p
->socket
->accept
&&
2930 p
->type
== SOCKET_SOCKET
&&
2931 socket_address_can_accept(&p
->address
)) {
2933 cfd
= socket_accept_in_cgroup(p
->socket
, p
, fd
);
2937 socket_apply_socket_options(p
->socket
, cfd
);
2940 socket_enter_running(p
->socket
, cfd
);
2944 socket_enter_stop_pre(p
->socket
, SOCKET_FAILURE_RESOURCES
);
2948 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2949 Socket
*s
= SOCKET(u
);
2955 if (pid
!= s
->control_pid
)
2960 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
2962 else if (code
== CLD_EXITED
)
2963 f
= SOCKET_FAILURE_EXIT_CODE
;
2964 else if (code
== CLD_KILLED
)
2965 f
= SOCKET_FAILURE_SIGNAL
;
2966 else if (code
== CLD_DUMPED
)
2967 f
= SOCKET_FAILURE_CORE_DUMP
;
2969 assert_not_reached("Unknown sigchld code");
2971 if (s
->control_command
) {
2972 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2974 if (s
->control_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
2978 unit_log_process_exit(
2979 u
, f
== SOCKET_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2981 socket_exec_command_to_string(s
->control_command_id
),
2984 if (s
->result
== SOCKET_SUCCESS
)
2987 if (s
->control_command
&&
2988 s
->control_command
->command_next
&&
2989 f
== SOCKET_SUCCESS
) {
2991 log_unit_debug(u
, "Running next command for state %s", socket_state_to_string(s
->state
));
2994 s
->control_command
= NULL
;
2995 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
2997 /* No further commands for this step, so let's figure
2998 * out what to do next */
3000 log_unit_debug(u
, "Got final SIGCHLD for state %s", socket_state_to_string(s
->state
));
3004 case SOCKET_START_PRE
:
3005 if (f
== SOCKET_SUCCESS
)
3006 socket_enter_start_chown(s
);
3008 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, f
);
3011 case SOCKET_START_CHOWN
:
3012 if (f
== SOCKET_SUCCESS
)
3013 socket_enter_start_post(s
);
3015 socket_enter_stop_pre(s
, f
);
3018 case SOCKET_START_POST
:
3019 if (f
== SOCKET_SUCCESS
)
3020 socket_enter_listening(s
);
3022 socket_enter_stop_pre(s
, f
);
3025 case SOCKET_STOP_PRE
:
3026 case SOCKET_STOP_PRE_SIGTERM
:
3027 case SOCKET_STOP_PRE_SIGKILL
:
3028 socket_enter_stop_post(s
, f
);
3031 case SOCKET_STOP_POST
:
3032 case SOCKET_FINAL_SIGTERM
:
3033 case SOCKET_FINAL_SIGKILL
:
3034 socket_enter_dead(s
, f
);
3038 assert_not_reached("Uh, control process died at wrong time.");
3042 /* Notify clients about changed exit status */
3043 unit_add_to_dbus_queue(u
);
3046 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
3047 Socket
*s
= SOCKET(userdata
);
3050 assert(s
->timer_event_source
== source
);
3054 case SOCKET_START_PRE
:
3055 log_unit_warning(UNIT(s
), "Starting timed out. Terminating.");
3056 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3059 case SOCKET_START_CHOWN
:
3060 case SOCKET_START_POST
:
3061 log_unit_warning(UNIT(s
), "Starting timed out. Stopping.");
3062 socket_enter_stop_pre(s
, SOCKET_FAILURE_TIMEOUT
);
3065 case SOCKET_STOP_PRE
:
3066 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
3067 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3070 case SOCKET_STOP_PRE_SIGTERM
:
3071 if (s
->kill_context
.send_sigkill
) {
3072 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
3073 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3075 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL. Ignoring.");
3076 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3080 case SOCKET_STOP_PRE_SIGKILL
:
3081 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
3082 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3085 case SOCKET_STOP_POST
:
3086 log_unit_warning(UNIT(s
), "Stopping timed out (2). Terminating.");
3087 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3090 case SOCKET_FINAL_SIGTERM
:
3091 if (s
->kill_context
.send_sigkill
) {
3092 log_unit_warning(UNIT(s
), "Stopping timed out (2). Killing.");
3093 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3095 log_unit_warning(UNIT(s
), "Stopping timed out (2). Skipping SIGKILL. Ignoring.");
3096 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3100 case SOCKET_FINAL_SIGKILL
:
3101 log_unit_warning(UNIT(s
), "Still around after SIGKILL (2). Entering failed mode.");
3102 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3106 assert_not_reached("Timeout at wrong time.");
3112 int socket_collect_fds(Socket
*s
, int **fds
) {
3113 size_t k
= 0, n
= 0;
3120 /* Called from the service code for requesting our fds */
3122 LIST_FOREACH(port
, p
, s
->ports
) {
3125 n
+= p
->n_auxiliary_fds
;
3137 LIST_FOREACH(port
, p
, s
->ports
) {
3142 for (i
= 0; i
< p
->n_auxiliary_fds
; ++i
)
3143 rfds
[k
++] = p
->auxiliary_fds
[i
];
3152 static void socket_reset_failed(Unit
*u
) {
3153 Socket
*s
= SOCKET(u
);
3157 if (s
->state
== SOCKET_FAILED
)
3158 socket_set_state(s
, SOCKET_DEAD
);
3160 s
->result
= SOCKET_SUCCESS
;
3163 void socket_connection_unref(Socket
*s
) {
3166 /* The service is dead. Yay!
3168 * This is strictly for one-instance-per-connection
3171 assert(s
->n_connections
> 0);
3174 log_unit_debug(UNIT(s
), "One connection closed, %u left.", s
->n_connections
);
3177 static void socket_trigger_notify(Unit
*u
, Unit
*other
) {
3178 Socket
*s
= SOCKET(u
);
3183 /* Filter out invocations with bogus state */
3184 if (other
->load_state
!= UNIT_LOADED
|| other
->type
!= UNIT_SERVICE
)
3187 /* Don't propagate state changes from the service if we are already down */
3188 if (!IN_SET(s
->state
, SOCKET_RUNNING
, SOCKET_LISTENING
))
3191 /* We don't care for the service state if we are in Accept=yes mode */
3195 /* Propagate start limit hit state */
3196 if (other
->start_limit_hit
) {
3197 socket_enter_stop_pre(s
, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
);
3201 /* Don't propagate anything if there's still a job queued */
3205 if (IN_SET(SERVICE(other
)->state
,
3206 SERVICE_DEAD
, SERVICE_FAILED
,
3207 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
3208 SERVICE_AUTO_RESTART
))
3209 socket_enter_listening(s
);
3211 if (SERVICE(other
)->state
== SERVICE_RUNNING
)
3212 socket_set_state(s
, SOCKET_RUNNING
);
3215 static int socket_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3216 return unit_kill_common(u
, who
, signo
, -1, SOCKET(u
)->control_pid
, error
);
3219 static int socket_get_timeout(Unit
*u
, usec_t
*timeout
) {
3220 Socket
*s
= SOCKET(u
);
3224 if (!s
->timer_event_source
)
3227 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3230 if (t
== USEC_INFINITY
)
3237 char *socket_fdname(Socket
*s
) {
3240 /* Returns the name to use for $LISTEN_NAMES. If the user
3241 * didn't specify anything specifically, use the socket unit's
3242 * name as fallback. */
3244 return s
->fdname
?: UNIT(s
)->id
;
3247 static int socket_control_pid(Unit
*u
) {
3248 Socket
*s
= SOCKET(u
);
3252 return s
->control_pid
;
3255 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
3256 [SOCKET_EXEC_START_PRE
] = "ExecStartPre",
3257 [SOCKET_EXEC_START_CHOWN
] = "ExecStartChown",
3258 [SOCKET_EXEC_START_POST
] = "ExecStartPost",
3259 [SOCKET_EXEC_STOP_PRE
] = "ExecStopPre",
3260 [SOCKET_EXEC_STOP_POST
] = "ExecStopPost"
3263 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
3265 static const char* const socket_result_table
[_SOCKET_RESULT_MAX
] = {
3266 [SOCKET_SUCCESS
] = "success",
3267 [SOCKET_FAILURE_RESOURCES
] = "resources",
3268 [SOCKET_FAILURE_TIMEOUT
] = "timeout",
3269 [SOCKET_FAILURE_EXIT_CODE
] = "exit-code",
3270 [SOCKET_FAILURE_SIGNAL
] = "signal",
3271 [SOCKET_FAILURE_CORE_DUMP
] = "core-dump",
3272 [SOCKET_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
3273 [SOCKET_FAILURE_TRIGGER_LIMIT_HIT
] = "trigger-limit-hit",
3274 [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
] = "service-start-limit-hit"
3277 DEFINE_STRING_TABLE_LOOKUP(socket_result
, SocketResult
);
3279 const UnitVTable socket_vtable
= {
3280 .object_size
= sizeof(Socket
),
3281 .exec_context_offset
= offsetof(Socket
, exec_context
),
3282 .cgroup_context_offset
= offsetof(Socket
, cgroup_context
),
3283 .kill_context_offset
= offsetof(Socket
, kill_context
),
3284 .exec_runtime_offset
= offsetof(Socket
, exec_runtime
),
3285 .dynamic_creds_offset
= offsetof(Socket
, dynamic_creds
),
3291 .private_section
= "Socket",
3293 .can_transient
= true,
3295 .init
= socket_init
,
3296 .done
= socket_done
,
3297 .load
= socket_load
,
3299 .coldplug
= socket_coldplug
,
3301 .dump
= socket_dump
,
3303 .start
= socket_start
,
3304 .stop
= socket_stop
,
3306 .kill
= socket_kill
,
3308 .get_timeout
= socket_get_timeout
,
3310 .serialize
= socket_serialize
,
3311 .deserialize_item
= socket_deserialize_item
,
3312 .distribute_fds
= socket_distribute_fds
,
3314 .active_state
= socket_active_state
,
3315 .sub_state_to_string
= socket_sub_state_to_string
,
3317 .may_gc
= socket_may_gc
,
3319 .sigchld_event
= socket_sigchld_event
,
3321 .trigger_notify
= socket_trigger_notify
,
3323 .reset_failed
= socket_reset_failed
,
3325 .control_pid
= socket_control_pid
,
3327 .bus_vtable
= bus_socket_vtable
,
3328 .bus_set_property
= bus_socket_set_property
,
3329 .bus_commit_properties
= bus_socket_commit_properties
,
3331 .status_message_formats
= {
3332 /*.starting_stopping = {
3333 [0] = "Starting socket %s...",
3334 [1] = "Stopping socket %s...",
3336 .finished_start_job
= {
3337 [JOB_DONE
] = "Listening on %s.",
3338 [JOB_FAILED
] = "Failed to listen on %s.",
3339 [JOB_TIMEOUT
] = "Timed out starting %s.",
3341 .finished_stop_job
= {
3342 [JOB_DONE
] = "Closed %s.",
3343 [JOB_FAILED
] = "Failed stopping %s.",
3344 [JOB_TIMEOUT
] = "Timed out stopping %s.",