2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <arpa/inet.h>
24 #include <netinet/tcp.h>
26 #include <sys/epoll.h>
29 #include <linux/sctp.h>
31 #include "alloc-util.h"
32 #include "bpf-firewall.h"
33 #include "bus-error.h"
36 #include "dbus-socket.h"
38 #include "exit-status.h"
40 #include "format-util.h"
42 #include "in-addr-util.h"
48 #include "parse-util.h"
49 #include "path-util.h"
50 #include "process-util.h"
51 #include "selinux-util.h"
52 #include "signal-util.h"
53 #include "smack-util.h"
56 #include "string-table.h"
57 #include "string-util.h"
59 #include "unit-name.h"
61 #include "user-util.h"
67 union sockaddr_union peer
;
71 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
72 [SOCKET_DEAD
] = UNIT_INACTIVE
,
73 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
74 [SOCKET_START_CHOWN
] = UNIT_ACTIVATING
,
75 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
76 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
77 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
78 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
79 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
80 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
81 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
82 [SOCKET_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
83 [SOCKET_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
84 [SOCKET_FAILED
] = UNIT_FAILED
87 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
);
88 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
90 static void socket_init(Unit
*u
) {
91 Socket
*s
= SOCKET(u
);
94 assert(u
->load_state
== UNIT_STUB
);
96 s
->backlog
= SOMAXCONN
;
97 s
->timeout_usec
= u
->manager
->default_timeout_start_usec
;
98 s
->directory_mode
= 0755;
99 s
->socket_mode
= 0666;
101 s
->max_connections
= 64;
108 s
->exec_context
.std_output
= u
->manager
->default_std_output
;
109 s
->exec_context
.std_error
= u
->manager
->default_std_error
;
111 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
113 s
->trigger_limit
.interval
= USEC_INFINITY
;
114 s
->trigger_limit
.burst
= (unsigned) -1;
117 static void socket_unwatch_control_pid(Socket
*s
) {
120 if (s
->control_pid
<= 0)
123 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
127 static void socket_cleanup_fd_list(SocketPort
*p
) {
130 close_many(p
->auxiliary_fds
, p
->n_auxiliary_fds
);
131 p
->auxiliary_fds
= mfree(p
->auxiliary_fds
);
132 p
->n_auxiliary_fds
= 0;
135 void socket_free_ports(Socket
*s
) {
140 while ((p
= s
->ports
)) {
141 LIST_REMOVE(port
, s
->ports
, p
);
143 sd_event_source_unref(p
->event_source
);
145 socket_cleanup_fd_list(p
);
152 static void socket_done(Unit
*u
) {
153 Socket
*s
= SOCKET(u
);
158 socket_free_ports(s
);
160 while ((p
= set_steal_first(s
->peers_by_address
)))
163 s
->peers_by_address
= set_free(s
->peers_by_address
);
165 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
166 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
167 s
->control_command
= NULL
;
169 dynamic_creds_unref(&s
->dynamic_creds
);
171 socket_unwatch_control_pid(s
);
173 unit_ref_unset(&s
->service
);
175 s
->tcp_congestion
= mfree(s
->tcp_congestion
);
176 s
->bind_to_device
= mfree(s
->bind_to_device
);
178 s
->smack
= mfree(s
->smack
);
179 s
->smack_ip_in
= mfree(s
->smack_ip_in
);
180 s
->smack_ip_out
= mfree(s
->smack_ip_out
);
182 strv_free(s
->symlinks
);
184 s
->user
= mfree(s
->user
);
185 s
->group
= mfree(s
->group
);
187 s
->fdname
= mfree(s
->fdname
);
189 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
192 static int socket_arm_timer(Socket
*s
, usec_t usec
) {
197 if (s
->timer_event_source
) {
198 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
202 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
205 if (usec
== USEC_INFINITY
)
208 r
= sd_event_add_time(
209 UNIT(s
)->manager
->event
,
210 &s
->timer_event_source
,
213 socket_dispatch_timer
, s
);
217 (void) sd_event_source_set_description(s
->timer_event_source
, "socket-timer");
222 int socket_instantiate_service(Socket
*s
) {
223 _cleanup_free_
char *prefix
= NULL
, *name
= NULL
;
229 /* This fills in s->service if it isn't filled in yet. For
230 * Accept=yes sockets we create the next connection service
231 * here. For Accept=no this is mostly a NOP since the service
232 * is figured out at load time anyway. */
234 if (UNIT_DEREF(s
->service
))
240 r
= unit_name_to_prefix(UNIT(s
)->id
, &prefix
);
244 if (asprintf(&name
, "%s@%u.service", prefix
, s
->n_accepted
) < 0)
247 r
= manager_load_unit(UNIT(s
)->manager
, name
, NULL
, NULL
, &u
);
251 unit_ref_set(&s
->service
, u
);
253 return unit_add_two_dependencies(UNIT(s
), UNIT_BEFORE
, UNIT_TRIGGERS
, u
, false, UNIT_DEPENDENCY_IMPLICIT
);
256 static bool have_non_accept_socket(Socket
*s
) {
264 LIST_FOREACH(port
, p
, s
->ports
) {
266 if (p
->type
!= SOCKET_SOCKET
)
269 if (!socket_address_can_accept(&p
->address
))
276 static int socket_add_mount_dependencies(Socket
*s
) {
282 LIST_FOREACH(port
, p
, s
->ports
) {
283 const char *path
= NULL
;
285 if (p
->type
== SOCKET_SOCKET
)
286 path
= socket_address_get_path(&p
->address
);
287 else if (IN_SET(p
->type
, SOCKET_FIFO
, SOCKET_SPECIAL
, SOCKET_USB_FUNCTION
))
293 r
= unit_require_mounts_for(UNIT(s
), path
, UNIT_DEPENDENCY_FILE
);
301 static int socket_add_device_dependencies(Socket
*s
) {
306 if (!s
->bind_to_device
|| streq(s
->bind_to_device
, "lo"))
309 t
= strjoina("/sys/subsystem/net/devices/", s
->bind_to_device
);
310 return unit_add_node_dependency(UNIT(s
), t
, false, UNIT_BINDS_TO
, UNIT_DEPENDENCY_FILE
);
313 static int socket_add_default_dependencies(Socket
*s
) {
317 if (!UNIT(s
)->default_dependencies
)
320 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_BEFORE
, SPECIAL_SOCKETS_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
324 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
325 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
330 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
333 _pure_
static bool socket_has_exec(Socket
*s
) {
337 for (i
= 0; i
< _SOCKET_EXEC_COMMAND_MAX
; i
++)
338 if (s
->exec_command
[i
])
344 static int socket_add_extras(Socket
*s
) {
350 /* Pick defaults for the trigger limit, if nothing was explicitly configured. We pick a relatively high limit
351 * in Accept=yes mode, and a lower limit for Accept=no. Reason: in Accept=yes mode we are invoking accept()
352 * ourselves before the trigger limit can hit, thus incoming connections are taken off the socket queue quickly
353 * and reliably. This is different for Accept=no, where the spawned service has to take the incoming traffic
354 * off the queues, which it might not necessarily do. Moreover, while Accept=no services are supposed to
355 * process whatever is queued in one go, and thus should normally never have to be started frequently. This is
356 * different for Accept=yes where each connection is processed by a new service instance, and thus frequent
357 * service starts are typical. */
359 if (s
->trigger_limit
.interval
== USEC_INFINITY
)
360 s
->trigger_limit
.interval
= 2 * USEC_PER_SEC
;
362 if (s
->trigger_limit
.burst
== (unsigned) -1) {
364 s
->trigger_limit
.burst
= 200;
366 s
->trigger_limit
.burst
= 20;
369 if (have_non_accept_socket(s
)) {
371 if (!UNIT_DEREF(s
->service
)) {
374 r
= unit_load_related_unit(u
, ".service", &x
);
378 unit_ref_set(&s
->service
, x
);
381 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT_DEREF(s
->service
), true, UNIT_DEPENDENCY_IMPLICIT
);
386 r
= socket_add_mount_dependencies(s
);
390 r
= socket_add_device_dependencies(s
);
394 r
= unit_patch_contexts(u
);
398 if (socket_has_exec(s
)) {
399 r
= unit_add_exec_dependencies(u
, &s
->exec_context
);
404 r
= unit_set_default_slice(u
);
408 r
= socket_add_default_dependencies(s
);
415 static const char *socket_find_symlink_target(Socket
*s
) {
416 const char *found
= NULL
;
419 LIST_FOREACH(port
, p
, s
->ports
) {
420 const char *f
= NULL
;
429 f
= socket_address_get_path(&p
->address
);
447 static int socket_verify(Socket
*s
) {
450 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
454 log_unit_error(UNIT(s
), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing.");
458 if (s
->accept
&& have_non_accept_socket(s
)) {
459 log_unit_error(UNIT(s
), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing.");
463 if (s
->accept
&& s
->max_connections
<= 0) {
464 log_unit_error(UNIT(s
), "MaxConnection= setting too small. Refusing.");
468 if (s
->accept
&& UNIT_DEREF(s
->service
)) {
469 log_unit_error(UNIT(s
), "Explicit service configuration for accepting socket units not supported. Refusing.");
473 if (s
->exec_context
.pam_name
&& s
->kill_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
474 log_unit_error(UNIT(s
), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing.");
478 if (!strv_isempty(s
->symlinks
) && !socket_find_symlink_target(s
)) {
479 log_unit_error(UNIT(s
), "Unit has symlinks set but none or more than one node in the file system. Refusing.");
486 static void peer_address_hash_func(const void *p
, struct siphash
*state
) {
487 const SocketPeer
*s
= p
;
491 if (s
->peer
.sa
.sa_family
== AF_INET
)
492 siphash24_compress(&s
->peer
.in
.sin_addr
, sizeof(s
->peer
.in
.sin_addr
), state
);
493 else if (s
->peer
.sa
.sa_family
== AF_INET6
)
494 siphash24_compress(&s
->peer
.in6
.sin6_addr
, sizeof(s
->peer
.in6
.sin6_addr
), state
);
495 else if (s
->peer
.sa
.sa_family
== AF_VSOCK
)
496 siphash24_compress(&s
->peer
.vm
.svm_cid
, sizeof(s
->peer
.vm
.svm_cid
), state
);
498 assert_not_reached("Unknown address family.");
501 static int peer_address_compare_func(const void *a
, const void *b
) {
502 const SocketPeer
*x
= a
, *y
= b
;
504 if (x
->peer
.sa
.sa_family
< y
->peer
.sa
.sa_family
)
506 if (x
->peer
.sa
.sa_family
> y
->peer
.sa
.sa_family
)
509 switch(x
->peer
.sa
.sa_family
) {
511 return memcmp(&x
->peer
.in
.sin_addr
, &y
->peer
.in
.sin_addr
, sizeof(x
->peer
.in
.sin_addr
));
513 return memcmp(&x
->peer
.in6
.sin6_addr
, &y
->peer
.in6
.sin6_addr
, sizeof(x
->peer
.in6
.sin6_addr
));
515 if (x
->peer
.vm
.svm_cid
< y
->peer
.vm
.svm_cid
)
517 if (x
->peer
.vm
.svm_cid
> y
->peer
.vm
.svm_cid
)
521 assert_not_reached("Black sheep in the family!");
524 const struct hash_ops peer_address_hash_ops
= {
525 .hash
= peer_address_hash_func
,
526 .compare
= peer_address_compare_func
529 static int socket_load(Unit
*u
) {
530 Socket
*s
= SOCKET(u
);
534 assert(u
->load_state
== UNIT_STUB
);
536 r
= set_ensure_allocated(&s
->peers_by_address
, &peer_address_hash_ops
);
540 r
= unit_load_fragment_and_dropin(u
);
544 if (u
->load_state
== UNIT_LOADED
) {
545 /* This is a new unit? Then let's add in some extras */
546 r
= socket_add_extras(s
);
551 return socket_verify(s
);
554 static SocketPeer
*socket_peer_new(void) {
557 p
= new0(SocketPeer
, 1);
566 SocketPeer
*socket_peer_ref(SocketPeer
*p
) {
570 assert(p
->n_ref
> 0);
576 SocketPeer
*socket_peer_unref(SocketPeer
*p
) {
580 assert(p
->n_ref
> 0);
588 set_remove(p
->socket
->peers_by_address
, p
);
593 int socket_acquire_peer(Socket
*s
, int fd
, SocketPeer
**p
) {
594 _cleanup_(socket_peer_unrefp
) SocketPeer
*remote
= NULL
;
595 SocketPeer sa
= {}, *i
;
596 socklen_t salen
= sizeof(sa
.peer
);
602 r
= getpeername(fd
, &sa
.peer
.sa
, &salen
);
604 return log_error_errno(errno
, "getpeername failed: %m");
606 if (!IN_SET(sa
.peer
.sa
.sa_family
, AF_INET
, AF_INET6
, AF_VSOCK
)) {
611 i
= set_get(s
->peers_by_address
, &sa
);
613 *p
= socket_peer_ref(i
);
617 remote
= socket_peer_new();
621 remote
->peer
= sa
.peer
;
622 remote
->peer_salen
= salen
;
624 r
= set_put(s
->peers_by_address
, remote
);
636 _const_
static const char* listen_lookup(int family
, int type
) {
638 if (family
== AF_NETLINK
)
639 return "ListenNetlink";
641 if (type
== SOCK_STREAM
)
642 return "ListenStream";
643 else if (type
== SOCK_DGRAM
)
644 return "ListenDatagram";
645 else if (type
== SOCK_SEQPACKET
)
646 return "ListenSequentialPacket";
648 assert_not_reached("Unknown socket type");
652 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
653 char time_string
[FORMAT_TIMESPAN_MAX
];
655 Socket
*s
= SOCKET(u
);
662 prefix
= strempty(prefix
);
663 prefix2
= strjoina(prefix
, "\t");
666 "%sSocket State: %s\n"
668 "%sBindIPv6Only: %s\n"
670 "%sSocketMode: %04o\n"
671 "%sDirectoryMode: %04o\n"
675 "%sTransparent: %s\n"
677 "%sPassCredentials: %s\n"
678 "%sPassSecurity: %s\n"
679 "%sTCPCongestion: %s\n"
680 "%sRemoveOnStop: %s\n"
683 "%sSELinuxContextFromNet: %s\n",
684 prefix
, socket_state_to_string(s
->state
),
685 prefix
, socket_result_to_string(s
->result
),
686 prefix
, socket_address_bind_ipv6_only_to_string(s
->bind_ipv6_only
),
688 prefix
, s
->socket_mode
,
689 prefix
, s
->directory_mode
,
690 prefix
, yes_no(s
->keep_alive
),
691 prefix
, yes_no(s
->no_delay
),
692 prefix
, yes_no(s
->free_bind
),
693 prefix
, yes_no(s
->transparent
),
694 prefix
, yes_no(s
->broadcast
),
695 prefix
, yes_no(s
->pass_cred
),
696 prefix
, yes_no(s
->pass_sec
),
697 prefix
, strna(s
->tcp_congestion
),
698 prefix
, yes_no(s
->remove_on_stop
),
699 prefix
, yes_no(s
->writable
),
700 prefix
, socket_fdname(s
),
701 prefix
, yes_no(s
->selinux_context_from_net
));
703 if (s
->control_pid
> 0)
705 "%sControl PID: "PID_FMT
"\n",
706 prefix
, s
->control_pid
);
708 if (s
->bind_to_device
)
710 "%sBindToDevice: %s\n",
711 prefix
, s
->bind_to_device
);
716 "%sNConnections: %u\n"
717 "%sMaxConnections: %u\n",
718 prefix
, s
->n_accepted
,
719 prefix
, s
->n_connections
,
720 prefix
, s
->max_connections
);
722 if (s
->priority
>= 0)
725 prefix
, s
->priority
);
727 if (s
->receive_buffer
> 0)
729 "%sReceiveBuffer: %zu\n",
730 prefix
, s
->receive_buffer
);
732 if (s
->send_buffer
> 0)
734 "%sSendBuffer: %zu\n",
735 prefix
, s
->send_buffer
);
747 if (s
->pipe_size
> 0)
750 prefix
, s
->pipe_size
);
757 if (s
->mq_maxmsg
> 0)
759 "%sMessageQueueMaxMessages: %li\n",
760 prefix
, s
->mq_maxmsg
);
762 if (s
->mq_msgsize
> 0)
764 "%sMessageQueueMessageSize: %li\n",
765 prefix
, s
->mq_msgsize
);
770 prefix
, yes_no(s
->reuse_port
));
774 "%sSmackLabel: %s\n",
779 "%sSmackLabelIPIn: %s\n",
780 prefix
, s
->smack_ip_in
);
784 "%sSmackLabelIPOut: %s\n",
785 prefix
, s
->smack_ip_out
);
787 if (!isempty(s
->user
) || !isempty(s
->group
))
790 "%sSocketGroup: %s\n",
791 prefix
, strna(s
->user
),
792 prefix
, strna(s
->group
));
794 if (s
->keep_alive_time
> 0)
796 "%sKeepAliveTimeSec: %s\n",
797 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->keep_alive_time
, USEC_PER_SEC
));
799 if (s
->keep_alive_interval
)
801 "%sKeepAliveIntervalSec: %s\n",
802 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->keep_alive_interval
, USEC_PER_SEC
));
804 if (s
->keep_alive_cnt
)
806 "%sKeepAliveProbes: %u\n",
807 prefix
, s
->keep_alive_cnt
);
811 "%sDeferAcceptSec: %s\n",
812 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->defer_accept
, USEC_PER_SEC
));
814 LIST_FOREACH(port
, p
, s
->ports
) {
816 if (p
->type
== SOCKET_SOCKET
) {
821 r
= socket_address_print(&p
->address
, &k
);
827 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(socket_address_family(&p
->address
), p
->address
.type
), t
);
829 } else if (p
->type
== SOCKET_SPECIAL
)
830 fprintf(f
, "%sListenSpecial: %s\n", prefix
, p
->path
);
831 else if (p
->type
== SOCKET_USB_FUNCTION
)
832 fprintf(f
, "%sListenUSBFunction: %s\n", prefix
, p
->path
);
833 else if (p
->type
== SOCKET_MQUEUE
)
834 fprintf(f
, "%sListenMessageQueue: %s\n", prefix
, p
->path
);
836 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
840 "%sTriggerLimitIntervalSec: %s\n"
841 "%sTriggerLimitBurst: %u\n",
842 prefix
, format_timespan(time_string
, FORMAT_TIMESPAN_MAX
, s
->trigger_limit
.interval
, USEC_PER_SEC
),
843 prefix
, s
->trigger_limit
.burst
);
845 exec_context_dump(&s
->exec_context
, f
, prefix
);
846 kill_context_dump(&s
->kill_context
, f
, prefix
);
848 for (c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
849 if (!s
->exec_command
[c
])
852 fprintf(f
, "%s-> %s:\n",
853 prefix
, socket_exec_command_to_string(c
));
855 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
858 cgroup_context_dump(&s
->cgroup_context
, f
, prefix
);
861 static int instance_from_socket(int fd
, unsigned nr
, char **instance
) {
864 union sockaddr_union local
, remote
;
870 if (getsockname(fd
, &local
.sa
, &l
) < 0)
874 if (getpeername(fd
, &remote
.sa
, &l
) < 0)
877 switch (local
.sa
.sa_family
) {
881 a
= be32toh(local
.in
.sin_addr
.s_addr
),
882 b
= be32toh(remote
.in
.sin_addr
.s_addr
);
885 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
887 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
888 be16toh(local
.in
.sin_port
),
889 b
>> 24, (b
>> 16) & 0xFF, (b
>> 8) & 0xFF, b
& 0xFF,
890 be16toh(remote
.in
.sin_port
)) < 0)
897 static const unsigned char ipv4_prefix
[] = {
898 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
901 if (memcmp(&local
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0 &&
902 memcmp(&remote
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
904 *a
= local
.in6
.sin6_addr
.s6_addr
+12,
905 *b
= remote
.in6
.sin6_addr
.s6_addr
+12;
908 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
910 a
[0], a
[1], a
[2], a
[3],
911 be16toh(local
.in6
.sin6_port
),
912 b
[0], b
[1], b
[2], b
[3],
913 be16toh(remote
.in6
.sin6_port
)) < 0)
916 char a
[INET6_ADDRSTRLEN
], b
[INET6_ADDRSTRLEN
];
921 inet_ntop(AF_INET6
, &local
.in6
.sin6_addr
, a
, sizeof(a
)),
922 be16toh(local
.in6
.sin6_port
),
923 inet_ntop(AF_INET6
, &remote
.in6
.sin6_addr
, b
, sizeof(b
)),
924 be16toh(remote
.in6
.sin6_port
)) < 0)
935 k
= getpeercred(fd
, &ucred
);
938 "%u-"PID_FMT
"-"UID_FMT
,
939 nr
, ucred
.pid
, ucred
.uid
) < 0)
941 } else if (k
== -ENODATA
) {
942 /* This handles the case where somebody is
943 * connecting from another pid/uid namespace
944 * (e.g. from outside of our container). */
959 local
.vm
.svm_cid
, local
.vm
.svm_port
,
960 remote
.vm
.svm_cid
, remote
.vm
.svm_port
) < 0)
966 assert_not_reached("Unhandled socket type.");
973 static void socket_close_fds(Socket
*s
) {
979 LIST_FOREACH(port
, p
, s
->ports
) {
982 was_open
= p
->fd
>= 0;
984 p
->event_source
= sd_event_source_unref(p
->event_source
);
985 p
->fd
= safe_close(p
->fd
);
986 socket_cleanup_fd_list(p
);
988 /* One little note: we should normally not delete any sockets in the file system here! After all some
989 * other process we spawned might still have a reference of this fd and wants to continue to use
990 * it. Therefore we normally delete sockets in the file system before we create a new one, not after we
991 * stopped using one! That all said, if the user explicitly requested this, we'll delete them here
992 * anyway, but only then. */
994 if (!was_open
|| !s
->remove_on_stop
)
1000 (void) unlink(p
->path
);
1004 (void) mq_unlink(p
->path
);
1008 (void) socket_address_unlink(&p
->address
);
1016 if (s
->remove_on_stop
)
1017 STRV_FOREACH(i
, s
->symlinks
)
1021 static void socket_apply_socket_options(Socket
*s
, int fd
) {
1027 if (s
->keep_alive
) {
1028 int b
= s
->keep_alive
;
1029 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &b
, sizeof(b
)) < 0)
1030 log_unit_warning_errno(UNIT(s
), errno
, "SO_KEEPALIVE failed: %m");
1033 if (s
->keep_alive_time
) {
1034 int value
= s
->keep_alive_time
/ USEC_PER_SEC
;
1035 if (setsockopt(fd
, SOL_TCP
, TCP_KEEPIDLE
, &value
, sizeof(value
)) < 0)
1036 log_unit_warning_errno(UNIT(s
), errno
, "TCP_KEEPIDLE failed: %m");
1039 if (s
->keep_alive_interval
) {
1040 int value
= s
->keep_alive_interval
/ USEC_PER_SEC
;
1041 if (setsockopt(fd
, SOL_TCP
, TCP_KEEPINTVL
, &value
, sizeof(value
)) < 0)
1042 log_unit_warning_errno(UNIT(s
), errno
, "TCP_KEEPINTVL failed: %m");
1045 if (s
->keep_alive_cnt
) {
1046 int value
= s
->keep_alive_cnt
;
1047 if (setsockopt(fd
, SOL_TCP
, TCP_KEEPCNT
, &value
, sizeof(value
)) < 0)
1048 log_unit_warning_errno(UNIT(s
), errno
, "TCP_KEEPCNT failed: %m");
1051 if (s
->defer_accept
) {
1052 int value
= s
->defer_accept
/ USEC_PER_SEC
;
1053 if (setsockopt(fd
, SOL_TCP
, TCP_DEFER_ACCEPT
, &value
, sizeof(value
)) < 0)
1054 log_unit_warning_errno(UNIT(s
), errno
, "TCP_DEFER_ACCEPT failed: %m");
1058 int b
= s
->no_delay
;
1060 if (s
->socket_protocol
== IPPROTO_SCTP
) {
1061 if (setsockopt(fd
, SOL_SCTP
, SCTP_NODELAY
, &b
, sizeof(b
)) < 0)
1062 log_unit_warning_errno(UNIT(s
), errno
, "SCTP_NODELAY failed: %m");
1064 if (setsockopt(fd
, SOL_TCP
, TCP_NODELAY
, &b
, sizeof(b
)) < 0)
1065 log_unit_warning_errno(UNIT(s
), errno
, "TCP_NODELAY failed: %m");
1071 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &one
, sizeof(one
)) < 0)
1072 log_unit_warning_errno(UNIT(s
), errno
, "SO_BROADCAST failed: %m");
1077 if (setsockopt(fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
)) < 0)
1078 log_unit_warning_errno(UNIT(s
), errno
, "SO_PASSCRED failed: %m");
1083 if (setsockopt(fd
, SOL_SOCKET
, SO_PASSSEC
, &one
, sizeof(one
)) < 0)
1084 log_unit_warning_errno(UNIT(s
), errno
, "SO_PASSSEC failed: %m");
1087 if (s
->priority
>= 0)
1088 if (setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, &s
->priority
, sizeof(s
->priority
)) < 0)
1089 log_unit_warning_errno(UNIT(s
), errno
, "SO_PRIORITY failed: %m");
1091 if (s
->receive_buffer
> 0) {
1092 int value
= (int) s
->receive_buffer
;
1094 /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
1096 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, &value
, sizeof(value
)) < 0)
1097 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &value
, sizeof(value
)) < 0)
1098 log_unit_warning_errno(UNIT(s
), errno
, "SO_RCVBUF failed: %m");
1101 if (s
->send_buffer
> 0) {
1102 int value
= (int) s
->send_buffer
;
1103 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUFFORCE
, &value
, sizeof(value
)) < 0)
1104 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &value
, sizeof(value
)) < 0)
1105 log_unit_warning_errno(UNIT(s
), errno
, "SO_SNDBUF failed: %m");
1109 if (setsockopt(fd
, SOL_SOCKET
, SO_MARK
, &s
->mark
, sizeof(s
->mark
)) < 0)
1110 log_unit_warning_errno(UNIT(s
), errno
, "SO_MARK failed: %m");
1113 if (setsockopt(fd
, IPPROTO_IP
, IP_TOS
, &s
->ip_tos
, sizeof(s
->ip_tos
)) < 0)
1114 log_unit_warning_errno(UNIT(s
), errno
, "IP_TOS failed: %m");
1116 if (s
->ip_ttl
>= 0) {
1119 r
= setsockopt(fd
, IPPROTO_IP
, IP_TTL
, &s
->ip_ttl
, sizeof(s
->ip_ttl
));
1121 if (socket_ipv6_is_supported())
1122 x
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &s
->ip_ttl
, sizeof(s
->ip_ttl
));
1125 errno
= EAFNOSUPPORT
;
1129 log_unit_warning_errno(UNIT(s
), errno
, "IP_TTL/IPV6_UNICAST_HOPS failed: %m");
1132 if (s
->tcp_congestion
)
1133 if (setsockopt(fd
, SOL_TCP
, TCP_CONGESTION
, s
->tcp_congestion
, strlen(s
->tcp_congestion
)+1) < 0)
1134 log_unit_warning_errno(UNIT(s
), errno
, "TCP_CONGESTION failed: %m");
1136 if (s
->smack_ip_in
) {
1137 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_IPIN
, s
->smack_ip_in
);
1139 log_unit_error_errno(UNIT(s
), r
, "mac_smack_apply_ip_in_fd: %m");
1142 if (s
->smack_ip_out
) {
1143 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_IPOUT
, s
->smack_ip_out
);
1145 log_unit_error_errno(UNIT(s
), r
, "mac_smack_apply_ip_out_fd: %m");
1149 static void socket_apply_fifo_options(Socket
*s
, int fd
) {
1155 if (s
->pipe_size
> 0)
1156 if (fcntl(fd
, F_SETPIPE_SZ
, s
->pipe_size
) < 0)
1157 log_unit_warning_errno(UNIT(s
), errno
, "Setting pipe size failed, ignoring: %m");
1160 r
= mac_smack_apply_fd(fd
, SMACK_ATTR_ACCESS
, s
->smack
);
1162 log_unit_error_errno(UNIT(s
), r
, "SMACK relabelling failed, ignoring: %m");
1166 static int fifo_address_create(
1168 mode_t directory_mode
,
1169 mode_t socket_mode
) {
1171 _cleanup_close_
int fd
= -1;
1178 (void) mkdir_parents_label(path
, directory_mode
);
1180 r
= mac_selinux_create_file_prepare(path
, S_IFIFO
);
1184 /* Enforce the right access mode for the fifo */
1185 old_mask
= umask(~ socket_mode
);
1187 /* Include the original umask in our mask */
1188 (void) umask(~socket_mode
| old_mask
);
1190 r
= mkfifo(path
, socket_mode
);
1191 (void) umask(old_mask
);
1193 if (r
< 0 && errno
!= EEXIST
) {
1198 fd
= open(path
, O_RDWR
| O_CLOEXEC
| O_NOCTTY
| O_NONBLOCK
| O_NOFOLLOW
);
1204 mac_selinux_create_file_clear();
1206 if (fstat(fd
, &st
) < 0) {
1211 if (!S_ISFIFO(st
.st_mode
) ||
1212 (st
.st_mode
& 0777) != (socket_mode
& ~old_mask
) ||
1213 st
.st_uid
!= getuid() ||
1214 st
.st_gid
!= getgid()) {
1225 mac_selinux_create_file_clear();
1229 static int special_address_create(const char *path
, bool writable
) {
1230 _cleanup_close_
int fd
= -1;
1236 fd
= open(path
, (writable
? O_RDWR
: O_RDONLY
)|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
);
1240 if (fstat(fd
, &st
) < 0)
1243 /* Check whether this is a /proc, /sys or /dev file or char device */
1244 if (!S_ISREG(st
.st_mode
) && !S_ISCHR(st
.st_mode
))
1253 static int usbffs_address_create(const char *path
) {
1254 _cleanup_close_
int fd
= -1;
1260 fd
= open(path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
);
1264 if (fstat(fd
, &st
) < 0)
1267 /* Check whether this is a regular file (ffs endpoint) */
1268 if (!S_ISREG(st
.st_mode
))
1277 static int mq_address_create(
1283 _cleanup_close_
int fd
= -1;
1286 struct mq_attr _attr
, *attr
= NULL
;
1291 if (maxmsg
> 0 && msgsize
> 0) {
1292 _attr
= (struct mq_attr
) {
1293 .mq_flags
= O_NONBLOCK
,
1294 .mq_maxmsg
= maxmsg
,
1295 .mq_msgsize
= msgsize
,
1300 /* Enforce the right access mode for the mq */
1301 old_mask
= umask(~ mq_mode
);
1303 /* Include the original umask in our mask */
1304 (void) umask(~mq_mode
| old_mask
);
1305 fd
= mq_open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_CREAT
, mq_mode
, attr
);
1306 (void) umask(old_mask
);
1311 if (fstat(fd
, &st
) < 0)
1314 if ((st
.st_mode
& 0777) != (mq_mode
& ~old_mask
) ||
1315 st
.st_uid
!= getuid() ||
1316 st
.st_gid
!= getgid())
1325 static int socket_symlink(Socket
*s
) {
1332 p
= socket_find_symlink_target(s
);
1336 STRV_FOREACH(i
, s
->symlinks
) {
1337 (void) mkdir_parents_label(*i
, s
->directory_mode
);
1339 r
= symlink_idempotent(p
, *i
);
1341 if (r
== -EEXIST
&& s
->remove_on_stop
) {
1342 /* If there's already something where we want to create the symlink, and the destructive
1343 * RemoveOnStop= mode is set, then we might as well try to remove what already exists and try
1346 if (unlink(*i
) >= 0)
1347 r
= symlink_idempotent(p
, *i
);
1351 log_unit_warning_errno(UNIT(s
), r
, "Failed to create symlink %s → %s, ignoring: %m", p
, *i
);
1357 static int usbffs_write_descs(int fd
, Service
*s
) {
1360 if (!s
->usb_function_descriptors
|| !s
->usb_function_strings
)
1363 r
= copy_file_fd(s
->usb_function_descriptors
, fd
, 0);
1367 return copy_file_fd(s
->usb_function_strings
, fd
, 0);
1370 static int usbffs_select_ep(const struct dirent
*d
) {
1371 return d
->d_name
[0] != '.' && !streq(d
->d_name
, "ep0");
1374 static int usbffs_dispatch_eps(SocketPort
*p
) {
1375 _cleanup_free_
struct dirent
**ent
= NULL
;
1378 r
= scandir(p
->path
, &ent
, usbffs_select_ep
, alphasort
);
1383 p
->auxiliary_fds
= new(int, n
);
1384 if (!p
->auxiliary_fds
)
1387 p
->n_auxiliary_fds
= n
;
1390 for (i
= 0; i
< n
; ++i
) {
1391 _cleanup_free_
char *ep
= NULL
;
1393 ep
= path_make_absolute(ent
[i
]->d_name
, p
->path
);
1397 path_kill_slashes(ep
);
1399 r
= usbffs_address_create(ep
);
1403 p
->auxiliary_fds
[k
] = r
;
1412 close_many(p
->auxiliary_fds
, k
);
1413 p
->auxiliary_fds
= mfree(p
->auxiliary_fds
);
1414 p
->n_auxiliary_fds
= 0;
1419 static int socket_determine_selinux_label(Socket
*s
, char **ret
) {
1426 if (s
->selinux_context_from_net
) {
1427 /* If this is requested, get label from the network label */
1429 r
= mac_selinux_get_our_label(ret
);
1430 if (r
== -EOPNOTSUPP
)
1434 /* Otherwise, get it from the executable we are about to start */
1435 r
= socket_instantiate_service(s
);
1439 if (!UNIT_ISSET(s
->service
))
1442 c
= SERVICE(UNIT_DEREF(s
->service
))->exec_command
[SERVICE_EXEC_START
];
1446 r
= mac_selinux_get_create_label_from_exe(c
->path
, ret
);
1447 if (IN_SET(r
, -EPERM
, -EOPNOTSUPP
))
1458 static int socket_address_listen_do(
1460 const SocketAddress
*address
,
1461 const char *label
) {
1466 return socket_address_listen(
1468 SOCK_CLOEXEC
|SOCK_NONBLOCK
,
1480 static int socket_address_listen_in_cgroup(
1482 const SocketAddress
*address
,
1483 const char *label
) {
1485 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1492 /* This is a wrapper around socket_address_listen(), that forks off a helper process inside the socket's cgroup
1493 * in which the socket is actually created. This way we ensure the socket is actually properly attached to the
1494 * unit's cgroup for the purpose of BPF filtering and such. */
1496 if (!IN_SET(address
->sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
1497 goto shortcut
; /* BPF filtering only applies to IPv4 + IPv6, shortcut things for other protocols */
1499 r
= bpf_firewall_supported();
1502 if (r
== 0) /* If BPF firewalling isn't supported anyway — there's no point in this forking complexity */
1505 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
1506 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
1508 r
= unit_fork_helper_process(UNIT(s
), &pid
);
1510 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off listener stub process: %m");
1514 pair
[0] = safe_close(pair
[0]);
1516 fd
= socket_address_listen_do(s
, address
, label
);
1518 log_unit_error_errno(UNIT(s
), fd
, "Failed to create listening socket: %m");
1519 _exit(EXIT_FAILURE
);
1522 r
= send_one_fd(pair
[1], fd
, 0);
1524 log_unit_error_errno(UNIT(s
), r
, "Failed to send listening socket to parent: %m");
1525 _exit(EXIT_FAILURE
);
1528 _exit(EXIT_SUCCESS
);
1531 pair
[1] = safe_close(pair
[1]);
1532 fd
= receive_one_fd(pair
[0], 0);
1534 /* We synchronously wait for the helper, as it shouldn't be slow */
1535 r
= wait_for_terminate_and_warn("listen-cgroup-helper", pid
, false);
1542 return log_unit_error_errno(UNIT(s
), fd
, "Failed to receive listening socket: %m");
1547 fd
= socket_address_listen_do(s
, address
, label
);
1549 return log_error_errno(fd
, "Failed to create listening socket: %m");
1554 static int socket_open_fds(Socket
*s
) {
1555 _cleanup_(mac_selinux_freep
) char *label
= NULL
;
1556 bool know_label
= false;
1562 LIST_FOREACH(port
, p
, s
->ports
) {
1572 /* Figure out label, if we don't it know yet. We do it once, for the first socket where
1573 * we need this and remember it for the rest. */
1575 r
= socket_determine_selinux_label(s
, &label
);
1582 /* Apply the socket protocol */
1583 switch (p
->address
.type
) {
1586 case SOCK_SEQPACKET
:
1587 if (s
->socket_protocol
== IPPROTO_SCTP
)
1588 p
->address
.protocol
= s
->socket_protocol
;
1592 if (s
->socket_protocol
== IPPROTO_UDPLITE
)
1593 p
->address
.protocol
= s
->socket_protocol
;
1597 r
= socket_address_listen_in_cgroup(s
, &p
->address
, label
);
1602 socket_apply_socket_options(s
, p
->fd
);
1606 case SOCKET_SPECIAL
:
1608 p
->fd
= special_address_create(p
->path
, s
->writable
);
1617 p
->fd
= fifo_address_create(
1626 socket_apply_fifo_options(s
, p
->fd
);
1632 p
->fd
= mq_address_create(
1643 case SOCKET_USB_FUNCTION
: {
1644 _cleanup_free_
char *ep
= NULL
;
1646 ep
= path_make_absolute("ep0", p
->path
);
1648 p
->fd
= usbffs_address_create(ep
);
1654 r
= usbffs_write_descs(p
->fd
, SERVICE(UNIT_DEREF(s
->service
)));
1658 r
= usbffs_dispatch_eps(p
);
1665 assert_not_reached("Unknown port type");
1672 socket_close_fds(s
);
1676 static void socket_unwatch_fds(Socket
*s
) {
1682 LIST_FOREACH(port
, p
, s
->ports
) {
1686 if (!p
->event_source
)
1689 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_OFF
);
1691 log_unit_debug_errno(UNIT(s
), r
, "Failed to disable event source: %m");
1695 static int socket_watch_fds(Socket
*s
) {
1701 LIST_FOREACH(port
, p
, s
->ports
) {
1705 if (p
->event_source
) {
1706 r
= sd_event_source_set_enabled(p
->event_source
, SD_EVENT_ON
);
1710 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &p
->event_source
, p
->fd
, EPOLLIN
, socket_dispatch_io
, p
);
1714 (void) sd_event_source_set_description(p
->event_source
, "socket-port-io");
1721 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch listening fds: %m");
1722 socket_unwatch_fds(s
);
1732 static int socket_check_open(Socket
*s
) {
1733 bool have_open
= false, have_closed
= false;
1738 LIST_FOREACH(port
, p
, s
->ports
) {
1744 if (have_open
&& have_closed
)
1745 return SOCKET_OPEN_SOME
;
1749 return SOCKET_OPEN_ALL
;
1751 return SOCKET_OPEN_NONE
;
1754 static void socket_set_state(Socket
*s
, SocketState state
) {
1755 SocketState old_state
;
1758 old_state
= s
->state
;
1766 SOCKET_STOP_PRE_SIGTERM
,
1767 SOCKET_STOP_PRE_SIGKILL
,
1769 SOCKET_FINAL_SIGTERM
,
1770 SOCKET_FINAL_SIGKILL
)) {
1772 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1773 socket_unwatch_control_pid(s
);
1774 s
->control_command
= NULL
;
1775 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1778 if (state
!= SOCKET_LISTENING
)
1779 socket_unwatch_fds(s
);
1787 SOCKET_STOP_PRE_SIGTERM
,
1788 SOCKET_STOP_PRE_SIGKILL
))
1789 socket_close_fds(s
);
1791 if (state
!= old_state
)
1792 log_unit_debug(UNIT(s
), "Changed %s -> %s", socket_state_to_string(old_state
), socket_state_to_string(state
));
1794 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], true);
1797 static int socket_coldplug(Unit
*u
) {
1798 Socket
*s
= SOCKET(u
);
1802 assert(s
->state
== SOCKET_DEAD
);
1804 if (s
->deserialized_state
== s
->state
)
1807 if (s
->control_pid
> 0 &&
1808 pid_is_unwaited(s
->control_pid
) &&
1809 IN_SET(s
->deserialized_state
,
1814 SOCKET_STOP_PRE_SIGTERM
,
1815 SOCKET_STOP_PRE_SIGKILL
,
1817 SOCKET_FINAL_SIGTERM
,
1818 SOCKET_FINAL_SIGKILL
)) {
1820 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
1824 r
= socket_arm_timer(s
, usec_add(u
->state_change_timestamp
.monotonic
, s
->timeout_usec
));
1829 if (IN_SET(s
->deserialized_state
,
1835 /* Originally, we used to simply reopen all sockets here that we didn't have file descriptors
1836 * for. However, this is problematic, as we won't traverse throught the SOCKET_START_CHOWN state for
1837 * them, and thus the UID/GID wouldn't be right. Hence, instead simply check if we have all fds open,
1838 * and if there's a mismatch, warn loudly. */
1840 r
= socket_check_open(s
);
1841 if (r
== SOCKET_OPEN_NONE
)
1842 log_unit_warning(UNIT(s
),
1843 "Socket unit configuration has changed while unit has been running, "
1844 "no open socket file descriptor left. "
1845 "The socket unit is not functional until restarted.");
1846 else if (r
== SOCKET_OPEN_SOME
)
1847 log_unit_warning(UNIT(s
),
1848 "Socket unit configuration has changed while unit has been running, "
1849 "and some socket file descriptors have not been opened yet. "
1850 "The socket unit is not fully functional until restarted.");
1853 if (s
->deserialized_state
== SOCKET_LISTENING
) {
1854 r
= socket_watch_fds(s
);
1859 if (!IN_SET(s
->deserialized_state
, SOCKET_DEAD
, SOCKET_FAILED
))
1860 (void) unit_setup_dynamic_creds(u
);
1862 socket_set_state(s
, s
->deserialized_state
);
1866 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
1869 ExecParameters exec_params
= {
1870 .flags
= EXEC_APPLY_SANDBOXING
|EXEC_APPLY_CHROOT
|EXEC_APPLY_TTY_STDIN
,
1880 (void) unit_realize_cgroup(UNIT(s
));
1881 if (s
->reset_accounting
) {
1882 (void) unit_reset_cpu_accounting(UNIT(s
));
1883 (void) unit_reset_ip_accounting(UNIT(s
));
1884 s
->reset_accounting
= false;
1887 unit_export_state_files(UNIT(s
));
1889 r
= unit_setup_exec_runtime(UNIT(s
));
1893 r
= unit_setup_dynamic_creds(UNIT(s
));
1897 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
1901 manager_set_exec_params(UNIT(s
)->manager
, &exec_params
);
1902 unit_set_exec_params(UNIT(s
), &exec_params
);
1904 exec_params
.argv
= c
->argv
;
1906 r
= exec_spawn(UNIT(s
),
1916 r
= unit_watch_pid(UNIT(s
), pid
);
1918 /* FIXME: we need to do something here */
1926 static int socket_chown(Socket
*s
, pid_t
*_pid
) {
1930 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
1934 /* We have to resolve the user names out-of-process, hence
1935 * let's fork here. It's messy, but well, what can we do? */
1937 r
= unit_fork_helper_process(UNIT(s
), &pid
);
1941 uid_t uid
= UID_INVALID
;
1942 gid_t gid
= GID_INVALID
;
1947 if (!isempty(s
->user
)) {
1948 const char *user
= s
->user
;
1950 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
);
1952 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve user %s: %m", user
);
1957 if (!isempty(s
->group
)) {
1958 const char *group
= s
->group
;
1960 r
= get_group_creds(&group
, &gid
);
1962 log_unit_error_errno(UNIT(s
), r
, "Failed to resolve group %s: %m", group
);
1967 LIST_FOREACH(port
, p
, s
->ports
) {
1968 const char *path
= NULL
;
1970 if (p
->type
== SOCKET_SOCKET
)
1971 path
= socket_address_get_path(&p
->address
);
1972 else if (p
->type
== SOCKET_FIFO
)
1978 if (chown(path
, uid
, gid
) < 0) {
1979 log_unit_error_errno(UNIT(s
), errno
, "Failed to chown(): %m");
1984 _exit(EXIT_SUCCESS
);
1987 r
= unit_watch_pid(UNIT(s
), pid
);
1995 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1999 static void socket_enter_dead(Socket
*s
, SocketResult f
) {
2002 if (s
->result
== SOCKET_SUCCESS
)
2005 if (s
->result
!= SOCKET_SUCCESS
)
2006 log_unit_warning(UNIT(s
), "Failed with result '%s'.", socket_result_to_string(s
->result
));
2008 socket_set_state(s
, s
->result
!= SOCKET_SUCCESS
? SOCKET_FAILED
: SOCKET_DEAD
);
2010 exec_runtime_destroy(s
->exec_runtime
);
2011 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
2013 exec_context_destroy_runtime_directory(&s
->exec_context
, UNIT(s
)->manager
->prefix
[EXEC_DIRECTORY_RUNTIME
]);
2015 unit_unref_uid_gid(UNIT(s
), true);
2017 dynamic_creds_destroy(&s
->dynamic_creds
);
2020 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
);
2022 static void socket_enter_stop_post(Socket
*s
, SocketResult f
) {
2026 if (s
->result
== SOCKET_SUCCESS
)
2029 socket_unwatch_control_pid(s
);
2030 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
2031 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
];
2033 if (s
->control_command
) {
2034 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2038 socket_set_state(s
, SOCKET_STOP_POST
);
2040 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_SUCCESS
);
2045 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
2046 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2049 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
) {
2054 if (s
->result
== SOCKET_SUCCESS
)
2057 r
= unit_kill_context(
2060 !IN_SET(state
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FINAL_SIGTERM
) ?
2061 KILL_KILL
: KILL_TERMINATE
,
2069 r
= socket_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_usec
));
2073 socket_set_state(s
, state
);
2074 } else if (state
== SOCKET_STOP_PRE_SIGTERM
)
2075 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_SUCCESS
);
2076 else if (state
== SOCKET_STOP_PRE_SIGKILL
)
2077 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2078 else if (state
== SOCKET_FINAL_SIGTERM
)
2079 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_SUCCESS
);
2081 socket_enter_dead(s
, SOCKET_SUCCESS
);
2086 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
2088 if (IN_SET(state
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_STOP_PRE_SIGKILL
))
2089 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2091 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2094 static void socket_enter_stop_pre(Socket
*s
, SocketResult f
) {
2098 if (s
->result
== SOCKET_SUCCESS
)
2101 socket_unwatch_control_pid(s
);
2102 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
2103 s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
];
2105 if (s
->control_command
) {
2106 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2110 socket_set_state(s
, SOCKET_STOP_PRE
);
2112 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
2117 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-pre' task: %m");
2118 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
2121 static void socket_enter_listening(Socket
*s
) {
2125 r
= socket_watch_fds(s
);
2127 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch sockets: %m");
2131 socket_set_state(s
, SOCKET_LISTENING
);
2135 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2138 static void socket_enter_start_post(Socket
*s
) {
2142 socket_unwatch_control_pid(s
);
2143 s
->control_command_id
= SOCKET_EXEC_START_POST
;
2144 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
];
2146 if (s
->control_command
) {
2147 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2149 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
2153 socket_set_state(s
, SOCKET_START_POST
);
2155 socket_enter_listening(s
);
2160 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2163 static void socket_enter_start_chown(Socket
*s
) {
2168 r
= socket_open_fds(s
);
2170 log_unit_warning_errno(UNIT(s
), r
, "Failed to listen on sockets: %m");
2174 if (!isempty(s
->user
) || !isempty(s
->group
)) {
2176 socket_unwatch_control_pid(s
);
2177 s
->control_command_id
= SOCKET_EXEC_START_CHOWN
;
2178 s
->control_command
= NULL
;
2180 r
= socket_chown(s
, &s
->control_pid
);
2182 log_unit_warning_errno(UNIT(s
), r
, "Failed to fork 'start-chown' task: %m");
2186 socket_set_state(s
, SOCKET_START_CHOWN
);
2188 socket_enter_start_post(s
);
2193 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2196 static void socket_enter_start_pre(Socket
*s
) {
2200 socket_unwatch_control_pid(s
);
2201 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
2202 s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
];
2204 if (s
->control_command
) {
2205 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2207 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
2211 socket_set_state(s
, SOCKET_START_PRE
);
2213 socket_enter_start_chown(s
);
2218 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2221 static void flush_ports(Socket
*s
) {
2224 /* Flush all incoming traffic, regardless if actual bytes or new connections, so that this socket isn't busy
2227 LIST_FOREACH(port
, p
, s
->ports
) {
2231 (void) flush_accept(p
->fd
);
2232 (void) flush_fd(p
->fd
);
2236 static void socket_enter_running(Socket
*s
, int cfd
) {
2237 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
2240 /* Note that this call takes possession of the connection fd passed. It either has to assign it somewhere or
2245 /* We don't take connections anymore if we are supposed to shut down anyway */
2246 if (unit_stop_pending(UNIT(s
))) {
2248 log_unit_debug(UNIT(s
), "Suppressing connection request since unit stop is scheduled.");
2251 cfd
= safe_close(cfd
);
2258 if (!ratelimit_test(&s
->trigger_limit
)) {
2260 log_unit_warning(UNIT(s
), "Trigger limit hit, refusing further activation.");
2261 socket_enter_stop_pre(s
, SOCKET_FAILURE_TRIGGER_LIMIT_HIT
);
2266 bool pending
= false;
2271 /* If there's already a start pending don't bother to
2273 HASHMAP_FOREACH_KEY(v
, other
, UNIT(s
)->dependencies
[UNIT_TRIGGERS
], i
)
2274 if (unit_active_or_pending(other
)) {
2280 if (!UNIT_ISSET(s
->service
)) {
2281 log_unit_error(UNIT(s
), "Service to activate vanished, refusing activation.");
2286 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT_DEREF(s
->service
), JOB_REPLACE
, &error
, NULL
);
2291 socket_set_state(s
, SOCKET_RUNNING
);
2293 _cleanup_free_
char *prefix
= NULL
, *instance
= NULL
, *name
= NULL
;
2294 _cleanup_(socket_peer_unrefp
) SocketPeer
*p
= NULL
;
2297 if (s
->n_connections
>= s
->max_connections
) {
2298 log_unit_warning(UNIT(s
), "Too many incoming connections (%u), dropping connection.",
2304 if (s
->max_connections_per_source
> 0) {
2305 r
= socket_acquire_peer(s
, cfd
, &p
);
2309 } else if (r
> 0 && p
->n_ref
> s
->max_connections_per_source
) {
2310 _cleanup_free_
char *t
= NULL
;
2312 (void) sockaddr_pretty(&p
->peer
.sa
, p
->peer_salen
, true, false, &t
);
2314 log_unit_warning(UNIT(s
),
2315 "Too many incoming connections (%u) from source %s, dropping connection.",
2316 p
->n_ref
, strnull(t
));
2322 r
= socket_instantiate_service(s
);
2326 r
= instance_from_socket(cfd
, s
->n_accepted
, &instance
);
2331 /* ENOTCONN is legitimate if TCP RST was received.
2332 * This connection is over, but the socket unit lives on. */
2333 log_unit_debug(UNIT(s
), "Got ENOTCONN on incoming socket, assuming aborted connection attempt, ignoring.");
2338 r
= unit_name_to_prefix(UNIT(s
)->id
, &prefix
);
2342 r
= unit_name_build(prefix
, instance
, ".service", &name
);
2346 r
= unit_add_name(UNIT_DEREF(s
->service
), name
);
2350 service
= SERVICE(UNIT_DEREF(s
->service
));
2351 unit_ref_unset(&s
->service
);
2354 unit_choose_id(UNIT(service
), name
);
2356 r
= service_set_socket_fd(service
, cfd
, s
, s
->selinux_context_from_net
);
2360 cfd
= -1; /* We passed ownership of the fd to the service now. Forget it here. */
2363 service
->peer
= p
; /* Pass ownership of the peer reference */
2366 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(service
), JOB_REPLACE
, &error
, NULL
);
2368 /* We failed to activate the new service, but it still exists. Let's make sure the service
2369 * closes and forgets the connection fd again, immediately. */
2370 service_close_socket_fd(service
);
2374 /* Notify clients about changed counters */
2375 unit_add_to_dbus_queue(UNIT(s
));
2381 log_unit_warning(UNIT(s
), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
2382 cfd
>= 0 ? "template" : "non-template",
2383 bus_error_message(&error
, r
));
2385 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2389 static void socket_run_next(Socket
*s
) {
2393 assert(s
->control_command
);
2394 assert(s
->control_command
->command_next
);
2396 socket_unwatch_control_pid(s
);
2398 s
->control_command
= s
->control_command
->command_next
;
2400 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
2407 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next task: %m");
2409 if (s
->state
== SOCKET_START_POST
)
2410 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
2411 else if (s
->state
== SOCKET_STOP_POST
)
2412 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
2414 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
2417 static int socket_start(Unit
*u
) {
2418 Socket
*s
= SOCKET(u
);
2423 /* We cannot fulfill this request right now, try again later
2425 if (IN_SET(s
->state
,
2427 SOCKET_STOP_PRE_SIGKILL
,
2428 SOCKET_STOP_PRE_SIGTERM
,
2430 SOCKET_FINAL_SIGTERM
,
2431 SOCKET_FINAL_SIGKILL
))
2434 /* Already on it! */
2435 if (IN_SET(s
->state
,
2441 /* Cannot run this without the service being around */
2442 if (UNIT_ISSET(s
->service
)) {
2445 service
= SERVICE(UNIT_DEREF(s
->service
));
2447 if (UNIT(service
)->load_state
!= UNIT_LOADED
) {
2448 log_unit_error(u
, "Socket service %s not loaded, refusing.", UNIT(service
)->id
);
2452 /* If the service is already active we cannot start the
2454 if (!IN_SET(service
->state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
)) {
2455 log_unit_error(u
, "Socket service %s already active, refusing.", UNIT(service
)->id
);
2460 assert(IN_SET(s
->state
, SOCKET_DEAD
, SOCKET_FAILED
));
2462 r
= unit_start_limit_test(u
);
2464 socket_enter_dead(s
, SOCKET_FAILURE_START_LIMIT_HIT
);
2468 r
= unit_acquire_invocation_id(u
);
2472 s
->result
= SOCKET_SUCCESS
;
2473 s
->reset_accounting
= true;
2475 socket_enter_start_pre(s
);
2479 static int socket_stop(Unit
*u
) {
2480 Socket
*s
= SOCKET(u
);
2485 if (IN_SET(s
->state
,
2487 SOCKET_STOP_PRE_SIGTERM
,
2488 SOCKET_STOP_PRE_SIGKILL
,
2490 SOCKET_FINAL_SIGTERM
,
2491 SOCKET_FINAL_SIGKILL
))
2494 /* If there's already something running we go directly into
2496 if (IN_SET(s
->state
,
2499 SOCKET_START_POST
)) {
2500 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_SUCCESS
);
2504 assert(IN_SET(s
->state
, SOCKET_LISTENING
, SOCKET_RUNNING
));
2506 socket_enter_stop_pre(s
, SOCKET_SUCCESS
);
2510 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2511 Socket
*s
= SOCKET(u
);
2519 unit_serialize_item(u
, f
, "state", socket_state_to_string(s
->state
));
2520 unit_serialize_item(u
, f
, "result", socket_result_to_string(s
->result
));
2521 unit_serialize_item_format(u
, f
, "n-accepted", "%u", s
->n_accepted
);
2523 if (s
->control_pid
> 0)
2524 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2526 if (s
->control_command_id
>= 0)
2527 unit_serialize_item(u
, f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
2529 LIST_FOREACH(port
, p
, s
->ports
) {
2535 copy
= fdset_put_dup(fds
, p
->fd
);
2539 if (p
->type
== SOCKET_SOCKET
) {
2540 _cleanup_free_
char *t
= NULL
;
2542 r
= socket_address_print(&p
->address
, &t
);
2546 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2547 unit_serialize_item_format(u
, f
, "netlink", "%i %s", copy
, t
);
2549 unit_serialize_item_format(u
, f
, "socket", "%i %i %s", copy
, p
->address
.type
, t
);
2551 } else if (p
->type
== SOCKET_SPECIAL
)
2552 unit_serialize_item_format(u
, f
, "special", "%i %s", copy
, p
->path
);
2553 else if (p
->type
== SOCKET_MQUEUE
)
2554 unit_serialize_item_format(u
, f
, "mqueue", "%i %s", copy
, p
->path
);
2555 else if (p
->type
== SOCKET_USB_FUNCTION
)
2556 unit_serialize_item_format(u
, f
, "ffs", "%i %s", copy
, p
->path
);
2558 assert(p
->type
== SOCKET_FIFO
);
2559 unit_serialize_item_format(u
, f
, "fifo", "%i %s", copy
, p
->path
);
2566 static void socket_port_take_fd(SocketPort
*p
, FDSet
*fds
, int fd
) {
2568 p
->fd
= fdset_remove(fds
, fd
);
2571 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2572 Socket
*s
= SOCKET(u
);
2578 if (streq(key
, "state")) {
2581 state
= socket_state_from_string(value
);
2583 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2585 s
->deserialized_state
= state
;
2586 } else if (streq(key
, "result")) {
2589 f
= socket_result_from_string(value
);
2591 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2592 else if (f
!= SOCKET_SUCCESS
)
2595 } else if (streq(key
, "n-accepted")) {
2598 if (safe_atou(value
, &k
) < 0)
2599 log_unit_debug(u
, "Failed to parse n-accepted value: %s", value
);
2602 } else if (streq(key
, "control-pid")) {
2605 if (parse_pid(value
, &pid
) < 0)
2606 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2608 s
->control_pid
= pid
;
2609 } else if (streq(key
, "control-command")) {
2610 SocketExecCommand id
;
2612 id
= socket_exec_command_from_string(value
);
2614 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2616 s
->control_command_id
= id
;
2617 s
->control_command
= s
->exec_command
[id
];
2619 } else if (streq(key
, "fifo")) {
2623 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2624 log_unit_debug(u
, "Failed to parse fifo value: %s", value
);
2626 LIST_FOREACH(port
, p
, s
->ports
)
2627 if (p
->type
== SOCKET_FIFO
&&
2628 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2629 socket_port_take_fd(p
, fds
, fd
);
2633 } else if (streq(key
, "special")) {
2637 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2638 log_unit_debug(u
, "Failed to parse special value: %s", value
);
2640 LIST_FOREACH(port
, p
, s
->ports
)
2641 if (p
->type
== SOCKET_SPECIAL
&&
2642 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2643 socket_port_take_fd(p
, fds
, fd
);
2647 } else if (streq(key
, "mqueue")) {
2651 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2652 log_unit_debug(u
, "Failed to parse mqueue value: %s", value
);
2654 LIST_FOREACH(port
, p
, s
->ports
)
2655 if (p
->type
== SOCKET_MQUEUE
&&
2656 streq(p
->path
, value
+skip
)) {
2657 socket_port_take_fd(p
, fds
, fd
);
2661 } else if (streq(key
, "socket")) {
2662 int fd
, type
, skip
= 0;
2665 if (sscanf(value
, "%i %i %n", &fd
, &type
, &skip
) < 2 || fd
< 0 || type
< 0 || !fdset_contains(fds
, fd
))
2666 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2668 LIST_FOREACH(port
, p
, s
->ports
)
2669 if (socket_address_is(&p
->address
, value
+skip
, type
)) {
2670 socket_port_take_fd(p
, fds
, fd
);
2674 } else if (streq(key
, "netlink")) {
2678 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2679 log_unit_debug(u
, "Failed to parse socket value: %s", value
);
2681 LIST_FOREACH(port
, p
, s
->ports
)
2682 if (socket_address_is_netlink(&p
->address
, value
+skip
)) {
2683 socket_port_take_fd(p
, fds
, fd
);
2687 } else if (streq(key
, "ffs")) {
2691 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
2692 log_unit_debug(u
, "Failed to parse ffs value: %s", value
);
2694 LIST_FOREACH(port
, p
, s
->ports
)
2695 if (p
->type
== SOCKET_USB_FUNCTION
&&
2696 path_equal_or_files_same(p
->path
, value
+skip
, 0)) {
2697 socket_port_take_fd(p
, fds
, fd
);
2702 log_unit_debug(UNIT(s
), "Unknown serialization key: %s", key
);
2707 static void socket_distribute_fds(Unit
*u
, FDSet
*fds
) {
2708 Socket
*s
= SOCKET(u
);
2713 LIST_FOREACH(port
, p
, s
->ports
) {
2717 if (p
->type
!= SOCKET_SOCKET
)
2723 FDSET_FOREACH(fd
, fds
, i
) {
2724 if (socket_address_matches_fd(&p
->address
, fd
)) {
2725 p
->fd
= fdset_remove(fds
, fd
);
2726 s
->deserialized_state
= SOCKET_LISTENING
;
2733 _pure_
static UnitActiveState
socket_active_state(Unit
*u
) {
2736 return state_translation_table
[SOCKET(u
)->state
];
2739 _pure_
static const char *socket_sub_state_to_string(Unit
*u
) {
2742 return socket_state_to_string(SOCKET(u
)->state
);
2745 const char* socket_port_type_to_string(SocketPort
*p
) {
2753 switch (p
->address
.type
) {
2761 case SOCK_SEQPACKET
:
2762 return "SequentialPacket";
2765 if (socket_address_family(&p
->address
) == AF_NETLINK
)
2773 case SOCKET_SPECIAL
:
2777 return "MessageQueue";
2782 case SOCKET_USB_FUNCTION
:
2783 return "USBFunction";
2790 _pure_
static bool socket_check_gc(Unit
*u
) {
2791 Socket
*s
= SOCKET(u
);
2795 return s
->n_connections
> 0;
2798 static int socket_accept_do(Socket
*s
, int fd
) {
2805 cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
);
2819 static int socket_accept_in_cgroup(Socket
*s
, SocketPort
*p
, int fd
) {
2820 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
2828 /* Similar to socket_address_listen_in_cgroup(), but for accept() rathern than socket(): make sure that any
2829 * connection socket is also properly associated with the cgroup. */
2831 if (!IN_SET(p
->address
.sockaddr
.sa
.sa_family
, AF_INET
, AF_INET6
))
2834 r
= bpf_firewall_supported();
2840 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
|SOCK_CLOEXEC
, 0, pair
) < 0)
2841 return log_unit_error_errno(UNIT(s
), errno
, "Failed to create communication channel: %m");
2843 r
= unit_fork_helper_process(UNIT(s
), &pid
);
2845 return log_unit_error_errno(UNIT(s
), r
, "Failed to fork off accept stub process: %m");
2849 pair
[0] = safe_close(pair
[0]);
2851 cfd
= socket_accept_do(s
, fd
);
2853 log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
2854 _exit(EXIT_FAILURE
);
2857 r
= send_one_fd(pair
[1], cfd
, 0);
2859 log_unit_error_errno(UNIT(s
), r
, "Failed to send connection socket to parent: %m");
2860 _exit(EXIT_FAILURE
);
2863 _exit(EXIT_SUCCESS
);
2866 pair
[1] = safe_close(pair
[1]);
2867 cfd
= receive_one_fd(pair
[0], 0);
2869 /* We synchronously wait for the helper, as it shouldn't be slow */
2870 r
= wait_for_terminate_and_warn("accept-cgroup-helper", pid
, false);
2877 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to receive connection socket: %m");
2882 cfd
= socket_accept_do(s
, fd
);
2884 return log_unit_error_errno(UNIT(s
), cfd
, "Failed to accept connection socket: %m");
2889 static int socket_dispatch_io(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
2890 SocketPort
*p
= userdata
;
2896 if (p
->socket
->state
!= SOCKET_LISTENING
)
2899 log_unit_debug(UNIT(p
->socket
), "Incoming traffic");
2901 if (revents
!= EPOLLIN
) {
2903 if (revents
& EPOLLHUP
)
2904 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.");
2906 log_unit_error(UNIT(p
->socket
), "Got unexpected poll event (0x%x) on socket.", revents
);
2910 if (p
->socket
->accept
&&
2911 p
->type
== SOCKET_SOCKET
&&
2912 socket_address_can_accept(&p
->address
)) {
2914 cfd
= socket_accept_in_cgroup(p
->socket
, p
, fd
);
2918 socket_apply_socket_options(p
->socket
, cfd
);
2921 socket_enter_running(p
->socket
, cfd
);
2925 socket_enter_stop_pre(p
->socket
, SOCKET_FAILURE_RESOURCES
);
2929 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2930 Socket
*s
= SOCKET(u
);
2936 if (pid
!= s
->control_pid
)
2941 if (is_clean_exit(code
, status
, EXIT_CLEAN_COMMAND
, NULL
))
2943 else if (code
== CLD_EXITED
)
2944 f
= SOCKET_FAILURE_EXIT_CODE
;
2945 else if (code
== CLD_KILLED
)
2946 f
= SOCKET_FAILURE_SIGNAL
;
2947 else if (code
== CLD_DUMPED
)
2948 f
= SOCKET_FAILURE_CORE_DUMP
;
2950 assert_not_reached("Unknown sigchld code");
2952 if (s
->control_command
) {
2953 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2955 if (s
->control_command
->flags
& EXEC_COMMAND_IGNORE_FAILURE
)
2959 log_unit_full(u
, f
== SOCKET_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2960 "Control process exited, code=%s status=%i",
2961 sigchld_code_to_string(code
), status
);
2963 if (s
->result
== SOCKET_SUCCESS
)
2966 if (s
->control_command
&&
2967 s
->control_command
->command_next
&&
2968 f
== SOCKET_SUCCESS
) {
2970 log_unit_debug(u
, "Running next command for state %s", socket_state_to_string(s
->state
));
2973 s
->control_command
= NULL
;
2974 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
2976 /* No further commands for this step, so let's figure
2977 * out what to do next */
2979 log_unit_debug(u
, "Got final SIGCHLD for state %s", socket_state_to_string(s
->state
));
2983 case SOCKET_START_PRE
:
2984 if (f
== SOCKET_SUCCESS
)
2985 socket_enter_start_chown(s
);
2987 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, f
);
2990 case SOCKET_START_CHOWN
:
2991 if (f
== SOCKET_SUCCESS
)
2992 socket_enter_start_post(s
);
2994 socket_enter_stop_pre(s
, f
);
2997 case SOCKET_START_POST
:
2998 if (f
== SOCKET_SUCCESS
)
2999 socket_enter_listening(s
);
3001 socket_enter_stop_pre(s
, f
);
3004 case SOCKET_STOP_PRE
:
3005 case SOCKET_STOP_PRE_SIGTERM
:
3006 case SOCKET_STOP_PRE_SIGKILL
:
3007 socket_enter_stop_post(s
, f
);
3010 case SOCKET_STOP_POST
:
3011 case SOCKET_FINAL_SIGTERM
:
3012 case SOCKET_FINAL_SIGKILL
:
3013 socket_enter_dead(s
, f
);
3017 assert_not_reached("Uh, control process died at wrong time.");
3021 /* Notify clients about changed exit status */
3022 unit_add_to_dbus_queue(u
);
3025 static int socket_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
3026 Socket
*s
= SOCKET(userdata
);
3029 assert(s
->timer_event_source
== source
);
3033 case SOCKET_START_PRE
:
3034 log_unit_warning(UNIT(s
), "Starting timed out. Terminating.");
3035 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3038 case SOCKET_START_CHOWN
:
3039 case SOCKET_START_POST
:
3040 log_unit_warning(UNIT(s
), "Starting timed out. Stopping.");
3041 socket_enter_stop_pre(s
, SOCKET_FAILURE_TIMEOUT
);
3044 case SOCKET_STOP_PRE
:
3045 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
3046 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3049 case SOCKET_STOP_PRE_SIGTERM
:
3050 if (s
->kill_context
.send_sigkill
) {
3051 log_unit_warning(UNIT(s
), "Stopping timed out. Killing.");
3052 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3054 log_unit_warning(UNIT(s
), "Stopping timed out. Skipping SIGKILL. Ignoring.");
3055 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3059 case SOCKET_STOP_PRE_SIGKILL
:
3060 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
3061 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
3064 case SOCKET_STOP_POST
:
3065 log_unit_warning(UNIT(s
), "Stopping timed out (2). Terminating.");
3066 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
3069 case SOCKET_FINAL_SIGTERM
:
3070 if (s
->kill_context
.send_sigkill
) {
3071 log_unit_warning(UNIT(s
), "Stopping timed out (2). Killing.");
3072 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
3074 log_unit_warning(UNIT(s
), "Stopping timed out (2). Skipping SIGKILL. Ignoring.");
3075 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3079 case SOCKET_FINAL_SIGKILL
:
3080 log_unit_warning(UNIT(s
), "Still around after SIGKILL (2). Entering failed mode.");
3081 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
3085 assert_not_reached("Timeout at wrong time.");
3091 int socket_collect_fds(Socket
*s
, int **fds
) {
3092 int *rfds
, k
= 0, n
= 0;
3098 /* Called from the service code for requesting our fds */
3100 LIST_FOREACH(port
, p
, s
->ports
) {
3103 n
+= p
->n_auxiliary_fds
;
3115 LIST_FOREACH(port
, p
, s
->ports
) {
3120 for (i
= 0; i
< p
->n_auxiliary_fds
; ++i
)
3121 rfds
[k
++] = p
->auxiliary_fds
[i
];
3130 static void socket_reset_failed(Unit
*u
) {
3131 Socket
*s
= SOCKET(u
);
3135 if (s
->state
== SOCKET_FAILED
)
3136 socket_set_state(s
, SOCKET_DEAD
);
3138 s
->result
= SOCKET_SUCCESS
;
3141 void socket_connection_unref(Socket
*s
) {
3144 /* The service is dead. Yay!
3146 * This is strictly for one-instance-per-connection
3149 assert(s
->n_connections
> 0);
3152 log_unit_debug(UNIT(s
), "One connection closed, %u left.", s
->n_connections
);
3155 static void socket_trigger_notify(Unit
*u
, Unit
*other
) {
3156 Socket
*s
= SOCKET(u
);
3161 /* Filter out invocations with bogus state */
3162 if (other
->load_state
!= UNIT_LOADED
|| other
->type
!= UNIT_SERVICE
)
3165 /* Don't propagate state changes from the service if we are already down */
3166 if (!IN_SET(s
->state
, SOCKET_RUNNING
, SOCKET_LISTENING
))
3169 /* We don't care for the service state if we are in Accept=yes mode */
3173 /* Propagate start limit hit state */
3174 if (other
->start_limit_hit
) {
3175 socket_enter_stop_pre(s
, SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
);
3179 /* Don't propagate anything if there's still a job queued */
3183 if (IN_SET(SERVICE(other
)->state
,
3184 SERVICE_DEAD
, SERVICE_FAILED
,
3185 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
3186 SERVICE_AUTO_RESTART
))
3187 socket_enter_listening(s
);
3189 if (SERVICE(other
)->state
== SERVICE_RUNNING
)
3190 socket_set_state(s
, SOCKET_RUNNING
);
3193 static int socket_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3194 return unit_kill_common(u
, who
, signo
, -1, SOCKET(u
)->control_pid
, error
);
3197 static int socket_get_timeout(Unit
*u
, usec_t
*timeout
) {
3198 Socket
*s
= SOCKET(u
);
3202 if (!s
->timer_event_source
)
3205 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3208 if (t
== USEC_INFINITY
)
3215 char *socket_fdname(Socket
*s
) {
3218 /* Returns the name to use for $LISTEN_NAMES. If the user
3219 * didn't specify anything specifically, use the socket unit's
3220 * name as fallback. */
3228 static int socket_control_pid(Unit
*u
) {
3229 Socket
*s
= SOCKET(u
);
3233 return s
->control_pid
;
3236 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
3237 [SOCKET_EXEC_START_PRE
] = "StartPre",
3238 [SOCKET_EXEC_START_CHOWN
] = "StartChown",
3239 [SOCKET_EXEC_START_POST
] = "StartPost",
3240 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
3241 [SOCKET_EXEC_STOP_POST
] = "StopPost"
3244 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
3246 static const char* const socket_result_table
[_SOCKET_RESULT_MAX
] = {
3247 [SOCKET_SUCCESS
] = "success",
3248 [SOCKET_FAILURE_RESOURCES
] = "resources",
3249 [SOCKET_FAILURE_TIMEOUT
] = "timeout",
3250 [SOCKET_FAILURE_EXIT_CODE
] = "exit-code",
3251 [SOCKET_FAILURE_SIGNAL
] = "signal",
3252 [SOCKET_FAILURE_CORE_DUMP
] = "core-dump",
3253 [SOCKET_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
3254 [SOCKET_FAILURE_TRIGGER_LIMIT_HIT
] = "trigger-limit-hit",
3255 [SOCKET_FAILURE_SERVICE_START_LIMIT_HIT
] = "service-start-limit-hit"
3258 DEFINE_STRING_TABLE_LOOKUP(socket_result
, SocketResult
);
3260 const UnitVTable socket_vtable
= {
3261 .object_size
= sizeof(Socket
),
3262 .exec_context_offset
= offsetof(Socket
, exec_context
),
3263 .cgroup_context_offset
= offsetof(Socket
, cgroup_context
),
3264 .kill_context_offset
= offsetof(Socket
, kill_context
),
3265 .exec_runtime_offset
= offsetof(Socket
, exec_runtime
),
3266 .dynamic_creds_offset
= offsetof(Socket
, dynamic_creds
),
3272 .private_section
= "Socket",
3274 .init
= socket_init
,
3275 .done
= socket_done
,
3276 .load
= socket_load
,
3278 .coldplug
= socket_coldplug
,
3280 .dump
= socket_dump
,
3282 .start
= socket_start
,
3283 .stop
= socket_stop
,
3285 .kill
= socket_kill
,
3287 .get_timeout
= socket_get_timeout
,
3289 .serialize
= socket_serialize
,
3290 .deserialize_item
= socket_deserialize_item
,
3291 .distribute_fds
= socket_distribute_fds
,
3293 .active_state
= socket_active_state
,
3294 .sub_state_to_string
= socket_sub_state_to_string
,
3296 .check_gc
= socket_check_gc
,
3298 .sigchld_event
= socket_sigchld_event
,
3300 .trigger_notify
= socket_trigger_notify
,
3302 .reset_failed
= socket_reset_failed
,
3304 .control_pid
= socket_control_pid
,
3306 .bus_vtable
= bus_socket_vtable
,
3307 .bus_set_property
= bus_socket_set_property
,
3308 .bus_commit_properties
= bus_socket_commit_properties
,
3310 .status_message_formats
= {
3311 /*.starting_stopping = {
3312 [0] = "Starting socket %s...",
3313 [1] = "Stopping socket %s...",
3315 .finished_start_job
= {
3316 [JOB_DONE
] = "Listening on %s.",
3317 [JOB_FAILED
] = "Failed to listen on %s.",
3318 [JOB_TIMEOUT
] = "Timed out starting %s.",
3320 .finished_stop_job
= {
3321 [JOB_DONE
] = "Closed %s.",
3322 [JOB_FAILED
] = "Failed stopping %s.",
3323 [JOB_TIMEOUT
] = "Timed out stopping %s.",