1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
27 #include <sys/epoll.h>
29 #include <arpa/inet.h>
34 #include "netinet/tcp.h"
36 #include "load-dropin.h"
37 #include "load-fragment.h"
40 #include "unit-name.h"
41 #include "dbus-socket.h"
44 #include "bus-errors.h"
46 #include "exit-status.h"
49 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
50 [SOCKET_DEAD
] = UNIT_INACTIVE
,
51 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
52 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
53 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
54 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
55 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
56 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
57 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
58 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
59 [SOCKET_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
60 [SOCKET_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
61 [SOCKET_FAILED
] = UNIT_FAILED
64 static void socket_init(Unit
*u
) {
65 Socket
*s
= SOCKET(u
);
68 assert(u
->load_state
== UNIT_STUB
);
70 s
->backlog
= SOMAXCONN
;
71 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
72 s
->directory_mode
= 0755;
73 s
->socket_mode
= 0666;
75 s
->max_connections
= 64;
82 exec_context_init(&s
->exec_context
);
83 s
->exec_context
.std_output
= u
->manager
->default_std_output
;
84 s
->exec_context
.std_error
= u
->manager
->default_std_error
;
86 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
89 static void socket_unwatch_control_pid(Socket
*s
) {
92 if (s
->control_pid
<= 0)
95 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
99 static void socket_done(Unit
*u
) {
100 Socket
*s
= SOCKET(u
);
105 while ((p
= s
->ports
)) {
106 LIST_REMOVE(SocketPort
, port
, s
->ports
, p
);
109 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
110 close_nointr_nofail(p
->fd
);
117 exec_context_done(&s
->exec_context
);
118 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
119 s
->control_command
= NULL
;
121 socket_unwatch_control_pid(s
);
123 unit_ref_unset(&s
->service
);
125 free(s
->tcp_congestion
);
126 s
->tcp_congestion
= NULL
;
128 free(s
->bind_to_device
);
129 s
->bind_to_device
= NULL
;
131 unit_unwatch_timer(u
, &s
->timer_watch
);
134 static int socket_instantiate_service(Socket
*s
) {
141 /* This fills in s->service if it isn't filled in yet. For
142 * Accept=yes sockets we create the next connection service
143 * here. For Accept=no this is mostly a NOP since the service
144 * is figured out at load time anyway. */
146 if (UNIT_DEREF(s
->service
))
151 if (!(prefix
= unit_name_to_prefix(UNIT(s
)->id
)))
154 r
= asprintf(&name
, "%s@%u.service", prefix
, s
->n_accepted
);
160 r
= manager_load_unit(UNIT(s
)->manager
, name
, NULL
, NULL
, &u
);
166 #ifdef HAVE_SYSV_COMPAT
167 if (SERVICE(u
)->sysv_path
) {
168 log_error("Using SysV services for socket activation is not supported. Refusing.");
174 unit_ref_set(&s
->service
, u
);
176 return unit_add_two_dependencies(UNIT(s
), UNIT_BEFORE
, UNIT_TRIGGERS
, u
, false);
179 static bool have_non_accept_socket(Socket
*s
) {
187 LIST_FOREACH(port
, p
, s
->ports
) {
189 if (p
->type
!= SOCKET_SOCKET
)
192 if (!socket_address_can_accept(&p
->address
))
199 static int socket_verify(Socket
*s
) {
202 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
206 log_error("%s lacks Listen setting. Refusing.", UNIT(s
)->id
);
210 if (s
->accept
&& have_non_accept_socket(s
)) {
211 log_error("%s configured for accepting sockets, but sockets are non-accepting. Refusing.", UNIT(s
)->id
);
215 if (s
->accept
&& s
->max_connections
<= 0) {
216 log_error("%s's MaxConnection setting too small. Refusing.", UNIT(s
)->id
);
220 if (s
->accept
&& UNIT_DEREF(s
->service
)) {
221 log_error("Explicit service configuration for accepting sockets not supported on %s. Refusing.", UNIT(s
)->id
);
225 if (s
->exec_context
.pam_name
&& s
->exec_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
226 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s
)->id
);
233 static bool socket_needs_mount(Socket
*s
, const char *prefix
) {
238 LIST_FOREACH(port
, p
, s
->ports
) {
240 if (p
->type
== SOCKET_SOCKET
) {
241 if (socket_address_needs_mount(&p
->address
, prefix
))
243 } else if (p
->type
== SOCKET_FIFO
|| p
->type
== SOCKET_SPECIAL
) {
244 if (path_startswith(p
->path
, prefix
))
252 int socket_add_one_mount_link(Socket
*s
, Mount
*m
) {
258 if (UNIT(s
)->load_state
!= UNIT_LOADED
||
259 UNIT(m
)->load_state
!= UNIT_LOADED
)
262 if (!socket_needs_mount(s
, m
->where
))
265 if ((r
= unit_add_two_dependencies(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, UNIT(m
), true)) < 0)
271 static int socket_add_mount_links(Socket
*s
) {
277 LIST_FOREACH(units_by_type
, other
, UNIT(s
)->manager
->units_by_type
[UNIT_MOUNT
])
278 if ((r
= socket_add_one_mount_link(s
, MOUNT(other
))) < 0)
284 static int socket_add_device_link(Socket
*s
) {
290 if (!s
->bind_to_device
)
293 if (asprintf(&t
, "/sys/subsystem/net/devices/%s", s
->bind_to_device
) < 0)
296 r
= unit_add_node_link(UNIT(s
), t
, false);
302 static int socket_add_default_dependencies(Socket
*s
) {
306 if (UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
) {
307 if ((r
= unit_add_dependency_by_name(UNIT(s
), UNIT_BEFORE
, SPECIAL_SOCKETS_TARGET
, NULL
, true)) < 0)
310 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true)) < 0)
314 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
317 static bool socket_has_exec(Socket
*s
) {
321 for (i
= 0; i
< _SOCKET_EXEC_COMMAND_MAX
; i
++)
322 if (s
->exec_command
[i
])
328 static int socket_load(Unit
*u
) {
329 Socket
*s
= SOCKET(u
);
333 assert(u
->load_state
== UNIT_STUB
);
335 if ((r
= unit_load_fragment_and_dropin(u
)) < 0)
338 /* This is a new unit? Then let's add in some extras */
339 if (u
->load_state
== UNIT_LOADED
) {
341 if (have_non_accept_socket(s
)) {
343 if (!UNIT_DEREF(s
->service
)) {
346 r
= unit_load_related_unit(u
, ".service", &x
);
350 unit_ref_set(&s
->service
, x
);
353 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT_DEREF(s
->service
), true);
358 if ((r
= socket_add_mount_links(s
)) < 0)
361 if ((r
= socket_add_device_link(s
)) < 0)
364 if (socket_has_exec(s
))
365 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
368 if ((r
= unit_add_default_cgroups(u
)) < 0)
371 if (UNIT(s
)->default_dependencies
)
372 if ((r
= socket_add_default_dependencies(s
)) < 0)
376 return socket_verify(s
);
379 static const char* listen_lookup(int family
, int type
) {
381 if (family
== AF_NETLINK
)
382 return "ListenNetlink";
384 if (type
== SOCK_STREAM
)
385 return "ListenStream";
386 else if (type
== SOCK_DGRAM
)
387 return "ListenDatagram";
388 else if (type
== SOCK_SEQPACKET
)
389 return "ListenSequentialPacket";
391 assert_not_reached("Unknown socket type");
395 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
398 Socket
*s
= SOCKET(u
);
406 p2
= strappend(prefix
, "\t");
407 prefix2
= p2
? p2
: prefix
;
410 "%sSocket State: %s\n"
412 "%sBindIPv6Only: %s\n"
414 "%sSocketMode: %04o\n"
415 "%sDirectoryMode: %04o\n"
418 "%sTransparent: %s\n"
420 "%sPassCredentials: %s\n"
421 "%sPassSecurity: %s\n"
422 "%sTCPCongestion: %s\n",
423 prefix
, socket_state_to_string(s
->state
),
424 prefix
, socket_result_to_string(s
->result
),
425 prefix
, socket_address_bind_ipv6_only_to_string(s
->bind_ipv6_only
),
427 prefix
, s
->socket_mode
,
428 prefix
, s
->directory_mode
,
429 prefix
, yes_no(s
->keep_alive
),
430 prefix
, yes_no(s
->free_bind
),
431 prefix
, yes_no(s
->transparent
),
432 prefix
, yes_no(s
->broadcast
),
433 prefix
, yes_no(s
->pass_cred
),
434 prefix
, yes_no(s
->pass_sec
),
435 prefix
, strna(s
->tcp_congestion
));
437 if (s
->control_pid
> 0)
439 "%sControl PID: %lu\n",
440 prefix
, (unsigned long) s
->control_pid
);
442 if (s
->bind_to_device
)
444 "%sBindToDevice: %s\n",
445 prefix
, s
->bind_to_device
);
450 "%sNConnections: %u\n"
451 "%sMaxConnections: %u\n",
452 prefix
, s
->n_accepted
,
453 prefix
, s
->n_connections
,
454 prefix
, s
->max_connections
);
456 if (s
->priority
>= 0)
459 prefix
, s
->priority
);
461 if (s
->receive_buffer
> 0)
463 "%sReceiveBuffer: %zu\n",
464 prefix
, s
->receive_buffer
);
466 if (s
->send_buffer
> 0)
468 "%sSendBuffer: %zu\n",
469 prefix
, s
->send_buffer
);
481 if (s
->pipe_size
> 0)
484 prefix
, s
->pipe_size
);
491 if (s
->mq_maxmsg
> 0)
493 "%sMessageQueueMaxMessages: %li\n",
494 prefix
, s
->mq_maxmsg
);
496 if (s
->mq_msgsize
> 0)
498 "%sMessageQueueMessageSize: %li\n",
499 prefix
, s
->mq_msgsize
);
501 LIST_FOREACH(port
, p
, s
->ports
) {
503 if (p
->type
== SOCKET_SOCKET
) {
508 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
513 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(socket_address_family(&p
->address
), p
->address
.type
), t
);
515 } else if (p
->type
== SOCKET_SPECIAL
)
516 fprintf(f
, "%sListenSpecial: %s\n", prefix
, p
->path
);
517 else if (p
->type
== SOCKET_MQUEUE
)
518 fprintf(f
, "%sListenMessageQueue: %s\n", prefix
, p
->path
);
520 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
523 exec_context_dump(&s
->exec_context
, f
, prefix
);
525 for (c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
526 if (!s
->exec_command
[c
])
529 fprintf(f
, "%s-> %s:\n",
530 prefix
, socket_exec_command_to_string(c
));
532 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
538 static int instance_from_socket(int fd
, unsigned nr
, char **instance
) {
543 struct sockaddr_un un
;
544 struct sockaddr_in in
;
545 struct sockaddr_in6 in6
;
546 struct sockaddr_storage storage
;
553 if (getsockname(fd
, &local
.sa
, &l
) < 0)
557 if (getpeername(fd
, &remote
.sa
, &l
) < 0)
560 switch (local
.sa
.sa_family
) {
564 a
= ntohl(local
.in
.sin_addr
.s_addr
),
565 b
= ntohl(remote
.in
.sin_addr
.s_addr
);
568 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
570 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
571 ntohs(local
.in
.sin_port
),
572 b
>> 24, (b
>> 16) & 0xFF, (b
>> 8) & 0xFF, b
& 0xFF,
573 ntohs(remote
.in
.sin_port
)) < 0)
580 static const char ipv4_prefix
[] = {
581 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
584 if (memcmp(&local
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0 &&
585 memcmp(&remote
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
587 *a
= local
.in6
.sin6_addr
.s6_addr
+12,
588 *b
= remote
.in6
.sin6_addr
.s6_addr
+12;
591 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
593 a
[0], a
[1], a
[2], a
[3],
594 ntohs(local
.in6
.sin6_port
),
595 b
[0], b
[1], b
[2], b
[3],
596 ntohs(remote
.in6
.sin6_port
)) < 0)
599 char a
[INET6_ADDRSTRLEN
], b
[INET6_ADDRSTRLEN
];
604 inet_ntop(AF_INET6
, &local
.in6
.sin6_addr
, a
, sizeof(a
)),
605 ntohs(local
.in6
.sin6_port
),
606 inet_ntop(AF_INET6
, &remote
.in6
.sin6_addr
, b
, sizeof(b
)),
607 ntohs(remote
.in6
.sin6_port
)) < 0)
618 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
) < 0)
624 (unsigned long) ucred
.pid
,
625 (unsigned long) ucred
.uid
) < 0)
632 assert_not_reached("Unhandled socket type.");
639 static void socket_close_fds(Socket
*s
) {
644 LIST_FOREACH(port
, p
, s
->ports
) {
648 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
649 close_nointr_nofail(p
->fd
);
651 /* One little note: we should never delete any sockets
652 * in the file system here! After all some other
653 * process we spawned might still have a reference of
654 * this fd and wants to continue to use it. Therefore
655 * we delete sockets in the file system before we
656 * create a new one, not after we stopped using
663 static void socket_apply_socket_options(Socket
*s
, int fd
) {
668 int b
= s
->keep_alive
;
669 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &b
, sizeof(b
)) < 0)
670 log_warning("SO_KEEPALIVE failed: %m");
675 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &one
, sizeof(one
)) < 0)
676 log_warning("SO_BROADCAST failed: %m");
681 if (setsockopt(fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
)) < 0)
682 log_warning("SO_PASSCRED failed: %m");
687 if (setsockopt(fd
, SOL_SOCKET
, SO_PASSSEC
, &one
, sizeof(one
)) < 0)
688 log_warning("SO_PASSSEC failed: %m");
691 if (s
->priority
>= 0)
692 if (setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, &s
->priority
, sizeof(s
->priority
)) < 0)
693 log_warning("SO_PRIORITY failed: %m");
695 if (s
->receive_buffer
> 0) {
696 int value
= (int) s
->receive_buffer
;
698 /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
700 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, &value
, sizeof(value
)) < 0)
701 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &value
, sizeof(value
)) < 0)
702 log_warning("SO_RCVBUF failed: %m");
705 if (s
->send_buffer
> 0) {
706 int value
= (int) s
->send_buffer
;
707 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUFFORCE
, &value
, sizeof(value
)) < 0)
708 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &value
, sizeof(value
)) < 0)
709 log_warning("SO_SNDBUF failed: %m");
713 if (setsockopt(fd
, SOL_SOCKET
, SO_MARK
, &s
->mark
, sizeof(s
->mark
)) < 0)
714 log_warning("SO_MARK failed: %m");
717 if (setsockopt(fd
, IPPROTO_IP
, IP_TOS
, &s
->ip_tos
, sizeof(s
->ip_tos
)) < 0)
718 log_warning("IP_TOS failed: %m");
720 if (s
->ip_ttl
>= 0) {
723 r
= setsockopt(fd
, IPPROTO_IP
, IP_TTL
, &s
->ip_ttl
, sizeof(s
->ip_ttl
));
725 if (socket_ipv6_is_supported())
726 x
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &s
->ip_ttl
, sizeof(s
->ip_ttl
));
729 errno
= EAFNOSUPPORT
;
733 log_warning("IP_TTL/IPV6_UNICAST_HOPS failed: %m");
736 if (s
->tcp_congestion
)
737 if (setsockopt(fd
, SOL_TCP
, TCP_CONGESTION
, s
->tcp_congestion
, strlen(s
->tcp_congestion
)+1) < 0)
738 log_warning("TCP_CONGESTION failed: %m");
741 static void socket_apply_fifo_options(Socket
*s
, int fd
) {
745 if (s
->pipe_size
> 0)
746 if (fcntl(fd
, F_SETPIPE_SZ
, s
->pipe_size
) < 0)
747 log_warning("F_SETPIPE_SZ: %m");
750 static int fifo_address_create(
752 mode_t directory_mode
,
763 mkdir_parents(path
, directory_mode
);
765 if ((r
= label_fifofile_set(path
)) < 0)
768 /* Enforce the right access mode for the fifo */
769 old_mask
= umask(~ socket_mode
);
771 /* Include the original umask in our mask */
772 umask(~socket_mode
| old_mask
);
774 r
= mkfifo(path
, socket_mode
);
777 if (r
< 0 && errno
!= EEXIST
) {
782 if ((fd
= open(path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
789 if (fstat(fd
, &st
) < 0) {
794 if (!S_ISFIFO(st
.st_mode
) ||
795 (st
.st_mode
& 0777) != (socket_mode
& ~old_mask
) ||
796 st
.st_uid
!= getuid() ||
797 st
.st_gid
!= getgid()) {
810 close_nointr_nofail(fd
);
815 static int special_address_create(
825 if ((fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
830 if (fstat(fd
, &st
) < 0) {
835 /* Check whether this is a /proc, /sys or /dev file or char device */
836 if (!S_ISREG(st
.st_mode
) && !S_ISCHR(st
.st_mode
)) {
846 close_nointr_nofail(fd
);
851 static int mq_address_create(
861 struct mq_attr _attr
, *attr
= NULL
;
866 if (maxmsg
> 0 && msgsize
> 0) {
868 _attr
.mq_flags
= O_NONBLOCK
;
869 _attr
.mq_maxmsg
= maxmsg
;
870 _attr
.mq_msgsize
= msgsize
;
874 /* Enforce the right access mode for the mq */
875 old_mask
= umask(~ mq_mode
);
877 /* Include the original umask in our mask */
878 umask(~mq_mode
| old_mask
);
880 fd
= mq_open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_CREAT
, mq_mode
, attr
);
888 if (fstat(fd
, &st
) < 0) {
893 if ((st
.st_mode
& 0777) != (mq_mode
& ~old_mask
) ||
894 st
.st_uid
!= getuid() ||
895 st
.st_gid
!= getgid()) {
906 close_nointr_nofail(fd
);
911 static int socket_open_fds(Socket
*s
) {
915 bool know_label
= false;
919 LIST_FOREACH(port
, p
, s
->ports
) {
924 if (p
->type
== SOCKET_SOCKET
) {
928 if ((r
= socket_instantiate_service(s
)) < 0)
931 if (UNIT_DEREF(s
->service
) &&
932 SERVICE(UNIT_DEREF(s
->service
))->exec_command
[SERVICE_EXEC_START
]) {
933 r
= label_get_create_label_from_exe(SERVICE(UNIT_DEREF(s
->service
))->exec_command
[SERVICE_EXEC_START
]->path
, &label
);
944 if ((r
= socket_address_listen(
957 socket_apply_socket_options(s
, p
->fd
);
959 } else if (p
->type
== SOCKET_SPECIAL
) {
961 if ((r
= special_address_create(
966 } else if (p
->type
== SOCKET_FIFO
) {
968 if ((r
= fifo_address_create(
975 socket_apply_fifo_options(s
, p
->fd
);
976 } else if (p
->type
== SOCKET_MQUEUE
) {
978 if ((r
= mq_address_create(
986 assert_not_reached("Unknown port type");
998 static void socket_unwatch_fds(Socket
*s
) {
1003 LIST_FOREACH(port
, p
, s
->ports
) {
1007 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
1011 static int socket_watch_fds(Socket
*s
) {
1017 LIST_FOREACH(port
, p
, s
->ports
) {
1021 p
->fd_watch
.socket_accept
=
1023 p
->type
== SOCKET_SOCKET
&&
1024 socket_address_can_accept(&p
->address
);
1026 if ((r
= unit_watch_fd(UNIT(s
), p
->fd
, EPOLLIN
, &p
->fd_watch
)) < 0)
1033 socket_unwatch_fds(s
);
1037 static void socket_set_state(Socket
*s
, SocketState state
) {
1038 SocketState old_state
;
1041 old_state
= s
->state
;
1044 if (state
!= SOCKET_START_PRE
&&
1045 state
!= SOCKET_START_POST
&&
1046 state
!= SOCKET_STOP_PRE
&&
1047 state
!= SOCKET_STOP_PRE_SIGTERM
&&
1048 state
!= SOCKET_STOP_PRE_SIGKILL
&&
1049 state
!= SOCKET_STOP_POST
&&
1050 state
!= SOCKET_FINAL_SIGTERM
&&
1051 state
!= SOCKET_FINAL_SIGKILL
) {
1052 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1053 socket_unwatch_control_pid(s
);
1054 s
->control_command
= NULL
;
1055 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1058 if (state
!= SOCKET_LISTENING
)
1059 socket_unwatch_fds(s
);
1061 if (state
!= SOCKET_START_POST
&&
1062 state
!= SOCKET_LISTENING
&&
1063 state
!= SOCKET_RUNNING
&&
1064 state
!= SOCKET_STOP_PRE
&&
1065 state
!= SOCKET_STOP_PRE_SIGTERM
&&
1066 state
!= SOCKET_STOP_PRE_SIGKILL
)
1067 socket_close_fds(s
);
1069 if (state
!= old_state
)
1070 log_debug("%s changed %s -> %s",
1072 socket_state_to_string(old_state
),
1073 socket_state_to_string(state
));
1075 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], true);
1078 static int socket_coldplug(Unit
*u
) {
1079 Socket
*s
= SOCKET(u
);
1083 assert(s
->state
== SOCKET_DEAD
);
1085 if (s
->deserialized_state
!= s
->state
) {
1087 if (s
->deserialized_state
== SOCKET_START_PRE
||
1088 s
->deserialized_state
== SOCKET_START_POST
||
1089 s
->deserialized_state
== SOCKET_STOP_PRE
||
1090 s
->deserialized_state
== SOCKET_STOP_PRE_SIGTERM
||
1091 s
->deserialized_state
== SOCKET_STOP_PRE_SIGKILL
||
1092 s
->deserialized_state
== SOCKET_STOP_POST
||
1093 s
->deserialized_state
== SOCKET_FINAL_SIGTERM
||
1094 s
->deserialized_state
== SOCKET_FINAL_SIGKILL
) {
1096 if (s
->control_pid
<= 0)
1099 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1102 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1106 if (s
->deserialized_state
== SOCKET_START_POST
||
1107 s
->deserialized_state
== SOCKET_LISTENING
||
1108 s
->deserialized_state
== SOCKET_RUNNING
||
1109 s
->deserialized_state
== SOCKET_STOP_PRE
||
1110 s
->deserialized_state
== SOCKET_STOP_PRE_SIGTERM
||
1111 s
->deserialized_state
== SOCKET_STOP_PRE_SIGKILL
)
1112 if ((r
= socket_open_fds(s
)) < 0)
1115 if (s
->deserialized_state
== SOCKET_LISTENING
)
1116 if ((r
= socket_watch_fds(s
)) < 0)
1119 socket_set_state(s
, s
->deserialized_state
);
1125 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
1134 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1137 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1146 UNIT(s
)->manager
->environment
,
1150 UNIT(s
)->manager
->confirm_spawn
,
1151 UNIT(s
)->cgroup_bondings
,
1152 UNIT(s
)->cgroup_attributes
,
1159 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1160 /* FIXME: we need to do something here */
1168 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1173 static void socket_enter_dead(Socket
*s
, SocketResult f
) {
1176 if (f
!= SOCKET_SUCCESS
)
1179 socket_set_state(s
, s
->result
!= SOCKET_SUCCESS
? SOCKET_FAILED
: SOCKET_DEAD
);
1182 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
);
1184 static void socket_enter_stop_post(Socket
*s
, SocketResult f
) {
1188 if (f
!= SOCKET_SUCCESS
)
1191 socket_unwatch_control_pid(s
);
1193 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
1195 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
])) {
1196 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1199 socket_set_state(s
, SOCKET_STOP_POST
);
1201 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_SUCCESS
);
1206 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s
)->id
, strerror(-r
));
1207 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
1210 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
) {
1212 Set
*pid_set
= NULL
;
1213 bool wait_for_exit
= false;
1217 if (f
!= SOCKET_SUCCESS
)
1220 if (s
->exec_context
.kill_mode
!= KILL_NONE
) {
1221 int sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_FINAL_SIGTERM
) ? s
->exec_context
.kill_signal
: SIGKILL
;
1223 if (s
->control_pid
> 0) {
1224 if (kill_and_sigcont(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1226 log_warning("Failed to kill control process %li: %m", (long) s
->control_pid
);
1228 wait_for_exit
= true;
1231 if (s
->exec_context
.kill_mode
== KILL_CONTROL_GROUP
) {
1233 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
))) {
1238 /* Exclude the control pid from being killed via the cgroup */
1239 if (s
->control_pid
> 0)
1240 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0)
1243 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, sig
, true, pid_set
)) < 0) {
1244 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
1245 log_warning("Failed to kill control group: %s", strerror(-r
));
1247 wait_for_exit
= true;
1254 if (wait_for_exit
) {
1255 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1258 socket_set_state(s
, state
);
1259 } else if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
1260 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
1262 socket_enter_dead(s
, SOCKET_SUCCESS
);
1267 log_warning("%s failed to kill processes: %s", UNIT(s
)->id
, strerror(-r
));
1269 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
1270 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
1272 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
1278 static void socket_enter_stop_pre(Socket
*s
, SocketResult f
) {
1282 if (f
!= SOCKET_SUCCESS
)
1285 socket_unwatch_control_pid(s
);
1287 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
1289 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
])) {
1290 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1293 socket_set_state(s
, SOCKET_STOP_PRE
);
1295 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
1300 log_warning("%s failed to run 'stop-pre' task: %s", UNIT(s
)->id
, strerror(-r
));
1301 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
1304 static void socket_enter_listening(Socket
*s
) {
1308 r
= socket_watch_fds(s
);
1310 log_warning("%s failed to watch sockets: %s", UNIT(s
)->id
, strerror(-r
));
1314 socket_set_state(s
, SOCKET_LISTENING
);
1318 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1321 static void socket_enter_start_post(Socket
*s
) {
1325 r
= socket_open_fds(s
);
1327 log_warning("%s failed to listen on sockets: %s", UNIT(s
)->id
, strerror(-r
));
1331 socket_unwatch_control_pid(s
);
1333 s
->control_command_id
= SOCKET_EXEC_START_POST
;
1335 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
])) {
1336 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
1338 log_warning("%s failed to run 'start-post' task: %s", UNIT(s
)->id
, strerror(-r
));
1342 socket_set_state(s
, SOCKET_START_POST
);
1344 socket_enter_listening(s
);
1349 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1352 static void socket_enter_start_pre(Socket
*s
) {
1356 socket_unwatch_control_pid(s
);
1358 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
1360 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
])) {
1361 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1364 socket_set_state(s
, SOCKET_START_PRE
);
1366 socket_enter_start_post(s
);
1371 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s
)->id
, strerror(-r
));
1372 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
1375 static void socket_enter_running(Socket
*s
, int cfd
) {
1380 dbus_error_init(&error
);
1382 /* We don't take connections anymore if we are supposed to
1383 * shut down anyway */
1384 if (unit_pending_inactive(UNIT(s
))) {
1385 log_debug("Suppressing connection request on %s since unit stop is scheduled.", UNIT(s
)->id
);
1388 close_nointr_nofail(cfd
);
1390 /* Flush all sockets by closing and reopening them */
1391 socket_close_fds(s
);
1393 r
= socket_watch_fds(s
);
1395 log_warning("%s failed to watch sockets: %s", UNIT(s
)->id
, strerror(-r
));
1396 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1406 bool pending
= false;
1408 /* If there's already a start pending don't bother to
1410 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERS
], i
)
1411 if (unit_pending_active(u
)) {
1417 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT_DEREF(s
->service
), JOB_REPLACE
, true, &error
, NULL
);
1422 socket_set_state(s
, SOCKET_RUNNING
);
1424 char *prefix
, *instance
= NULL
, *name
;
1427 if (s
->n_connections
>= s
->max_connections
) {
1428 log_warning("Too many incoming connections (%u)", s
->n_connections
);
1429 close_nointr_nofail(cfd
);
1433 r
= socket_instantiate_service(s
);
1437 r
= instance_from_socket(cfd
, s
->n_accepted
, &instance
);
1442 /* ENOTCONN is legitimate if TCP RST was received.
1443 * This connection is over, but the socket unit lives on. */
1444 close_nointr_nofail(cfd
);
1448 prefix
= unit_name_to_prefix(UNIT(s
)->id
);
1455 name
= unit_name_build(prefix
, instance
, ".service");
1464 r
= unit_add_name(UNIT_DEREF(s
->service
), name
);
1470 service
= SERVICE(UNIT_DEREF(s
->service
));
1471 unit_ref_unset(&s
->service
);
1474 UNIT(service
)->no_gc
= false;
1476 unit_choose_id(UNIT(service
), name
);
1479 r
= service_set_socket_fd(service
, cfd
, s
);
1484 s
->n_connections
++;
1486 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(service
), JOB_REPLACE
, true, &error
, NULL
);
1490 /* Notify clients about changed counters */
1491 unit_add_to_dbus_queue(UNIT(s
));
1497 log_warning("%s failed to queue socket startup job: %s", UNIT(s
)->id
, bus_error(&error
, r
));
1498 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1501 close_nointr_nofail(cfd
);
1503 dbus_error_free(&error
);
1506 static void socket_run_next(Socket
*s
) {
1510 assert(s
->control_command
);
1511 assert(s
->control_command
->command_next
);
1513 socket_unwatch_control_pid(s
);
1515 s
->control_command
= s
->control_command
->command_next
;
1517 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1523 log_warning("%s failed to run next task: %s", UNIT(s
)->id
, strerror(-r
));
1525 if (s
->state
== SOCKET_START_POST
)
1526 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1527 else if (s
->state
== SOCKET_STOP_POST
)
1528 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
1530 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
1533 static int socket_start(Unit
*u
) {
1534 Socket
*s
= SOCKET(u
);
1538 /* We cannot fulfill this request right now, try again later
1540 if (s
->state
== SOCKET_STOP_PRE
||
1541 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
1542 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
1543 s
->state
== SOCKET_STOP_POST
||
1544 s
->state
== SOCKET_FINAL_SIGTERM
||
1545 s
->state
== SOCKET_FINAL_SIGKILL
)
1548 if (s
->state
== SOCKET_START_PRE
||
1549 s
->state
== SOCKET_START_POST
)
1552 /* Cannot run this without the service being around */
1553 if (UNIT_DEREF(s
->service
)) {
1556 service
= SERVICE(UNIT_DEREF(s
->service
));
1558 if (UNIT(service
)->load_state
!= UNIT_LOADED
) {
1559 log_error("Socket service %s not loaded, refusing.", UNIT(service
)->id
);
1563 /* If the service is already active we cannot start the
1565 if (service
->state
!= SERVICE_DEAD
&&
1566 service
->state
!= SERVICE_FAILED
&&
1567 service
->state
!= SERVICE_AUTO_RESTART
) {
1568 log_error("Socket service %s already active, refusing.", UNIT(service
)->id
);
1572 #ifdef HAVE_SYSV_COMPAT
1573 if (service
->sysv_path
) {
1574 log_error("Using SysV services for socket activation is not supported. Refusing.");
1580 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_FAILED
);
1582 s
->result
= SOCKET_SUCCESS
;
1583 socket_enter_start_pre(s
);
1587 static int socket_stop(Unit
*u
) {
1588 Socket
*s
= SOCKET(u
);
1593 if (s
->state
== SOCKET_STOP_PRE
||
1594 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
1595 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
1596 s
->state
== SOCKET_STOP_POST
||
1597 s
->state
== SOCKET_FINAL_SIGTERM
||
1598 s
->state
== SOCKET_FINAL_SIGKILL
)
1601 /* If there's already something running we go directly into
1603 if (s
->state
== SOCKET_START_PRE
||
1604 s
->state
== SOCKET_START_POST
) {
1605 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_SUCCESS
);
1609 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
1611 socket_enter_stop_pre(s
, SOCKET_SUCCESS
);
1615 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1616 Socket
*s
= SOCKET(u
);
1624 unit_serialize_item(u
, f
, "state", socket_state_to_string(s
->state
));
1625 unit_serialize_item(u
, f
, "result", socket_result_to_string(s
->result
));
1626 unit_serialize_item_format(u
, f
, "n-accepted", "%u", s
->n_accepted
);
1628 if (s
->control_pid
> 0)
1629 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
1631 if (s
->control_command_id
>= 0)
1632 unit_serialize_item(u
, f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
1634 LIST_FOREACH(port
, p
, s
->ports
) {
1640 if ((copy
= fdset_put_dup(fds
, p
->fd
)) < 0)
1643 if (p
->type
== SOCKET_SOCKET
) {
1646 if ((r
= socket_address_print(&p
->address
, &t
)) < 0)
1649 if (socket_address_family(&p
->address
) == AF_NETLINK
)
1650 unit_serialize_item_format(u
, f
, "netlink", "%i %s", copy
, t
);
1652 unit_serialize_item_format(u
, f
, "socket", "%i %i %s", copy
, p
->address
.type
, t
);
1654 } else if (p
->type
== SOCKET_SPECIAL
)
1655 unit_serialize_item_format(u
, f
, "special", "%i %s", copy
, p
->path
);
1657 assert(p
->type
== SOCKET_FIFO
);
1658 unit_serialize_item_format(u
, f
, "fifo", "%i %s", copy
, p
->path
);
1665 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1666 Socket
*s
= SOCKET(u
);
1673 if (streq(key
, "state")) {
1676 if ((state
= socket_state_from_string(value
)) < 0)
1677 log_debug("Failed to parse state value %s", value
);
1679 s
->deserialized_state
= state
;
1680 } else if (streq(key
, "result")) {
1683 f
= socket_result_from_string(value
);
1685 log_debug("Failed to parse result value %s", value
);
1686 else if (f
!= SOCKET_SUCCESS
)
1689 } else if (streq(key
, "n-accepted")) {
1692 if (safe_atou(value
, &k
) < 0)
1693 log_debug("Failed to parse n-accepted value %s", value
);
1696 } else if (streq(key
, "control-pid")) {
1699 if (parse_pid(value
, &pid
) < 0)
1700 log_debug("Failed to parse control-pid value %s", value
);
1702 s
->control_pid
= pid
;
1703 } else if (streq(key
, "control-command")) {
1704 SocketExecCommand id
;
1706 if ((id
= socket_exec_command_from_string(value
)) < 0)
1707 log_debug("Failed to parse exec-command value %s", value
);
1709 s
->control_command_id
= id
;
1710 s
->control_command
= s
->exec_command
[id
];
1712 } else if (streq(key
, "fifo")) {
1716 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1717 log_debug("Failed to parse fifo value %s", value
);
1720 LIST_FOREACH(port
, p
, s
->ports
)
1721 if (p
->type
== SOCKET_FIFO
&&
1722 streq_ptr(p
->path
, value
+skip
))
1727 close_nointr_nofail(p
->fd
);
1728 p
->fd
= fdset_remove(fds
, fd
);
1732 } else if (streq(key
, "special")) {
1736 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1737 log_debug("Failed to parse special value %s", value
);
1740 LIST_FOREACH(port
, p
, s
->ports
)
1741 if (p
->type
== SOCKET_SPECIAL
&&
1742 streq_ptr(p
->path
, value
+skip
))
1747 close_nointr_nofail(p
->fd
);
1748 p
->fd
= fdset_remove(fds
, fd
);
1752 } else if (streq(key
, "socket")) {
1753 int fd
, type
, skip
= 0;
1756 if (sscanf(value
, "%i %i %n", &fd
, &type
, &skip
) < 2 || fd
< 0 || type
< 0 || !fdset_contains(fds
, fd
))
1757 log_debug("Failed to parse socket value %s", value
);
1760 LIST_FOREACH(port
, p
, s
->ports
)
1761 if (socket_address_is(&p
->address
, value
+skip
, type
))
1766 close_nointr_nofail(p
->fd
);
1767 p
->fd
= fdset_remove(fds
, fd
);
1771 } else if (streq(key
, "netlink")) {
1775 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1776 log_debug("Failed to parse socket value %s", value
);
1779 LIST_FOREACH(port
, p
, s
->ports
)
1780 if (socket_address_is_netlink(&p
->address
, value
+skip
))
1785 close_nointr_nofail(p
->fd
);
1786 p
->fd
= fdset_remove(fds
, fd
);
1791 log_debug("Unknown serialization key '%s'", key
);
1796 static UnitActiveState
socket_active_state(Unit
*u
) {
1799 return state_translation_table
[SOCKET(u
)->state
];
1802 static const char *socket_sub_state_to_string(Unit
*u
) {
1805 return socket_state_to_string(SOCKET(u
)->state
);
1808 static bool socket_check_gc(Unit
*u
) {
1809 Socket
*s
= SOCKET(u
);
1813 return s
->n_connections
> 0;
1816 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
1817 Socket
*s
= SOCKET(u
);
1823 if (s
->state
!= SOCKET_LISTENING
)
1826 log_debug("Incoming traffic on %s", u
->id
);
1828 if (events
!= EPOLLIN
) {
1830 if (events
& EPOLLHUP
)
1831 log_error("%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", u
->id
);
1833 log_error("%s: Got unexpected poll event (0x%x) on socket.", u
->id
, events
);
1838 if (w
->socket_accept
) {
1841 if ((cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
)) < 0) {
1846 log_error("Failed to accept socket: %m");
1853 socket_apply_socket_options(s
, cfd
);
1856 socket_enter_running(s
, cfd
);
1860 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1863 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1864 Socket
*s
= SOCKET(u
);
1870 if (pid
!= s
->control_pid
)
1875 if (is_clean_exit(code
, status
))
1877 else if (code
== CLD_EXITED
)
1878 f
= SOCKET_FAILURE_EXIT_CODE
;
1879 else if (code
== CLD_KILLED
)
1880 f
= SOCKET_FAILURE_SIGNAL
;
1881 else if (code
== CLD_DUMPED
)
1882 f
= SOCKET_FAILURE_CORE_DUMP
;
1884 assert_not_reached("Unknown code");
1886 if (s
->control_command
) {
1887 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
1889 if (s
->control_command
->ignore
)
1893 log_full(f
== SOCKET_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
1894 "%s control process exited, code=%s status=%i", u
->id
, sigchld_code_to_string(code
), status
);
1896 if (f
!= SOCKET_SUCCESS
)
1899 if (s
->control_command
&&
1900 s
->control_command
->command_next
&&
1901 f
== SOCKET_SUCCESS
) {
1903 log_debug("%s running next command for state %s", u
->id
, socket_state_to_string(s
->state
));
1906 s
->control_command
= NULL
;
1907 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1909 /* No further commands for this step, so let's figure
1910 * out what to do next */
1912 log_debug("%s got final SIGCHLD for state %s", u
->id
, socket_state_to_string(s
->state
));
1916 case SOCKET_START_PRE
:
1917 if (f
== SOCKET_SUCCESS
)
1918 socket_enter_start_post(s
);
1920 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, f
);
1923 case SOCKET_START_POST
:
1924 if (f
== SOCKET_SUCCESS
)
1925 socket_enter_listening(s
);
1927 socket_enter_stop_pre(s
, f
);
1930 case SOCKET_STOP_PRE
:
1931 case SOCKET_STOP_PRE_SIGTERM
:
1932 case SOCKET_STOP_PRE_SIGKILL
:
1933 socket_enter_stop_post(s
, f
);
1936 case SOCKET_STOP_POST
:
1937 case SOCKET_FINAL_SIGTERM
:
1938 case SOCKET_FINAL_SIGKILL
:
1939 socket_enter_dead(s
, f
);
1943 assert_not_reached("Uh, control process died at wrong time.");
1947 /* Notify clients about changed exit status */
1948 unit_add_to_dbus_queue(u
);
1951 static void socket_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
1952 Socket
*s
= SOCKET(u
);
1955 assert(elapsed
== 1);
1956 assert(w
== &s
->timer_watch
);
1960 case SOCKET_START_PRE
:
1961 log_warning("%s starting timed out. Terminating.", u
->id
);
1962 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
1965 case SOCKET_START_POST
:
1966 log_warning("%s starting timed out. Stopping.", u
->id
);
1967 socket_enter_stop_pre(s
, SOCKET_FAILURE_TIMEOUT
);
1970 case SOCKET_STOP_PRE
:
1971 log_warning("%s stopping timed out. Terminating.", u
->id
);
1972 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
1975 case SOCKET_STOP_PRE_SIGTERM
:
1976 if (s
->exec_context
.send_sigkill
) {
1977 log_warning("%s stopping timed out. Killing.", u
->id
);
1978 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
1980 log_warning("%s stopping timed out. Skipping SIGKILL. Ignoring.", u
->id
);
1981 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
1985 case SOCKET_STOP_PRE_SIGKILL
:
1986 log_warning("%s still around after SIGKILL. Ignoring.", u
->id
);
1987 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
1990 case SOCKET_STOP_POST
:
1991 log_warning("%s stopping timed out (2). Terminating.", u
->id
);
1992 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
1995 case SOCKET_FINAL_SIGTERM
:
1996 if (s
->exec_context
.send_sigkill
) {
1997 log_warning("%s stopping timed out (2). Killing.", u
->id
);
1998 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
2000 log_warning("%s stopping timed out (2). Skipping SIGKILL. Ignoring.", u
->id
);
2001 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
2005 case SOCKET_FINAL_SIGKILL
:
2006 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u
->id
);
2007 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
2011 assert_not_reached("Timeout at wrong time.");
2015 int socket_collect_fds(Socket
*s
, int **fds
, unsigned *n_fds
) {
2024 /* Called from the service code for requesting our fds */
2027 LIST_FOREACH(port
, p
, s
->ports
)
2037 if (!(rfds
= new(int, rn_fds
)))
2041 LIST_FOREACH(port
, p
, s
->ports
)
2045 assert(k
== rn_fds
);
2053 void socket_notify_service_dead(Socket
*s
, bool failed_permanent
) {
2056 /* The service is dead. Dang!
2058 * This is strictly for one-instance-for-all-connections
2061 if (s
->state
== SOCKET_RUNNING
) {
2062 log_debug("%s got notified about service death (failed permanently: %s)", UNIT(s
)->id
, yes_no(failed_permanent
));
2063 if (failed_permanent
)
2064 socket_enter_stop_pre(s
, SOCKET_FAILURE_SERVICE_FAILED_PERMANENT
);
2066 socket_enter_listening(s
);
2070 void socket_connection_unref(Socket
*s
) {
2073 /* The service is dead. Yay!
2075 * This is strictly for one-instance-per-connection
2078 assert(s
->n_connections
> 0);
2081 log_debug("%s: One connection closed, %u left.", UNIT(s
)->id
, s
->n_connections
);
2084 static void socket_reset_failed(Unit
*u
) {
2085 Socket
*s
= SOCKET(u
);
2089 if (s
->state
== SOCKET_FAILED
)
2090 socket_set_state(s
, SOCKET_DEAD
);
2092 s
->result
= SOCKET_SUCCESS
;
2095 static int socket_kill(Unit
*u
, KillWho who
, KillMode mode
, int signo
, DBusError
*error
) {
2096 Socket
*s
= SOCKET(u
);
2098 Set
*pid_set
= NULL
;
2102 if (who
== KILL_MAIN
) {
2103 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "Socket units have no main processes");
2107 if (s
->control_pid
<= 0 && who
== KILL_CONTROL
) {
2108 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No control process to kill");
2112 if (who
== KILL_CONTROL
|| who
== KILL_ALL
)
2113 if (s
->control_pid
> 0)
2114 if (kill(s
->control_pid
, signo
) < 0)
2117 if (who
== KILL_ALL
&& mode
== KILL_CONTROL_GROUP
) {
2120 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
)))
2123 /* Exclude the control pid from being killed via the cgroup */
2124 if (s
->control_pid
> 0)
2125 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0) {
2130 if ((q
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, signo
, false, pid_set
)) < 0)
2131 if (q
!= -EAGAIN
&& q
!= -ESRCH
&& q
!= -ENOENT
)
2142 static const char* const socket_state_table
[_SOCKET_STATE_MAX
] = {
2143 [SOCKET_DEAD
] = "dead",
2144 [SOCKET_START_PRE
] = "start-pre",
2145 [SOCKET_START_POST
] = "start-post",
2146 [SOCKET_LISTENING
] = "listening",
2147 [SOCKET_RUNNING
] = "running",
2148 [SOCKET_STOP_PRE
] = "stop-pre",
2149 [SOCKET_STOP_PRE_SIGTERM
] = "stop-pre-sigterm",
2150 [SOCKET_STOP_PRE_SIGKILL
] = "stop-pre-sigkill",
2151 [SOCKET_STOP_POST
] = "stop-post",
2152 [SOCKET_FINAL_SIGTERM
] = "final-sigterm",
2153 [SOCKET_FINAL_SIGKILL
] = "final-sigkill",
2154 [SOCKET_FAILED
] = "failed"
2157 DEFINE_STRING_TABLE_LOOKUP(socket_state
, SocketState
);
2159 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
2160 [SOCKET_EXEC_START_PRE
] = "StartPre",
2161 [SOCKET_EXEC_START_POST
] = "StartPost",
2162 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
2163 [SOCKET_EXEC_STOP_POST
] = "StopPost"
2166 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
2168 static const char* const socket_result_table
[_SOCKET_RESULT_MAX
] = {
2169 [SOCKET_SUCCESS
] = "success",
2170 [SOCKET_FAILURE_RESOURCES
] = "resources",
2171 [SOCKET_FAILURE_TIMEOUT
] = "timeout",
2172 [SOCKET_FAILURE_EXIT_CODE
] = "exit-code",
2173 [SOCKET_FAILURE_SIGNAL
] = "signal",
2174 [SOCKET_FAILURE_CORE_DUMP
] = "core-dump",
2175 [SOCKET_FAILURE_SERVICE_FAILED_PERMANENT
] = "service-failed-permanent"
2178 DEFINE_STRING_TABLE_LOOKUP(socket_result
, SocketResult
);
2180 const UnitVTable socket_vtable
= {
2181 .suffix
= ".socket",
2182 .object_size
= sizeof(Socket
),
2188 .init
= socket_init
,
2189 .done
= socket_done
,
2190 .load
= socket_load
,
2192 .kill
= socket_kill
,
2194 .coldplug
= socket_coldplug
,
2196 .dump
= socket_dump
,
2198 .start
= socket_start
,
2199 .stop
= socket_stop
,
2201 .serialize
= socket_serialize
,
2202 .deserialize_item
= socket_deserialize_item
,
2204 .active_state
= socket_active_state
,
2205 .sub_state_to_string
= socket_sub_state_to_string
,
2207 .check_gc
= socket_check_gc
,
2209 .fd_event
= socket_fd_event
,
2210 .sigchld_event
= socket_sigchld_event
,
2211 .timer_event
= socket_timer_event
,
2213 .reset_failed
= socket_reset_failed
,
2215 .bus_interface
= "org.freedesktop.systemd1.Socket",
2216 .bus_message_handler
= bus_socket_message_handler
,
2217 .bus_invalidating_properties
= bus_socket_invalidating_properties