diff --git a/src/ngx_http_lua_pipe.c b/src/ngx_http_lua_pipe.c index e842366571..fa259b42b7 100644 --- a/src/ngx_http_lua_pipe.c +++ b/src/ngx_http_lua_pipe.c @@ -30,6 +30,7 @@ static ssize_t ngx_http_lua_pipe_fd_read(ngx_connection_t *c, u_char *buf, size_t size); static ssize_t ngx_http_lua_pipe_fd_write(ngx_connection_t *c, u_char *buf, size_t size); +static int ngx_http_lua_pipe_create(int pipefd[2]); static void ngx_http_lua_pipe_close_helper(ngx_http_lua_pipe_t *pipe, ngx_http_lua_pipe_ctx_t *pipe_ctx, ngx_event_t *ev); static void ngx_http_lua_pipe_close_stdin(ngx_http_lua_pipe_t *pipe); @@ -598,6 +599,32 @@ ngx_http_lua_execvpe(const char *program, char * const argv[], #endif +static int +ngx_http_lua_pipe_create(int pipefd[2]) +{ +#if (NGX_HTTP_LUA_HAVE_PIPE2) + return pipe2(pipefd, O_CLOEXEC); + +#else + ngx_err_t err; + + if (pipe(pipefd) == -1) { + return -1; + } + + if (ngx_cloexec(pipefd[0]) == -1 || ngx_cloexec(pipefd[1]) == -1) { + err = ngx_errno; + (void) close(pipefd[0]); + (void) close(pipefd[1]); + ngx_set_errno(err); + return -1; + } + + return 0; +#endif +} + + int ngx_http_lua_ffi_pipe_spawn(ngx_http_request_t *r, ngx_http_lua_ffi_pipe_proc_t *proc, @@ -642,7 +669,7 @@ ngx_http_lua_ffi_pipe_spawn(ngx_http_request_t *r, goto free_pool; } - rc = pipe(in); + rc = ngx_http_lua_pipe_create(in); if (rc == -1) { *errbuf_size = ngx_snprintf(errbuf, *errbuf_size, "pipe failed: %s", strerror(errno)) @@ -650,7 +677,7 @@ ngx_http_lua_ffi_pipe_spawn(ngx_http_request_t *r, goto free_pool; } - rc = pipe(out); + rc = ngx_http_lua_pipe_create(out); if (rc == -1) { *errbuf_size = ngx_snprintf(errbuf, *errbuf_size, "pipe failed: %s", strerror(errno)) @@ -659,7 +686,7 @@ ngx_http_lua_ffi_pipe_spawn(ngx_http_request_t *r, } if (!merge_stderr) { - rc = pipe(err); + rc = ngx_http_lua_pipe_create(err); if (rc == -1) { *errbuf_size = ngx_snprintf(errbuf, *errbuf_size, "pipe failed: %s", strerror(errno)) diff --git a/t/200-pipe-fd-isolation.t b/t/200-pipe-fd-isolation.t new file mode 100644 index 0000000000..5abd1bc232 --- /dev/null +++ b/t/200-pipe-fd-isolation.t @@ -0,0 +1,69 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: + +use Test::Nginx::Socket::Lua; + +repeat_each(1); + +plan tests => repeat_each() * blocks() * 2; + +no_long_string(); + +add_block_preprocessor(sub { + my $block = shift; + + my $http_config = $block->http_config || ''; + $http_config .= <<'_EOC_'; + lua_package_path "../lua-resty-core/lib/?.lua;../lua-resty-lrucache/lib/?.lua;;"; + + init_by_lua_block { + require "resty.core" + } +_EOC_ + $block->set_value("http_config", $http_config); +}); + +run_tests(); + +__DATA__ + +=== TEST 1: a later subprocess must not retain an earlier subprocess stdin +--- config + location = /t { + content_by_lua_block { + local pipe = require("ngx.pipe") + local first, err = pipe.spawn({"sh", "-c", + "cat; printf stderr >&2"}) + assert(first, err) + + local bytes + bytes, err = first:write("hello\n") + assert(bytes, err) + + local second + second, err = pipe.spawn({"sleep", "2"}) + assert(second, err) + + local ok + ok, err = first:shutdown("stdin") + assert(ok, err) + + first:set_timeouts(nil, 500, 500) + local stdout + stdout, err = first:stdout_read_all() + assert(stdout and not err, err) + + local stderr + stderr, err = first:stderr_read_all() + assert(stderr and not err, err) + + ngx.print(stdout) + ngx.say(stderr) + } + } +--- request +GET /t +--- response_body +hello +stderr +--- no_error_log +[error]