diff options
author | Tilghman Lesher <tilghman@meg.abyt.es> | 2008-04-16 22:57:54 +0000 |
---|---|---|
committer | Tilghman Lesher <tilghman@meg.abyt.es> | 2008-04-16 22:57:54 +0000 |
commit | 123ac5fd646cb7a0ed3b7055cda609db93c20487 (patch) | |
tree | 6dea5b8c8d8bb81a47195b75172db608ff217382 /apps | |
parent | 752f6681b1ea748a1acef2a55e17c4c564715fde (diff) |
Standardized routines for forking processes (keeps all the specialized code in one place).
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@114188 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'apps')
-rw-r--r-- | apps/app_externalivr.c | 14 | ||||
-rw-r--r-- | apps/app_festival.c | 16 | ||||
-rw-r--r-- | apps/app_ices.c | 20 | ||||
-rw-r--r-- | apps/app_mp3.c | 20 | ||||
-rw-r--r-- | apps/app_nbscat.c | 22 | ||||
-rw-r--r-- | apps/app_zapras.c | 19 |
6 files changed, 26 insertions, 85 deletions
diff --git a/apps/app_externalivr.c b/apps/app_externalivr.c index b01253d5c..101237680 100644 --- a/apps/app_externalivr.c +++ b/apps/app_externalivr.c @@ -319,14 +319,10 @@ static int app_exec(struct ast_channel *chan, void *data) .finishlist = AST_LIST_HEAD_INIT_VALUE, }; struct ivr_localuser *u = &foo; - sigset_t fullset, oldset; AST_DECLARE_APP_ARGS(args, AST_APP_ARG(cmd)[32]; ); - sigfillset(&fullset); - pthread_sigmask(SIG_BLOCK, &fullset, &oldset); - u->abort_current_sound = 0; u->chan = chan; @@ -405,7 +401,7 @@ static int app_exec(struct ast_channel *chan, void *data) gen_active = 1; } - pid = fork(); + pid = ast_safe_fork(0); if (pid < 0) { ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno)); goto exit; @@ -413,19 +409,13 @@ static int app_exec(struct ast_channel *chan, void *data) if (!pid) { /* child process */ - int i; - - signal(SIGPIPE, SIG_DFL); - pthread_sigmask(SIG_UNBLOCK, &fullset, NULL); - if (ast_opt_high_priority) ast_set_priority(0); dup2(child_stdin[0], STDIN_FILENO); dup2(child_stdout[1], STDOUT_FILENO); dup2(child_stderr[1], STDERR_FILENO); - for (i = STDERR_FILENO + 1; i < 1024; i++) - close(i); + ast_close_fds_above_n(STDERR_FILENO); execv(args.cmd[0], args.cmd); fprintf(stderr, "Failed to execute '%s': %s\n", args.cmd[0], strerror(errno)); _exit(1); diff --git a/apps/app_festival.c b/apps/app_festival.c index e788291ed..096b34c25 100644 --- a/apps/app_festival.c +++ b/apps/app_festival.c @@ -117,30 +117,20 @@ static char *socket_receive_file_to_buff(int fd, int *size) static int send_waveform_to_fd(char *waveform, int length, int fd) { int res; - int x; #ifdef __PPC__ char c; #endif - sigset_t fullset, oldset; - sigfillset(&fullset); - pthread_sigmask(SIG_BLOCK, &fullset, &oldset); - - res = fork(); + res = ast_safe_fork(0); if (res < 0) ast_log(LOG_WARNING, "Fork failed\n"); if (res) { - pthread_sigmask(SIG_SETMASK, &oldset, NULL); return res; } - for (x = 0; x < 256; x++) { - if (x != fd) - close(x); - } + dup2(fd, 0); + ast_close_fds_above_n(0); if (ast_opt_high_priority) ast_set_priority(0); - signal(SIGPIPE, SIG_DFL); - pthread_sigmask(SIG_UNBLOCK, &fullset, NULL); #ifdef __PPC__ for (x = 0; x < length; x += 2) { c = *(waveform + x + 1); diff --git a/apps/app_ices.c b/apps/app_ices.c index b2a4e9b91..0a011b3b3 100644 --- a/apps/app_ices.c +++ b/apps/app_ices.c @@ -43,6 +43,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/pbx.h" #include "asterisk/module.h" #include "asterisk/translate.h" +#include "asterisk/app.h" #define ICES "/usr/bin/ices" #define LOCAL_ICES "/usr/local/bin/ices" @@ -60,31 +61,18 @@ static char *descrip = static int icesencode(char *filename, int fd) { int res; - int x; - sigset_t fullset, oldset; - sigfillset(&fullset); - pthread_sigmask(SIG_BLOCK, &fullset, &oldset); - - res = fork(); + res = ast_safe_fork(0); if (res < 0) ast_log(LOG_WARNING, "Fork failed\n"); if (res) { - pthread_sigmask(SIG_SETMASK, &oldset, NULL); return res; } - /* Stop ignoring PIPE */ - signal(SIGPIPE, SIG_DFL); - pthread_sigmask(SIG_UNBLOCK, &fullset, NULL); - if (ast_opt_high_priority) ast_set_priority(0); dup2(fd, STDIN_FILENO); - for (x=STDERR_FILENO + 1;x<1024;x++) { - if ((x != STDIN_FILENO) && (x != STDOUT_FILENO)) - close(x); - } + ast_close_fds_above_n(STDERR_FILENO); /* Most commonly installed in /usr/local/bin */ execl(ICES, "ices", filename, (char *)NULL); /* But many places has it in /usr/bin */ @@ -183,7 +171,7 @@ static int ices_exec(struct ast_channel *chan, void *data) } } close(fds[1]); - + if (pid > -1) kill(pid, SIGKILL); if (!res && oreadformat) diff --git a/apps/app_mp3.c b/apps/app_mp3.c index d2f2f5c0e..33bee620a 100644 --- a/apps/app_mp3.c +++ b/apps/app_mp3.c @@ -39,6 +39,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/pbx.h" #include "asterisk/module.h" #include "asterisk/translate.h" +#include "asterisk/app.h" #define LOCAL_MPG_123 "/usr/local/bin/mpg123" #define MPG_123 "/usr/bin/mpg123" @@ -56,29 +57,19 @@ static char *descrip = static int mp3play(char *filename, int fd) { int res; - int x; - sigset_t fullset, oldset; - sigfillset(&fullset); - pthread_sigmask(SIG_BLOCK, &fullset, &oldset); - - res = fork(); + res = ast_safe_fork(0); if (res < 0) ast_log(LOG_WARNING, "Fork failed\n"); if (res) { - pthread_sigmask(SIG_SETMASK, &oldset, NULL); return res; } if (ast_opt_high_priority) ast_set_priority(0); - signal(SIGPIPE, SIG_DFL); - pthread_sigmask(SIG_UNBLOCK, &fullset, NULL); dup2(fd, STDOUT_FILENO); - for (x=STDERR_FILENO + 1;x<256;x++) { - if (x != STDOUT_FILENO) - close(x); - } + ast_close_fds_above_n(STDERR_FILENO); + /* Execute mpg123, but buffer if it's a net connection */ if (!strncasecmp(filename, "http://", 7)) { /* Most commonly installed in /usr/local/bin */ @@ -96,7 +87,8 @@ static int mp3play(char *filename, int fd) /* As a last-ditch effort, try to use PATH */ execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL); } - ast_log(LOG_WARNING, "Execute of mpg123 failed\n"); + /* Can't use ast_log since FD's are closed */ + fprintf(stderr, "Execute of mpg123 failed\n"); _exit(0); } diff --git a/apps/app_nbscat.c b/apps/app_nbscat.c index 4e7203319..f47129674 100644 --- a/apps/app_nbscat.c +++ b/apps/app_nbscat.c @@ -41,6 +41,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/pbx.h" #include "asterisk/module.h" #include "asterisk/translate.h" +#include "asterisk/app.h" #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k" #define NBSCAT "/usr/bin/nbscat8k" @@ -61,34 +62,25 @@ static char *descrip = static int NBScatplay(int fd) { int res; - int x; - sigset_t fullset, oldset; - sigfillset(&fullset); - pthread_sigmask(SIG_BLOCK, &fullset, &oldset); - - res = fork(); - if (res < 0) + res = ast_safe_fork(0); + if (res < 0) { ast_log(LOG_WARNING, "Fork failed\n"); + } + if (res) { - pthread_sigmask(SIG_SETMASK, &oldset, NULL); return res; } - signal(SIGPIPE, SIG_DFL); - pthread_sigmask(SIG_UNBLOCK, &fullset, NULL); if (ast_opt_high_priority) ast_set_priority(0); dup2(fd, STDOUT_FILENO); - for (x = STDERR_FILENO + 1; x < 1024; x++) { - if (x != STDOUT_FILENO) - close(x); - } + ast_close_fds_above_n(STDERR_FILENO); /* Most commonly installed in /usr/local/bin */ execl(NBSCAT, "nbscat8k", "-d", (char *)NULL); execl(LOCAL_NBSCAT, "nbscat8k", "-d", (char *)NULL); - ast_log(LOG_WARNING, "Execute of nbscat8k failed\n"); + fprintf(stderr, "Execute of nbscat8k failed\n"); _exit(0); } diff --git a/apps/app_zapras.c b/apps/app_zapras.c index 231a9f2a6..6e079e32d 100644 --- a/apps/app_zapras.c +++ b/apps/app_zapras.c @@ -50,6 +50,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/channel.h" #include "asterisk/pbx.h" #include "asterisk/module.h" +#include "asterisk/app.h" static char *app = "ZapRAS"; @@ -69,30 +70,18 @@ static char *descrip = static pid_t spawn_ras(struct ast_channel *chan, char *args) { pid_t pid; - int x; char *c; char *argv[PPP_MAX_ARGS]; int argc = 0; char *stringp=NULL; - sigset_t fullset, oldset; - - sigfillset(&fullset); - pthread_sigmask(SIG_BLOCK, &fullset, &oldset); /* Start by forking */ - pid = fork(); + pid = ast_safe_fork(1); if (pid) { - pthread_sigmask(SIG_SETMASK, &oldset, NULL); return pid; } - /* Restore original signal handlers */ - for (x=0;x<NSIG;x++) - signal(x, SIG_DFL); - - pthread_sigmask(SIG_UNBLOCK, &fullset, NULL); - /* Execute RAS on File handles */ dup2(chan->fds[0], STDIN_FILENO); @@ -101,8 +90,7 @@ static pid_t spawn_ras(struct ast_channel *chan, char *args) ast_set_priority(0); /* Close other file descriptors */ - for (x=STDERR_FILENO + 1;x<1024;x++) - close(x); + ast_close_fds_above_n(STDERR_FILENO); /* Reset all arguments */ memset(argv, 0, sizeof(argv)); @@ -185,6 +173,7 @@ static void run_ras(struct ast_channel *chan, char *args) break; } } + ast_safe_fork_cleanup(); } static int zapras_exec(struct ast_channel *chan, void *data) |