.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;
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;
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);
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);
#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"
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 */
}
}
close(fds[1]);
-
+
if (pid > -1)
kill(pid, SIGKILL);
if (!res && oreadformat)
#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"
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 */
/* 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);
}
#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"
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);
}
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
+#include "asterisk/app.h"
static char *app = "ZapRAS";
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);
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));
break;
}
}
+ ast_safe_fork_cleanup();
}
static int zapras_exec(struct ast_channel *chan, void *data)
/*! \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
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));
#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"
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();
+}
+
int ast_safe_system(const char *s)
{
pid_t pid;
-#ifdef HAVE_WORKING_FORK
- int x;
-#endif
int res;
struct rusage rusage;
int status;
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);
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;
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))
}
}
- /* 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) {
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];
close(fds[1]);
if (efd > -1)
close(efd);
- ast_unreplace_sigchld();
}
+ ast_safe_fork_cleanup();
switch (res) {
case AGI_RESULT_SUCCESS:
int argc = 0;
DIR *dir = NULL;
struct dirent *de;
- sigset_t signal_set, old_set;
if (!strcasecmp(class->dir, "nodir")) {
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]);
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)) {
/* 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];