fa0499
# ./pullrev.sh 1828172 1862968 1863191 1867878 1867882 1867968 1867970 1867971
fa0499
http://svn.apache.org/viewvc?view=revision&revision=1828172
fa0499
http://svn.apache.org/viewvc?view=revision&revision=1862968
fa0499
http://svn.apache.org/viewvc?view=revision&revision=1863191
fa0499
http://svn.apache.org/viewvc?view=revision&revision=1867878
fa0499
http://svn.apache.org/viewvc?view=revision&revision=1867882
fa0499
http://svn.apache.org/viewvc?view=revision&revision=1867968
fa0499
http://svn.apache.org/viewvc?view=revision&revision=1867970
fa0499
http://svn.apache.org/viewvc?view=revision&revision=1867971
fa0499
fa0499
--- httpd-2.4.41/modules/generators/mod_cgi.c
fa0499
+++ httpd-2.4.41/modules/generators/mod_cgi.c
fa0499
@@ -92,6 +92,10 @@
fa0499
     apr_size_t  bufbytes;
fa0499
 } cgi_server_conf;
fa0499
 
fa0499
+typedef struct {
fa0499
+    apr_interval_time_t timeout;
fa0499
+} cgi_dirconf;
fa0499
+
fa0499
 static void *create_cgi_config(apr_pool_t *p, server_rec *s)
fa0499
 {
fa0499
     cgi_server_conf *c =
fa0499
@@ -112,6 +116,12 @@
fa0499
     return overrides->logname ? overrides : base;
fa0499
 }
fa0499
 
fa0499
+static void *create_cgi_dirconf(apr_pool_t *p, char *dummy)
fa0499
+{
fa0499
+    cgi_dirconf *c = (cgi_dirconf *) apr_pcalloc(p, sizeof(cgi_dirconf));
fa0499
+    return c;
fa0499
+}
fa0499
+
fa0499
 static const char *set_scriptlog(cmd_parms *cmd, void *dummy, const char *arg)
fa0499
 {
fa0499
     server_rec *s = cmd->server;
fa0499
@@ -150,6 +160,17 @@
fa0499
     return NULL;
fa0499
 }
fa0499
 
fa0499
+static const char *set_script_timeout(cmd_parms *cmd, void *dummy, const char *arg)
fa0499
+{
fa0499
+    cgi_dirconf *dc = dummy;
fa0499
+
fa0499
+    if (ap_timeout_parameter_parse(arg, &dc->timeout, "s") != APR_SUCCESS) {
fa0499
+        return "CGIScriptTimeout has wrong format";
fa0499
+    }
fa0499
+
fa0499
+    return NULL;
fa0499
+}
fa0499
+
fa0499
 static const command_rec cgi_cmds[] =
fa0499
 {
fa0499
 AP_INIT_TAKE1("ScriptLog", set_scriptlog, NULL, RSRC_CONF,
fa0499
@@ -158,6 +179,9 @@
fa0499
      "the maximum length (in bytes) of the script debug log"),
fa0499
 AP_INIT_TAKE1("ScriptLogBuffer", set_scriptlog_buffer, NULL, RSRC_CONF,
fa0499
      "the maximum size (in bytes) to record of a POST request"),
fa0499
+AP_INIT_TAKE1("CGIScriptTimeout", set_script_timeout, NULL, RSRC_CONF | ACCESS_CONF,
fa0499
+     "The amount of time to wait between successful reads from "
fa0499
+     "the CGI script, in seconds."),
fa0499
     {NULL}
fa0499
 };
fa0499
 
fa0499
@@ -471,23 +495,26 @@
fa0499
                           apr_filepath_name_get(r->filename));
fa0499
         }
fa0499
         else {
fa0499
+            cgi_dirconf *dc = ap_get_module_config(r->per_dir_config, &cgi_module);
fa0499
+            apr_interval_time_t timeout = dc->timeout > 0 ? dc->timeout : r->server->timeout;
fa0499
+
fa0499
             apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT);
fa0499
 
fa0499
             *script_in = procnew->out;
fa0499
             if (!*script_in)
fa0499
                 return APR_EBADF;
fa0499
-            apr_file_pipe_timeout_set(*script_in, r->server->timeout);
fa0499
+            apr_file_pipe_timeout_set(*script_in, timeout);
fa0499
 
fa0499
             if (e_info->prog_type == RUN_AS_CGI) {
fa0499
                 *script_out = procnew->in;
fa0499
                 if (!*script_out)
fa0499
                     return APR_EBADF;
fa0499
-                apr_file_pipe_timeout_set(*script_out, r->server->timeout);
fa0499
+                apr_file_pipe_timeout_set(*script_out, timeout);
fa0499
 
fa0499
                 *script_err = procnew->err;
fa0499
                 if (!*script_err)
fa0499
                     return APR_EBADF;
fa0499
-                apr_file_pipe_timeout_set(*script_err, r->server->timeout);
fa0499
+                apr_file_pipe_timeout_set(*script_err, timeout);
fa0499
             }
fa0499
         }
fa0499
     }
fa0499
@@ -541,212 +568,10 @@
fa0499
     return APR_SUCCESS;
fa0499
 }
fa0499
 
fa0499
-static void discard_script_output(apr_bucket_brigade *bb)
fa0499
-{
fa0499
-    apr_bucket *e;
fa0499
-    const char *buf;
fa0499
-    apr_size_t len;
fa0499
-    apr_status_t rv;
fa0499
-
fa0499
-    for (e = APR_BRIGADE_FIRST(bb);
fa0499
-         e != APR_BRIGADE_SENTINEL(bb);
fa0499
-         e = APR_BUCKET_NEXT(e))
fa0499
-    {
fa0499
-        if (APR_BUCKET_IS_EOS(e)) {
fa0499
-            break;
fa0499
-        }
fa0499
-        rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
fa0499
-        if (rv != APR_SUCCESS) {
fa0499
-            break;
fa0499
-        }
fa0499
-    }
fa0499
-}
fa0499
-
fa0499
 #if APR_FILES_AS_SOCKETS
