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_STOP_POST_SIGTERM
] = UNIT_DEACTIVATING
,
47 [SOCKET_STOP_POST_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_STOP_POST_SIGTERM
] = "stop-post-sigterm",
62 [SOCKET_STOP_POST_SIGKILL
] = "stop-post-sigkill",
63 [SOCKET_MAINTAINANCE
] = "maintainance"
66 static void socket_done(Unit
*u
) {
67 Socket
*s
= SOCKET(u
);
72 while ((p
= s
->ports
)) {
73 LIST_REMOVE(SocketPort
, port
, s
->ports
, p
);
81 exec_context_done(&s
->exec_context
);
82 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_MAX
);
83 s
->control_command
= NULL
;
85 if (s
->control_pid
> 0) {
86 unit_unwatch_pid(u
, s
->control_pid
);
92 free(s
->bind_to_device
);
94 unit_unwatch_timer(u
, &s
->timer_watch
);
97 static int socket_init(Unit
*u
, UnitLoadState
*new_state
) {
98 Socket
*s
= SOCKET(u
);
102 /* First, reset everything to the defaults, in case this is a
106 s
->timer_watch
.type
= WATCH_INVALID
;
107 s
->bind_ipv6_only
= false;
108 s
->backlog
= SOMAXCONN
;
109 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
110 s
->directory_mode
= 0755;
111 s
->socket_mode
= 0666;
115 exec_context_init(&s
->exec_context
);
117 if ((r
= unit_load_fragment(u
, new_state
)) < 0)
120 if (*new_state
== UNIT_STUB
)
123 if ((r
= unit_load_dropin(unit_follow_merge(u
))) < 0)
126 /* This is a new unit? Then let's add in some extras */
127 if (*new_state
== UNIT_LOADED
) {
129 if (!(t
= unit_name_change_suffix(unit_id(u
), ".service")))
132 r
= manager_load_unit(u
->meta
.manager
, t
, (Unit
**) &s
->service
);
138 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(s
->service
))) < 0)
141 if ((r
= unit_add_exec_dependencies(u
, &s
->exec_context
)) < 0)
144 if ((r
= unit_add_default_cgroup(u
)) < 0)
151 static const char* listen_lookup(int type
) {
153 if (type
== SOCK_STREAM
)
154 return "ListenStream";
155 else if (type
== SOCK_DGRAM
)
156 return "ListenDatagram";
157 else if (type
== SOCK_SEQPACKET
)
158 return "ListenSequentialPacket";
160 assert_not_reached("Unknown socket type");
164 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
166 static const char* const command_table
[_SOCKET_EXEC_MAX
] = {
167 [SOCKET_EXEC_START_PRE
] = "StartPre",
168 [SOCKET_EXEC_START_POST
] = "StartPost",
169 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
170 [SOCKET_EXEC_STOP_POST
] = "StopPost"
174 Socket
*s
= SOCKET(u
);
182 p2
= strappend(prefix
, "\t");
183 prefix2
= p2
? p2
: prefix
;
186 "%sSocket State: %s\n"
187 "%sBindIPv6Only: %s\n"
190 "%sSocketMode: %04o\n"
191 "%sDirectoryMode: %04o\n",
192 prefix
, state_string_table
[s
->state
],
193 prefix
, yes_no(s
->bind_ipv6_only
),
195 prefix
, kill_mode_to_string(s
->kill_mode
),
196 prefix
, s
->socket_mode
,
197 prefix
, s
->directory_mode
);
199 if (s
->control_pid
> 0)
201 "%sControl PID: %llu\n",
202 prefix
, (unsigned long long) s
->control_pid
);
204 if (s
->bind_to_device
)
206 "%sBindToDevice: %s\n",
207 prefix
, s
->bind_to_device
);
209 LIST_FOREACH(port
, p
, s
->ports
) {
211 if (p
->type
== SOCKET_SOCKET
) {
216 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
221 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(p
->address
.type
), k
);
224 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
227 exec_context_dump(&s
->exec_context
, f
, prefix
);
229 for (c
= 0; c
< _SOCKET_EXEC_MAX
; c
++) {
230 if (!s
->exec_command
[c
])
233 fprintf(f
, "%s→ %s:\n",
234 prefix
, command_table
[c
]);
236 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
242 static void socket_close_fds(Socket
*s
) {
247 LIST_FOREACH(port
, p
, s
->ports
) {
251 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
252 assert_se(close_nointr(p
->fd
) >= 0);
258 static int socket_open_fds(Socket
*s
) {
264 LIST_FOREACH(port
, p
, s
->ports
) {
269 if (p
->type
== SOCKET_SOCKET
) {
271 if ((r
= socket_address_listen(
283 assert(p
->type
== SOCKET_FIFO
);
285 mkdir_parents(p
->path
, s
->directory_mode
);
287 if (mkfifo(p
->path
, s
->socket_mode
) < 0 && errno
!= EEXIST
) {
292 if ((p
->fd
= open(p
->path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
297 if (fstat(p
->fd
, &st
) < 0) {
302 /* FIXME verify user, access mode */
304 if (!S_ISFIFO(st
.st_mode
)) {
318 static void socket_unwatch_fds(Socket
*s
) {
323 LIST_FOREACH(port
, p
, s
->ports
) {
327 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
331 static int socket_watch_fds(Socket
*s
) {
337 LIST_FOREACH(port
, p
, s
->ports
) {
341 if ((r
= unit_watch_fd(UNIT(s
), p
->fd
, EPOLLIN
, &p
->fd_watch
)) < 0)
348 socket_unwatch_fds(s
);
352 static void socket_set_state(Socket
*s
, SocketState state
) {
353 SocketState old_state
;
356 old_state
= s
->state
;
359 if (state
!= SOCKET_START_PRE
&&
360 state
!= SOCKET_START_POST
&&
361 state
!= SOCKET_STOP_PRE
&&
362 state
!= SOCKET_STOP_PRE_SIGTERM
&&
363 state
!= SOCKET_STOP_PRE_SIGKILL
&&
364 state
!= SOCKET_STOP_POST
&&
365 state
!= SOCKET_STOP_POST_SIGTERM
&&
366 state
!= SOCKET_STOP_POST_SIGKILL
)
367 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
369 if (state
!= SOCKET_START_PRE
&&
370 state
!= SOCKET_START_POST
&&
371 state
!= SOCKET_STOP_PRE
&&
372 state
!= SOCKET_STOP_PRE_SIGTERM
&&
373 state
!= SOCKET_STOP_PRE_SIGKILL
&&
374 state
!= SOCKET_STOP_POST
&&
375 state
!= SOCKET_STOP_POST_SIGTERM
&&
376 state
!= SOCKET_STOP_POST_SIGKILL
)
377 if (s
->control_pid
> 0) {
378 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
382 if (state
!= SOCKET_START_PRE
&&
383 state
!= SOCKET_START_POST
&&
384 state
!= SOCKET_STOP_PRE
&&
385 state
!= SOCKET_STOP_POST
)
386 s
->control_command
= NULL
;
388 if (state
!= SOCKET_START_POST
&&
389 state
!= SOCKET_LISTENING
&&
390 state
!= SOCKET_RUNNING
&&
391 state
!= SOCKET_STOP_PRE
&&
392 state
!= SOCKET_STOP_PRE_SIGTERM
&&
393 state
!= SOCKET_STOP_PRE_SIGKILL
)
396 if (state
!= SOCKET_LISTENING
)
397 socket_unwatch_fds(s
);
399 if (state
== old_state
)
402 log_debug("%s changed %s → %s", unit_id(UNIT(s
)), state_string_table
[old_state
], state_string_table
[state
]);
404 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
407 static int socket_spawn(Socket
*s
, ExecCommand
*c
, bool timeout
, pid_t
*_pid
) {
416 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
419 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
421 if ((r
= exec_spawn(c
,
426 UNIT(s
)->meta
.cgroup_bondings
,
430 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
431 /* FIXME: we need to do something here */
440 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
445 static void socket_enter_dead(Socket
*s
, bool success
) {
451 socket_set_state(s
, s
->failure
? SOCKET_MAINTAINANCE
: SOCKET_DEAD
);
454 static void socket_enter_stop_post(Socket
*s
, bool success
) {
461 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
]))
462 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
465 socket_set_state(s
, SOCKET_STOP_POST
);
467 if (!s
->control_command
)
468 socket_enter_dead(s
, true);
473 log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
474 socket_enter_dead(s
, false);
477 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
) {
485 if (s
->control_pid
> 0) {
489 sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_POST_SIGTERM
) ? SIGTERM
: SIGKILL
;
491 if (s
->kill_mode
== KILL_CONTROL_GROUP
) {
493 if ((r
= cgroup_bonding_kill_list(UNIT(s
)->meta
.cgroup_bondings
, sig
)) < 0) {
494 if (r
!= -EAGAIN
&& r
!= -ESRCH
)
501 if (kill(s
->kill_mode
== KILL_PROCESS
? s
->control_pid
: -s
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
508 socket_set_state(s
, state
);
510 if (s
->control_pid
<= 0)
511 socket_enter_dead(s
, true);
516 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s
)), strerror(-r
));
518 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
519 socket_enter_stop_post(s
, false);
521 socket_enter_dead(s
, false);
524 static void socket_enter_stop_pre(Socket
*s
, bool success
) {
531 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
]))
532 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
535 socket_set_state(s
, SOCKET_STOP_PRE
);
537 if (!s
->control_command
)
538 socket_enter_stop_post(s
, true);
543 log_warning("%s failed to run stop-pre executable: %s", unit_id(UNIT(s
)), strerror(-r
));
544 socket_enter_stop_post(s
, false);
547 static void socket_enter_listening(Socket
*s
) {
551 if ((r
= socket_watch_fds(s
)) < 0) {
552 log_warning("%s failed to watch sockets: %s", unit_id(UNIT(s
)), strerror(-r
));
556 socket_set_state(s
, SOCKET_LISTENING
);
560 socket_enter_stop_pre(s
, false);
563 static void socket_enter_start_post(Socket
*s
) {
567 if ((r
= socket_open_fds(s
)) < 0) {
568 log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s
)), strerror(-r
));
572 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
]))
573 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0) {
574 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
578 socket_set_state(s
, SOCKET_START_POST
);
580 if (!s
->control_command
)
581 socket_enter_listening(s
);
586 socket_enter_stop_pre(s
, false);
589 static void socket_enter_start_pre(Socket
*s
) {
593 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
]))
594 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
597 socket_set_state(s
, SOCKET_START_PRE
);
599 if (!s
->control_command
)
600 socket_enter_start_post(s
);
605 log_warning("%s failed to run start-pre exectuable: %s", unit_id(UNIT(s
)), strerror(-r
));
606 socket_enter_dead(s
, false);
609 static void socket_enter_running(Socket
*s
) {
614 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
->service
), JOB_REPLACE
, true, NULL
)) < 0)
617 socket_set_state(s
, SOCKET_RUNNING
);
621 log_warning("%s failed to queue socket startup job: %s", unit_id(UNIT(s
)), strerror(-r
));
622 socket_enter_dead(s
, false);
625 static void socket_run_next(Socket
*s
, bool success
) {
629 assert(s
->control_command
);
630 assert(s
->control_command
->command_next
);
635 s
->control_command
= s
->control_command
->command_next
;
637 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
643 if (s
->state
== SOCKET_STOP_PRE
)
644 socket_enter_stop_post(s
, false);
645 else if (s
->state
== SOCKET_STOP_POST
)
646 socket_enter_dead(s
, false);
648 socket_enter_stop_pre(s
, false);
651 static int socket_start(Unit
*u
) {
652 Socket
*s
= SOCKET(u
);
656 /* We cannot fulfill this request right now, try again later
658 if (s
->state
== SOCKET_STOP_PRE
||
659 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
660 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
661 s
->state
== SOCKET_STOP_POST
||
662 s
->state
== SOCKET_STOP_POST_SIGTERM
||
663 s
->state
== SOCKET_STOP_POST_SIGKILL
)
666 if (s
->state
== SOCKET_START_PRE
||
667 s
->state
== SOCKET_START_POST
)
670 /* Cannot run this without the service being around */
671 if (s
->service
->meta
.load_state
!= UNIT_LOADED
)
674 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_MAINTAINANCE
);
677 socket_enter_start_pre(s
);
681 static int socket_stop(Unit
*u
) {
682 Socket
*s
= SOCKET(u
);
686 /* We cannot fulfill this request right now, try again later
688 if (s
->state
== SOCKET_START_PRE
||
689 s
->state
== SOCKET_START_POST
)
692 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
694 socket_enter_stop_pre(s
, true);
698 static UnitActiveState
socket_active_state(Unit
*u
) {
701 return state_translation_table
[SOCKET(u
)->state
];
704 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
705 Socket
*s
= SOCKET(u
);
709 log_debug("Incoming traffic on %s", unit_id(u
));
711 if (events
!= EPOLLIN
)
712 socket_enter_stop_pre(s
, false);
714 socket_enter_running(s
);
717 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
718 Socket
*s
= SOCKET(u
);
724 success
= code
== CLD_EXITED
&& status
== 0;
725 s
->failure
= s
->failure
|| !success
;
727 assert(s
->control_pid
== pid
);
728 assert(s
->control_command
);
730 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
733 log_debug("%s control process exited, code=%s status=%i", unit_id(u
), sigchld_code_to_string(code
), status
);
735 if (s
->control_command
->command_next
&&
736 (success
|| (s
->state
== SOCKET_STOP_PRE
|| s
->state
== SOCKET_STOP_POST
))) {
737 log_debug("%s running next command for the state %s", unit_id(u
), state_string_table
[s
->state
]);
738 socket_run_next(s
, success
);
740 /* No further commands for this step, so let's figure
741 * out what to do next */
743 log_debug("%s got final SIGCHLD for state %s", unit_id(u
), state_string_table
[s
->state
]);
747 case SOCKET_START_PRE
:
749 socket_enter_start_post(s
);
751 socket_enter_stop_pre(s
, false);
754 case SOCKET_START_POST
:
756 socket_enter_listening(s
);
758 socket_enter_stop_pre(s
, false);
761 case SOCKET_STOP_PRE
:
762 case SOCKET_STOP_PRE_SIGTERM
:
763 case SOCKET_STOP_PRE_SIGKILL
:
764 socket_enter_stop_post(s
, success
);
767 case SOCKET_STOP_POST
:
768 case SOCKET_STOP_POST_SIGTERM
:
769 case SOCKET_STOP_POST_SIGKILL
:
770 socket_enter_dead(s
, success
);
774 assert_not_reached("Uh, control process died at wrong time.");
779 static void socket_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
780 Socket
*s
= SOCKET(u
);
783 assert(elapsed
== 1);
785 assert(w
== &s
->timer_watch
);
789 case SOCKET_START_PRE
:
790 case SOCKET_START_POST
:
791 log_warning("%s operation timed out. Stopping.", unit_id(u
));
792 socket_enter_stop_pre(s
, false);
795 case SOCKET_STOP_PRE
:
796 log_warning("%s stopping timed out. Terminating.", unit_id(u
));
797 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, false);
800 case SOCKET_STOP_PRE_SIGTERM
:
801 log_warning("%s stopping timed out. Killing.", unit_id(u
));
802 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, false);
805 case SOCKET_STOP_PRE_SIGKILL
:
806 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u
));
807 socket_enter_stop_post(s
, false);
810 case SOCKET_STOP_POST
:
811 log_warning("%s stopping timed out (2). Terminating.", unit_id(u
));
812 socket_enter_signal(s
, SOCKET_STOP_POST_SIGTERM
, false);
815 case SOCKET_STOP_POST_SIGTERM
:
816 log_warning("%s stopping timed out (2). Killing.", unit_id(u
));
817 socket_enter_signal(s
, SOCKET_STOP_POST_SIGKILL
, false);
820 case SOCKET_STOP_POST_SIGKILL
:
821 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u
));
822 socket_enter_dead(s
, false);
826 assert_not_reached("Timeout at wrong time.");
830 int socket_collect_fds(Socket
*s
, int **fds
, unsigned *n_fds
) {
839 /* Called from the service code for requesting our fds */
842 LIST_FOREACH(port
, p
, s
->ports
)
846 if (!(rfds
= new(int, rn_fds
)) < 0)
850 LIST_FOREACH(port
, p
, s
->ports
)
862 void socket_notify_service_dead(Socket
*s
) {
865 /* The service is dead. Dang. */
867 if (s
->state
== SOCKET_RUNNING
) {
868 log_debug("%s got notified about service death.", unit_id(UNIT(s
)));
869 socket_enter_listening(s
);
873 const UnitVTable socket_vtable
= {
881 .start
= socket_start
,
884 .active_state
= socket_active_state
,
886 .fd_event
= socket_fd_event
,
887 .sigchld_event
= socket_sigchld_event
,
888 .timer_event
= socket_timer_event