From 4ea36af0418d7f6592c33669432404f8251d5556 Mon Sep 17 00:00:00 2001 From: Grzegorz Nosek Date: Sun, 3 Feb 2013 13:32:17 +0100 Subject: Fix 403 error handling Report 403 errors over normal stdout/stderr (after setting up the pipes). Properly reporting the error response over stdout requires: - flushing the I/O, which would otherwise get buffered - skipping atexit handlers (would otherwise close the FCGI connection cleanly, interfering with the parent process still trying to talk over it) --- fcgiwrap.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/fcgiwrap.c b/fcgiwrap.c index 4ec75bc..e5b00ab 100644 --- a/fcgiwrap.c +++ b/fcgiwrap.c @@ -487,14 +487,15 @@ static void inherit_environment(void) static void error_403(const char *reason, const char *filename) { - FCGI_fputs("Status: 403 Forbidden\nContent-type: text/plain\n\n403", FCGI_stdout); + puts("Status: 403 Forbidden\nContent-type: text/plain\n\n403"); + fflush(stdout); if (filename) { - FCGI_fprintf(FCGI_stderr, "%s (%s)\n", reason, filename); + fprintf(stderr, "%s (%s)\n", reason, filename); } else { - FCGI_fputs(reason, FCGI_stderr); - FCGI_fputc('\n', FCGI_stderr); + fputs(reason, stderr); + fputc('\n', stderr); } - exit(99); + _exit(99); } static void handle_fcgi_request(void) @@ -517,6 +518,21 @@ static void handle_fcgi_request(void) goto err_fork; case 0: /* child */ + close(pipe_in[1]); + close(pipe_out[0]); + close(pipe_err[0]); + + dup2(pipe_in[0], 0); + dup2(pipe_out[1], 1); + dup2(pipe_err[1], 2); + + close(pipe_in[0]); + close(pipe_out[1]); + close(pipe_err[1]); + + signal(SIGCHLD, SIG_DFL); + signal(SIGPIPE, SIG_DFL); + filename = get_cgi_filename(); inherit_environment(); if (!filename) @@ -532,20 +548,6 @@ static void handle_fcgi_request(void) *last_slash = '/'; - close(pipe_in[1]); - close(pipe_out[0]); - close(pipe_err[0]); - - dup2(pipe_in[0], 0); - dup2(pipe_out[1], 1); - dup2(pipe_err[1], 2); - - close(pipe_in[0]); - close(pipe_out[1]); - close(pipe_err[1]); - - signal(SIGCHLD, SIG_DFL); - signal(SIGPIPE, SIG_DFL); execl(filename, filename, (void *)NULL); puts("Status: 502 Bad Gateway\nContent-type: text/plain\n\n502"); exit(99); -- cgit v1.2.3