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"
39 #include "unit-name.h"
40 #include "dbus-socket.h"
43 #include "bus-errors.h"
45 #include "exit-status.h"
48 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
49 [SOCKET_DEAD
] = UNIT_INACTIVE
,
50 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
51 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
52 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
53 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
54 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
55 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
56 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
57 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
58 [SOCKET_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
59 [SOCKET_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
60 [SOCKET_FAILED
] = UNIT_FAILED
63 static void socket_init(Unit
*u
) {
64 Socket
*s
= SOCKET(u
);
67 assert(u
->load_state
== UNIT_STUB
);
69 s
->backlog
= SOMAXCONN
;
70 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
71 s
->directory_mode
= 0755;
72 s
->socket_mode
= 0666;
74 s
->max_connections
= 64;
81 exec_context_init(&s
->exec_context
);
82 s
->exec_context
.std_output
= u
->manager
->default_std_output
;
83 s
->exec_context
.std_error
= u
->manager
->default_std_error
;
85 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
88 static void socket_unwatch_control_pid(Socket
*s
) {
91 if (s
->control_pid
<= 0)
94 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
98 static void socket_done(Unit
*u
) {
99 Socket
*s
= SOCKET(u
);
104 while ((p
= s
->ports
)) {
105 LIST_REMOVE(SocketPort
, port
, s
->ports
, p
);
108 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
109 close_nointr_nofail(p
->fd
);
116 exec_context_done(&s
->exec_context
);
117 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
118 s
->control_command
= NULL
;
120 socket_unwatch_control_pid(s
);
122 unit_ref_unset(&s
->service
);
124 free(s
->tcp_congestion
);
125 s
->tcp_congestion
= NULL
;
127 free(s
->bind_to_device
);
128 s
->bind_to_device
= NULL
;
130 unit_unwatch_timer(u
, &s
->timer_watch
);
133 static int socket_instantiate_service(Socket
*s
) {
140 /* This fills in s->service if it isn't filled in yet. For
141 * Accept=yes sockets we create the next connection service
142 * here. For Accept=no this is mostly a NOP since the service
143 * is figured out at load time anyway. */
145 if (UNIT_DEREF(s
->service
))
150 if (!(prefix
= unit_name_to_prefix(UNIT(s
)->id
)))
153 r
= asprintf(&name
, "%s@%u.service", prefix
, s
->n_accepted
);
159 r
= manager_load_unit(UNIT(s
)->manager
, name
, NULL
, NULL
, &u
);
165 #ifdef HAVE_SYSV_COMPAT
166 if (SERVICE(u
)->sysv_path
) {
167 log_error("Using SysV services for socket activation is not supported. Refusing.");
173 unit_ref_set(&s
->service
, u
);
175 return unit_add_two_dependencies(UNIT(s
), UNIT_BEFORE
, UNIT_TRIGGERS
, u
, false);
178 static bool have_non_accept_socket(Socket
*s
) {
186 LIST_FOREACH(port
, p
, s
->ports
) {
188 if (p
->type
!= SOCKET_SOCKET
)
191 if (!socket_address_can_accept(&p
->address
))
198 static int socket_verify(Socket
*s
) {
201 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
205 log_error("%s lacks Listen setting. Refusing.", UNIT(s
)->id
);
209 if (s
->accept
&& have_non_accept_socket(s
)) {
210 log_error("%s configured for accepting sockets, but sockets are non-accepting. Refusing.", UNIT(s
)->id
);
214 if (s
->accept
&& s
->max_connections
<= 0) {
215 log_error("%s's MaxConnection setting too small. Refusing.", UNIT(s
)->id
);
219 if (s
->accept
&& UNIT_DEREF(s
->service
)) {
220 log_error("Explicit service configuration for accepting sockets not supported on %s. Refusing.", UNIT(s
)->id
);
224 if (s
->exec_context
.pam_name
&& s
->exec_context
.kill_mode
!= KILL_CONTROL_GROUP
) {
225 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", UNIT(s
)->id
);
232 static bool socket_needs_mount(Socket
*s
, const char *prefix
) {
237 LIST_FOREACH(port
, p
, s
->ports
) {
239 if (p
->type
== SOCKET_SOCKET
) {
240 if (socket_address_needs_mount(&p
->address
, prefix
))
242 } else if (p
->type
== SOCKET_FIFO
|| p
->type
== SOCKET_SPECIAL
) {
243 if (path_startswith(p
->path
, prefix
))
251 int socket_add_one_mount_link(Socket
*s
, Mount
*m
) {
257 if (UNIT(s
)->load_state
!= UNIT_LOADED
||
258 UNIT(m
)->load_state
!= UNIT_LOADED
)
261 if (!socket_needs_mount(s
, m
->where
))
264 if ((r
= unit_add_two_dependencies(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, UNIT(m
), true)) < 0)
270 static int socket_add_mount_links(Socket
*s
) {
276 LIST_FOREACH(units_by_type
, other
, UNIT(s
)->manager
->units_by_type
[UNIT_MOUNT
])
277 if ((r
= socket_add_one_mount_link(s
, MOUNT(other
))) < 0)
283 static int socket_add_device_link(Socket
*s
) {
289 if (!s
->bind_to_device
)
292 if (asprintf(&t
, "/sys/subsystem/net/devices/%s", s
->bind_to_device
) < 0)
295 r
= unit_add_node_link(UNIT(s
), t
, false);
301 static int socket_add_default_dependencies(Socket
*s
) {
305 if (UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
) {
306 if ((r
= unit_add_dependency_by_name(UNIT(s
), UNIT_BEFORE
, SPECIAL_SOCKETS_TARGET
, NULL
, true)) < 0)
309 if ((r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true)) < 0)
313 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
316 static bool socket_has_exec(Socket
*s
) {
320 for (i
= 0; i
< _SOCKET_EXEC_COMMAND_MAX
; i
++)
321 if (s
->exec_command
[i
])
327 static int socket_load(Unit
*u
) {
328 Socket
*s
= SOCKET(u
);
332 assert(u
->load_state
== UNIT_STUB
);
334 if ((r
= unit_load_fragment_and_dropin(u
)) < 0)
337 /* This is a new unit? Then let's add in some extras */
338 if (u
->load_state
== UNIT_LOADED
) {
340 if (have_non_accept_socket(s
)) {
342 if (!UNIT_DEREF(s
->service
)) {
345 r
= unit_load_related_unit(u
, ".service", &x
);
349 unit_ref_set(&s
->service
, x
);
352 r
= unit_add_two_dependencies(u
, UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT_DEREF(s
->service
), true);
357 if ((r
= socket_add_mount_links(s
)) < 0)
360 if ((r
= socket_add_device_link(s
)) < 0)
363 if (socket_has_exec(s
))
364 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
367 if ((r
= unit_add_default_cgroups(u
)) < 0)
370 if (UNIT(s
)->default_dependencies
)
371 if ((r
= socket_add_default_dependencies(s
)) < 0)
375 return socket_verify(s
);
378 static const char* listen_lookup(int family
, int type
) {
380 if (family
== AF_NETLINK
)
381 return "ListenNetlink";
383 if (type
== SOCK_STREAM
)
384 return "ListenStream";
385 else if (type
== SOCK_DGRAM
)
386 return "ListenDatagram";
387 else if (type
== SOCK_SEQPACKET
)
388 return "ListenSequentialPacket";
390 assert_not_reached("Unknown socket type");
394 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
397 Socket
*s
= SOCKET(u
);
405 p2
= strappend(prefix
, "\t");
406 prefix2
= p2
? p2
: prefix
;
409 "%sSocket State: %s\n"
411 "%sBindIPv6Only: %s\n"
413 "%sSocketMode: %04o\n"
414 "%sDirectoryMode: %04o\n"
417 "%sTransparent: %s\n"
419 "%sPassCredentials: %s\n"
420 "%sTCPCongestion: %s\n",
421 prefix
, socket_state_to_string(s
->state
),
422 prefix
, socket_result_to_string(s
->result
),
423 prefix
, socket_address_bind_ipv6_only_to_string(s
->bind_ipv6_only
),
425 prefix
, s
->socket_mode
,
426 prefix
, s
->directory_mode
,
427 prefix
, yes_no(s
->keep_alive
),
428 prefix
, yes_no(s
->free_bind
),
429 prefix
, yes_no(s
->transparent
),
430 prefix
, yes_no(s
->broadcast
),
431 prefix
, yes_no(s
->pass_cred
),
432 prefix
, strna(s
->tcp_congestion
));
434 if (s
->control_pid
> 0)
436 "%sControl PID: %lu\n",
437 prefix
, (unsigned long) s
->control_pid
);
439 if (s
->bind_to_device
)
441 "%sBindToDevice: %s\n",
442 prefix
, s
->bind_to_device
);
447 "%sNConnections: %u\n"
448 "%sMaxConnections: %u\n",
449 prefix
, s
->n_accepted
,
450 prefix
, s
->n_connections
,
451 prefix
, s
->max_connections
);
453 if (s
->priority
>= 0)
456 prefix
, s
->priority
);
458 if (s
->receive_buffer
> 0)
460 "%sReceiveBuffer: %zu\n",
461 prefix
, s
->receive_buffer
);
463 if (s
->send_buffer
> 0)
465 "%sSendBuffer: %zu\n",
466 prefix
, s
->send_buffer
);
478 if (s
->pipe_size
> 0)
481 prefix
, s
->pipe_size
);
488 if (s
->mq_maxmsg
> 0)
490 "%sMessageQueueMaxMessages: %li\n",
491 prefix
, s
->mq_maxmsg
);
493 if (s
->mq_msgsize
> 0)
495 "%sMessageQueueMessageSize: %li\n",
496 prefix
, s
->mq_msgsize
);
498 LIST_FOREACH(port
, p
, s
->ports
) {
500 if (p
->type
== SOCKET_SOCKET
) {
505 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
510 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(socket_address_family(&p
->address
), p
->address
.type
), t
);
512 } else if (p
->type
== SOCKET_SPECIAL
)
513 fprintf(f
, "%sListenSpecial: %s\n", prefix
, p
->path
);
514 else if (p
->type
== SOCKET_MQUEUE
)
515 fprintf(f
, "%sListenMessageQueue: %s\n", prefix
, p
->path
);
517 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
520 exec_context_dump(&s
->exec_context
, f
, prefix
);
522 for (c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
523 if (!s
->exec_command
[c
])
526 fprintf(f
, "%s-> %s:\n",
527 prefix
, socket_exec_command_to_string(c
));
529 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
535 static int instance_from_socket(int fd
, unsigned nr
, char **instance
) {
540 struct sockaddr_un un
;
541 struct sockaddr_in in
;
542 struct sockaddr_in6 in6
;
543 struct sockaddr_storage storage
;
550 if (getsockname(fd
, &local
.sa
, &l
) < 0)
554 if (getpeername(fd
, &remote
.sa
, &l
) < 0)
557 switch (local
.sa
.sa_family
) {
561 a
= ntohl(local
.in
.sin_addr
.s_addr
),
562 b
= ntohl(remote
.in
.sin_addr
.s_addr
);
565 "%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
566 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
567 ntohs(local
.in
.sin_port
),
568 b
>> 24, (b
>> 16) & 0xFF, (b
>> 8) & 0xFF, b
& 0xFF,
569 ntohs(remote
.in
.sin_port
)) < 0)
576 static const char ipv4_prefix
[] = {
577 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
580 if (memcmp(&local
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0 &&
581 memcmp(&remote
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
583 *a
= local
.in6
.sin6_addr
.s6_addr
+12,
584 *b
= remote
.in6
.sin6_addr
.s6_addr
+12;
587 "%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
588 a
[0], a
[1], a
[2], a
[3],
589 ntohs(local
.in6
.sin6_port
),
590 b
[0], b
[1], b
[2], b
[3],
591 ntohs(remote
.in6
.sin6_port
)) < 0)
594 char a
[INET6_ADDRSTRLEN
], b
[INET6_ADDRSTRLEN
];
598 inet_ntop(AF_INET6
, &local
.in6
.sin6_addr
, a
, sizeof(a
)),
599 ntohs(local
.in6
.sin6_port
),
600 inet_ntop(AF_INET6
, &remote
.in6
.sin6_addr
, b
, sizeof(b
)),
601 ntohs(remote
.in6
.sin6_port
)) < 0)
612 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
) < 0)
618 (unsigned long) ucred
.pid
,
619 (unsigned long) ucred
.uid
) < 0)
626 assert_not_reached("Unhandled socket type.");
633 static void socket_close_fds(Socket
*s
) {
638 LIST_FOREACH(port
, p
, s
->ports
) {
642 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
643 close_nointr_nofail(p
->fd
);
645 /* One little note: we should never delete any sockets
646 * in the file system here! After all some other
647 * process we spawned might still have a reference of
648 * this fd and wants to continue to use it. Therefore
649 * we delete sockets in the file system before we
650 * create a new one, not after we stopped using
657 static void socket_apply_socket_options(Socket
*s
, int fd
) {
662 int b
= s
->keep_alive
;
663 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &b
, sizeof(b
)) < 0)
664 log_warning("SO_KEEPALIVE failed: %m");
669 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &one
, sizeof(one
)) < 0)
670 log_warning("SO_BROADCAST failed: %m");
675 if (setsockopt(fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
)) < 0)
676 log_warning("SO_PASSCRED failed: %m");
679 if (s
->priority
>= 0)
680 if (setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, &s
->priority
, sizeof(s
->priority
)) < 0)
681 log_warning("SO_PRIORITY failed: %m");
683 if (s
->receive_buffer
> 0) {
684 int value
= (int) s
->receive_buffer
;
686 /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
688 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, &value
, sizeof(value
)) < 0)
689 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &value
, sizeof(value
)) < 0)
690 log_warning("SO_RCVBUF failed: %m");
693 if (s
->send_buffer
> 0) {
694 int value
= (int) s
->send_buffer
;
695 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUFFORCE
, &value
, sizeof(value
)) < 0)
696 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &value
, sizeof(value
)) < 0)
697 log_warning("SO_SNDBUF failed: %m");
701 if (setsockopt(fd
, SOL_SOCKET
, SO_MARK
, &s
->mark
, sizeof(s
->mark
)) < 0)
702 log_warning("SO_MARK failed: %m");
705 if (setsockopt(fd
, IPPROTO_IP
, IP_TOS
, &s
->ip_tos
, sizeof(s
->ip_tos
)) < 0)
706 log_warning("IP_TOS failed: %m");
708 if (s
->ip_ttl
>= 0) {
711 r
= setsockopt(fd
, IPPROTO_IP
, IP_TTL
, &s
->ip_ttl
, sizeof(s
->ip_ttl
));
713 if (socket_ipv6_is_supported())
714 x
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &s
->ip_ttl
, sizeof(s
->ip_ttl
));
717 errno
= EAFNOSUPPORT
;
721 log_warning("IP_TTL/IPV6_UNICAST_HOPS failed: %m");
724 if (s
->tcp_congestion
)
725 if (setsockopt(fd
, SOL_TCP
, TCP_CONGESTION
, s
->tcp_congestion
, strlen(s
->tcp_congestion
)+1) < 0)
726 log_warning("TCP_CONGESTION failed: %m");
729 static void socket_apply_fifo_options(Socket
*s
, int fd
) {
733 if (s
->pipe_size
> 0)
734 if (fcntl(fd
, F_SETPIPE_SZ
, s
->pipe_size
) < 0)
735 log_warning("F_SETPIPE_SZ: %m");
738 static int fifo_address_create(
740 mode_t directory_mode
,
751 mkdir_parents(path
, directory_mode
);
753 if ((r
= label_fifofile_set(path
)) < 0)
756 /* Enforce the right access mode for the fifo */
757 old_mask
= umask(~ socket_mode
);
759 /* Include the original umask in our mask */
760 umask(~socket_mode
| old_mask
);
762 r
= mkfifo(path
, socket_mode
);
765 if (r
< 0 && errno
!= EEXIST
) {
770 if ((fd
= open(path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
777 if (fstat(fd
, &st
) < 0) {
782 if (!S_ISFIFO(st
.st_mode
) ||
783 (st
.st_mode
& 0777) != (socket_mode
& ~old_mask
) ||
784 st
.st_uid
!= getuid() ||
785 st
.st_gid
!= getgid()) {
798 close_nointr_nofail(fd
);
803 static int special_address_create(
813 if ((fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
818 if (fstat(fd
, &st
) < 0) {
823 /* Check whether this is a /proc, /sys or /dev file or char device */
824 if (!S_ISREG(st
.st_mode
) && !S_ISCHR(st
.st_mode
)) {
834 close_nointr_nofail(fd
);
839 static int mq_address_create(
849 struct mq_attr _attr
, *attr
= NULL
;
854 if (maxmsg
> 0 && msgsize
> 0) {
856 _attr
.mq_flags
= O_NONBLOCK
;
857 _attr
.mq_maxmsg
= maxmsg
;
858 _attr
.mq_msgsize
= msgsize
;
862 /* Enforce the right access mode for the mq */
863 old_mask
= umask(~ mq_mode
);
865 /* Include the original umask in our mask */
866 umask(~mq_mode
| old_mask
);
868 fd
= mq_open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_CREAT
, mq_mode
, attr
);
876 if (fstat(fd
, &st
) < 0) {
881 if ((st
.st_mode
& 0777) != (mq_mode
& ~old_mask
) ||
882 st
.st_uid
!= getuid() ||
883 st
.st_gid
!= getgid()) {
894 close_nointr_nofail(fd
);
899 static int socket_open_fds(Socket
*s
) {
903 bool know_label
= false;
907 LIST_FOREACH(port
, p
, s
->ports
) {
912 if (p
->type
== SOCKET_SOCKET
) {
916 if ((r
= socket_instantiate_service(s
)) < 0)
919 if (UNIT_DEREF(s
->service
) &&
920 SERVICE(UNIT_DEREF(s
->service
))->exec_command
[SERVICE_EXEC_START
]) {
921 r
= label_get_create_label_from_exe(SERVICE(UNIT_DEREF(s
->service
))->exec_command
[SERVICE_EXEC_START
]->path
, &label
);
932 if ((r
= socket_address_listen(
945 socket_apply_socket_options(s
, p
->fd
);
947 } else if (p
->type
== SOCKET_SPECIAL
) {
949 if ((r
= special_address_create(
954 } else if (p
->type
== SOCKET_FIFO
) {
956 if ((r
= fifo_address_create(
963 socket_apply_fifo_options(s
, p
->fd
);
964 } else if (p
->type
== SOCKET_MQUEUE
) {
966 if ((r
= mq_address_create(
974 assert_not_reached("Unknown port type");
986 static void socket_unwatch_fds(Socket
*s
) {
991 LIST_FOREACH(port
, p
, s
->ports
) {
995 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
999 static int socket_watch_fds(Socket
*s
) {
1005 LIST_FOREACH(port
, p
, s
->ports
) {
1009 p
->fd_watch
.socket_accept
=
1011 p
->type
== SOCKET_SOCKET
&&
1012 socket_address_can_accept(&p
->address
);
1014 if ((r
= unit_watch_fd(UNIT(s
), p
->fd
, EPOLLIN
, &p
->fd_watch
)) < 0)
1021 socket_unwatch_fds(s
);
1025 static void socket_set_state(Socket
*s
, SocketState state
) {
1026 SocketState old_state
;
1029 old_state
= s
->state
;
1032 if (state
!= SOCKET_START_PRE
&&
1033 state
!= SOCKET_START_POST
&&
1034 state
!= SOCKET_STOP_PRE
&&
1035 state
!= SOCKET_STOP_PRE_SIGTERM
&&
1036 state
!= SOCKET_STOP_PRE_SIGKILL
&&
1037 state
!= SOCKET_STOP_POST
&&
1038 state
!= SOCKET_FINAL_SIGTERM
&&
1039 state
!= SOCKET_FINAL_SIGKILL
) {
1040 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1041 socket_unwatch_control_pid(s
);
1042 s
->control_command
= NULL
;
1043 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1046 if (state
!= SOCKET_LISTENING
)
1047 socket_unwatch_fds(s
);
1049 if (state
!= SOCKET_START_POST
&&
1050 state
!= SOCKET_LISTENING
&&
1051 state
!= SOCKET_RUNNING
&&
1052 state
!= SOCKET_STOP_PRE
&&
1053 state
!= SOCKET_STOP_PRE_SIGTERM
&&
1054 state
!= SOCKET_STOP_PRE_SIGKILL
)
1055 socket_close_fds(s
);
1057 if (state
!= old_state
)
1058 log_debug("%s changed %s -> %s",
1060 socket_state_to_string(old_state
),
1061 socket_state_to_string(state
));
1063 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], true);
1066 static int socket_coldplug(Unit
*u
) {
1067 Socket
*s
= SOCKET(u
);
1071 assert(s
->state
== SOCKET_DEAD
);
1073 if (s
->deserialized_state
!= s
->state
) {
1075 if (s
->deserialized_state
== SOCKET_START_PRE
||
1076 s
->deserialized_state
== SOCKET_START_POST
||
1077 s
->deserialized_state
== SOCKET_STOP_PRE
||
1078 s
->deserialized_state
== SOCKET_STOP_PRE_SIGTERM
||
1079 s
->deserialized_state
== SOCKET_STOP_PRE_SIGKILL
||
1080 s
->deserialized_state
== SOCKET_STOP_POST
||
1081 s
->deserialized_state
== SOCKET_FINAL_SIGTERM
||
1082 s
->deserialized_state
== SOCKET_FINAL_SIGKILL
) {
1084 if (s
->control_pid
<= 0)
1087 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1090 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1094 if (s
->deserialized_state
== SOCKET_START_POST
||
1095 s
->deserialized_state
== SOCKET_LISTENING
||
1096 s
->deserialized_state
== SOCKET_RUNNING
||
1097 s
->deserialized_state
== SOCKET_STOP_PRE
||
1098 s
->deserialized_state
== SOCKET_STOP_PRE_SIGTERM
||
1099 s
->deserialized_state
== SOCKET_STOP_PRE_SIGKILL
)
1100 if ((r
= socket_open_fds(s
)) < 0)
1103 if (s
->deserialized_state
== SOCKET_LISTENING
)
1104 if ((r
= socket_watch_fds(s
)) < 0)
1107 socket_set_state(s
, s
->deserialized_state
);
1113 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
1122 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1125 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1134 UNIT(s
)->manager
->environment
,
1138 UNIT(s
)->manager
->confirm_spawn
,
1139 UNIT(s
)->cgroup_bondings
,
1140 UNIT(s
)->cgroup_attributes
,
1147 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1148 /* FIXME: we need to do something here */
1156 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1161 static void socket_enter_dead(Socket
*s
, SocketResult f
) {
1164 if (f
!= SOCKET_SUCCESS
)
1167 socket_set_state(s
, s
->result
!= SOCKET_SUCCESS
? SOCKET_FAILED
: SOCKET_DEAD
);
1170 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
);
1172 static void socket_enter_stop_post(Socket
*s
, SocketResult f
) {
1176 if (f
!= SOCKET_SUCCESS
)
1179 socket_unwatch_control_pid(s
);
1181 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
1183 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
])) {
1184 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1187 socket_set_state(s
, SOCKET_STOP_POST
);
1189 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_SUCCESS
);
1194 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s
)->id
, strerror(-r
));
1195 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
1198 static void socket_enter_signal(Socket
*s
, SocketState state
, SocketResult f
) {
1200 Set
*pid_set
= NULL
;
1201 bool wait_for_exit
= false;
1205 if (f
!= SOCKET_SUCCESS
)
1208 if (s
->exec_context
.kill_mode
!= KILL_NONE
) {
1209 int sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_FINAL_SIGTERM
) ? s
->exec_context
.kill_signal
: SIGKILL
;
1211 if (s
->control_pid
> 0) {
1212 if (kill_and_sigcont(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1214 log_warning("Failed to kill control process %li: %m", (long) s
->control_pid
);
1216 wait_for_exit
= true;
1219 if (s
->exec_context
.kill_mode
== KILL_CONTROL_GROUP
) {
1221 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
))) {
1226 /* Exclude the control pid from being killed via the cgroup */
1227 if (s
->control_pid
> 0)
1228 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0)
1231 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, sig
, true, pid_set
)) < 0) {
1232 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
1233 log_warning("Failed to kill control group: %s", strerror(-r
));
1235 wait_for_exit
= true;
1242 if (wait_for_exit
) {
1243 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1246 socket_set_state(s
, state
);
1247 } else if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
1248 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
1250 socket_enter_dead(s
, SOCKET_SUCCESS
);
1255 log_warning("%s failed to kill processes: %s", UNIT(s
)->id
, strerror(-r
));
1257 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
1258 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
1260 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
1266 static void socket_enter_stop_pre(Socket
*s
, SocketResult f
) {
1270 if (f
!= SOCKET_SUCCESS
)
1273 socket_unwatch_control_pid(s
);
1275 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
1277 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
])) {
1278 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1281 socket_set_state(s
, SOCKET_STOP_PRE
);
1283 socket_enter_stop_post(s
, SOCKET_SUCCESS
);
1288 log_warning("%s failed to run 'stop-pre' task: %s", UNIT(s
)->id
, strerror(-r
));
1289 socket_enter_stop_post(s
, SOCKET_FAILURE_RESOURCES
);
1292 static void socket_enter_listening(Socket
*s
) {
1296 r
= socket_watch_fds(s
);
1298 log_warning("%s failed to watch sockets: %s", UNIT(s
)->id
, strerror(-r
));
1302 socket_set_state(s
, SOCKET_LISTENING
);
1306 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1309 static void socket_enter_start_post(Socket
*s
) {
1313 r
= socket_open_fds(s
);
1315 log_warning("%s failed to listen on sockets: %s", UNIT(s
)->id
, strerror(-r
));
1319 socket_unwatch_control_pid(s
);
1321 s
->control_command_id
= SOCKET_EXEC_START_POST
;
1323 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
])) {
1324 r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
);
1326 log_warning("%s failed to run 'start-post' task: %s", UNIT(s
)->id
, strerror(-r
));
1330 socket_set_state(s
, SOCKET_START_POST
);
1332 socket_enter_listening(s
);
1337 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1340 static void socket_enter_start_pre(Socket
*s
) {
1344 socket_unwatch_control_pid(s
);
1346 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
1348 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
])) {
1349 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1352 socket_set_state(s
, SOCKET_START_PRE
);
1354 socket_enter_start_post(s
);
1359 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s
)->id
, strerror(-r
));
1360 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
1363 static void socket_enter_running(Socket
*s
, int cfd
) {
1368 dbus_error_init(&error
);
1370 /* We don't take connections anymore if we are supposed to
1371 * shut down anyway */
1372 if (unit_pending_inactive(UNIT(s
))) {
1373 log_debug("Suppressing connection request on %s since unit stop is scheduled.", UNIT(s
)->id
);
1376 close_nointr_nofail(cfd
);
1378 /* Flush all sockets by closing and reopening them */
1379 socket_close_fds(s
);
1381 r
= socket_watch_fds(s
);
1383 log_warning("%s failed to watch sockets: %s", UNIT(s
)->id
, strerror(-r
));
1384 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1394 bool pending
= false;
1396 /* If there's already a start pending don't bother to
1398 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERS
], i
)
1399 if (unit_pending_active(u
)) {
1405 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT_DEREF(s
->service
), JOB_REPLACE
, true, &error
, NULL
);
1410 socket_set_state(s
, SOCKET_RUNNING
);
1412 char *prefix
, *instance
= NULL
, *name
;
1415 if (s
->n_connections
>= s
->max_connections
) {
1416 log_warning("Too many incoming connections (%u)", s
->n_connections
);
1417 close_nointr_nofail(cfd
);
1421 r
= socket_instantiate_service(s
);
1425 r
= instance_from_socket(cfd
, s
->n_accepted
, &instance
);
1430 /* ENOTCONN is legitimate if TCP RST was received.
1431 * This connection is over, but the socket unit lives on. */
1432 close_nointr_nofail(cfd
);
1436 prefix
= unit_name_to_prefix(UNIT(s
)->id
);
1443 name
= unit_name_build(prefix
, instance
, ".service");
1452 r
= unit_add_name(UNIT_DEREF(s
->service
), name
);
1458 service
= SERVICE(UNIT_DEREF(s
->service
));
1459 unit_ref_unset(&s
->service
);
1462 UNIT(service
)->no_gc
= false;
1464 unit_choose_id(UNIT(service
), name
);
1467 r
= service_set_socket_fd(service
, cfd
, s
);
1472 s
->n_connections
++;
1474 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(service
), JOB_REPLACE
, true, &error
, NULL
);
1478 /* Notify clients about changed counters */
1479 unit_add_to_dbus_queue(UNIT(s
));
1485 log_warning("%s failed to queue socket startup job: %s", UNIT(s
)->id
, bus_error(&error
, r
));
1486 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1489 close_nointr_nofail(cfd
);
1491 dbus_error_free(&error
);
1494 static void socket_run_next(Socket
*s
) {
1498 assert(s
->control_command
);
1499 assert(s
->control_command
->command_next
);
1501 socket_unwatch_control_pid(s
);
1503 s
->control_command
= s
->control_command
->command_next
;
1505 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1511 log_warning("%s failed to run next task: %s", UNIT(s
)->id
, strerror(-r
));
1513 if (s
->state
== SOCKET_START_POST
)
1514 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1515 else if (s
->state
== SOCKET_STOP_POST
)
1516 socket_enter_dead(s
, SOCKET_FAILURE_RESOURCES
);
1518 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_RESOURCES
);
1521 static int socket_start(Unit
*u
) {
1522 Socket
*s
= SOCKET(u
);
1526 /* We cannot fulfill this request right now, try again later
1528 if (s
->state
== SOCKET_STOP_PRE
||
1529 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
1530 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
1531 s
->state
== SOCKET_STOP_POST
||
1532 s
->state
== SOCKET_FINAL_SIGTERM
||
1533 s
->state
== SOCKET_FINAL_SIGKILL
)
1536 if (s
->state
== SOCKET_START_PRE
||
1537 s
->state
== SOCKET_START_POST
)
1540 /* Cannot run this without the service being around */
1541 if (UNIT_DEREF(s
->service
)) {
1544 service
= SERVICE(UNIT_DEREF(s
->service
));
1546 if (UNIT(service
)->load_state
!= UNIT_LOADED
) {
1547 log_error("Socket service %s not loaded, refusing.", UNIT(service
)->id
);
1551 /* If the service is already active we cannot start the
1553 if (service
->state
!= SERVICE_DEAD
&&
1554 service
->state
!= SERVICE_FAILED
&&
1555 service
->state
!= SERVICE_AUTO_RESTART
) {
1556 log_error("Socket service %s already active, refusing.", UNIT(service
)->id
);
1560 #ifdef HAVE_SYSV_COMPAT
1561 if (service
->sysv_path
) {
1562 log_error("Using SysV services for socket activation is not supported. Refusing.");
1568 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_FAILED
);
1570 s
->result
= SOCKET_SUCCESS
;
1571 socket_enter_start_pre(s
);
1575 static int socket_stop(Unit
*u
) {
1576 Socket
*s
= SOCKET(u
);
1581 if (s
->state
== SOCKET_STOP_PRE
||
1582 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
1583 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
1584 s
->state
== SOCKET_STOP_POST
||
1585 s
->state
== SOCKET_FINAL_SIGTERM
||
1586 s
->state
== SOCKET_FINAL_SIGKILL
)
1589 /* If there's already something running we go directly into
1591 if (s
->state
== SOCKET_START_PRE
||
1592 s
->state
== SOCKET_START_POST
) {
1593 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_SUCCESS
);
1597 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
1599 socket_enter_stop_pre(s
, SOCKET_SUCCESS
);
1603 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1604 Socket
*s
= SOCKET(u
);
1612 unit_serialize_item(u
, f
, "state", socket_state_to_string(s
->state
));
1613 unit_serialize_item(u
, f
, "result", socket_result_to_string(s
->result
));
1614 unit_serialize_item_format(u
, f
, "n-accepted", "%u", s
->n_accepted
);
1616 if (s
->control_pid
> 0)
1617 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
1619 if (s
->control_command_id
>= 0)
1620 unit_serialize_item(u
, f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
1622 LIST_FOREACH(port
, p
, s
->ports
) {
1628 if ((copy
= fdset_put_dup(fds
, p
->fd
)) < 0)
1631 if (p
->type
== SOCKET_SOCKET
) {
1634 if ((r
= socket_address_print(&p
->address
, &t
)) < 0)
1637 if (socket_address_family(&p
->address
) == AF_NETLINK
)
1638 unit_serialize_item_format(u
, f
, "netlink", "%i %s", copy
, t
);
1640 unit_serialize_item_format(u
, f
, "socket", "%i %i %s", copy
, p
->address
.type
, t
);
1642 } else if (p
->type
== SOCKET_SPECIAL
)
1643 unit_serialize_item_format(u
, f
, "special", "%i %s", copy
, p
->path
);
1645 assert(p
->type
== SOCKET_FIFO
);
1646 unit_serialize_item_format(u
, f
, "fifo", "%i %s", copy
, p
->path
);
1653 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1654 Socket
*s
= SOCKET(u
);
1661 if (streq(key
, "state")) {
1664 if ((state
= socket_state_from_string(value
)) < 0)
1665 log_debug("Failed to parse state value %s", value
);
1667 s
->deserialized_state
= state
;
1668 } else if (streq(key
, "result")) {
1671 f
= socket_result_from_string(value
);
1673 log_debug("Failed to parse result value %s", value
);
1674 else if (f
!= SOCKET_SUCCESS
)
1677 } else if (streq(key
, "n-accepted")) {
1680 if (safe_atou(value
, &k
) < 0)
1681 log_debug("Failed to parse n-accepted value %s", value
);
1684 } else if (streq(key
, "control-pid")) {
1687 if (parse_pid(value
, &pid
) < 0)
1688 log_debug("Failed to parse control-pid value %s", value
);
1690 s
->control_pid
= pid
;
1691 } else if (streq(key
, "control-command")) {
1692 SocketExecCommand id
;
1694 if ((id
= socket_exec_command_from_string(value
)) < 0)
1695 log_debug("Failed to parse exec-command value %s", value
);
1697 s
->control_command_id
= id
;
1698 s
->control_command
= s
->exec_command
[id
];
1700 } else if (streq(key
, "fifo")) {
1704 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1705 log_debug("Failed to parse fifo value %s", value
);
1708 LIST_FOREACH(port
, p
, s
->ports
)
1709 if (p
->type
== SOCKET_FIFO
&&
1710 streq_ptr(p
->path
, value
+skip
))
1715 close_nointr_nofail(p
->fd
);
1716 p
->fd
= fdset_remove(fds
, fd
);
1720 } else if (streq(key
, "special")) {
1724 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1725 log_debug("Failed to parse special value %s", value
);
1728 LIST_FOREACH(port
, p
, s
->ports
)
1729 if (p
->type
== SOCKET_SPECIAL
&&
1730 streq_ptr(p
->path
, value
+skip
))
1735 close_nointr_nofail(p
->fd
);
1736 p
->fd
= fdset_remove(fds
, fd
);
1740 } else if (streq(key
, "socket")) {
1741 int fd
, type
, skip
= 0;
1744 if (sscanf(value
, "%i %i %n", &fd
, &type
, &skip
) < 2 || fd
< 0 || type
< 0 || !fdset_contains(fds
, fd
))
1745 log_debug("Failed to parse socket value %s", value
);
1748 LIST_FOREACH(port
, p
, s
->ports
)
1749 if (socket_address_is(&p
->address
, value
+skip
, type
))
1754 close_nointr_nofail(p
->fd
);
1755 p
->fd
= fdset_remove(fds
, fd
);
1759 } else if (streq(key
, "netlink")) {
1763 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1764 log_debug("Failed to parse socket value %s", value
);
1767 LIST_FOREACH(port
, p
, s
->ports
)
1768 if (socket_address_is_netlink(&p
->address
, value
+skip
))
1773 close_nointr_nofail(p
->fd
);
1774 p
->fd
= fdset_remove(fds
, fd
);
1779 log_debug("Unknown serialization key '%s'", key
);
1784 static UnitActiveState
socket_active_state(Unit
*u
) {
1787 return state_translation_table
[SOCKET(u
)->state
];
1790 static const char *socket_sub_state_to_string(Unit
*u
) {
1793 return socket_state_to_string(SOCKET(u
)->state
);
1796 static bool socket_check_gc(Unit
*u
) {
1797 Socket
*s
= SOCKET(u
);
1801 return s
->n_connections
> 0;
1804 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
1805 Socket
*s
= SOCKET(u
);
1811 if (s
->state
!= SOCKET_LISTENING
)
1814 log_debug("Incoming traffic on %s", u
->id
);
1816 if (events
!= EPOLLIN
) {
1818 if (events
& EPOLLHUP
)
1819 log_error("%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", u
->id
);
1821 log_error("%s: Got unexpected poll event (0x%x) on socket.", u
->id
, events
);
1826 if (w
->socket_accept
) {
1829 if ((cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
)) < 0) {
1834 log_error("Failed to accept socket: %m");
1841 socket_apply_socket_options(s
, cfd
);
1844 socket_enter_running(s
, cfd
);
1848 socket_enter_stop_pre(s
, SOCKET_FAILURE_RESOURCES
);
1851 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1852 Socket
*s
= SOCKET(u
);
1858 if (pid
!= s
->control_pid
)
1863 if (is_clean_exit(code
, status
))
1865 else if (code
== CLD_EXITED
)
1866 f
= SOCKET_FAILURE_EXIT_CODE
;
1867 else if (code
== CLD_KILLED
)
1868 f
= SOCKET_FAILURE_SIGNAL
;
1869 else if (code
== CLD_DUMPED
)
1870 f
= SOCKET_FAILURE_CORE_DUMP
;
1872 assert_not_reached("Unknown code");
1874 if (s
->control_command
) {
1875 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
1877 if (s
->control_command
->ignore
)
1881 log_full(f
== SOCKET_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
1882 "%s control process exited, code=%s status=%i", u
->id
, sigchld_code_to_string(code
), status
);
1884 if (f
!= SOCKET_SUCCESS
)
1887 if (s
->control_command
&&
1888 s
->control_command
->command_next
&&
1889 f
== SOCKET_SUCCESS
) {
1891 log_debug("%s running next command for state %s", u
->id
, socket_state_to_string(s
->state
));
1894 s
->control_command
= NULL
;
1895 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1897 /* No further commands for this step, so let's figure
1898 * out what to do next */
1900 log_debug("%s got final SIGCHLD for state %s", u
->id
, socket_state_to_string(s
->state
));
1904 case SOCKET_START_PRE
:
1905 if (f
== SOCKET_SUCCESS
)
1906 socket_enter_start_post(s
);
1908 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, f
);
1911 case SOCKET_START_POST
:
1912 if (f
== SOCKET_SUCCESS
)
1913 socket_enter_listening(s
);
1915 socket_enter_stop_pre(s
, f
);
1918 case SOCKET_STOP_PRE
:
1919 case SOCKET_STOP_PRE_SIGTERM
:
1920 case SOCKET_STOP_PRE_SIGKILL
:
1921 socket_enter_stop_post(s
, f
);
1924 case SOCKET_STOP_POST
:
1925 case SOCKET_FINAL_SIGTERM
:
1926 case SOCKET_FINAL_SIGKILL
:
1927 socket_enter_dead(s
, f
);
1931 assert_not_reached("Uh, control process died at wrong time.");
1935 /* Notify clients about changed exit status */
1936 unit_add_to_dbus_queue(u
);
1939 static void socket_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
1940 Socket
*s
= SOCKET(u
);
1943 assert(elapsed
== 1);
1944 assert(w
== &s
->timer_watch
);
1948 case SOCKET_START_PRE
:
1949 log_warning("%s starting timed out. Terminating.", u
->id
);
1950 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
1953 case SOCKET_START_POST
:
1954 log_warning("%s starting timed out. Stopping.", u
->id
);
1955 socket_enter_stop_pre(s
, SOCKET_FAILURE_TIMEOUT
);
1958 case SOCKET_STOP_PRE
:
1959 log_warning("%s stopping timed out. Terminating.", u
->id
);
1960 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
1963 case SOCKET_STOP_PRE_SIGTERM
:
1964 if (s
->exec_context
.send_sigkill
) {
1965 log_warning("%s stopping timed out. Killing.", u
->id
);
1966 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
1968 log_warning("%s stopping timed out. Skipping SIGKILL. Ignoring.", u
->id
);
1969 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
1973 case SOCKET_STOP_PRE_SIGKILL
:
1974 log_warning("%s still around after SIGKILL. Ignoring.", u
->id
);
1975 socket_enter_stop_post(s
, SOCKET_FAILURE_TIMEOUT
);
1978 case SOCKET_STOP_POST
:
1979 log_warning("%s stopping timed out (2). Terminating.", u
->id
);
1980 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, SOCKET_FAILURE_TIMEOUT
);
1983 case SOCKET_FINAL_SIGTERM
:
1984 if (s
->exec_context
.send_sigkill
) {
1985 log_warning("%s stopping timed out (2). Killing.", u
->id
);
1986 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, SOCKET_FAILURE_TIMEOUT
);
1988 log_warning("%s stopping timed out (2). Skipping SIGKILL. Ignoring.", u
->id
);
1989 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
1993 case SOCKET_FINAL_SIGKILL
:
1994 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u
->id
);
1995 socket_enter_dead(s
, SOCKET_FAILURE_TIMEOUT
);
1999 assert_not_reached("Timeout at wrong time.");
2003 int socket_collect_fds(Socket
*s
, int **fds
, unsigned *n_fds
) {
2012 /* Called from the service code for requesting our fds */
2015 LIST_FOREACH(port
, p
, s
->ports
)
2025 if (!(rfds
= new(int, rn_fds
)))
2029 LIST_FOREACH(port
, p
, s
->ports
)
2033 assert(k
== rn_fds
);
2041 void socket_notify_service_dead(Socket
*s
, bool broken
) {
2044 /* The service is dead. Dang!
2046 * This is strictly for one-instance-for-all-connections
2049 if (s
->state
== SOCKET_RUNNING
) {
2050 log_debug("%s got notified about service death (broken: %s)", UNIT(s
)->id
, yes_no(broken
));
2052 socket_enter_stop_pre(s
, SOCKET_FAILURE_SERVICE_BROKEN
);
2054 socket_enter_listening(s
);
2058 void socket_connection_unref(Socket
*s
) {
2061 /* The service is dead. Yay!
2063 * This is strictly for one-instance-per-connection
2066 assert(s
->n_connections
> 0);
2069 log_debug("%s: One connection closed, %u left.", UNIT(s
)->id
, s
->n_connections
);
2072 static void socket_reset_failed(Unit
*u
) {
2073 Socket
*s
= SOCKET(u
);
2077 if (s
->state
== SOCKET_FAILED
)
2078 socket_set_state(s
, SOCKET_DEAD
);
2080 s
->result
= SOCKET_SUCCESS
;
2083 static int socket_kill(Unit
*u
, KillWho who
, KillMode mode
, int signo
, DBusError
*error
) {
2084 Socket
*s
= SOCKET(u
);
2086 Set
*pid_set
= NULL
;
2090 if (who
== KILL_MAIN
) {
2091 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "Socket units have no main processes");
2095 if (s
->control_pid
<= 0 && who
== KILL_CONTROL
) {
2096 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No control process to kill");
2100 if (who
== KILL_CONTROL
|| who
== KILL_ALL
)
2101 if (s
->control_pid
> 0)
2102 if (kill(s
->control_pid
, signo
) < 0)
2105 if (who
== KILL_ALL
&& mode
== KILL_CONTROL_GROUP
) {
2108 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
)))
2111 /* Exclude the control pid from being killed via the cgroup */
2112 if (s
->control_pid
> 0)
2113 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0) {
2118 if ((q
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, signo
, false, pid_set
)) < 0)
2119 if (q
!= -EAGAIN
&& q
!= -ESRCH
&& q
!= -ENOENT
)
2130 static const char* const socket_state_table
[_SOCKET_STATE_MAX
] = {
2131 [SOCKET_DEAD
] = "dead",
2132 [SOCKET_START_PRE
] = "start-pre",
2133 [SOCKET_START_POST
] = "start-post",
2134 [SOCKET_LISTENING
] = "listening",
2135 [SOCKET_RUNNING
] = "running",
2136 [SOCKET_STOP_PRE
] = "stop-pre",
2137 [SOCKET_STOP_PRE_SIGTERM
] = "stop-pre-sigterm",
2138 [SOCKET_STOP_PRE_SIGKILL
] = "stop-pre-sigkill",
2139 [SOCKET_STOP_POST
] = "stop-post",
2140 [SOCKET_FINAL_SIGTERM
] = "final-sigterm",
2141 [SOCKET_FINAL_SIGKILL
] = "final-sigkill",
2142 [SOCKET_FAILED
] = "failed"
2145 DEFINE_STRING_TABLE_LOOKUP(socket_state
, SocketState
);
2147 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
2148 [SOCKET_EXEC_START_PRE
] = "StartPre",
2149 [SOCKET_EXEC_START_POST
] = "StartPost",
2150 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
2151 [SOCKET_EXEC_STOP_POST
] = "StopPost"
2154 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
2156 static const char* const socket_result_table
[_SOCKET_RESULT_MAX
] = {
2157 [SOCKET_SUCCESS
] = "success",
2158 [SOCKET_FAILURE_RESOURCES
] = "resources",
2159 [SOCKET_FAILURE_TIMEOUT
] = "timeout",
2160 [SOCKET_FAILURE_EXIT_CODE
] = "exit-code",
2161 [SOCKET_FAILURE_SIGNAL
] = "signal",
2162 [SOCKET_FAILURE_CORE_DUMP
] = "core-dump",
2163 [SOCKET_FAILURE_SERVICE_BROKEN
] = "service-broken"
2166 DEFINE_STRING_TABLE_LOOKUP(socket_result
, SocketResult
);
2168 const UnitVTable socket_vtable
= {
2169 .suffix
= ".socket",
2170 .object_size
= sizeof(Socket
),
2176 .init
= socket_init
,
2177 .done
= socket_done
,
2178 .load
= socket_load
,
2180 .kill
= socket_kill
,
2182 .coldplug
= socket_coldplug
,
2184 .dump
= socket_dump
,
2186 .start
= socket_start
,
2187 .stop
= socket_stop
,
2189 .serialize
= socket_serialize
,
2190 .deserialize_item
= socket_deserialize_item
,
2192 .active_state
= socket_active_state
,
2193 .sub_state_to_string
= socket_sub_state_to_string
,
2195 .check_gc
= socket_check_gc
,
2197 .fd_event
= socket_fd_event
,
2198 .sigchld_event
= socket_sigchld_event
,
2199 .timer_event
= socket_timer_event
,
2201 .reset_failed
= socket_reset_failed
,
2203 .bus_interface
= "org.freedesktop.systemd1.Socket",
2204 .bus_message_handler
= bus_socket_message_handler
,
2205 .bus_invalidating_properties
= bus_socket_invalidating_properties