fa0499
-
fa0499
-/* A CGI bucket type is needed to catch any output to stderr from the
fa0499
- * script; see PR 22030. */
fa0499
-static const apr_bucket_type_t bucket_type_cgi;
fa0499
-
fa0499
-struct cgi_bucket_data {
fa0499
-    apr_pollset_t *pollset;
fa0499
-    request_rec *r;
fa0499
-};
fa0499
-
fa0499
-/* Create a CGI bucket using pipes from script stdout 'out'
fa0499
- * and stderr 'err', for request 'r'. */
fa0499
-static apr_bucket *cgi_bucket_create(request_rec *r,
fa0499
-                                     apr_file_t *out, apr_file_t *err,
fa0499
-                                     apr_bucket_alloc_t *list)
fa0499
-{
fa0499
-    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
fa0499
-    apr_status_t rv;
fa0499
-    apr_pollfd_t fd;
fa0499
-    struct cgi_bucket_data *data = apr_palloc(r->pool, sizeof *data);
fa0499
-
fa0499
-    APR_BUCKET_INIT(b);
fa0499
-    b->free = apr_bucket_free;
fa0499
-    b->list = list;
fa0499
-    b->type = &bucket_type_cgi;
fa0499
-    b->length = (apr_size_t)(-1);
fa0499
-    b->start = -1;
fa0499
-
fa0499
-    /* Create the pollset */
fa0499
-    rv = apr_pollset_create(&data->pollset, 2, r->pool, 0);
fa0499
-    if (rv != APR_SUCCESS) {
fa0499
-        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01217)
fa0499
-                     "apr_pollset_create(); check system or user limits");
fa0499
-        return NULL;
fa0499
-    }
fa0499
-
fa0499
-    fd.desc_type = APR_POLL_FILE;
fa0499
-    fd.reqevents = APR_POLLIN;
fa0499
-    fd.p = r->pool;
fa0499
-    fd.desc.f = out; /* script's stdout */
fa0499
-    fd.client_data = (void *)1;
fa0499
-    rv = apr_pollset_add(data->pollset, &fd;;
fa0499
-    if (rv != APR_SUCCESS) {
fa0499
-        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01218)
fa0499
-                     "apr_pollset_add(); check system or user limits");
fa0499
-        return NULL;
fa0499
-    }
fa0499
-
fa0499
-    fd.desc.f = err; /* script's stderr */
fa0499
-    fd.client_data = (void *)2;
fa0499
-    rv = apr_pollset_add(data->pollset, &fd;;
fa0499
-    if (rv != APR_SUCCESS) {
fa0499
-        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01219)
fa0499
-                     "apr_pollset_add(); check system or user limits");
fa0499
-        return NULL;
fa0499
-    }
fa0499
-
fa0499
-    data->r = r;
fa0499
-    b->data = data;
fa0499
-    return b;
fa0499
-}
fa0499
-
fa0499
-/* Create a duplicate CGI bucket using given bucket data */
fa0499
-static apr_bucket *cgi_bucket_dup(struct cgi_bucket_data *data,
fa0499
-                                  apr_bucket_alloc_t *list)
fa0499
-{
fa0499
-    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
fa0499
-    APR_BUCKET_INIT(b);
fa0499
-    b->free = apr_bucket_free;
fa0499
-    b->list = list;
fa0499
-    b->type = &bucket_type_cgi;
fa0499
-    b->length = (apr_size_t)(-1);
fa0499
-    b->start = -1;
fa0499
-    b->data = data;
fa0499
-    return b;
fa0499
-}
fa0499
-
fa0499
-/* Handle stdout from CGI child.  Duplicate of logic from the _read
fa0499
- * method of the real APR pipe bucket implementation. */
fa0499
-static apr_status_t cgi_read_stdout(apr_bucket *a, apr_file_t *out,
fa0499
-                                    const char **str, apr_size_t *len)
fa0499
-{
fa0499
-    char *buf;
fa0499
-    apr_status_t rv;
fa0499
-
fa0499
-    *str = NULL;
fa0499
-    *len = APR_BUCKET_BUFF_SIZE;
fa0499
-    buf = apr_bucket_alloc(*len, a->list); /* XXX: check for failure? */
fa0499
-
fa0499
-    rv = apr_file_read(out, buf, len);
fa0499
-
fa0499
-    if (rv != APR_SUCCESS && rv != APR_EOF) {
fa0499
-        apr_bucket_free(buf);
fa0499
-        return rv;
fa0499
-    }
fa0499
-
fa0499
-    if (*len > 0) {
fa0499
-        struct cgi_bucket_data *data = a->data;
fa0499
-        apr_bucket_heap *h;
fa0499
-
fa0499
-        /* Change the current bucket to refer to what we read */
fa0499
-        a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
fa0499
-        h = a->data;
fa0499
-        h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
fa0499
-        *str = buf;
fa0499
-        APR_BUCKET_INSERT_AFTER(a, cgi_bucket_dup(data, a->list));
fa0499
-    }
fa0499
-    else {
fa0499
-        apr_bucket_free(buf);
fa0499
-        a = apr_bucket_immortal_make(a, "", 0);
fa0499
-        *str = a->data;
fa0499
-    }
fa0499
-    return rv;
fa0499
-}
fa0499
-
fa0499
-/* Read method of CGI bucket: polls on stderr and stdout of the child,
fa0499
- * sending any stderr output immediately away to the error log. */
fa0499
-static apr_status_t cgi_bucket_read(apr_bucket *b, const char **str,
fa0499
-                                    apr_size_t *len, apr_read_type_e block)
fa0499
-{
fa0499
-    struct cgi_bucket_data *data = b->data;
fa0499
-    apr_interval_time_t timeout;
fa0499
-    apr_status_t rv;
fa0499
-    int gotdata = 0;
fa0499
-
fa0499
-    timeout = block == APR_NONBLOCK_READ ? 0 : data->r->server->timeout;
fa0499
-
fa0499
-    do {
fa0499
-        const apr_pollfd_t *results;
fa0499
-        apr_int32_t num;
fa0499
-
fa0499
-        rv = apr_pollset_poll(data->pollset, timeout, &num, &results);
fa0499
-        if (APR_STATUS_IS_TIMEUP(rv)) {
fa0499
-            if (timeout) {
fa0499
-                ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, data->r, APLOGNO(01220)
fa0499
-                              "Timeout waiting for output from CGI script %s",
fa0499
-                              data->r->filename);
fa0499
-                return rv;
fa0499
-            }
fa0499
-            else {
fa0499
-                return APR_EAGAIN;
fa0499
-            }
fa0499
-        }
fa0499
-        else if (APR_STATUS_IS_EINTR(rv)) {
fa0499
-            continue;
fa0499
-        }
fa0499
-        else if (rv != APR_SUCCESS) {
fa0499
-            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, data->r, APLOGNO(01221)
fa0499
-                          "poll failed waiting for CGI child");
fa0499
-            return rv;
fa0499
-        }
fa0499
-
fa0499
-        for (; num; num--, results++) {
fa0499
-            if (results[0].client_data == (void *)1) {
fa0499
-                /* stdout */
fa0499
-                rv = cgi_read_stdout(b, results[0].desc.f, str, len);
fa0499
-                if (APR_STATUS_IS_EOF(rv)) {
fa0499
-                    rv = APR_SUCCESS;
fa0499
-                }
fa0499
-                gotdata = 1;
fa0499
-            } else {
fa0499
-                /* stderr */
fa0499
-                apr_status_t rv2 = log_script_err(data->r, results[0].desc.f);
fa0499
-                if (APR_STATUS_IS_EOF(rv2)) {
fa0499
-                    apr_pollset_remove(data->pollset, &results[0]);
fa0499
-                }
fa0499
-            }
fa0499
-        }
fa0499
-
fa0499
-    } while (!gotdata);
fa0499
-
fa0499
-    return rv;
fa0499
-}
fa0499
-
fa0499
-static const apr_bucket_type_t bucket_type_cgi = {
fa0499
-    "CGI", 5, APR_BUCKET_DATA,
fa0499
-    apr_bucket_destroy_noop,
fa0499
-    cgi_bucket_read,
fa0499
-    apr_bucket_setaside_notimpl,
fa0499
-    apr_bucket_split_notimpl,
fa0499
-    apr_bucket_copy_notimpl
fa0499
-};
fa0499
-
fa0499
+#define WANT_CGI_BUCKET
fa0499
 #endif
fa0499
+#include "cgi_common.h"
fa0499
 
fa0499
 static int cgi_handler(request_rec *r)
