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