summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn Strauss <gstrauss@gluelogic.com>2023-04-30 18:07:52 -0400
committerGlenn Strauss <gstrauss@gluelogic.com>2023-05-03 23:11:35 -0400
commit424d7d99b3340aaa45bfb7ef58b72788141cff2c (patch)
treec388289fe96a0aa030285852b225b3d76bc9f4c5
parentc3710a8bff7623a2ba5b1cc76e55c3567ec95bb8 (diff)
downloadlighttpd-git-424d7d99b3340aaa45bfb7ef58b72788141cff2c.tar.gz
[mod_cgi] reuse fd already opened to /dev/null
lighttpd STDIN_FILENO is reopened to /dev/null at server startup Let CGI inherit fd STDIN_FILENO when (0 == r->reqbody_length)
-rw-r--r--src/mod_cgi.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/src/mod_cgi.c b/src/mod_cgi.c
index d7b7e04a..7af7a40f 100644
--- a/src/mod_cgi.c
+++ b/src/mod_cgi.c
@@ -795,6 +795,9 @@ static int cgi_write_request(handler_ctx *hctx, int fd) {
return 0;
}
+/* lighttpd STDIN_FILENO is reopened to /dev/null, inheritable by children */
+#define MOD_CGI_INHERIT_STDIN_DEV_NULL
+
__attribute_cold__
static int cgi_create_err (request_st * const r, int cgi_fds[4], const char *msg)
{
@@ -809,7 +812,9 @@ static int cgi_create_err (request_st * const r, int cgi_fds[4], const char *msg
int * const to_cgi_fds = cgi_fds; /* some fd might be -1; not checking */
if (0 == r->reqbody_length) {
+ #ifndef MOD_CGI_INHERIT_STDIN_DEV_NULL
fdio_close_file(to_cgi_fds[0]); /* /dev/null */
+ #endif
}
else if (-1 != to_cgi_fds[1]) { /* not (shared) open file in chunkqueue */
fdio_close_pipe(to_cgi_fds[0]);
@@ -838,12 +843,12 @@ static int cgi_create_env(request_st * const r, plugin_data * const p, handler_c
#endif
if (0 == r->reqbody_length) {
- /* future: might keep fd open in p->devnull for reuse
- * and dup() here, or do not close() (later in this func) */
+ #ifndef MOD_CGI_INHERIT_STDIN_DEV_NULL
to_cgi_fds[0] = fdevent_open_devnull();
if (-1 == to_cgi_fds[0]) {
return cgi_create_err(r, cgi_fds, "open() /dev/null");
}
+ #endif
}
else if (!(r->conf.stream_request_body /*(if not streaming request body)*/
& (FDEVENT_STREAM_REQUEST|FDEVENT_STREAM_REQUEST_BUFMIN))
@@ -866,7 +871,7 @@ static int cgi_create_env(request_st * const r, plugin_data * const p, handler_c
}
#ifdef _WIN32
- if (-1 == to_cgi_fds[0]) {
+ if (-1 == to_cgi_fds[0] && 0 != r->reqbody_length) {
if (0 != fdevent_socketpair_cloexec(AF_INET,SOCK_STREAM,0,to_cgi_fds))
return cgi_create_err(r, cgi_fds, "socketpair()");
if (0 != fdevent_fcntl_set_nb(to_cgi_fds[1]))
@@ -883,7 +888,7 @@ static int cgi_create_env(request_st * const r, plugin_data * const p, handler_c
from_cgi_fds[1] = tmpfd;
#else
unsigned int bufsz_hint = 16384;
- if (-1 == to_cgi_fds[0]) {
+ if (-1 == to_cgi_fds[0] && 0 != r->reqbody_length) {
if (0 != fdevent_pipe_cloexec(to_cgi_fds, bufsz_hint))
return cgi_create_err(r, cgi_fds, "pipe()");
if (0 != fdevent_fcntl_set_nb(to_cgi_fds[1]))
@@ -1046,7 +1051,9 @@ static int cgi_create_env(request_st * const r, plugin_data * const p, handler_c
hctx->cgi_pid = cgi_pid_add(p, pid, hctx);
if (0 == r->reqbody_length) {
+ #ifndef MOD_CGI_INHERIT_STDIN_DEV_NULL
fdio_close_file(to_cgi_fds[0]);
+ #endif
}
else if (-1 == to_cgi_fds[1]) {
chunkqueue * const cq = &r->reqbody_queue;