From 05f1ad25b8effdf7497ccc4f3d95cb15a035f0b8 Mon Sep 17 00:00:00 2001 From: Davin McCall Date: Mon, 26 Jun 2017 18:58:03 +0100 Subject: [PATCH] service: use recorded state to check whether we can track child process We have a flag which records whether we are tracking the child process. A BGPROCESS service can't always track the child, so it stops once the dependencies have stopped without waiting for the process. Previously we did a manual check rather than using the existing flag to see whether the process could be tracked before immediately stopping. --- src/service.cc | 16 +++------------- src/service.h | 6 +++--- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/service.cc b/src/service.cc index b6d726a..cda831e 100644 --- a/src/service.cc +++ b/src/service.cc @@ -1185,19 +1185,8 @@ void base_process_service::all_deps_stopped() noexcept // In most cases, the rest is done in handle_exit_status. // If we are a BGPROCESS and the process is not our immediate child, however, that // won't work - check for this now: - if (record_type == service_type::BGPROCESS) { - // TODO use 'tracking_child' instead - int status; - pid_t r = waitpid(pid, &status, WNOHANG); - if (r == -1 && errno == ECHILD) { - // We can't track this child (or it's terminated already) - stopped(); - } - else if (r == pid) { - // Process may have died due to signal since we explicitly requested it to - // stop by signalling it; no need to log any termination status. - stopped(); - } + if (record_type == service_type::BGPROCESS && ! tracking_child) { + stopped(); } } else { @@ -1279,6 +1268,7 @@ base_process_service::base_process_service(service_set *sset, string name, servi waiting_restart_timer = false; reserved_child_watch = false; + tracking_child = false; } void base_process_service::do_restart() noexcept diff --git a/src/service.h b/src/service.h index c0d1abc..c4640fb 100644 --- a/src/service.h +++ b/src/service.h @@ -612,11 +612,11 @@ class base_process_service : public service_record service_child_watcher child_listener; exec_status_pipe_watcher child_status_listener; process_restart_timer restart_timer; - timespec last_start_time; + time_val last_start_time; // Restart interval time and restart count are used to track the number of automatic restarts // over an interval. Too many restarts over an interval will inhibit further restarts. - timespec restart_interval_time; + time_val restart_interval_time; int restart_interval_count; timespec restart_interval; @@ -625,6 +625,7 @@ class base_process_service : public service_record bool waiting_restart_timer : 1; bool reserved_child_watch : 1; + bool tracking_child : 1; // Start the process, return true on success virtual bool start_ps_process() noexcept override; @@ -691,7 +692,6 @@ class bgproc_service : public base_process_service bool doing_recovery : 1; // if we are currently recovering a BGPROCESS (restarting process, while // holding STARTED service state) - bool tracking_child : 1; enum class pid_result_t { OK, -- 2.25.1