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