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 exec_context_init(&s
->exec_context
);
110 if ((r
= unit_load_fragment_and_dropin(u
)) <= 0) {
116 if (!(t
= unit_name_change_suffix(unit_id(u
), ".service"))) {
121 r
= manager_load_unit(u
->meta
.manager
, t
, (Unit
**) &s
->service
);
127 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(s
->service
))) < 0)
137 static const char* listen_lookup(int type
) {
139 if (type
== SOCK_STREAM
)
140 return "ListenStream";
141 else if (type
== SOCK_DGRAM
)
142 return "ListenDatagram";
143 else if (type
== SOCK_SEQPACKET
)
144 return "ListenSequentialPacket";
146 assert_not_reached("Unknown socket type");
150 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
152 static const char* const command_table
[_SOCKET_EXEC_MAX
] = {
153 [SOCKET_EXEC_START_PRE
] = "StartPre",
154 [SOCKET_EXEC_START_POST
] = "StartPost",
155 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
156 [SOCKET_EXEC_STOP_POST
] = "StopPost"
160 Socket
*s
= SOCKET(u
);
167 prefix2
= strappend(prefix
, "\t");
172 "%sSocket State: %s\n"
173 "%sBindIPv6Only: %s\n"
175 prefix
, state_string_table
[s
->state
],
176 prefix
, yes_no(s
->bind_ipv6_only
),
179 if (s
->bind_to_device
)
181 "%sBindToDevice: %s\n",
182 prefix
, s
->bind_to_device
);
184 LIST_FOREACH(port
, p
, s
->ports
) {
186 if (p
->type
== SOCKET_SOCKET
) {
191 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
196 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(p
->address
.type
), k
);
199 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
202 exec_context_dump(&s
->exec_context
, f
, prefix
);
204 for (c
= 0; c
< _SOCKET_EXEC_MAX
; c
++) {
205 if (!s
->exec_command
[c
])
208 fprintf(f
, "%s→ %s:\n",
209 prefix
, command_table
[c
]);
211 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
217 static void socket_close_fds(Socket
*s
) {
222 LIST_FOREACH(port
, p
, s
->ports
) {
226 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
227 assert_se(close_nointr(p
->fd
) >= 0);
233 static int socket_open_fds(Socket
*s
) {
239 LIST_FOREACH(port
, p
, s
->ports
) {
244 if (p
->type
== SOCKET_SOCKET
) {
246 if ((r
= socket_address_listen(&p
->address
, s
->backlog
, s
->bind_ipv6_only
, s
->bind_to_device
, &p
->fd
)) < 0)
251 assert(p
->type
== SOCKET_FIFO
);
253 if (mkfifo(p
->path
, 0666 & ~s
->exec_context
.umask
) < 0 && errno
!= EEXIST
) {
258 if ((p
->fd
= open(p
->path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
263 if (fstat(p
->fd
, &st
) < 0) {
268 /* FIXME verify user, access mode */
270 if (!S_ISFIFO(st
.st_mode
)) {
284 static void socket_unwatch_fds(Socket
*s
) {
289 LIST_FOREACH(port
, p
, s
->ports
) {
293 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
297 static int socket_watch_fds(Socket
*s
) {
303 LIST_FOREACH(port
, p
, s
->ports
) {
307 if ((r
= unit_watch_fd(UNIT(s
), p
->fd
, EPOLLIN
, &p
->fd_watch
)) < 0)
314 socket_unwatch_fds(s
);
318 static void socket_set_state(Socket
*s
, SocketState state
) {
319 SocketState old_state
;
322 old_state
= s
->state
;
325 if (state
!= SOCKET_START_PRE
&&
326 state
!= SOCKET_START_POST
&&
327 state
!= SOCKET_STOP_PRE
&&
328 state
!= SOCKET_STOP_PRE_SIGTERM
&&
329 state
!= SOCKET_STOP_PRE_SIGKILL
&&
330 state
!= SOCKET_STOP_POST
&&
331 state
!= SOCKET_STOP_POST_SIGTERM
&&
332 state
!= SOCKET_STOP_POST_SIGKILL
)
333 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
335 if (state
!= SOCKET_START_PRE
&&
336 state
!= SOCKET_START_POST
&&
337 state
!= SOCKET_STOP_PRE
&&
338 state
!= SOCKET_STOP_PRE_SIGTERM
&&
339 state
!= SOCKET_STOP_PRE_SIGKILL
&&
340 state
!= SOCKET_STOP_POST
&&
341 state
!= SOCKET_STOP_POST_SIGTERM
&&
342 state
!= SOCKET_STOP_POST_SIGKILL
)
343 if (s
->control_pid
> 0) {
344 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
348 if (state
!= SOCKET_START_PRE
&&
349 state
!= SOCKET_START_POST
&&
350 state
!= SOCKET_STOP_PRE
&&
351 state
!= SOCKET_STOP_POST
)
352 s
->control_command
= NULL
;
354 if (state
!= SOCKET_START_POST
&&
355 state
!= SOCKET_LISTENING
&&
356 state
!= SOCKET_RUNNING
&&
357 state
!= SOCKET_STOP_PRE
&&
358 state
!= SOCKET_STOP_PRE_SIGTERM
&&
359 state
!= SOCKET_STOP_PRE_SIGKILL
)
362 if (state
!= SOCKET_LISTENING
)
363 socket_unwatch_fds(s
);
365 log_debug("%s changed %s → %s", unit_id(UNIT(s
)), state_string_table
[old_state
], state_string_table
[state
]);
367 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
370 static int socket_spawn(Socket
*s
, ExecCommand
*c
, bool timeout
, pid_t
*_pid
) {
379 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
382 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
384 if ((r
= exec_spawn(c
, &s
->exec_context
, NULL
, 0, &pid
)) < 0)
387 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
388 /* FIXME: we need to do something here */
397 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
402 static void socket_enter_dead(Socket
*s
, bool success
) {
408 socket_set_state(s
, s
->failure
? SOCKET_MAINTAINANCE
: SOCKET_DEAD
);
411 static void socket_enter_stop_post(Socket
*s
, bool success
) {
418 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
]))
419 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
422 socket_set_state(s
, SOCKET_STOP_POST
);
424 if (!s
->control_command
)
425 socket_enter_dead(s
, true);
430 log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
431 socket_enter_dead(s
, false);
434 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
) {
442 if (s
->control_pid
> 0) {
445 sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_POST_SIGTERM
) ? SIGTERM
: SIGKILL
;
447 if (kill(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
453 socket_set_state(s
, state
);
455 if (s
->control_pid
<= 0)
456 socket_enter_dead(s
, true);
461 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s
)), strerror(-r
));
463 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
464 socket_enter_stop_post(s
, false);
466 socket_enter_dead(s
, false);
469 static void socket_enter_stop_pre(Socket
*s
, bool success
) {
476 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
]))
477 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
480 socket_set_state(s
, SOCKET_STOP_PRE
);
482 if (!s
->control_command
)
483 socket_enter_stop_post(s
, true);
488 log_warning("%s failed to run stop-pre executable: %s", unit_id(UNIT(s
)), strerror(-r
));
489 socket_enter_stop_post(s
, false);
492 static void socket_enter_listening(Socket
*s
) {
496 if ((r
= socket_watch_fds(s
)) < 0) {
497 log_warning("%s failed to watch sockets: %s", unit_id(UNIT(s
)), strerror(-r
));
501 socket_set_state(s
, SOCKET_LISTENING
);
505 socket_enter_stop_pre(s
, false);
508 static void socket_enter_start_post(Socket
*s
) {
512 if ((r
= socket_open_fds(s
)) < 0) {
513 log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s
)), strerror(-r
));
517 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
]))
518 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0) {
519 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
523 socket_set_state(s
, SOCKET_START_POST
);
525 if (!s
->control_command
)
526 socket_enter_listening(s
);
531 socket_enter_stop_pre(s
, false);
534 static void socket_enter_start_pre(Socket
*s
) {
538 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
]))
539 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
542 socket_set_state(s
, SOCKET_START_PRE
);
544 if (!s
->control_command
)
545 socket_enter_start_post(s
);
550 log_warning("%s failed to run start-pre exectuable: %s", unit_id(UNIT(s
)), strerror(-r
));
551 socket_enter_dead(s
, false);
554 static void socket_enter_running(Socket
*s
) {
559 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
->service
), JOB_REPLACE
, true, NULL
)) < 0)
562 socket_set_state(s
, SOCKET_RUNNING
);
566 log_warning("%s failed to queue socket startup job: %s", unit_id(UNIT(s
)), strerror(-r
));
567 socket_enter_dead(s
, false);
570 static void socket_run_next(Socket
*s
, bool success
) {
574 assert(s
->control_command
);
575 assert(s
->control_command
->command_next
);
580 s
->control_command
= s
->control_command
->command_next
;
582 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
588 if (s
->state
== SOCKET_STOP_PRE
)
589 socket_enter_stop_post(s
, false);
590 else if (s
->state
== SOCKET_STOP_POST
)
591 socket_enter_dead(s
, false);
593 socket_enter_stop_pre(s
, false);
596 static int socket_start(Unit
*u
) {
597 Socket
*s
= SOCKET(u
);
601 /* We cannot fulfill this request right now, try again later
603 if (s
->state
== SOCKET_STOP_PRE
||
604 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
605 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
606 s
->state
== SOCKET_STOP_POST
||
607 s
->state
== SOCKET_STOP_POST_SIGTERM
||
608 s
->state
== SOCKET_STOP_POST_SIGKILL
)
611 if (s
->state
== SOCKET_START_PRE
||
612 s
->state
== SOCKET_START_POST
)
615 /* Cannot run this without the service being around */
616 if (s
->service
->meta
.load_state
!= UNIT_LOADED
)
619 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_MAINTAINANCE
);
622 socket_enter_start_pre(s
);
626 static int socket_stop(Unit
*u
) {
627 Socket
*s
= SOCKET(u
);
631 /* We cannot fulfill this request right now, try again later
633 if (s
->state
== SOCKET_START_PRE
||
634 s
->state
== SOCKET_START_POST
)
637 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
639 socket_enter_stop_pre(s
, true);
643 static UnitActiveState
socket_active_state(Unit
*u
) {
646 return state_translation_table
[SOCKET(u
)->state
];
649 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
650 Socket
*s
= SOCKET(u
);
654 log_debug("Incoming traffic on %s", unit_id(u
));
656 if (events
!= EPOLLIN
)
657 socket_enter_stop_pre(s
, false);
659 socket_enter_running(s
);
662 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
663 Socket
*s
= SOCKET(u
);
669 success
= code
== CLD_EXITED
&& status
== 0;
670 s
->failure
= s
->failure
|| !success
;
672 assert(s
->control_pid
== pid
);
673 assert(s
->control_command
);
675 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
678 log_debug("%s control process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
680 if (s
->control_command
->command_next
&&
681 (success
|| (s
->state
== SOCKET_EXEC_STOP_PRE
|| s
->state
== SOCKET_EXEC_STOP_POST
))) {
682 log_debug("%s running next command for the state %s", unit_id(u
), state_string_table
[s
->state
]);
683 socket_run_next(s
, success
);
685 /* No further commands for this step, so let's figure
686 * out what to do next */
688 log_debug("%s got final SIGCHLD for state %s", unit_id(u
), state_string_table
[s
->state
]);
692 case SOCKET_START_PRE
:
694 socket_enter_start_post(s
);
696 socket_enter_stop_pre(s
, false);
699 case SOCKET_START_POST
:
701 socket_enter_listening(s
);
703 socket_enter_stop_pre(s
, false);
706 case SOCKET_STOP_PRE
:
707 case SOCKET_STOP_PRE_SIGTERM
:
708 case SOCKET_STOP_PRE_SIGKILL
:
709 socket_enter_stop_post(s
, success
);
712 case SOCKET_STOP_POST
:
713 case SOCKET_STOP_POST_SIGTERM
:
714 case SOCKET_STOP_POST_SIGKILL
:
715 socket_enter_dead(s
, success
);
719 assert_not_reached("Uh, control process died at wrong time.");
724 static void socket_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
725 Socket
*s
= SOCKET(u
);
728 assert(elapsed
== 1);
730 assert(w
== &s
->timer_watch
);
734 case SOCKET_START_PRE
:
735 case SOCKET_START_POST
:
736 log_warning("%s operation timed out. Stopping.", unit_id(u
));
737 socket_enter_stop_pre(s
, false);
740 case SOCKET_STOP_PRE
:
741 log_warning("%s stopping timed out. Terminating.", unit_id(u
));
742 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, false);
745 case SOCKET_STOP_PRE_SIGTERM
:
746 log_warning("%s stopping timed out. Killing.", unit_id(u
));
747 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, false);
750 case SOCKET_STOP_PRE_SIGKILL
:
751 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u
));
752 socket_enter_stop_post(s
, false);
755 case SOCKET_STOP_POST
:
756 log_warning("%s stopping timed out (2). Terminating.", unit_id(u
));
757 socket_enter_signal(s
, SOCKET_STOP_POST_SIGTERM
, false);
760 case SOCKET_STOP_POST_SIGTERM
:
761 log_warning("%s stopping timed out (2). Killing.", unit_id(u
));
762 socket_enter_signal(s
, SOCKET_STOP_POST_SIGKILL
, false);
765 case SOCKET_STOP_POST_SIGKILL
:
766 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u
));
767 socket_enter_dead(s
, false);
771 assert_not_reached("Timeout at wrong time.");
775 int socket_collect_fds(Socket
*s
, int **fds
, unsigned *n_fds
) {
784 /* Called from the service code for requesting our fds */
787 LIST_FOREACH(port
, p
, s
->ports
)
791 if (!(rfds
= new(int, rn_fds
)) < 0)
795 LIST_FOREACH(port
, p
, s
->ports
)
807 void socket_notify_service_dead(Socket
*s
) {
810 /* The service is dead. Dang. */
812 if (s
->state
== SOCKET_RUNNING
) {
813 log_debug("%s got notified about service death.", unit_id(UNIT(s
)));
814 socket_enter_listening(s
);
818 const UnitVTable socket_vtable
= {
826 .start
= socket_start
,
829 .active_state
= socket_active_state
,
831 .fd_event
= socket_fd_event
,
832 .sigchld_event
= socket_sigchld_event
,
833 .timer_event
= socket_timer_event