#! /bin/sh /usr/share/dpatch/dpatch-run
## suexec_log_cloexec.dpatch
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Patch to allow suexec to log after a cgi error (#312385)
## DP: http://issues.apache.org/bugzilla/show_bug.cgi?id=10744

@DPATCH@

diff -ru apache2-2.2.3.orig/support/suexec.c apache2-2.2.3/support/suexec.c 
--- apache2-2.2.3.orig/support/suexec.c	2006-07-12 05:38:44.000000000 +0200
+++ apache2-2.2.3/support/suexec.c	2007-05-24 18:37:04.000000000 +0200
@@ -46,6 +46,7 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <stdlib.h>
+#include <fcntl.h>
 
 #ifdef HAVE_PWD_H
 #include <pwd.h>
@@ -596,17 +597,14 @@
 #endif /* AP_SUEXEC_UMASK */
 
     /*
-     * Be sure to close the log file so the CGI can't
-     * mess with it.  If the exec fails, it will be reopened
-     * automatically when log_err is called.  Note that the log
-     * might not actually be open if AP_LOG_EXEC isn't defined.
-     * However, the "log" cell isn't ifdef'd so let's be defensive
-     * and assume someone might have done something with it
-     * outside an ifdef'd AP_LOG_EXEC block.
-     */
-    if (log != NULL) {
-        fclose(log);
-        log = NULL;
+     * ask fcntl(2) to set the FD_CLOEXEC flag on the log file,
+     * so it'll be automagically closed if the exec() call succeeds.
+     */
+    fflush(log);
+    setbuf(log,NULL);
+    if(fcntl(fileno(log),F_SETFD,FD_CLOEXEC)==-1) {
+      log_err("error: can't set close-on-exec flag");
+      exit(122);
     }
 
     /*
