[mod_fastcgi,mod_scgi] parse bin_path at startup

This commit is contained in:
Glenn Strauss 2017-07-01 10:25:41 -04:00
parent 35c4bf5857
commit 6e1c02f386
2 changed files with 52 additions and 55 deletions

View File

@ -50,6 +50,13 @@
*
*/
typedef struct {
char **ptr;
size_t size;
size_t used;
} char_array;
typedef struct fcgi_proc {
size_t id; /* id will be between 1 and max_procs */
buffer *unixsocket; /* config.socket + "-" + id */
@ -243,6 +250,8 @@ typedef struct {
int listen_backlog;
int refcount;
char_array args;
} fcgi_extension_host;
/*
@ -294,13 +303,6 @@ typedef struct {
unsigned int debug;
} plugin_config;
typedef struct {
char **ptr;
size_t size;
size_t used;
} char_array;
/* generic plugin data, shared between all connections */
typedef struct {
PLUGIN_DATA;
@ -546,8 +548,9 @@ static void fastcgi_host_free(fcgi_extension_host *h) {
fastcgi_process_free(h->first);
fastcgi_process_free(h->unused_procs);
for (size_t i = 0; i < h->args.used; ++i) free(h->args.ptr[i]);
free(h->args.ptr);
free(h);
}
static fcgi_exts *fastcgi_extensions_init(void) {
@ -868,6 +871,7 @@ static int env_add(char_array *env, const char *key, size_t key_len, const char
static int parse_binpath(char_array *env, buffer *b) {
char *start;
size_t i;
char c;
/* search for spaces */
start = b->ptr;
@ -885,9 +889,10 @@ static int parse_binpath(char_array *env, buffer *b) {
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}
c = b->ptr[i];
b->ptr[i] = '\0';
env->ptr[env->used++] = start;
env->ptr[env->used++] = strdup(start);
b->ptr[i] = c;
start = b->ptr + i + 1;
break;
@ -905,7 +910,7 @@ static int parse_binpath(char_array *env, buffer *b) {
}
/* the rest */
env->ptr[env->used++] = start;
env->ptr[env->used++] = strdup(start);
if (env->size == 0) {
env->size = 16;
@ -984,8 +989,6 @@ static int fcgi_spawn_connection(server *srv,
if (-1 == status) {
/* server is not up, spawn it */
char_array env;
char_array arg;
buffer *bin_path = NULL;
size_t i;
int val;
int dfd = -1;
@ -1028,10 +1031,6 @@ static int fcgi_spawn_connection(server *srv,
env.size = 0;
env.used = 0;
arg.ptr = NULL;
arg.size = 0;
arg.used = 0;
/* build clean environment */
if (host->bin_env_copy->used) {
for (i = 0; i < host->bin_env_copy->used; i++) {
@ -1071,25 +1070,18 @@ static int fcgi_spawn_connection(server *srv,
}
env.ptr[env.used] = NULL;
bin_path = buffer_init_buffer(host->bin_path);
parse_binpath(&arg, bin_path);
}
dfd = fdevent_open_dirname(arg.ptr[0]);
dfd = fdevent_open_dirname(host->args.ptr[0]);
if (-1 == dfd) {
log_error_write(srv, __FILE__, __LINE__, "sss", "open dirname failed:", strerror(errno), arg.ptr[0]);
log_error_write(srv, __FILE__, __LINE__, "sss", "open dirname failed:", strerror(errno), host->args.ptr[0]);
}
/*(FCGI_LISTENSOCK_FILENO == STDIN_FILENO == 0)*/
proc->pid = (dfd >= 0) ? fdevent_fork_execve(arg.ptr[0], arg.ptr, env.ptr, fcgi_fd, -1, -1, dfd) : -1;
proc->pid = (dfd >= 0) ? fdevent_fork_execve(host->args.ptr[0], host->args.ptr, env.ptr, fcgi_fd, -1, -1, dfd) : -1;
for (i = 0; i < env.used; ++i) free(env.ptr[i]);
free(env.ptr);
/*(arg[] contains string references into bin_path)*/
/*for (i = 0; i < arg.used; ++i) free(arg.ptr[i]);*/
free(arg.ptr);
buffer_free(bin_path);
if (-1 != dfd) close(dfd);
close(fcgi_fd);
@ -1379,16 +1371,13 @@ SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
size_t pno;
struct stat st;
size_t nchars = strcspn(host->bin_path->ptr, " \t");
char c = host->bin_path->ptr[nchars];
host->bin_path->ptr[nchars] = '\0';
if (0 == nchars || 0 != stat(host->bin_path->ptr, &st) || !S_ISREG(st.st_mode) || !(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
host->bin_path->ptr[nchars] = c;
parse_binpath(&host->args, host->bin_path);
if (0 != stat(host->args.ptr[0], &st) || !S_ISREG(st.st_mode) || !(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
log_error_write(srv, __FILE__, __LINE__, "SSs",
"invalid \"bin-path\" => \"", host->bin_path->ptr,
"\" (check that file exists, is regular file, and is executable by lighttpd)");
}
host->bin_path->ptr[nchars] = c;
if (s->debug) {
log_error_write(srv, __FILE__, __LINE__, "ssbsdsbsd",

View File

@ -42,6 +42,13 @@
*
*/
typedef struct {
char **ptr;
size_t size;
size_t used;
} char_array;
typedef struct scgi_proc {
size_t id; /* id will be between 1 and max_procs */
buffer *socket; /* config.socket + "-" + id */
@ -234,6 +241,8 @@ typedef struct {
int listen_backlog;
int refcount;
char_array args;
} scgi_extension_host;
/*
@ -283,13 +292,6 @@ typedef struct {
int debug;
} plugin_config;
typedef struct {
char **ptr;
size_t size;
size_t used;
} char_array;
/* generic plugin data, shared between all connections */
typedef struct {
PLUGIN_DATA;
@ -436,8 +438,9 @@ static void scgi_host_free(scgi_extension_host *h) {
scgi_process_free(h->first);
scgi_process_free(h->unused_procs);
for (size_t i = 0; i < h->args.used; ++i) free(h->args.ptr[i]);
free(h->args.ptr);
free(h);
}
static scgi_exts *scgi_extensions_init(void) {
@ -793,8 +796,6 @@ static int scgi_spawn_connection(server *srv,
if (-1 == status) {
/* server is not up, spawn in */
char_array env;
char *args[4];
buffer *b;
size_t i;
int val;
@ -878,21 +879,10 @@ static int scgi_spawn_connection(server *srv,
env.ptr[env.used] = NULL;
}
/*(preserve prior behavior for exec of command)*/
/*(admin should really prefer to put any complex command into script)*/
b = buffer_init();
buffer_copy_string_len(b, CONST_STR_LEN("exec "));
buffer_append_string_buffer(b, host->bin_path);
*(const char **)&args[0] = "/bin/sh";
*(const char **)&args[1] = "-c";
*(const char **)&args[2] = b->ptr;
args[3] = NULL;
proc->pid = fdevent_fork_execve(args[0], args, env.ptr, scgi_fd, -1, -1, -1);
proc->pid = fdevent_fork_execve(host->args.ptr[0], host->args.ptr, env.ptr, scgi_fd, -1, -1, -1);
for (i = 0; i < env.used; ++i) free(env.ptr[i]);
free(env.ptr);
buffer_free(b);
close(scgi_fd);
if (-1 == proc->pid) {
@ -1188,6 +1178,24 @@ SETDEFAULTS_FUNC(mod_scgi_set_defaults) {
}
df->bin_path->ptr[nchars] = c;
/*(preserve prior behavior for exec of command)*/
/*(admin should really prefer to put any complex command into script)*/
df->args.ptr = calloc(4, sizeof(char *));
force_assert(df->args.ptr);
df->args.used = 3;
df->args.size = 4;
df->args.ptr[0] = malloc(sizeof("/bin/sh"));
force_assert(df->args.ptr[0]);
memcpy(df->args.ptr[0], "/bin/sh", sizeof("/bin/sh"));
df->args.ptr[1] = malloc(sizeof("-c"));
force_assert(df->args.ptr[1]);
memcpy(df->args.ptr[1], "-c", sizeof("-c"));
df->args.ptr[2] = malloc(sizeof("exec ")-1+buffer_string_length(df->bin_path)+1);
force_assert(df->args.ptr[2]);
memcpy(df->args.ptr[2], "exec ", sizeof("exec ")-1);
memcpy(df->args.ptr[2]+sizeof("exec ")-1, df->bin_path->ptr, buffer_string_length(df->bin_path)+1);
df->args.ptr[3] = NULL;
/* HACK: just to make sure the adaptive spawing is disabled */
df->min_procs = df->max_procs;