[asterisk-commits] tilghman: trunk r114188 - in /trunk: apps/ include/asterisk/ main/ res/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Apr 16 17:57:54 CDT 2008


Author: tilghman
Date: Wed Apr 16 17:57:54 2008
New Revision: 114188

URL: http://svn.digium.com/view/asterisk?view=rev&rev=114188
Log:
Standardized routines for forking processes (keeps all the specialized code in one place).

Modified:
    trunk/apps/app_externalivr.c
    trunk/apps/app_festival.c
    trunk/apps/app_ices.c
    trunk/apps/app_mp3.c
    trunk/apps/app_nbscat.c
    trunk/apps/app_zapras.c
    trunk/include/asterisk/app.h
    trunk/include/asterisk/logger.h
    trunk/main/app.c
    trunk/main/asterisk.c
    trunk/main/logger.c
    trunk/res/res_agi.c
    trunk/res/res_musiconhold.c

Modified: trunk/apps/app_externalivr.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_externalivr.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/apps/app_externalivr.c (original)
+++ trunk/apps/app_externalivr.c Wed Apr 16 17:57:54 2008
@@ -319,13 +319,9 @@
 		.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 @@
 			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 @@
 	
 		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);

Modified: trunk/apps/app_festival.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_festival.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/apps/app_festival.c (original)
+++ trunk/apps/app_festival.c Wed Apr 16 17:57:54 2008
@@ -117,30 +117,20 @@
 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);

Modified: trunk/apps/app_ices.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_ices.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/apps/app_ices.c (original)
+++ trunk/apps/app_ices.c Wed Apr 16 17:57:54 2008
@@ -43,6 +43,7 @@
 #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 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 @@
 		}
 	}
 	close(fds[1]);
-	
+
 	if (pid > -1)
 		kill(pid, SIGKILL);
 	if (!res && oreadformat)

Modified: trunk/apps/app_mp3.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_mp3.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/apps/app_mp3.c (original)
+++ trunk/apps/app_mp3.c Wed Apr 16 17:57:54 2008
@@ -39,6 +39,7 @@
 #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 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 @@
 		/* 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);
 }
 

Modified: trunk/apps/app_nbscat.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_nbscat.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/apps/app_nbscat.c (original)
+++ trunk/apps/app_nbscat.c Wed Apr 16 17:57:54 2008
@@ -41,6 +41,7 @@
 #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 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);
 }
 

Modified: trunk/apps/app_zapras.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_zapras.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/apps/app_zapras.c (original)
+++ trunk/apps/app_zapras.c Wed Apr 16 17:57:54 2008
@@ -50,6 +50,7 @@
 #include "asterisk/channel.h"
 #include "asterisk/pbx.h"
 #include "asterisk/module.h"
+#include "asterisk/app.h"
 
 static char *app = "ZapRAS";
 
@@ -69,29 +70,17 @@
 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 @@
 		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 @@
 			break;
 		}
 	}
+	ast_safe_fork_cleanup();
 }
 
 static int zapras_exec(struct ast_channel *chan, void *data)

Modified: trunk/include/asterisk/app.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/app.h?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/include/asterisk/app.h (original)
+++ trunk/include/asterisk/app.h Wed Apr 16 17:57:54 2008
@@ -480,6 +480,15 @@
 /*! \brief Decode an encoded control or extended ASCII character */
 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)
 }
 #endif

Modified: trunk/include/asterisk/logger.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/logger.h?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/include/asterisk/logger.h (original)
+++ trunk/include/asterisk/logger.h Wed Apr 16 17:57:54 2008
@@ -77,6 +77,9 @@
  */
 void ast_verbose(const char *fmt, ...)
 	__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_unregister_verbose(void (*verboser)(const char *string));

Modified: trunk/main/app.c
URL: http://svn.digium.com/view/asterisk/trunk/main/app.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/main/app.c (original)
+++ trunk/main/app.c Wed Apr 16 17:57:54 2008
@@ -32,6 +32,7 @@
 #endif
 #include <regex.h>
 #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/channel.h"
@@ -1759,3 +1760,65 @@
 	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();
+}
+

