2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
#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"
|
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#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>
|
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "sys-socket.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UIO_H
|
2009-10-11 14:31:42 +00:00
|
|
|
# include <sys/uio.h>
|
2005-06-12 09:29:00 +00:00
|
|
|
#endif
|
2009-10-11 14:31:42 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2009-10-11 14:31:42 +00:00
|
|
|
# include <sys/wait.h>
|
2005-06-12 09:29:00 +00:00
|
|
|
#endif
|
|
|
|
|
2009-04-10 17:35:19 +00:00
|
|
|
#include "version.h"
|
|
|
|
|
2005-06-12 10:31:19 +00:00
|
|
|
enum {EOL_UNSET, EOL_N, EOL_RN};
|
2005-06-12 09:29:00 +00:00
|
|
|
|
|
|
|
/*
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-06-12 09:29:00 +00:00
|
|
|
* TODO:
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-06-12 10:31:19 +00:00
|
|
|
* - add timeout for a connect to a non-scgi process
|
2005-06-12 09:29:00 +00:00
|
|
|
* (use state_timestamp + state)
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-06-12 09:29:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct scgi_proc {
|
|
|
|
size_t id; /* id will be between 1 and max_procs */
|
|
|
|
buffer *socket; /* config.socket + "-" + id */
|
|
|
|
unsigned port; /* config.port + pno */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
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 scgi_proc *prev, *next; /* see first */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
time_t disable_ts; /* replace by host->something */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
int is_local;
|
|
|
|
|
|
|
|
enum { PROC_STATE_UNSET, /* init-phase */
|
|
|
|
PROC_STATE_RUNNING, /* alive */
|
|
|
|
PROC_STATE_DIED_WAIT_FOR_PID,
|
|
|
|
PROC_STATE_KILLED, /* was killed as we don't have the load anymore */
|
|
|
|
PROC_STATE_DIED, /* marked as dead, should be restarted */
|
|
|
|
PROC_STATE_DISABLED /* proc disabled as it resulted in an error */
|
2006-10-04 13:26:23 +00:00
|
|
|
} state;
|
2005-06-12 09:29:00 +00:00
|
|
|
} scgi_proc;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
/* list of processes handling this extension
|
|
|
|
* sorted by lowest load
|
|
|
|
*
|
|
|
|
* whenever a job is done move it up in the list
|
2006-10-04 13:26:23 +00:00
|
|
|
* until it is sorted, move it down as soon as the
|
2005-06-12 09:29:00 +00:00
|
|
|
* job is started
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
scgi_proc *first;
|
|
|
|
scgi_proc *unused_procs;
|
2005-06-12 09:29:00 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
/*
|
2005-06-12 09:29:00 +00:00
|
|
|
* spawn at least min_procs, at max_procs.
|
|
|
|
*
|
2006-10-04 13:26:23 +00:00
|
|
|
* as soon as the load of the first entry
|
2005-06-12 09:29:00 +00:00
|
|
|
* is max_load_per_proc we spawn a new one
|
2006-10-04 13:26:23 +00:00
|
|
|
* and add it to the first entry and give it
|
2005-06-12 09:29:00 +00:00
|
|
|
* the load
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-06-12 09:29:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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
|
2006-10-04 13:26:23 +00:00
|
|
|
* used for idle_timeout until min_procs is
|
2005-06-12 09:29:00 +00:00
|
|
|
* reached. this helps to get the processlist
|
|
|
|
* small again we had a small peak load.
|
|
|
|
*
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
unsigned short idle_timeout;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
/*
|
|
|
|
* time after a disabled remote connection is tried to be re-enabled
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
*
|
2005-06-12 09:29:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
unsigned short disable_time;
|
|
|
|
|
|
|
|
/*
|
2005-06-12 10:31:19 +00:00
|
|
|
* same scgi processes get a little bit larger
|
2006-10-04 13:26:23 +00:00
|
|
|
* than wanted. max_requests_per_proc kills a
|
2005-06-12 09:29:00 +00:00
|
|
|
* process after a number of handled requests.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
size_t max_requests_per_proc;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
|
|
|
|
/* config */
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
/*
|
|
|
|
* host:port
|
2005-06-12 09:29:00 +00:00
|
|
|
*
|
2006-10-04 13:26:23 +00:00
|
|
|
* if host is one of the local IP adresses the
|
2005-06-12 09:29:00 +00:00
|
|
|
* whole connection is local
|
|
|
|
*
|
|
|
|
* if tcp/ip should be used host AND port have
|
2006-10-04 13:26:23 +00:00
|
|
|
* to be specified
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
buffer *host;
|
2005-06-12 09:29:00 +00:00
|
|
|
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;
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
/* if socket is local we can start the scgi
|
2005-06-12 09:29:00 +00:00
|
|
|
* process ourself
|
|
|
|
*
|
|
|
|
* bin-path is the path to the binary
|
|
|
|
*
|
|
|
|
* check min_procs and max_procs for the number
|
|
|
|
* of process to start-up
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
buffer *bin_path;
|
|
|
|
|
|
|
|
/* bin-path is set bin-environment is taken to
|
2005-06-12 09:29:00 +00:00
|
|
|
* create the environement before starting the
|
|
|
|
* FastCGI process
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-06-12 09:29:00 +00:00
|
|
|
*/
|
|
|
|
array *bin_env;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
array *bin_env_copy;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
/*
|
2006-10-04 13:26:23 +00:00
|
|
|
* docroot-translation between URL->phys and the
|
2005-06-12 09:29:00 +00:00
|
|
|
* remote host
|
|
|
|
*
|
|
|
|
* reasons:
|
|
|
|
* - different dir-layout if remote
|
|
|
|
* - chroot if local
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
buffer *docroot;
|
|
|
|
|
|
|
|
/*
|
2006-10-04 13:26:23 +00:00
|
|
|
* check_local tell you if the phys file is stat()ed
|
2005-06-12 09:29:00 +00:00
|
|
|
* or not. FastCGI doesn't care if the service is
|
|
|
|
* remote. If the web-server side doesn't contain
|
2005-06-12 10:31:19 +00:00
|
|
|
* the scgi-files we should not stat() for them
|
2005-06-12 09:29:00 +00:00
|
|
|
* and say '404 not found'.
|
|
|
|
*/
|
|
|
|
unsigned short check_local;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* append PATH_INFO to SCRIPT_FILENAME
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-06-12 09:29:00 +00:00
|
|
|
* php needs this if cgi.fix_pathinfo is provied
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-06-12 09:29:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-05-22 10:04:01 +00:00
|
|
|
/*
|
|
|
|
* workaround for program when prefix="/"
|
|
|
|
*
|
|
|
|
* rule to build PATH_INFO is hardcoded for when check_local is disabled
|
|
|
|
* enable this option to use the workaround
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned short fix_root_path_name;
|
2016-04-22 03:05:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the backend includes X-Sendfile in the response
|
|
|
|
* we use the value as filename and ignore the content.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
unsigned short xsendfile_allow;
|
|
|
|
array *xsendfile_docroot;
|
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
ssize_t load; /* replace by host->load */
|
|
|
|
|
|
|
|
size_t max_id; /* corresponds most of the time to
|
|
|
|
num_procs.
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
only if a process is killed max_id waits for the process itself
|
|
|
|
to die and decrements its afterwards */
|
2016-03-30 20:17:17 +00:00
|
|
|
|
|
|
|
int listen_backlog;
|
2005-06-12 09:29:00 +00:00
|
|
|
} scgi_extension_host;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* one extension can have multiple hosts assigned
|
2006-10-04 13:26:23 +00:00
|
|
|
* one host can spawn additional processes on the same
|
2005-06-12 09:29:00 +00:00
|
|
|
* socket (if we control it)
|
|
|
|
*
|
|
|
|
* ext -> host -> procs
|
|
|
|
* 1:n 1:n
|
|
|
|
*
|
2006-10-04 13:26:23 +00:00
|
|
|
* if the scgi process is remote that whole goes down
|
2005-06-12 09:29:00 +00:00
|
|
|
* to
|
|
|
|
*
|
|
|
|
* ext -> host -> procs
|
2006-10-04 13:26:23 +00:00
|
|
|
* 1:n 1:1
|
2005-06-12 09:29:00 +00:00
|
|
|
*
|
|
|
|
* 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 */
|
|
|
|
|
2008-05-22 03:41:17 +00:00
|
|
|
int note_is_sent;
|
2005-06-12 09:29:00 +00:00
|
|
|
scgi_extension_host **hosts;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
size_t used;
|
|
|
|
size_t size;
|
|
|
|
} scgi_extension;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
scgi_extension **exts;
|
|
|
|
|
|
|
|
size_t used;
|
|
|
|
size_t size;
|
|
|
|
} scgi_exts;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2006-10-04 13:26:23 +00:00
|
|
|
scgi_exts *exts;
|
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
int debug;
|
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char **ptr;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
size_t size;
|
|
|
|
size_t used;
|
|
|
|
} char_array;
|
|
|
|
|
|
|
|
/* generic plugin data, shared between all connections */
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
buffer *scgi_env;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
buffer *path;
|
|
|
|
buffer *parse_response;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
plugin_config **config_storage;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
plugin_config conf; /* this is only used as long as no handler_ctx is setup */
|
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
/* connection specific data */
|
2006-10-04 13:26:23 +00:00
|
|
|
typedef enum { FCGI_STATE_INIT, FCGI_STATE_CONNECT, FCGI_STATE_PREPARE_WRITE,
|
|
|
|
FCGI_STATE_WRITE, FCGI_STATE_READ
|
2005-06-12 09:29:00 +00:00
|
|
|
} scgi_connection_state_t;
|
|
|
|
|
|
|
|
typedef struct {
|
2006-10-04 13:26:23 +00:00
|
|
|
buffer *response;
|
2005-06-12 09:29:00 +00:00
|
|
|
size_t response_len;
|
|
|
|
int response_type;
|
|
|
|
int response_padding;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
scgi_proc *proc;
|
|
|
|
scgi_extension_host *host;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
scgi_connection_state_t state;
|
|
|
|
time_t state_timestamp;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
int reconnects; /* number of reconnect attempts */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-09-26 08:56:39 +00:00
|
|
|
chunkqueue *wb;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
buffer *response_header;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
int delayed; /* flag to mark that the connect() is delayed */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
size_t request_id;
|
2005-06-12 10:31:19 +00:00
|
|
|
int fd; /* fd to the scgi process */
|
2005-06-12 09:29:00 +00:00
|
|
|
int fde_ndx; /* index into the fd-event buffer */
|
|
|
|
|
|
|
|
pid_t pid;
|
|
|
|
int got_proc;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
plugin_config conf;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
connection *remote_conn; /* dumb pointer */
|
|
|
|
plugin_data *plugin_data; /* dumb pointer */
|
|
|
|
} handler_ctx;
|
|
|
|
|
|
|
|
|
|
|
|
/* ok, we need a prototype */
|
2010-08-06 21:57:15 +00:00
|
|
|
static handler_t scgi_handle_fdevent(server *srv, void *ctx, int revents);
|
2005-06-12 09:29:00 +00:00
|
|
|
|
|
|
|
int scgi_proclist_sort_down(server *srv, scgi_extension_host *host, scgi_proc *proc);
|
|
|
|
|
2009-07-13 13:41:44 +00:00
|
|
|
static void reset_signals(void) {
|
|
|
|
#ifdef SIGTTOU
|
|
|
|
signal(SIGTTOU, SIG_DFL);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
|
|
|
signal(SIGTTIN, SIG_DFL);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
|
|
|
signal(SIGTSTP, SIG_DFL);
|
|
|
|
#endif
|
|
|
|
signal(SIGHUP, SIG_DFL);
|
|
|
|
signal(SIGPIPE, SIG_DFL);
|
|
|
|
signal(SIGUSR1, SIG_DFL);
|
|
|
|
}
|
2005-06-12 09:29:00 +00:00
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static handler_ctx * handler_ctx_init(void) {
|
2005-06-12 09:29:00 +00:00
|
|
|
handler_ctx * hctx;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
hctx = calloc(1, sizeof(*hctx));
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(hctx);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
hctx->fde_ndx = -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
hctx->response = buffer_init();
|
|
|
|
hctx->response_header = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
hctx->request_id = 0;
|
|
|
|
hctx->state = FCGI_STATE_INIT;
|
|
|
|
hctx->proc = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
hctx->response_len = 0;
|
|
|
|
hctx->response_type = 0;
|
|
|
|
hctx->response_padding = 0;
|
|
|
|
hctx->fd = -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
hctx->reconnects = 0;
|
2005-09-26 08:56:39 +00:00
|
|
|
|
|
|
|
hctx->wb = chunkqueue_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
return hctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handler_ctx_free(handler_ctx *hctx) {
|
|
|
|
buffer_free(hctx->response);
|
|
|
|
buffer_free(hctx->response_header);
|
2005-09-26 08:56:39 +00:00
|
|
|
|
|
|
|
chunkqueue_free(hctx->wb);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
free(hctx);
|
|
|
|
}
|
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static scgi_proc *scgi_process_init(void) {
|
2005-06-12 09:29:00 +00:00
|
|
|
scgi_proc *f;
|
|
|
|
|
|
|
|
f = calloc(1, sizeof(*f));
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(f);
|
2005-06-12 09:29:00 +00:00
|
|
|
f->socket = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
f->prev = NULL;
|
|
|
|
f->next = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static void scgi_process_free(scgi_proc *f) {
|
2005-06-12 09:29:00 +00:00
|
|
|
if (!f) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 10:31:19 +00:00
|
|
|
scgi_process_free(f->next);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
buffer_free(f->socket);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
free(f);
|
|
|
|
}
|
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static scgi_extension_host *scgi_host_init(void) {
|
2005-06-12 09:29:00 +00:00
|
|
|
scgi_extension_host *f;
|
|
|
|
|
|
|
|
f = calloc(1, sizeof(*f));
|
|
|
|
|
|
|
|
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();
|
2016-04-22 03:05:48 +00:00
|
|
|
f->xsendfile_docroot = array_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static void scgi_host_free(scgi_extension_host *h) {
|
2005-06-12 09:29:00 +00:00
|
|
|
if (!h) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
buffer_free(h->host);
|
|
|
|
buffer_free(h->unixsocket);
|
|
|
|
buffer_free(h->docroot);
|
|
|
|
buffer_free(h->bin_path);
|
|
|
|
array_free(h->bin_env);
|
|
|
|
array_free(h->bin_env_copy);
|
2016-04-22 03:05:48 +00:00
|
|
|
array_free(h->xsendfile_docroot);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 10:31:19 +00:00
|
|
|
scgi_process_free(h->first);
|
|
|
|
scgi_process_free(h->unused_procs);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
free(h);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static scgi_exts *scgi_extensions_init(void) {
|
2005-06-12 09:29:00 +00:00
|
|
|
scgi_exts *f;
|
|
|
|
|
|
|
|
f = calloc(1, sizeof(*f));
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(f);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static void scgi_extensions_free(scgi_exts *f) {
|
2005-06-12 09:29:00 +00:00
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
if (!f) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
for (i = 0; i < f->used; i++) {
|
|
|
|
scgi_extension *fe;
|
|
|
|
size_t j;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
fe = f->exts[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
for (j = 0; j < fe->used; j++) {
|
|
|
|
scgi_extension_host *h;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
h = fe->hosts[j];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 10:31:19 +00:00
|
|
|
scgi_host_free(h);
|
2005-06-12 09:29:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
buffer_free(fe->key);
|
|
|
|
free(fe->hosts);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
free(fe);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
free(f->exts);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
free(f);
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static int scgi_extension_insert(scgi_exts *ext, buffer *key, scgi_extension_host *fh) {
|
2005-06-12 09:29:00 +00:00
|
|
|
scgi_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));
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(fe);
|
2005-06-12 09:29:00 +00:00
|
|
|
fe->key = buffer_init();
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_buffer(fe->key, key);
|
2005-06-12 09:29:00 +00:00
|
|
|
|
|
|
|
/* */
|
|
|
|
|
|
|
|
if (ext->size == 0) {
|
|
|
|
ext->size = 8;
|
|
|
|
ext->exts = malloc(ext->size * sizeof(*(ext->exts)));
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(ext->exts);
|
2005-06-12 09:29:00 +00:00
|
|
|
} else if (ext->used == ext->size) {
|
|
|
|
ext->size += 8;
|
|
|
|
ext->exts = realloc(ext->exts, ext->size * sizeof(*(ext->exts)));
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(ext->exts);
|
2005-06-12 09:29:00 +00:00
|
|
|
}
|
|
|
|
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)));
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(fe->hosts);
|
2005-06-12 09:29:00 +00:00
|
|
|
} else if (fe->size == fe->used) {
|
|
|
|
fe->size += 4;
|
|
|
|
fe->hosts = realloc(fe->hosts, fe->size * sizeof(*(fe->hosts)));
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(fe->hosts);
|
2005-06-12 09:29:00 +00:00
|
|
|
}
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
fe->hosts[fe->used++] = fh;
|
2005-06-12 09:29:00 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INIT_FUNC(mod_scgi_init) {
|
|
|
|
plugin_data *p;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
p = calloc(1, sizeof(*p));
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
p->scgi_env = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
p->path = buffer_init();
|
|
|
|
p->parse_response = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FREE_FUNC(mod_scgi_free) {
|
|
|
|
plugin_data *p = p_d;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
UNUSED(srv);
|
|
|
|
|
|
|
|
buffer_free(p->scgi_env);
|
|
|
|
buffer_free(p->path);
|
|
|
|
buffer_free(p->parse_response);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
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];
|
|
|
|
scgi_exts *exts;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-05-14 09:38:33 +00:00
|
|
|
if (NULL == s) continue;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
exts = s->exts;
|
|
|
|
|
|
|
|
for (j = 0; j < exts->used; j++) {
|
|
|
|
scgi_extension *ex;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
ex = exts->exts[j];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
for (n = 0; n < ex->used; n++) {
|
|
|
|
scgi_proc *proc;
|
|
|
|
scgi_extension_host *host;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
host = ex->hosts[n];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
for (proc = host->first; proc; proc = proc->next) {
|
|
|
|
if (proc->pid != 0) kill(proc->pid, SIGTERM);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
if (proc->is_local &&
|
2015-02-08 12:37:10 +00:00
|
|
|
!buffer_string_is_empty(proc->socket)) {
|
2005-06-12 09:29:00 +00:00
|
|
|
unlink(proc->socket->ptr);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
for (proc = host->unused_procs; proc; proc = proc->next) {
|
|
|
|
if (proc->pid != 0) kill(proc->pid, SIGTERM);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
if (proc->is_local &&
|
2015-02-08 12:37:10 +00:00
|
|
|
!buffer_string_is_empty(proc->socket)) {
|
2005-06-12 09:29:00 +00:00
|
|
|
unlink(proc->socket->ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 10:31:19 +00:00
|
|
|
scgi_extensions_free(s->exts);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
free(p->config_storage);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
free(p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
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;
|
2008-07-31 20:45:36 +00:00
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
if (!key || !val) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
dst = malloc(key_len + val_len + 3);
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(dst);
|
2005-06-12 09:29:00 +00:00
|
|
|
memcpy(dst, key, key_len);
|
|
|
|
dst[key_len] = '=';
|
|
|
|
/* add the \0 from the value */
|
|
|
|
memcpy(dst + key_len + 1, val, val_len + 1);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2008-07-31 20:45:36 +00:00
|
|
|
for (i = 0; i < env->used; i++) {
|
|
|
|
if (0 == strncmp(dst, env->ptr[i], key_len + 1)) {
|
|
|
|
/* don't care about free as we are in a forked child which is going to exec(...) */
|
|
|
|
/* free(env->ptr[i]); */
|
|
|
|
env->ptr[i] = dst;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
if (env->size == 0) {
|
|
|
|
env->size = 16;
|
|
|
|
env->ptr = malloc(env->size * sizeof(*env->ptr));
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(env->ptr);
|
2005-06-12 09:29:00 +00:00
|
|
|
} else if (env->size == env->used) {
|
|
|
|
env->size += 16;
|
|
|
|
env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
|
2015-10-16 19:44:06 +00:00
|
|
|
force_assert(env->ptr);
|
2005-06-12 09:29:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
env->ptr[env->used++] = dst;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-14 18:07:01 +00:00
|
|
|
#if !defined(HAVE_FORK)
|
2006-10-04 13:26:23 +00:00
|
|
|
static int scgi_spawn_connection(server *srv,
|
2016-03-14 18:07:01 +00:00
|
|
|
plugin_data *p,
|
|
|
|
scgi_extension_host *host,
|
|
|
|
scgi_proc *proc) {
|
|
|
|
UNUSED(srv);
|
|
|
|
UNUSED(p);
|
|
|
|
UNUSED(host);
|
|
|
|
UNUSED(proc);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* -> defined(HAVE_FORK) */
|
|
|
|
|
|
|
|
static int scgi_spawn_connection(server *srv,
|
|
|
|
plugin_data *p,
|
|
|
|
scgi_extension_host *host,
|
|
|
|
scgi_proc *proc) {
|
2005-06-12 09:29:00 +00:00
|
|
|
int scgi_fd;
|
|
|
|
int socket_type, status;
|
|
|
|
struct timeval tv = { 0, 100 * 1000 };
|
|
|
|
#ifdef HAVE_SYS_UN_H
|
|
|
|
struct sockaddr_un scgi_addr_un;
|
|
|
|
#endif
|
|
|
|
struct sockaddr_in scgi_addr_in;
|
|
|
|
struct sockaddr *scgi_addr;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
socklen_t servlen;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-06-12 09:29:00 +00:00
|
|
|
if (p->conf.debug) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sdb",
|
|
|
|
"new proc, socket:", proc->port, proc->socket);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-04-01 01:59:32 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (!buffer_string_is_empty(proc->socket)) {
|
2005-06-12 09:29:00 +00:00
|
|
|
#ifdef HAVE_SYS_UN_H
|
2015-12-04 20:22:42 +00:00
|
|
|
memset(&scgi_addr_un, 0, sizeof(scgi_addr_un));
|
2005-06-12 09:29:00 +00:00
|
|
|
scgi_addr_un.sun_family = AF_UNIX;
|
2015-02-08 19:10:44 +00:00
|
|
|
if (buffer_string_length(proc->socket) + 1 > sizeof(scgi_addr_un.sun_path)) {
|
2014-02-16 13:08:43 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sB",
|
|
|
|
"ERROR: Unix Domain socket filename too long:",
|
|
|
|
proc->socket);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-02-08 19:10:44 +00:00
|
|
|
memcpy(scgi_addr_un.sun_path, proc->socket->ptr, buffer_string_length(proc->socket) + 1);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|