Skip to content

Commit

Permalink
feature: added support for specifying environment variables in ngx.pi…
Browse files Browse the repository at this point in the history
…pe spawn() FFI API.

Signed-off-by: Thibault Charbonnier <thibaultcha@me.com>
  • Loading branch information
spacewander authored and thibaultcha committed May 24, 2019
1 parent 1c756b3 commit a7afd56
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
15 changes: 15 additions & 0 deletions config
Expand Up @@ -557,6 +557,21 @@ CC_TEST_FLAGS="$SAVED_CC_TEST_FLAGS"

# ----------------------------------------

ngx_feature="execvpe"
ngx_feature_libs=
ngx_feature_name="NGX_HTTP_LUA_HAVE_EXECVPE"
ngx_feature_run=no
ngx_feature_incs=
ngx_feature_test='char* argv[] = {"/bin/sh"};execvpe("/bin/sh", argv, NULL);'
SAVED_CC_TEST_FLAGS="$CC_TEST_FLAGS"
CC_TEST_FLAGS="-Werror -Wall $CC_TEST_FLAGS"

. auto/feature

CC_TEST_FLAGS="$SAVED_CC_TEST_FLAGS"

# ----------------------------------------

if test -n "$ngx_module_link"; then
ngx_module_type=HTTP_AUX_FILTER
ngx_module_name=$ngx_addon_name
Expand Down
31 changes: 30 additions & 1 deletion src/ngx_http_lua_pipe.c
Expand Up @@ -551,7 +551,7 @@ ngx_http_lua_pipe_fd_write(ngx_connection_t *c, u_char *buf, size_t size)
int
ngx_http_lua_ffi_pipe_spawn(ngx_http_lua_ffi_pipe_proc_t *proc,
const char *file, const char **argv, int merge_stderr, size_t buffer_size,
u_char *errbuf, size_t *errbuf_size)
const char **environ, u_char *errbuf, size_t *errbuf_size)
{
int rc;
int in[2];
Expand All @@ -571,6 +571,15 @@ ngx_http_lua_ffi_pipe_spawn(ngx_http_lua_ffi_pipe_proc_t *proc,
ngx_http_lua_pipe_signal_t *sig;
sigset_t set;

#if !(NGX_HTTP_LUA_HAVE_EXECVPE)
if (environ != NULL) {
*errbuf_size = ngx_snprintf(errbuf, *errbuf_size,
"environ option not supported")
- errbuf;
return NGX_ERROR;
}
#endif

pool_size = ngx_align(NGX_MIN_POOL_SIZE + buffer_size * 2,
NGX_POOL_ALIGNMENT);

Expand Down Expand Up @@ -758,11 +767,31 @@ ngx_http_lua_ffi_pipe_spawn(ngx_http_lua_ffi_pipe_proc_t *proc,
}
}

#if (NGX_HTTP_LUA_HAVE_EXECVPE)
if (environ != NULL) {
if (execvpe(file, (char * const *) argv, (char * const *) environ)
== -1)
{
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child execvpe() failed while "
"executing %s", file);
}

} else {
if (execvp(file, (char * const *) argv) == -1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child execvp() failed while "
"executing %s", file);
}
}

#else
if (execvp(file, (char * const *) argv) == -1) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
"lua pipe child execvp() failed while executing %s",
file);
}
#endif

exit(EXIT_FAILURE);
}
Expand Down

0 comments on commit a7afd56

Please sign in to comment.