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
This commit is contained in:
Tilghman Lesher
2008-04-16 22:57:54 +00:00
parent 752f6681b1
commit 123ac5fd64
13 changed files with 163 additions and 137 deletions

View File

@@ -319,14 +319,10 @@ static int app_exec(struct ast_channel *chan, void *data)
.finishlist = AST_LIST_HEAD_INIT_VALUE, .finishlist = AST_LIST_HEAD_INIT_VALUE,
}; };
struct ivr_localuser *u = &foo; struct ivr_localuser *u = &foo;
sigset_t fullset, oldset;
AST_DECLARE_APP_ARGS(args, AST_DECLARE_APP_ARGS(args,
AST_APP_ARG(cmd)[32]; AST_APP_ARG(cmd)[32];
); );
sigfillset(&fullset);
pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
u->abort_current_sound = 0; u->abort_current_sound = 0;
u->chan = chan; u->chan = chan;
@@ -405,7 +401,7 @@ static int app_exec(struct ast_channel *chan, void *data)
gen_active = 1; gen_active = 1;
} }
pid = fork(); pid = ast_safe_fork(0);
if (pid < 0) { if (pid < 0) {
ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno)); ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno));
goto exit; goto exit;
@@ -413,19 +409,13 @@ static int app_exec(struct ast_channel *chan, void *data)
if (!pid) { if (!pid) {
/* child process */ /* child process */
int i;
signal(SIGPIPE, SIG_DFL);
pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
if (ast_opt_high_priority) if (ast_opt_high_priority)
ast_set_priority(0); ast_set_priority(0);
dup2(child_stdin[0], STDIN_FILENO); dup2(child_stdin[0], STDIN_FILENO);
dup2(child_stdout[1], STDOUT_FILENO); dup2(child_stdout[1], STDOUT_FILENO);
dup2(child_stderr[1], STDERR_FILENO); dup2(child_stderr[1], STDERR_FILENO);
for (i = STDERR_FILENO + 1; i < 1024; i++) ast_close_fds_above_n(STDERR_FILENO);
close(i);
execv(args.cmd[0], args.cmd); execv(args.cmd[0], args.cmd);
fprintf(stderr, "Failed to execute '%s': %s\n", args.cmd[0], strerror(errno)); fprintf(stderr, "Failed to execute '%s': %s\n", args.cmd[0], strerror(errno));
_exit(1); _exit(1);

View File

@@ -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) static int send_waveform_to_fd(char *waveform, int length, int fd)
{ {
int res; int res;
int x;
#ifdef __PPC__ #ifdef __PPC__
char c; char c;
#endif #endif
sigset_t fullset, oldset;
sigfillset(&fullset); res = ast_safe_fork(0);
pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
res = fork();
if (res < 0) if (res < 0)
ast_log(LOG_WARNING, "Fork failed\n"); ast_log(LOG_WARNING, "Fork failed\n");
if (res) { if (res) {
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
return res; return res;
} }
for (x = 0; x < 256; x++) { dup2(fd, 0);
if (x != fd) ast_close_fds_above_n(0);
close(x);
}
if (ast_opt_high_priority) if (ast_opt_high_priority)
ast_set_priority(0); ast_set_priority(0);
signal(SIGPIPE, SIG_DFL);
pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
#ifdef __PPC__ #ifdef __PPC__
for (x = 0; x < length; x += 2) { for (x = 0; x < length; x += 2) {
c = *(waveform + x + 1); c = *(waveform + x + 1);

View File

@@ -43,6 +43,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/pbx.h" #include "asterisk/pbx.h"
#include "asterisk/module.h" #include "asterisk/module.h"
#include "asterisk/translate.h" #include "asterisk/translate.h"
#include "asterisk/app.h"
#define ICES "/usr/bin/ices" #define ICES "/usr/bin/ices"
#define LOCAL_ICES "/usr/local/bin/ices" #define LOCAL_ICES "/usr/local/bin/ices"
@@ -60,31 +61,18 @@ static char *descrip =
static int icesencode(char *filename, int fd) static int icesencode(char *filename, int fd)
{ {
int res; int res;
int x;
sigset_t fullset, oldset;
sigfillset(&fullset); res = ast_safe_fork(0);
pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
res = fork();
if (res < 0) if (res < 0)
ast_log(LOG_WARNING, "Fork failed\n"); ast_log(LOG_WARNING, "Fork failed\n");
if (res) { if (res) {
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
return res; return res;
} }
/* Stop ignoring PIPE */
signal(SIGPIPE, SIG_DFL);
pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
if (ast_opt_high_priority) if (ast_opt_high_priority)
ast_set_priority(0); ast_set_priority(0);
dup2(fd, STDIN_FILENO); dup2(fd, STDIN_FILENO);
for (x=STDERR_FILENO + 1;x<1024;x++) { ast_close_fds_above_n(STDERR_FILENO);
if ((x != STDIN_FILENO) && (x != STDOUT_FILENO))
close(x);
}
/* Most commonly installed in /usr/local/bin */ /* Most commonly installed in /usr/local/bin */
execl(ICES, "ices", filename, (char *)NULL); execl(ICES, "ices", filename, (char *)NULL);
/* But many places has it in /usr/bin */ /* But many places has it in /usr/bin */

View File

@@ -39,6 +39,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/pbx.h" #include "asterisk/pbx.h"
#include "asterisk/module.h" #include "asterisk/module.h"
#include "asterisk/translate.h" #include "asterisk/translate.h"
#include "asterisk/app.h"
#define LOCAL_MPG_123 "/usr/local/bin/mpg123" #define LOCAL_MPG_123 "/usr/local/bin/mpg123"
#define MPG_123 "/usr/bin/mpg123" #define MPG_123 "/usr/bin/mpg123"
@@ -56,29 +57,19 @@ static char *descrip =
static int mp3play(char *filename, int fd) static int mp3play(char *filename, int fd)
{ {
int res; int res;
int x;
sigset_t fullset, oldset;
sigfillset(&fullset); res = ast_safe_fork(0);
pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
res = fork();
if (res < 0) if (res < 0)
ast_log(LOG_WARNING, "Fork failed\n"); ast_log(LOG_WARNING, "Fork failed\n");
if (res) { if (res) {
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
return res; return res;
} }
if (ast_opt_high_priority) if (ast_opt_high_priority)
ast_set_priority(0); ast_set_priority(0);
signal(SIGPIPE, SIG_DFL);
pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
dup2(fd, STDOUT_FILENO); dup2(fd, STDOUT_FILENO);
for (x=STDERR_FILENO + 1;x<256;x++) { ast_close_fds_above_n(STDERR_FILENO);
if (x != STDOUT_FILENO)
close(x);
}
/* Execute mpg123, but buffer if it's a net connection */ /* Execute mpg123, but buffer if it's a net connection */
if (!strncasecmp(filename, "http://", 7)) { if (!strncasecmp(filename, "http://", 7)) {
/* Most commonly installed in /usr/local/bin */ /* 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 */ /* As a last-ditch effort, try to use PATH */
execlp("mpg123", "mpg123", "-q", "-s", "-f", "8192", "--mono", "-r", "8000", filename, (char *)NULL); 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); _exit(0);
} }

View File

@@ -41,6 +41,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/pbx.h" #include "asterisk/pbx.h"
#include "asterisk/module.h" #include "asterisk/module.h"
#include "asterisk/translate.h" #include "asterisk/translate.h"
#include "asterisk/app.h"
#define LOCAL_NBSCAT "/usr/local/bin/nbscat8k" #define LOCAL_NBSCAT "/usr/local/bin/nbscat8k"
#define NBSCAT "/usr/bin/nbscat8k" #define NBSCAT "/usr/bin/nbscat8k"
@@ -61,34 +62,25 @@ static char *descrip =
static int NBScatplay(int fd) static int NBScatplay(int fd)
{ {
int res; int res;
int x;
sigset_t fullset, oldset;
sigfillset(&fullset); res = ast_safe_fork(0);
pthread_sigmask(SIG_BLOCK, &fullset, &oldset); if (res < 0) {
res = fork();
if (res < 0)
ast_log(LOG_WARNING, "Fork failed\n"); ast_log(LOG_WARNING, "Fork failed\n");
}
if (res) { if (res) {
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
return res; return res;
} }
signal(SIGPIPE, SIG_DFL);
pthread_sigmask(SIG_UNBLOCK, &fullset, NULL);
if (ast_opt_high_priority) if (ast_opt_high_priority)
ast_set_priority(0); ast_set_priority(0);
dup2(fd, STDOUT_FILENO); dup2(fd, STDOUT_FILENO);
for (x = STDERR_FILENO + 1; x < 1024; x++) { ast_close_fds_above_n(STDERR_FILENO);
if (x != STDOUT_FILENO)
close(x);
}
/* Most commonly installed in /usr/local/bin */ /* Most commonly installed in /usr/local/bin */
execl(NBSCAT, "nbscat8k", "-d", (char *)NULL); execl(NBSCAT, "nbscat8k", "-d", (char *)NULL);
execl(LOCAL_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); _exit(0);
} }

View File

@@ -50,6 +50,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/channel.h" #include "asterisk/channel.h"
#include "asterisk/pbx.h" #include "asterisk/pbx.h"
#include "asterisk/module.h" #include "asterisk/module.h"
#include "asterisk/app.h"
static char *app = "ZapRAS"; static char *app = "ZapRAS";
@@ -69,30 +70,18 @@ static char *descrip =
static pid_t spawn_ras(struct ast_channel *chan, char *args) static pid_t spawn_ras(struct ast_channel *chan, char *args)
{ {
pid_t pid; pid_t pid;
int x;
char *c; char *c;
char *argv[PPP_MAX_ARGS]; char *argv[PPP_MAX_ARGS];
int argc = 0; int argc = 0;
char *stringp=NULL; char *stringp=NULL;
sigset_t fullset, oldset;
sigfillset(&fullset);
pthread_sigmask(SIG_BLOCK, &fullset, &oldset);
/* Start by forking */ /* Start by forking */
pid = fork(); pid = ast_safe_fork(1);
if (pid) { if (pid) {
pthread_sigmask(SIG_SETMASK, &oldset, NULL);
return pid; 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 */ /* Execute RAS on File handles */
dup2(chan->fds[0], STDIN_FILENO); 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); ast_set_priority(0);
/* Close other file descriptors */ /* Close other file descriptors */
for (x=STDERR_FILENO + 1;x<1024;x++) ast_close_fds_above_n(STDERR_FILENO);
close(x);
/* Reset all arguments */ /* Reset all arguments */
memset(argv, 0, sizeof(argv)); memset(argv, 0, sizeof(argv));
@@ -185,6 +173,7 @@ static void run_ras(struct ast_channel *chan, char *args)
break; break;
} }
} }
ast_safe_fork_cleanup();
} }
static int zapras_exec(struct ast_channel *chan, void *data) static int zapras_exec(struct ast_channel *chan, void *data)

View File

@@ -480,6 +480,15 @@ int ast_record_review(struct ast_channel *chan, const char *playfile, const char
/*! \brief Decode an encoded control or extended ASCII character */ /*! \brief Decode an encoded control or extended ASCII character */
int ast_get_encoded_char(const char *stream, char *result, size_t *consumed); int ast_get_encoded_char(const char *stream, char *result, size_t *consumed);
/*! \brief Common routine for child processes, to close all fds prior to exec(2) */
void ast_close_fds_above_n(int n);
/*! \brief Common routine to safely fork without a chance of a signal handler firing badly in the child */
int ast_safe_fork(int stop_reaper);
/*! \brief Common routine to cleanup after fork'ed process is complete (if reaping was stopped) */
void ast_safe_fork_cleanup(void);
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
} }
#endif #endif

View File

@@ -78,6 +78,9 @@ void ast_queue_log(const char *queuename, const char *callid, const char *agent,
void ast_verbose(const char *fmt, ...) void ast_verbose(const char *fmt, ...)
__attribute__ ((format (printf, 1, 2))); __attribute__ ((format (printf, 1, 2)));
void ast_child_verbose(int level, const char *fmt, ...)
__attribute__ ((format (printf, 2, 3)));
int ast_register_verbose(void (*verboser)(const char *string)); int ast_register_verbose(void (*verboser)(const char *string));
int ast_unregister_verbose(void (*verboser)(const char *string)); int ast_unregister_verbose(void (*verboser)(const char *string));

View File

@@ -32,6 +32,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#endif #endif
#include <regex.h> #include <regex.h>
#include <sys/file.h> /* added this to allow to compile, sorry! */ #include <sys/file.h> /* added this to allow to compile, sorry! */
#include <signal.h>
#include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */ #include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */
#include "asterisk/channel.h" #include "asterisk/channel.h"
@@ -1759,3 +1760,65 @@ int ast_get_encoded_char(const char *stream, char *result, size_t *consumed)
return 0; return 0;
} }
void ast_close_fds_above_n(int n)
{
int x, null;
null = open("/dev/null", O_RDONLY);
for (x = n + 1; x <= (null >= 8192 ? null : 8192); x++) {
if (x != null) {
/* Side effect of dup2 is that it closes any existing fd without error.
* This prevents valgrind and other debugging tools from sending up
* false error reports. */
dup2(null, x);
close(x);
}
}
close(null);
}
int ast_safe_fork(int stop_reaper)
{
sigset_t signal_set, old_set;
int pid;
/* Don't let the default signal handler for children reap our status */
if (stop_reaper) {
ast_replace_sigchld();
}
sigfillset(&signal_set);
pthread_sigmask(SIG_BLOCK, &signal_set, &old_set);
pid = fork();
if (pid != 0) {
/* Fork failed or parent */
pthread_sigmask(SIG_SETMASK, &old_set, NULL);
return pid;
} else {
/* Child */
/* Before we unblock our signals, return our trapped signals back to the defaults */
signal(SIGHUP, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
signal(SIGINT, SIG_DFL);
signal(SIGURG, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
signal(SIGXFSZ, SIG_DFL);
/* unblock important signal handlers */
if (pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL)) {
ast_log(LOG_WARNING, "unable to unblock signals for AGI script: %s\n", strerror(errno));
_exit(1);
}
return pid;
}
}
void ast_safe_fork_cleanup(void)
{
ast_unreplace_sigchld();
}

View File

@@ -848,9 +848,6 @@ void ast_unreplace_sigchld(void)
int ast_safe_system(const char *s) int ast_safe_system(const char *s)
{ {
pid_t pid; pid_t pid;
#ifdef HAVE_WORKING_FORK
int x;
#endif
int res; int res;
struct rusage rusage; struct rusage rusage;
int status; int status;
@@ -869,8 +866,7 @@ int ast_safe_system(const char *s)
if (ast_opt_high_priority) if (ast_opt_high_priority)
ast_set_priority(0); ast_set_priority(0);
/* Close file descriptors and launch system command */ /* Close file descriptors and launch system command */
for (x = STDERR_FILENO + 1; x < 4096; x++) ast_close_fds_above_n(STDERR_FILENO);
close(x);
#endif #endif
execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL); execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
_exit(1); _exit(1);

View File

@@ -413,6 +413,55 @@ static void init_logger_chain(int reload, int locked)
ast_config_destroy(cfg); ast_config_destroy(cfg);
} }
void ast_child_verbose(int level, const char *fmt, ...)
{
char *msg = NULL, *emsg = NULL, *sptr, *eptr;
va_list ap, aq;
int size;
/* Don't bother, if the level isn't that high */
if (option_verbose < level) {
return;
}
va_start(ap, fmt);
va_copy(aq, ap);
if ((size = vsnprintf(msg, 0, fmt, ap)) < 0) {
va_end(ap);
va_end(aq);
return;
}
va_end(ap);
if (!(msg = ast_malloc(size + 1))) {
va_end(aq);
return;
}
vsnprintf(msg, size + 1, fmt, aq);
va_end(aq);
if (!(emsg = ast_malloc(size * 2 + 1))) {
ast_free(msg);
return;
}
for (sptr = msg, eptr = emsg; ; sptr++) {
if (*sptr == '"') {
*eptr++ = '\\';
}
*eptr++ = *sptr;
if (*sptr == '\0') {
break;
}
}
ast_free(msg);
fprintf(stdout, "verbose \"%s\" %d\n", emsg, level);
fflush(stdout);
ast_free(emsg);
}
void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...) void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
{ {
va_list ap; va_list ap;

View File

@@ -603,8 +603,7 @@ static enum agi_result launch_netscript(char *agiurl, char *argv[], int *fds, in
static enum agi_result launch_script(struct ast_channel *chan, char *script, char *argv[], int *fds, int *efd, int *opid) static enum agi_result launch_script(struct ast_channel *chan, char *script, char *argv[], int *fds, int *efd, int *opid)
{ {
char tmp[256]; char tmp[256];
int pid, toast[2], fromast[2], audio[2], x, res; int pid, toast[2], fromast[2], audio[2], res;
sigset_t signal_set, old_set;
struct stat st; struct stat st;
if (!strncasecmp(script, "agi://", 6)) if (!strncasecmp(script, "agi://", 6))
@@ -657,12 +656,8 @@ static enum agi_result launch_script(struct ast_channel *chan, char *script, cha
} }
} }
/* Block SIGHUP during the fork - prevents a race */ if ((pid = ast_safe_fork(1)) < 0) {
sigfillset(&signal_set);
pthread_sigmask(SIG_BLOCK, &signal_set, &old_set);
if ((pid = fork()) < 0) {
ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno)); ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno));
pthread_sigmask(SIG_SETMASK, &old_set, NULL);
return AGI_RESULT_FAILURE; return AGI_RESULT_FAILURE;
} }
if (!pid) { if (!pid) {
@@ -690,34 +685,17 @@ static enum agi_result launch_script(struct ast_channel *chan, char *script, cha
else else
close(STDERR_FILENO + 1); close(STDERR_FILENO + 1);
/* Before we unblock our signals, return our trapped signals back to the defaults */
signal(SIGHUP, SIG_DFL);
signal(SIGCHLD, SIG_DFL);
signal(SIGINT, SIG_DFL);
signal(SIGURG, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGPIPE, SIG_DFL);
signal(SIGXFSZ, SIG_DFL);
/* unblock important signal handlers */
if (pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL)) {
ast_log(LOG_WARNING, "unable to unblock signals for AGI script: %s\n", strerror(errno));
_exit(1);
}
/* Close everything but stdin/out/error */ /* Close everything but stdin/out/error */
for (x = STDERR_FILENO + 2; x < 1024; x++) ast_close_fds_above_n(STDERR_FILENO + 1);
close(x);
/* Execute script */ /* Execute script */
/* XXX argv should be deprecated in favor of passing agi_argX paramaters */ /* XXX argv should be deprecated in favor of passing agi_argX paramaters */
execv(script, argv); execv(script, argv);
/* Can't use ast_log since FD's are closed */ /* Can't use ast_log since FD's are closed */
fprintf(stdout, "verbose \"Failed to execute '%s': %s\" 2\n", script, strerror(errno)); ast_child_verbose(1, "Failed to execute '%s': %s", script, strerror(errno));
fflush(stdout); fflush(stdout);
_exit(1); _exit(1);
} }
pthread_sigmask(SIG_SETMASK, &old_set, NULL);
ast_verb(3, "Launched AGI Script %s\n", script); ast_verb(3, "Launched AGI Script %s\n", script);
fds[0] = toast[0]; fds[0] = toast[0];
fds[1] = fromast[1]; fds[1] = fromast[1];
@@ -2908,8 +2886,8 @@ static int agi_exec_full(struct ast_channel *chan, void *data, int enhanced, int
close(fds[1]); close(fds[1]);
if (efd > -1) if (efd > -1)
close(efd); close(efd);
ast_unreplace_sigchld();
} }
ast_safe_fork_cleanup();
switch (res) { switch (res) {
case AGI_RESULT_SUCCESS: case AGI_RESULT_SUCCESS:

View File

@@ -404,7 +404,6 @@ static int spawn_mp3(struct mohclass *class)
int argc = 0; int argc = 0;
DIR *dir = NULL; DIR *dir = NULL;
struct dirent *de; struct dirent *de;
sigset_t signal_set, old_set;
if (!strcasecmp(class->dir, "nodir")) { if (!strcasecmp(class->dir, "nodir")) {
@@ -490,12 +489,8 @@ static int spawn_mp3(struct mohclass *class)
sleep(respawn_time - (time(NULL) - class->start)); sleep(respawn_time - (time(NULL) - class->start));
} }
/* Block signals during the fork() */
sigfillset(&signal_set);
pthread_sigmask(SIG_BLOCK, &signal_set, &old_set);
time(&class->start); time(&class->start);
class->pid = fork(); class->pid = ast_safe_fork(0);
if (class->pid < 0) { if (class->pid < 0) {
close(fds[0]); close(fds[0]);
close(fds[1]); close(fds[1]);
@@ -503,24 +498,16 @@ static int spawn_mp3(struct mohclass *class)
return -1; return -1;
} }
if (!class->pid) { if (!class->pid) {
int x;
if (ast_opt_high_priority) if (ast_opt_high_priority)
ast_set_priority(0); ast_set_priority(0);
/* Reset ignored signals back to default */
signal(SIGPIPE, SIG_DFL);
pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL);
close(fds[0]); close(fds[0]);
/* Stdout goes to pipe */ /* Stdout goes to pipe */
dup2(fds[1], STDOUT_FILENO); dup2(fds[1], STDOUT_FILENO);
/* Close unused file descriptors */
for (x=3;x<8192;x++) { /* Close everything else */
if (-1 != fcntl(x, F_GETFL)) { ast_close_fds_above_n(STDERR_FILENO);
close(x);
}
}
/* Child */ /* Child */
chdir(class->dir); chdir(class->dir);
if (ast_test_flag(class, MOH_CUSTOM)) { if (ast_test_flag(class, MOH_CUSTOM)) {
@@ -533,12 +520,12 @@ static int spawn_mp3(struct mohclass *class)
/* Check PATH as a last-ditch effort */ /* Check PATH as a last-ditch effort */
execvp("mpg123", argv); execvp("mpg123", argv);
} }
ast_log(LOG_WARNING, "Exec failed: %s\n", strerror(errno)); /* Can't use logger, since log FDs are closed */
fprintf(stderr, "MOH: exec failed: %s\n", strerror(errno));
close(fds[1]); close(fds[1]);
_exit(1); _exit(1);
} else { } else {
/* Parent */ /* Parent */
pthread_sigmask(SIG_SETMASK, &old_set, NULL);
close(fds[1]); close(fds[1]);
} }
return fds[0]; return fds[0];