mirror of
https://github.com/apache/httpd.git
synced 2025-08-07 04:02:58 +03:00
mod_cgid: Continuation of r1862968, experimental fd passing support.
Split out CGI bucket implementation from mod_cgi and use in both mod_cgi and mod_cgid, bringing stderr handling in mod_cgid up to par with mod_cgi. (There is a lot of code which has been copied between mod_cgi{,d} so there's scope for further reduction of source duplication between the modules using this header) * modules/generators/cgi_common.h: Copied from mod_cgi.c, removed everything but the CGI bucket implementation with only one change: (struct cgi_bucket_data, cgi_bucket_create, cgi_bucket_read): Take a timeout on bucket creation, store and use on reads. * modules/generators/mod_cgi.c [APR_FILES_AS_SOCKETS]: Include cgi_common.h. (cgi_handler): Pass configured timeout to CGI bucket. * modules/generators/mod_cgid.c: Include cgi_common.h. (log_script_err): Copy from mod_cgi.c. (log_script): Use log_script_err. (send_req): Take fd for stderr. (cgid_child_errfn): Handle fd-passing case by writing error to stderr for client to pass through ap_log_rerror. (cgid_handler): Create pipe for stderr, pass write-end to server via send_req, use read-end to create CGI bucket. Handle stderr output in failure paths. PR: 54221 git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1863191 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -590,192 +590,7 @@ static void discard_script_output(apr_bucket_brigade *bb)
|
||||
}
|
||||
|
||||
#if APR_FILES_AS_SOCKETS
|
||||
|
||||
/* A CGI bucket type is needed to catch any output to stderr from the
|
||||
* script; see PR 22030. */
|
||||
static const apr_bucket_type_t bucket_type_cgi;
|
||||
|
||||
struct cgi_bucket_data {
|
||||
apr_pollset_t *pollset;
|
||||
request_rec *r;
|
||||
};
|
||||
|
||||
/* Create a CGI bucket using pipes from script stdout 'out'
|
||||
* and stderr 'err', for request 'r'. */
|
||||
static apr_bucket *cgi_bucket_create(request_rec *r,
|
||||
apr_file_t *out, apr_file_t *err,
|
||||
apr_bucket_alloc_t *list)
|
||||
{
|
||||
apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
|
||||
apr_status_t rv;
|
||||
apr_pollfd_t fd;
|
||||
struct cgi_bucket_data *data = apr_palloc(r->pool, sizeof *data);
|
||||
|
||||
APR_BUCKET_INIT(b);
|
||||
b->free = apr_bucket_free;
|
||||
b->list = list;
|
||||
b->type = &bucket_type_cgi;
|
||||
b->length = (apr_size_t)(-1);
|
||||
b->start = -1;
|
||||
|
||||
/* Create the pollset */
|
||||
rv = apr_pollset_create(&data->pollset, 2, r->pool, 0);
|
||||
if (rv != APR_SUCCESS) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01217)
|
||||
"apr_pollset_create(); check system or user limits");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fd.desc_type = APR_POLL_FILE;
|
||||
fd.reqevents = APR_POLLIN;
|
||||
fd.p = r->pool;
|
||||
fd.desc.f = out; /* script's stdout */
|
||||
fd.client_data = (void *)1;
|
||||
rv = apr_pollset_add(data->pollset, &fd);
|
||||
if (rv != APR_SUCCESS) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01218)
|
||||
"apr_pollset_add(); check system or user limits");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fd.desc.f = err; /* script's stderr */
|
||||
fd.client_data = (void *)2;
|
||||
rv = apr_pollset_add(data->pollset, &fd);
|
||||
if (rv != APR_SUCCESS) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01219)
|
||||
"apr_pollset_add(); check system or user limits");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data->r = r;
|
||||
b->data = data;
|
||||
return b;
|
||||
}
|
||||
|
||||
/* Create a duplicate CGI bucket using given bucket data */
|
||||
static apr_bucket *cgi_bucket_dup(struct cgi_bucket_data *data,
|
||||
apr_bucket_alloc_t *list)
|
||||
{
|
||||
apr_bucket *b = apr_bucket_alloc(sizeof(*b), list);
|
||||
APR_BUCKET_INIT(b);
|
||||
b->free = apr_bucket_free;
|
||||
b->list = list;
|
||||
b->type = &bucket_type_cgi;
|
||||
b->length = (apr_size_t)(-1);
|
||||
b->start = -1;
|
||||
b->data = data;
|
||||
return b;
|
||||
}
|
||||
|
||||
/* Handle stdout from CGI child. Duplicate of logic from the _read
|
||||
* method of the real APR pipe bucket implementation. */
|
||||
static apr_status_t cgi_read_stdout(apr_bucket *a, apr_file_t *out,
|
||||
const char **str, apr_size_t *len)
|
||||
{
|
||||
char *buf;
|
||||
apr_status_t rv;
|
||||
|
||||
*str = NULL;
|
||||
*len = APR_BUCKET_BUFF_SIZE;
|
||||
buf = apr_bucket_alloc(*len, a->list); /* XXX: check for failure? */
|
||||
|
||||
rv = apr_file_read(out, buf, len);
|
||||
|
||||
if (rv != APR_SUCCESS && rv != APR_EOF) {
|
||||
apr_bucket_free(buf);
|
||||
return rv;
|
||||
}
|
||||
|
||||
if (*len > 0) {
|
||||
struct cgi_bucket_data *data = a->data;
|
||||
apr_bucket_heap *h;
|
||||
|
||||
/* Change the current bucket to refer to what we read */
|
||||
a = apr_bucket_heap_make(a, buf, *len, apr_bucket_free);
|
||||
h = a->data;
|
||||
h->alloc_len = APR_BUCKET_BUFF_SIZE; /* note the real buffer size */
|
||||
*str = buf;
|
||||
APR_BUCKET_INSERT_AFTER(a, cgi_bucket_dup(data, a->list));
|
||||
}
|
||||
else {
|
||||
apr_bucket_free(buf);
|
||||
a = apr_bucket_immortal_make(a, "", 0);
|
||||
*str = a->data;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
/* Read method of CGI bucket: polls on stderr and stdout of the child,
|
||||
* sending any stderr output immediately away to the error log. */
|
||||
static apr_status_t cgi_bucket_read(apr_bucket *b, const char **str,
|
||||
apr_size_t *len, apr_read_type_e block)
|
||||
{
|
||||
struct cgi_bucket_data *data = b->data;
|
||||
apr_interval_time_t timeout = 0;
|
||||
apr_status_t rv;
|
||||
int gotdata = 0;
|
||||
cgi_dirconf *dc = ap_get_module_config(data->r->per_dir_config, &cgi_module);
|
||||
|
||||
if (block != APR_NONBLOCK_READ) {
|
||||
timeout = dc->timeout > 0 ? dc->timeout : data->r->server->timeout;
|
||||
}
|
||||
|
||||
do {
|
||||
const apr_pollfd_t *results;
|
||||
apr_int32_t num;
|
||||
|
||||
rv = apr_pollset_poll(data->pollset, timeout, &num, &results);
|
||||
if (APR_STATUS_IS_TIMEUP(rv)) {
|
||||
if (timeout) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, data->r, APLOGNO(01220)
|
||||
"Timeout waiting for output from CGI script %s",
|
||||
data->r->filename);
|
||||
return rv;
|
||||
}
|
||||
else {
|
||||
return APR_EAGAIN;
|
||||
}
|
||||
}
|
||||
else if (APR_STATUS_IS_EINTR(rv)) {
|
||||
continue;
|
||||
}
|
||||
else if (rv != APR_SUCCESS) {
|
||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, data->r, APLOGNO(01221)
|
||||
"poll failed waiting for CGI child");
|
||||
return rv;
|
||||
}
|
||||
|
||||
for (; num; num--, results++) {
|
||||
if (results[0].client_data == (void *)1) {
|
||||
/* stdout */
|
||||
rv = cgi_read_stdout(b, results[0].desc.f, str, len);
|
||||
if (APR_STATUS_IS_EOF(rv)) {
|
||||
rv = APR_SUCCESS;
|
||||
}
|
||||
gotdata = 1;
|
||||
} else {
|
||||
/* stderr */
|
||||
apr_status_t rv2 = log_script_err(data->r, results[0].desc.f);
|
||||
if (APR_STATUS_IS_EOF(rv2)) {
|
||||
apr_pollset_remove(data->pollset, &results[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} while (!gotdata);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
static const apr_bucket_type_t bucket_type_cgi = {
|
||||
"CGI", 5, APR_BUCKET_DATA,
|
||||
apr_bucket_destroy_noop,
|
||||
cgi_bucket_read,
|
||||
apr_bucket_setaside_notimpl,
|
||||
apr_bucket_split_notimpl,
|
||||
apr_bucket_copy_notimpl
|
||||
};
|
||||
|
||||
#include "cgi_common.h"
|
||||
#endif
|
||||
|
||||
static int cgi_handler(request_rec *r)
|
||||
@@ -960,7 +775,7 @@ static int cgi_handler(request_rec *r)
|
||||
apr_file_pipe_timeout_set(script_in, 0);
|
||||
apr_file_pipe_timeout_set(script_err, 0);
|
||||
|
||||
b = cgi_bucket_create(r, script_in, script_err, c->bucket_alloc);
|
||||
b = cgi_bucket_create(r, dc->timeout, script_in, script_err, c->bucket_alloc);
|
||||
if (b == NULL)
|
||||
return HTTP_INTERNAL_SERVER_ERROR;
|
||||
#else
|
||||
|
Reference in New Issue
Block a user