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