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>
33 #include "load-dropin.h"
34 #include "load-fragment.h"
36 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
37 [SOCKET_DEAD
] = UNIT_INACTIVE
,
38 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
39 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
40 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
41 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
42 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
43 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
44 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
45 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
46 [SOCKET_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
47 [SOCKET_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
48 [SOCKET_MAINTAINANCE
] = UNIT_INACTIVE
,
51 static const char* const state_string_table
[_SOCKET_STATE_MAX
] = {
52 [SOCKET_DEAD
] = "dead",
53 [SOCKET_START_PRE
] = "start-pre",
54 [SOCKET_START_POST
] = "start-post",
55 [SOCKET_LISTENING
] = "listening",
56 [SOCKET_RUNNING
] = "running",
57 [SOCKET_STOP_PRE
] = "stop-pre",
58 [SOCKET_STOP_PRE_SIGTERM
] = "stop-pre-sigterm",
59 [SOCKET_STOP_PRE_SIGKILL
] = "stop-pre-sigkill",
60 [SOCKET_STOP_POST
] = "stop-post",
61 [SOCKET_FINAL_SIGTERM
] = "final-sigterm",
62 [SOCKET_FINAL_SIGKILL
] = "final-sigkill",
63 [SOCKET_MAINTAINANCE
] = "maintainance"
66 static void socket_unwatch_control_pid(Socket
*s
) {
69 if (s
->control_pid
<= 0)
72 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
76 static void socket_done(Unit
*u
) {
77 Socket
*s
= SOCKET(u
);
82 while ((p
= s
->ports
)) {
83 LIST_REMOVE(SocketPort
, port
, s
->ports
, p
);
91 exec_context_done(&s
->exec_context
);
92 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_COMMAND_MAX
);
93 s
->control_command
= NULL
;
95 socket_unwatch_control_pid(s
);
99 free(s
->bind_to_device
);
100 s
->bind_to_device
= NULL
;
102 unit_unwatch_timer(u
, &s
->timer_watch
);
105 static void socket_init(Unit
*u
) {
106 Socket
*s
= SOCKET(u
);
109 assert(u
->meta
.load_state
== UNIT_STUB
);
112 s
->timer_watch
.type
= WATCH_INVALID
;
113 s
->bind_ipv6_only
= false;
114 s
->backlog
= SOMAXCONN
;
115 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
116 s
->directory_mode
= 0755;
117 s
->socket_mode
= 0666;
122 exec_context_init(&s
->exec_context
);
125 static int socket_load(Unit
*u
) {
126 Socket
*s
= SOCKET(u
);
130 assert(u
->meta
.load_state
== UNIT_STUB
);
132 if ((r
= unit_load_fragment_and_dropin(u
)) < 0)
135 /* This is a new unit? Then let's add in some extras */
136 if (u
->meta
.load_state
== UNIT_LOADED
) {
138 if ((r
= unit_load_related_unit(u
, ".service", (Unit
**) &s
->service
)))
141 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(s
->service
))) < 0)
144 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
147 if ((r
= unit_add_default_cgroup(u
)) < 0)
154 static const char* listen_lookup(int type
) {
156 if (type
== SOCK_STREAM
)
157 return "ListenStream";
158 else if (type
== SOCK_DGRAM
)
159 return "ListenDatagram";
160 else if (type
== SOCK_SEQPACKET
)
161 return "ListenSequentialPacket";
163 assert_not_reached("Unknown socket type");
167 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
169 static const char* const command_table
[_SOCKET_EXEC_COMMAND_MAX
] = {
170 [SOCKET_EXEC_START_PRE
] = "StartPre",
171 [SOCKET_EXEC_START_POST
] = "StartPost",
172 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
173 [SOCKET_EXEC_STOP_POST
] = "StopPost"
177 Socket
*s
= SOCKET(u
);
185 p2
= strappend(prefix
, "\t");
186 prefix2
= p2
? p2
: prefix
;
189 "%sSocket State: %s\n"
190 "%sBindIPv6Only: %s\n"
193 "%sSocketMode: %04o\n"
194 "%sDirectoryMode: %04o\n",
195 prefix
, state_string_table
[s
->state
],
196 prefix
, yes_no(s
->bind_ipv6_only
),
198 prefix
, kill_mode_to_string(s
->kill_mode
),
199 prefix
, s
->socket_mode
,
200 prefix
, s
->directory_mode
);
202 if (s
->control_pid
> 0)
204 "%sControl PID: %llu\n",
205 prefix
, (unsigned long long) s
->control_pid
);
207 if (s
->bind_to_device
)
209 "%sBindToDevice: %s\n",
210 prefix
, s
->bind_to_device
);
212 LIST_FOREACH(port
, p
, s
->ports
) {
214 if (p
->type
== SOCKET_SOCKET
) {
219 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
224 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(p
->address
.type
), k
);
227 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
230 exec_context_dump(&s
->exec_context
, f
, prefix
);
232 for (c
= 0; c
< _SOCKET_EXEC_COMMAND_MAX
; c
++) {
233 if (!s
->exec_command
[c
])
236 fprintf(f
, "%s→ %s:\n",
237 prefix
, command_table
[c
]);
239 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
245 static void socket_close_fds(Socket
*s
) {
250 LIST_FOREACH(port
, p
, s
->ports
) {
254 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
255 assert_se(close_nointr(p
->fd
) >= 0);
261 static int socket_open_fds(Socket
*s
) {
267 LIST_FOREACH(port
, p
, s
->ports
) {
272 if (p
->type
== SOCKET_SOCKET
) {
274 if ((r
= socket_address_listen(
286 assert(p
->type
== SOCKET_FIFO
);
288 mkdir_parents(p
->path
, s
->directory_mode
);
290 if (mkfifo(p
->path
, s
->socket_mode
) < 0 && errno
!= EEXIST
) {
295 if ((p
->fd
= open(p
->path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
300 if (fstat(p
->fd
, &st
) < 0) {
305 /* FIXME verify user, access mode */
307 if (!S_ISFIFO(st
.st_mode
)) {
321 static void socket_unwatch_fds(Socket
*s
) {
326 LIST_FOREACH(port
, p
, s
->ports
) {
330 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
334 static int socket_watch_fds(Socket
*s
) {
340 LIST_FOREACH(port
, p
, s
->ports
) {
344 if ((r
= unit_watch_fd(UNIT(s
), p
->fd
, EPOLLIN
, &p
->fd_watch
)) < 0)
351 socket_unwatch_fds(s
);
355 static void socket_set_state(Socket
*s
, SocketState state
) {
356 SocketState old_state
;
359 old_state
= s
->state
;
362 if (state
!= SOCKET_START_PRE
&&
363 state
!= SOCKET_START_POST
&&
364 state
!= SOCKET_STOP_PRE
&&
365 state
!= SOCKET_STOP_PRE_SIGTERM
&&
366 state
!= SOCKET_STOP_PRE_SIGKILL
&&
367 state
!= SOCKET_STOP_POST
&&
368 state
!= SOCKET_FINAL_SIGTERM
&&
369 state
!= SOCKET_FINAL_SIGKILL
) {
370 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
371 socket_unwatch_control_pid(s
);
372 s
->control_command
= NULL
;
375 if (state
!= SOCKET_START_POST
&&
376 state
!= SOCKET_LISTENING
&&
377 state
!= SOCKET_RUNNING
&&
378 state
!= SOCKET_STOP_PRE
&&
379 state
!= SOCKET_STOP_PRE_SIGTERM
&&
380 state
!= SOCKET_STOP_PRE_SIGKILL
)
383 if (state
!= SOCKET_LISTENING
)
384 socket_unwatch_fds(s
);
386 if (state
!= old_state
)
387 log_debug("%s changed %s → %s", unit_id(UNIT(s
)), state_string_table
[old_state
], state_string_table
[state
]);
389 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
392 static int socket_spawn(Socket
*s
, ExecCommand
*c
, pid_t
*_pid
) {
400 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
403 if ((r
= exec_spawn(c
,
408 UNIT(s
)->meta
.manager
->confirm_spawn
,
409 UNIT(s
)->meta
.cgroup_bondings
,
413 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
414 /* FIXME: we need to do something here */
422 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
427 static void socket_enter_dead(Socket
*s
, bool success
) {
433 socket_set_state(s
, s
->failure
? SOCKET_MAINTAINANCE
: SOCKET_DEAD
);
436 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
);
438 static void socket_enter_stop_post(Socket
*s
, bool success
) {
445 socket_unwatch_control_pid(s
);
447 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
])) {
448 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
451 socket_set_state(s
, SOCKET_STOP_POST
);
453 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, true);
458 log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
459 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
462 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
) {
471 if (s
->kill_mode
!= KILL_NONE
) {
472 int sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_FINAL_SIGTERM
) ? SIGTERM
: SIGKILL
;
474 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
476 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
477 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
483 if (!sent
&& s
->control_pid
> 0)
484 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
491 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
494 socket_set_state(s
, state
);
495 } else if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
496 socket_enter_stop_post(s
, true);
498 socket_enter_dead(s
, true);
503 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s
)), strerror(-r
));
505 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
506 socket_enter_stop_post(s
, false);
508 socket_enter_dead(s
, false);
511 static void socket_enter_stop_pre(Socket
*s
, bool success
) {
518 socket_unwatch_control_pid(s
);
520 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
])) {
521 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
524 socket_set_state(s
, SOCKET_STOP_PRE
);
526 socket_enter_stop_post(s
, true);
531 log_warning("%s failed to run stop-pre executable: %s", unit_id(UNIT(s
)), strerror(-r
));
532 socket_enter_stop_post(s
, false);
535 static void socket_enter_listening(Socket
*s
) {
539 if ((r
= socket_watch_fds(s
)) < 0) {
540 log_warning("%s failed to watch sockets: %s", unit_id(UNIT(s
)), strerror(-r
));
544 socket_set_state(s
, SOCKET_LISTENING
);
548 socket_enter_stop_pre(s
, false);
551 static void socket_enter_start_post(Socket
*s
) {
555 if ((r
= socket_open_fds(s
)) < 0) {
556 log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s
)), strerror(-r
));
560 socket_unwatch_control_pid(s
);
562 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
])) {
563 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0) {
564 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
568 socket_set_state(s
, SOCKET_START_POST
);
570 socket_enter_listening(s
);
575 socket_enter_stop_pre(s
, false);
578 static void socket_enter_start_pre(Socket
*s
) {
582 socket_unwatch_control_pid(s
);
584 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
])) {
585 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
588 socket_set_state(s
, SOCKET_START_PRE
);
590 socket_enter_start_post(s
);
595 log_warning("%s failed to run start-pre exectuable: %s", unit_id(UNIT(s
)), strerror(-r
));
596 socket_enter_dead(s
, false);
599 static void socket_enter_running(Socket
*s
) {
604 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
->service
), JOB_REPLACE
, true, NULL
)) < 0)
607 socket_set_state(s
, SOCKET_RUNNING
);
611 log_warning("%s failed to queue socket startup job: %s", unit_id(UNIT(s
)), strerror(-r
));
612 socket_enter_stop_pre(s
, false);
615 static void socket_run_next(Socket
*s
, bool success
) {
619 assert(s
->control_command
);
620 assert(s
->control_command
->command_next
);
625 socket_unwatch_control_pid(s
);
627 s
->control_command
= s
->control_command
->command_next
;
629 if ((r
= socket_spawn(s
, s
->control_command
, &s
->control_pid
)) < 0)
635 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s
)), strerror(-r
));
637 if (s
->state
== SOCKET_START_POST
)
638 socket_enter_stop_pre(s
, false);
639 else if (s
->state
== SOCKET_STOP_POST
)
640 socket_enter_dead(s
, false);
642 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
645 static int socket_start(Unit
*u
) {
646 Socket
*s
= SOCKET(u
);
650 /* We cannot fulfill this request right now, try again later
652 if (s
->state
== SOCKET_STOP_PRE
||
653 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
654 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
655 s
->state
== SOCKET_STOP_POST
||
656 s
->state
== SOCKET_FINAL_SIGTERM
||
657 s
->state
== SOCKET_FINAL_SIGKILL
)
660 if (s
->state
== SOCKET_START_PRE
||
661 s
->state
== SOCKET_START_POST
)
664 /* Cannot run this without the service being around */
665 if (s
->service
->meta
.load_state
!= UNIT_LOADED
)
668 /* If the service is alredy actvie we cannot start the
670 if (s
->service
->state
!= SERVICE_DEAD
&&
671 s
->service
->state
!= SERVICE_MAINTAINANCE
&&
672 s
->service
->state
!= SERVICE_AUTO_RESTART
)
675 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_MAINTAINANCE
);
678 socket_enter_start_pre(s
);
682 static int socket_stop(Unit
*u
) {
683 Socket
*s
= SOCKET(u
);
687 /* We cannot fulfill this request right now, try again later
689 if (s
->state
== SOCKET_START_PRE
||
690 s
->state
== SOCKET_START_POST
)
694 if (s
->state
== SOCKET_STOP_PRE
||
695 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
696 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
697 s
->state
== SOCKET_STOP_POST
||
698 s
->state
== SOCKET_FINAL_SIGTERM
||
699 s
->state
== SOCKET_FINAL_SIGTERM
)
702 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
704 socket_enter_stop_pre(s
, true);
708 static UnitActiveState
socket_active_state(Unit
*u
) {
711 return state_translation_table
[SOCKET(u
)->state
];
714 static const char *socket_sub_state_to_string(Unit
*u
) {
717 return state_string_table
[SOCKET(u
)->state
];
720 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
721 Socket
*s
= SOCKET(u
);
725 log_debug("Incoming traffic on %s", unit_id(u
));
727 if (events
!= EPOLLIN
)
728 socket_enter_stop_pre(s
, false);
730 socket_enter_running(s
);
733 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
734 Socket
*s
= SOCKET(u
);
740 success
= code
== CLD_EXITED
&& status
== 0;
741 s
->failure
= s
->failure
|| !success
;
743 assert(s
->control_pid
== pid
);
744 assert(s
->control_command
);
746 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
749 log_debug("%s control process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
751 if (s
->control_command
->command_next
&& success
) {
752 log_debug("%s running next command for state %s", unit_id(u
), state_string_table
[s
->state
]);
753 socket_run_next(s
, success
);
755 /* No further commands for this step, so let's figure
756 * out what to do next */
758 log_debug("%s got final SIGCHLD for state %s", unit_id(u
), state_string_table
[s
->state
]);
762 case SOCKET_START_PRE
:
764 socket_enter_start_post(s
);
766 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
769 case SOCKET_START_POST
:
771 socket_enter_listening(s
);
773 socket_enter_stop_pre(s
, false);
776 case SOCKET_STOP_PRE
:
777 case SOCKET_STOP_PRE_SIGTERM
:
778 case SOCKET_STOP_PRE_SIGKILL
:
779 socket_enter_stop_post(s
, success
);
782 case SOCKET_STOP_POST
:
783 case SOCKET_FINAL_SIGTERM
:
784 case SOCKET_FINAL_SIGKILL
:
785 socket_enter_dead(s
, success
);
789 assert_not_reached("Uh, control process died at wrong time.");
794 static void socket_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
795 Socket
*s
= SOCKET(u
);
798 assert(elapsed
== 1);
799 assert(w
== &s
->timer_watch
);
803 case SOCKET_START_PRE
:
804 log_warning("%s starting timed out. Terminating.", unit_id(u
));
805 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
807 case SOCKET_START_POST
:
808 log_warning("%s starting timed out. Stopping.", unit_id(u
));
809 socket_enter_stop_pre(s
, false);
812 case SOCKET_STOP_PRE
:
813 log_warning("%s stopping timed out. Terminating.", unit_id(u
));
814 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, false);
817 case SOCKET_STOP_PRE_SIGTERM
:
818 log_warning("%s stopping timed out. Killing.", unit_id(u
));
819 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, false);
822 case SOCKET_STOP_PRE_SIGKILL
:
823 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u
));
824 socket_enter_stop_post(s
, false);
827 case SOCKET_STOP_POST
:
828 log_warning("%s stopping timed out (2). Terminating.", unit_id(u
));
829 socket_enter_signal(s
, SOCKET_FINAL_SIGTERM
, false);
832 case SOCKET_FINAL_SIGTERM
:
833 log_warning("%s stopping timed out (2). Killing.", unit_id(u
));
834 socket_enter_signal(s
, SOCKET_FINAL_SIGKILL
, false);
837 case SOCKET_FINAL_SIGKILL
:
838 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u
));
839 socket_enter_dead(s
, false);
843 assert_not_reached("Timeout at wrong time.");
847 int socket_collect_fds(Socket
*s
, int **fds
, unsigned *n_fds
) {
856 /* Called from the service code for requesting our fds */
859 LIST_FOREACH(port
, p
, s
->ports
)
863 if (!(rfds
= new(int, rn_fds
)) < 0)
867 LIST_FOREACH(port
, p
, s
->ports
)
879 void socket_notify_service_dead(Socket
*s
) {
882 /* The service is dead. Dang. */
884 if (s
->state
== SOCKET_RUNNING
) {
885 log_debug("%s got notified about service death.", unit_id(UNIT(s
)));
886 socket_enter_listening(s
);
890 const UnitVTable socket_vtable
= {
899 .start
= socket_start
,
902 .active_state
= socket_active_state
,
903 .sub_state_to_string
= socket_sub_state_to_string
,
905 .fd_event
= socket_fd_event
,
906 .sigchld_event
= socket_sigchld_event
,
907 .timer_event
= socket_timer_event