From a105da15cc7393d9b568e32590ab839ee06d2d9d Mon Sep 17 00:00:00 2001 From: Jennifer Richards Date: Wed, 18 Apr 2018 13:45:21 -0400 Subject: [PATCH] Track and clean up monitoring processes by pid, fix some debug msgs --- include/mon_internal.h | 1 + mon/mons.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- tid/tids.c | 6 +++--- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/include/mon_internal.h b/include/mon_internal.h index 4dac054..45eb60b 100644 --- a/include/mon_internal.h +++ b/include/mon_internal.h @@ -134,6 +134,7 @@ struct mons_instance { MONS_AUTH_FUNC *auth_handler; void *cookie; GPtrArray *handlers; + GArray *pids; /* PIDs of active mons processes */ }; /* Client instance */ diff --git a/mon/mons.c b/mon/mons.c index 82495ee..f2e69c5 100644 --- a/mon/mons.c +++ b/mon/mons.c @@ -46,12 +46,17 @@ #include "mons_handlers.h" +static void mons_sweep_procs(MONS_INSTANCE *mons); + static int mons_destructor(void *object) { MONS_INSTANCE *mons = talloc_get_type_abort(object, MONS_INSTANCE); - if (mons->handlers) { + if (mons->handlers) g_ptr_array_unref(mons->handlers); - } + + if (mons->pids) + g_array_unref(mons->pids); + return 0; } @@ -88,6 +93,12 @@ MONS_INSTANCE *mons_new(TALLOC_CTX *mem_ctx) talloc_free(mons); return NULL; } + + mons->pids = g_array_new(FALSE, FALSE, sizeof(pid_t)); + if (mons->pids == NULL) { + talloc_free(mons); + return NULL; + } } return mons; } @@ -233,12 +244,41 @@ int mons_accept(MONS_INSTANCE *mons, int listen) ); close(conn); exit(0); /* exit to kill forked child process */ - } else { - close(conn); } + /* Only the parent process gets here */ + close(conn); + g_array_append_val(mons->pids, pid); + /* clean up any processes that have completed */ - //while (waitpid(-1, 0, WNOHANG) > 0); TODO: only clean up our own pids + mons_sweep_procs(mons); return 0; } + +void mons_sweep_procs(MONS_INSTANCE *mons) +{ + guint ii; + pid_t pid; + int status; + + /* loop backwards over the array so we can remove elements as we go */ + for (ii=mons->pids->len; ii > 0; ii--) { + /* ii-1 is the current index */ + pid = g_array_index(mons->pids, pid_t, ii-1); + if (waitpid(pid, &status, WNOHANG) > 0) { + /* the process exited */ + tr_debug("mons_sweep_procs: monitoring process %d terminated.", pid); + + g_array_remove_index_fast(mons->pids, ii-1); /* disturbs only indices >= ii-1 which we've already handled */ + if (WIFEXITED(status)) { + if (WEXITSTATUS(status) == 0) + tr_debug("mons_sweep_procs: monitoring process %d succeeded.", pid); + else + tr_debug("mons_sweep_procs: monitoring process %d exited with status %d.", pid, WTERMSIG(status)); + } else if (WIFSIGNALED(status)) { + tr_debug("mons_sweep_procs: monitoring process %d terminated by signal %d.", pid, WTERMSIG(status)); + } + } + } +} diff --git a/tid/tids.c b/tid/tids.c index bcbd820..f7c587a 100644 --- a/tid/tids.c +++ b/tid/tids.c @@ -444,15 +444,15 @@ void tids_sweep_procs(TIDS_INSTANCE *tids) g_array_remove_index_fast(tids->pids, ii-1); /* disturbs only indices >= ii-1 which we've already handled */ if (WIFEXITED(status) && (WEXITSTATUS(status) == 0)) { - tr_debug("tids_sweep_procs: TID process succeeded"); + tr_debug("tids_sweep_procs: TID process %d succeeded.", pid); tids->req_count++; } else { tids->error_count++; if (WIFEXITED(status)) { - tr_debug("tids_sweep_procs: TID process %d exited with status %d", pid, WTERMSIG(status)); + tr_debug("tids_sweep_procs: TID process %d exited with status %d.", pid, WTERMSIG(status)); } else if (WIFSIGNALED(status)) { - tr_debug("tids_sweep_procs: TID process %d terminated by signal %d", pid, WTERMSIG(status)); + tr_debug("tids_sweep_procs: TID process %d terminated by signal %d.", pid, WTERMSIG(status)); } } } -- 2.1.4