2 #include <sys/socket.h>
5 #include "dinit-socket.h"
6 #include "dinit-util.h"
8 #include "proc-service.h"
11 * Most of the implementation for process-based services (process, scripted, bgprocess) is here.
13 * See proc-service.h header for interface details.
16 // Given a string and a list of pairs of (start,end) indices for each argument in that string,
17 // store a null terminator for the argument. Return a `char *` vector containing the beginning
18 // of each argument and a trailing nullptr. (The returned array is invalidated if the string is later modified).
19 std::vector<const char *> separate_args(std::string &s, std::list<std::pair<unsigned,unsigned>> &arg_indices)
21 std::vector<const char *> r;
22 r.reserve(arg_indices.size() + 1);
24 // First store nul terminator for each part:
25 for (auto index_pair : arg_indices) {
26 if (index_pair.second < s.length()) {
27 s[index_pair.second] = 0;
31 // Now we can get the C string (c_str) and store offsets into it:
32 const char * cstr = s.c_str();
33 for (auto index_pair : arg_indices) {
34 r.push_back(cstr + index_pair.first);
40 void process_service::exec_succeeded() noexcept
42 // This could be a smooth recovery (state already STARTED). Even more, the process
43 // might be stopped (and killed via a signal) during smooth recovery. We don't to
44 // process startup again in either case, so we check for state STARTING:
45 if (get_state() == service_state_t::STARTING) {
48 else if (get_state() == service_state_t::STOPPING) {
49 // stopping, but smooth recovery was in process. That's now over so we can
50 // commence normal stop. Note that if pid == -1 the process already stopped(!),
51 // that's handled below.
52 if (pid != -1 && stop_check_dependents()) {
58 rearm exec_status_pipe_watcher::fd_event(eventloop_t &loop, int fd, int flags) noexcept
60 base_process_service *sr = service;
61 sr->waiting_for_execstat = false;
64 int r = read(get_watched_fd(), &exec_status, sizeof(int));
66 close(get_watched_fd());
69 // We read an errno code; exec() failed, and the service startup failed.
71 sr->child_listener.deregister(event_loop, sr->pid);
72 sr->reserved_child_watch = false;
73 if (sr->stop_timer_armed) {
74 sr->restart_timer.stop_timer(loop);
75 sr->stop_timer_armed = false;
79 sr->exec_failed(exec_status);
85 // Somehow the process managed to complete before we even saw the exec() status.
86 sr->handle_exit_status(sr->exit_status);
90 sr->services->process_queues();
92 return rearm::REMOVED;
95 dasynq::rearm service_child_watcher::status_change(eventloop_t &loop, pid_t child, int status) noexcept
97 base_process_service *sr = service;
100 sr->exit_status = status;
102 // Ok, for a process service, any process death which we didn't rig
103 // ourselves is a bit... unexpected. Probably, the child died because
104 // we asked it to (sr->service_state == STOPPING). But even if
105 // we didn't, there's not much we can do.
107 if (sr->waiting_for_execstat) {
108 // We still don't have an exec() status from the forked child, wait for that
109 // before doing any further processing.
110 return dasynq::rearm::NOOP; // hold watch reservation
113 // Must stop watch now since handle_exit_status might result in re-launch:
114 // (stop_watch instead of deregister, so that we hold watch reservation).
117 if (sr->stop_timer_armed) {
118 sr->restart_timer.stop_timer(loop);
119 sr->stop_timer_armed = false;
122 sr->handle_exit_status(status);
123 return dasynq::rearm::NOOP;
126 void process_service::handle_exit_status(int exit_status) noexcept
128 bool did_exit = WIFEXITED(exit_status);
129 bool was_signalled = WIFSIGNALED(exit_status);
131 auto service_state = get_state();
133 if (exit_status != 0 && service_state != service_state_t::STOPPING) {
135 log(loglevel_t::ERROR, "Service ", get_name(), " process terminated with exit code ",
136 WEXITSTATUS(exit_status));
138 else if (was_signalled) {
139 log(loglevel_t::ERROR, "Service ", get_name(), " terminated due to signal ",
140 WTERMSIG(exit_status));
144 if (service_state == service_state_t::STARTING) {
145 if (did_exit && WEXITSTATUS(exit_status) == 0) {
152 else if (service_state == service_state_t::STOPPING) {
153 // We won't log a non-zero exit status or termination due to signal here -
154 // we assume that the process died because we signalled it.
157 else if (smooth_recovery && service_state == service_state_t::STARTED
158 && get_target_state() == service_state_t::STARTED) {
159 do_smooth_recovery();
165 services->process_queues();
168 void process_service::exec_failed(int errcode) noexcept
170 log(loglevel_t::ERROR, get_name(), ": execution failed: ", strerror(errcode));
171 if (get_state() == service_state_t::STARTING) {
175 // Process service in smooth recovery:
180 void bgproc_service::handle_exit_status(int exit_status) noexcept
183 bool did_exit = WIFEXITED(exit_status);
184 bool was_signalled = WIFSIGNALED(exit_status);
185 auto service_state = get_state();
187 if (exit_status != 0 && service_state != service_state_t::STOPPING) {
189 log(loglevel_t::ERROR, "Service ", get_name(), " process terminated with exit code ",
190 WEXITSTATUS(exit_status));
192 else if (was_signalled) {
193 log(loglevel_t::ERROR, "Service ", get_name(), " terminated due to signal ",
194 WTERMSIG(exit_status));
198 // This may be a "smooth recovery" where we are restarting the process while leaving the
199 // service in the STARTED state.
200 if (restarting && service_state == service_state_t::STARTED) {
202 bool need_stop = false;
203 if ((did_exit && WEXITSTATUS(exit_status) != 0) || was_signalled) {
207 // We need to re-read the PID, since it has now changed.
208 if (pid_file.length() != 0) {
209 auto pid_result = read_pid_file(&exit_status);
210 switch (pid_result) {
211 case pid_result_t::FAILED:
212 // Failed startup: no auto-restart.
215 case pid_result_t::TERMINATED:
217 case pid_result_t::OK:
224 // Failed startup: no auto-restart.
226 services->process_queues();
233 if (service_state == service_state_t::STARTING) {
234 // POSIX requires that if the process exited clearly with a status code of 0,
235 // the exit status value will be 0:
236 if (exit_status == 0) {
237 auto pid_result = read_pid_file(&exit_status);
238 switch (pid_result) {
239 case pid_result_t::FAILED:
240 // Failed startup: no auto-restart.
243 case pid_result_t::TERMINATED:
244 // started, but immediately terminated
247 case pid_result_t::OK:
256 else if (service_state == service_state_t::STOPPING) {
257 // We won't log a non-zero exit status or termination due to signal here -
258 // we assume that the process died because we signalled it.
262 // we must be STARTED
263 if (smooth_recovery && get_target_state() == service_state_t::STARTED) {
264 do_smooth_recovery();
267 if (! do_auto_restart() && start_explicit) {
268 start_explicit = false;
275 services->process_queues();
278 void bgproc_service::exec_failed(int errcode) noexcept
280 log(loglevel_t::ERROR, get_name(), ": execution failed: ", strerror(errcode));
281 // Only time we execute is for startup:
285 void scripted_service::handle_exit_status(int exit_status) noexcept
287 bool did_exit = WIFEXITED(exit_status);
288 bool was_signalled = WIFSIGNALED(exit_status);
289 auto service_state = get_state();
291 // For a scripted service, a termination occurs in one of three main cases:
292 // - the start script completed (or failed), when service was STARTING
293 // - the start script was interrupted to cancel startup; state is STOPPING
294 // - the stop script complete (or failed), state is STOPPING
296 if (service_state == service_state_t::STOPPING) {
297 // We might be running the stop script, or we might be running the start script and have issued
298 // a cancel order via SIGINT:
299 if (did_exit && WEXITSTATUS(exit_status) == 0) {
300 if (interrupting_start) {
301 interrupting_start = false;
302 // launch stop script:
306 // We were running the stop script and finished successfully
311 if (interrupting_start) {
312 // We issued a start interrupt, so we expected this failure:
314 log(loglevel_t::INFO, "Service ", get_name(), " start cancelled; exit code ",
315 WEXITSTATUS(exit_status));
317 else if (was_signalled) {
318 log(loglevel_t::INFO, "Service ", get_name(), " start cancelled from signal ",
319 WTERMSIG(exit_status));
323 // ??? failed to stop! Let's log it as warning:
325 log(loglevel_t::WARN, "Service ", get_name(), " stop command failed with exit code ",
326 WEXITSTATUS(exit_status));
328 else if (was_signalled) {
329 log(loglevel_t::WARN, "Service ", get_name(), " stop command terminated due to signal ",
330 WTERMSIG(exit_status));
333 // Even if the stop script failed, assume that service is now stopped, so that any dependencies
334 // can be stopped. There's not really any other useful course of action here.
335 interrupting_start = false;
338 services->process_queues();
341 if (exit_status == 0) {
347 log(loglevel_t::ERROR, "Service ", get_name(), " command failed with exit code ",
348 WEXITSTATUS(exit_status));
350 else if (was_signalled) {
351 log(loglevel_t::ERROR, "Service ", get_name(), " command terminated due to signal ",
352 WTERMSIG(exit_status));
356 services->process_queues();
360 void scripted_service::exec_failed(int errcode) noexcept
362 log(loglevel_t::ERROR, get_name(), ": execution failed: ", strerror(errcode));
363 auto service_state = get_state();
364 if (service_state == service_state_t::STARTING) {
367 else if (service_state == service_state_t::STOPPING) {
368 // We've logged the failure, but it's probably better not to leave the service in
374 bgproc_service::pid_result_t
375 bgproc_service::read_pid_file(int *exit_status) noexcept
377 const char *pid_file_c = pid_file.c_str();
378 int fd = open(pid_file_c, O_CLOEXEC);
380 log(loglevel_t::ERROR, get_name(), ": read pid file: ", strerror(errno));
381 return pid_result_t::FAILED;
384 char pidbuf[21]; // just enough to hold any 64-bit integer
385 int r = ss_read(fd, pidbuf, 20);
387 // Could not read from PID file
388 log(loglevel_t::ERROR, get_name(), ": could not read from pidfile; ", strerror(errno));
390 return pid_result_t::FAILED;
394 pidbuf[r] = 0; // store nul terminator
396 bool valid_pid = false;
398 unsigned long long v = std::stoull(pidbuf, nullptr, 0);
399 if (v <= std::numeric_limits<pid_t>::max()) {
404 catch (std::out_of_range &exc) {
407 catch (std::invalid_argument &exc) {
408 // Ok, so it doesn't look like a number: proceed...
412 pid_t wait_r = waitpid(pid, exit_status, WNOHANG);
413 if (wait_r == -1 && errno == ECHILD) {
414 // We can't track this child - check process exists:
415 if (kill(pid, 0) == 0 || errno != ESRCH) {
416 tracking_child = false;
417 return pid_result_t::OK;
420 log(loglevel_t::ERROR, get_name(), ": pid read from pidfile (", pid, ") is not valid");
422 return pid_result_t::FAILED;
425 else if (wait_r == pid) {
427 return pid_result_t::TERMINATED;
429 else if (wait_r == 0) {
430 // We can track the child
431 child_listener.add_reserved(event_loop, pid, dasynq::DEFAULT_PRIORITY - 10);
432 tracking_child = true;
433 reserved_child_watch = true;
434 return pid_result_t::OK;
438 log(loglevel_t::ERROR, get_name(), ": pid read from pidfile (", pid, ") is not valid");
440 return pid_result_t::FAILED;
443 void process_service::bring_down() noexcept
445 waiting_for_deps = false;
446 if (waiting_for_execstat) {
447 // The process is still starting. This should be uncommon, but can occur during
448 // smooth recovery. We can't do much now; we have to wait until we get the
449 // status, and then act appropriately.
452 else if (pid != -1) {
453 // The process is still kicking on - must actually kill it. We signal the process
454 // group (-pid) rather than just the process as there's less risk then of creating
455 // an orphaned process group:
456 if (! onstart_flags.no_sigterm) {
459 if (term_signal != -1) {
460 kill_pg(term_signal);
463 // In most cases, the rest is done in handle_exit_status.
464 // If we are a BGPROCESS and the process is not our immediate child, however, that
465 // won't work - check for this now:
466 if (get_type() == service_type_t::BGPROCESS && ! tracking_child) {
469 else if (stop_timeout != time_val(0,0)) {
470 restart_timer.arm_timer_rel(event_loop, stop_timeout);
471 stop_timer_armed = true;
475 // The process is already dead.
480 void scripted_service::bring_down() noexcept
482 waiting_for_deps = false;
483 if (stop_command.length() == 0) {
486 else if (! start_ps_process(stop_arg_parts, false)) {
487 // Couldn't execute stop script, but there's not much we can do:
491 // successfully started stop script: start kill timer:
492 if (stop_timeout != time_val(0,0)) {
493 restart_timer.arm_timer_rel(event_loop, stop_timeout);
494 stop_timer_armed = true;
499 dasynq::rearm process_restart_timer::timer_expiry(eventloop_t &, int expiry_count)
501 service->timer_expired();
503 // Leave the timer disabled, or, if it has been reset by any processing above, leave it armed:
504 return dasynq::rearm::NOOP;