1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include <netinet/tcp.h>
11 #include <linux/sctp.h>
13 #include "alloc-util.h"
14 #include "bpf-firewall.h"
15 #include "bus-error.h"
17 #include "chase-symlinks.h"
19 #include "dbus-socket.h"
20 #include "dbus-unit.h"
22 #include "errno-list.h"
23 #include "exit-status.h"
25 #include "format-util.h"
26 #include "in-addr-util.h"
28 #include "ip-protocol-list.h"
31 #include "mkdir-label.h"
32 #include "parse-util.h"
33 #include "path-util.h"
34 #include "process-util.h"
35 #include "selinux-util.h"
36 #include "serialize.h"
38 #include "signal-util.h"
39 #include "smack-util.h"
41 #include "socket-netlink.h"
43 #include "string-table.h"
44 #include "string-util.h"
46 #include "unit-name.h"
48 #include "user-util.h"
54 union sockaddr_union peer
;
58 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
59 [SOCKET_DEAD
] = UNIT_INACTIVE
,
60 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
61 [SOCKET_START_CHOWN
] = UNIT_ACTIVATING
,
62 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
63 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
64 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
65 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
66 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
67 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
68 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
69 [SOCKET_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
70 [SOCKET_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
71 [SOCKET_FAILED
] = UNIT_FAILED
,
72 [SOCKET_CLEANING
] = UNIT_MAINTENANCE
,
75 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
76 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
77 static void flush_ports(Socket
*s
);
79 static void socket_init(Unit
*u
) {
80 Socket
*s
= SOCKET(u
);
83 assert(u
->load_state
== UNIT_STUB
);
85 s
->backlog
= SOMAXCONN
;
86 s
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
87 s
->directory_mode
= 0755;
88 s
->socket_mode
= 0666;
90 s
->max_connections
= 64;
97 s
->exec_context
.std_output
= u
->manager
->default_std_output
;
98 s
->exec_context
.std_error
= u
->manager
->default_std_error
;
100 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
102 s
->trigger_limit
.interval
= USEC_INFINITY
;
103 s
->trigger_limit
.burst
= UINT_MAX
;
106 static void socket_unwatch_control_pid(Socket
*s
) {
109 if (s
->control_pid
<= 0)
112 unit_unwatch_pid(UNIT(s
), TAKE_PID(s
->control_pid
));
115 static void socket_cleanup_fd_list(SocketPort
*p
) {
118 close_many(p
->auxiliary_fds
, p
->n_auxiliary_fds
);
119 p
->auxiliary_fds
= mfree(p
->auxiliary_fds
);
120 p
->n_auxiliary_fds
= 0;
123 void socket_free_ports(Socket
*s
) {
128 while ((p
= s
->ports
)) {
129 LIST_REMOVE(port
, s
->ports
, p
);
131 sd_event_source_unref(p
->event_source
);
133 socket_cleanup_fd_list(p
);
140 static void socket_done(Unit
*u
) {
141 Socket
*s
= SOCKET(u
);
146 socket_free_ports(s
);
148 while ((p
= set_steal_first(s
->peers_by_address
)))
151 s
->peers_by_address
= set_free(s
->peers_by_address
);
153 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
, false);
154 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
155 s
->control_command
= NULL
;
157 dynamic_creds_unref(&s
->dynamic_creds
);
159 socket_unwatch_control_pid(s
);
161 unit_ref_unset(&s
->service
);
163 s
->tcp_congestion
= mfree(s
->tcp_congestion
);
164 s
->bind_to_device
= mfree(s
->bind_to_device
);
166 s
->smack
= mfree(s
->smack
);
167 s
->smack_ip_in
= mfree(s
->smack_ip_in
);
168 s
->smack_ip_out
= mfree(s
->smack_ip_out
);
170 strv_free(s
->symlinks
);
172 s
->user
= mfree(s
->user
);
173 s
->group
= mfree(s
->group
);
175 s
->fdname
= mfree(s
->fdname
);
177 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
180 static int socket_arm_timer(Socket
*s
, usec_t usec
) {
185 if (s
->timer_event_source
) {
186 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
190 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
193 if (usec
== USEC_INFINITY
)
196 r
= sd_event_add_time(
197 UNIT(s
)->manager
->event
,
198 &s
->timer_event_source
,
201 socket_dispatch_timer
, s
);
205 (void) sd_event_source_set_description(s
->timer_event_source
, "socket-timer");
210 static bool have_non_accept_socket(Socket
*s
) {
218 LIST_FOREACH(port
, p
, s
->ports
) {
220 if (p
->type
!= SOCKET_SOCKET
)
223 if (!socket_address_can_accept(&p
->address
))
230 static int socket_add_mount_dependencies(Socket
*s
) {
236 LIST_FOREACH(port
, p
, s
->ports
) {
237 const char *path
= NULL
;
239 if (p
->type
== SOCKET_SOCKET
)
240 path
= socket_address_get_path(&p
->address
);
241 else if (IN_SET(p
->type
, SOCKET_FIFO
, SOCKET_SPECIAL
, SOCKET_USB_FUNCTION
))
247 r
= unit_require_mounts_for(UNIT(s
), path
, UNIT_DEPENDENCY_FILE
);
255 static int socket_add_device_dependencies(Socket
*s
) {
260 if (!s
->bind_to_device
|| streq(s
->bind_to_device
, "lo"))
263 t
= strjoina("/sys/subsystem/net/devices/", s
->bind_to_device
);
264 return unit_add_node_dependency(UNIT(s
), t
, UNIT_BINDS_TO
, UNIT_DEPENDENCY_FILE
);
267 static int socket_add_default_dependencies(Socket
*s
) {
271 if (!UNIT(s
)->default_dependencies
)
274 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_BEFORE
, SPECIAL_SOCKETS_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
278 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
279 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
284 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
287 _pure_
static bool socket_has_exec(Socket
*s
) {
291 for (i
= 0; i
< _SOCKET_EXEC_COMMAND_MAX
; i
++)
292 if (s
->exec_command
[i
])
298 static int socket_add_extras(Socket
*s
) {
304 /* Pick defaults for the trigger limit, if nothing was explicitly configured. We pick a relatively high limit
305 * in Accept=yes mode, and a lower limit for Accept=no. Reason: in Accept=yes mode we are invoking accept()
306 * ourselves before the trigger limit can hit, thus incoming connections are taken off the socket queue quickly
307 * and reliably. This is different for Accept=no, where the spawned service has to take the incoming traffic
308 * off the queues, which it might not necessarily do. Moreover, while Accept=no services are supposed to
309 * process whatever is queued in one go, and thus should normally never have to be started frequently. This is
310 * different for Accept=yes where each connection is processed by a new service instance, and thus frequent
311 * service starts are typical. */
313 if (s
->trigger_limit
.interval
== USEC_INFINITY
)
314 s
->trigger_limit
.interval
= 2 * USEC_PER_SEC
;
316 if (s
->trigger_limit
.burst
== UINT_MAX
) {
318 s
->trigger_limit
.burst
= 200;
320 s
->trigger_limit
.burst
= 20;
323 if (have_non_accept_socket(s
)) {
325 if (!UNIT_DEREF(s
->service
)) {
328 r
= unit_load_related_unit(u
, ".service", &x
);
332 unit_ref_set(&s
->service
, u
, x
);
335 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT_DEREF(s
->service
), true, UNIT_DEPENDENCY_IMPLICIT
);
340 r
= socket_add_mount_dependencies(s
);
344 r
= socket_add_device_dependencies(s
);
348 r
= unit_patch_contexts(u
);
352 if (socket_has_exec(s
)) {
353 r
= unit_add_exec_dependencies(u
, &s
->exec_context
);
358 r
= unit_set_default_slice(u
);
362 r
= socket_add_default_dependencies(s
);
369 static const char *socket_find_symlink_target(Socket
*s
) {
370 const char *found
= NULL
;
373 LIST_FOREACH(port
, p
, s
->ports
) {
374 const char *f
= NULL
;
383 f
= socket_address_get_path(&p
->address
);
401 static int socket_verify(Socket
*s
) {
403 assert(UNIT(s
)->load_state
== UNIT_LOADED
);
406 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing.");
408 if (s
->accept
&& have_non_accept_socket(s
))
409 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing.");
411 if (s
->accept
&& s
->max_connections
<= 0)
412 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "MaxConnection= setting too small. Refusing.");
414 if (s
->accept
&& UNIT_DEREF(s
->service
))
415 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Explicit service configuration for accepting socket units not supported. Refusing.");
417 if (s
->exec_context
.pam_name
&& s
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
)
418 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing.");
420 if (!strv_isempty(s
->symlinks
) && !socket_find_symlink_target(s
))
421 return log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOEXEC
), "Unit has symlinks set but none or more than one node in the file system. Refusing.");
426 static void peer_address_hash_func(const SocketPeer
*s
, struct siphash
*state
) {
429 if (s
->peer
.sa
.sa_family
== AF_INET
)
430 siphash24_compress(&s
->peer
.in
.sin_addr
, sizeof(s
->peer
.in
.sin_addr
), state
);
431 else if (s
->peer
.sa
.sa_family
== AF_INET6
)
432 siphash24_compress(&s
->peer
.in6
.sin6_addr
, sizeof(s
->peer
.in6
.sin6_addr
), state
);
433 else if (s
->peer
.sa
.sa_family
== AF_VSOCK
)
434 siphash24_compress(&s
->peer
.vm
.svm_cid
, sizeof(s
->peer
.vm
.svm_cid
), state
);
436 assert_not_reached();
439 static int peer_address_compare_func(const SocketPeer
*x
, const SocketPeer
*y
) {
442 r
= CMP(x
->peer
.sa
.sa_family
, y
->peer
.sa
.sa_family
);
446 switch(x
->peer
.sa
.sa_family
) {
448 return memcmp(&x
->peer
.in
.sin_addr
, &y
->peer
.in
.sin_addr
, sizeof(x
->peer
.in
.sin_addr
));
450 return memcmp(&x
->peer
.in6
.sin6_addr
, &y
->peer
.in6
.sin6_addr
, sizeof(x
->peer
.in6
.sin6_addr
));
452 return CMP(x
->peer
.vm
.svm_cid
, y
->peer
.vm
.svm_cid
);
454 assert_not_reached();
457 DEFINE_PRIVATE_HASH_OPS(peer_address_hash_ops
, SocketPeer
, peer_address_hash_func
, peer_address_compare_func
);
459 static int socket_load(Unit
*u
) {
460 Socket
*s
= SOCKET(u
);
464 assert(u
->load_state
== UNIT_STUB
);
466 r
= unit_load_fragment_and_dropin(u
, true);
470 if (u
->load_state
!= UNIT_LOADED
)
473 /* This is a new unit? Then let's add in some extras */
474 r
= socket_add_extras(s
);
478 return socket_verify(s
);
481 static SocketPeer
*socket_peer_new(void) {
484 p
= new(SocketPeer
, 1);
494 static SocketPeer
*socket_peer_free(SocketPeer
*p
) {
498 set_remove(p
->socket
->peers_by_address
, p
);
503 DEFINE_TRIVIAL_REF_UNREF_FUNC(SocketPeer
, socket_peer
, socket_peer_free
);
505 int socket_acquire_peer(Socket
*s
, int fd
, SocketPeer
**p
) {
506 _cleanup_(socket_peer_unrefp
) SocketPeer
*remote
= NULL
;
508 .peer_salen
= sizeof(union sockaddr_union
),
515 if (getpeername(fd
, &sa
.peer
.sa
, &sa
.peer_salen
) < 0)
516 return log_unit_error_errno(UNIT(s
), errno
, "getpeername failed: %m");
518 if (!IN_SET(sa
.peer
.sa
.sa_family
, AF_INET
, AF_INET6
, AF_VSOCK
)) {
523 r
= set_ensure_allocated(&s
->peers_by_address
, &peer_address_hash_ops
);
527 i
= set_get(s
->peers_by_address
, &sa
);
529 *p
= socket_peer_ref(i
);
533 remote
= socket_peer_new();
537 remote
->peer
= sa
.peer
;
538 remote
->peer_salen
= sa
.peer_salen
;
540 r
= set_put(s
->peers_by_address
, remote
);
546 *p
= TAKE_PTR(remote
);
550 _const_
static const char* listen_lookup(int family
, int type
) {
552 if (family
== AF_NETLINK
)
553 return "ListenNetlink";
555 if (type
== SOCK_STREAM
)
556 return "ListenStream";
557 else if (type
== SOCK_DGRAM
)
558 return "ListenDatagram";
559 else if (type
== SOCK_SEQPACKET
)
560 return "ListenSequentialPacket";
562 assert_not_reached();
566 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
567 Socket
*s
= SOCKET(u
);
569 const char *prefix2
, *str
;
574 prefix
= strempty(prefix
);
575 prefix2
= strjoina(prefix
, "\t");
578 "%sSocket State: %s\n"
580 "%sClean Result: %s\n"
581 "%sBindIPv6Only: %s\n"
583 "%sSocketMode: %04o\n"
584 "%sDirectoryMode: %04o\n"
588 "%sTransparent: %s\n"
590 "%sPassCredentials: %s\n"
591 "%sPassSecurity: %s\n"
592 "%sPassPacketInfo: %s\n"
593 "%sTCPCongestion: %s\n"
594 "%sRemoveOnStop: %s\n"
596 "%sFileDescriptorName: %s\n"
597 "%sSELinuxContextFromNet: %s\n",
598 prefix
, socket_state_to_string(s
->state
),
599 prefix
, socket_result_to_string(s
->result
),
600 prefix
, socket_result_to_string(s
->clean_result
),
601 prefix
, socket_address_bind_ipv6_only_to_string(s
->bind_ipv6_only
),
603 prefix
, s
->socket_mode
,
604 prefix
, s
->directory_mode
,
605 prefix
, yes_no(s
->keep_alive
),
606 prefix
, yes_no(s
->no_delay
),
607 prefix
, yes_no(s
->free_bind
),
608 prefix
, yes_no(s
->transparent
),
609 prefix
, yes_no(s
->broadcast
),
610 prefix
, yes_no(s
->pass_cred
),
611 prefix
, yes_no(s
->pass_sec
),
612 prefix
, yes_no(s
->pass_pktinfo
),
613 prefix
, strna(s
->tcp_congestion
),
614 prefix
, yes_no(s
->remove_on_stop
),
615 prefix
, yes_no(s
->writable
),
616 prefix
, socket_fdname(s
),
617 prefix
, yes_no(s
->selinux_context_from_net
));
619 if (s
->timestamping
!= SOCKET_TIMESTAMPING_OFF
)
621 "%sTimestamping: %s\n",
622 prefix
, socket_timestamping_to_string(s
->timestamping
));
624 if (s
->control_pid
> 0)
626 "%sControl PID: "PID_FMT
"\n",
627 prefix
, s
->control_pid
);
629 if (s
->bind_to_device
)
631 "%sBindToDevice: %s\n",
632 prefix
, s
->bind_to_device
);
637 "%sNConnections: %u\n"
638 "%sMaxConnections: %u\n"
639 "%sMaxConnectionsPerSource: %u\n",
640 prefix
, s
->n_accepted
,
641 prefix
, s
->n_connections
,
642 prefix
, s
->max_connections
,
643 prefix
, s
->max_connections_per_source
);
646 "%sFlushPending: %s\n",
647 prefix
, yes_no(s
->flush_pending
));
650 if (s
->priority
>= 0)
653 prefix
, s
->priority
);
655 if (s
->receive_buffer
> 0)
657 "%sReceiveBuffer: %zu\n",
658 prefix
, s
->receive_buffer
);
660 if (s
->send_buffer
> 0)
662 "%sSendBuffer: %zu\n",
663 prefix
, s
->send_buffer
);
675 if (s
->pipe_size
> 0)
678 prefix
, s
->pipe_size
);
685 if (s
->mq_maxmsg
> 0)
687 "%sMessageQueueMaxMessages: %li\n",
688 prefix
, s
->mq_maxmsg
);
690 if (s
->mq_msgsize
> 0)
692 "%sMessageQueueMessageSize: %li\n",
693 prefix
, s
->mq_msgsize
);
698 prefix
, yes_no(s
->reuse_port
));
702 "%sSmackLabel: %s\n",
707 "%sSmackLabelIPIn: %s\n",
708 prefix
, s
->smack_ip_in
);
712 "%sSmackLabelIPOut: %s\n",
713 prefix
, s
->smack_ip_out
);
715 if (!isempty(s
->user
) || !isempty(s
->group
))
718 "%sSocketGroup: %s\n",
719 prefix
, strna(s
->user
),
720 prefix
, strna(s
->group
));
722 if (s
->keep_alive_time
> 0)
724 "%sKeepAliveTimeSec: %s\n",
725 prefix
, FORMAT_TIMESPAN(s
->keep_alive_time
, USEC_PER_SEC
));
727 if (s
->keep_alive_interval
> 0)
729 "%sKeepAliveIntervalSec: %s\n",
730 prefix
, FORMAT_TIMESPAN(s
->keep_alive_interval
, USEC_PER_SEC
));
732 if (s
->keep_alive_cnt
> 0)
734 "%sKeepAliveProbes: %u\n",
735 prefix
, s
->keep_alive_cnt
);
737 if (s
->defer_accept
> 0)
739 "%sDeferAcceptSec: %s\n",
740 prefix
, FORMAT_TIMESPAN(s
->defer_accept
, USEC_PER_SEC
));
742 LIST_FOREACH(port
, p
, s
->ports
) {
745 case SOCKET_SOCKET
: {
746 _cleanup_free_
char *k
= NULL
;
750 r
= socket_address_print(&p
->address
, &k
);
752 t
= strerror_safe(r
);
756 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(socket_address_family(&p
->address
), p
->address
.type
), t
);
760 fprintf(f
, "%sListenSpecial: %s\n", prefix
, p
->path
);
762 case SOCKET_USB_FUNCTION
:
763 fprintf(f
, "%sListenUSBFunction: %s\n", prefix
, p
->path
);
766 fprintf(f
, "%sListenMessageQueue: %s\n", prefix
, p
->path
);
769 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
774 "%sTriggerLimitIntervalSec: %s\n"
775 "%sTriggerLimitBurst: %u\n",
776 prefix
, FORMAT_TIMESPAN(s
->trigger_limit
.interval
, USEC_PER_SEC
),
777 prefix
, s
->trigger_limit
.burst
);
779 str
= ip_protocol_to_name(s
->socket_protocol
);
781 fprintf(f
, "%sSocketProtocol: %s\n", prefix
, str
);
783 if (!strv_isempty(s
->symlinks
)) {
786 fprintf(f
, "%sSymlinks:", prefix
);
787 STRV_FOREACH(q
, s
->symlinks
)
788 fprintf(f
, " %s", *q
);
794 "%sTimeoutSec: %s\n",
795 prefix
, FORMAT_TIMESPAN(s
->timeout_usec
, USEC_PER_SEC
));
797 exec_context_dump(&s
->exec_context
, f
, prefix
);
798 kill_context_dump(&s
->kill_context
, f
, prefix
);
800 for (SocketExecCommand c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
801 if (!s
->exec_command
[c
])
804 fprintf(f
, "%s-> %s:\n",
805 prefix
, socket_exec_command_to_string(c
));
807 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
810 cgroup_context_dump(UNIT(s
), f
, prefix
);
813 static int instance_from_socket(int fd
, unsigned nr
, char **instance
) {
816 union sockaddr_union local
, remote
;
822 if (getsockname(fd
, &local
.sa
, &l
) < 0)
826 if (getpeername(fd
, &remote
.sa
, &l
) < 0)
829 switch (local
.sa
.sa_family
) {
833 a
= be32toh(local
.in
.sin_addr
.s_addr
),
834 b
= be32toh(remote
.in
.sin_addr
.s_addr
);
837 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
839 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
840 be16toh(local
.in
.sin_port
),
841 b
>> 24, (b
>> 16) & 0xFF, (b
>> 8) & 0xFF, b
& 0xFF,
842 be16toh(remote
.in
.sin_port
)) < 0)
849 static const unsigned char ipv4_prefix
[] = {
850 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
853 if (memcmp(&local
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0 &&
854 memcmp(&remote
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
856 *a
= local
.in6
.sin6_addr
.s6_addr
+12,
857 *b
= remote
.in6
.sin6_addr
.s6_addr
+12;
860 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
862 a
[0], a
[1], a
[2], a
[3],
863 be16toh(local
.in6
.sin6_port
),
864 b
[0], b
[1], b
[2], b
[3],
865 be16toh(remote
.in6
.sin6_port
)) < 0)
868 char a
[INET6_ADDRSTRLEN
], b
[INET6_ADDRSTRLEN
];
873 inet_ntop(AF_INET6
, &local
.in6
.sin6_addr
, a
, sizeof(a
)),
874 be16toh(local
.in6
.sin6_port
),
875 inet_ntop(AF_INET6
, &remote
.in6
.sin6_addr
, b
, sizeof(b
)),
876 be16toh(remote
.in6
.sin6_port
)) < 0)
887 k
= getpeercred(fd
, &ucred
);
890 "%u-"PID_FMT
"-"UID_FMT
,
891 nr
, ucred
.pid
, ucred
.uid
) < 0)
893 } else if (k
== -ENODATA
) {
894 /* This handles the case where somebody is
895 * connecting from another pid/uid namespace
896 * (e.g. from outside of our container). */
911 local
.vm
.svm_cid
, local
.vm
.svm_port
,
912 remote
.vm
.svm_cid
, remote
.vm
.svm_port
) < 0)
918 assert_not_reached();
925 static void socket_close_fds(Socket
*s
) {
931 LIST_FOREACH(port
, p
, s
->ports
) {
934 was_open
= p
->fd
>= 0;
936 p
->event_source
= sd_event_source_disable_unref(p
->event_source
);
937 p
->fd
= safe_close(p
->fd
);
938 socket_cleanup_fd_list(p
);
940 /* One little note: we should normally not delete any sockets in the file system here! After all some
941 * other process we spawned might still have a reference of this fd and wants to continue to use
942 * it. Therefore we normally delete sockets in the file system before we create a new one, not after we
943 * stopped using one! That all said, if the user explicitly requested this, we'll delete them here
944 * anyway, but only then. */
946 if (!was_open
|| !s
->remove_on_stop
)
952 (void) unlink(p
->path
);
956 (void) mq_unlink(p
->path
);
960 (void) socket_address_unlink(&p
->address
);
968 if (s
->remove_on_stop
)
969 STRV_FOREACH(i
, s
->symlinks
)
972 /* Note that we don't return NULL here, since s has not been freed. */
975 static void socket_apply_socket_options(Socket
*s
, SocketPort
*p
, int fd
) {
983 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_KEEPALIVE
, true);
985 log_unit_warning_errno(UNIT(s
), r
, "SO_KEEPALIVE failed: %m");
988 if (s
->keep_alive_time
> 0) {
989 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPIDLE
, s
->keep_alive_time
/ USEC_PER_SEC
);
991 log_unit_warning_errno(UNIT(s
), r
, "TCP_KEEPIDLE failed: %m");
994 if (s
->keep_alive_interval
> 0) {
995 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPINTVL
, s
->keep_alive_interval
/ USEC_PER_SEC
);
997 log_unit_warning_errno(UNIT(s
), r
, "TCP_KEEPINTVL failed: %m");
1000 if (s
->keep_alive_cnt
> 0) {
1001 r
= setsockopt_int(fd
, SOL_TCP
, TCP_KEEPCNT
, s
->keep_alive_cnt
);
1003 log_unit_warning_errno(UNIT(s
), r
, "TCP_KEEPCNT failed: %m");
1006 if (s
->defer_accept
> 0) {
1007 r
= setsockopt_int(fd
, SOL_TCP
, TCP_DEFER_ACCEPT
, s
->defer_accept
/ USEC_PER_SEC
);
1009 log_unit_warning_errno(UNIT(s
), r
, "TCP_DEFER_ACCEPT failed: %m");
1013 if (s
->socket_protocol
== IPPROTO_SCTP
) {
1014 r
= setsockopt_int(fd
, SOL_SCTP
, SCTP_NODELAY
, true);
1016 log_unit_warning_errno(UNIT(s
), r
, "SCTP_NODELAY failed: %m");
1018 r
= setsockopt_int(fd
, SOL_TCP
, TCP_NODELAY
, true);
1020 log_unit_warning_errno(UNIT(s
), r
, "TCP_NODELAY failed: %m");
1025 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_BROADCAST
, true);
1027 log_unit_warning_errno(UNIT(s
), r
, "SO_BROADCAST failed: %m");
1031 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSCRED
, true);
1033 log_unit_warning_errno(UNIT(s
), r
, "SO_PASSCRED failed: %m");
1037 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PASSSEC
, true);
1039 log_unit_warning_errno(UNIT(s
), r
, "SO_PASSSEC failed: %m");
1042 if (s
->pass_pktinfo
) {
1043 r
= socket_set_recvpktinfo(fd
, socket_address_family(&p
->address
), true);
1045 log_unit_warning_errno(UNIT(s
), r
, "Failed to enable packet info socket option: %m");
1048 if (s
->timestamping
!= SOCKET_TIMESTAMPING_OFF
) {
1049 r
= setsockopt_int(fd
, SOL_SOCKET
,
1050 s
->timestamping
== SOCKET_TIMESTAMPING_NS
? SO_TIMESTAMPNS
: SO_TIMESTAMP
,
1053 log_unit_warning_errno(UNIT(s
), r
, "Failed to enable timestamping socket option, ignoring: %m");
1056 if (s
->priority
>= 0) {
1057 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_PRIORITY
, s
->priority
);
1059 log_unit_warning_errno(UNIT(s
), r
, "SO_PRIORITY failed: %m");
1062 if (s
->receive_buffer
> 0) {
1063 r
= fd_set_rcvbuf(fd
, s
->receive_buffer
, false);
1065 log_unit_full_errno(UNIT(s
), ERRNO_IS_PRIVILEGE(r
) ? LOG_DEBUG
: LOG_WARNING
, r
,
1066 "SO_RCVBUF/SO_RCVBUFFORCE failed: %m");
1069 if (s
->send_buffer
> 0) {
1070 r
= fd_set_sndbuf(fd
, s
->send_buffer
, false);
1072 log_unit_full_errno(UNIT(s
), ERRNO_IS_PRIVILEGE(r
) ? LOG_DEBUG
: LOG_WARNING
, r
,
1073 "SO_SNDBUF/SO_SNDBUFFORCE failed: %m");
1077 r
= setsockopt_int(fd
, SOL_SOCKET
, SO_MARK
, s
->mark
);
1079 log_unit_warning_errno(UNIT(s
), r
, "SO_MARK failed: %m");
1082 if (s
->ip_tos
>= 0) {
1083 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_TOS
, s
->ip_tos
);
1085 log_unit_warning_errno(UNIT(s
), r
, "IP_TOS failed: %m");
1088 if (s
->ip_ttl
>= 0) {
1089 r
= socket_set_ttl(fd
, socket_address_family(&p
->address
), s
->ip_ttl
);
1091 log_unit_warning_errno(UNIT(s
), r
, "IP_TTL/IPV6_UNICAST_HOPS failed: %m");
1094 if (s
->tcp_congestion
)
1095 if (setsockopt(fd
, SOL_TCP
, TCP_CONGESTION
, s
->tcp_congestion
, strlen(s
->tcp_congestion
)+1) < 0)
1096 log_unit_warning_errno(UNIT(s
), errno
, "TCP_CONGESTION failed: %m");
1098 if (s
->smack_ip_in
) {
1099 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_IPIN
, s
->smack_ip_in
);
1101 log_unit_error_errno(UNIT(s
), r
, "mac_smack_apply_ip_in_fd: %m");
1104 if (s
->smack_ip_out
) {
1105 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_IPOUT
, s
->smack_ip_out
);
1107 log_unit_error_errno(UNIT(s
), r
, "mac_smack_apply_ip_out_fd: %m");
1111 static void socket_apply_fifo_options(Socket
*s
, int fd
) {
1117 if (s
->pipe_size
> 0)
1118 if (fcntl(fd
, F_SETPIPE_SZ
, s
->pipe_size
) < 0)
1119 log_unit_warning_errno(UNIT(s
), errno
, "Setting pipe size failed, ignoring: %m");
1122 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_ACCESS
, s
->smack
);
1124 log_unit_error_errno(UNIT(s
), r
, "SMACK relabelling failed, ignoring: %m");
1128 static int fifo_address_create(
1130 mode_t directory_mode
,
1131 mode_t socket_mode
) {
1133 _cleanup_close_
int fd
= -1;
1140 (void) mkdir_parents_label(path
, directory_mode
);
1142 r
= mac_selinux_create_file_prepare(path
, S_IFIFO
);
1146 /* Enforce the right access mode for the fifo */
1147 old_mask
= umask(~socket_mode
);
1149 /* Include the original umask in our mask */
1150 (void) umask(~socket_mode
| old_mask
);
1152 r
= mkfifo(path
, socket_mode
);
1153 (void) umask(old_mask
);
1155 if (r
< 0 && errno
!= EEXIST
) {
1160 fd
= open(path
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
| O_NOFOLLOW
);
1166 mac_selinux_create_file_clear();
1168 if (fstat(fd
, &st
) < 0) {
1173 if (!S_ISFIFO(st
.st_mode
) ||
1174 (st
.st_mode
& 0777) != (socket_mode
& ~old_mask
) ||
1175 st
.st_uid
!= getuid() ||
1176 st
.st_gid
!= getgid()) {
1184 mac_selinux_create_file_clear();
1188 static int special_address_create(const char *path
, bool writable
) {
1189 _cleanup_close_
int fd
= -1;
1194 fd
= open(path
, (writable
? O_RDWR
: O_RDONLY
)|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
);
1198 if (fstat(fd
, &st
) < 0)
1201 /* Check whether this is a /proc, /sys or /dev file or char device */
1202 if (!S_ISREG(st
.st_mode
) && !S_ISCHR(st
.st_mode
))
1208 static int usbffs_address_create(const char *path
) {
1209 _cleanup_close_
int fd
= -1;
1214 fd
= open(path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
);
1218 if (fstat(fd
, &st
) < 0)
1221 /* Check whether this is a regular file (ffs endpoint) */
1222 if (!S_ISREG(st
.st_mode
))
1228 static int mq_address_create(
1234 _cleanup_close_
int fd
= -1;
1237 struct mq_attr _attr
, *attr
= NULL
;
1241 if (maxmsg
> 0 && msgsize
> 0) {
1242 _attr
= (struct mq_attr
) {
1243 .mq_flags
= O_NONBLOCK
,
1244 .mq_maxmsg
= maxmsg
,
1245 .mq_msgsize
= msgsize
,
1250 /* Enforce the right access mode for the mq */
1251 old_mask
= umask(~mq_mode
);
1253 /* Include the original umask in our mask */
1254 (void) umask(~mq_mode
| old_mask
);
1255 fd
= mq_open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_CREAT
, mq_mode
, attr
);
1256 (void) umask(old_mask
);
1261 if (fstat(fd
, &st
) < 0)
1264 if ((st
.st_mode
& 0777) != (mq_mode
& ~old_mask
) ||
1265 st
.st_uid
!= getuid() ||
1266 st
.st_gid
!= getgid())
1272 static int socket_symlink(Socket
*s
) {
1279 p
= socket_find_symlink_target(s
);
1283 STRV_FOREACH(i
, s
->symlinks
) {
1284 (void) mkdir_parents_label(*i
, s
->directory_mode
);
1286 r
= symlink_idempotent(p
, *i
, false);
1288 if (r
== -EEXIST
&& s
->remove_on_stop
) {
1289 /* If there's already something where we want to create the symlink, and the destructive
1290 * RemoveOnStop= mode is set, then we might as well try to remove what already exists and try
1293 if (unlink(*i
) >= 0)
1294 r
= symlink_idempotent(p
, *i
, false);
1298 log_unit_warning_errno(UNIT(s
), r
, "Failed to create symlink %s → %s, ignoring: %m", p
, *i
);
1304 static int usbffs_write_descs(int fd
, Service
*s
) {
1307 if (!s
->usb_function_descriptors
|| !s
->usb_function_strings
)
1310 r
= copy_file_fd(s
->usb_function_descriptors
, fd
, 0);
1314 return copy_file_fd(s
->usb_function_strings
, fd
, 0);
1317 static int usbffs_select_ep(const struct dirent
*d
) {
1318 return d
->d_name
[0] != '.' && !streq(d
->d_name
, "ep0");
1321 static int usbffs_dispatch_eps(SocketPort
*p
) {
1322 _cleanup_free_
struct dirent
**ent
= NULL
;
1326 r
= scandir(p
->path
, &ent
, usbffs_select_ep
, alphasort
);
1331 p
->auxiliary_fds
= new(int, n
);
1332 if (!p
->auxiliary_fds
) {
1337 p
->n_auxiliary_fds
= n
;
1340 for (size_t i
= 0; i
< n
; ++i
) {
1341 _cleanup_free_
char *ep
= NULL
;
1343 ep
= path_make_absolute(ent
[i
]->d_name
, p
->path
);
1351 r
= usbffs_address_create(ep
);
1355 p
->auxiliary_fds
[k
++] = r
;
1362 close_many(p
->auxiliary_fds
, k
);
1363 p
->auxiliary_fds
= mfree(p
->auxiliary_fds
);
1364 p
->n_auxiliary_fds
= 0;
1367 for (size_t i
= 0; i
< n
; ++i
)
1373 int socket_load_service_unit(Socket
*s
, int cfd
, Unit
**ret
) {
1374 /* Figure out what the unit that will be used to handle the connections on the socket looks like.
1376 * If cfd < 0, then we don't have a connection yet. In case of Accept=yes sockets, use a fake
1380 if (UNIT_ISSET(s
->service
)) {
1381 *ret
= UNIT_DEREF(s
->service
);
1388 /* Build the instance name and load the unit */
1389 _cleanup_free_
char *prefix
= NULL
, *instance
= NULL
, *name
= NULL
;
1392 r
= unit_name_to_prefix(UNIT(s
)->id
, &prefix
);
1397 r
= instance_from_socket(cfd
, s
->n_accepted
, &instance
);
1398 if (ERRNO_IS_DISCONNECT(r
))
1399 /* ENOTCONN is legitimate if TCP RST was received. Other socket families might return
1400 * different errors. This connection is over, but the socket unit lives on. */
1401 return log_unit_debug_errno(UNIT(s
), r
,
1402 "Got %s on incoming socket, assuming aborted connection attempt, ignoring.",
1408 /* For accepting sockets, we don't know how the instance will be called until we get a connection and
1409 * can figure out what the peer name is. So let's use "internal" as the instance to make it clear
1410 * that this is not an actual peer name. We use "unknown" when we cannot figure out the peer. */
1411 r
= unit_name_build(prefix
, instance
?: "internal", ".service", &name
);
1415 return manager_load_unit(UNIT(s
)->manager
, name
, NULL
, NULL
, ret
);
1418 static int socket_determine_selinux_label(Socket
*s
, char **ret
) {
1424 if (s
->selinux_context_from_net
) {
1425 /* If this is requested, get the label from the network label */
1427 r
= mac_selinux_get_our_label(ret
);
1428 if (r
== -EOPNOTSUPP
)
1432 /* Otherwise, get it from the executable we are about to start. */
1436 _cleanup_free_
char *path
= NULL
;
1438 r
= socket_load_service_unit(s
, -1, &service
);
1444 c
= SERVICE(service
)->exec_command
[SERVICE_EXEC_START
];
1448 r
= chase_symlinks(c
->path
, SERVICE(service
)->exec_context
.root_directory
, CHASE_PREFIX_ROOT
, &path
, NULL
);
1452 r
= mac_selinux_get_create_label_from_exe(path
, ret
);
1453 if (IN_SET(r
, -EPERM
, -EOPNOTSUPP
))
1464 static int socket_address_listen_do(
1466 const SocketAddress
*address
,
1467 const char *label
) {
1472 return socket_address_listen(
1474 SOCK_CLOEXEC
|SOCK_NONBLOCK
,
1486 #define log_address_error_errno(u, address, error, fmt) \
1488 _cleanup_free_ char *_t = NULL; \
1490 (void) socket_address_print(address, &_t); \
1491 log_unit_error_errno(u, error, fmt, strna(_t)); \
1494 static int fork_needed(const SocketAddress
*address
, const ExecContext
*context
) {
1500 /* Check if we need to do the cgroup or netns stuff. If not we can do things much simpler. */
1502 if (IN_SET(address
->sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1503 r
= bpf_firewall_supported();
1506 if (r
!= BPF_FIREWALL_UNSUPPORTED
) /* If BPF firewalling isn't supported anyway — there's no point in this forking complexity */
1510 return context
->private_network
|| context
->network_namespace_path
;
1513 static int socket_address_listen_in_cgroup(
1515 const SocketAddress
*address
,
1516 const char *label
) {
1518 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1525 /* This is a wrapper around socket_address_listen(), that forks off a helper process inside the
1526 * socket's cgroup and network namespace in which the socket is actually created. This way we ensure
1527 * the socket is actually properly attached to the unit's cgroup for the purpose of BPF filtering and
1530 r
= fork_needed(address
, &s
->exec_context
);
1534 /* Shortcut things... */
1535 fd
= socket_address_listen_do(s
, address
, label
);
1537 return log_address_error_errno(UNIT(s
), address
, fd
, "Failed to create listening socket (%s): %m");
1542 r
= unit_setup_exec_runtime(UNIT(s
));
1544 return log_unit_error_errno(UNIT(s
), r
, "Failed acquire runtime: %m");
1546 if (s
->exec_context
.network_namespace_path
&&
1548 s
->exec_runtime
->netns_storage_socket
[0] >= 0) {
1549 r
= open_shareable_ns_path(s
->exec_runtime
->netns_storage_socket
, s
->exec_context
.network_namespace_path
, CLONE_NEWNET
);
1551 return log_unit_error_errno(UNIT(s
), r
, "Failed to open network namespace path %s: %m", s
->exec_context
.network_namespace_path
);
1554 if (s
->exec_context
.ipc_namespace_path
&&
1556 s
->exec_runtime
->ipcns_storage_socket
[0] >= 0) {
1557 r
= open_shareable_ns_path(s
->exec_runtime
->ipcns_storage_socket
, s
->exec_context
.ipc_namespace_path
, CLONE_NEWIPC
);
1559 return log_unit_error_errno(UNIT(s
), r
, "Failed to open IPC namespace path %s: %m", s
->exec_context
.ipc_namespace_path
);
1562 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
1563 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
1565 r
= unit_fork_helper_process(UNIT(s
), "(sd-listen)", &pid
);
1567 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off listener stub process: %m");
1571 pair
[0] = safe_close(pair
[0]);
1573 if ((s
->exec_context
.private_network
|| s
->exec_context
.network_namespace_path
) &&
1575 s
->exec_runtime
->netns_storage_socket
[0] >= 0) {
1577 if (ns_type_supported(NAMESPACE_NET
)) {
1578 r
= setup_shareable_ns(s
->exec_runtime
->netns_storage_socket
, CLONE_NEWNET
);
1580 log_unit_error_errno(UNIT(s
), r
, "Failed to join network namespace: %m");
1581 _exit(EXIT_NETWORK
);
1583 } else if (s
->exec_context
.network_namespace_path
) {
1584 log_unit_error(UNIT(s
), "Network namespace path configured but network namespaces not supported.");
1585 _exit(EXIT_NETWORK
);
1587 log_unit_warning(UNIT(s
), "PrivateNetwork=yes is configured, but the kernel does not support network namespaces, ignoring.");
1590 fd
= socket_address_listen_do(s
, address
, label
);
1592 log_address_error_errno(UNIT(s
), address
, fd
, "Failed to create listening socket (%s): %m");
1593 _exit(EXIT_FAILURE
);
1596 r
= send_one_fd(pair
[1], fd
, 0);
1598 log_address_error_errno(UNIT(s
), address
, r
, "Failed to send listening socket (%s) to parent: %m");
1599 _exit(EXIT_FAILURE
);
1602 _exit(EXIT_SUCCESS
);
1605 pair
[1] = safe_close(pair
[1]);
1606 fd
= receive_one_fd(pair
[0], 0);
1608 /* We synchronously wait for the helper, as it shouldn't be slow */
1609 r
= wait_for_terminate_and_check("(sd-listen)", pid
, WAIT_LOG_ABNORMAL
);
1616 return log_address_error_errno(UNIT(s
), address
, fd
, "Failed to receive listening socket (%s): %m");
1621 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(Socket
*, socket_close_fds
, NULL
);
1623 static int socket_open_fds(Socket
*orig_s
) {
1624 _cleanup_(socket_close_fdsp
) Socket
*s
= orig_s
;
1625 _cleanup_(mac_selinux_freep
) char *label
= NULL
;
1626 bool know_label
= false;
1632 LIST_FOREACH(port
, p
, s
->ports
) {
1642 /* Figure out the label, if we don't it know yet. We do it once for the first
1643 * socket where we need this and remember it for the rest. */
1645 r
= socket_determine_selinux_label(s
, &label
);
1647 return log_unit_error_errno(UNIT(s
), r
, "Failed to determine SELinux label: %m");
1652 /* Apply the socket protocol */
1653 switch (p
->address
.type
) {
1656 case SOCK_SEQPACKET
:
1657 if (s
->socket_protocol
== IPPROTO_SCTP
)
1658 p
->address
.protocol
= s
->socket_protocol
;
1662 if (s
->socket_protocol
== IPPROTO_UDPLITE
)
1663 p
->address
.protocol
= s
->socket_protocol
;
1667 p
->fd
= socket_address_listen_in_cgroup(s
, &p
->address
, label
);
1671 socket_apply_socket_options(s
, p
, p
->fd
);
1675 case SOCKET_SPECIAL
:
1677 p
->fd
= special_address_create(p
->path
, s
->writable
);
1679 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open special file %s: %m", p
->path
);
1684 p
->fd
= fifo_address_create(
1689 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open FIFO %s: %m", p
->path
);
1691 socket_apply_fifo_options(s
, p
->fd
);
1697 p
->fd
= mq_address_create(
1703 return log_unit_error_errno(UNIT(s
), p
->fd
, "Failed to open message queue %s: %m", p
->path
);
1706 case SOCKET_USB_FUNCTION
: {
1707 _cleanup_free_
char *ep
= NULL
;
1709 ep
= path_make_absolute("ep0", p
->path
);
1713 p
->fd
= usbffs_address_create(ep
);
1717 r
= usbffs_write_descs(p
->fd
, SERVICE(UNIT_DEREF(s
->service
)));
1721 r
= usbffs_dispatch_eps(p
);
1728 assert_not_reached();
1736 static void socket_unwatch_fds(Socket
*s
) {
1742 LIST_FOREACH(port
, p
, s
->ports
) {
1746 if (!p
->event_source
)
1749 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_OFF
);
1751 log_unit_debug_errno(UNIT(s
), r
, "Failed to disable event source: %m");
1755 static int socket_watch_fds(Socket
*s
) {
1761 LIST_FOREACH(port
, p
, s
->ports
) {
1765 if (p
->event_source
) {
1766 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_ON
);
1770 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &p
->event_source
, p
->fd
, EPOLLIN
, socket_dispatch_io
, p
);
1774 (void) sd_event_source_set_description(p
->event_source
, "socket-port-io");
1781 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch listening fds: %m");
1782 socket_unwatch_fds(s
);
1792 static int socket_check_open(Socket
*s
) {
1793 bool have_open
= false, have_closed
= false;
1798 LIST_FOREACH(port
, p
, s
->ports
) {
1804 if (have_open
&& have_closed
)
1805 return SOCKET_OPEN_SOME
;
1809 return SOCKET_OPEN_ALL
;
1811 return SOCKET_OPEN_NONE
;
1814 static void socket_set_state(Socket
*s
, SocketState state
) {
1815 SocketState old_state
;
1818 if (s
->state
!= state
)
1819 bus_unit_send_pending_change_signal(UNIT(s
), false);
1821 old_state
= s
->state
;
1829 SOCKET_STOP_PRE_SIGTERM
,
1830 SOCKET_STOP_PRE_SIGKILL
,
1832 SOCKET_FINAL_SIGTERM
,
1833 SOCKET_FINAL_SIGKILL
,
1836 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
1837 socket_unwatch_control_pid(s
);
1838 s
->control_command
= NULL
;
1839 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1842 if (state
!= SOCKET_LISTENING
)
1843 socket_unwatch_fds(s
);
1851 SOCKET_STOP_PRE_SIGTERM
,
1852 SOCKET_STOP_PRE_SIGKILL
,
1854 socket_close_fds(s
);
1856 if (state
!= old_state
)
1857 log_unit_debug(UNIT(s
), "Changed %s -> %s", socket_state_to_string(old_state
), socket_state_to_string(state
));
1859 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
1862 static int socket_coldplug(Unit
*u
) {
1863 Socket
*s
= SOCKET(u
);
1867 assert(s
->state
== SOCKET_DEAD
);
1869 if (s
->deserialized_state
== s
->state
)
1872 if (s
->control_pid
> 0 &&
1873 pid_is_unwaited(s
->control_pid
) &&
1874 IN_SET(s
->deserialized_state
,
1879 SOCKET_STOP_PRE_SIGTERM
,
1880 SOCKET_STOP_PRE_SIGKILL
,
1882 SOCKET_FINAL_SIGTERM
,
1883 SOCKET_FINAL_SIGKILL
,
1886 r
= unit_watch_pid(UNIT(s
), s
->control_pid
, false);
1890 r
= socket_arm_timer(s
, usec_add(u
->state_change_timestamp
.monotonic
, s
->timeout_usec
));
1895 if (IN_SET(s
->deserialized_state
,
1901 /* Originally, we used to simply reopen all sockets here that we didn't have file descriptors
1902 * for. However, this is problematic, as we won't traverse through the SOCKET_START_CHOWN state for
1903 * them, and thus the UID/GID wouldn't be right. Hence, instead simply check if we have all fds open,
1904 * and if there's a mismatch, warn loudly. */
1906 r
= socket_check_open(s
);
1907 if (r
== SOCKET_OPEN_NONE
)
1908 log_unit_warning(UNIT(s
),
1909 "Socket unit configuration has changed while unit has been running, "
1910 "no open socket file descriptor left. "
1911 "The socket unit is not functional until restarted.");
1912 else if (r
== SOCKET_OPEN_SOME
)
1913 log_unit_warning(UNIT(s
),
1914 "Socket unit configuration has changed while unit has been running, "
1915 "and some socket file descriptors have not been opened yet. "
1916 "The socket unit is not fully functional until restarted.");
1919 if (s
->deserialized_state
== SOCKET_LISTENING
) {
1920 r
= socket_watch_fds(s
);
1925 if (!IN_SET(s
->deserialized_state
, SOCKET_DEAD
, SOCKET_FAILED
, SOCKET_CLEANING
)) {
1926 (void) unit_setup_dynamic_creds(u
);
1927 (void) unit_setup_exec_runtime(u
);
1930 socket_set_state(s
, s
->deserialized_state
);
1934 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
1936 _cleanup_(exec_params_clear
) ExecParameters exec_params
= {
1937 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
1950 r
= unit_prepare_exec(UNIT(s
));
1954 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
1958 r
= unit_set_exec_params(UNIT(s
), &exec_params
);
1962 r
= exec_spawn(UNIT(s
),
1972 r
= unit_watch_pid(UNIT(s
), pid
, true);
1981 static int socket_chown(Socket
*s
, pid_t
*_pid
) {
1985 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
1989 /* We have to resolve the user names out-of-process, hence
1990 * let's fork here. It's messy, but well, what can we do? */
1992 r
= unit_fork_helper_process(UNIT(s
), "(sd-chown)", &pid
);
1996 uid_t uid
= UID_INVALID
;
1997 gid_t gid
= GID_INVALID
;
2002 if (!isempty(s
->user
)) {
2003 const char *user
= s
->user
;
2005 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
, 0);
2007 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve user %s: %m", user
);
2012 if (!isempty(s
->group
)) {
2013 const char *group
= s
->group
;
2015 r
= get_group_creds(&group
, &gid
, 0);
2017 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve group %s: %m", group
);
2022 LIST_FOREACH(port
, p
, s
->ports
) {
2023 const char *path
= NULL
;
2025 if (p
->type
== SOCKET_SOCKET
)
2026 path
= socket_address_get_path(&p
->address
);
2027 else if (p
->type
== SOCKET_FIFO
)
2033 if (chown(path
, uid
, gid
) < 0) {
2034 log_unit_error_errno(UNIT(s
), errno
, "Failed to chown(): %m");
2039 _exit(EXIT_SUCCESS
);
2042 r
= unit_watch_pid(UNIT(s
), pid
, true);
2050 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
2054 static void socket_enter_dead(Socket
*s
, SocketResult f
) {
2057 if (s
->result
== SOCKET_SUCCESS
)
2060 if (s
->result
== SOCKET_SUCCESS
)
2061 unit_log_success(UNIT(s
));
2063 unit_log_failure(UNIT(s
), socket_result_to_string(s
->result
));
2065 unit_warn_leftover_processes(UNIT(s
), unit_log_leftover_process_stop
);
2067 socket_set_state(s
, s
->result
!= SOCKET_SUCCESS
? SOCKET_FAILED
: SOCKET_DEAD
);
2069 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
, true);
2071 unit_destroy_runtime_data(UNIT(s
), &s
->exec_context
);
2073 unit_unref_uid_gid(UNIT(s
), true);
2075 dynamic_creds_destroy(&s
->dynamic_creds
);
2078 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
);
2080 static void socket_enter_stop_post(Socket
*s
, SocketResult f
) {
2084 if (s
->result
== SOCKET_SUCCESS
)
2087 socket_unwatch_control_pid(s
);
2088 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
2089 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
];
2091 if (s
->control_command
) {
2092 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2096 socket_set_state(s
, SOCKET_STOP_POST
);
2098 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_SUCCESS
);
2103 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
2104 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2107 static int state_to_kill_operation(Socket
*s
, SocketState state
) {
2108 if (state
== SOCKET_STOP_PRE_SIGTERM
&& unit_has_job_type(UNIT(s
), JOB_RESTART
))
2109 return KILL_RESTART
;
2111 if (state
== SOCKET_FINAL_SIGTERM
)
2112 return KILL_TERMINATE
;
2117 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
) {
2122 if (s
->result
== SOCKET_SUCCESS
)
2125 r
= unit_kill_context(
2128 state_to_kill_operation(s
, state
),
2136 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
2140 socket_set_state(s
, state
);
2141 } else if (state
== SOCKET_STOP_PRE_SIGTERM
)
2142 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_SUCCESS
);
2143 else if (state
== SOCKET_STOP_PRE_SIGKILL
)
2144 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2145 else if (state
== SOCKET_FINAL_SIGTERM
)
2146 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_SUCCESS
);
2148 socket_enter_dead(s
, SOCKET_SUCCESS
);
2153 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
2155 if (IN_SET(state
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_STOP_PRE_SIGKILL
))
2156 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2158 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2161 static void socket_enter_stop_pre(Socket
*s
, SocketResult f
) {
2165 if (s
->result
== SOCKET_SUCCESS
)
2168 socket_unwatch_control_pid(s
);
2169 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
2170 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
];
2172 if (s
->control_command
) {
2173 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2177 socket_set_state(s
, SOCKET_STOP_PRE
);
2179 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2184 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-pre' task: %m");
2185 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2188 static void socket_enter_listening(Socket
*s
) {
2192 if (!s
->accept
&& s
->flush_pending
) {
2193 log_unit_debug(UNIT(s
), "Flushing socket before listening.");
2197 r
= socket_watch_fds(s
);
2199 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch sockets: %m");
2203 socket_set_state(s
, SOCKET_LISTENING
);
2207 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2210 static void socket_enter_start_post(Socket
*s
) {
2214 socket_unwatch_control_pid(s
);
2215 s
->control_command_id
= SOCKET_EXEC_START_POST
;
2216 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
];
2218 if (s
->control_command
) {
2219 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2221 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
2225 socket_set_state(s
, SOCKET_START_POST
);
2227 socket_enter_listening(s
);
2232 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2235 static void socket_enter_start_chown(Socket
*s
) {
2240 r
= socket_open_fds(s
);
2242 log_unit_warning_errno(UNIT(s
), r
, "Failed to listen on sockets: %m");
2246 if (!isempty(s
->user
) || !isempty(s
->group
)) {
2248 socket_unwatch_control_pid(s
);
2249 s
->control_command_id
= SOCKET_EXEC_START_CHOWN
;
2250 s
->control_command
= NULL
;
2252 r
= socket_chown(s
, &s
->control_pid
);
2254 log_unit_warning_errno(UNIT(s
), r
, "Failed to fork 'start-chown' task: %m");
2258 socket_set_state(s
, SOCKET_START_CHOWN
);
2260 socket_enter_start_post(s
);
2265 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2268 static void socket_enter_start_pre(Socket
*s
) {
2272 socket_unwatch_control_pid(s
);
2274 unit_warn_leftover_processes(UNIT(s
), unit_log_leftover_process_start
);
2276 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
2277 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
];
2279 if (s
->control_command
) {
2280 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2282 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
2286 socket_set_state(s
, SOCKET_START_PRE
);
2288 socket_enter_start_chown(s
);
2293 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2296 static void flush_ports(Socket
*s
) {
2299 /* Flush all incoming traffic, regardless if actual bytes or new connections, so that this socket isn't busy
2302 LIST_FOREACH(port
, p
, s
->ports
) {
2306 (void) flush_accept(p
->fd
);
2307 (void) flush_fd(p
->fd
);
2311 static void socket_enter_running(Socket
*s
, int cfd_in
) {
2312 /* Note that this call takes possession of the connection fd passed. It either has to assign it
2313 * somewhere or close it. */
2314 _cleanup_close_
int cfd
= cfd_in
;
2316 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2321 /* We don't take connections anymore if we are supposed to shut down anyway */
2322 if (unit_stop_pending(UNIT(s
))) {
2324 log_unit_debug(UNIT(s
), "Suppressing connection request since unit stop is scheduled.");
2333 if (!ratelimit_below(&s
->trigger_limit
)) {
2334 log_unit_warning(UNIT(s
), "Trigger limit hit, refusing further activation.");
2335 socket_enter_stop_pre(s
, SOCKET_FAILURE_TRIGGER_LIMIT_HIT
);
2340 bool pending
= false;
2343 /* If there's already a start pending don't bother to do anything */
2344 UNIT_FOREACH_DEPENDENCY(other
, UNIT(s
), UNIT_ATOM_TRIGGERS
)
2345 if (unit_active_or_pending(other
)) {
2351 if (!UNIT_ISSET(s
->service
)) {
2352 r
= log_unit_error_errno(UNIT(s
), SYNTHETIC_ERRNO(ENOENT
),
2353 "Service to activate vanished, refusing activation.");
2357 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT_DEREF(s
->service
), JOB_REPLACE
, NULL
, &error
, NULL
);
2362 socket_set_state(s
, SOCKET_RUNNING
);
2364 _cleanup_(socket_peer_unrefp
) SocketPeer
*p
= NULL
;
2367 if (s
->n_connections
>= s
->max_connections
) {
2368 log_unit_warning(UNIT(s
), "Too many incoming connections (%u), dropping connection.",
2373 if (s
->max_connections_per_source
> 0) {
2374 r
= socket_acquire_peer(s
, cfd
, &p
);
2375 if (ERRNO_IS_DISCONNECT(r
))
2377 if (r
< 0) /* We didn't have enough resources to acquire peer information, let's fail. */
2379 if (r
> 0 && p
->n_ref
> s
->max_connections_per_source
) {
2380 _cleanup_free_
char *t
= NULL
;
2382 (void) sockaddr_pretty(&p
->peer
.sa
, p
->peer_salen
, true, false, &t
);
2384 log_unit_warning(UNIT(s
),
2385 "Too many incoming connections (%u) from source %s, dropping connection.",
2386 p
->n_ref
, strnull(t
));
2391 r
= socket_load_service_unit(s
, cfd
, &service
);
2392 if (ERRNO_IS_DISCONNECT(r
))
2397 r
= unit_add_two_dependencies(UNIT(s
), UNIT_BEFORE
, UNIT_TRIGGERS
, service
,
2398 false, UNIT_DEPENDENCY_IMPLICIT
);
2404 r
= service_set_socket_fd(SERVICE(service
), cfd
, s
, p
, s
->selinux_context_from_net
);
2405 if (ERRNO_IS_DISCONNECT(r
))
2410 TAKE_FD(cfd
); /* We passed ownership of the fd to the service now. Forget it here. */
2413 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, service
, JOB_REPLACE
, NULL
, &error
, NULL
);
2415 /* We failed to activate the new service, but it still exists. Let's make sure the
2416 * service closes and forgets the connection fd again, immediately. */
2417 service_close_socket_fd(SERVICE(service
));
2421 /* Notify clients about changed counters */
2422 unit_add_to_dbus_queue(UNIT(s
));
2433 if (ERRNO_IS_RESOURCE(r
))
2434 log_unit_warning(UNIT(s
), "Failed to queue service startup job: %s",
2435 bus_error_message(&error
, r
));
2437 log_unit_warning(UNIT(s
), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
2438 cfd
>= 0 ? "template" : "non-template",
2439 bus_error_message(&error
, r
));
2441 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2444 static void socket_run_next(Socket
*s
) {
2448 assert(s
->control_command
);
2449 assert(s
->control_command
->command_next
);
2451 socket_unwatch_control_pid(s
);
2453 s
->control_command
= s
->control_command
->command_next
;
2455 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2462 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next task: %m");
2464 if (s
->state
== SOCKET_START_POST
)
2465 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2466 else if (s
->state
== SOCKET_STOP_POST
)
2467 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2469 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2472 static int socket_start(Unit
*u
) {
2473 Socket
*s
= SOCKET(u
);
2478 /* We cannot fulfill this request right now, try again later
2480 if (IN_SET(s
->state
,
2482 SOCKET_STOP_PRE_SIGKILL
,
2483 SOCKET_STOP_PRE_SIGTERM
,
2485 SOCKET_FINAL_SIGTERM
,
2486 SOCKET_FINAL_SIGKILL
,
2490 /* Already on it! */
2491 if (IN_SET(s
->state
,
2497 /* Cannot run this without the service being around */
2498 if (UNIT_ISSET(s
->service
)) {
2501 service
= SERVICE(UNIT_DEREF(s
->service
));
2503 if (UNIT(service
)->load_state
!= UNIT_LOADED
)
2504 return log_unit_error_errno(u
, SYNTHETIC_ERRNO(ENOENT
), "Socket service %s not loaded, refusing.", UNIT(service
)->id
);
2506 /* If the service is already active we cannot start the
2508 if (!IN_SET(service
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
2509 return log_unit_error_errno(u
, SYNTHETIC_ERRNO(EBUSY
), "Socket service %s already active, refusing.", UNIT(service
)->id
);
2512 assert(IN_SET(s
->state
, SOCKET_DEAD
, SOCKET_FAILED
));
2514 r
= unit_acquire_invocation_id(u
);
2518 s
->result
= SOCKET_SUCCESS
;
2519 exec_command_reset_status_list_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
2521 u
->reset_accounting
= true;
2523 socket_enter_start_pre(s
);
2527 static int socket_stop(Unit
*u
) {
2528 Socket
*s
= SOCKET(u
);
2533 if (IN_SET(s
->state
,
2535 SOCKET_STOP_PRE_SIGTERM
,
2536 SOCKET_STOP_PRE_SIGKILL
,
2538 SOCKET_FINAL_SIGTERM
,
2539 SOCKET_FINAL_SIGKILL
))
2542 /* If there's already something running we go directly into
2544 if (IN_SET(s
->state
,
2547 SOCKET_START_POST
)) {
2548 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_SUCCESS
);
2552 /* If we are currently cleaning, then abort it, brutally. */
2553 if (s
->state
== SOCKET_CLEANING
) {
2554 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_SUCCESS
);
2558 assert(IN_SET(s
->state
, SOCKET_LISTENING
, SOCKET_RUNNING
));
2560 socket_enter_stop_pre(s
, SOCKET_SUCCESS
);
2564 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2565 Socket
*s
= SOCKET(u
);
2573 (void) serialize_item(f
, "state", socket_state_to_string(s
->state
));
2574 (void) serialize_item(f
, "result", socket_result_to_string(s
->result
));
2575 (void) serialize_item_format(f
, "n-accepted", "%u", s
->n_accepted
);
2576 (void) serialize_item_format(f
, "n-refused", "%u", s
->n_refused
);
2578 if (s
->control_pid
> 0)
2579 (void) serialize_item_format(f
, "control-pid", PID_FMT
, s
->control_pid
);
2581 if (s
->control_command_id
>= 0)
2582 (void) serialize_item(f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
2584 LIST_FOREACH(port
, p
, s
->ports
) {
2590 copy
= fdset_put_dup(fds
, p
->fd
);
2592 return log_unit_warning_errno(u
, copy
, "Failed to serialize socket fd: %m");
2594 if (p
->type
== SOCKET_SOCKET
) {
2595 _cleanup_free_
char *t
= NULL
;
2597 r
= socket_address_print(&p
->address
, &t
);
2599 return log_unit_error_errno(u
, r
, "Failed to format socket address: %m");
2601 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2602 (void) serialize_item_format(f
, "netlink", "%i %s", copy
, t
);
2604 (void) serialize_item_format(f
, "socket", "%i %i %s", copy
, p
->address
.type
, t
);
2605 } else if (p
->type
== SOCKET_SPECIAL
)
2606 (void) serialize_item_format(f
, "special", "%i %s", copy
, p
->path
);
2607 else if (p
->type
== SOCKET_MQUEUE
)
2608 (void) serialize_item_format(f
, "mqueue", "%i %s", copy
, p
->path
);
2609 else if (p
->type
== SOCKET_USB_FUNCTION
)
2610 (void) serialize_item_format(f
, "ffs", "%i %s", copy
, p
->path
);
2612 assert(p
->type
== SOCKET_FIFO
);
2613 (void) serialize_item_format(f
, "fifo", "%i %s", copy
, p
->path
);
2620 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2621 Socket
*s
= SOCKET(u
);
2627 if (streq(key
, "state")) {
2630 state
= socket_state_from_string(value
);
2632 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2634 s
->deserialized_state
= state
;
2635 } else if (streq(key
, "result")) {
2638 f
= socket_result_from_string(value
);
2640 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2641 else if (f
!= SOCKET_SUCCESS
)
2644 } else if (streq(key
, "n-accepted")) {
2647 if (safe_atou(value
, &k
) < 0)
2648 log_unit_debug(u
, "Failed to parse n-accepted value: %s", value
);
2651 } else if (streq(key
, "n-refused")) {
2654 if (safe_atou(value
, &k
) < 0)
2655 log_unit_debug(u
, "Failed to parse n-refused value: %s", value
);
2658 } else if (streq(key
, "control-pid")) {
2661 if (parse_pid(value
, &pid
) < 0)
2662 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2664 s
->control_pid
= pid
;
2665 } else if (streq(key
, "control-command")) {
2666 SocketExecCommand id
;
2668 id
= socket_exec_command_from_string(value
);
2670 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2672 s
->control_command_id
= id
;
2673 s
->control_command
= s
->exec_command
[id
];
2675 } else if (streq(key
, "fifo")) {
2679 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2680 log_unit_debug(u
, "Failed to parse fifo value: %s", value
);
2684 LIST_FOREACH(port
, p
, s
->ports
)
2686 p
->type
== SOCKET_FIFO
&&
2687 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2688 p
->fd
= fdset_remove(fds
, fd
);
2693 log_unit_debug(u
, "No matching fifo socket found: %s", value
+skip
);
2696 } else if (streq(key
, "special")) {
2700 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2701 log_unit_debug(u
, "Failed to parse special value: %s", value
);
2705 LIST_FOREACH(port
, p
, s
->ports
)
2707 p
->type
== SOCKET_SPECIAL
&&
2708 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2709 p
->fd
= fdset_remove(fds
, fd
);
2714 log_unit_debug(u
, "No matching special socket found: %s", value
+skip
);
2717 } else if (streq(key
, "mqueue")) {
2721 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2722 log_unit_debug(u
, "Failed to parse mqueue value: %s", value
);
2726 LIST_FOREACH(port
, p
, s
->ports
)
2728 p
->type
== SOCKET_MQUEUE
&&
2729 streq(p
->path
, value
+skip
)) {
2730 p
->fd
= fdset_remove(fds
, fd
);
2735 log_unit_debug(u
, "No matching mqueue socket found: %s", value
+skip
);
2738 } else if (streq(key
, "socket")) {
2739 int fd
, type
, skip
= 0;
2742 if (sscanf(value
, "%i %i %n", &fd
, &type
, &skip
) < 2 || fd
< 0 || type
< 0 || !fdset_contains(fds
, fd
))
2743 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2747 LIST_FOREACH(port
, p
, s
->ports
)
2749 socket_address_is(&p
->address
, value
+skip
, type
)) {
2750 p
->fd
= fdset_remove(fds
, fd
);
2755 log_unit_debug(u
, "No matching %s socket found: %s",
2756 socket_address_type_to_string(type
), value
+skip
);
2759 } else if (streq(key
, "netlink")) {
2763 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2764 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2768 LIST_FOREACH(port
, p
, s
->ports
)
2770 socket_address_is_netlink(&p
->address
, value
+skip
)) {
2771 p
->fd
= fdset_remove(fds
, fd
);
2776 log_unit_debug(u
, "No matching netlink socket found: %s", value
+skip
);
2779 } else if (streq(key
, "ffs")) {
2783 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2784 log_unit_debug(u
, "Failed to parse ffs value: %s", value
);
2788 LIST_FOREACH(port
, p
, s
->ports
)
2790 p
->type
== SOCKET_USB_FUNCTION
&&
2791 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2792 p
->fd
= fdset_remove(fds
, fd
);
2797 log_unit_debug(u
, "No matching ffs socket found: %s", value
+skip
);
2801 log_unit_debug(UNIT(s
), "Unknown serialization key: %s", key
);
2806 static void socket_distribute_fds(Unit
*u
, FDSet
*fds
) {
2807 Socket
*s
= SOCKET(u
);
2812 LIST_FOREACH(port
, p
, s
->ports
) {
2815 if (p
->type
!= SOCKET_SOCKET
)
2821 FDSET_FOREACH(fd
, fds
) {
2822 if (socket_address_matches_fd(&p
->address
, fd
)) {
2823 p
->fd
= fdset_remove(fds
, fd
);
2824 s
->deserialized_state
= SOCKET_LISTENING
;
2831 _pure_
static UnitActiveState
socket_active_state(Unit
*u
) {
2834 return state_translation_table
[SOCKET(u
)->state
];
2837 _pure_
static const char *socket_sub_state_to_string(Unit
*u
) {
2840 return socket_state_to_string(SOCKET(u
)->state
);
2843 const char* socket_port_type_to_string(SocketPort
*p
) {
2851 switch (p
->address
.type
) {
2859 case SOCK_SEQPACKET
:
2860 return "SequentialPacket";
2863 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2871 case SOCKET_SPECIAL
:
2875 return "MessageQueue";
2880 case SOCKET_USB_FUNCTION
:
2881 return "USBFunction";
2888 SocketType
socket_port_type_from_string(const char *s
) {
2891 if (STR_IN_SET(s
, "Stream", "Datagram", "SequentialPacket", "Netlink"))
2892 return SOCKET_SOCKET
;
2893 else if (streq(s
, "Special"))
2894 return SOCKET_SPECIAL
;
2895 else if (streq(s
, "MessageQueue"))
2896 return SOCKET_MQUEUE
;
2897 else if (streq(s
, "FIFO"))
2899 else if (streq(s
, "USBFunction"))
2900 return SOCKET_USB_FUNCTION
;
2902 return _SOCKET_TYPE_INVALID
;
2905 _pure_
static bool socket_may_gc(Unit
*u
) {
2906 Socket
*s
= SOCKET(u
);
2910 return s
->n_connections
== 0;
2913 static int socket_accept_do(Socket
*s
, int fd
) {
2919 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
2921 /* Convert transient network errors into clean and well-defined EAGAIN */
2922 return ERRNO_IS_ACCEPT_AGAIN(errno
) ? -EAGAIN
: -errno
;
2927 static int socket_accept_in_cgroup(Socket
*s
, SocketPort
*p
, int fd
) {
2928 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
2936 /* Similar to socket_address_listen_in_cgroup(), but for accept() rather than socket(): make sure that any
2937 * connection socket is also properly associated with the cgroup. */
2939 if (!IN_SET(p
->address
.sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
2942 r
= bpf_firewall_supported();
2945 if (r
== BPF_FIREWALL_UNSUPPORTED
)
2948 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
2949 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
2951 r
= unit_fork_helper_process(UNIT(s
), "(sd-accept)", &pid
);
2953 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off accept stub process: %m");
2957 pair
[0] = safe_close(pair
[0]);
2959 cfd
= socket_accept_do(s
, fd
);
2960 if (cfd
== -EAGAIN
) /* spurious accept() */
2961 _exit(EXIT_SUCCESS
);
2963 log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
2964 _exit(EXIT_FAILURE
);
2967 r
= send_one_fd(pair
[1], cfd
, 0);
2969 log_unit_error_errno(UNIT(s
), r
, "Failed to send connection socket to parent: %m");
2970 _exit(EXIT_FAILURE
);
2973 _exit(EXIT_SUCCESS
);
2976 pair
[1] = safe_close(pair
[1]);
2977 cfd
= receive_one_fd(pair
[0], 0);
2979 /* We synchronously wait for the helper, as it shouldn't be slow */
2980 r
= wait_for_terminate_and_check("(sd-accept)", pid
, WAIT_LOG_ABNORMAL
);
2986 /* If we received no fd, we got EIO here. If this happens with a process exit code of EXIT_SUCCESS
2987 * this is a spurious accept(), let's convert that back to EAGAIN here. */
2991 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to receive connection socket: %m");
2996 cfd
= socket_accept_do(s
, fd
);
2997 if (cfd
== -EAGAIN
) /* spurious accept(), skip it silently */
3000 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
3005 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
3006 SocketPort
*p
= userdata
;
3012 if (p
->socket
->state
!= SOCKET_LISTENING
)
3015 log_unit_debug(UNIT(p
->socket
), "Incoming traffic");
3017 if (revents
!= EPOLLIN
) {
3018 if (revents
& EPOLLHUP
)
3019 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.");
3021 log_unit_error(UNIT(p
->socket
), "Got unexpected poll event (0x%x) on socket.", revents
);
3025 if (p
->socket
->accept
&&
3026 p
->type
== SOCKET_SOCKET
&&
3027 socket_address_can_accept(&p
->address
)) {
3029 cfd
= socket_accept_in_cgroup(p
->socket
, p
, fd
);
3030 if (cfd
== -EAGAIN
) /* Spurious accept() */
3035 socket_apply_socket_options(p
->socket
, p
, cfd
);
3038 socket_enter_running(p
->socket
, cfd
);
3042 socket_enter_stop_pre(p
->socket
, SOCKET_FAILURE_RESOURCES
);
3046 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
3047 Socket
*s
= SOCKET(u
);
3053 if (pid
!= s
->control_pid
)
3058 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
3060 else if (code
== CLD_EXITED
)
3061 f
= SOCKET_FAILURE_EXIT_CODE
;
3062 else if (code
== CLD_KILLED
)
3063 f
= SOCKET_FAILURE_SIGNAL
;
3064 else if (code
== CLD_DUMPED
)
3065 f
= SOCKET_FAILURE_CORE_DUMP
;
3067 assert_not_reached();
3069 if (s
->control_command
) {
3070 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
3072 if (s
->control_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
3076 unit_log_process_exit(
3079 socket_exec_command_to_string(s
->control_command_id
),
3080 f
== SOCKET_SUCCESS
,
3083 if (s
->result
== SOCKET_SUCCESS
)
3086 if (s
->control_command
&&
3087 s
->control_command
->command_next
&&
3088 f
== SOCKET_SUCCESS
) {
3090 log_unit_debug(u
, "Running next command for state %s", socket_state_to_string(s
->state
));
3093 s
->control_command
= NULL
;
3094 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
3096 /* No further commands for this step, so let's figure
3097 * out what to do next */
3099 log_unit_debug(u
, "Got final SIGCHLD for state %s", socket_state_to_string(s
->state
));
3103 case SOCKET_START_PRE
:
3104 if (f
== SOCKET_SUCCESS
)
3105 socket_enter_start_chown(s
);
3107 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, f
);
3110 case SOCKET_START_CHOWN
:
3111 if (f
== SOCKET_SUCCESS
)
3112 socket_enter_start_post(s
);
3114 socket_enter_stop_pre(s
, f
);
3117 case SOCKET_START_POST
:
3118 if (f
== SOCKET_SUCCESS
)
3119 socket_enter_listening(s
);
3121 socket_enter_stop_pre(s
, f
);
3124 case SOCKET_STOP_PRE
:
3125 case SOCKET_STOP_PRE_SIGTERM
:
3126 case SOCKET_STOP_PRE_SIGKILL
:
3127 socket_enter_stop_post(s
, f
);
3130 case SOCKET_STOP_POST
:
3131 case SOCKET_FINAL_SIGTERM
:
3132 case SOCKET_FINAL_SIGKILL
:
3133 socket_enter_dead(s
, f
);
3136 case SOCKET_CLEANING
:
3138 if (s
->clean_result
== SOCKET_SUCCESS
)
3139 s
->clean_result
= f
;
3141 socket_enter_dead(s
, SOCKET_SUCCESS
);
3145 assert_not_reached();
3149 /* Notify clients about changed exit status */
3150 unit_add_to_dbus_queue(u
);
3153 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
3154 Socket
*s
= SOCKET(userdata
);
3157 assert(s
->timer_event_source
== source
);
3161 case SOCKET_START_PRE
:
3162 log_unit_warning(UNIT(s
), "Starting timed out. Terminating.");
3163 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3166 case SOCKET_START_CHOWN
:
3167 case SOCKET_START_POST
:
3168 log_unit_warning(UNIT(s
), "Starting timed out. Stopping.");
3169 socket_enter_stop_pre(s
, SOCKET_FAILURE_TIMEOUT
);
3172 case SOCKET_STOP_PRE
:
3173 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
3174 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3177 case SOCKET_STOP_PRE_SIGTERM
:
3178 if (s
->kill_context
.send_sigkill
) {
3179 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
3180 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3182 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL. Ignoring.");
3183 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3187 case SOCKET_STOP_PRE_SIGKILL
:
3188 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
3189 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3192 case SOCKET_STOP_POST
:
3193 log_unit_warning(UNIT(s
), "Stopping timed out (2). Terminating.");
3194 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3197 case SOCKET_FINAL_SIGTERM
:
3198 if (s
->kill_context
.send_sigkill
) {
3199 log_unit_warning(UNIT(s
), "Stopping timed out (2). Killing.");
3200 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3202 log_unit_warning(UNIT(s
), "Stopping timed out (2). Skipping SIGKILL. Ignoring.");
3203 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3207 case SOCKET_FINAL_SIGKILL
:
3208 log_unit_warning(UNIT(s
), "Still around after SIGKILL (2). Entering failed mode.");
3209 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3212 case SOCKET_CLEANING
:
3213 log_unit_warning(UNIT(s
), "Cleaning timed out. killing.");
3215 if (s
->clean_result
== SOCKET_SUCCESS
)
3216 s
->clean_result
= SOCKET_FAILURE_TIMEOUT
;
3218 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, 0);
3222 assert_not_reached();
3228 int socket_collect_fds(Socket
*s
, int **fds
) {
3229 size_t k
= 0, n
= 0;
3236 /* Called from the service code for requesting our fds */
3238 LIST_FOREACH(port
, p
, s
->ports
) {
3241 n
+= p
->n_auxiliary_fds
;
3253 LIST_FOREACH(port
, p
, s
->ports
) {
3256 for (size_t i
= 0; i
< p
->n_auxiliary_fds
; ++i
)
3257 rfds
[k
++] = p
->auxiliary_fds
[i
];
3266 static void socket_reset_failed(Unit
*u
) {
3267 Socket
*s
= SOCKET(u
);
3271 if (s
->state
== SOCKET_FAILED
)
3272 socket_set_state(s
, SOCKET_DEAD
);
3274 s
->result
= SOCKET_SUCCESS
;
3275 s
->clean_result
= SOCKET_SUCCESS
;
3278 void socket_connection_unref(Socket
*s
) {
3281 /* The service is dead. Yay!
3283 * This is strictly for one-instance-per-connection
3286 assert(s
->n_connections
> 0);
3289 log_unit_debug(UNIT(s
), "One connection closed, %u left.", s
->n_connections
);
3292 static void socket_trigger_notify(Unit
*u
, Unit
*other
) {
3293 Socket
*s
= SOCKET(u
);
3298 /* Filter out invocations with bogus state */
3299 assert(UNIT_IS_LOAD_COMPLETE(other
->load_state
));
3300 assert(other
->type
== UNIT_SERVICE
);
3302 /* Don't propagate state changes from the service if we are already down */
3303 if (!IN_SET(s
->state
, SOCKET_RUNNING
, SOCKET_LISTENING
))
3306 /* We don't care for the service state if we are in Accept=yes mode */
3310 /* Propagate start limit hit state */
3311 if (other
->start_limit_hit
) {
3312 socket_enter_stop_pre(s
, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
);
3316 /* Don't propagate anything if there's still a job queued */
3320 if (IN_SET(SERVICE(other
)->state
,
3321 SERVICE_DEAD
, SERVICE_FAILED
,
3322 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
3323 SERVICE_AUTO_RESTART
))
3324 socket_enter_listening(s
);
3326 if (SERVICE(other
)->state
== SERVICE_RUNNING
)
3327 socket_set_state(s
, SOCKET_RUNNING
);
3330 static int socket_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3331 return unit_kill_common(u
, who
, signo
, -1, SOCKET(u
)->control_pid
, error
);
3334 static int socket_get_timeout(Unit
*u
, usec_t
*timeout
) {
3335 Socket
*s
= SOCKET(u
);
3339 if (!s
->timer_event_source
)
3342 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3345 if (t
== USEC_INFINITY
)
3352 char *socket_fdname(Socket
*s
) {
3355 /* Returns the name to use for $LISTEN_NAMES. If the user
3356 * didn't specify anything specifically, use the socket unit's
3357 * name as fallback. */
3359 return s
->fdname
?: UNIT(s
)->id
;
3362 static int socket_control_pid(Unit
*u
) {
3363 Socket
*s
= SOCKET(u
);
3367 return s
->control_pid
;
3370 static int socket_clean(Unit
*u
, ExecCleanMask mask
) {
3371 _cleanup_strv_free_
char **l
= NULL
;
3372 Socket
*s
= SOCKET(u
);
3378 if (s
->state
!= SOCKET_DEAD
)
3381 r
= exec_context_get_clean_directories(&s
->exec_context
, u
->manager
->prefix
, mask
, &l
);
3385 if (strv_isempty(l
))
3388 socket_unwatch_control_pid(s
);
3389 s
->clean_result
= SOCKET_SUCCESS
;
3390 s
->control_command
= NULL
;
3391 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
3393 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->exec_context
.timeout_clean_usec
));
3397 r
= unit_fork_and_watch_rm_rf(u
, l
, &s
->control_pid
);
3401 socket_set_state(s
, SOCKET_CLEANING
);
3406 log_unit_warning_errno(u
, r
, "Failed to initiate cleaning: %m");
3407 s
->clean_result
= SOCKET_FAILURE_RESOURCES
;
3408 s
->timer_event_source
= sd_event_source_disable_unref(s
->timer_event_source
);
3412 static int socket_can_clean(Unit
*u
, ExecCleanMask
*ret
) {
3413 Socket
*s
= SOCKET(u
);
3417 return exec_context_get_clean_mask(&s
->exec_context
, ret
);
3420 static int socket_can_start(Unit
*u
) {
3421 Socket
*s
= SOCKET(u
);
3426 r
= unit_test_start_limit(u
);
3428 socket_enter_dead(s
, SOCKET_FAILURE_START_LIMIT_HIT
);
3435 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
3436 [SOCKET_EXEC_START_PRE
] = "ExecStartPre",
3437 [SOCKET_EXEC_START_CHOWN
] = "ExecStartChown",
3438 [SOCKET_EXEC_START_POST
] = "ExecStartPost",
3439 [SOCKET_EXEC_STOP_PRE
] = "ExecStopPre",
3440 [SOCKET_EXEC_STOP_POST
] = "ExecStopPost"
3443 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
3445 static const char* const socket_result_table
[_SOCKET_RESULT_MAX
] = {
3446 [SOCKET_SUCCESS
] = "success",
3447 [SOCKET_FAILURE_RESOURCES
] = "resources",
3448 [SOCKET_FAILURE_TIMEOUT
] = "timeout",
3449 [SOCKET_FAILURE_EXIT_CODE
] = "exit-code",
3450 [SOCKET_FAILURE_SIGNAL
] = "signal",
3451 [SOCKET_FAILURE_CORE_DUMP
] = "core-dump",
3452 [SOCKET_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
3453 [SOCKET_FAILURE_TRIGGER_LIMIT_HIT
] = "trigger-limit-hit",
3454 [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
] = "service-start-limit-hit"
3457 DEFINE_STRING_TABLE_LOOKUP(socket_result
, SocketResult
);
3459 static const char* const socket_timestamping_table
[_SOCKET_TIMESTAMPING_MAX
] = {
3460 [SOCKET_TIMESTAMPING_OFF
] = "off",
3461 [SOCKET_TIMESTAMPING_US
] = "us",
3462 [SOCKET_TIMESTAMPING_NS
] = "ns",
3465 DEFINE_STRING_TABLE_LOOKUP(socket_timestamping
, SocketTimestamping
);
3467 SocketTimestamping
socket_timestamping_from_string_harder(const char *p
) {
3468 SocketTimestamping t
;
3472 return _SOCKET_TIMESTAMPING_INVALID
;
3474 t
= socket_timestamping_from_string(p
);
3478 /* Let's alternatively support the various other aliases parse_time() accepts for ns and µs here,
3480 if (streq(p
, "nsec"))
3481 return SOCKET_TIMESTAMPING_NS
;
3482 if (STR_IN_SET(p
, "usec", "µs"))
3483 return SOCKET_TIMESTAMPING_US
;
3485 r
= parse_boolean(p
);
3487 return _SOCKET_TIMESTAMPING_INVALID
;
3489 return r
? SOCKET_TIMESTAMPING_NS
: SOCKET_TIMESTAMPING_OFF
; /* If boolean yes, default to ns accuracy */
3492 const UnitVTable socket_vtable
= {
3493 .object_size
= sizeof(Socket
),
3494 .exec_context_offset
= offsetof(Socket
, exec_context
),
3495 .cgroup_context_offset
= offsetof(Socket
, cgroup_context
),
3496 .kill_context_offset
= offsetof(Socket
, kill_context
),
3497 .exec_runtime_offset
= offsetof(Socket
, exec_runtime
),
3498 .dynamic_creds_offset
= offsetof(Socket
, dynamic_creds
),
3504 .private_section
= "Socket",
3506 .can_transient
= true,
3507 .can_trigger
= true,
3510 .init
= socket_init
,
3511 .done
= socket_done
,
3512 .load
= socket_load
,
3514 .coldplug
= socket_coldplug
,
3516 .dump
= socket_dump
,
3518 .start
= socket_start
,
3519 .stop
= socket_stop
,
3521 .kill
= socket_kill
,
3522 .clean
= socket_clean
,
3523 .can_clean
= socket_can_clean
,
3525 .get_timeout
= socket_get_timeout
,
3527 .serialize
= socket_serialize
,
3528 .deserialize_item
= socket_deserialize_item
,
3529 .distribute_fds
= socket_distribute_fds
,
3531 .active_state
= socket_active_state
,
3532 .sub_state_to_string
= socket_sub_state_to_string
,
3534 .will_restart
= unit_will_restart_default
,
3536 .may_gc
= socket_may_gc
,
3538 .sigchld_event
= socket_sigchld_event
,
3540 .trigger_notify
= socket_trigger_notify
,
3542 .reset_failed
= socket_reset_failed
,
3544 .control_pid
= socket_control_pid
,
3546 .bus_set_property
= bus_socket_set_property
,
3547 .bus_commit_properties
= bus_socket_commit_properties
,
3549 .status_message_formats
= {
3550 .finished_start_job
= {
3551 [JOB_DONE
] = "Listening on %s.",
3552 [JOB_FAILED
] = "Failed to listen on %s.",
3553 [JOB_TIMEOUT
] = "Timed out starting %s.",
3555 .finished_stop_job
= {
3556 [JOB_DONE
] = "Closed %s.",
3557 [JOB_FAILED
] = "Failed stopping %s.",
3558 [JOB_TIMEOUT
] = "Timed out stopping %s.",
3562 .can_start
= socket_can_start
,