1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
15 static const UnitActiveState state_translation_table
[_SOCKET_STATE_MAX
] = {
16 [SOCKET_DEAD
] = UNIT_INACTIVE
,
17 [SOCKET_START_PRE
] = UNIT_ACTIVATING
,
18 [SOCKET_START_POST
] = UNIT_ACTIVATING
,
19 [SOCKET_LISTENING
] = UNIT_ACTIVE
,
20 [SOCKET_RUNNING
] = UNIT_ACTIVE
,
21 [SOCKET_STOP_PRE
] = UNIT_DEACTIVATING
,
22 [SOCKET_STOP_PRE_SIGTERM
] = UNIT_DEACTIVATING
,
23 [SOCKET_STOP_PRE_SIGKILL
] = UNIT_DEACTIVATING
,
24 [SOCKET_STOP_POST
] = UNIT_DEACTIVATING
,
25 [SOCKET_STOP_POST_SIGTERM
] = UNIT_DEACTIVATING
,
26 [SOCKET_STOP_POST_SIGKILL
] = UNIT_DEACTIVATING
,
27 [SOCKET_MAINTAINANCE
] = UNIT_INACTIVE
,
30 static const char* const state_string_table
[_SOCKET_STATE_MAX
] = {
31 [SOCKET_DEAD
] = "dead",
32 [SOCKET_START_PRE
] = "start-pre",
33 [SOCKET_START_POST
] = "start-post",
34 [SOCKET_LISTENING
] = "listening",
35 [SOCKET_RUNNING
] = "running",
36 [SOCKET_STOP_PRE
] = "stop-pre",
37 [SOCKET_STOP_PRE_SIGTERM
] = "stop-pre-sigterm",
38 [SOCKET_STOP_PRE_SIGKILL
] = "stop-pre-sigkill",
39 [SOCKET_STOP_POST
] = "stop-post",
40 [SOCKET_STOP_POST_SIGTERM
] = "stop-post-sigterm",
41 [SOCKET_STOP_POST_SIGKILL
] = "stop-post-sigkill",
42 [SOCKET_MAINTAINANCE
] = "maintainance"
45 static void socket_done(Unit
*u
) {
46 Socket
*s
= SOCKET(u
);
51 while ((p
= s
->ports
)) {
52 LIST_REMOVE(SocketPort
, port
, s
->ports
, p
);
60 exec_context_done(&s
->exec_context
);
61 exec_command_free_array(s
->exec_command
, _SOCKET_EXEC_MAX
);
62 s
->control_command
= NULL
;
64 if (s
->control_pid
> 0) {
65 unit_unwatch_pid(u
, s
->control_pid
);
71 free(s
->bind_to_device
);
73 unit_unwatch_timer(u
, &s
->timer_watch
);
76 static int socket_init(Unit
*u
) {
77 Socket
*s
= SOCKET(u
);
81 /* First, reset everything to the defaults, in case this is a
85 s
->timer_watch
.type
= WATCH_INVALID
;
86 s
->bind_ipv6_only
= false;
87 s
->backlog
= SOMAXCONN
;
88 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
89 exec_context_init(&s
->exec_context
);
91 if ((r
= unit_load_fragment_and_dropin(u
)) < 0)
94 if (!(t
= unit_name_change_suffix(unit_id(u
), ".service"))) {
99 r
= manager_load_unit(u
->meta
.manager
, t
, (Unit
**) &s
->service
);
105 if ((r
= unit_add_dependency(u
, UNIT_BEFORE
, UNIT(s
->service
))) < 0)
115 static const char* listen_lookup(int type
) {
117 if (type
== SOCK_STREAM
)
118 return "ListenStream";
119 else if (type
== SOCK_DGRAM
)
120 return "ListenDatagram";
121 else if (type
== SOCK_SEQPACKET
)
122 return "ListenSequentialPacket";
124 assert_not_reached("Unknown socket type");
128 static void socket_dump(Unit
*u
, FILE *f
, const char *prefix
) {
130 static const char* const command_table
[_SOCKET_EXEC_MAX
] = {
131 [SOCKET_EXEC_START_PRE
] = "StartPre",
132 [SOCKET_EXEC_START_POST
] = "StartPost",
133 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
134 [SOCKET_EXEC_STOP_POST
] = "StopPost"
138 Socket
*s
= SOCKET(u
);
144 prefix2
= strappend(prefix
, "\t");
149 "%sSocket State: %s\n"
150 "%sBindIPv6Only: %s\n"
152 prefix
, state_string_table
[s
->state
],
153 prefix
, yes_no(s
->bind_ipv6_only
),
156 if (s
->bind_to_device
)
158 "%sBindToDevice: %s\n",
159 prefix
, s
->bind_to_device
);
161 LIST_FOREACH(port
, p
, s
->ports
) {
163 if (p
->type
== SOCKET_SOCKET
) {
168 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
173 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(p
->address
.type
), k
);
176 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
179 exec_context_dump(&s
->exec_context
, f
, prefix
);
181 for (c
= 0; c
< _SOCKET_EXEC_MAX
; c
++) {
182 if (!s
->exec_command
[c
])
185 fprintf(f
, "%s→ %s:\n",
186 prefix
, command_table
[c
]);
188 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
194 static void socket_close_fds(Socket
*s
) {
199 LIST_FOREACH(port
, p
, s
->ports
) {
203 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
204 assert_se(close_nointr(p
->fd
) >= 0);
210 static int socket_open_fds(Socket
*s
) {
216 LIST_FOREACH(port
, p
, s
->ports
) {
221 if (p
->type
== SOCKET_SOCKET
) {
223 if ((r
= socket_address_listen(&p
->address
, s
->backlog
, s
->bind_ipv6_only
, s
->bind_to_device
, &p
->fd
)) < 0)
228 assert(p
->type
== SOCKET_FIFO
);
230 if (mkfifo(p
->path
, 0666 & ~s
->exec_context
.umask
) < 0 && errno
!= EEXIST
) {
235 if ((p
->fd
= open(p
->path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
240 if (fstat(p
->fd
, &st
) < 0) {
245 /* FIXME verify user, access mode */
247 if (!S_ISFIFO(st
.st_mode
)) {
261 static void socket_unwatch_fds(Socket
*s
) {
266 LIST_FOREACH(port
, p
, s
->ports
) {
270 unit_unwatch_fd(UNIT(s
), &p
->fd_watch
);
274 static int socket_watch_fds(Socket
*s
) {
280 LIST_FOREACH(port
, p
, s
->ports
) {
284 if ((r
= unit_watch_fd(UNIT(s
), p
->fd
, POLLIN
, &p
->fd_watch
)) < 0)
291 socket_unwatch_fds(s
);
295 static void socket_set_state(Socket
*s
, SocketState state
) {
296 SocketState old_state
;
299 old_state
= s
->state
;
302 if (state
!= SOCKET_START_PRE
&&
303 state
!= SOCKET_START_POST
&&
304 state
!= SOCKET_STOP_PRE
&&
305 state
!= SOCKET_STOP_PRE_SIGTERM
&&
306 state
!= SOCKET_STOP_PRE_SIGKILL
&&
307 state
!= SOCKET_STOP_POST
&&
308 state
!= SOCKET_STOP_POST_SIGTERM
&&
309 state
!= SOCKET_STOP_POST_SIGKILL
)
310 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
312 if (state
!= SOCKET_START_PRE
&&
313 state
!= SOCKET_START_POST
&&
314 state
!= SOCKET_STOP_PRE
&&
315 state
!= SOCKET_STOP_PRE_SIGTERM
&&
316 state
!= SOCKET_STOP_PRE_SIGKILL
&&
317 state
!= SOCKET_STOP_POST
&&
318 state
!= SOCKET_STOP_POST_SIGTERM
&&
319 state
!= SOCKET_STOP_POST_SIGKILL
)
320 if (s
->control_pid
> 0) {
321 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
325 if (state
!= SOCKET_START_PRE
&&
326 state
!= SOCKET_START_POST
&&
327 state
!= SOCKET_STOP_PRE
&&
328 state
!= SOCKET_STOP_POST
)
329 s
->control_command
= NULL
;
331 if (state
!= SOCKET_START_POST
&&
332 state
!= SOCKET_LISTENING
&&
333 state
!= SOCKET_RUNNING
&&
334 state
!= SOCKET_STOP_PRE
&&
335 state
!= SOCKET_STOP_PRE_SIGTERM
&&
336 state
!= SOCKET_STOP_PRE_SIGKILL
)
339 if (state
!= SOCKET_LISTENING
)
340 socket_unwatch_fds(s
);
342 log_debug("%s changing %s → %s", unit_id(UNIT(s
)), state_string_table
[old_state
], state_string_table
[state
]);
344 unit_notify(UNIT(s
), state_translation_table
[old_state
], state_translation_table
[state
]);
347 static int socket_spawn(Socket
*s
, ExecCommand
*c
, bool timeout
, pid_t
*_pid
) {
356 if ((r
= unit_watch_timer(UNIT(s
), s
->timeout_usec
, &s
->timer_watch
)) < 0)
359 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
361 if ((r
= exec_spawn(c
, &s
->exec_context
, NULL
, 0, &pid
)) < 0)
364 if ((r
= unit_watch_pid(UNIT(s
), pid
)) < 0)
365 /* FIXME: we need to do something here */
374 unit_unwatch_timer(UNIT(s
), &s
->timer_watch
);
379 static void socket_enter_dead(Socket
*s
, bool success
) {
385 socket_set_state(s
, s
->failure
? SOCKET_MAINTAINANCE
: SOCKET_DEAD
);
388 static void socket_enter_stop_post(Socket
*s
, bool success
) {
395 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
])) {
397 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
400 socket_set_state(s
, SOCKET_STOP_POST
);
402 socket_enter_dead(s
, true);
407 log_warning("%s failed to run stop-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
408 socket_enter_dead(s
, false);
411 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
) {
419 if (s
->control_pid
> 0) {
422 sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_POST_SIGTERM
) ? SIGTERM
: SIGKILL
;
424 if (kill(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
429 socket_set_state(s
, state
);
431 socket_enter_dead(s
, true);
436 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s
)), strerror(-r
));
438 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
439 socket_enter_stop_post(s
, false);
441 socket_enter_dead(s
, false);
444 static void socket_enter_stop_pre(Socket
*s
, bool success
) {
451 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
])) {
453 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
456 socket_set_state(s
, SOCKET_STOP_PRE
);
458 socket_enter_stop_post(s
, true);
463 log_warning("%s failed to run stop-pre executable: %s", unit_id(UNIT(s
)), strerror(-r
));
464 socket_enter_stop_post(s
, false);
467 static void socket_enter_start_post(Socket
*s
) {
471 if ((r
= socket_open_fds(s
)) < 0 ||
472 (r
= socket_watch_fds(s
)) < 0) {
473 log_warning("%s failed to listen on sockets: %s", unit_id(UNIT(s
)), strerror(-r
));
477 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
])) {
479 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0) {
480 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s
)), strerror(-r
));
484 socket_set_state(s
, SOCKET_START_POST
);
486 socket_set_state(s
, SOCKET_LISTENING
);
491 socket_enter_stop_pre(s
, false);
494 static void socket_enter_start_pre(Socket
*s
) {
498 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
])) {
500 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
503 socket_set_state(s
, SOCKET_START_PRE
);
505 socket_enter_start_post(s
);
510 log_warning("%s failed to run start-pre exectuable: %s", unit_id(UNIT(s
)), strerror(-r
));
511 socket_enter_dead(s
, false);
514 static void socket_enter_running(Socket
*s
) {
519 if ((r
= manager_add_job(UNIT(s
)->meta
.manager
, JOB_START
, UNIT(s
->service
), JOB_REPLACE
, true, NULL
)) < 0)
522 socket_set_state(s
, SOCKET_RUNNING
);
526 log_warning("%s failed to queue socket startup job: %s", unit_id(UNIT(s
)), strerror(-r
));
527 socket_enter_dead(s
, false);
530 static void socket_run_next(Socket
*s
, bool success
) {
534 assert(s
->control_command
);
535 assert(s
->control_command
->command_next
);
540 s
->control_command
= s
->control_command
->command_next
;
542 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
548 if (s
->state
== SOCKET_STOP_PRE
)
549 socket_enter_stop_post(s
, false);
550 else if (s
->state
== SOCKET_STOP_POST
)
551 socket_enter_dead(s
, false);
553 socket_enter_stop_pre(s
, false);
556 static int socket_start(Unit
*u
) {
557 Socket
*s
= SOCKET(u
);
561 /* We cannot fulfill this request right now, try again later
563 if (s
->state
== SOCKET_STOP_PRE
||
564 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
565 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
566 s
->state
== SOCKET_STOP_POST
||
567 s
->state
== SOCKET_STOP_POST_SIGTERM
||
568 s
->state
== SOCKET_STOP_POST_SIGKILL
)
571 if (s
->state
== SOCKET_START_PRE
||
572 s
->state
== SOCKET_START_POST
)
575 /* Cannot run this without the service being around */
576 if (s
->service
->meta
.load_state
!= UNIT_LOADED
)
579 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_MAINTAINANCE
);
582 socket_enter_start_pre(s
);
586 static int socket_stop(Unit
*u
) {
587 Socket
*s
= SOCKET(u
);
591 /* We cannot fulfill this request right now, try again later
593 if (s
->state
== SOCKET_START_PRE
||
594 s
->state
== SOCKET_START_POST
)
597 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
599 socket_enter_stop_pre(s
, true);
603 static UnitActiveState
socket_active_state(Unit
*u
) {
606 return state_translation_table
[SOCKET(u
)->state
];
609 static void socket_fd_event(Unit
*u
, int fd
, uint32_t events
, Watch
*w
) {
610 Socket
*s
= SOCKET(u
);
614 log_info("Incoming traffic on %s", unit_id(u
));
616 if (events
!= POLLIN
)
617 socket_enter_stop_pre(s
, false);
619 socket_enter_running(s
);
622 static void socket_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
623 Socket
*s
= SOCKET(u
);
629 success
= code
== CLD_EXITED
&& status
== 0;
630 s
->failure
= s
->failure
|| !success
;
632 assert(s
->control_pid
== pid
);
633 assert(s
->control_command
);
635 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
638 log_debug("%s control process exited, code=%s status=%i", unit_id(u
), sigchld_code(code
), status
);
640 if (s
->control_command
->command_next
&&
641 (success
|| (s
->state
== SOCKET_EXEC_STOP_PRE
|| s
->state
== SOCKET_EXEC_STOP_POST
))) {
642 log_debug("%s running next command for the state %s", unit_id(u
), state_string_table
[s
->state
]);
643 socket_run_next(s
, success
);
645 /* No further commands for this step, so let's figure
646 * out what to do next */
648 log_debug("%s got final SIGCHLD for state %s", unit_id(u
), state_string_table
[s
->state
]);
652 case SOCKET_START_PRE
:
654 socket_enter_start_post(s
);
656 socket_enter_stop_pre(s
, false);
659 case SOCKET_START_POST
:
661 socket_set_state(s
, SOCKET_LISTENING
);
663 socket_enter_stop_pre(s
, false);
666 case SOCKET_STOP_PRE
:
667 case SOCKET_STOP_PRE_SIGTERM
:
668 case SOCKET_STOP_PRE_SIGKILL
:
669 socket_enter_stop_post(s
, success
);
672 case SOCKET_STOP_POST
:
673 case SOCKET_STOP_POST_SIGTERM
:
674 case SOCKET_STOP_POST_SIGKILL
:
675 socket_enter_dead(s
, success
);
679 assert_not_reached("Uh, control process died at wrong time.");
684 static void socket_timer_event(Unit
*u
, uint64_t elapsed
, Watch
*w
) {
685 Socket
*s
= SOCKET(u
);
688 assert(elapsed
== 1);
690 assert(w
== &s
->timer_watch
);
694 case SOCKET_START_PRE
:
695 case SOCKET_START_POST
:
696 log_warning("%s operation timed out. Stopping.", unit_id(u
));
697 socket_enter_stop_pre(s
, false);
700 case SOCKET_STOP_PRE
:
701 log_warning("%s stopping timed out. Terminating.", unit_id(u
));
702 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, false);
705 case SOCKET_STOP_PRE_SIGTERM
:
706 log_warning("%s stopping timed out. Killing.", unit_id(u
));
707 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, false);
710 case SOCKET_STOP_PRE_SIGKILL
:
711 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u
));
712 socket_enter_stop_post(s
, false);
715 case SOCKET_STOP_POST
:
716 log_warning("%s stopping timed out (2). Terminating.", unit_id(u
));
717 socket_enter_signal(s
, SOCKET_STOP_POST_SIGTERM
, false);
720 case SOCKET_STOP_POST_SIGTERM
:
721 log_warning("%s stopping timed out (2). Killing.", unit_id(u
));
722 socket_enter_signal(s
, SOCKET_STOP_POST_SIGKILL
, false);
725 case SOCKET_STOP_POST_SIGKILL
:
726 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u
));
727 socket_enter_dead(s
, false);
731 assert_not_reached("Timeout at wrong time.");
735 int socket_collect_fds(Socket
*s
, int **fds
, unsigned *n_fds
) {
744 /* Called from the service code for requesting our fds */
747 LIST_FOREACH(port
, p
, s
->ports
)
751 if (!(rfds
= new(int, rn_fds
)) < 0)
755 LIST_FOREACH(port
, p
, s
->ports
)
767 const UnitVTable socket_vtable
= {
775 .start
= socket_start
,
778 .active_state
= socket_active_state
,
780 .fd_event
= socket_fd_event
,
781 .sigchld_event
= socket_sigchld_event
,
782 .timer_event
= socket_timer_event