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"
410 "%sBindIPv6Only: %s\n"
412 "%sSocketMode: %04o\n"
413 "%sDirectoryMode: %04o\n"
416 "%sTransparent: %s\n"
418 "%sPassCrededentials: %s\n"
419 "%sTCPCongestion: %s\n",
420 prefix
, socket_state_to_string(s
->state
),
421 prefix
, socket_address_bind_ipv6_only_to_string(s
->bind_ipv6_only
),
423 prefix
, s
->socket_mode
,
424 prefix
, s
->directory_mode
,
425 prefix
, yes_no(s
->keep_alive
),
426 prefix
, yes_no(s
->free_bind
),
427 prefix
, yes_no(s
->transparent
),
428 prefix
, yes_no(s
->broadcast
),
429 prefix
, yes_no(s
->pass_cred
),
430 prefix
, strna(s
->tcp_congestion
));
432 if (s
->control_pid
> 0)
434 "%sControl PID: %lu\n",
435 prefix
, (unsigned long) s
->control_pid
);
437 if (s
->bind_to_device
)
439 "%sBindToDevice: %s\n",
440 prefix
, s
->bind_to_device
);
445 "%sNConnections: %u\n"
446 "%sMaxConnections: %u\n",
447 prefix
, s
->n_accepted
,
448 prefix
, s
->n_connections
,
449 prefix
, s
->max_connections
);
451 if (s
->priority
>= 0)
454 prefix
, s
->priority
);
456 if (s
->receive_buffer
> 0)
458 "%sReceiveBuffer: %zu\n",
459 prefix
, s
->receive_buffer
);
461 if (s
->send_buffer
> 0)
463 "%sSendBuffer: %zu\n",
464 prefix
, s
->send_buffer
);
476 if (s
->pipe_size
> 0)
479 prefix
, s
->pipe_size
);
486 if (s
->mq_maxmsg
> 0)
488 "%sMessageQueueMaxMessages: %li\n",
489 prefix
, s
->mq_maxmsg
);
491 if (s
->mq_msgsize
> 0)
493 "%sMessageQueueMessageSize: %li\n",
494 prefix
, s
->mq_msgsize
);
496 LIST_FOREACH(port
, p
, s
->ports
) {
498 if (p
->type
== SOCKET_SOCKET
) {
503 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
508 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(socket_address_family(&p
->address
), p
->address
.type
), t
);
510 } else if (p
->type
== SOCKET_SPECIAL
)
511 fprintf(f
, "%sListenSpecial: %s\n", prefix
, p
->path
);
512 else if (p
->type
== SOCKET_MQUEUE
)
513 fprintf(f
, "%sListenMessageQueue: %s\n", prefix
, p
->path
);
515 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
518 exec_context_dump(&s
->exec_context
, f
, prefix
);
520 for (c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
521 if (!s
->exec_command
[c
])
524 fprintf(f
, "%s-> %s:\n",
525 prefix
, socket_exec_command_to_string(c
));
527 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
533 static int instance_from_socket(int fd
, unsigned nr
, char **instance
) {
538 struct sockaddr_un un
;
539 struct sockaddr_in in
;
540 struct sockaddr_in6 in6
;
541 struct sockaddr_storage storage
;
548 if (getsockname(fd
, &local
.sa
, &l
) < 0)
552 if (getpeername(fd
, &remote
.sa
, &l
) < 0)
555 switch (local
.sa
.sa_family
) {
559 a
= ntohl(local
.in
.sin_addr
.s_addr
),
560 b
= ntohl(remote
.in
.sin_addr
.s_addr
);
563 "%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
564 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
565 ntohs(local
.in
.sin_port
),
566 b
>> 24, (b
>> 16) & 0xFF, (b
>> 8) & 0xFF, b
& 0xFF,
567 ntohs(remote
.in
.sin_port
)) < 0)
574 static const char ipv4_prefix
[] = {
575 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
578 if (memcmp(&local
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0 &&
579 memcmp(&remote
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
581 *a
= local
.in6
.sin6_addr
.s6_addr
+12,
582 *b
= remote
.in6
.sin6_addr
.s6_addr
+12;
585 "%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
586 a
[0], a
[1], a
[2], a
[3],
587 ntohs(local
.in6
.sin6_port
),
588 b
[0], b
[1], b
[2], b
[3],
589 ntohs(remote
.in6
.sin6_port
)) < 0)
592 char a
[INET6_ADDRSTRLEN
], b
[INET6_ADDRSTRLEN
];
596 inet_ntop(AF_INET6
, &local
.in6
.sin6_addr
, a
, sizeof(a
)),
597 ntohs(local
.in6
.sin6_port
),
598 inet_ntop(AF_INET6
, &remote
.in6
.sin6_addr
, b
, sizeof(b
)),
599 ntohs(remote
.in6
.sin6_port
)) < 0)
610 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
) < 0)
616 (unsigned long) ucred
.pid
,
617 (unsigned long) ucred
.uid
) < 0)
624 assert_not_reached("Unhandled socket type.");
631 static void socket_close_fds(Socket
*s
) {
636 LIST_FOREACH(port
, p
, s
->ports
) {
640 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
641 close_nointr_nofail(p
->fd
);
643 /* One little note: we should never delete any sockets
644 * in the file system here! After all some other
645 * process we spawned might still have a reference of
646 * this fd and wants to continue to use it. Therefore
647 * we delete sockets in the file system before we
648 * create a new one, not after we stopped using
655 static void socket_apply_socket_options(Socket
*s
, int fd
) {
660 int b
= s
->keep_alive
;
661 if (setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &b
, sizeof(b
)) < 0)
662 log_warning("SO_KEEPALIVE failed: %m");
667 if (setsockopt(fd
, SOL_SOCKET
, SO_BROADCAST
, &one
, sizeof(one
)) < 0)
668 log_warning("SO_BROADCAST failed: %m");
673 if (setsockopt(fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
)) < 0)
674 log_warning("SO_PASSCRED failed: %m");
677 if (s
->priority
>= 0)
678 if (setsockopt(fd
, SOL_SOCKET
, SO_PRIORITY
, &s
->priority
, sizeof(s
->priority
)) < 0)
679 log_warning("SO_PRIORITY failed: %m");
681 if (s
->receive_buffer
> 0) {
682 int value
= (int) s
->receive_buffer
;
684 /* We first try with SO_RCVBUFFORCE, in case we have the perms for that */
686 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUFFORCE
, &value
, sizeof(value
)) < 0)
687 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &value
, sizeof(value
)) < 0)
688 log_warning("SO_RCVBUF failed: %m");
691 if (s
->send_buffer
> 0) {
692 int value
= (int) s
->send_buffer
;
693 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUFFORCE
, &value
, sizeof(value
)) < 0)
694 if (setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &value
, sizeof(value
)) < 0)
695 log_warning("SO_SNDBUF failed: %m");
699 if (setsockopt(fd
, SOL_SOCKET
, SO_MARK
, &s
->mark
, sizeof(s
->mark
)) < 0)
700 log_warning("SO_MARK failed: %m");
703 if (setsockopt(fd
, IPPROTO_IP
, IP_TOS
, &s
->ip_tos
, sizeof(s
->ip_tos
)) < 0)
704 log_warning("IP_TOS failed: %m");
706 if (s
->ip_ttl
>= 0) {
709 r
= setsockopt(fd
, IPPROTO_IP
, IP_TTL
, &s
->ip_ttl
, sizeof(s
->ip_ttl
));
711 if (socket_ipv6_is_supported())
712 x
= setsockopt(fd
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
, &s
->ip_ttl
, sizeof(s
->ip_ttl
));
715 errno
= EAFNOSUPPORT
;
719 log_warning("IP_TTL/IPV6_UNICAST_HOPS failed: %m");
722 if (s
->tcp_congestion
)
723 if (setsockopt(fd
, SOL_TCP
, TCP_CONGESTION
, s
->tcp_congestion
, strlen(s
->tcp_congestion
)+1) < 0)
724 log_warning("TCP_CONGESTION failed: %m");
727 static void socket_apply_fifo_options(Socket
*s
, int fd
) {
731 if (s
->pipe_size
> 0)
732 if (fcntl(fd
, F_SETPIPE_SZ
, s
->pipe_size
) < 0)
733 log_warning("F_SETPIPE_SZ: %m");
736 static int fifo_address_create(
738 mode_t directory_mode
,
749 mkdir_parents(path
, directory_mode
);
751 if ((r
= label_fifofile_set(path
)) < 0)
754 /* Enforce the right access mode for the fifo */
755 old_mask
= umask(~ socket_mode
);
757 /* Include the original umask in our mask */
758 umask(~socket_mode
| old_mask
);
760 r
= mkfifo(path
, socket_mode
);
763 if (r
< 0 && errno
!= EEXIST
) {
768 if ((fd
= open(path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
775 if (fstat(fd
, &st
) < 0) {
780 if (!S_ISFIFO(st
.st_mode
) ||
781 (st
.st_mode
& 0777) != (socket_mode
& ~old_mask
) ||
782 st
.st_uid
!= getuid() ||
783 st
.st_gid
!= getgid()) {
796 close_nointr_nofail(fd
);
801 static int special_address_create(
811 if ((fd
= open(path
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
816 if (fstat(fd
, &st
) < 0) {
821 /* Check whether this is a /proc, /sys or /dev file or char device */
822 if (!S_ISREG(st
.st_mode
) && !S_ISCHR(st
.st_mode
)) {
832 close_nointr_nofail(fd
);
837 static int mq_address_create(
847 struct mq_attr _attr
, *attr
= NULL
;
852 if (maxmsg
> 0 && msgsize
> 0) {
854 _attr
.mq_flags
= O_NONBLOCK
;
855 _attr
.mq_maxmsg
= maxmsg
;
856 _attr
.mq_msgsize
= msgsize
;
860 /* Enforce the right access mode for the mq */
861 old_mask
= umask(~ mq_mode
);
863 /* Include the original umask in our mask */
864 umask(~mq_mode
| old_mask
);
866 fd
= mq_open(path
, O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_CREAT
, mq_mode
, attr
);
874 if (fstat(fd
, &st
) < 0) {
879 if ((st
.st_mode
& 0777) != (mq_mode
& ~old_mask
) ||
880 st
.st_uid
!= getuid() ||
881 st
.st_gid
!= getgid()) {
892 close_nointr_nofail(fd
);
897 static int socket_open_fds(Socket
*s
) {
901 bool know_label
= false;
905 LIST_FOREACH(port
, p
, s
->ports
) {
910 if (p
->type
== SOCKET_SOCKET
) {
914 if ((r
= socket_instantiate_service(s
)) < 0)
917 if (UNIT_DEREF(s
->service
) &&
918 SERVICE(UNIT_DEREF(s
->service
))->exec_command
[SERVICE_EXEC_START
]) {
919 r
= label_get_create_label_from_exe(SERVICE(UNIT_DEREF(s
->service
))->exec_command
[SERVICE_EXEC_START
]->path
, &label
);
930 if ((r
= socket_address_listen(
943 socket_apply_socket_options(s
, p
->fd
);
945 } else if (p
->type
== SOCKET_SPECIAL
) {
947 if ((r
= special_address_create(
952 } else if (p
->type
== SOCKET_FIFO
) {
954 if ((r
= fifo_address_create(
961 socket_apply_fifo_options(s
, p
->fd
);
962 } else if (p
->type
== SOCKET_MQUEUE
) {
964 if ((r
= mq_address_create(
972 assert_not_reached("Unknown port type");
984 static void socket_unwatch_fds(Socket
*s
) {
989 LIST_FOREACH(port
, p
, s
->ports
) {
993 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
997 static int socket_watch_fds(Socket
*s
) {
1003 LIST_FOREACH(port
, p
, s
->ports
) {
1007 p
->fd_watch
.socket_accept
=
1009 p
->type
== SOCKET_SOCKET
&&
1010 socket_address_can_accept(&p
->address
);
1012 if ((r
= unit_watch_fd(UNIT(s
), p
->fd
, EPOLLIN
, &p
->fd_watch
)) < 0)
1019 socket_unwatch_fds(s
);
1023 static void socket_set_state(Socket
*s
, SocketState state
) {
1024 SocketState old_state
;
1027 old_state
= s
->state
;
1030 if (state
!= SOCKET_START_PRE
&&
1031 state
!= SOCKET_START_POST
&&
1032 state
!= SOCKET_STOP_PRE
&&
1033 state
!= SOCKET_STOP_PRE_SIGTERM
&&
1034 state
!= SOCKET_STOP_PRE_SIGKILL
&&
1035 state
!= SOCKET_STOP_POST
&&
1036 state
!= SOCKET_FINAL_SIGTERM
&&
1037 state
!= SOCKET_FINAL_SIGKILL
) {
1038 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1039 socket_unwatch_control_pid(s
);
1040 s
->control_command
= NULL
;
1041 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1044 if (state
!= SOCKET_LISTENING
)
1045 socket_unwatch_fds(s
);
1047 if (state
!= SOCKET_START_POST
&&
1048 state
!= SOCKET_LISTENING
&&
1049 state
!= SOCKET_RUNNING
&&
1050 state
!= SOCKET_STOP_PRE
&&
1051 state
!= SOCKET_STOP_PRE_SIGTERM
&&
1052 state
!= SOCKET_STOP_PRE_SIGKILL
)
1053 socket_close_fds(s
);
1055 if (state
!= old_state
)
1056 log_debug("%s changed %s -> %s",
1058 socket_state_to_string(old_state
),
1059 socket_state_to_string(state
));
1061 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
], true);
1064 static int socket_coldplug(Unit
*u
) {
1065 Socket
*s
= SOCKET(u
);
1069 assert(s
->state
== SOCKET_DEAD
);
1071 if (s
->deserialized_state
!= s
->state
) {
1073 if (s
->deserialized_state
== SOCKET_START_PRE
||
1074 s
->deserialized_state
== SOCKET_START_POST
||
1075 s
->deserialized_state
== SOCKET_STOP_PRE
||
1076 s
->deserialized_state
== SOCKET_STOP_PRE_SIGTERM
||
1077 s
->deserialized_state
== SOCKET_STOP_PRE_SIGKILL
||
1078 s
->deserialized_state
== SOCKET_STOP_POST
||
1079 s
->deserialized_state
== SOCKET_FINAL_SIGTERM
||
1080 s
->deserialized_state
== SOCKET_FINAL_SIGKILL
) {
1082 if (s
->control_pid
<= 0)
1085 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
1088 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1092 if (s
->deserialized_state
== SOCKET_START_POST
||
1093 s
->deserialized_state
== SOCKET_LISTENING
||
1094 s
->deserialized_state
== SOCKET_RUNNING
||
1095 s
->deserialized_state
== SOCKET_STOP_PRE
||
1096 s
->deserialized_state
== SOCKET_STOP_PRE_SIGTERM
||
1097 s
->deserialized_state
== SOCKET_STOP_PRE_SIGKILL
)
1098 if ((r
= socket_open_fds(s
)) < 0)
1101 if (s
->deserialized_state
== SOCKET_LISTENING
)
1102 if ((r
= socket_watch_fds(s
)) < 0)
1105 socket_set_state(s
, s
->deserialized_state
);
1111 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
1120 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1123 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
1132 UNIT(s
)->manager
->environment
,
1136 UNIT(s
)->manager
->confirm_spawn
,
1137 UNIT(s
)->cgroup_bondings
,
1138 UNIT(s
)->cgroup_attributes
,
1145 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
1146 /* FIXME: we need to do something here */
1154 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
1159 static void socket_enter_dead(Socket
*s
, bool success
) {
1165 socket_set_state(s
, s
->failure
? SOCKET_FAILED
: SOCKET_DEAD
);
1168 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
);
1170 static void socket_enter_stop_post(Socket
*s
, bool success
) {
1177 socket_unwatch_control_pid(s
);
1179 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
1181 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
])) {
1182 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1185 socket_set_state(s
, SOCKET_STOP_POST
);
1187 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, true);
1192 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s
)->id
, strerror(-r
));
1193 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1196 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
) {
1198 Set
*pid_set
= NULL
;
1199 bool wait_for_exit
= false;
1206 if (s
->exec_context
.kill_mode
!= KILL_NONE
) {
1207 int sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_FINAL_SIGTERM
) ? s
->exec_context
.kill_signal
: SIGKILL
;
1209 if (s
->control_pid
> 0) {
1210 if (kill_and_sigcont(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
)
1212 log_warning("Failed to kill control process %li: %m", (long) s
->control_pid
);
1214 wait_for_exit
= true;
1217 if (s
->exec_context
.kill_mode
== KILL_CONTROL_GROUP
) {
1219 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
))) {
1224 /* Exclude the control pid from being killed via the cgroup */
1225 if (s
->control_pid
> 0)
1226 if ((r
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0)
1229 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, sig
, true, pid_set
)) < 0) {
1230 if (r
!= -EAGAIN
&& r
!= -ESRCH
&& r
!= -ENOENT
)
1231 log_warning("Failed to kill control group: %s", strerror(-r
));
1233 wait_for_exit
= true;
1240 if (wait_for_exit
) {
1241 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
1244 socket_set_state(s
, state
);
1245 } else if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
1246 socket_enter_stop_post(s
, true);
1248 socket_enter_dead(s
, true);
1253 log_warning("%s failed to kill processes: %s", UNIT(s
)->id
, strerror(-r
));
1255 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
1256 socket_enter_stop_post(s
, false);
1258 socket_enter_dead(s
, false);
1264 static void socket_enter_stop_pre(Socket
*s
, bool success
) {
1271 socket_unwatch_control_pid(s
);
1273 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
1275 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
])) {
1276 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1279 socket_set_state(s
, SOCKET_STOP_PRE
);
1281 socket_enter_stop_post(s
, true);
1286 log_warning("%s failed to run 'stop-pre' task: %s", UNIT(s
)->id
, strerror(-r
));
1287 socket_enter_stop_post(s
, false);
1290 static void socket_enter_listening(Socket
*s
) {
1294 if ((r
= socket_watch_fds(s
)) < 0) {
1295 log_warning("%s failed to watch sockets: %s", UNIT(s
)->id
, strerror(-r
));
1299 socket_set_state(s
, SOCKET_LISTENING
);
1303 socket_enter_stop_pre(s
, false);
1306 static void socket_enter_start_post(Socket
*s
) {
1310 if ((r
= socket_open_fds(s
)) < 0) {
1311 log_warning("%s failed to listen on sockets: %s", UNIT(s
)->id
, strerror(-r
));
1315 socket_unwatch_control_pid(s
);
1317 s
->control_command_id
= SOCKET_EXEC_START_POST
;
1319 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
])) {
1320 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0) {
1321 log_warning("%s failed to run 'start-post' task: %s", UNIT(s
)->id
, strerror(-r
));
1325 socket_set_state(s
, SOCKET_START_POST
);
1327 socket_enter_listening(s
);
1332 socket_enter_stop_pre(s
, false);
1335 static void socket_enter_start_pre(Socket
*s
) {
1339 socket_unwatch_control_pid(s
);
1341 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
1343 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
])) {
1344 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1347 socket_set_state(s
, SOCKET_START_PRE
);
1349 socket_enter_start_post(s
);
1354 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s
)->id
, strerror(-r
));
1355 socket_enter_dead(s
, false);
1358 static void socket_enter_running(Socket
*s
, int cfd
) {
1363 dbus_error_init(&error
);
1365 /* We don't take connections anymore if we are supposed to
1366 * shut down anyway */
1367 if (unit_pending_inactive(UNIT(s
))) {
1368 log_debug("Suppressing connection request on %s since unit stop is scheduled.", UNIT(s
)->id
);
1371 close_nointr_nofail(cfd
);
1373 /* Flush all sockets by closing and reopening them */
1374 socket_close_fds(s
);
1376 r
= socket_watch_fds(s
);
1378 log_warning("%s failed to watch sockets: %s", UNIT(s
)->id
, strerror(-r
));
1379 socket_enter_stop_pre(s
, false);
1389 bool pending
= false;
1391 /* If there's already a start pending don't bother to
1393 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERS
], i
)
1394 if (unit_pending_active(u
)) {
1400 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT_DEREF(s
->service
), JOB_REPLACE
, true, &error
, NULL
);
1405 socket_set_state(s
, SOCKET_RUNNING
);
1407 char *prefix
, *instance
= NULL
, *name
;
1410 if (s
->n_connections
>= s
->max_connections
) {
1411 log_warning("Too many incoming connections (%u)", s
->n_connections
);
1412 close_nointr_nofail(cfd
);
1416 r
= socket_instantiate_service(s
);
1420 r
= instance_from_socket(cfd
, s
->n_accepted
, &instance
);
1425 /* ENOTCONN is legitimate if TCP RST was received.
1426 * This connection is over, but the socket unit lives on. */
1427 close_nointr_nofail(cfd
);
1431 prefix
= unit_name_to_prefix(UNIT(s
)->id
);
1438 name
= unit_name_build(prefix
, instance
, ".service");
1447 r
= unit_add_name(UNIT_DEREF(s
->service
), name
);
1453 service
= SERVICE(UNIT_DEREF(s
->service
));
1454 unit_ref_unset(&s
->service
);
1457 UNIT(service
)->no_gc
= false;
1459 unit_choose_id(UNIT(service
), name
);
1462 r
= service_set_socket_fd(service
, cfd
, s
);
1467 s
->n_connections
++;
1469 r
= manager_add_job(UNIT(s
)->manager
, JOB_START
, UNIT(service
), JOB_REPLACE
, true, &error
, NULL
);
1473 /* Notify clients about changed counters */
1474 unit_add_to_dbus_queue(UNIT(s
));
1480 log_warning("%s failed to queue socket startup job: %s", UNIT(s
)->id
, bus_error(&error
, r
));
1481 socket_enter_stop_pre(s
, false);
1484 close_nointr_nofail(cfd
);
1486 dbus_error_free(&error
);
1489 static void socket_run_next(Socket
*s
, bool success
) {
1493 assert(s
->control_command
);
1494 assert(s
->control_command
->command_next
);
1499 socket_unwatch_control_pid(s
);
1501 s
->control_command
= s
->control_command
->command_next
;
1503 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
1509 log_warning("%s failed to run next task: %s", UNIT(s
)->id
, strerror(-r
));
1511 if (s
->state
== SOCKET_START_POST
)
1512 socket_enter_stop_pre(s
, false);
1513 else if (s
->state
== SOCKET_STOP_POST
)
1514 socket_enter_dead(s
, false);
1516 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1519 static int socket_start(Unit
*u
) {
1520 Socket
*s
= SOCKET(u
);
1524 /* We cannot fulfill this request right now, try again later
1526 if (s
->state
== SOCKET_STOP_PRE
||
1527 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
1528 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
1529 s
->state
== SOCKET_STOP_POST
||
1530 s
->state
== SOCKET_FINAL_SIGTERM
||
1531 s
->state
== SOCKET_FINAL_SIGKILL
)
1534 if (s
->state
== SOCKET_START_PRE
||
1535 s
->state
== SOCKET_START_POST
)
1538 /* Cannot run this without the service being around */
1539 if (UNIT_DEREF(s
->service
)) {
1542 service
= SERVICE(UNIT_DEREF(s
->service
));
1544 if (UNIT(service
)->load_state
!= UNIT_LOADED
) {
1545 log_error("Socket service %s not loaded, refusing.", UNIT(service
)->id
);
1549 /* If the service is already active we cannot start the
1551 if (service
->state
!= SERVICE_DEAD
&&
1552 service
->state
!= SERVICE_FAILED
&&
1553 service
->state
!= SERVICE_AUTO_RESTART
) {
1554 log_error("Socket service %s already active, refusing.", UNIT(service
)->id
);
1558 #ifdef HAVE_SYSV_COMPAT
1559 if (service
->sysv_path
) {
1560 log_error("Using SysV services for socket activation is not supported. Refusing.");
1566 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_FAILED
);
1569 socket_enter_start_pre(s
);
1573 static int socket_stop(Unit
*u
) {
1574 Socket
*s
= SOCKET(u
);
1579 if (s
->state
== SOCKET_STOP_PRE
||
1580 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
1581 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
1582 s
->state
== SOCKET_STOP_POST
||
1583 s
->state
== SOCKET_FINAL_SIGTERM
||
1584 s
->state
== SOCKET_FINAL_SIGKILL
)
1587 /* If there's already something running we go directly into
1589 if (s
->state
== SOCKET_START_PRE
||
1590 s
->state
== SOCKET_START_POST
) {
1591 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, true);
1595 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
1597 socket_enter_stop_pre(s
, true);
1601 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1602 Socket
*s
= SOCKET(u
);
1610 unit_serialize_item(u
, f
, "state", socket_state_to_string(s
->state
));
1611 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
1612 unit_serialize_item_format(u
, f
, "n-accepted", "%u", s
->n_accepted
);
1614 if (s
->control_pid
> 0)
1615 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
1617 if (s
->control_command_id
>= 0)
1618 unit_serialize_item(u
, f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
1620 LIST_FOREACH(port
, p
, s
->ports
) {
1626 if ((copy
= fdset_put_dup(fds
, p
->fd
)) < 0)
1629 if (p
->type
== SOCKET_SOCKET
) {
1632 if ((r
= socket_address_print(&p
->address
, &t
)) < 0)
1635 if (socket_address_family(&p
->address
) == AF_NETLINK
)
1636 unit_serialize_item_format(u
, f
, "netlink", "%i %s", copy
, t
);
1638 unit_serialize_item_format(u
, f
, "socket", "%i %i %s", copy
, p
->address
.type
, t
);
1640 } else if (p
->type
== SOCKET_SPECIAL
)
1641 unit_serialize_item_format(u
, f
, "special", "%i %s", copy
, p
->path
);
1643 assert(p
->type
== SOCKET_FIFO
);
1644 unit_serialize_item_format(u
, f
, "fifo", "%i %s", copy
, p
->path
);
1651 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1652 Socket
*s
= SOCKET(u
);
1659 if (streq(key
, "state")) {
1662 if ((state
= socket_state_from_string(value
)) < 0)
1663 log_debug("Failed to parse state value %s", value
);
1665 s
->deserialized_state
= state
;
1666 } else if (streq(key
, "failure")) {
1669 if ((b
= parse_boolean(value
)) < 0)
1670 log_debug("Failed to parse failure value %s", value
);
1672 s
->failure
= b
|| s
->failure
;
1674 } else if (streq(key
, "n-accepted")) {
1677 if (safe_atou(value
, &k
) < 0)
1678 log_debug("Failed to parse n-accepted value %s", value
);
1681 } else if (streq(key
, "control-pid")) {
1684 if (parse_pid(value
, &pid
) < 0)
1685 log_debug("Failed to parse control-pid value %s", value
);
1687 s
->control_pid
= pid
;
1688 } else if (streq(key
, "control-command")) {
1689 SocketExecCommand id
;
1691 if ((id
= socket_exec_command_from_string(value
)) < 0)
1692 log_debug("Failed to parse exec-command value %s", value
);
1694 s
->control_command_id
= id
;
1695 s
->control_command
= s
->exec_command
[id
];
1697 } else if (streq(key
, "fifo")) {
1701 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1702 log_debug("Failed to parse fifo value %s", value
);
1705 LIST_FOREACH(port
, p
, s
->ports
)
1706 if (p
->type
== SOCKET_FIFO
&&
1707 streq_ptr(p
->path
, value
+skip
))
1712 close_nointr_nofail(p
->fd
);
1713 p
->fd
= fdset_remove(fds
, fd
);
1717 } else if (streq(key
, "special")) {
1721 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1722 log_debug("Failed to parse special value %s", value
);
1725 LIST_FOREACH(port
, p
, s
->ports
)
1726 if (p
->type
== SOCKET_SPECIAL
&&
1727 streq_ptr(p
->path
, value
+skip
))
1732 close_nointr_nofail(p
->fd
);
1733 p
->fd
= fdset_remove(fds
, fd
);
1737 } else if (streq(key
, "socket")) {
1738 int fd
, type
, skip
= 0;
1741 if (sscanf(value
, "%i %i %n", &fd
, &type
, &skip
) < 2 || fd
< 0 || type
< 0 || !fdset_contains(fds
, fd
))
1742 log_debug("Failed to parse socket value %s", value
);
1745 LIST_FOREACH(port
, p
, s
->ports
)
1746 if (socket_address_is(&p
->address
, value
+skip
, type
))
1751 close_nointr_nofail(p
->fd
);
1752 p
->fd
= fdset_remove(fds
, fd
);
1756 } else if (streq(key
, "netlink")) {
1760 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1761 log_debug("Failed to parse socket value %s", value
);
1764 LIST_FOREACH(port
, p
, s
->ports
)
1765 if (socket_address_is_netlink(&p
->address
, value
+skip
))
1770 close_nointr_nofail(p
->fd
);
1771 p
->fd
= fdset_remove(fds
, fd
);
1776 log_debug("Unknown serialization key '%s'", key
);
1781 static UnitActiveState
socket_active_state(Unit
*u
) {
1784 return state_translation_table
[SOCKET(u
)->state
];
1787 static const char *socket_sub_state_to_string(Unit
*u
) {
1790 return socket_state_to_string(SOCKET(u
)->state
);
1793 static bool socket_check_gc(Unit
*u
) {
1794 Socket
*s
= SOCKET(u
);
1798 return s
->n_connections
> 0;
1801 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
1802 Socket
*s
= SOCKET(u
);
1808 if (s
->state
!= SOCKET_LISTENING
)
1811 log_debug("Incoming traffic on %s", u
->id
);
1813 if (events
!= EPOLLIN
) {
1815 if (events
& EPOLLHUP
)
1816 log_error("%s: Got POLLHUP on a listening socket. The service probably invoked shutdown() on it, and should better not do that.", u
->id
);
1818 log_error("%s: Got unexpected poll event (0x%x) on socket.", u
->id
, events
);
1823 if (w
->socket_accept
) {
1826 if ((cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
)) < 0) {
1831 log_error("Failed to accept socket: %m");
1838 socket_apply_socket_options(s
, cfd
);
1841 socket_enter_running(s
, cfd
);
1845 socket_enter_stop_pre(s
, false);
1848 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1849 Socket
*s
= SOCKET(u
);
1855 if (pid
!= s
->control_pid
)
1860 success
= is_clean_exit(code
, status
);
1862 if (s
->control_command
) {
1863 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
1865 if (s
->control_command
->ignore
)
1869 log_full(success
? LOG_DEBUG
: LOG_NOTICE
,
1870 "%s control process exited, code=%s status=%i", u
->id
, sigchld_code_to_string(code
), status
);
1871 s
->failure
= s
->failure
|| !success
;
1873 if (s
->control_command
&& s
->control_command
->command_next
&& success
) {
1874 log_debug("%s running next command for state %s", u
->id
, socket_state_to_string(s
->state
));
1875 socket_run_next(s
, success
);
1877 s
->control_command
= NULL
;
1878 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1880 /* No further commands for this step, so let's figure
1881 * out what to do next */
1883 log_debug("%s got final SIGCHLD for state %s", u
->id
, socket_state_to_string(s
->state
));
1887 case SOCKET_START_PRE
:
1889 socket_enter_start_post(s
);
1891 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1894 case SOCKET_START_POST
:
1896 socket_enter_listening(s
);
1898 socket_enter_stop_pre(s
, false);
1901 case SOCKET_STOP_PRE
:
1902 case SOCKET_STOP_PRE_SIGTERM
:
1903 case SOCKET_STOP_PRE_SIGKILL
:
1904 socket_enter_stop_post(s
, success
);
1907 case SOCKET_STOP_POST
:
1908 case SOCKET_FINAL_SIGTERM
:
1909 case SOCKET_FINAL_SIGKILL
:
1910 socket_enter_dead(s
, success
);
1914 assert_not_reached("Uh, control process died at wrong time.");
1918 /* Notify clients about changed exit status */
1919 unit_add_to_dbus_queue(u
);
1922 static void socket_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
1923 Socket
*s
= SOCKET(u
);
1926 assert(elapsed
== 1);
1927 assert(w
== &s
->timer_watch
);
1931 case SOCKET_START_PRE
:
1932 log_warning("%s starting timed out. Terminating.", u
->id
);
1933 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1936 case SOCKET_START_POST
:
1937 log_warning("%s starting timed out. Stopping.", u
->id
);
1938 socket_enter_stop_pre(s
, false);
1941 case SOCKET_STOP_PRE
:
1942 log_warning("%s stopping timed out. Terminating.", u
->id
);
1943 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, false);
1946 case SOCKET_STOP_PRE_SIGTERM
:
1947 if (s
->exec_context
.send_sigkill
) {
1948 log_warning("%s stopping timed out. Killing.", u
->id
);
1949 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, false);
1951 log_warning("%s stopping timed out. Skipping SIGKILL. Ignoring.", u
->id
);
1952 socket_enter_stop_post(s
, false);
1956 case SOCKET_STOP_PRE_SIGKILL
:
1957 log_warning("%s still around after SIGKILL. Ignoring.", u
->id
);
1958 socket_enter_stop_post(s
, false);
1961 case SOCKET_STOP_POST
:
1962 log_warning("%s stopping timed out (2). Terminating.", u
->id
);
1963 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1966 case SOCKET_FINAL_SIGTERM
:
1967 if (s
->exec_context
.send_sigkill
) {
1968 log_warning("%s stopping timed out (2). Killing.", u
->id
);
1969 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, false);
1971 log_warning("%s stopping timed out (2). Skipping SIGKILL. Ignoring.", u
->id
);
1972 socket_enter_dead(s
, false);
1976 case SOCKET_FINAL_SIGKILL
:
1977 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u
->id
);
1978 socket_enter_dead(s
, false);
1982 assert_not_reached("Timeout at wrong time.");
1986 int socket_collect_fds(Socket
*s
, int **fds
, unsigned *n_fds
) {
1995 /* Called from the service code for requesting our fds */
1998 LIST_FOREACH(port
, p
, s
->ports
)
2008 if (!(rfds
= new(int, rn_fds
)))
2012 LIST_FOREACH(port
, p
, s
->ports
)
2016 assert(k
== rn_fds
);
2024 void socket_notify_service_dead(Socket
*s
) {
2027 /* The service is dead. Dang!
2029 * This is strictly for one-instance-for-all-connections
2032 if (s
->state
== SOCKET_RUNNING
) {
2033 log_debug("%s got notified about service death.", UNIT(s
)->id
);
2034 socket_enter_listening(s
);
2038 void socket_connection_unref(Socket
*s
) {
2041 /* The service is dead. Yay!
2043 * This is strictly for one-instance-per-connection
2046 assert(s
->n_connections
> 0);
2049 log_debug("%s: One connection closed, %u left.", UNIT(s
)->id
, s
->n_connections
);
2052 static void socket_reset_failed(Unit
*u
) {
2053 Socket
*s
= SOCKET(u
);
2057 if (s
->state
== SOCKET_FAILED
)
2058 socket_set_state(s
, SOCKET_DEAD
);
2063 static int socket_kill(Unit
*u
, KillWho who
, KillMode mode
, int signo
, DBusError
*error
) {
2064 Socket
*s
= SOCKET(u
);
2066 Set
*pid_set
= NULL
;
2070 if (who
== KILL_MAIN
) {
2071 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "Socket units have no main processes");
2075 if (s
->control_pid
<= 0 && who
== KILL_CONTROL
) {
2076 dbus_set_error(error
, BUS_ERROR_NO_SUCH_PROCESS
, "No control process to kill");
2080 if (who
== KILL_CONTROL
|| who
== KILL_ALL
)
2081 if (s
->control_pid
> 0)
2082 if (kill(s
->control_pid
, signo
) < 0)
2085 if (who
== KILL_ALL
&& mode
== KILL_CONTROL_GROUP
) {
2088 if (!(pid_set
= set_new(trivial_hash_func
, trivial_compare_func
)))
2091 /* Exclude the control pid from being killed via the cgroup */
2092 if (s
->control_pid
> 0)
2093 if ((q
= set_put(pid_set
, LONG_TO_PTR(s
->control_pid
))) < 0) {
2098 if ((q
= cgroup_bonding_kill_list(UNIT(s
)->cgroup_bondings
, signo
, false, pid_set
)) < 0)
2099 if (q
!= -EAGAIN
&& q
!= -ESRCH
&& q
!= -ENOENT
)
2110 static const char* const socket_state_table
[_SOCKET_STATE_MAX
] = {
2111 [SOCKET_DEAD
] = "dead",
2112 [SOCKET_START_PRE
] = "start-pre",
2113 [SOCKET_START_POST
] = "start-post",
2114 [SOCKET_LISTENING
] = "listening",
2115 [SOCKET_RUNNING
] = "running",
2116 [SOCKET_STOP_PRE
] = "stop-pre",
2117 [SOCKET_STOP_PRE_SIGTERM
] = "stop-pre-sigterm",
2118 [SOCKET_STOP_PRE_SIGKILL
] = "stop-pre-sigkill",
2119 [SOCKET_STOP_POST
] = "stop-post",
2120 [SOCKET_FINAL_SIGTERM
] = "final-sigterm",
2121 [SOCKET_FINAL_SIGKILL
] = "final-sigkill",
2122 [SOCKET_FAILED
] = "failed"
2125 DEFINE_STRING_TABLE_LOOKUP(socket_state
, SocketState
);
2127 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
2128 [SOCKET_EXEC_START_PRE
] = "StartPre",
2129 [SOCKET_EXEC_START_POST
] = "StartPost",
2130 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
2131 [SOCKET_EXEC_STOP_POST
] = "StopPost"
2134 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
2136 const UnitVTable socket_vtable
= {
2137 .suffix
= ".socket",
2138 .object_size
= sizeof(Socket
),
2144 .init
= socket_init
,
2145 .done
= socket_done
,
2146 .load
= socket_load
,
2148 .kill
= socket_kill
,
2150 .coldplug
= socket_coldplug
,
2152 .dump
= socket_dump
,
2154 .start
= socket_start
,
2155 .stop
= socket_stop
,
2157 .serialize
= socket_serialize
,
2158 .deserialize_item
= socket_deserialize_item
,
2160 .active_state
= socket_active_state
,
2161 .sub_state_to_string
= socket_sub_state_to_string
,
2163 .check_gc
= socket_check_gc
,
2165 .fd_event
= socket_fd_event
,
2166 .sigchld_event
= socket_sigchld_event
,
2167 .timer_event
= socket_timer_event
,
2169 .reset_failed
= socket_reset_failed
,
2171 .bus_interface
= "org.freedesktop.systemd1.Socket",
2172 .bus_message_handler
= bus_socket_message_handler
,
2173 .bus_invalidating_properties
= bus_socket_invalidating_properties