1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
15 static const NameActiveState state_table
[_SOCKET_STATE_MAX
] = {
16 [SOCKET_DEAD
] = NAME_INACTIVE
,
17 [SOCKET_START_PRE
] = NAME_ACTIVATING
,
18 [SOCKET_START_POST
] = NAME_ACTIVATING
,
19 [SOCKET_LISTENING
] = NAME_ACTIVE
,
20 [SOCKET_RUNNING
] = NAME_ACTIVE
,
21 [SOCKET_STOP_PRE
] = NAME_DEACTIVATING
,
22 [SOCKET_STOP_PRE_SIGTERM
] = NAME_DEACTIVATING
,
23 [SOCKET_STOP_PRE_SIGKILL
] = NAME_DEACTIVATING
,
24 [SOCKET_STOP_POST
] = NAME_DEACTIVATING
,
25 [SOCKET_STOP_POST_SIGTERM
] = NAME_DEACTIVATING
,
26 [SOCKET_STOP_POST_SIGKILL
] = NAME_DEACTIVATING
,
27 [SOCKET_MAINTAINANCE
] = NAME_INACTIVE
,
30 static int socket_init(Name
*n
) {
31 Socket
*s
= SOCKET(n
);
35 /* First, reset everything to the defaults, in case this is a
38 s
->bind_ipv6_only
= false;
39 s
->backlog
= SOMAXCONN
;
40 s
->timeout_usec
= DEFAULT_TIMEOUT_USEC
;
41 exec_context_init(&s
->exec_context
);
43 if ((r
= name_load_fragment_and_dropin(n
)) < 0)
46 if (!(t
= name_change_suffix(name_id(n
), ".service")))
49 r
= manager_load_name(n
->meta
.manager
, t
, (Name
**) &s
->service
);
55 if ((r
= set_ensure_allocated(n
->meta
.dependencies
+ NAME_BEFORE
, trivial_hash_func
, trivial_compare_func
)) < 0)
58 if ((r
= set_put(n
->meta
.dependencies
[NAME_BEFORE
], s
->service
)) < 0)
64 static void socket_done(Name
*n
) {
65 Socket
*s
= SOCKET(n
);
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 name_unwatch_pid(n
, s
->control_pid
);
90 name_unwatch_timer(n
, &s
->timer_id
);
93 static const char* listen_lookup(int type
) {
95 if (type
== SOCK_STREAM
)
96 return "ListenStream";
97 else if (type
== SOCK_DGRAM
)
98 return "ListenDatagram";
99 else if (type
== SOCK_SEQPACKET
)
100 return "ListenSequentialPacket";
102 assert_not_reached("Unknown socket type");
106 static void socket_dump(Name
*n
, FILE *f
, const char *prefix
) {
108 static const char* const state_table
[_SOCKET_STATE_MAX
] = {
109 [SOCKET_DEAD
] = "dead",
110 [SOCKET_START_PRE
] = "start-pre",
111 [SOCKET_START_POST
] = "start-post",
112 [SOCKET_LISTENING
] = "listening",
113 [SOCKET_RUNNING
] = "running",
114 [SOCKET_STOP_PRE
] = "stop-pre",
115 [SOCKET_STOP_PRE_SIGTERM
] = "stop-pre-sigterm",
116 [SOCKET_STOP_PRE_SIGKILL
] = "stop-pre-sigkill",
117 [SOCKET_STOP_POST
] = "stop-post",
118 [SOCKET_STOP_POST_SIGTERM
] = "stop-post-sigterm",
119 [SOCKET_STOP_POST_SIGKILL
] = "stop-post-sigkill",
120 [SOCKET_MAINTAINANCE
] = "maintainance"
123 static const char* const command_table
[_SOCKET_EXEC_MAX
] = {
124 [SOCKET_EXEC_START_PRE
] = "StartPre",
125 [SOCKET_EXEC_START_POST
] = "StartPost",
126 [SOCKET_EXEC_STOP_PRE
] = "StopPre",
127 [SOCKET_EXEC_STOP_POST
] = "StopPost"
131 Socket
*s
= SOCKET(n
);
137 "%sSocket State: %s\n"
138 "%sBindIPv6Only: %s\n"
140 prefix
, state_table
[s
->state
],
141 prefix
, yes_no(s
->bind_ipv6_only
),
144 LIST_FOREACH(port
, p
, s
->ports
) {
146 if (p
->type
== SOCKET_SOCKET
) {
151 if ((r
= socket_address_print(&p
->address
, &k
)) < 0)
156 fprintf(f
, "%s%s: %s\n", prefix
, listen_lookup(p
->address
.type
), k
);
159 fprintf(f
, "%sListenFIFO: %s\n", prefix
, p
->path
);
162 exec_context_dump(&s
->exec_context
, f
, prefix
);
164 for (c
= 0; c
< _SOCKET_EXEC_MAX
; c
++) {
167 LIST_FOREACH(command
, i
, s
->exec_command
[c
])
168 fprintf(f
, "%s%s: %s\n", prefix
, command_table
[c
], i
->path
);
172 static void socket_close_fds(Socket
*s
) {
177 LIST_FOREACH(port
, p
, s
->ports
) {
181 name_unwatch_fd(NAME(s
), p
->fd
);
182 assert_se(close_nointr(p
->fd
) >= 0);
188 static int socket_open_fds(Socket
*s
) {
194 LIST_FOREACH(port
, p
, s
->ports
) {
199 if (p
->type
== SOCKET_SOCKET
) {
201 if ((r
= socket_address_listen(&p
->address
, s
->backlog
, s
->bind_ipv6_only
, &p
->fd
)) < 0)
206 assert(p
->type
== SOCKET_FIFO
);
208 if (mkfifo(p
->path
, 0666 & ~s
->exec_context
.umask
) < 0 && errno
!= EEXIST
) {
213 if ((p
->fd
= open(p
->path
, O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
|O_NOFOLLOW
)) < 0) {
218 if (fstat(p
->fd
, &st
) < 0) {
223 /* FIXME verify user, access mode */
225 if (!S_ISFIFO(st
.st_mode
)) {
239 static void socket_unwatch_fds(Socket
*s
) {
244 LIST_FOREACH(port
, p
, s
->ports
) {
248 name_unwatch_fd(NAME(s
), p
->fd
);
252 static int socket_watch_fds(Socket
*s
) {
258 LIST_FOREACH(port
, p
, s
->ports
) {
262 if ((r
= name_watch_fd(NAME(s
), p
->fd
, POLLIN
)) < 0)
269 socket_unwatch_fds(s
);
273 static void socket_set_state(Socket
*s
, SocketState state
) {
274 SocketState old_state
;
277 old_state
= s
->state
;
280 if (state
!= SOCKET_START_PRE
&&
281 state
!= SOCKET_START_POST
&&
282 state
!= SOCKET_STOP_PRE
&&
283 state
!= SOCKET_STOP_PRE_SIGTERM
&&
284 state
!= SOCKET_STOP_PRE_SIGKILL
&&
285 state
!= SOCKET_STOP_POST
&&
286 state
!= SOCKET_STOP_POST_SIGTERM
&&
287 state
!= SOCKET_STOP_POST_SIGKILL
)
288 name_unwatch_timer(NAME(s
), &s
->timer_id
);
290 if (state
!= SOCKET_START_PRE
&&
291 state
!= SOCKET_START_POST
&&
292 state
!= SOCKET_STOP_PRE
&&
293 state
!= SOCKET_STOP_PRE_SIGTERM
&&
294 state
!= SOCKET_STOP_PRE_SIGKILL
&&
295 state
!= SOCKET_STOP_POST
&&
296 state
!= SOCKET_STOP_POST_SIGTERM
&&
297 state
!= SOCKET_STOP_POST_SIGKILL
)
298 if (s
->control_pid
>= 0) {
299 name_unwatch_pid(NAME(s
), s
->control_pid
);
303 if (state
!= SOCKET_START_PRE
&&
304 state
!= SOCKET_START_POST
&&
305 state
!= SOCKET_STOP_PRE
&&
306 state
!= SOCKET_STOP_POST
)
307 s
->control_command
= NULL
;
309 if (state
!= SOCKET_START_POST
&&
310 state
!= SOCKET_LISTENING
&&
311 state
!= SOCKET_RUNNING
&&
312 state
!= SOCKET_STOP_PRE
&&
313 state
!= SOCKET_STOP_PRE_SIGTERM
&&
314 state
!= SOCKET_STOP_PRE_SIGKILL
)
317 if (state
!= SOCKET_LISTENING
)
318 socket_unwatch_fds(s
);
320 name_notify(NAME(s
), state_table
[old_state
], state_table
[s
->state
]);
323 static int socket_spawn(Socket
*s
, ExecCommand
*c
, bool timeout
, pid_t
*_pid
) {
332 if ((r
= name_watch_timer(NAME(s
), s
->timeout_usec
, &s
->timer_id
)) < 0)
335 name_unwatch_timer(NAME(s
), &s
->timer_id
);
337 if ((r
= exec_spawn(c
, &s
->exec_context
, NULL
, 0, &pid
)) < 0)
340 if ((r
= name_watch_pid(NAME(s
), pid
)) < 0)
341 /* FIXME: we need to do something here */
350 name_unwatch_timer(NAME(s
), &s
->timer_id
);
355 static void socket_enter_dead(Socket
*s
, bool success
) {
361 socket_set_state(s
, s
->failure
? SOCKET_MAINTAINANCE
: SOCKET_DEAD
);
364 static void socket_enter_stop_post(Socket
*s
, bool success
) {
371 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_POST
])) {
373 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
376 socket_set_state(s
, SOCKET_STOP_POST
);
378 socket_enter_dead(s
, true);
383 log_warning("%s failed to run stop-post executable: %s", name_id(NAME(s
)), strerror(-r
));
384 socket_enter_dead(s
, false);
387 static void socket_enter_signal(Socket
*s
, SocketState state
, bool success
) {
395 if (s
->control_pid
> 0) {
398 sig
= (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_POST_SIGTERM
) ? SIGTERM
: SIGKILL
;
400 if (kill(s
->control_pid
, sig
) < 0 && errno
!= ESRCH
) {
405 socket_set_state(s
, state
);
407 socket_enter_dead(s
, true);
412 log_warning("%s failed to kill processes: %s", name_id(NAME(s
)), strerror(-r
));
414 if (state
== SOCKET_STOP_PRE_SIGTERM
|| state
== SOCKET_STOP_PRE_SIGKILL
)
415 socket_enter_stop_post(s
, false);
417 socket_enter_dead(s
, false);
420 static void socket_enter_stop_pre(Socket
*s
, bool success
) {
427 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_STOP_PRE
])) {
429 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
432 socket_set_state(s
, SOCKET_STOP_PRE
);
434 socket_enter_stop_post(s
, true);
439 log_warning("%s failed to run stop-pre executable: %s", name_id(NAME(s
)), strerror(-r
));
440 socket_enter_stop_post(s
, false);
443 static void socket_enter_start_post(Socket
*s
) {
447 if ((r
= socket_open_fds(s
)) < 0 ||
448 (r
= socket_watch_fds(s
)) < 0) {
449 log_warning("%s failed to listen on sockets: %s", name_id(NAME(s
)), strerror(-r
));
453 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_POST
])) {
455 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0) {
456 log_warning("%s failed to run start-post executable: %s", name_id(NAME(s
)), strerror(-r
));
460 socket_set_state(s
, SOCKET_START_POST
);
462 socket_set_state(s
, SOCKET_LISTENING
);
467 socket_enter_stop_pre(s
, false);
470 static void socket_enter_start_pre(Socket
*s
) {
474 if ((s
->control_command
= s
->exec_command
[SOCKET_EXEC_START_PRE
])) {
476 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
479 socket_set_state(s
, SOCKET_START_PRE
);
481 socket_enter_start_post(s
);
486 log_warning("%s failed to run start-pre exectuable: %s", name_id(NAME(s
)), strerror(-r
));
487 socket_enter_dead(s
, false);
490 static void socket_enter_running(Socket
*s
) {
495 if ((r
= manager_add_job(NAME(s
)->meta
.manager
, JOB_START
, NAME(s
->service
), JOB_REPLACE
, true, NULL
)) < 0)
498 socket_set_state(s
, SOCKET_RUNNING
);
502 log_warning("%s failed to queue socket startup job: %s", name_id(NAME(s
)), strerror(-r
));
503 socket_enter_dead(s
, false);
506 static void socket_run_next(Socket
*s
, bool success
) {
510 assert(s
->control_command
);
511 assert(s
->control_command
->command_next
);
516 s
->control_command
= s
->control_command
->command_next
;
518 if ((r
= socket_spawn(s
, s
->control_command
, true, &s
->control_pid
)) < 0)
524 if (s
->state
== SOCKET_STOP_PRE
)
525 socket_enter_stop_post(s
, false);
526 else if (s
->state
== SOCKET_STOP_POST
)
527 socket_enter_dead(s
, false);
529 socket_enter_stop_pre(s
, false);
532 static int socket_start(Name
*n
) {
533 Socket
*s
= SOCKET(n
);
537 /* We cannot fulfill this request right now, try again later
539 if (s
->state
== SOCKET_STOP_PRE
||
540 s
->state
== SOCKET_STOP_PRE_SIGKILL
||
541 s
->state
== SOCKET_STOP_PRE_SIGTERM
||
542 s
->state
== SOCKET_STOP_POST
||
543 s
->state
== SOCKET_STOP_POST_SIGTERM
||
544 s
->state
== SOCKET_STOP_POST_SIGKILL
)
547 if (s
->state
== SOCKET_START_PRE
||
548 s
->state
== SOCKET_START_POST
)
551 /* Cannot run this without the service being around */
552 if (s
->service
->meta
.load_state
!= NAME_LOADED
)
555 assert(s
->state
== SOCKET_DEAD
|| s
->state
== SOCKET_MAINTAINANCE
);
558 socket_enter_start_pre(s
);
562 static int socket_stop(Name
*n
) {
563 Socket
*s
= SOCKET(n
);
567 /* We cannot fulfill this request right now, try again later
569 if (s
->state
== SOCKET_START_PRE
||
570 s
->state
== SOCKET_START_POST
)
573 assert(s
->state
== SOCKET_LISTENING
|| s
->state
== SOCKET_RUNNING
);
575 socket_enter_stop_pre(s
, true);
579 static NameActiveState
socket_active_state(Name
*n
) {
582 return state_table
[SOCKET(n
)->state
];
585 static void socket_fd_event(Name
*n
, int fd
, uint32_t events
) {
586 Socket
*s
= SOCKET(n
);
590 log_info("Incoming traffic on %s", name_id(n
));
592 if (events
!= POLLIN
)
593 socket_enter_stop_pre(s
, false);
595 socket_enter_running(s
);
598 static void socket_sigchld_event(Name
*n
, pid_t pid
, int code
, int status
) {
599 Socket
*s
= SOCKET(n
);
605 success
= code
== CLD_EXITED
|| status
== 0;
606 s
->failure
= s
->failure
|| !success
;
608 assert(s
->control_pid
== pid
);
609 assert(s
->control_command
);
611 exec_status_fill(&s
->control_command
->exec_status
, pid
, code
, status
);
614 log_debug("%s: control process exited, code=%s status=%i", name_id(n
), sigchld_code(code
), status
);
616 if (s
->control_command
->command_next
&&
617 (success
|| (s
->state
== SOCKET_EXEC_STOP_PRE
|| s
->state
== SOCKET_EXEC_STOP_POST
)))
618 socket_run_next(s
, success
);
620 /* No further commands for this step, so let's figure
621 * out what to do next */
625 case SOCKET_START_PRE
:
627 socket_enter_start_pre(s
);
629 socket_enter_stop_pre(s
, false);
632 case SOCKET_START_POST
:
634 socket_set_state(s
, SOCKET_LISTENING
);
636 socket_enter_stop_pre(s
, false);
639 case SOCKET_STOP_PRE
:
640 case SOCKET_STOP_PRE_SIGTERM
:
641 case SOCKET_STOP_PRE_SIGKILL
:
642 socket_enter_stop_post(s
, success
);
645 case SOCKET_STOP_POST
:
646 case SOCKET_STOP_POST_SIGTERM
:
647 case SOCKET_STOP_POST_SIGKILL
:
648 socket_enter_dead(s
, success
);
652 assert_not_reached("Uh, control process died at wrong time.");
657 static void socket_timer_event(Name
*n
, int id
, uint64_t elapsed
) {
658 Socket
*s
= SOCKET(n
);
661 assert(elapsed
== 1);
663 assert(s
->timer_id
== id
);
667 case SOCKET_START_PRE
:
668 case SOCKET_START_POST
:
669 log_warning("%s operation timed out. Stopping.", name_id(n
));
670 socket_enter_stop_pre(s
, false);
673 case SOCKET_STOP_PRE
:
674 log_warning("%s stopping timed out. Terminating.", name_id(n
));
675 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGTERM
, false);
678 case SOCKET_STOP_PRE_SIGTERM
:
679 log_warning("%s stopping timed out. Killing.", name_id(n
));
680 socket_enter_signal(s
, SOCKET_STOP_PRE_SIGKILL
, false);
683 case SOCKET_STOP_PRE_SIGKILL
:
684 log_warning("%s still around after SIGKILL. Ignoring.", name_id(n
));
685 socket_enter_stop_post(s
, false);
688 case SOCKET_STOP_POST
:
689 log_warning("%s stopping timed out (2). Terminating.", name_id(n
));
690 socket_enter_signal(s
, SOCKET_STOP_POST_SIGTERM
, false);
693 case SOCKET_STOP_POST_SIGTERM
:
694 log_warning("%s stopping timed out (2). Killing.", name_id(n
));
695 socket_enter_signal(s
, SOCKET_STOP_POST_SIGKILL
, false);
698 case SOCKET_STOP_POST_SIGKILL
:
699 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", name_id(n
));
700 socket_enter_dead(s
, false);
704 assert_not_reached("Timeout at wrong time.");
708 const NameVTable socket_vtable
= {
716 .start
= socket_start
,
719 .active_state
= socket_active_state
,
721 .fd_event
= socket_fd_event
,
722 .sigchld_event
= socket_sigchld_event
,
723 .timer_event
= socket_timer_event