From 04a4a825d5e8df3e1e49df6a989a798da16e9064 Mon Sep 17 00:00:00 2001 From: russell Date: Thu, 25 May 2006 18:31:19 +0000 Subject: Add the ability to retrieve the exit code of the forked AGI process. If there is an error executing the AGI script, or the AGI script itself returns a non-zero value, the AGISTATUS variable will now be set to FAILURE instead of SUCCESS. git-svn-id: http://svn.digium.com/svn/asterisk/trunk@30328 f38db490-d61c-443f-a65b-d21fe96a405b --- asterisk.c | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'asterisk.c') diff --git a/asterisk.c b/asterisk.c index 5c1807f86..2ab474ed8 100644 --- a/asterisk.c +++ b/asterisk.c @@ -608,21 +608,15 @@ static void null_sig_handler(int signal) } AST_MUTEX_DEFINE_STATIC(safe_system_lock); +/*! Keep track of how many threads are currently trying to wait*() on + * a child process */ static unsigned int safe_system_level = 0; static void *safe_system_prev_handler; -int ast_safe_system(const char *s) +void ast_replace_sigchld(void) { - pid_t pid; - int x; - int res; - struct rusage rusage; - int status; unsigned int level; - /* keep track of how many ast_safe_system() functions - are running at this moment - */ ast_mutex_lock(&safe_system_lock); level = safe_system_level++; @@ -631,6 +625,31 @@ int ast_safe_system(const char *s) safe_system_prev_handler = signal(SIGCHLD, null_sig_handler); ast_mutex_unlock(&safe_system_lock); +} + +void ast_unreplace_sigchld(void) +{ + unsigned int level; + + ast_mutex_lock(&safe_system_lock); + level = --safe_system_level; + + /* only restore the handler if we are the last one */ + if (level == 0) + signal(SIGCHLD, safe_system_prev_handler); + + ast_mutex_unlock(&safe_system_lock); +} + +int ast_safe_system(const char *s) +{ + pid_t pid; + int x; + int res; + struct rusage rusage; + int status; + + ast_replace_sigchld(); pid = fork(); @@ -656,14 +675,7 @@ int ast_safe_system(const char *s) res = -1; } - ast_mutex_lock(&safe_system_lock); - level = --safe_system_level; - - /* only restore the handler if we are the last one */ - if (level == 0) - signal(SIGCHLD, safe_system_prev_handler); - - ast_mutex_unlock(&safe_system_lock); + ast_unreplace_sigchld(); return res; } -- cgit v1.2.3