fa0499
 {
fa0499
@@ -766,6 +591,8 @@
fa0499
     apr_status_t rv;
fa0499
     cgi_exec_info_t e_info;
fa0499
     conn_rec *c;
fa0499
+    cgi_dirconf *dc = ap_get_module_config(r->per_dir_config, &cgi_module);
fa0499
+    apr_interval_time_t timeout = dc->timeout > 0 ? dc->timeout : r->server->timeout;
fa0499
 
fa0499
     if (strcmp(r->handler, CGI_MAGIC_TYPE) && strcmp(r->handler, "cgi-script")) {
fa0499
         return DECLINED;
fa0499
@@ -925,10 +752,7 @@
fa0499
     AP_DEBUG_ASSERT(script_in != NULL);
fa0499
 
fa0499
 #if APR_FILES_AS_SOCKETS
fa0499
-    apr_file_pipe_timeout_set(script_in, 0);
fa0499
-    apr_file_pipe_timeout_set(script_err, 0);
fa0499
-
fa0499
-    b = cgi_bucket_create(r, script_in, script_err, c->bucket_alloc);
fa0499
+    b = cgi_bucket_create(r, dc->timeout, script_in, script_err, c->bucket_alloc);
fa0499
     if (b == NULL)
fa0499
         return HTTP_INTERNAL_SERVER_ERROR;
fa0499
 #else
fa0499
@@ -938,111 +762,7 @@
fa0499
     b = apr_bucket_eos_create(c->bucket_alloc);
fa0499
     APR_BRIGADE_INSERT_TAIL(bb, b);
fa0499
 
fa0499
-    /* Handle script return... */
fa0499
-    if (!nph) {
fa0499
-        const char *location;
fa0499
-        char sbuf[MAX_STRING_LEN];
fa0499
-        int ret;
fa0499
-
fa0499
-        if ((ret = ap_scan_script_header_err_brigade_ex(r, bb, sbuf,
fa0499
-                                                        APLOG_MODULE_INDEX)))
fa0499
-        {
fa0499
-            ret = log_script(r, conf, ret, dbuf, sbuf, bb, script_err);
fa0499
-
fa0499
-            /*
fa0499
-             * ret could be HTTP_NOT_MODIFIED in the case that the CGI script
fa0499
-             * does not set an explicit status and ap_meets_conditions, which
fa0499
-             * is called by ap_scan_script_header_err_brigade, detects that
fa0499
-             * the conditions of the requests are met and the response is
fa0499
-             * not modified.
fa0499
-             * In this case set r->status and return OK in order to prevent
fa0499
-             * running through the error processing stack as this would
fa0499
-             * break with mod_cache, if the conditions had been set by
fa0499
-             * mod_cache itself to validate a stale entity.
fa0499
-             * BTW: We circumvent the error processing stack anyway if the
fa0499
-             * CGI script set an explicit status code (whatever it is) and
fa0499
-             * the only possible values for ret here are:
fa0499
-             *
fa0499
-             * HTTP_NOT_MODIFIED          (set by ap_meets_conditions)
fa0499
-             * HTTP_PRECONDITION_FAILED   (set by ap_meets_conditions)
fa0499
-             * HTTP_INTERNAL_SERVER_ERROR (if something went wrong during the
fa0499
-             * processing of the response of the CGI script, e.g broken headers
fa0499
-             * or a crashed CGI process).
fa0499
-             */
fa0499
-            if (ret == HTTP_NOT_MODIFIED) {
fa0499
-                r->status = ret;
fa0499
-                return OK;
fa0499
-            }
fa0499
-
fa0499
-            return ret;
fa0499
-        }
fa0499
-
fa0499
-        location = apr_table_get(r->headers_out, "Location");
fa0499
-
fa0499
-        if (location && r->status == 200) {
fa0499
-            /* For a redirect whether internal or not, discard any
fa0499
-             * remaining stdout from the script, and log any remaining
fa0499
-             * stderr output, as normal. */
fa0499
-            discard_script_output(bb);
fa0499
-            apr_brigade_destroy(bb);
fa0499
-            apr_file_pipe_timeout_set(script_err, r->server->timeout);
fa0499
-            log_script_err(r, script_err);
fa0499
-        }
fa0499
-
fa0499
-        if (location && location[0] == '/' && r->status == 200) {
fa0499
-            /* This redirect needs to be a GET no matter what the original
fa0499
-             * method was.
fa0499
-             */
fa0499
-            r->method = "GET";
fa0499
-            r->method_number = M_GET;
fa0499
-
fa0499
-            /* We already read the message body (if any), so don't allow
fa0499
-             * the redirected request to think it has one.  We can ignore
fa0499
-             * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
fa0499
-             */
fa0499
-            apr_table_unset(r->headers_in, "Content-Length");
fa0499
-
fa0499
-            ap_internal_redirect_handler(location, r);
fa0499
-            return OK;
fa0499
-        }
fa0499
-        else if (location && r->status == 200) {
fa0499
-            /* XXX: Note that if a script wants to produce its own Redirect
fa0499
-             * body, it now has to explicitly *say* "Status: 302"
fa0499
-             */
fa0499
-            return HTTP_MOVED_TEMPORARILY;
fa0499
-        }
fa0499
-
fa0499
-        rv = ap_pass_brigade(r->output_filters, bb);
fa0499
-    }
fa0499
-    else /* nph */ {
fa0499
-        struct ap_filter_t *cur;
fa0499
-
fa0499
-        /* get rid of all filters up through protocol...  since we
fa0499
-         * haven't parsed off the headers, there is no way they can
fa0499
-         * work
fa0499
-         */
fa0499
-
fa0499
-        cur = r->proto_output_filters;
fa0499
-        while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
fa0499
-            cur = cur->next;
fa0499
-        }
fa0499
-        r->output_filters = r->proto_output_filters = cur;
fa0499
-
fa0499
-        rv = ap_pass_brigade(r->output_filters, bb);
fa0499
-    }
fa0499
-
fa0499
-    /* don't soak up script output if errors occurred writing it
fa0499
-     * out...  otherwise, we prolong the life of the script when the
fa0499
-     * connection drops or we stopped sending output for some other
fa0499
-     * reason */
fa0499
-    if (rv == APR_SUCCESS && !r->connection->aborted) {
fa0499
-        apr_file_pipe_timeout_set(script_err, r->server->timeout);
fa0499
-        log_script_err(r, script_err);
fa0499
-    }
fa0499
-
fa0499
-    apr_file_close(script_err);
fa0499
-
fa0499
-    return OK;                      /* NOT r->status, even if it has changed. */
fa0499
+    return cgi_handle_response(r, nph, bb, timeout, conf, dbuf, script_err);
fa0499
 }
fa0499
 
fa0499
 /*============================================================================
fa0499
@@ -1277,7 +997,7 @@
fa0499
 AP_DECLARE_MODULE(cgi) =
fa0499
 {
fa0499
     STANDARD20_MODULE_STUFF,
fa0499
-    NULL,                        /* dir config creater */
fa0499
+    create_cgi_dirconf,          /* dir config creater */
fa0499
     NULL,                        /* dir merger --- default is to override */
fa0499
     create_cgi_config,           /* server config */
fa0499
     merge_cgi_config,            /* merge server config */
fa0499
--- httpd-2.4.41/modules/generators/config5.m4
fa0499
+++ httpd-2.4.41/modules/generators/config5.m4
fa0499
@@ -78,4 +78,15 @@
fa0499
 
fa0499
 APR_ADDTO(INCLUDES, [-I\$(top_srcdir)/$modpath_current])
fa0499
 
fa0499
+AC_ARG_ENABLE(cgid-fdpassing,
fa0499
+  [APACHE_HELP_STRING(--enable-cgid-fdpassing,Enable experimental mod_cgid support for fd passing)],
fa0499
+  [if test "$enableval" = "yes"; then
fa0499
+     AC_CHECK_DECL(CMSG_DATA,
fa0499
+       [AC_DEFINE([HAVE_CGID_FDPASSING], 1, [Enable FD passing support in mod_cgid])],
fa0499
+       [AC_MSG_ERROR([cannot support mod_cgid fd-passing on this system])], [
fa0499
+#include <sys/types.h>
fa0499
+#include <sys/socket.h>])
fa0499
+  fi
fa0499
+])
fa0499
+
fa0499
 APACHE_MODPATH_FINISH
fa0499
--- httpd-2.4.41/modules/generators/mod_cgid.c
fa0499
+++ httpd-2.4.41/modules/generators/mod_cgid.c
fa0499
@@ -342,15 +342,19 @@
fa0499
     return close(fd);
fa0499
 }
fa0499
 
fa0499
-/* deal with incomplete reads and signals
fa0499
- * assume you really have to read buf_size bytes
fa0499
- */
fa0499
-static apr_status_t sock_read(int fd, void *vbuf, size_t buf_size)
fa0499
+/* Read from the socket dealing with incomplete messages and signals.
fa0499
+ * Returns 0 on success or errno on failure.  Stderr fd passed as
fa0499
+ * auxiliary data from other end is written to *errfd, or else stderr
fa0499
+ * fileno if not present. */
fa0499
+static apr_status_t sock_readhdr(int fd, int *errfd, void *vbuf, size_t buf_size)
fa0499
 {
fa0499
-    char *buf = vbuf;
fa0499
     int rc;
fa0499
+#ifndef HAVE_CGID_FDPASSING
fa0499
+    char *buf = vbuf;
fa0499
     size_t bytes_read = 0;
fa0499
 
fa0499
+    if (errfd) *errfd = 0;
fa0499
+    
fa0499
     do {
fa0499
         do {
fa0499
             rc = read(fd, buf + bytes_read, buf_size - bytes_read);
fa0499
@@ -365,9 +369,60 @@
fa0499
         }
fa0499
     } while (bytes_read < buf_size);
fa0499
 
fa0499
+   
fa0499
+#else /* with FD passing */
fa0499
+    struct msghdr msg = {0};
fa0499
+    struct iovec vec = {vbuf, buf_size};
fa0499
+    struct cmsghdr *cmsg;
fa0499
+    union {  /* union to ensure alignment */
fa0499
+        struct cmsghdr cm;
fa0499
+        char buf[CMSG_SPACE(sizeof(int))];
fa0499
+    } u;
fa0499
+    
fa0499
+    msg.msg_iov = &vec;
fa0499
+    msg.msg_iovlen = 1;
fa0499
+
fa0499
+    if (errfd) {
fa0499
+        msg.msg_control = u.buf;
fa0499
+        msg.msg_controllen = sizeof(u.buf);
fa0499
+        *errfd = 0;
fa0499
+    }
fa0499
+    
fa0499
+    /* use MSG_WAITALL to skip loop on truncated reads */
fa0499
+    do {
fa0499
+        rc = recvmsg(fd, &msg, MSG_WAITALL);
fa0499
+    } while (rc < 0 && errno == EINTR);
fa0499
+
fa0499
+    if (rc == 0) {
fa0499
+        return ECONNRESET;
fa0499
+    }
fa0499
+    else if (rc < 0) {
fa0499
+        return errno;
fa0499
+    }
fa0499
+    else if (rc != buf_size) {
fa0499
+        /* MSG_WAITALL should ensure the recvmsg blocks until the
fa0499
+         * entire length is read, but let's be paranoid. */
fa0499
+        return APR_INCOMPLETE;
fa0499
+    }
fa0499
+
fa0499
+    if (errfd
fa0499
+        && (cmsg = CMSG_FIRSTHDR(&msg)) != NULL
fa0499
+        && cmsg->cmsg_len == CMSG_LEN(sizeof(*errfd))
fa0499
+        && cmsg->cmsg_level == SOL_SOCKET
fa0499
+        && cmsg->cmsg_type == SCM_RIGHTS) {
fa0499
+        *errfd = *((int *) CMSG_DATA(cmsg));
fa0499
+    }
fa0499
+#endif
fa0499
+    
fa0499
     return APR_SUCCESS;
fa0499
 }
fa0499
 
fa0499
+/* As sock_readhdr but without auxiliary fd passing. */
fa0499
+static apr_status_t sock_read(int fd, void *vbuf, size_t buf_size)
fa0499
+{
fa0499
+    return sock_readhdr(fd, NULL, vbuf, buf_size);
fa0499
+}
fa0499
+
fa0499
 /* deal with signals
fa0499
  */
fa0499
 static apr_status_t sock_write(int fd, const void *buf, size_t buf_size)
fa0499
@@ -384,7 +439,7 @@
fa0499
     return APR_SUCCESS;
fa0499
 }
