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>
34 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
35 [SOCKET_DEAD
] = UNIT_INACTIVE
,
36 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
37 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
38 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
39 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
40 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
41 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
42 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
43 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
44 [SOCKET_STOP_POST_SIGTERM
] = UNIT_DEACTIVATING
,
45 [SOCKET_STOP_POST_SIGKILL
] = UNIT_DEACTIVATING
,
46 [SOCKET_MAINTAINANCE
] = UNIT_INACTIVE
,
49 static const char* const state_string_table
[_SOCKET_STATE_MAX
] = {
50 [SOCKET_DEAD
] = "dead",
51 [SOCKET_START_PRE
] = "start-pre",
52 [SOCKET_START_POST
] = "start-post",
53 [SOCKET_LISTENING
] = "listening",
54 [SOCKET_RUNNING
] = "running",
55 [SOCKET_STOP_PRE
] = "stop-pre",
56 [SOCKET_STOP_PRE_SIGTERM
] = "stop-pre-sigterm",
57 [SOCKET_STOP_PRE_SIGKILL
] = "stop-pre-sigkill",
58 [SOCKET_STOP_POST
] = "stop-post",
59 [SOCKET_STOP_POST_SIGTERM
] = "stop-post-sigterm",
60 [SOCKET_STOP_POST_SIGKILL
] = "stop-post-sigkill",
61 [SOCKET_MAINTAINANCE
] = "maintainance"
64 static void socket_done(Unit
*u
) {
65 Socket
*s
= SOCKET(u
);
70 while ((p
= s
->ports
)) {
71 LIST_REMOVE(SocketPort
, port
, s
->ports
, p
);
79 exec_context_done(&s
->exec_context
);
80 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_MAX
);
81 s
->control_command
= NULL
;
83 if (s
->control_pid
> 0) {
84 unit_unwatch_pid(u
, s
->control_pid
);
90 free(s
->bind_to_device
);
92 unit_unwatch_timer(u
, &s
->timer_watch
);
95 static int socket_init(Unit
*u
) {
96 Socket
*s
= SOCKET(u
);
100 /* First, reset everything to the defaults, in case this is a
104 s
->timer_watch
.type
= WATCH_INVALID
;
105 s
->bind_ipv6_only
= false;
106 s
->backlog
= SOMAXCONN
;
107 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
108 s
->directory_mode
= 0755;
109 s
->socket_mode
= 0666;
110 exec_context_init(&s
->exec_context
);
112 if ((r
= unit_load_fragment_and_dropin(u
)) <= 0) {
118 if (!(t
= unit_name_change_suffix(unit_id(u
), ".service"))) {
123 r
= manager_load_unit(u
->meta
.manager
, t
, (Unit
**) &s
->service
);
129 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(s
->service
))) < 0)
139 static const char* listen_lookup(int type
) {
141 if (type
== SOCK_STREAM
)
142 return "ListenStream";
143 else if (type
== SOCK_DGRAM
)
144 return "ListenDatagram";
145 else if (type
== SOCK_SEQPACKET
)
146 return "ListenSequentialPacket";
148 assert_not_reached("Unknown socket type");
152 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
154 static const char* const command_table
[_SOCKET_EXEC_MAX
] = {
155 [SOCKET_EXEC_START_PRE
] = "StartPre",
156 [SOCKET_EXEC_START_POST
] = "StartPost",
157 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
158 [SOCKET_EXEC_STOP_POST
] = "StopPost"
162 Socket
*s
= SOCKET(u
);
170 p2
= strappend(prefix
, "\t");
171 prefix2
= p2
? p2
: prefix
;
174 "%sSocket State: %s\n"
175 "%sBindIPv6Only: %s\n"
177 "%sSocketMode: %04o\n"
178 "%sDirectoryMode: %04o\n",
179 prefix
, state_string_table
[s
->state
],
180 prefix
, yes_no(s
->bind_ipv6_only
),
182 prefix
, s
->socket_mode
,
183 prefix
, s
->directory_mode
);
185 if (s
->bind_to_device
)
187 "%sBindToDevice: %s\n",
188 prefix
, s
->bind_to_device
);
190 LIST_FOREACH(port
, p
, s
->ports
) {
192 if (p
->type
== SOCKET_SOCKET
) {
197 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
202 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(p
->address
.type
), k
);
205 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
208 exec_context_dump(&s
->exec_context
, f
, prefix
);
210 for (c
= 0; c
< _SOCKET_EXEC_MAX
; c
++) {
211 if (!s
->exec_command
[c
])
214 fprintf(f
, "%s→ %s:\n",
215 prefix
, command_table
[c
]);
217 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
223 static void socket_close_fds(Socket
*s
) {
228 LIST_FOREACH(port
, p
, s
->ports
) {
232 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
233 assert_se(close_nointr(p
->fd
) >= 0);
239 static int socket_open_fds(Socket
*s
) {
245 LIST_FOREACH(port
, p
, s
->ports
) {
250 if (p
->type
== SOCKET_SOCKET
) {
252 if ((r
= socket_address_listen(
264 assert(p
->type
== SOCKET_FIFO
);
266 if (mkfifo(p
->path
, 0666 & ~s
->exec_context
.umask
) < 0 && errno
!= EEXIST
) {
271 if ((p
->fd
= open(p
->path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
276 if (fstat(p
->fd
, &st
) < 0) {
281 /* FIXME verify user, access mode */
283 if (!S_ISFIFO(st
.st_mode
)) {
297 static void socket_unwatch_fds(Socket
*s
) {
302 LIST_FOREACH(port
, p
, s
->ports
) {
306 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
310 static int socket_watch_fds(Socket
*s
) {
316 LIST_FOREACH(port
, p
, s
->ports
) {
320 if ((r
= unit_watch_fd(UNIT(s
), p
->fd
, EPOLLIN
, &p
->fd_watch
)) < 0)
327 socket_unwatch_fds(s
);
331 static void socket_set_state(Socket
*s
, SocketState state
) {
332 SocketState old_state
;
335 old_state
= s
->state
;
338 if (state
!= SOCKET_START_PRE
&&
339 state
!= SOCKET_START_POST
&&
340 state
!= SOCKET_STOP_PRE
&&
341 state
!= SOCKET_STOP_PRE_SIGTERM
&&
342 state
!= SOCKET_STOP_PRE_SIGKILL
&&
343 state
!= SOCKET_STOP_POST
&&
344 state
!= SOCKET_STOP_POST_SIGTERM
&&
345 state
!= SOCKET_STOP_POST_SIGKILL
)
346 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
348 if (state
!= SOCKET_START_PRE
&&
349 state
!= SOCKET_START_POST
&&
350 state
!= SOCKET_STOP_PRE
&&
351 state
!= SOCKET_STOP_PRE_SIGTERM
&&
352 state
!= SOCKET_STOP_PRE_SIGKILL
&&
353 state
!= SOCKET_STOP_POST
&&
354 state
!= SOCKET_STOP_POST_SIGTERM
&&
355 state
!= SOCKET_STOP_POST_SIGKILL
)
356 if (s
->control_pid
> 0) {
357 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
361 if (state
!= SOCKET_START_PRE
&&
362 state
!= SOCKET_START_POST
&&
363 state
!= SOCKET_STOP_PRE
&&
364 state
!= SOCKET_STOP_POST
)
365 s
->control_command
= NULL
;
367 if (state
!= SOCKET_START_POST
&&
368 state
!= SOCKET_LISTENING
&&
369 state
!= SOCKET_RUNNING
&&
370 state
!= SOCKET_STOP_PRE
&&
371 state
!= SOCKET_STOP_PRE_SIGTERM
&&
372 state
!= SOCKET_STOP_PRE_SIGKILL
)
375 if (state
!= SOCKET_LISTENING
)
376 socket_unwatch_fds(s
);
378 log_debug("%s changed %s → %s", unit_id(UNIT(s
)), state_string_table
[old_state
], state_string_table
[state
]);
380 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
383 static int socket_spawn(Socket
*s
, ExecCommand
*c
, bool timeout
, pid_t
*_pid
) {
392 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
395 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
397 if ((r
= exec_spawn(c
, &s
->exec_context
, NULL
, 0, true, true, &pid
)) < 0)
400 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
401 /* FIXME: we need to do something here */
410 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
415 static void socket_enter_dead(Socket
*s
, bool success
) {
421 socket_set_state(s
, s
->failure
? SOCKET_MAINTAINANCE
: SOCKET_DEAD
);
424 static void socket_enter_stop_post(Socket
*s
, bool success
) {
431 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
]))
432 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
435 socket_set_state(s
, SOCKET_STOP_POST
);
437 if (!s
->control_command
)
438 socket_enter_dead(s
, true);
443 log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
444 socket_enter_dead(s
, false);
447 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
) {
455 if (s
->control_pid
> 0) {
458 sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_POST_SIGTERM
) ? SIGTERM
: SIGKILL
;
460 if (kill(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
466 socket_set_state(s
, state
);
468 if (s
->control_pid
<= 0)
469 socket_enter_dead(s
, true);
474 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s
)), strerror(-r
));
476 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
477 socket_enter_stop_post(s
, false);
479 socket_enter_dead(s
, false);
482 static void socket_enter_stop_pre(Socket
*s
, bool success
) {
489 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
]))
490 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
493 socket_set_state(s
, SOCKET_STOP_PRE
);
495 if (!s
->control_command
)
496 socket_enter_stop_post(s
, true);
501 log_warning("%s failed to run stop-pre executable: %s", unit_id(UNIT(s
)), strerror(-r
));
502 socket_enter_stop_post(s
, false);
505 static void socket_enter_listening(Socket
*s
) {
509 if ((r
= socket_watch_fds(s
)) < 0) {
510 log_warning("%s failed to watch sockets: %s", unit_id(UNIT(s
)), strerror(-r
));
514 socket_set_state(s
, SOCKET_LISTENING
);
518 socket_enter_stop_pre(s
, false);
521 static void socket_enter_start_post(Socket
*s
) {
525 if ((r
= socket_open_fds(s
)) < 0) {
526 log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s
)), strerror(-r
));
530 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
]))
531 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0) {
532 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
536 socket_set_state(s
, SOCKET_START_POST
);
538 if (!s
->control_command
)
539 socket_enter_listening(s
);
544 socket_enter_stop_pre(s
, false);
547 static void socket_enter_start_pre(Socket
*s
) {
551 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
]))
552 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
555 socket_set_state(s
, SOCKET_START_PRE
);
557 if (!s
->control_command
)
558 socket_enter_start_post(s
);
563 log_warning("%s failed to run start-pre exectuable: %s", unit_id(UNIT(s
)), strerror(-r
));
564 socket_enter_dead(s
, false);
567 static void socket_enter_running(Socket
*s
) {
572 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
->service
), JOB_REPLACE
, true, NULL
)) < 0)
575 socket_set_state(s
, SOCKET_RUNNING
);
579 log_warning("%s failed to queue socket startup job: %s", unit_id(UNIT(s
)), strerror(-r
));
580 socket_enter_dead(s
, false);
583 static void socket_run_next(Socket
*s
, bool success
) {
587 assert(s
->control_command
);
588 assert(s
->control_command
->command_next
);
593 s
->control_command
= s
->control_command
->command_next
;
595 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
601 if (s
->state
== SOCKET_STOP_PRE
)
602 socket_enter_stop_post(s
, false);
603 else if (s
->state
== SOCKET_STOP_POST
)
604 socket_enter_dead(s
, false);
606 socket_enter_stop_pre(s
, false);
609 static int socket_start(Unit
*u
) {
610 Socket
*s
= SOCKET(u
);
614 /* We cannot fulfill this request right now, try again later
616 if (s
->state
== SOCKET_STOP_PRE
||
617 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
618 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
619 s
->state
== SOCKET_STOP_POST
||
620 s
->state
== SOCKET_STOP_POST_SIGTERM
||
621 s
->state
== SOCKET_STOP_POST_SIGKILL
)
624 if (s
->state
== SOCKET_START_PRE
||
625 s
->state
== SOCKET_START_POST
)
628 /* Cannot run this without the service being around */
629 if (s
->service
->meta
.load_state
!= UNIT_LOADED
)
632 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_MAINTAINANCE
);
635 socket_enter_start_pre(s
);
639 static int socket_stop(Unit
*u
) {
640 Socket
*s
= SOCKET(u
);
644 /* We cannot fulfill this request right now, try again later
646 if (s
->state
== SOCKET_START_PRE
||
647 s
->state
== SOCKET_START_POST
)
650 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
652 socket_enter_stop_pre(s
, true);
656 static UnitActiveState
socket_active_state(Unit
*u
) {
659 return state_translation_table
[SOCKET(u
)->state
];
662 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
663 Socket
*s
= SOCKET(u
);
667 log_debug("Incoming traffic on %s", unit_id(u
));
669 if (events
!= EPOLLIN
)
670 socket_enter_stop_pre(s
, false);
672 socket_enter_running(s
);
675 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
676 Socket
*s
= SOCKET(u
);
682 success
= code
== CLD_EXITED
&& status
== 0;
683 s
->failure
= s
->failure
|| !success
;
685 assert(s
->control_pid
== pid
);
686 assert(s
->control_command
);
688 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
691 log_debug("%s control process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
693 if (s
->control_command
->command_next
&&
694 (success
|| (s
->state
== SOCKET_EXEC_STOP_PRE
|| s
->state
== SOCKET_EXEC_STOP_POST
))) {
695 log_debug("%s running next command for the state %s", unit_id(u
), state_string_table
[s
->state
]);
696 socket_run_next(s
, success
);
698 /* No further commands for this step, so let's figure
699 * out what to do next */
701 log_debug("%s got final SIGCHLD for state %s", unit_id(u
), state_string_table
[s
->state
]);
705 case SOCKET_START_PRE
:
707 socket_enter_start_post(s
);
709 socket_enter_stop_pre(s
, false);
712 case SOCKET_START_POST
:
714 socket_enter_listening(s
);
716 socket_enter_stop_pre(s
, false);
719 case SOCKET_STOP_PRE
:
720 case SOCKET_STOP_PRE_SIGTERM
:
721 case SOCKET_STOP_PRE_SIGKILL
:
722 socket_enter_stop_post(s
, success
);
725 case SOCKET_STOP_POST
:
726 case SOCKET_STOP_POST_SIGTERM
:
727 case SOCKET_STOP_POST_SIGKILL
:
728 socket_enter_dead(s
, success
);
732 assert_not_reached("Uh, control process died at wrong time.");
737 static void socket_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
738 Socket
*s
= SOCKET(u
);
741 assert(elapsed
== 1);
743 assert(w
== &s
->timer_watch
);
747 case SOCKET_START_PRE
:
748 case SOCKET_START_POST
:
749 log_warning("%s operation timed out. Stopping.", unit_id(u
));
750 socket_enter_stop_pre(s
, false);
753 case SOCKET_STOP_PRE
:
754 log_warning("%s stopping timed out. Terminating.", unit_id(u
));
755 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, false);
758 case SOCKET_STOP_PRE_SIGTERM
:
759 log_warning("%s stopping timed out. Killing.", unit_id(u
));
760 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, false);
763 case SOCKET_STOP_PRE_SIGKILL
:
764 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u
));
765 socket_enter_stop_post(s
, false);
768 case SOCKET_STOP_POST
:
769 log_warning("%s stopping timed out (2). Terminating.", unit_id(u
));
770 socket_enter_signal(s
, SOCKET_STOP_POST_SIGTERM
, false);
773 case SOCKET_STOP_POST_SIGTERM
:
774 log_warning("%s stopping timed out (2). Killing.", unit_id(u
));
775 socket_enter_signal(s
, SOCKET_STOP_POST_SIGKILL
, false);
778 case SOCKET_STOP_POST_SIGKILL
:
779 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u
));
780 socket_enter_dead(s
, false);
784 assert_not_reached("Timeout at wrong time.");
788 int socket_collect_fds(Socket
*s
, int **fds
, unsigned *n_fds
) {
797 /* Called from the service code for requesting our fds */
800 LIST_FOREACH(port
, p
, s
->ports
)
804 if (!(rfds
= new(int, rn_fds
)) < 0)
808 LIST_FOREACH(port
, p
, s
->ports
)
820 void socket_notify_service_dead(Socket
*s
) {
823 /* The service is dead. Dang. */
825 if (s
->state
== SOCKET_RUNNING
) {
826 log_debug("%s got notified about service death.", unit_id(UNIT(s
)));
827 socket_enter_listening(s
);
831 const UnitVTable socket_vtable
= {
839 .start
= socket_start
,
842 .active_state
= socket_active_state
,
844 .fd_event
= socket_fd_event
,
845 .sigchld_event
= socket_sigchld_event
,
846 .timer_event
= socket_timer_event