Modified: trunk/main/asterisk.c
URL: http://svn.digium.com/view/asterisk/trunk/main/asterisk.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/main/asterisk.c (original)
+++ trunk/main/asterisk.c Wed Apr 16 17:57:54 2008
@@ -848,9 +848,6 @@
 int ast_safe_system(const char *s)
 {
 	pid_t pid;
-#ifdef HAVE_WORKING_FORK
-	int x;
-#endif
 	int res;
 	struct rusage rusage;
 	int status;
@@ -869,8 +866,7 @@
 		if (ast_opt_high_priority)
 			ast_set_priority(0);
 		/* Close file descriptors and launch system command */
-		for (x = STDERR_FILENO + 1; x < 4096; x++)
-			close(x);
+		ast_close_fds_above_n(STDERR_FILENO);
 #endif
 		execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
 		_exit(1);

Modified: trunk/main/logger.c
URL: http://svn.digium.com/view/asterisk/trunk/main/logger.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/main/logger.c (original)
+++ trunk/main/logger.c Wed Apr 16 17:57:54 2008
@@ -413,6 +413,55 @@
 	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, ...)
 {
 	va_list ap;

Modified: trunk/res/res_agi.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_agi.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/res/res_agi.c (original)
+++ trunk/res/res_agi.c Wed Apr 16 17:57:54 2008
@@ -603,8 +603,7 @@
 static enum agi_result launch_script(struct ast_channel *chan, char *script, char *argv[], int *fds, int *efd, int *opid)
 {
 	char tmp[256];
-	int pid, toast[2], fromast[2], audio[2], x, res;
-	sigset_t signal_set, old_set;
+	int pid, toast[2], fromast[2], audio[2], res;
 	struct stat st;
 
 	if (!strncasecmp(script, "agi://", 6))
@@ -657,12 +656,8 @@
 		}
 	}
 
-	/* Block SIGHUP during the fork - prevents a race */
-	sigfillset(&signal_set);
-	pthread_sigmask(SIG_BLOCK, &signal_set, &old_set);
-	if ((pid = fork()) < 0) {
+	if ((pid = ast_safe_fork(1)) < 0) {
 		ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno));
-		pthread_sigmask(SIG_SETMASK, &old_set, NULL);
 		return AGI_RESULT_FAILURE;
 	}
 	if (!pid) {
@@ -690,34 +685,17 @@
 		else
 			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 */
-		for (x = STDERR_FILENO + 2; x < 1024; x++) 
-			close(x);
+		ast_close_fds_above_n(STDERR_FILENO + 1);
 
 		/* Execute script */
 		/* XXX argv should be deprecated in favor of passing agi_argX paramaters */
 		execv(script, argv);
 		/* 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);
 		_exit(1);
 	}
-	pthread_sigmask(SIG_SETMASK, &old_set, NULL);
 	ast_verb(3, "Launched AGI Script %s\n", script);
 	fds[0] = toast[0];
 	fds[1] = fromast[1];
@@ -2908,8 +2886,8 @@
 			close(fds[1]);
 		if (efd > -1)
 			close(efd);
-		ast_unreplace_sigchld();
 	} 
+	ast_safe_fork_cleanup();
 
 	switch (res) {
 	case AGI_RESULT_SUCCESS:

Modified: trunk/res/res_musiconhold.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_musiconhold.c?view=diff&rev=114188&r1=114187&r2=114188
==============================================================================
--- trunk/res/res_musiconhold.c (original)
+++ trunk/res/res_musiconhold.c Wed Apr 16 17:57:54 2008
@@ -404,7 +404,6 @@
 	int argc = 0;
 	DIR *dir = NULL;
 	struct dirent *de;
-	sigset_t signal_set, old_set;
 
 	
 	if (!strcasecmp(class->dir, "nodir")) {
@@ -490,12 +489,8 @@
 		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);
-	class->pid = fork();
+	class->pid = ast_safe_fork(0);
 	if (class->pid < 0) {
 		close(fds[0]);
 		close(fds[1]);
@@ -503,24 +498,16 @@
 		return -1;
 	}
 	if (!class->pid) {
-		int x;
-
 		if (ast_opt_high_priority)
 			ast_set_priority(0);
-
-		/* Reset ignored signals back to default */
-		signal(SIGPIPE, SIG_DFL);
-		pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL);
 
 		close(fds[0]);
 		/* Stdout goes to pipe */
 		dup2(fds[1], STDOUT_FILENO);
-		/* Close unused file descriptors */
-		for (x=3;x<8192;x++) {
-			if (-1 != fcntl(x, F_GETFL)) {
-				close(x);
-			}
-		}
+
+		/* Close everything else */
+		ast_close_fds_above_n(STDERR_FILENO);
+
 		/* Child */
 		chdir(class->dir);
 		if (ast_test_flag(class, MOH_CUSTOM)) {
@@ -533,12 +520,12 @@
 			/* Check PATH as a last-ditch effort */
 			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]);
 		_exit(1);
 	} else {
 		/* Parent */
-		pthread_sigmask(SIG_SETMASK, &old_set, NULL);
 		close(fds[1]);
 	}
 	return fds[0];




More information about the asterisk-commits mailing list