fa0499
 
fa0499
-static apr_status_t sock_writev(int fd, request_rec *r, int count, ...)
fa0499
+static apr_status_t sock_writev(int fd, int auxfd, request_rec *r, int count, ...)
fa0499
 {
fa0499
     va_list ap;
fa0499
     int rc;
fa0499
@@ -399,9 +454,39 @@
fa0499
     }
fa0499
     va_end(ap);
fa0499
 
fa0499
+#ifndef HAVE_CGID_FDPASSING
fa0499
     do {
fa0499
         rc = writev(fd, vec, count);
fa0499
     } while (rc < 0 && errno == EINTR);
fa0499
+#else
fa0499
+    {
fa0499
+        struct msghdr msg = { 0 };
fa0499
+        struct cmsghdr *cmsg;
fa0499
+        union { /* union for alignment */
fa0499
+            char buf[CMSG_SPACE(sizeof(int))];
fa0499
+            struct cmsghdr align;
fa0499
+        } u;
fa0499
+
fa0499
+        msg.msg_iov = vec;
fa0499
+        msg.msg_iovlen = count;
fa0499
+
fa0499
+        if (auxfd) {
fa0499
+            msg.msg_control = u.buf;
fa0499
+            msg.msg_controllen = sizeof(u.buf);
fa0499
+
fa0499
+            cmsg = CMSG_FIRSTHDR(&msg;;
fa0499
+            cmsg->cmsg_level = SOL_SOCKET;
fa0499
+            cmsg->cmsg_type = SCM_RIGHTS;
fa0499
+            cmsg->cmsg_len = CMSG_LEN(sizeof(int));
fa0499
+            *((int *) CMSG_DATA(cmsg)) = auxfd;
fa0499
+        }
fa0499
+
fa0499
+        do {
fa0499
+            rc = sendmsg(fd, &msg, 0);
fa0499
+        } while (rc < 0 && errno == EINTR);
fa0499
+    }
fa0499
+#endif
fa0499
+    
fa0499
     if (rc < 0) {
fa0499
         return errno;
fa0499
     }
fa0499
@@ -410,7 +495,7 @@
fa0499
 }
fa0499
 
fa0499
 static apr_status_t get_req(int fd, request_rec *r, char **argv0, char ***env,
fa0499
-                            cgid_req_t *req)
fa0499
+                            int *errfd, cgid_req_t *req)
fa0499
 {
fa0499
     int i;
fa0499
     char **environ;
fa0499
@@ -421,7 +506,7 @@
fa0499
     r->server = apr_pcalloc(r->pool, sizeof(server_rec));
fa0499
 
fa0499
     /* read the request header */
fa0499
-    stat = sock_read(fd, req, sizeof(*req));
fa0499
+    stat = sock_readhdr(fd, errfd, req, sizeof(*req));
fa0499
     if (stat != APR_SUCCESS) {
fa0499
         return stat;
fa0499
     }
fa0499
@@ -479,14 +564,15 @@
fa0499
     return APR_SUCCESS;
fa0499
 }
fa0499
 
