1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 "load-dropin.h"
35 #include "load-fragment.h"
37 #include "unit-name.h"
38 #include "dbus-socket.h"
40 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
41 [SOCKET_DEAD
] = UNIT_INACTIVE
,
42 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
43 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
44 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
45 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
46 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
47 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
48 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
49 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
50 [SOCKET_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
51 [SOCKET_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
52 [SOCKET_MAINTENANCE
] = UNIT_INACTIVE
,
55 static void socket_init(Unit
*u
) {
56 Socket
*s
= SOCKET(u
);
59 assert(u
->meta
.load_state
== UNIT_STUB
);
61 s
->backlog
= SOMAXCONN
;
62 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
63 s
->directory_mode
= 0755;
64 s
->socket_mode
= 0666;
66 exec_context_init(&s
->exec_context
);
68 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
71 static void socket_unwatch_control_pid(Socket
*s
) {
74 if (s
->control_pid
<= 0)
77 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
81 static void socket_done(Unit
*u
) {
82 Socket
*s
= SOCKET(u
);
87 while ((p
= s
->ports
)) {
88 LIST_REMOVE(SocketPort
, port
, s
->ports
, p
);
91 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
92 close_nointr_nofail(p
->fd
);
99 exec_context_done(&s
->exec_context
);
100 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
101 s
->control_command
= NULL
;
103 socket_unwatch_control_pid(s
);
107 free(s
->bind_to_device
);
108 s
->bind_to_device
= NULL
;
110 unit_unwatch_timer(u
, &s
->timer_watch
);
113 static bool have_non_accept_socket(Socket
*s
) {
121 LIST_FOREACH(port
, p
, s
->ports
) {
123 if (p
->type
!= SOCKET_SOCKET
)
126 if (!socket_address_can_accept(&p
->address
))
133 static int socket_verify(Socket
*s
) {
136 if (UNIT(s
)->meta
.load_state
!= UNIT_LOADED
)
140 log_error("%s lacks Listen setting. Refusing.", UNIT(s
)->meta
.id
);
147 static bool socket_needs_mount(Socket
*s
, const char *prefix
) {
152 LIST_FOREACH(port
, p
, s
->ports
) {
154 if (p
->type
== SOCKET_SOCKET
) {
155 if (socket_address_needs_mount(&p
->address
, prefix
))
158 assert(p
->type
== SOCKET_FIFO
);
159 if (path_startswith(p
->path
, prefix
))
167 int socket_add_one_mount_link(Socket
*s
, Mount
*m
) {
173 if (s
->meta
.load_state
!= UNIT_LOADED
||
174 m
->meta
.load_state
!= UNIT_LOADED
)
177 if (!socket_needs_mount(s
, m
->where
))
180 if ((r
= unit_add_dependency(UNIT(m
), UNIT_BEFORE
, UNIT(s
), true)) < 0)
183 if ((r
= unit_add_dependency(UNIT(s
), UNIT_REQUIRES
, UNIT(m
), true)) < 0)
189 static int socket_add_mount_links(Socket
*s
) {
195 LIST_FOREACH(units_per_type
, other
, s
->meta
.manager
->units_per_type
[UNIT_MOUNT
])
196 if ((r
= socket_add_one_mount_link(s
, (Mount
*) other
)) < 0)
202 static int socket_add_device_link(Socket
*s
) {
208 if (!s
->bind_to_device
)
211 if (asprintf(&t
, "/sys/subsystem/net/devices/%s", s
->bind_to_device
) < 0)
214 r
= unit_add_node_link(UNIT(s
), t
, false);
220 static int socket_load(Unit
*u
) {
221 Socket
*s
= SOCKET(u
);
225 assert(u
->meta
.load_state
== UNIT_STUB
);
227 if ((r
= unit_load_fragment_and_dropin(u
)) < 0)
230 /* This is a new unit? Then let's add in some extras */
231 if (u
->meta
.load_state
== UNIT_LOADED
) {
233 if (have_non_accept_socket(s
)) {
234 if ((r
= unit_load_related_unit(u
, ".service", (Unit
**) &s
->service
)))
237 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(s
->service
), true)) < 0)
241 if ((r
= socket_add_mount_links(s
)) < 0)
244 if ((r
= socket_add_device_link(s
)) < 0)
247 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
250 if ((r
= unit_add_default_cgroup(u
)) < 0)
254 return socket_verify(s
);
257 static const char* listen_lookup(int type
) {
259 if (type
== SOCK_STREAM
)
260 return "ListenStream";
261 else if (type
== SOCK_DGRAM
)
262 return "ListenDatagram";
263 else if (type
== SOCK_SEQPACKET
)
264 return "ListenSequentialPacket";
266 assert_not_reached("Unknown socket type");
270 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
273 Socket
*s
= SOCKET(u
);
281 p2
= strappend(prefix
, "\t");
282 prefix2
= p2
? p2
: prefix
;
285 "%sSocket State: %s\n"
286 "%sBindIPv6Only: %s\n"
289 "%sSocketMode: %04o\n"
290 "%sDirectoryMode: %04o\n",
291 prefix
, socket_state_to_string(s
->state
),
292 prefix
, socket_address_bind_ipv6_only_to_string(s
->bind_ipv6_only
),
294 prefix
, kill_mode_to_string(s
->kill_mode
),
295 prefix
, s
->socket_mode
,
296 prefix
, s
->directory_mode
);
298 if (s
->control_pid
> 0)
300 "%sControl PID: %llu\n",
301 prefix
, (unsigned long long) s
->control_pid
);
303 if (s
->bind_to_device
)
305 "%sBindToDevice: %s\n",
306 prefix
, s
->bind_to_device
);
311 prefix
, s
->n_accepted
);
313 LIST_FOREACH(port
, p
, s
->ports
) {
315 if (p
->type
== SOCKET_SOCKET
) {
320 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
325 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(p
->address
.type
), k
);
328 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
331 exec_context_dump(&s
->exec_context
, f
, prefix
);
333 for (c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
334 if (!s
->exec_command
[c
])
337 fprintf(f
, "%s-> %s:\n",
338 prefix
, socket_exec_command_to_string(c
));
340 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
346 static int instance_from_socket(int fd
, unsigned nr
, char **instance
) {
351 struct sockaddr_un un
;
352 struct sockaddr_in in
;
353 struct sockaddr_in6 in6
;
354 struct sockaddr_storage storage
;
361 if (getsockname(fd
, &local
.sa
, &l
) < 0)
365 if (getpeername(fd
, &remote
.sa
, &l
) < 0)
368 switch (local
.sa
.sa_family
) {
372 a
= ntohl(local
.in
.sin_addr
.s_addr
),
373 b
= ntohl(remote
.in
.sin_addr
.s_addr
);
376 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
378 a
>> 24, (a
>> 16) & 0xFF, (a
>> 8) & 0xFF, a
& 0xFF,
379 ntohs(local
.in
.sin_port
),
380 b
>> 24, (b
>> 16) & 0xFF, (b
>> 8) & 0xFF, b
& 0xFF,
381 ntohs(remote
.in
.sin_port
)) < 0)
388 static const char ipv4_prefix
[] = {
389 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF
392 if (memcmp(&local
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0 &&
393 memcmp(&remote
.in6
.sin6_addr
, ipv4_prefix
, sizeof(ipv4_prefix
)) == 0) {
395 *a
= local
.in6
.sin6_addr
.s6_addr
+12,
396 *b
= remote
.in6
.sin6_addr
.s6_addr
+12;
399 "%u-%u.%u.%u.%u:%u-%u.%u.%u.%u:%u",
401 a
[0], a
[1], a
[2], a
[3],
402 ntohs(local
.in6
.sin6_port
),
403 b
[0], b
[1], b
[2], b
[3],
404 ntohs(remote
.in6
.sin6_port
)) < 0)
407 char a
[INET6_ADDRSTRLEN
], b
[INET6_ADDRSTRLEN
];
412 inet_ntop(AF_INET6
, &local
.in6
.sin6_addr
, a
, sizeof(a
)),
413 ntohs(local
.in6
.sin6_port
),
414 inet_ntop(AF_INET6
, &remote
.in6
.sin6_addr
, b
, sizeof(b
)),
415 ntohs(remote
.in6
.sin6_port
)) < 0)
426 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
) < 0)
432 (unsigned long long) ucred
.pid
,
433 (unsigned long long) ucred
.uid
) < 0)
440 assert_not_reached("Unhandled socket type.");
447 static void socket_close_fds(Socket
*s
) {
452 LIST_FOREACH(port
, p
, s
->ports
) {
456 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
457 close_nointr_nofail(p
->fd
);
459 /* One little note: we should never delete any sockets
460 * in the file system here! After all some other
461 * process we spawned might still have a reference of
462 * this fd and wants to continue to use it. Therefore
463 * we delete sockets in the file system before we
464 * create a new one, not after we stopped using
471 static int socket_open_fds(Socket
*s
) {
477 LIST_FOREACH(port
, p
, s
->ports
) {
482 if (p
->type
== SOCKET_SOCKET
) {
484 if ((r
= socket_address_listen(
496 assert(p
->type
== SOCKET_FIFO
);
498 mkdir_parents(p
->path
, s
->directory_mode
);
500 if (mkfifo(p
->path
, s
->socket_mode
) < 0 && errno
!= EEXIST
) {
505 if ((p
->fd
= open(p
->path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
510 if (fstat(p
->fd
, &st
) < 0) {
515 /* FIXME verify user, access mode */
517 if (!S_ISFIFO(st
.st_mode
)) {
531 static void socket_unwatch_fds(Socket
*s
) {
536 LIST_FOREACH(port
, p
, s
->ports
) {
540 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
544 static int socket_watch_fds(Socket
*s
) {
550 LIST_FOREACH(port
, p
, s
->ports
) {
554 p
->fd_watch
.socket_accept
=
556 p
->type
== SOCKET_SOCKET
&&
557 socket_address_can_accept(&p
->address
);
559 if ((r
= unit_watch_fd(UNIT(s
), p
->fd
, EPOLLIN
, &p
->fd_watch
)) < 0)
566 socket_unwatch_fds(s
);
570 static void socket_set_state(Socket
*s
, SocketState state
) {
571 SocketState old_state
;
574 old_state
= s
->state
;
577 if (state
!= SOCKET_START_PRE
&&
578 state
!= SOCKET_START_POST
&&
579 state
!= SOCKET_STOP_PRE
&&
580 state
!= SOCKET_STOP_PRE_SIGTERM
&&
581 state
!= SOCKET_STOP_PRE_SIGKILL
&&
582 state
!= SOCKET_STOP_POST
&&
583 state
!= SOCKET_FINAL_SIGTERM
&&
584 state
!= SOCKET_FINAL_SIGKILL
) {
585 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
586 socket_unwatch_control_pid(s
);
587 s
->control_command
= NULL
;
588 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
591 if (state
!= SOCKET_LISTENING
)
592 socket_unwatch_fds(s
);
594 if (state
!= SOCKET_START_POST
&&
595 state
!= SOCKET_LISTENING
&&
596 state
!= SOCKET_RUNNING
&&
597 state
!= SOCKET_STOP_PRE
&&
598 state
!= SOCKET_STOP_PRE_SIGTERM
&&
599 state
!= SOCKET_STOP_PRE_SIGKILL
)
602 if (state
!= old_state
)
603 log_debug("%s changed %s -> %s",
605 socket_state_to_string(old_state
),
606 socket_state_to_string(state
));
608 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
611 static int socket_coldplug(Unit
*u
) {
612 Socket
*s
= SOCKET(u
);
616 assert(s
->state
== SOCKET_DEAD
);
618 if (s
->deserialized_state
!= s
->state
) {
620 if (s
->deserialized_state
== SOCKET_START_PRE
||
621 s
->deserialized_state
== SOCKET_START_POST
||
622 s
->deserialized_state
== SOCKET_STOP_PRE
||
623 s
->deserialized_state
== SOCKET_STOP_PRE_SIGTERM
||
624 s
->deserialized_state
== SOCKET_STOP_PRE_SIGKILL
||
625 s
->deserialized_state
== SOCKET_STOP_POST
||
626 s
->deserialized_state
== SOCKET_FINAL_SIGTERM
||
627 s
->deserialized_state
== SOCKET_FINAL_SIGKILL
) {
629 if (s
->control_pid
<= 0)
632 if ((r
= unit_watch_pid(UNIT(s
), s
->control_pid
)) < 0)
635 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
639 if (s
->deserialized_state
== SOCKET_START_POST
||
640 s
->deserialized_state
== SOCKET_LISTENING
||
641 s
->deserialized_state
== SOCKET_RUNNING
||
642 s
->deserialized_state
== SOCKET_STOP_PRE
||
643 s
->deserialized_state
== SOCKET_STOP_PRE_SIGTERM
||
644 s
->deserialized_state
== SOCKET_STOP_PRE_SIGKILL
)
645 if ((r
= socket_open_fds(s
)) < 0)
648 if (s
->deserialized_state
== SOCKET_LISTENING
)
649 if ((r
= socket_watch_fds(s
)) < 0)
652 socket_set_state(s
, s
->deserialized_state
);
658 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
667 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
670 if (!(argv
= unit_full_printf_strv(UNIT(s
), c
->argv
))) {
679 s
->meta
.manager
->environment
,
682 UNIT(s
)->meta
.manager
->confirm_spawn
,
683 UNIT(s
)->meta
.cgroup_bondings
,
690 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
691 /* FIXME: we need to do something here */
699 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
704 static void socket_enter_dead(Socket
*s
, bool success
) {
710 socket_set_state(s
, s
->failure
? SOCKET_MAINTENANCE
: SOCKET_DEAD
);
713 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
);
715 static void socket_enter_stop_post(Socket
*s
, bool success
) {
722 socket_unwatch_control_pid(s
);
724 s
->control_command_id
= SOCKET_EXEC_STOP_POST
;
726 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
])) {
727 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
730 socket_set_state(s
, SOCKET_STOP_POST
);
732 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, true);
737 log_warning("%s failed to run stop-post executable: %s", s
->meta
.id
, strerror(-r
));
738 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
741 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
) {
750 if (s
->kill_mode
!= KILL_NONE
) {
751 int sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
753 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
755 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
756 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
762 if (!sent
&& s
->control_pid
> 0)
763 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
769 if (sent
&& s
->control_pid
> 0) {
770 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
773 socket_set_state(s
, state
);
774 } else if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
775 socket_enter_stop_post(s
, true);
777 socket_enter_dead(s
, true);
782 log_warning("%s failed to kill processes: %s", s
->meta
.id
, strerror(-r
));
784 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
785 socket_enter_stop_post(s
, false);
787 socket_enter_dead(s
, false);
790 static void socket_enter_stop_pre(Socket
*s
, bool success
) {
797 socket_unwatch_control_pid(s
);
799 s
->control_command_id
= SOCKET_EXEC_STOP_PRE
;
801 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
])) {
802 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
805 socket_set_state(s
, SOCKET_STOP_PRE
);
807 socket_enter_stop_post(s
, true);
812 log_warning("%s failed to run stop-pre executable: %s", s
->meta
.id
, strerror(-r
));
813 socket_enter_stop_post(s
, false);
816 static void socket_enter_listening(Socket
*s
) {
820 if ((r
= socket_watch_fds(s
)) < 0) {
821 log_warning("%s failed to watch sockets: %s", s
->meta
.id
, strerror(-r
));
825 socket_set_state(s
, SOCKET_LISTENING
);
829 socket_enter_stop_pre(s
, false);
832 static void socket_enter_start_post(Socket
*s
) {
836 if ((r
= socket_open_fds(s
)) < 0) {
837 log_warning("%s failed to listen on sockets: %s", s
->meta
.id
, strerror(-r
));
841 socket_unwatch_control_pid(s
);
843 s
->control_command_id
= SOCKET_EXEC_START_POST
;
845 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
])) {
846 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0) {
847 log_warning("%s failed to run start-post executable: %s", s
->meta
.id
, strerror(-r
));
851 socket_set_state(s
, SOCKET_START_POST
);
853 socket_enter_listening(s
);
858 socket_enter_stop_pre(s
, false);
861 static void socket_enter_start_pre(Socket
*s
) {
865 socket_unwatch_control_pid(s
);
867 s
->control_command_id
= SOCKET_EXEC_START_PRE
;
869 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
])) {
870 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
873 socket_set_state(s
, SOCKET_START_PRE
);
875 socket_enter_start_post(s
);
880 log_warning("%s failed to run start-pre exectuable: %s", s
->meta
.id
, strerror(-r
));
881 socket_enter_dead(s
, false);
884 static void socket_enter_running(Socket
*s
, int cfd
) {
890 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
->service
), JOB_REPLACE
, true, NULL
)) < 0)
893 socket_set_state(s
, SOCKET_RUNNING
);
896 char *prefix
, *instance
, *name
;
898 if ((r
= instance_from_socket(cfd
, s
->n_accepted
++, &instance
)) < 0)
901 if (!(prefix
= unit_name_to_prefix(UNIT(s
)->meta
.id
))) {
907 name
= unit_name_build(prefix
, instance
, ".service");
916 r
= manager_load_unit(UNIT(s
)->meta
.manager
, name
, NULL
, &u
);
922 if ((r
= service_set_socket_fd(SERVICE(u
), cfd
)) < 0)
927 if ((r
= manager_add_job(u
->meta
.manager
, JOB_START
, u
, JOB_REPLACE
, true, NULL
)) < 0)
934 log_warning("%s failed to queue socket startup job: %s", s
->meta
.id
, strerror(-r
));
935 socket_enter_stop_pre(s
, false);
938 close_nointr_nofail(cfd
);
941 static void socket_run_next(Socket
*s
, bool success
) {
945 assert(s
->control_command
);
946 assert(s
->control_command
->command_next
);
951 socket_unwatch_control_pid(s
);
953 s
->control_command
= s
->control_command
->command_next
;
955 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
961 log_warning("%s failed to run spawn next executable: %s", s
->meta
.id
, strerror(-r
));
963 if (s
->state
== SOCKET_START_POST
)
964 socket_enter_stop_pre(s
, false);
965 else if (s
->state
== SOCKET_STOP_POST
)
966 socket_enter_dead(s
, false);
968 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
971 static int socket_start(Unit
*u
) {
972 Socket
*s
= SOCKET(u
);
976 /* We cannot fulfill this request right now, try again later
978 if (s
->state
== SOCKET_STOP_PRE
||
979 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
980 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
981 s
->state
== SOCKET_STOP_POST
||
982 s
->state
== SOCKET_FINAL_SIGTERM
||
983 s
->state
== SOCKET_FINAL_SIGKILL
)
986 if (s
->state
== SOCKET_START_PRE
||
987 s
->state
== SOCKET_START_POST
)
990 /* Cannot run this without the service being around */
992 if (s
->service
->meta
.load_state
!= UNIT_LOADED
)
995 /* If the service is alredy actvie we cannot start the
997 if (s
->service
->state
!= SERVICE_DEAD
&&
998 s
->service
->state
!= SERVICE_MAINTENANCE
&&
999 s
->service
->state
!= SERVICE_AUTO_RESTART
)
1003 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_MAINTENANCE
);
1006 socket_enter_start_pre(s
);
1010 static int socket_stop(Unit
*u
) {
1011 Socket
*s
= SOCKET(u
);
1015 /* We cannot fulfill this request right now, try again later
1017 if (s
->state
== SOCKET_START_PRE
||
1018 s
->state
== SOCKET_START_POST
)
1022 if (s
->state
== SOCKET_STOP_PRE
||
1023 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
1024 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
1025 s
->state
== SOCKET_STOP_POST
||
1026 s
->state
== SOCKET_FINAL_SIGTERM
||
1027 s
->state
== SOCKET_FINAL_SIGTERM
)
1030 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
1032 socket_enter_stop_pre(s
, true);
1036 static int socket_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1037 Socket
*s
= SOCKET(u
);
1045 unit_serialize_item(u
, f
, "state", socket_state_to_string(s
->state
));
1046 unit_serialize_item(u
, f
, "failure", yes_no(s
->failure
));
1047 unit_serialize_item_format(u
, f
, "n-accepted", "%u", s
->n_accepted
);
1049 if (s
->control_pid
> 0)
1050 unit_serialize_item_format(u
, f
, "control-pid", "%lu", (unsigned long) s
->control_pid
);
1052 if (s
->control_command_id
>= 0)
1053 unit_serialize_item(u
, f
, "control-command", socket_exec_command_to_string(s
->control_command_id
));
1055 LIST_FOREACH(port
, p
, s
->ports
) {
1061 if ((copy
= fdset_put_dup(fds
, p
->fd
)) < 0)
1064 if (p
->type
== SOCKET_SOCKET
) {
1067 if ((r
= socket_address_print(&p
->address
, &t
)) < 0)
1070 unit_serialize_item_format(u
, f
, "socket", "%i %i %s", copy
, p
->address
.type
, t
);
1073 assert(p
->type
== SOCKET_FIFO
);
1074 unit_serialize_item_format(u
, f
, "fifo", "%i %s", copy
, p
->path
);
1081 static int socket_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1082 Socket
*s
= SOCKET(u
);
1090 if (streq(key
, "state")) {
1093 if ((state
= socket_state_from_string(value
)) < 0)
1094 log_debug("Failed to parse state value %s", value
);
1096 s
->deserialized_state
= state
;
1097 } else if (streq(key
, "failure")) {
1100 if ((b
= parse_boolean(value
)) < 0)
1101 log_debug("Failed to parse failure value %s", value
);
1103 s
->failure
= b
|| s
->failure
;
1105 } else if (streq(key
, "n-accepted")) {
1108 if ((r
= safe_atou(value
, &k
)) < 0)
1109 log_debug("Failed to parse n-accepted value %s", value
);
1112 } else if (streq(key
, "control-pid")) {
1115 if ((r
= parse_pid(value
, &pid
)) < 0)
1116 log_debug("Failed to parse control-pid value %s", value
);
1118 s
->control_pid
= pid
;
1119 } else if (streq(key
, "control-command")) {
1120 SocketExecCommand id
;
1122 if ((id
= socket_exec_command_from_string(value
)) < 0)
1123 log_debug("Failed to parse exec-command value %s", value
);
1125 s
->control_command_id
= id
;
1126 s
->control_command
= s
->exec_command
[id
];
1128 } else if (streq(key
, "fifo")) {
1132 if (sscanf(value
, "%i %n", &fd
, &skip
) < 1 || fd
< 0 || !fdset_contains(fds
, fd
))
1133 log_debug("Failed to parse fifo value %s", value
);
1136 LIST_FOREACH(port
, p
, s
->ports
)
1137 if (streq(p
->path
, value
+skip
))
1142 close_nointr_nofail(p
->fd
);
1143 p
->fd
= fdset_remove(fds
, fd
);
1147 } else if (streq(key
, "socket")) {
1148 int fd
, type
, skip
= 0;
1151 if (sscanf(value
, "%i %i %n", &fd
, &type
, &skip
) < 2 || fd
< 0 || type
< 0 || !fdset_contains(fds
, fd
))
1152 log_debug("Failed to parse socket value %s", value
);
1155 LIST_FOREACH(port
, p
, s
->ports
)
1156 if (socket_address_is(&p
->address
, value
+skip
, type
))
1161 close_nointr_nofail(p
->fd
);
1162 p
->fd
= fdset_remove(fds
, fd
);
1167 log_debug("Unknown serialization key '%s'", key
);
1172 static UnitActiveState
socket_active_state(Unit
*u
) {
1175 return state_translation_table
[SOCKET(u
)->state
];
1178 static const char *socket_sub_state_to_string(Unit
*u
) {
1181 return socket_state_to_string(SOCKET(u
)->state
);
1184 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
1185 Socket
*s
= SOCKET(u
);
1191 if (s
->state
!= SOCKET_LISTENING
)
1194 log_debug("Incoming traffic on %s", u
->meta
.id
);
1196 if (events
!= EPOLLIN
) {
1197 log_error("Got invalid poll event on socket.");
1201 if (w
->socket_accept
) {
1204 if ((cfd
= accept4(fd
, NULL
, NULL
, SOCK_NONBLOCK
)) < 0) {
1209 log_error("Failed to accept socket: %m");
1217 socket_enter_running(s
, cfd
);
1221 socket_enter_stop_pre(s
, false);
1224 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
1225 Socket
*s
= SOCKET(u
);
1231 if (pid
!= s
->control_pid
)
1236 success
= is_clean_exit(code
, status
);
1237 s
->failure
= s
->failure
|| !success
;
1239 if (s
->control_command
)
1240 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
1242 log_debug("%s control process exited, code=%s status=%i", u
->meta
.id
, sigchld_code_to_string(code
), status
);
1244 if (s
->control_command
&& s
->control_command
->command_next
&& success
) {
1245 log_debug("%s running next command for state %s", u
->meta
.id
, socket_state_to_string(s
->state
));
1246 socket_run_next(s
, success
);
1248 s
->control_command
= NULL
;
1249 s
->control_command_id
= _SOCKET_EXEC_COMMAND_INVALID
;
1251 /* No further commands for this step, so let's figure
1252 * out what to do next */
1254 log_debug("%s got final SIGCHLD for state %s", u
->meta
.id
, socket_state_to_string(s
->state
));
1258 case SOCKET_START_PRE
:
1260 socket_enter_start_post(s
);
1262 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1265 case SOCKET_START_POST
:
1267 socket_enter_listening(s
);
1269 socket_enter_stop_pre(s
, false);
1272 case SOCKET_STOP_PRE
:
1273 case SOCKET_STOP_PRE_SIGTERM
:
1274 case SOCKET_STOP_PRE_SIGKILL
:
1275 socket_enter_stop_post(s
, success
);
1278 case SOCKET_STOP_POST
:
1279 case SOCKET_FINAL_SIGTERM
:
1280 case SOCKET_FINAL_SIGKILL
:
1281 socket_enter_dead(s
, success
);
1285 assert_not_reached("Uh, control process died at wrong time.");
1290 static void socket_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
1291 Socket
*s
= SOCKET(u
);
1294 assert(elapsed
== 1);
1295 assert(w
== &s
->timer_watch
);
1299 case SOCKET_START_PRE
:
1300 log_warning("%s starting timed out. Terminating.", u
->meta
.id
);
1301 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1303 case SOCKET_START_POST
:
1304 log_warning("%s starting timed out. Stopping.", u
->meta
.id
);
1305 socket_enter_stop_pre(s
, false);
1308 case SOCKET_STOP_PRE
:
1309 log_warning("%s stopping timed out. Terminating.", u
->meta
.id
);
1310 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, false);
1313 case SOCKET_STOP_PRE_SIGTERM
:
1314 log_warning("%s stopping timed out. Killing.", u
->meta
.id
);
1315 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, false);
1318 case SOCKET_STOP_PRE_SIGKILL
:
1319 log_warning("%s still around after SIGKILL. Ignoring.", u
->meta
.id
);
1320 socket_enter_stop_post(s
, false);
1323 case SOCKET_STOP_POST
:
1324 log_warning("%s stopping timed out (2). Terminating.", u
->meta
.id
);
1325 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
1328 case SOCKET_FINAL_SIGTERM
:
1329 log_warning("%s stopping timed out (2). Killing.", u
->meta
.id
);
1330 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, false);
1333 case SOCKET_FINAL_SIGKILL
:
1334 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u
->meta
.id
);
1335 socket_enter_dead(s
, false);
1339 assert_not_reached("Timeout at wrong time.");
1343 int socket_collect_fds(Socket
*s
, int **fds
, unsigned *n_fds
) {
1352 /* Called from the service code for requesting our fds */
1355 LIST_FOREACH(port
, p
, s
->ports
)
1359 if (!(rfds
= new(int, rn_fds
)) < 0)
1363 LIST_FOREACH(port
, p
, s
->ports
)
1367 assert(k
== rn_fds
);
1375 void socket_notify_service_dead(Socket
*s
) {
1378 /* The service is dead. Dang. */
1380 if (s
->state
== SOCKET_RUNNING
) {
1381 log_debug("%s got notified about service death.", s
->meta
.id
);
1382 socket_enter_listening(s
);
1386 static const char* const socket_state_table
[_SOCKET_STATE_MAX
] = {
1387 [SOCKET_DEAD
] = "dead",
1388 [SOCKET_START_PRE
] = "start-pre",
1389 [SOCKET_START_POST
] = "start-post",
1390 [SOCKET_LISTENING
] = "listening",
1391 [SOCKET_RUNNING
] = "running",
1392 [SOCKET_STOP_PRE
] = "stop-pre",
1393 [SOCKET_STOP_PRE_SIGTERM
] = "stop-pre-sigterm",
1394 [SOCKET_STOP_PRE_SIGKILL
] = "stop-pre-sigkill",
1395 [SOCKET_STOP_POST
] = "stop-post",
1396 [SOCKET_FINAL_SIGTERM
] = "final-sigterm",
1397 [SOCKET_FINAL_SIGKILL
] = "final-sigkill",
1398 [SOCKET_MAINTENANCE
] = "maintenance"
1401 DEFINE_STRING_TABLE_LOOKUP(socket_state
, SocketState
);
1403 static const char* const socket_exec_command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
1404 [SOCKET_EXEC_START_PRE
] = "StartPre",
1405 [SOCKET_EXEC_START_POST
] = "StartPost",
1406 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
1407 [SOCKET_EXEC_STOP_POST
] = "StopPost"
1410 DEFINE_STRING_TABLE_LOOKUP(socket_exec_command
, SocketExecCommand
);
1412 const UnitVTable socket_vtable
= {
1413 .suffix
= ".socket",
1415 .init
= socket_init
,
1416 .done
= socket_done
,
1417 .load
= socket_load
,
1419 .coldplug
= socket_coldplug
,
1421 .dump
= socket_dump
,
1423 .start
= socket_start
,
1424 .stop
= socket_stop
,
1426 .serialize
= socket_serialize
,
1427 .deserialize_item
= socket_deserialize_item
,
1429 .active_state
= socket_active_state
,
1430 .sub_state_to_string
= socket_sub_state_to_string
,
1432 .fd_event
= socket_fd_event
,
1433 .sigchld_event
= socket_sigchld_event
,
1434 .timer_event
= socket_timer_event
,
1436 .bus_message_handler
= bus_socket_message_handler