mirror of
https://github.com/apache/httpd.git
synced 2025-08-08 15:02:10 +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:
@@ -1 +1 @@
|
|||||||
10176
|
10177
|
||||||
|
216
modules/generators/cgi_common.h
Normal file
216
modules/generators/cgi_common.h
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership.
|
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||||
|
* (the "License"); you may not use this file except in compliance with
|
||||||
|
* the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "apr.h"
|
||||||
|
#include "apr_strings.h"
|
||||||
|
#include "apr_buckets.h"
|
||||||
|
#include "apr_lib.h"
|
||||||
|
#include "apr_poll.h"
|
||||||
|
|
||||||
|
#define APR_WANT_STRFUNC
|
||||||
|
#define APR_WANT_MEMFUNC
|
||||||
|
#include "apr_want.h"
|
||||||
|
|
||||||
|
#include "httpd.h"
|
||||||
|
#include "util_filter.h"
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
apr_interval_time_t timeout;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* 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_interval_time_t timeout,
|
||||||
|
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;
|
||||||
|
data->timeout = timeout;
|
||||||
|
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;
|
||||||
|
|
||||||
|
if (block != APR_NONBLOCK_READ) {
|
||||||
|
timeout = data->timeout > 0 ? data->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
|
||||||
|
};
|
||||||
|
|
@@ -590,192 +590,7 @@ static void discard_script_output(apr_bucket_brigade *bb)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if APR_FILES_AS_SOCKETS
|
#if APR_FILES_AS_SOCKETS
|
||||||
|
#include "cgi_common.h"
|
||||||
/* 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
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int cgi_handler(request_rec *r)
|
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_in, 0);
|
||||||
apr_file_pipe_timeout_set(script_err, 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)
|
if (b == NULL)
|
||||||
return HTTP_INTERNAL_SERVER_ERROR;
|
return HTTP_INTERNAL_SERVER_ERROR;
|
||||||
#else
|
#else
|
||||||
|
@@ -556,8 +556,8 @@ static apr_status_t get_req(int fd, request_rec *r, char **argv0, char ***env,
|
|||||||
return APR_SUCCESS;
|
return APR_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static apr_status_t send_req(int fd, request_rec *r, char *argv0, char **env,
|
static apr_status_t send_req(int fd, apr_file_t *errpipe, request_rec *r,
|
||||||
int req_type)
|
char *argv0, char **env, int req_type)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
cgid_req_t req = {0};
|
cgid_req_t req = {0};
|
||||||
@@ -585,8 +585,8 @@ static apr_status_t send_req(int fd, request_rec *r, char *argv0, char **env,
|
|||||||
req.args_len = r->args ? strlen(r->args) : 0;
|
req.args_len = r->args ? strlen(r->args) : 0;
|
||||||
req.loglevel = r->server->log.level;
|
req.loglevel = r->server->log.level;
|
||||||
|
|
||||||
if (r->server->error_log)
|
if (errpipe)
|
||||||
apr_os_file_get(&errfd, r->server->error_log);
|
apr_os_file_get(&errfd, errpipe);
|
||||||
else
|
else
|
||||||
errfd = 0;
|
errfd = 0;
|
||||||
|
|
||||||
@@ -665,20 +665,34 @@ static void daemon_signal_handler(int sig)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Callback executed in the forked child process if exec of the CGI
|
||||||
|
* script fails. For the fd-passing case, output to stderr goes to
|
||||||
|
* the client (request handling thread) and is logged via
|
||||||
|
* ap_log_rerror there. For the non-fd-passing case, the "fake"
|
||||||
|
* request_rec passed via userdata is used to log. */
|
||||||
static void cgid_child_errfn(apr_pool_t *pool, apr_status_t err,
|
static void cgid_child_errfn(apr_pool_t *pool, apr_status_t err,
|
||||||
const char *description)
|
const char *description)
|
||||||
{
|
{
|
||||||
request_rec *r;
|
|
||||||
void *vr;
|
void *vr;
|
||||||
|
|
||||||
apr_pool_userdata_get(&vr, ERRFN_USERDATA_KEY, pool);
|
apr_pool_userdata_get(&vr, ERRFN_USERDATA_KEY, pool);
|
||||||
r = vr;
|
if (vr) {
|
||||||
|
request_rec *r = vr;
|
||||||
/* sure we got r, but don't call ap_log_rerror() because we don't
|
|
||||||
* have r->headers_in and possibly other storage referenced by
|
/* sure we got r, but don't call ap_log_rerror() because we don't
|
||||||
* ap_log_rerror()
|
* have r->headers_in and possibly other storage referenced by
|
||||||
*/
|
* ap_log_rerror()
|
||||||
ap_log_error(APLOG_MARK, APLOG_ERR, err, r->server, APLOGNO(01241) "%s", description);
|
*/
|
||||||
|
ap_log_error(APLOG_MARK, APLOG_ERR, err, r->server, APLOGNO(01241) "%s", description);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const char *logstr;
|
||||||
|
|
||||||
|
logstr = apr_psprintf(pool, APLOGNO(01241) "error spawning CGI child: %s (%pm)\n",
|
||||||
|
description, &err);
|
||||||
|
fputs(logstr, stderr);
|
||||||
|
fflush(stderr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cgid_server(void *data)
|
static int cgid_server(void *data)
|
||||||
@@ -893,7 +907,10 @@ static int cgid_server(void *data)
|
|||||||
close(sd2);
|
close(sd2);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
apr_pool_userdata_set(r, ERRFN_USERDATA_KEY, apr_pool_cleanup_null, ptrans);
|
if (errfileno == STDERR_FILENO) {
|
||||||
|
/* Used by cgid_child_errfn without fd-passing. */
|
||||||
|
apr_pool_userdata_set(r, ERRFN_USERDATA_KEY, apr_pool_cleanup_null, ptrans);
|
||||||
|
}
|
||||||
|
|
||||||
argv = (const char * const *)create_argv(r->pool, NULL, NULL, NULL, argv0, r->args);
|
argv = (const char * const *)create_argv(r->pool, NULL, NULL, NULL, argv0, r->args);
|
||||||
|
|
||||||
@@ -1192,6 +1209,33 @@ static int log_scripterror(request_rec *r, cgid_server_conf * conf, int ret,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Soak up stderr from a script and redirect it to the error log.
|
||||||
|
* TODO: log_scripterror() and this could move to cgi_common.h. */
|
||||||
|
static apr_status_t log_script_err(request_rec *r, apr_file_t *script_err)
|
||||||
|
{
|
||||||
|
char argsbuffer[HUGE_STRING_LEN];
|
||||||
|
char *newline;
|
||||||
|
apr_status_t rv;
|
||||||
|
cgid_server_conf *conf = ap_get_module_config(r->server->module_config, &cgid_module);
|
||||||
|
|
||||||
|
while ((rv = apr_file_gets(argsbuffer, HUGE_STRING_LEN,
|
||||||
|
script_err)) == APR_SUCCESS) {
|
||||||
|
|
||||||
|
newline = strchr(argsbuffer, '\n');
|
||||||
|
if (newline) {
|
||||||
|
char *prev = newline - 1;
|
||||||
|
if (prev >= argsbuffer && *prev == '\r') {
|
||||||
|
newline = prev;
|
||||||
|
}
|
||||||
|
|
||||||
|
*newline = '\0';
|
||||||
|
}
|
||||||
|
log_scripterror(r, conf, r->status, 0, argsbuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
static int log_script(request_rec *r, cgid_server_conf * conf, int ret,
|
static int log_script(request_rec *r, cgid_server_conf * conf, int ret,
|
||||||
char *dbuf, const char *sbuf, apr_bucket_brigade *bb,
|
char *dbuf, const char *sbuf, apr_bucket_brigade *bb,
|
||||||
apr_file_t *script_err)
|
apr_file_t *script_err)
|
||||||
@@ -1297,6 +1341,11 @@ static int log_script(request_rec *r, cgid_server_conf * conf, int ret,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_CGID_FDPASSING
|
||||||
|
/* Pull in CGI bucket implementation. */
|
||||||
|
#include "cgi_common.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
static int connect_to_daemon(int *sdptr, request_rec *r,
|
static int connect_to_daemon(int *sdptr, request_rec *r,
|
||||||
cgid_server_conf *conf)
|
cgid_server_conf *conf)
|
||||||
{
|
{
|
||||||
@@ -1488,6 +1537,7 @@ static apr_status_t cleanup_script(void *vptr)
|
|||||||
|
|
||||||
static int cgid_handler(request_rec *r)
|
static int cgid_handler(request_rec *r)
|
||||||
{
|
{
|
||||||
|
conn_rec *c = r->connection;
|
||||||
int retval, nph, dbpos;
|
int retval, nph, dbpos;
|
||||||
char *argv0, *dbuf;
|
char *argv0, *dbuf;
|
||||||
apr_bucket_brigade *bb;
|
apr_bucket_brigade *bb;
|
||||||
@@ -1497,10 +1547,11 @@ static int cgid_handler(request_rec *r)
|
|||||||
int seen_eos, child_stopped_reading;
|
int seen_eos, child_stopped_reading;
|
||||||
int sd;
|
int sd;
|
||||||
char **env;
|
char **env;
|
||||||
apr_file_t *tempsock;
|
apr_file_t *tempsock, *script_err, *errpipe_out;
|
||||||
struct cleanup_script_info *info;
|
struct cleanup_script_info *info;
|
||||||
apr_status_t rv;
|
apr_status_t rv;
|
||||||
cgid_dirconf *dc;
|
cgid_dirconf *dc;
|
||||||
|
apr_interval_time_t timeout;
|
||||||
|
|
||||||
if (strcmp(r->handler, CGI_MAGIC_TYPE) && strcmp(r->handler, "cgi-script")) {
|
if (strcmp(r->handler, CGI_MAGIC_TYPE) && strcmp(r->handler, "cgi-script")) {
|
||||||
return DECLINED;
|
return DECLINED;
|
||||||
@@ -1509,7 +1560,7 @@ static int cgid_handler(request_rec *r)
|
|||||||
conf = ap_get_module_config(r->server->module_config, &cgid_module);
|
conf = ap_get_module_config(r->server->module_config, &cgid_module);
|
||||||
dc = ap_get_module_config(r->per_dir_config, &cgid_module);
|
dc = ap_get_module_config(r->per_dir_config, &cgid_module);
|
||||||
|
|
||||||
|
timeout = dc->timeout > 0 ? dc->timeout : r->server->timeout;
|
||||||
is_included = !strcmp(r->protocol, "INCLUDED");
|
is_included = !strcmp(r->protocol, "INCLUDED");
|
||||||
|
|
||||||
if ((argv0 = strrchr(r->filename, '/')) != NULL) {
|
if ((argv0 = strrchr(r->filename, '/')) != NULL) {
|
||||||
@@ -1562,6 +1613,17 @@ static int cgid_handler(request_rec *r)
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef HAVE_CGID_FDPASSING
|
||||||
|
rv = apr_file_pipe_create(&script_err, &errpipe_out, r->pool);
|
||||||
|
if (rv) {
|
||||||
|
return log_scripterror(r, conf, HTTP_SERVICE_UNAVAILABLE, rv, APLOGNO(10176)
|
||||||
|
"could not create pipe for stderr");
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
script_err = NULL;
|
||||||
|
errpipe_out = NULL;
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* httpd core function used to add common environment variables like
|
* httpd core function used to add common environment variables like
|
||||||
* DOCUMENT_ROOT.
|
* DOCUMENT_ROOT.
|
||||||
@@ -1574,12 +1636,16 @@ static int cgid_handler(request_rec *r)
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = send_req(sd, r, argv0, env, CGI_REQ);
|
rv = send_req(sd, errpipe_out, r, argv0, env, CGI_REQ);
|
||||||
if (rv != APR_SUCCESS) {
|
if (rv != APR_SUCCESS) {
|
||||||
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01268)
|
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01268)
|
||||||
"write to cgi daemon process");
|
"write to cgi daemon process");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* The write-end of the pipe is only used by the server, so close
|
||||||
|
* it here. */
|
||||||
|
if (errpipe_out) apr_file_close(errpipe_out);
|
||||||
|
|
||||||
info = apr_palloc(r->pool, sizeof(struct cleanup_script_info));
|
info = apr_palloc(r->pool, sizeof(struct cleanup_script_info));
|
||||||
info->conf = conf;
|
info->conf = conf;
|
||||||
info->r = r;
|
info->r = r;
|
||||||
@@ -1601,12 +1667,7 @@ static int cgid_handler(request_rec *r)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
apr_os_pipe_put_ex(&tempsock, &sd, 1, r->pool);
|
apr_os_pipe_put_ex(&tempsock, &sd, 1, r->pool);
|
||||||
if (dc->timeout > 0) {
|
apr_file_pipe_timeout_set(tempsock, timeout);
|
||||||
apr_file_pipe_timeout_set(tempsock, dc->timeout);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
apr_file_pipe_timeout_set(tempsock, r->server->timeout);
|
|
||||||
}
|
|
||||||
apr_pool_cleanup_kill(r->pool, (void *)((long)sd), close_unix_socket);
|
apr_pool_cleanup_kill(r->pool, (void *)((long)sd), close_unix_socket);
|
||||||
|
|
||||||
/* Transfer any put/post args, CERN style...
|
/* Transfer any put/post args, CERN style...
|
||||||
@@ -1698,23 +1759,28 @@ static int cgid_handler(request_rec *r)
|
|||||||
*/
|
*/
|
||||||
shutdown(sd, 1);
|
shutdown(sd, 1);
|
||||||
|
|
||||||
|
bb = apr_brigade_create(r->pool, c->bucket_alloc);
|
||||||
|
#ifdef HAVE_CGID_FDPASSING
|
||||||
|
b = cgi_bucket_create(r, dc->timeout, tempsock, script_err, c->bucket_alloc);
|
||||||
|
if (b == NULL)
|
||||||
|
return HTTP_INTERNAL_SERVER_ERROR; /* should call log_scripterror() w/ _UNAVAILABLE? */
|
||||||
|
#else
|
||||||
|
b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
|
||||||
|
#endif
|
||||||
|
APR_BRIGADE_INSERT_TAIL(bb, b);
|
||||||
|
b = apr_bucket_eos_create(c->bucket_alloc);
|
||||||
|
APR_BRIGADE_INSERT_TAIL(bb, b);
|
||||||
|
|
||||||
/* Handle script return... */
|
/* Handle script return... */
|
||||||
if (!nph) {
|
if (!nph) {
|
||||||
conn_rec *c = r->connection;
|
|
||||||
const char *location;
|
const char *location;
|
||||||
char sbuf[MAX_STRING_LEN];
|
char sbuf[MAX_STRING_LEN];
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
bb = apr_brigade_create(r->pool, c->bucket_alloc);
|
|
||||||
b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
|
|
||||||
APR_BRIGADE_INSERT_TAIL(bb, b);
|
|
||||||
b = apr_bucket_eos_create(c->bucket_alloc);
|
|
||||||
APR_BRIGADE_INSERT_TAIL(bb, b);
|
|
||||||
|
|
||||||
if ((ret = ap_scan_script_header_err_brigade_ex(r, bb, sbuf,
|
if ((ret = ap_scan_script_header_err_brigade_ex(r, bb, sbuf,
|
||||||
APLOG_MODULE_INDEX)))
|
APLOG_MODULE_INDEX)))
|
||||||
{
|
{
|
||||||
ret = log_script(r, conf, ret, dbuf, sbuf, bb, NULL);
|
ret = log_script(r, conf, ret, dbuf, sbuf, bb, script_err);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* ret could be HTTP_NOT_MODIFIED in the case that the CGI script
|
* ret could be HTTP_NOT_MODIFIED in the case that the CGI script
|
||||||
@@ -1751,6 +1817,11 @@ static int cgid_handler(request_rec *r)
|
|||||||
/* Soak up all the script output */
|
/* Soak up all the script output */
|
||||||
discard_script_output(bb);
|
discard_script_output(bb);
|
||||||
apr_brigade_destroy(bb);
|
apr_brigade_destroy(bb);
|
||||||
|
if (script_err) {
|
||||||
|
apr_file_pipe_timeout_set(script_err, timeout);
|
||||||
|
log_script_err(r, script_err);
|
||||||
|
}
|
||||||
|
|
||||||
/* This redirect needs to be a GET no matter what the original
|
/* This redirect needs to be a GET no matter what the original
|
||||||
* method was.
|
* method was.
|
||||||
*/
|
*/
|
||||||
@@ -1783,7 +1854,6 @@ static int cgid_handler(request_rec *r)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (nph) {
|
if (nph) {
|
||||||
conn_rec *c = r->connection;
|
|
||||||
struct ap_filter_t *cur;
|
struct ap_filter_t *cur;
|
||||||
|
|
||||||
/* get rid of all filters up through protocol... since we
|
/* get rid of all filters up through protocol... since we
|
||||||
@@ -1797,14 +1867,20 @@ static int cgid_handler(request_rec *r)
|
|||||||
}
|
}
|
||||||
r->output_filters = r->proto_output_filters = cur;
|
r->output_filters = r->proto_output_filters = cur;
|
||||||
|
|
||||||
bb = apr_brigade_create(r->pool, c->bucket_alloc);
|
rv = ap_pass_brigade(r->output_filters, bb);
|
||||||
b = apr_bucket_pipe_create(tempsock, c->bucket_alloc);
|
|
||||||
APR_BRIGADE_INSERT_TAIL(bb, b);
|
|
||||||
b = apr_bucket_eos_create(c->bucket_alloc);
|
|
||||||
APR_BRIGADE_INSERT_TAIL(bb, b);
|
|
||||||
ap_pass_brigade(r->output_filters, bb);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* don't soak up script output if errors occurred writing it
|
||||||
|
* out... otherwise, we prolong the life of the script when the
|
||||||
|
* connection drops or we stopped sending output for some other
|
||||||
|
* reason */
|
||||||
|
if (script_err && rv == APR_SUCCESS && !r->connection->aborted) {
|
||||||
|
apr_file_pipe_timeout_set(script_err, timeout);
|
||||||
|
log_script_err(r, script_err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (script_err) apr_file_close(script_err);
|
||||||
|
|
||||||
return OK; /* NOT r->status, even if it has changed. */
|
return OK; /* NOT r->status, even if it has changed. */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1922,7 +1998,7 @@ static int include_cmd(include_ctx_t *ctx, ap_filter_t *f,
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
send_req(sd, r, command, env, SSI_REQ);
|
send_req(sd, NULL, r, command, env, SSI_REQ);
|
||||||
|
|
||||||
info = apr_palloc(r->pool, sizeof(struct cleanup_script_info));
|
info = apr_palloc(r->pool, sizeof(struct cleanup_script_info));
|
||||||
info->conf = conf;
|
info->conf = conf;
|
||||||
|
Reference in New Issue
Block a user