You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lighttpd1.4/src/mod_fastcgi.c

3778 lines
97 KiB
C

#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <signal.h>
#include "buffer.h"
#include "server.h"
#include "keyvalue.h"
#include "log.h"
#include "http_chunk.h"
#include "fdevent.h"
#include "connections.h"
#include "response.h"
#include "joblist.h"
#include "plugin.h"
#include "inet_ntop_cache.h"
#include "stat_cache.h"
#include "status_counter.h"
#ifdef HAVE_FASTCGI_FASTCGI_H
#include <fastcgi/fastcgi.h>
#else
#ifdef HAVE_FASTCGI_H
#include <fastcgi.h>
#else
#include "fastcgi.h"
#endif
#endif /* HAVE_FASTCGI_FASTCGI_H */
#include <stdio.h>
#ifdef HAVE_SYS_FILIO_H
# include <sys/filio.h>
#endif
#include "sys-socket.h"
#ifndef UNIX_PATH_MAX
# define UNIX_PATH_MAX 108
#endif
#ifdef HAVE_SYS_UIO_H
#include <sys/uio.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
/*
*
* TODO:
*
* - add timeout for a connect to a non-fastcgi process
* (use state_timestamp + state)
*
*/
typedef struct fcgi_proc {
size_t id; /* id will be between 1 and max_procs */
buffer *unixsocket; /* config.socket + "-" + id */
unsigned port; /* config.port + pno */
buffer *connection_name; /* either tcp:<host>:<port> or unix:<socket> for debuggin purposes */
pid_t pid; /* PID of the spawned process (0 if not spawned locally) */
size_t load; /* number of requests waiting on this process */
time_t last_used; /* see idle_timeout */
size_t requests; /* see max_requests */
struct fcgi_proc *prev, *next; /* see first */
time_t disabled_until; /* this proc is disabled until, use something else until than */
int is_local;
enum {
PROC_STATE_UNSET, /* init-phase */
PROC_STATE_RUNNING, /* alive */
PROC_STATE_OVERLOADED, /* listen-queue is full,
don't send something to this proc for the next 2 seconds */
PROC_STATE_DIED_WAIT_FOR_PID, /* */
PROC_STATE_DIED, /* marked as dead, should be restarted */
PROC_STATE_KILLED /* was killed as we don't have the load anymore */
} state;
} fcgi_proc;
typedef struct {
/* the key that is used to reference this value */
buffer *id;
/* list of processes handling this extension
* sorted by lowest load
*
* whenever a job is done move it up in the list
* until it is sorted, move it down as soon as the
* job is started
*/
fcgi_proc *first;
fcgi_proc *unused_procs;
/*
* spawn at least min_procs, at max_procs.
*
* as soon as the load of the first entry
* is max_load_per_proc we spawn a new one
* and add it to the first entry and give it
* the load
*
*/
unsigned short min_procs;
unsigned short max_procs;
size_t num_procs; /* how many procs are started */
size_t active_procs; /* how many of them are really running */
unsigned short max_load_per_proc;
/*
* kick the process from the list if it was not
* used for idle_timeout until min_procs is
* reached. this helps to get the processlist
* small again we had a small peak load.
*
*/
unsigned short idle_timeout;
/*
* time after a disabled remote connection is tried to be re-enabled
*
*
*/
unsigned short disable_time;
/*
* same fastcgi processes get a little bit larger
* than wanted. max_requests_per_proc kills a
* process after a number of handled requests.
*
*/
size_t max_requests_per_proc;
/* config */
/*
* host:port
*
* if host is one of the local IP adresses the
* whole connection is local
*
* if tcp/ip should be used host AND port have
* to be specified
*
*/
buffer *host;
unsigned short port;
/*
* Unix Domain Socket
*
* instead of TCP/IP we can use Unix Domain Sockets
* - more secure (you have fileperms to play with)
* - more control (on locally)
* - more speed (no extra overhead)
*/
buffer *unixsocket;
/* if socket is local we can start the fastcgi
* process ourself
*
* bin-path is the path to the binary
*
* check min_procs and max_procs for the number
* of process to start-up
*/
buffer *bin_path;
/* bin-path is set bin-environment is taken to
* create the environement before starting the
* FastCGI process
*
*/
array *bin_env;
array *bin_env_copy;
/*
* docroot-translation between URL->phys and the
* remote host
*
* reasons:
* - different dir-layout if remote
* - chroot if local
*
*/
buffer *docroot;
/*
* fastcgi-mode:
* - responser
* - authorizer
*
*/
unsigned short mode;
/*
* check_local tell you if the phys file is stat()ed
* or not. FastCGI doesn't care if the service is
* remote. If the web-server side doesn't contain
* the fastcgi-files we should not stat() for them
* and say '404 not found'.
*/
unsigned short check_local;
/*
* append PATH_INFO to SCRIPT_FILENAME
*
* php needs this if cgi.fix_pathinfo is provied
*
*/
unsigned short break_scriptfilename_for_php;
/*
* If the backend includes X-LIGHTTPD-send-file in the response
* we use the value as filename and ignore the content.
*
*/
unsigned short allow_xsendfile;
ssize_t load; /* replace by host->load */
size_t max_id; /* corresponds most of the time to
num_procs.
only if a process is killed max_id waits for the process itself
to die and decrements its afterwards */
buffer *strip_request_uri;
} fcgi_extension_host;
/*
* one extension can have multiple hosts assigned
* one host can spawn additional processes on the same
* socket (if we control it)
*
* ext -> host -> procs
* 1:n 1:n
*
* if the fastcgi process is remote that whole goes down
* to
*
* ext -> host -> procs
* 1:n 1:1
*
* in case of PHP and FCGI_CHILDREN we have again a procs
* but we don't control it directly.
*
*/
typedef struct {
buffer *key; /* like .php */
int note_is_sent;
fcgi_extension_host **hosts;
size_t used;
size_t size;
} fcgi_extension;
typedef struct {
fcgi_extension **exts;
size_t used;
size_t size;
} fcgi_exts;
typedef struct {
fcgi_exts *exts;
array *ext_mapping;
int debug;
} plugin_config;
typedef struct {
size_t *ptr;
size_t used;
size_t size;
} buffer_uint;
typedef struct {
char **ptr;
size_t size;
size_t used;
} char_array;
/* generic plugin data, shared between all connections */
typedef struct {
PLUGIN_DATA;
buffer_uint fcgi_request_id;
buffer *fcgi_env;
buffer *path;
buffer *parse_response;
buffer *statuskey;
plugin_config **config_storage;
plugin_config conf; /* this is only used as long as no handler_ctx is setup */
} plugin_data;
/* connection specific data */
typedef enum {
FCGI_STATE_UNSET,
FCGI_STATE_INIT,
FCGI_STATE_CONNECT_DELAYED,
FCGI_STATE_PREPARE_WRITE,
FCGI_STATE_WRITE,
FCGI_STATE_READ
} fcgi_connection_state_t;
typedef struct {
fcgi_proc *proc;
fcgi_extension_host *host;
fcgi_extension *ext;
fcgi_connection_state_t state;
time_t state_timestamp;
int reconnects; /* number of reconnect attempts */
chunkqueue *rb; /* read queue */
chunkqueue *wb; /* write queue */
buffer *response_header;
size_t request_id;
int fd; /* fd to the fastcgi process */
int fde_ndx; /* index into the fd-event buffer */
pid_t pid;
int got_proc;
int send_content_body;
plugin_config conf;
connection *remote_conn; /* dumb pointer */
plugin_data *plugin_data; /* dumb pointer */
} handler_ctx;
/* ok, we need a prototype */
static handler_t fcgi_handle_fdevent(void *s, void *ctx, int revents);
int fastcgi_status_copy_procname(buffer *b, fcgi_extension_host *host, fcgi_proc *proc) {
buffer_copy_string(b, "fastcgi.backend.");
buffer_append_string_buffer(b, host->id);
if (proc) {
buffer_append_string(b, ".");
buffer_append_long(b, proc->id);
}
return 0;
}
int fastcgi_status_init(server *srv, buffer *b, fcgi_extension_host *host, fcgi_proc *proc) {
#define CLEAN(x) \
fastcgi_status_copy_procname(b, host, proc); \
buffer_append_string(b, x); \
status_counter_set(srv, CONST_BUF_LEN(b), 0);
CLEAN(".disabled");
CLEAN(".died");
CLEAN(".overloaded");
CLEAN(".connected");
CLEAN(".load");
#undef CLEAN
#define CLEAN(x) \
fastcgi_status_copy_procname(b, host, NULL); \
buffer_append_string(b, x); \
status_counter_set(srv, CONST_BUF_LEN(b), 0);
CLEAN(".load");
#undef CLEAN
return 0;
}
static handler_ctx * handler_ctx_init() {
handler_ctx * hctx;
hctx = calloc(1, sizeof(*hctx));
assert(hctx);
hctx->fde_ndx = -1;
hctx->response_header = buffer_init();
hctx->request_id = 0;
hctx->state = FCGI_STATE_INIT;
hctx->proc = NULL;
hctx->fd = -1;
hctx->reconnects = 0;
hctx->send_content_body = 1;
hctx->rb = chunkqueue_init();
hctx->wb = chunkqueue_init();
return hctx;
}
static void handler_ctx_free(handler_ctx *hctx) {
if (hctx->host) {
hctx->host->load--;
hctx->host = NULL;
}
buffer_free(hctx->response_header);
chunkqueue_free(hctx->rb);
chunkqueue_free(hctx->wb);
free(hctx);
}
fcgi_proc *fastcgi_process_init() {
fcgi_proc *f;
f = calloc(1, sizeof(*f));
f->unixsocket = buffer_init();
f->connection_name = buffer_init();
f->prev = NULL;
f->next = NULL;
return f;
}
void fastcgi_process_free(fcgi_proc *f) {
if (!f) return;
fastcgi_process_free(f->next);
buffer_free(f->unixsocket);
buffer_free(f->connection_name);
free(f);
}
fcgi_extension_host *fastcgi_host_init() {
fcgi_extension_host *f;
f = calloc(1, sizeof(*f));
f->id = buffer_init();
f->host = buffer_init();
f->unixsocket = buffer_init();
f->docroot = buffer_init();
f->bin_path = buffer_init();
f->bin_env = array_init();
f->bin_env_copy = array_init();
f->strip_request_uri = buffer_init();
return f;
}
void fastcgi_host_free(fcgi_extension_host *h) {
if (!h) return;
buffer_free(h->id);
buffer_free(h->host);
buffer_free(h->unixsocket);
buffer_free(h->docroot);
buffer_free(h->bin_path);
buffer_free(h->strip_request_uri);
array_free(h->bin_env);
array_free(h->bin_env_copy);
fastcgi_process_free(h->first);
fastcgi_process_free(h->unused_procs);
free(h);
}
fcgi_exts *fastcgi_extensions_init() {
fcgi_exts *f;
f = calloc(1, sizeof(*f));
return f;
}
void fastcgi_extensions_free(fcgi_exts *f) {
size_t i;
if (!f) return;
for (i = 0; i < f->used; i++) {
fcgi_extension *fe;
size_t j;
fe = f->exts[i];
for (j = 0; j < fe->used; j++) {
fcgi_extension_host *h;
h = fe->hosts[j];
fastcgi_host_free(h);
}
buffer_free(fe->key);
free(fe->hosts);
free(fe);
}
free(f->exts);
free(f);
}
int fastcgi_extension_insert(fcgi_exts *ext, buffer *key, fcgi_extension_host *fh) {
fcgi_extension *fe;
size_t i;
/* there is something */
for (i = 0; i < ext->used; i++) {
if (buffer_is_equal(key, ext->exts[i]->key)) {
break;
}
}
if (i == ext->used) {
/* filextension is new */
fe = calloc(1, sizeof(*fe));
assert(fe);
fe->key = buffer_init();
buffer_copy_string_buffer(fe->key, key);
/* */
if (ext->size == 0) {
ext->size = 8;
ext->exts = malloc(ext->size * sizeof(*(ext->exts)));
assert(ext->exts);
} else if (ext->used == ext->size) {
ext->size += 8;
ext->exts = realloc(ext->exts, ext->size * sizeof(*(ext->exts)));
assert(ext->exts);
}
ext->exts[ext->used++] = fe;
} else {
fe = ext->exts[i];
}
if (fe->size == 0) {
fe->size = 4;
fe->hosts = malloc(fe->size * sizeof(*(fe->hosts)));
assert(fe->hosts);
} else if (fe->size == fe->used) {
fe->size += 4;
fe->hosts = realloc(fe->hosts, fe->size * sizeof(*(fe->hosts)));
assert(fe->hosts);
}
fe->hosts[fe->used++] = fh;
return 0;
}
INIT_FUNC(mod_fastcgi_init) {
plugin_data *p;
p = calloc(1, sizeof(*p));
p->fcgi_env = buffer_init();
p->path = buffer_init();
p->parse_response = buffer_init();
p->statuskey = buffer_init();
return p;
}
FREE_FUNC(mod_fastcgi_free) {
plugin_data *p = p_d;
buffer_uint *r = &(p->fcgi_request_id);
UNUSED(srv);
if (r->ptr) free(r->ptr);
buffer_free(p->fcgi_env);
buffer_free(p->path);
buffer_free(p->parse_response);
buffer_free(p->statuskey);
if (p->config_storage) {
size_t i, j, n;
for (i = 0; i < srv->config_context->used; i++) {
plugin_config *s = p->config_storage[i];
fcgi_exts *exts;
if (!s) continue;
exts = s->exts;
for (j = 0; j < exts->used; j++) {
fcgi_extension *ex;
ex = exts->exts[j];
for (n = 0; n < ex->used; n++) {
fcgi_proc *proc;
fcgi_extension_host *host;
host = ex->hosts[n];
for (proc = host->first; proc; proc = proc->next) {
if (proc->pid != 0) kill(proc->pid, SIGTERM);
if (proc->is_local &&
!buffer_is_empty(proc->unixsocket)) {
unlink(proc->unixsocket->ptr);
}
}
for (proc = host->unused_procs; proc; proc = proc->next) {
if (proc->pid != 0) kill(proc->pid, SIGTERM);
if (proc->is_local &&
!buffer_is_empty(proc->unixsocket)) {
unlink(proc->unixsocket->ptr);
}
}
}
}
fastcgi_extensions_free(s->exts);
array_free(s->ext_mapping);
free(s);
}
free(p->config_storage);
}
free(p);
return HANDLER_GO_ON;
}
static int env_add(char_array *env, const char *key, size_t key_len, const char *val, size_t val_len) {
char *dst;
if (!key || !val) return -1;
dst = malloc(key_len + val_len + 3);
memcpy(dst, key, key_len);
dst[key_len] = '=';
/* add the \0 from the value */
memcpy(dst + key_len + 1, val, val_len + 1);
if (env->size == 0) {
env->size = 16;
env->ptr = malloc(env->size * sizeof(*env->ptr));
} else if (env->size == env->used + 1) {
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}
env->ptr[env->used++] = dst;
return 0;
}
static int parse_binpath(char_array *env, buffer *b) {
char *start;
size_t i;
/* search for spaces */
start = b->ptr;
for (i = 0; i < b->used - 1; i++) {
switch(b->ptr[i]) {
case ' ':
case '\t':
/* a WS, stop here and copy the argument */
if (env->size == 0) {
env->size = 16;
env->ptr = malloc(env->size * sizeof(*env->ptr));
} else if (env->size == env->used) {
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}
b->ptr[i] = '\0';
env->ptr[env->used++] = start;
start = b->ptr + i + 1;
break;
default:
break;
}
}
if (env->size == 0) {
env->size = 16;
env->ptr = malloc(env->size * sizeof(*env->ptr));
} else if (env->size == env->used) { /* we need one extra for the terminating NULL */
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}
/* the rest */
env->ptr[env->used++] = start;
if (env->size == 0) {
env->size = 16;
env->ptr = malloc(env->size * sizeof(*env->ptr));
} else if (env->size == env->used) { /* we need one extra for the terminating NULL */
env->size += 16;
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
}
/* terminate */
env->ptr[env->used++] = NULL;
return 0;
}
static int fcgi_spawn_connection(server *srv,
plugin_data *p,
fcgi_extension_host *host,
fcgi_proc *proc) {
int fcgi_fd;
int socket_type, status;
struct timeval tv = { 0, 100 * 1000 };
#ifdef HAVE_SYS_UN_H
struct sockaddr_un fcgi_addr_un;
#endif
struct sockaddr_in fcgi_addr_in;
struct sockaddr *fcgi_addr;
socklen_t servlen;
#ifndef HAVE_FORK
return -1;
#endif
if (p->conf.debug) {
log_error_write(srv, __FILE__, __LINE__, "sdb",
"new proc, socket:", proc->port, proc->unixsocket);
}
if (!buffer_is_empty(proc->unixsocket)) {
memset(&fcgi_addr, 0, sizeof(fcgi_addr));
#ifdef HAVE_SYS_UN_H
fcgi_addr_un.sun_family = AF_UNIX;
strcpy(fcgi_addr_un.sun_path, proc->unixsocket->ptr);
#ifdef SUN_LEN
servlen = SUN_LEN(&fcgi_addr_un);
#else
/* stevens says: */
servlen = proc->unixsocket->used + sizeof(fcgi_addr_un.sun_family);
#endif
socket_type = AF_UNIX;
fcgi_addr = (struct sockaddr *) &fcgi_addr_un;
buffer_copy_string(proc->connection_name, "unix:");
buffer_append_string_buffer(proc->connection_name, proc->unixsocket);
#else
log_error_write(srv, __FILE__, __LINE__, "s",
"ERROR: Unix Domain sockets are not supported.");
return -1;
#endif
} else {
fcgi_addr_in.sin_family = AF_INET;
if (buffer_is_empty(host->host)) {
fcgi_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
} else {
struct hostent *he;
/* set a usefull default */
fcgi_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
if (NULL == (he = gethostbyname(host->host->ptr))) {
log_error_write(srv, __FILE__, __LINE__,
"sdb", "gethostbyname failed: ",
h_errno, host->host);
return -1;
}
if (he->h_addrtype != AF_INET) {
log_error_write(srv, __FILE__, __LINE__, "sd", "addr-type != AF_INET: ", he->h_addrtype);
return -1;
}
if (he->h_length != sizeof(struct in_addr)) {
log_error_write(srv, __FILE__, __LINE__, "sd", "addr-length != sizeof(in_addr): ", he->h_length);
return -1;
}
memcpy(&(fcgi_addr_in.sin_addr.s_addr), he->h_addr_list[0], he->h_length);
}
fcgi_addr_in.sin_port = htons(proc->port);
servlen = sizeof(fcgi_addr_in);
socket_type = AF_INET;
fcgi_addr = (struct sockaddr *) &fcgi_addr_in;
buffer_copy_string(proc->connection_name, "tcp:");
buffer_append_string_buffer(proc->connection_name, host->host);
buffer_append_string(proc->connection_name, ":");
buffer_append_long(proc->connection_name, proc->port);
}
if (-1 == (fcgi_fd = socket(socket_type, SOCK_STREAM, 0))) {
log_error_write(srv, __FILE__, __LINE__, "ss",
"failed:", strerror(errno));
return -1;
}
if (-1 == connect(fcgi_fd, fcgi_addr, servlen)) {
/* server is not up, spawn in */
pid_t child;
int val;
if (errno != ENOENT &&
!buffer_is_empty(proc->unixsocket)) {
unlink(proc->unixsocket->ptr);
}
close(fcgi_fd);
/* reopen socket */
if (-1 == (fcgi_fd = socket(socket_type, SOCK_STREAM, 0))) {
log_error_write(srv, __FILE__, __LINE__, "ss",
"socket failed:", strerror(errno));
return -1;
}
val = 1;
if (setsockopt(fcgi_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
log_error_write(srv, __FILE__, __LINE__, "ss",
"socketsockopt failed:", strerror(errno));
return -1;
}
/* create socket */
if (-1 == bind(fcgi_fd, fcgi_addr, servlen)) {
log_error_write(srv, __FILE__, __LINE__, "sbs",
"bind failed for:",
proc->connection_name,
strerror(errno));
return -1;
}
if (-1 == listen(fcgi_fd, 1024)) {
log_error_write(srv, __FILE__, __LINE__, "ss",
"listen failed:", strerror(errno));
return -1;
}
#ifdef HAVE_FORK
switch ((child = fork())) {
case 0: {
size_t i = 0;
char *c;
char_array env;
char_array arg;
/* create environment */
env.ptr = NULL;
env.size = 0;
env.used = 0;
arg.ptr = NULL;
arg.size = 0;
arg.used = 0;
if(fcgi_fd != FCGI_LISTENSOCK_FILENO) {
close(FCGI_LISTENSOCK_FILENO);
dup2(fcgi_fd, FCGI_LISTENSOCK_FILENO);
close(fcgi_fd);
}
/* we don't need the client socket */
for (i = 3; i < 256; i++) {
close(i);
}
/* build clean environment */
if (host->bin_env_copy->used) {
for (i = 0; i < host->bin_env_copy->used; i++) {
data_string *ds = (data_string *)host->bin_env_copy->data[i];
char *ge;
if (NULL != (ge = getenv(ds->value->ptr))) {
env_add(&env, CONST_BUF_LEN(ds->value), ge, strlen(ge));
}
}
} else {
for (i = 0; environ[i]; i++) {
char *eq;
if (NULL != (eq = strchr(environ[i], '='))) {
env_add(&env, environ[i], eq - environ[i], eq+1, strlen(eq+1));
}
}
}
/* create environment */
for (i = 0; i < host->bin_env->used; i++) {
data_string *ds = (data_string *)host->bin_env->data[i];
env_add(&env, CONST_BUF_LEN(ds->key), CONST_BUF_LEN(ds->value));
}
for (i = 0; i < env.used; i++) {
/* search for PHP_FCGI_CHILDREN */
if (0 == strncmp(env.ptr[i], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
}
/* not found, add a default */
if (i == env.used) {
env_add(&env, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
}
env.ptr[env.used] = NULL;
parse_binpath(&arg, host->bin_path);
/* chdir into the base of the bin-path,
* search for the last / */
if (NULL != (c = strrchr(arg.ptr[0], '/'))) {
*c = '\0';
/* change to the physical directory */
if (-1 == chdir(arg.ptr[0])) {
*c = '/';
log_error_write(srv, __FILE__, __LINE__, "sss", "chdir failed:", strerror(errno), arg.ptr[0]);
}
*c = '/';
}
/* exec the cgi */
execve(arg.ptr[0], arg.ptr, env.ptr);
log_error_write(srv, __FILE__, __LINE__, "sbs",
"execve failed for:", host->bin_path, strerror(errno));
exit(errno);