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