fa0499
-static apr_status_t send_req(int fd, request_rec *r, char *argv0, char **env,
fa0499
-                             int req_type)
fa0499
+static apr_status_t send_req(int fd, apr_file_t *errpipe, request_rec *r,
fa0499
+                             char *argv0, char **env, int req_type)
fa0499
 {
fa0499
     int i;
fa0499
     cgid_req_t req = {0};
fa0499
     apr_status_t stat;
fa0499
     ap_unix_identity_t * ugid = ap_run_get_suexec_identity(r);
fa0499
     core_dir_config *core_conf = ap_get_core_module_config(r->per_dir_config);
fa0499
+    int errfd;
fa0499
 
fa0499
 
fa0499
     if (ugid == NULL) {
fa0499
@@ -507,16 +593,21 @@
fa0499
     req.args_len = r->args ? strlen(r->args) : 0;
fa0499
     req.loglevel = r->server->log.level;
fa0499
 
fa0499
+    if (errpipe)
fa0499
+        apr_os_file_get(&errfd, errpipe);
fa0499
+    else
fa0499
+        errfd = 0;
fa0499
+    
fa0499
     /* Write the request header */
fa0499
     if (req.args_len) {
fa0499
-        stat = sock_writev(fd, r, 5,
fa0499
+        stat = sock_writev(fd, errfd, r, 5,
fa0499
                            &req, sizeof(req),
fa0499
                            r->filename, req.filename_len,
fa0499
                            argv0, req.argv0_len,
fa0499
                            r->uri, req.uri_len,
fa0499
                            r->args, req.args_len);
fa0499
     } else {
fa0499
-        stat = sock_writev(fd, r, 4,
fa0499
+        stat = sock_writev(fd, errfd, r, 4,
fa0499
                            &req, sizeof(req),
fa0499
                            r->filename, req.filename_len,
fa0499
                            argv0, req.argv0_len,
fa0499
@@ -531,7 +622,7 @@
fa0499
     for (i = 0; i < req.env_count; i++) {
fa0499
         apr_size_t curlen = strlen(env[i]);
fa0499
 
fa0499
-        if ((stat = sock_writev(fd, r, 2, &curlen, sizeof(curlen),
fa0499
+        if ((stat = sock_writev(fd, 0, r, 2, &curlen, sizeof(curlen),
fa0499
                                 env[i], curlen)) != APR_SUCCESS) {
fa0499
             return stat;
fa0499
         }
fa0499
@@ -582,20 +673,34 @@
fa0499
     }
fa0499
 }
fa0499
 
fa0499
+/* Callback executed in the forked child process if exec of the CGI
fa0499
+ * script fails.  For the fd-passing case, output to stderr goes to
fa0499
+ * the client (request handling thread) and is logged via
fa0499
+ * ap_log_rerror there.  For the non-fd-passing case, the "fake"
fa0499
+ * request_rec passed via userdata is used to log. */
fa0499
 static void cgid_child_errfn(apr_pool_t *pool, apr_status_t err,
fa0499
                              const char *description)
fa0499
 {
fa0499
-    request_rec *r;
fa0499
     void *vr;
fa0499
 
fa0499
     apr_pool_userdata_get(&vr, ERRFN_USERDATA_KEY, pool);
fa0499
-    r = vr;
fa0499
-
fa0499
-    /* sure we got r, but don't call ap_log_rerror() because we don't
fa0499
-     * have r->headers_in and possibly other storage referenced by
fa0499
-     * ap_log_rerror()
fa0499
-     */
fa0499
-    ap_log_error(APLOG_MARK, APLOG_ERR, err, r->server, APLOGNO(01241) "%s", description);
fa0499
+    if (vr) {
fa0499
+        request_rec *r = vr;
fa0499
+        
fa0499
+        /* sure we got r, but don't call ap_log_rerror() because we don't
fa0499
+         * have r->headers_in and possibly other storage referenced by
fa0499
+         * ap_log_rerror()
fa0499
+         */
fa0499
+        ap_log_error(APLOG_MARK, APLOG_ERR, err, r->server, APLOGNO(01241) "%s", description);
fa0499
+    }
fa0499
+    else {
fa0499
+        const char *logstr;
fa0499
+        
fa0499
+        logstr = apr_psprintf(pool, APLOGNO(01241) "error spawning CGI child: %s (%pm)\n",
fa0499
+                              description, &err;;
fa0499
+        fputs(logstr, stderr);
fa0499
+        fflush(stderr);
fa0499
+    }
fa0499
 }
fa0499
 
fa0499
 static int cgid_server(void *data)
fa0499
@@ -669,7 +774,7 @@
fa0499
     }
fa0499
 
fa0499
     while (!daemon_should_exit) {
fa0499
-        int errfileno = STDERR_FILENO;
fa0499
+        int errfileno;
fa0499
         char *argv0 = NULL;
fa0499
         char **env = NULL;
fa0499
         const char * const *argv;
fa0499
@@ -709,7 +814,7 @@
fa0499
         r = apr_pcalloc(ptrans, sizeof(request_rec));
fa0499
         procnew = apr_pcalloc(ptrans, sizeof(*procnew));
fa0499
         r->pool = ptrans;
fa0499
-        stat = get_req(sd2, r, &argv0, &env, &cgid_req);
fa0499
+        stat = get_req(sd2, r, &argv0, &env, &errfileno, &cgid_req);
fa0499
         if (stat != APR_SUCCESS) {
fa0499
             ap_log_error(APLOG_MARK, APLOG_ERR, stat,
fa0499
                          main_server, APLOGNO(01248)
fa0499
@@ -741,6 +846,16 @@
fa0499
             continue;
fa0499
         }
fa0499
 
fa0499
+        if (errfileno == 0) {
fa0499
+            errfileno = STDERR_FILENO;
fa0499
+        }
fa0499
+        else {
fa0499
+            ap_log_error(APLOG_MARK, APLOG_DEBUG, rv, main_server,
fa0499
+                          "using passed fd %d as stderr", errfileno);
fa0499
+            /* Limit the received fd lifetime to pool lifetime */
fa0499
+            apr_pool_cleanup_register(ptrans, (void *)((long)errfileno),
fa0499
+                                      close_unix_socket, close_unix_socket);
fa0499
+        }
fa0499
         apr_os_file_put(&r->server->error_log, &errfileno, 0, r->pool);
fa0499
         apr_os_file_put(&inout, &sd2, 0, r->pool);
fa0499
 
fa0499
@@ -800,7 +915,10 @@
fa0499
             close(sd2);
fa0499
         }
fa0499
         else {
fa0499
-            apr_pool_userdata_set(r, ERRFN_USERDATA_KEY, apr_pool_cleanup_null, ptrans);
fa0499
+            if (errfileno == STDERR_FILENO) {
fa0499
+                /* Used by cgid_child_errfn without fd-passing. */
fa0499
+                apr_pool_userdata_set(r, ERRFN_USERDATA_KEY, apr_pool_cleanup_null, ptrans);
fa0499
+            }
fa0499
 
fa0499
             argv = (const char * const *)create_argv(r->pool, NULL, NULL, NULL, argv0, r->args);
fa0499
 
fa0499
@@ -1099,6 +1217,33 @@
fa0499
     return ret;
fa0499
 }
fa0499
 
fa0499
+/* Soak up stderr from a script and redirect it to the error log.
fa0499
+ * TODO: log_scripterror() and this could move to cgi_common.h. */
fa0499
+static apr_status_t log_script_err(request_rec *r, apr_file_t *script_err)
fa0499
+{
fa0499
+    char argsbuffer[HUGE_STRING_LEN];
fa0499
+    char *newline;
fa0499
+    apr_status_t rv;
fa0499
+    cgid_server_conf *conf = ap_get_module_config(r->server->module_config, &cgid_module);
fa0499
+
fa0499
+    while ((rv = apr_file_gets(argsbuffer, HUGE_STRING_LEN,
fa0499
+                               script_err)) == APR_SUCCESS) {
fa0499
+
fa0499
+        newline = strchr(argsbuffer, '\n');
fa0499
+        if (newline) {
fa0499
+            char *prev = newline - 1;
fa0499
+            if (prev >= argsbuffer && *prev == '\r') {
fa0499
+                newline = prev;
fa0499
+            }
fa0499
+
fa0499
+            *newline = '\0';
fa0499
+        }
fa0499
+        log_scripterror(r, conf, r->status, 0, argsbuffer);
fa0499
+    }
fa0499
+
fa0499
+    return rv;
fa0499
+}
fa0499
+
fa0499
 static int log_script(request_rec *r, cgid_server_conf * conf, int ret,
fa0499
                       char *dbuf, const char *sbuf, apr_bucket_brigade *bb,
fa0499
                       apr_file_t *script_err)
fa0499
@@ -1204,6 +1349,13 @@
fa0499
     return ret;
fa0499
 }
fa0499
 
fa0499
+/* Pull in CGI bucket implementation. */
fa0499
+#define cgi_server_conf cgid_server_conf
fa0499
+#ifdef HAVE_CGID_FDPASSING
fa0499
+#define WANT_CGI_BUCKET
fa0499
+#endif
fa0499
+#include "cgi_common.h"
fa0499
+
fa0499
 static int connect_to_daemon(int *sdptr, request_rec *r,
fa0499
                              cgid_server_conf *conf)
fa0499
 {
fa0499
@@ -1270,27 +1422,6 @@
fa0499
     return OK;
fa0499
 }
fa0499
 
fa0499
-static void discard_script_output(apr_bucket_brigade *bb)
fa0499
-{
fa0499
-    apr_bucket *e;
fa0499
-    const char *buf;
fa0499
-    apr_size_t len;
fa0499
-    apr_status_t rv;
fa0499
-
fa0499
-    for (e = APR_BRIGADE_FIRST(bb);
fa0499
-         e != APR_BRIGADE_SENTINEL(bb);
fa0499
-         e = APR_BUCKET_NEXT(e))
fa0499
-    {
fa0499
-        if (APR_BUCKET_IS_EOS(e)) {
fa0499
-            break;
fa0499
-        }
fa0499
-        rv = apr_bucket_read(e, &buf, &len, APR_BLOCK_READ);
fa0499
-        if (rv != APR_SUCCESS) {
fa0499
-            break;
fa0499
-        }
fa0499
-    }
fa0499
-}
fa0499
-
fa0499
 /****************************************************************
fa0499
  *
fa0499
  * Actual cgid handling...
fa0499
@@ -1395,6 +1526,7 @@
fa0499
 
fa0499
 static int cgid_handler(request_rec *r)
fa0499
 {
fa0499
+    conn_rec *c = r->connection;
fa0499
     int retval, nph, dbpos;
fa0499
     char *argv0, *dbuf;
fa0499
     apr_bucket_brigade *bb;
fa0499
@@ -1404,10 +1536,11 @@
fa0499
     int seen_eos, child_stopped_reading;
fa0499
     int sd;
fa0499
     char **env;
fa0499
-    apr_file_t *tempsock;
fa0499
+    apr_file_t *tempsock, *script_err, *errpipe_out;
fa0499
     struct cleanup_script_info *info;
fa0499
     apr_status_t rv;
fa0499
     cgid_dirconf *dc;
fa0499
+    apr_interval_time_t timeout;
fa0499
 
fa0499
     if (strcmp(r->handler, CGI_MAGIC_TYPE) && strcmp(r->handler, "cgi-script")) {
fa0499
         return DECLINED;
fa0499
@@ -1416,7 +1549,7 @@
fa0499
     conf = ap_get_module_config(r->server->module_config, &cgid_module);
fa0499
     dc = ap_get_module_config(r->per_dir_config, &cgid_module);
fa0499
 
fa0499
-    
fa0499
+    timeout = dc->timeout > 0 ? dc->timeout : r->server->timeout;
fa0499
     is_included = !strcmp(r->protocol, "INCLUDED");
fa0499
 
fa0499
     if ((argv0 = strrchr(r->filename, '/')) != NULL) {
fa0499
@@ -1469,6 +1602,17 @@
fa0499
     }
fa0499
     */
fa0499
 
fa0499
+#ifdef HAVE_CGID_FDPASSING
fa0499
+    rv = apr_file_pipe_create(&script_err, &errpipe_out, r->pool);
fa0499
+    if (rv) {
fa0499
+        return log_scripterror(r, conf, HTTP_SERVICE_UNAVAILABLE, rv, APLOGNO(10176)
fa0499
+                               "could not create pipe for stderr");
fa0499
+    }
fa0499
+#else
fa0499
+    script_err = NULL;
fa0499
+    errpipe_out = NULL;
fa0499
+#endif
fa0499
+    
fa0499
     /*
fa0499
      * httpd core function used to add common environment variables like
fa0499
      * DOCUMENT_ROOT. 
fa0499
@@ -1481,12 +1625,16 @@
fa0499
         return retval;
fa0499
     }
fa0499
 
fa0499
-    rv = send_req(sd, r, argv0, env, CGI_REQ);
fa0499
+    rv = send_req(sd, errpipe_out, r, argv0, env, CGI_REQ);
fa0499
     if (rv != APR_SUCCESS) {
fa0499
         ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01268)
fa0499
                      "write to cgi daemon process");
fa0499
     }
fa0499
 
fa0499
+    /* The write-end of the pipe is only used by the server, so close
fa0499
+     * it here. */
fa0499
+    if (errpipe_out) apr_file_close(errpipe_out);
fa0499
+    
fa0499
     info = apr_palloc(r->pool, sizeof(struct cleanup_script_info));
fa0499
     info->conf = conf;
fa0499
     info->r = r;
fa0499
@@ -1508,12 +1656,7 @@
fa0499
      */
fa0499
 
fa0499
     apr_os_pipe_put_ex(&tempsock, &sd, 1, r->pool);
fa0499
-    if (dc->timeout > 0) { 
fa0499
-        apr_file_pipe_timeout_set(tempsock, dc->timeout);
fa0499
-    }
fa0499
-    else { 
fa0499
-        apr_file_pipe_timeout_set(tempsock, r->server->timeout);
fa0499
-    }
fa0499
+    apr_file_pipe_timeout_set(tempsock, timeout);
fa0499
     apr_pool_cleanup_kill(r->pool, (void *)((long)sd), close_unix_socket);
fa0499
 
fa0499
     /* Transfer any put/post args, CERN style...
fa0499
@@ -1605,114 +1748,19 @@
fa0499
      */
fa0499
     shutdown(sd, 1);
fa0499
 
fa0499
-    /* Handle script return... */
fa0499
-    if (!nph) {
fa0499
-        conn_rec *c = r->connection;
fa0499
-        const char *location;
fa0499
-        char sbuf[MAX_STRING_LEN];
fa0499
-        int ret;
fa0499
-
fa0499
-        bb = apr_brigade_create(r->pool, c->bucket_alloc);
fa0499
-        b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
fa0499
-        APR_BRIGADE_INSERT_TAIL(bb, b);
fa0499
-        b = apr_bucket_eos_create(c->bucket_alloc);
fa0499
-        APR_BRIGADE_INSERT_TAIL(bb, b);
fa0499
-
fa0499
-        if ((ret = ap_scan_script_header_err_brigade_ex(r, bb, sbuf,
fa0499
-                                                        APLOG_MODULE_INDEX)))
fa0499
-        {
fa0499
-            ret = log_script(r, conf, ret, dbuf, sbuf, bb, NULL);
fa0499
-
fa0499
-            /*
fa0499
-             * ret could be HTTP_NOT_MODIFIED in the case that the CGI script
fa0499
-             * does not set an explicit status and ap_meets_conditions, which
fa0499
-             * is called by ap_scan_script_header_err_brigade, detects that
fa0499
-             * the conditions of the requests are met and the response is
fa0499
-             * not modified.
fa0499
-             * In this case set r->status and return OK in order to prevent
fa0499
-             * running through the error processing stack as this would
fa0499
-             * break with mod_cache, if the conditions had been set by
fa0499
-             * mod_cache itself to validate a stale entity.
fa0499
-             * BTW: We circumvent the error processing stack anyway if the
fa0499
-             * CGI script set an explicit status code (whatever it is) and
fa0499
-             * the only possible values for ret here are:
fa0499
-             *
fa0499
-             * HTTP_NOT_MODIFIED          (set by ap_meets_conditions)
fa0499
-             * HTTP_PRECONDITION_FAILED   (set by ap_meets_conditions)
fa0499
-             * HTTP_INTERNAL_SERVER_ERROR (if something went wrong during the
fa0499
-             * processing of the response of the CGI script, e.g broken headers
fa0499
-             * or a crashed CGI process).
fa0499
-             */
fa0499
-            if (ret == HTTP_NOT_MODIFIED) {
fa0499
-                r->status = ret;
fa0499
-                return OK;
fa0499
-            }
fa0499
-
fa0499
-            return ret;
fa0499
-        }
fa0499
-
fa0499
-        location = apr_table_get(r->headers_out, "Location");
fa0499
-
fa0499
-        if (location && location[0] == '/' && r->status == 200) {
fa0499
-
fa0499
-            /* Soak up all the script output */
fa0499
-            discard_script_output(bb);
fa0499
-            apr_brigade_destroy(bb);
fa0499
-            /* This redirect needs to be a GET no matter what the original
fa0499
-             * method was.
fa0499
-             */
fa0499
-            r->method = "GET";
fa0499
-            r->method_number = M_GET;
fa0499
-
fa0499
-            /* We already read the message body (if any), so don't allow
fa0499
-             * the redirected request to think it has one. We can ignore
fa0499
-             * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
fa0499
-             */
fa0499
-            apr_table_unset(r->headers_in, "Content-Length");
fa0499
-
fa0499
-            ap_internal_redirect_handler(location, r);
fa0499
-            return OK;
fa0499
-        }
fa0499
-        else if (location && r->status == 200) {
fa0499
-            /* XXX: Note that if a script wants to produce its own Redirect
fa0499
-             * body, it now has to explicitly *say* "Status: 302"
fa0499
-             */
fa0499
-            discard_script_output(bb);
fa0499
-            apr_brigade_destroy(bb);
fa0499
-            return HTTP_MOVED_TEMPORARILY;
fa0499
-        }
fa0499
-
fa0499
-        rv = ap_pass_brigade(r->output_filters, bb);
fa0499
-        if (rv != APR_SUCCESS) { 
fa0499
-            ap_log_rerror(APLOG_MARK, APLOG_TRACE1, rv, r,
fa0499
-                          "Failed to flush CGI output to client");
fa0499
-        }
fa0499
-    }
fa0499
-
fa0499
-    if (nph) {
fa0499
-        conn_rec *c = r->connection;
fa0499
-        struct ap_filter_t *cur;
fa0499
-
fa0499
-        /* get rid of all filters up through protocol...  since we
fa0499
-         * haven't parsed off the headers, there is no way they can
fa0499
-         * work
fa0499
-         */
fa0499
-
fa0499
-        cur = r->proto_output_filters;
fa0499
-        while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
fa0499
-            cur = cur->next;
fa0499
-        }
fa0499
-        r->output_filters = r->proto_output_filters = cur;
fa0499
-
fa0499
-        bb = apr_brigade_create(r->pool, c->bucket_alloc);
fa0499
-        b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
fa0499
-        APR_BRIGADE_INSERT_TAIL(bb, b);
fa0499
-        b = apr_bucket_eos_create(c->bucket_alloc);
fa0499
-        APR_BRIGADE_INSERT_TAIL(bb, b);
fa0499
-        ap_pass_brigade(r->output_filters, bb);
fa0499
-    }
fa0499
+    bb = apr_brigade_create(r->pool, c->bucket_alloc);
fa0499
+#ifdef HAVE_CGID_FDPASSING
fa0499
+    b = cgi_bucket_create(r, dc->timeout, tempsock, script_err, c->bucket_alloc);
fa0499
+    if (b == NULL)
fa0499
+        return HTTP_INTERNAL_SERVER_ERROR; /* should call log_scripterror() w/ _UNAVAILABLE? */
fa0499
+#else
fa0499
+    b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
fa0499
+#endif
fa0499
+    APR_BRIGADE_INSERT_TAIL(bb, b);
fa0499
+    b = apr_bucket_eos_create(c->bucket_alloc);
fa0499
+    APR_BRIGADE_INSERT_TAIL(bb, b);
fa0499
 
fa0499
-    return OK; /* NOT r->status, even if it has changed. */
fa0499
+    return cgi_handle_response(r, nph, bb, timeout, conf, dbuf, script_err);
fa0499
 }
fa0499
 
fa0499
 
fa0499
@@ -1829,7 +1877,7 @@
fa0499
         return retval;
fa0499
     }
fa0499
 
fa0499
-    send_req(sd, r, command, env, SSI_REQ);
fa0499
+    send_req(sd, NULL, r, command, env, SSI_REQ);
fa0499
 
fa0499
     info = apr_palloc(r->pool, sizeof(struct cleanup_script_info));
fa0499
     info->conf = conf;
fa0499
--- httpd-2.4.41/modules/generators/cgi_common.h
fa0499
+++ httpd-2.4.41/modules/generators/cgi_common.h
fa0499
@@ -0,0 +1,359 @@
fa0499
+/* Licensed to the Apache Software Foundation (ASF) under one or more
fa0499
+ * contributor license agreements.  See the NOTICE file distributed with
fa0499
+ * this work for additional information regarding copyright ownership.
fa0499
+ * The ASF licenses this file to You under the Apache License, Version 2.0
fa0499
+ * (the "License"); you may not use this file except in compliance with
fa0499
+ * the License.  You may obtain a copy of the License at
fa0499
+ *
fa0499
+ *     http://www.apache.org/licenses/LICENSE-2.0
fa0499
+ *
fa0499
+ * Unless required by applicable law or agreed to in writing, software
fa0499
+ * distributed under the License is distributed on an "AS IS" BASIS,
fa0499
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
fa0499
+ * See the License for the specific language governing permissions and
fa0499
+ * limitations under the License.
fa0499
+ */
fa0499
+
fa0499
+#include "apr.h"
fa0499
+#include "apr_strings.h"
fa0499
+#include "apr_buckets.h"
fa0499
+#include "apr_lib.h"
fa0499
+#include "apr_poll.h"
fa0499
+
fa0499
+#define APR_WANT_STRFUNC
fa0499
+#define APR_WANT_MEMFUNC
fa0499
+#include "apr_want.h"
fa0499
+
fa0499
+#include "httpd.h"
fa0499
+#include "util_filter.h"
fa0499
+
fa0499
+static void discard_script_output(apr_bucket_brigade *bb)
fa0499
+{
fa0499
+    apr_bucket *e;
fa0499
+    const char *buf;
fa0499
+    apr_size_t len;
fa0499
+
fa0499
+    for (e = APR_BRIGADE_FIRST(bb);
fa0499
+         e != APR_BRIGADE_SENTINEL(bb) && !APR_BUCKET_IS_EOS(e);
fa0499
+         e = APR_BRIGADE_FIRST(bb))
fa0499
+    {
fa0499
+        if (apr_bucket_read(e, &buf, &len, APR_BLOCK_READ)) {
fa0499
+            break;
fa0499
+        }
fa0499
+        apr_bucket_delete(e);
fa0499
+    }
fa0499
+}
fa0499
+
fa0499
+#ifdef WANT_CGI_BUCKET
fa0499
+/* A CGI bucket type is needed to catch any output to stderr from the
fa0499
+ * script; see PR 22030. */
fa0499
+static const apr_bucket_type_t bucket_type_cgi;
fa0499
+
fa0499
+struct cgi_bucket_data {
fa0499
+    apr_pollset_t *pollset;
fa0499
+    request_rec *r;
fa0499
+    apr_interval_time_t timeout;
fa0499
+};
fa0499
+
fa0499
+/* Create a CGI bucket using pipes from script stdout 'out'
fa0499
+ * and stderr 'err', for request 'r'. */
fa0499
+static apr_bucket *cgi_bucket_create(request_rec *r,
fa0499
+                                     apr_interval_time_t timeout,
fa0499
+                                     apr_file_t *out, apr_file_t *err,
fa0499
+                                     apr_bucket_alloc_t *list)
fa0499
+{
fa0499
+    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
fa0499
+    apr_status_t rv;
fa0499
+    apr_pollfd_t fd;
fa0499
+    struct cgi_bucket_data *data = apr_palloc(r->pool, sizeof *data);
fa0499
+
fa0499
+    /* Disable APR timeout handling since we'll use poll() entirely. */
fa0499
+    apr_file_pipe_timeout_set(out, 0);
fa0499
+    apr_file_pipe_timeout_set(err, 0);
fa0499
+    
fa0499
+    APR_BUCKET_INIT(b);
fa0499
+    b->free = apr_bucket_free;
fa0499
+    b->list = list;
fa0499
+    b->type = &bucket_type_cgi;
fa0499
+    b->length = (apr_size_t)(-1);
fa0499
+    b->start = -1;
fa0499
+
fa0499
+    /* Create the pollset */
fa0499
+    rv = apr_pollset_create(&data->pollset, 2, r->pool, 0);
fa0499
+    if (rv != APR_SUCCESS) {
fa0499
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01217)
fa0499
+                     "apr_pollset_create(); check system or user limits");
fa0499
+        return NULL;
fa0499
+    }
fa0499
+
fa0499
+    fd.desc_type = APR_POLL_FILE;
fa0499
+    fd.reqevents = APR_POLLIN;
fa0499
+    fd.p = r->pool;
fa0499
+    fd.desc.f = out; /* script's stdout */
fa0499
+    fd.client_data = (void *)1;
fa0499
+    rv = apr_pollset_add(data->pollset, &fd;;
fa0499
+    if (rv != APR_SUCCESS) {
fa0499
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01218)
fa0499
+                     "apr_pollset_add(); check system or user limits");
fa0499
+        return NULL;
fa0499
+    }
fa0499
+
fa0499
+    fd.desc.f = err; /* script's stderr */
fa0499
+    fd.client_data = (void *)2;
fa0499
+    rv = apr_pollset_add(data->pollset, &fd;;
fa0499
+    if (rv != APR_SUCCESS) {
fa0499
+        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01219)
fa0499
+                     "apr_pollset_add(); check system or user limits");
fa0499
+        return NULL;
fa0499
+    }
fa0499
+
fa0499
+    data->r = r;
fa0499
+    data->timeout = timeout;
fa0499
+    b->data = data;
fa0499
+    return b;
fa0499
+}
fa0499
+
fa0499
+/* Create a duplicate CGI bucket using given bucket data */
fa0499
+static apr_bucket *cgi_bucket_dup(struct cgi_bucket_data *data,
fa0499
+                                  apr_bucket_alloc_t *list)
fa0499
+{
fa0499
+    apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
fa0499
+    APR_BUCKET_INIT(b);
fa0499
+    b->free = apr_bucket_free;
fa0499
+    b->list = list;
fa0499
+    b->type = &bucket_type_cgi;
fa0499
+    b->length = (apr_size_t)(-1);
fa0499
+    b->start = -1;
fa0499
+    b->data = data;
fa0499
+    return b;
fa0499
+}
fa0499
+
fa0499
+/* Handle stdout from CGI child.  Duplicate of logic from the _read
fa0499
+ * method of the real APR pipe bucket implementation. */
fa0499
+static apr_status_t cgi_read_stdout(apr_bucket *a, apr_file_t *out,
fa0499
+                                    const char **str, apr_size_t *len)
fa0499
+{
fa0499
+    char *buf;
fa0499
+    apr_status_t rv;
fa0499
+
fa0499
+    *str = NULL;
fa0499
+    *len = APR_BUCKET_BUFF_SIZE;
fa0499
+    buf = apr_bucket_alloc(*len, a->list); /* XXX: check for failure? */
fa0499
+
fa0499
+    rv = apr_file_read(out, buf, len);
fa0499
+
fa0499
+    if (rv != APR_SUCCESS && rv != APR_EOF) {
fa0499
+        apr_bucket_free(buf);
fa0499
+        return rv;
fa0499
+    }
fa0499
+
fa0499
+    if (*len > 0) {
fa0499
+        struct cgi_bucket_data *data = a->data;
fa0499
+        apr_bucket_heap *h;
fa0499
+
fa0499
+        /* Change the current bucket to refer to what we read */
fa0499
+        a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
fa0499
+        h = a->data;
fa0499
+        h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
fa0499
+        *str = buf;
fa0499
+        APR_BUCKET_INSERT_AFTER(a, cgi_bucket_dup(data, a->list));
fa0499
+    }
fa0499
+    else {
fa0499
+        apr_bucket_free(buf);
fa0499
+        a = apr_bucket_immortal_make(a, "", 0);
fa0499
+        *str = a->data;
fa0499
+    }
fa0499
+    return rv;
fa0499
+}
fa0499
+
fa0499
+/* Read method of CGI bucket: polls on stderr and stdout of the child,
fa0499
+ * sending any stderr output immediately away to the error log. */
fa0499
+static apr_status_t cgi_bucket_read(apr_bucket *b, const char **str,
fa0499
+                                    apr_size_t *len, apr_read_type_e block)
fa0499
+{
fa0499
+    struct cgi_bucket_data *data = b->data;
fa0499
+    apr_interval_time_t timeout = 0;
fa0499
+    apr_status_t rv;
fa0499
+    int gotdata = 0;
fa0499
+
fa0499
+    if (block != APR_NONBLOCK_READ) {
fa0499
+        timeout = data->timeout > 0 ? data->timeout : data->r->server->timeout;
fa0499
+    }
fa0499
+
fa0499
+    do {
fa0499
+        const apr_pollfd_t *results;
fa0499
+        apr_int32_t num;
fa0499
+
fa0499
+        rv = apr_pollset_poll(data->pollset, timeout, &num, &results);
fa0499
+        if (APR_STATUS_IS_TIMEUP(rv)) {
fa0499
+            if (timeout) {
fa0499
+                ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, data->r, APLOGNO(01220)
fa0499
+                              "Timeout waiting for output from CGI script %s",
fa0499
+                              data->r->filename);
fa0499
+                return rv;
fa0499
+            }
fa0499
+            else {
fa0499
+                return APR_EAGAIN;
fa0499
+            }
fa0499
+        }
fa0499
+        else if (APR_STATUS_IS_EINTR(rv)) {
fa0499
+            continue;
fa0499
+        }
fa0499
+        else if (rv != APR_SUCCESS) {
fa0499
+            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, data->r, APLOGNO(01221)
fa0499
+                          "poll failed waiting for CGI child");
fa0499
+            return rv;
fa0499
+        }
fa0499
+
fa0499
+        for (; num; num--, results++) {
fa0499
+            if (results[0].client_data == (void *)1) {
fa0499
+                /* stdout */
fa0499
+                rv = cgi_read_stdout(b, results[0].desc.f, str, len);
fa0499
+                if (APR_STATUS_IS_EOF(rv)) {
fa0499
+                    rv = APR_SUCCESS;
fa0499
+                }
fa0499
+                gotdata = 1;
fa0499
+            } else {
fa0499
+                /* stderr */
fa0499
+                apr_status_t rv2 = log_script_err(data->r, results[0].desc.f);
fa0499
+                if (APR_STATUS_IS_EOF(rv2)) {
fa0499
+                    apr_pollset_remove(data->pollset, &results[0]);
fa0499
+                }
fa0499
+            }
fa0499
+        }
fa0499
+
fa0499
+    } while (!gotdata);
fa0499
+
fa0499
+    return rv;
fa0499
+}
fa0499
+
fa0499
+static const apr_bucket_type_t bucket_type_cgi = {
fa0499
+    "CGI", 5, APR_BUCKET_DATA,
fa0499
+    apr_bucket_destroy_noop,
fa0499
+    cgi_bucket_read,
fa0499
+    apr_bucket_setaside_notimpl,
fa0499
+    apr_bucket_split_notimpl,
fa0499
+    apr_bucket_copy_notimpl
fa0499
+};
fa0499
+
fa0499
+#endif /* WANT_CGI_BUCKET */
fa0499
+
fa0499
+/* Handle the CGI response output, having set up the brigade with the
fa0499
+ * CGI or PIPE bucket as appropriate. */
fa0499
+static int cgi_handle_response(request_rec *r, int nph, apr_bucket_brigade *bb,
fa0499
+                               apr_interval_time_t timeout, cgi_server_conf *conf,
fa0499
+                               char *logdata, apr_file_t *script_err)
fa0499
+{
fa0499
+    apr_status_t rv;
fa0499
+    
fa0499
+    /* Handle script return... */
fa0499
+    if (!nph) {
fa0499
+        const char *location;
fa0499
+        char sbuf[MAX_STRING_LEN];
fa0499
+        int ret;
fa0499
+
fa0499
+        if ((ret = ap_scan_script_header_err_brigade_ex(r, bb, sbuf,
fa0499
+                                                        APLOG_MODULE_INDEX)))
fa0499
+        {
fa0499
+            ret = log_script(r, conf, ret, logdata, sbuf, bb, script_err);
fa0499
+
fa0499
+            /*
fa0499
+             * ret could be HTTP_NOT_MODIFIED in the case that the CGI script
fa0499
+             * does not set an explicit status and ap_meets_conditions, which
fa0499
+             * is called by ap_scan_script_header_err_brigade, detects that
fa0499
+             * the conditions of the requests are met and the response is
fa0499
+             * not modified.
fa0499
+             * In this case set r->status and return OK in order to prevent
fa0499
+             * running through the error processing stack as this would
fa0499
+             * break with mod_cache, if the conditions had been set by
fa0499
+             * mod_cache itself to validate a stale entity.
fa0499
+             * BTW: We circumvent the error processing stack anyway if the
fa0499
+             * CGI script set an explicit status code (whatever it is) and
fa0499
+             * the only possible values for ret here are:
fa0499
+             *
fa0499
+             * HTTP_NOT_MODIFIED          (set by ap_meets_conditions)
fa0499
+             * HTTP_PRECONDITION_FAILED   (set by ap_meets_conditions)
fa0499
+             * HTTP_INTERNAL_SERVER_ERROR (if something went wrong during the
fa0499
+             * processing of the response of the CGI script, e.g broken headers
fa0499
+             * or a crashed CGI process).
fa0499
+             */
fa0499
+            if (ret == HTTP_NOT_MODIFIED) {
fa0499
+                r->status = ret;
fa0499
+                return OK;
fa0499
+            }
fa0499
+
fa0499
+            return ret;
fa0499
+        }
fa0499
+
fa0499
+        location = apr_table_get(r->headers_out, "Location");
fa0499
+
fa0499
+        if (location && r->status == 200) {
fa0499
+            /* For a redirect whether internal or not, discard any
fa0499
+             * remaining stdout from the script, and log any remaining
fa0499
+             * stderr output, as normal. */
fa0499
+            discard_script_output(bb);
fa0499
+            apr_brigade_destroy(bb);
fa0499
+
fa0499
+            if (script_err) {
fa0499
+                apr_file_pipe_timeout_set(script_err, timeout);
fa0499
+                log_script_err(r, script_err);
fa0499
+            }
fa0499
+        }
fa0499
+
fa0499
+        if (location && location[0] == '/' && r->status == 200) {
fa0499
+            /* This redirect needs to be a GET no matter what the original
fa0499
+             * method was.
fa0499
+             */
fa0499
+            r->method = "GET";
fa0499
+            r->method_number = M_GET;
fa0499
+
fa0499
+            /* We already read the message body (if any), so don't allow
fa0499
+             * the redirected request to think it has one.  We can ignore
fa0499
+             * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR.
fa0499
+             */
fa0499
+            apr_table_unset(r->headers_in, "Content-Length");
fa0499
+
fa0499
+            ap_internal_redirect_handler(location, r);
fa0499
+            return OK;
fa0499
+        }
fa0499
+        else if (location && r->status == 200) {
fa0499
+            /* XXX: Note that if a script wants to produce its own Redirect
fa0499
+             * body, it now has to explicitly *say* "Status: 302"
fa0499
+             */
fa0499
+            discard_script_output(bb);
fa0499
+            apr_brigade_destroy(bb);
fa0499
+            return HTTP_MOVED_TEMPORARILY;
fa0499
+        }
fa0499
+
fa0499
+        rv = ap_pass_brigade(r->output_filters, bb);
fa0499
+    }
fa0499
+    else /* nph */ {
fa0499
+        struct ap_filter_t *cur;
fa0499
+
fa0499
+        /* get rid of all filters up through protocol...  since we
fa0499
+         * haven't parsed off the headers, there is no way they can
fa0499
+         * work
fa0499
+         */
fa0499
+
fa0499
+        cur = r->proto_output_filters;
fa0499
+        while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) {
fa0499
+            cur = cur->next;
fa0499
+        }
fa0499
+        r->output_filters = r->proto_output_filters = cur;
fa0499
+
fa0499
+        rv = ap_pass_brigade(r->output_filters, bb);
fa0499
+    }
fa0499
+
fa0499
+    /* don't soak up script output if errors occurred writing it
fa0499
+     * out...  otherwise, we prolong the life of the script when the
fa0499
+     * connection drops or we stopped sending output for some other
fa0499
+     * reason */
fa0499
+    if (script_err && rv == APR_SUCCESS && !r->connection->aborted) {
fa0499
+        apr_file_pipe_timeout_set(script_err, timeout);
fa0499
+        log_script_err(r, script_err);
fa0499
+    }
fa0499
+
fa0499
+    if (script_err) apr_file_close(script_err);
fa0499
+
fa0499
+    return OK;                      /* NOT r->status, even if it has changed. */
fa0499
+}