Index: res/res_agi.c =================================================================== --- res/res_agi.c (revision 34462) +++ res/res_agi.c (working copy) @@ -38,6 +38,7 @@ #include #include #include +#include #include "asterisk.h" @@ -285,15 +286,6 @@ /* Don't run AGI scripts with realtime priority -- it causes audio stutter */ ast_set_priority(0); - /* Redirect stdin and out, provide enhanced audio channel if desired */ - dup2(fromast[0], STDIN_FILENO); - dup2(toast[1], STDOUT_FILENO); - if (efd) { - dup2(audio[0], STDERR_FILENO + 1); - } else { - close(STDERR_FILENO + 1); - } - /* unblock important signal handlers */ if (sigfillset(&signal_set) || pthread_sigmask(SIG_UNBLOCK, &signal_set, NULL)) { ast_log(LOG_WARNING, "unable to unblock signals for AGI script: %s\n", strerror(errno)); @@ -301,9 +293,24 @@ } /* Close everything but stdin/out/error */ - for (x=STDERR_FILENO + 2;x<1024;x++) - close(x); + for (x = 0; x < 1024; x++) { + if (x != fromast[0] && x != toast[1] && x != STDERR_FILENO && ((efd && x != audio[0]) || (!efd))) + close(x); + } + /* Ensure the next section happens all at once. */ + sched_yield(); + + /* Redirect stdin and out, provide enhanced audio channel if desired */ + dup2(fromast[0], STDIN_FILENO); + close(fromast[0]); + dup2(toast[1], STDOUT_FILENO); + close(toast[1]); + if (efd) { + dup2(audio[0], STDERR_FILENO + 1); + close(audio[0]); + } + /* Execute script */ execv(script, argv); /* Can't use ast_log since FD's are closed */ @@ -328,7 +335,6 @@ *opid = pid; return 0; - } static void setup_env(struct ast_channel *chan, char *request, int fd, int enhanced)