[Pkg-nagios-changes] [pkg-mod-gearman] 28/48: make waitpid child check optional
Stig Sandbeck Mathisen
ssm at debian.org
Sun Nov 24 22:38:10 UTC 2013
This is an automated email from the git hooks/post-receive script.
ssm pushed a commit to branch master
in repository pkg-mod-gearman.
commit 978b9dbfc50980cc1c18447b07abd41b7e0eddf9
Author: Sven Nierlein <Sven.Nierlein at consol.de>
Date: Thu Oct 31 23:57:17 2013 +0100
make waitpid child check optional
---
common/check_utils.c | 2 +-
common/utils.c | 22 ++++++++++++----------
include/utils.h | 4 +++-
t/02-full.c | 8 ++++----
worker/worker.c | 4 ++--
5 files changed, 22 insertions(+), 18 deletions(-)
diff --git a/common/check_utils.c b/common/check_utils.c
index b32c42a..73620e9 100644
--- a/common/check_utils.c
+++ b/common/check_utils.c
@@ -451,7 +451,7 @@ void kill_child_checks(void) {
signal(SIGINT, SIG_DFL);
return;
}
- if(pid_alive(current_child_pid)) {
+ if(pid_alive(current_child_pid, TRUE)) {
gm_log( GM_LOG_TRACE, "kill_child_checks(): send SIGKILL to %d\n", current_child_pid);
kill(current_child_pid, SIGKILL);
}
diff --git a/common/utils.c b/common/utils.c
index a4c88ed..3276cd1 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -1212,9 +1212,8 @@ int free_job(gm_job_t *job) {
return(GM_OK);
}
-
/* verify if a pid is alive */
-int pid_alive(int pid) {
+int pid_alive(int pid, int with_child_check) {
int status;
if(pid < 0) { pid = -pid; }
@@ -1225,14 +1224,17 @@ int pid_alive(int pid) {
/* send kill 0 to verify the process still exists */
if(kill(pid, 0) == 0) {
- if(waitpid(pid, &status, WNOHANG) == -1) {
- perror("waitpid");
- }
- if(WIFEXITED(status)) {
- return FALSE;
- }
- if(WIFSIGNALED(status) && (WTERMSIG(status) == SIGINT || WTERMSIG(status) == SIGQUIT)) {
- return FALSE;
+ // waitpid is only available for child processes
+ if(with_child_check) {
+ if(waitpid(pid, &status, WNOHANG) == -1) {
+ perror("waitpid");
+ }
+ if(WIFEXITED(status)) {
+ return FALSE;
+ }
+ if(WIFSIGNALED(status) && (WTERMSIG(status) == SIGINT || WTERMSIG(status) == SIGQUIT)) {
+ return FALSE;
+ }
}
return TRUE;
diff --git a/include/utils.h b/include/utils.h
index 32aaaa0..e4ab491 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -307,16 +307,18 @@ int set_default_job(gm_job_t *job, mod_gm_opt_t *mod_gm_opt);
*/
int free_job(gm_job_t *job);
+
/**
* pid_alive
*
* check if a pid is alive
*
* @param[in] pid - pid to check
+ * @param[in] with_child_check - check child by waitpid too
*
* @return true if pid is alive
*/
-int pid_alive(int pid);
+int pid_alive(int pid, int with_child_check);
/**
* escapestring
diff --git a/t/02-full.c b/t/02-full.c
index 41d660f..304ba0a 100644
--- a/t/02-full.c
+++ b/t/02-full.c
@@ -383,7 +383,7 @@ int main (int argc, char **argv, char **env) {
diag("make sure gearmand is in your PATH. Common locations are /usr/sbin or /usr/local/sbin");
exit( EXIT_FAILURE );
}
- if(!ok(pid_alive(gearmand_pid) == TRUE, "gearmand alive")) {
+ if(!ok(pid_alive(gearmand_pid, FALSE) == TRUE, "gearmand alive")) {
check_logfile("/tmp/gearmand.log", 3);
kill(gearmand_pid, SIGTERM);
kill(worker_pid, SIGTERM);
@@ -391,7 +391,7 @@ int main (int argc, char **argv, char **env) {
}
if(!ok(worker_pid > 0, "worker started with pid: %d", worker_pid))
diag("could not start worker");
- if(!ok(pid_alive(worker_pid) == TRUE, "worker alive")) {
+ if(!ok(pid_alive(worker_pid, FALSE) == TRUE, "worker alive")) {
check_logfile(worker_logfile, 3);
kill(gearmand_pid, SIGTERM);
kill(worker_pid, SIGTERM);
@@ -521,7 +521,7 @@ int main (int argc, char **argv, char **env) {
/* wait 5 seconds to shutdown */
for(i=0;i<=5;i++) {
waitpid(gearmand_pid, &status, WNOHANG);
- if(pid_alive(gearmand_pid) == FALSE) {
+ if(pid_alive(gearmand_pid, FALSE) == FALSE) {
todo();
ok(status == 0, "gearmand exited with: %d", real_exit_code(status));
endtodo;
@@ -530,7 +530,7 @@ int main (int argc, char **argv, char **env) {
sleep(1);
}
- if(pid_alive(gearmand_pid) == TRUE) {
+ if(pid_alive(gearmand_pid, FALSE) == TRUE) {
/* kill it the hard way */
kill(gearmand_pid, SIGTERM);
waitpid(gearmand_pid, &status, 0);
diff --git a/worker/worker.c b/worker/worker.c
index 532126a..43e0276 100644
--- a/worker/worker.c
+++ b/worker/worker.c
@@ -202,7 +202,7 @@ void count_current_worker(int restart) {
*/
/* check if status worker died */
- if( shm[SHM_STATUS_WORKER_PID] != -1 && pid_alive(shm[SHM_STATUS_WORKER_PID]) == FALSE ) {
+ if( shm[SHM_STATUS_WORKER_PID] != -1 && pid_alive(shm[SHM_STATUS_WORKER_PID], TRUE) == FALSE ) {
gm_log( GM_LOG_TRACE, "removed stale status worker, old pid: %d\n", shm[SHM_STATUS_WORKER_PID] );
shm[SHM_STATUS_WORKER_PID] = -1;
}
@@ -214,7 +214,7 @@ void count_current_worker(int restart) {
for(x=SHM_SHIFT; x < mod_gm_opt->max_worker+SHM_SHIFT; x++) {
/* verify worker is alive */
gm_log( GM_LOG_TRACE3, "worker slot: shm[%d] = %d\n", x, shm[x]);
- if( shm[x] != -1 && pid_alive(shm[x]) == FALSE ) {
+ if( shm[x] != -1 && pid_alive(shm[x], TRUE) == FALSE ) {
gm_log( GM_LOG_TRACE, "removed stale worker %d, old pid: %d\n", x, shm[x]);
shm[x] = -1;
/* immediately start new worker, otherwise the fork rate cannot be guaranteed */
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-nagios/pkg-mod-gearman
More information about the Pkg-nagios-changes
mailing list