2005-02-20 14:27: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"
|
2005-09-14 13:55:06 +00:00
|
|
|
#include "stat_cache.h"
|
2006-09-14 12:00:32 +00:00
|
|
|
#include "status_counter.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
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>
|
|
|
|
|
2006-10-03 22:22:47 +00:00
|
|
|
#ifdef HAVE_FASTCGI_FASTCGI_H
|
2009-10-11 14:31:42 +00:00
|
|
|
# include <fastcgi/fastcgi.h>
|
2006-10-03 22:22:47 +00:00
|
|
|
#else
|
2009-10-11 14:31:42 +00:00
|
|
|
# ifdef HAVE_FASTCGI_H
|
|
|
|
# include <fastcgi.h>
|
|
|
|
# else
|
|
|
|
# include "fastcgi.h"
|
|
|
|
# endif
|
2006-10-03 22:22:47 +00:00
|
|
|
#endif /* HAVE_FASTCGI_FASTCGI_H */
|
2009-10-11 14:31:42 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
# include <sys/filio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "sys-socket.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_UIO_H
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
|
2009-04-10 17:35:19 +00:00
|
|
|
#include "version.h"
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/*
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* TODO:
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* - add timeout for a connect to a non-fastcgi process
|
|
|
|
* (use state_timestamp + state)
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct fcgi_proc {
|
|
|
|
size_t id; /* id will be between 1 and max_procs */
|
2006-03-08 23:58:18 +00:00
|
|
|
buffer *unixsocket; /* config.socket + "-" + id */
|
2005-02-20 14:27:00 +00:00
|
|
|
unsigned port; /* config.port + pno */
|
2006-03-08 23:58:18 +00:00
|
|
|
|
2007-05-03 22:08:59 +00:00
|
|
|
buffer *connection_name; /* either tcp:<host>:<port> or unix:<socket> for debugging purposes */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27: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 */
|
|
|
|
|
|
|
|
size_t requests; /* see max_requests */
|
|
|
|
struct fcgi_proc *prev, *next; /* see first */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2007-05-03 22:08:59 +00:00
|
|
|
time_t disabled_until; /* this proc is disabled until, use something else until then */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
int is_local;
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
enum {
|
2006-03-08 23:58:18 +00:00
|
|
|
PROC_STATE_UNSET, /* init-phase */
|
|
|
|
PROC_STATE_RUNNING, /* alive */
|
2006-10-04 13:26:23 +00:00
|
|
|
PROC_STATE_OVERLOADED, /* listen-queue is full,
|
2007-05-03 22:08:59 +00:00
|
|
|
don't send anything to this proc for the next 2 seconds */
|
2006-03-08 23:58:18 +00:00
|
|
|
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 */
|
2006-10-04 13:26:23 +00:00
|
|
|
} state;
|
2005-02-20 14:27:00 +00:00
|
|
|
} fcgi_proc;
|
|
|
|
|
|
|
|
typedef struct {
|
2006-01-04 01:03:32 +00:00
|
|
|
/* the key that is used to reference this value */
|
|
|
|
buffer *id;
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* 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-02-20 14:27:00 +00:00
|
|
|
* job is started
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
fcgi_proc *first;
|
|
|
|
fcgi_proc *unused_procs;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
/*
|
2005-02-20 14:27: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-02-20 14:27: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-02-20 14:27:00 +00:00
|
|
|
* the load
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned short max_procs;
|
|
|
|
size_t num_procs; /* how many procs are started */
|
2009-10-16 09:02:41 +00:00
|
|
|
size_t active_procs; /* how many of them are really running, i.e. state = PROC_STATE_RUNNING */
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* time after a disabled remote connection is tried to be re-enabled
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
unsigned short disable_time;
|
|
|
|
|
|
|
|
/*
|
2007-05-03 22:08:59 +00:00
|
|
|
* some fastcgi processes get a little bit larger
|
2006-10-04 13:26:23 +00:00
|
|
|
* than wanted. max_requests_per_proc kills a
|
2005-02-20 14:27: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-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
/* config */
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
/*
|
|
|
|
* host:port
|
2005-02-20 14:27:00 +00:00
|
|
|
*
|
2006-10-04 13:26:23 +00:00
|
|
|
* if host is one of the local IP adresses the
|
2005-02-20 14:27:00 +00:00
|
|
|
* whole connection is local
|
|
|
|
*
|
2008-01-16 01:00:32 +00:00
|
|
|
* if port is not 0, and host is not specified,
|
|
|
|
* "localhost" (INADDR_LOOPBACK) is assumed.
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
buffer *host;
|
2005-02-20 14:27: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 fastcgi
|
2005-02-20 14:27:00 +00:00
|
|
|
* process ourself
|
|
|
|
*
|
|
|
|
* bin-path is the path to the binary
|
|
|
|
*
|
|
|
|
* check min_procs and max_procs for the number
|
2007-05-03 22:08:59 +00:00
|
|
|
* of process to start up
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
buffer *bin_path;
|
|
|
|
|
|
|
|
/* bin-path is set bin-environment is taken to
|
2005-02-20 14:27:00 +00:00
|
|
|
* create the environement before starting the
|
|
|
|
* FastCGI process
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
|
|
|
array *bin_env;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
array *bin_env_copy;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/*
|
2006-10-04 13:26:23 +00:00
|
|
|
* docroot-translation between URL->phys and the
|
2005-02-20 14:27:00 +00:00
|
|
|
* remote host
|
|
|
|
*
|
|
|
|
* reasons:
|
|
|
|
* - different dir-layout if remote
|
|
|
|
* - chroot if local
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
buffer *docroot;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* fastcgi-mode:
|
|
|
|
* - responser
|
|
|
|
* - authorizer
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
unsigned short mode;
|
|
|
|
|
|
|
|
/*
|
2007-05-03 22:08:59 +00:00
|
|
|
* check_local tells you if the phys file is stat()ed
|
2005-02-20 14:27:00 +00:00
|
|
|
* 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;
|
|
|
|
|
2005-03-02 18:36:02 +00:00
|
|
|
/*
|
|
|
|
* append PATH_INFO to SCRIPT_FILENAME
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2007-05-03 22:08:59 +00:00
|
|
|
* php needs this if cgi.fix_pathinfo is provided
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-03-02 18:36:02 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-03-02 18:36:02 +00:00
|
|
|
unsigned short break_scriptfilename_for_php;
|
2005-09-14 13:55:06 +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;
|
|
|
|
|
2005-09-14 13:55:06 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27: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-02-20 14:27:00 +00:00
|
|
|
only if a process is killed max_id waits for the process itself
|
2007-05-03 22:08:59 +00:00
|
|
|
to die and decrements it afterwards */
|
2005-11-10 18:22:07 +00:00
|
|
|
|
|
|
|
buffer *strip_request_uri;
|
2007-04-10 17:08:11 +00:00
|
|
|
|
|
|
|
unsigned short kill_signal; /* we need a setting for this as libfcgi
|
|
|
|
applications prefer SIGUSR1 while the
|
|
|
|
rest of the world would use SIGTERM
|
|
|
|
*sigh* */
|
2005-02-20 14:27:00 +00:00
|
|
|
} fcgi_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-02-20 14:27: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 fastcgi process is remote that whole goes down
|
2005-02-20 14:27:00 +00:00
|
|
|
* to
|
|
|
|
*
|
|
|
|
* ext -> host -> procs
|
2006-10-04 13:26:23 +00:00
|
|
|
* 1:n 1:1
|
2005-02-20 14:27: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 */
|
|
|
|
|
2006-03-08 23:58:18 +00:00
|
|
|
int note_is_sent;
|
2007-01-03 09:46:32 +00:00
|
|
|
int last_used_ndx;
|
2006-03-08 23:58:18 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fcgi_extension_host **hosts;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t used;
|
|
|
|
size_t size;
|
|
|
|
} fcgi_extension;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
fcgi_extension **exts;
|
|
|
|
|
|
|
|
size_t used;
|
|
|
|
size_t size;
|
|
|
|
} fcgi_exts;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2006-10-04 13:26:23 +00:00
|
|
|
fcgi_exts *exts;
|
2006-01-31 12:05:03 +00:00
|
|
|
|
|
|
|
array *ext_mapping;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2010-07-04 07:45:29 +00:00
|
|
|
unsigned int debug;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char **ptr;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27: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-02-20 14:27:00 +00:00
|
|
|
buffer *fcgi_env;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer *path;
|
2006-01-04 01:03:32 +00:00
|
|
|
|
|
|
|
buffer *statuskey;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_config **config_storage;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27: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 {
|
2006-01-02 23:17:04 +00:00
|
|
|
FCGI_STATE_UNSET,
|
2006-10-04 13:26:23 +00:00
|
|
|
FCGI_STATE_INIT,
|
|
|
|
FCGI_STATE_CONNECT_DELAYED,
|
|
|
|
FCGI_STATE_PREPARE_WRITE,
|
|
|
|
FCGI_STATE_WRITE,
|
|
|
|
FCGI_STATE_READ
|
2005-02-20 14:27:00 +00:00
|
|
|
} fcgi_connection_state_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
fcgi_proc *proc;
|
|
|
|
fcgi_extension_host *host;
|
2006-01-02 23:17:04 +00:00
|
|
|
fcgi_extension *ext;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fcgi_connection_state_t state;
|
|
|
|
time_t state_timestamp;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27: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 *rb; /* read queue */
|
|
|
|
chunkqueue *wb; /* write queue */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer *response_header;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t request_id;
|
|
|
|
int fd; /* fd to the fastcgi process */
|
|
|
|
int fde_ndx; /* index into the fd-event buffer */
|
|
|
|
|
2005-03-01 16:54:47 +00:00
|
|
|
pid_t pid;
|
|
|
|
int got_proc;
|
2005-09-14 13:55:06 +00:00
|
|
|
|
|
|
|
int send_content_body;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
plugin_config conf;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27: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 fcgi_handle_fdevent(server *srv, void *ctx, int revents);
|
2005-02-20 14:27:00 +00:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-05-19 18:39:44 +00:00
|
|
|
static void fastcgi_status_copy_procname(buffer *b, fcgi_extension_host *host, fcgi_proc *proc) {
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_copy_string_len(b, CONST_STR_LEN("fastcgi.backend."));
|
2006-01-04 01:03:32 +00:00
|
|
|
buffer_append_string_buffer(b, host->id);
|
2006-02-14 23:51:52 +00:00
|
|
|
if (proc) {
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("."));
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_append_int(b, proc->id);
|
2006-02-14 23:51:52 +00:00
|
|
|
}
|
2009-05-19 18:39:44 +00:00
|
|
|
}
|
2006-01-04 01:03:32 +00:00
|
|
|
|
2009-05-19 18:39:44 +00:00
|
|
|
static void fcgi_proc_load_inc(server *srv, handler_ctx *hctx) {
|
2009-06-03 16:07:20 +00:00
|
|
|
plugin_data *p = hctx->plugin_data;
|
2009-05-19 18:39:44 +00:00
|
|
|
hctx->proc->load++;
|
|
|
|
|
|
|
|
status_counter_inc(srv, CONST_STR_LEN("fastcgi.active-requests"));
|
|
|
|
|
|
|
|
fastcgi_status_copy_procname(p->statuskey, hctx->host, hctx->proc);
|
|
|
|
buffer_append_string_len(p->statuskey, CONST_STR_LEN(".load"));
|
|
|
|
|
|
|
|
status_counter_set(srv, CONST_BUF_LEN(p->statuskey), hctx->proc->load);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fcgi_proc_load_dec(server *srv, handler_ctx *hctx) {
|
2009-06-03 16:07:20 +00:00
|
|
|
plugin_data *p = hctx->plugin_data;
|
2009-05-19 18:39:44 +00:00
|
|
|
hctx->proc->load--;
|
|
|
|
|
|
|
|
status_counter_dec(srv, CONST_STR_LEN("fastcgi.active-requests"));
|
|
|
|
|
|
|
|
fastcgi_status_copy_procname(p->statuskey, hctx->host, hctx->proc);
|
|
|
|
buffer_append_string_len(p->statuskey, CONST_STR_LEN(".load"));
|
|
|
|
|
|
|
|
status_counter_set(srv, CONST_BUF_LEN(p->statuskey), hctx->proc->load);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fcgi_host_assign(server *srv, handler_ctx *hctx, fcgi_extension_host *host) {
|
2009-06-03 16:07:20 +00:00
|
|
|
plugin_data *p = hctx->plugin_data;
|
2009-05-19 18:39:44 +00:00
|
|
|
hctx->host = host;
|
|
|
|
hctx->host->load++;
|
|
|
|
|
|
|
|
fastcgi_status_copy_procname(p->statuskey, hctx->host, NULL);
|
|
|
|
buffer_append_string_len(p->statuskey, CONST_STR_LEN(".load"));
|
|
|
|
|
|
|
|
status_counter_set(srv, CONST_BUF_LEN(p->statuskey), hctx->host->load);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fcgi_host_reset(server *srv, handler_ctx *hctx) {
|
2009-06-03 16:07:20 +00:00
|
|
|
plugin_data *p = hctx->plugin_data;
|
2009-05-19 18:39:44 +00:00
|
|
|
hctx->host->load--;
|
|
|
|
|
|
|
|
fastcgi_status_copy_procname(p->statuskey, hctx->host, NULL);
|
|
|
|
buffer_append_string_len(p->statuskey, CONST_STR_LEN(".load"));
|
|
|
|
|
|
|
|
status_counter_set(srv, CONST_BUF_LEN(p->statuskey), hctx->host->load);
|
|
|
|
|
|
|
|
hctx->host = NULL;
|
2006-01-04 01:03:32 +00:00
|
|
|
}
|
2006-01-02 23:17:04 +00:00
|
|
|
|
2009-10-11 21:54:50 +00:00
|
|
|
static void fcgi_host_disable(server *srv, handler_ctx *hctx) {
|
|
|
|
plugin_data *p = hctx->plugin_data;
|
|
|
|
|
|
|
|
if (hctx->host->disable_time || hctx->proc->is_local) {
|
2009-10-16 09:02:41 +00:00
|
|
|
if (hctx->proc->state == PROC_STATE_RUNNING) hctx->host->active_procs--;
|
2009-10-11 21:54:50 +00:00
|
|
|
hctx->proc->disabled_until = srv->cur_ts + hctx->host->disable_time;
|
|
|
|
hctx->proc->state = hctx->proc->is_local ? PROC_STATE_DIED_WAIT_FOR_PID : PROC_STATE_DIED;
|
|
|
|
|
|
|
|
if (p->conf.debug) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sds",
|
|
|
|
"backend disabled for", hctx->host->disable_time, "seconds");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static int fastcgi_status_init(server *srv, buffer *b, fcgi_extension_host *host, fcgi_proc *proc) {
|
2006-01-04 01:03:32 +00:00
|
|
|
#define CLEAN(x) \
|
|
|
|
fastcgi_status_copy_procname(b, host, proc); \
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(x)); \
|
2006-01-04 01:03:32 +00:00
|
|
|
status_counter_set(srv, CONST_BUF_LEN(b), 0);
|
|
|
|
|
|
|
|
CLEAN(".disabled");
|
|
|
|
CLEAN(".died");
|
|
|
|
CLEAN(".overloaded");
|
|
|
|
CLEAN(".connected");
|
|
|
|
CLEAN(".load");
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
#undef CLEAN
|
2006-02-14 23:51:52 +00:00
|
|
|
|
|
|
|
#define CLEAN(x) \
|
|
|
|
fastcgi_status_copy_procname(b, host, NULL); \
|
2008-07-30 19:38:32 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN(x)); \
|
2006-02-14 23:51:52 +00:00
|
|
|
status_counter_set(srv, CONST_BUF_LEN(b), 0);
|
|
|
|
|
|
|
|
CLEAN(".load");
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
#undef CLEAN
|
2006-01-04 01:03:32 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static handler_ctx * handler_ctx_init(void) {
|
2005-02-20 14:27:00 +00:00
|
|
|
handler_ctx * hctx;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27: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-02-20 14:27:00 +00:00
|
|
|
hctx->fde_ndx = -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
hctx->response_header = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
hctx->request_id = 0;
|
|
|
|
hctx->state = FCGI_STATE_INIT;
|
|
|
|
hctx->proc = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
hctx->fd = -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
hctx->reconnects = 0;
|
2005-09-14 13:55:06 +00:00
|
|
|
hctx->send_content_body = 1;
|
2005-09-08 10:00:32 +00:00
|
|
|
|
|
|
|
hctx->rb = chunkqueue_init();
|
2005-09-26 08:56:39 +00:00
|
|
|
hctx->wb = chunkqueue_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return hctx;
|
|
|
|
}
|
|
|
|
|
2009-05-19 18:39:44 +00:00
|
|
|
static void handler_ctx_free(server *srv, handler_ctx *hctx) {
|
2006-02-01 09:48:03 +00:00
|
|
|
if (hctx->host) {
|
2009-05-19 18:39:44 +00:00
|
|
|
fcgi_host_reset(srv, hctx);
|
2006-02-01 09:48:03 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_free(hctx->response_header);
|
2005-09-08 10:00:32 +00:00
|
|
|
|
|
|
|
chunkqueue_free(hctx->rb);
|
2005-09-26 08:56:39 +00:00
|
|
|
chunkqueue_free(hctx->wb);
|
2005-09-08 10:00:32 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(hctx);
|
|
|
|
}
|
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static fcgi_proc *fastcgi_process_init(void) {
|
2005-02-20 14:27:00 +00:00
|
|
|
fcgi_proc *f;
|
|
|
|
|
|
|
|
f = calloc(1, sizeof(*f));
|
2006-03-08 23:58:18 +00:00
|
|
|
f->unixsocket = buffer_init();
|
|
|
|
f->connection_name = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
f->prev = NULL;
|
|
|
|
f->next = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static void fastcgi_process_free(fcgi_proc *f) {
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!f) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fastcgi_process_free(f->next);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-03-08 23:58:18 +00:00
|
|
|
buffer_free(f->unixsocket);
|
|
|
|
buffer_free(f->connection_name);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(f);
|
|
|
|
}
|
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static fcgi_extension_host *fastcgi_host_init(void) {
|
2005-02-20 14:27:00 +00:00
|
|
|
fcgi_extension_host *f;
|
|
|
|
|
|
|
|
f = calloc(1, sizeof(*f));
|
|
|
|
|
2006-01-04 01:03:32 +00:00
|
|
|
f->id = buffer_init();
|
2005-02-20 14:27:00 +00:00
|
|
|
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();
|
2005-11-10 18:22:07 +00:00
|
|
|
f->strip_request_uri = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static void fastcgi_host_free(fcgi_extension_host *h) {
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!h) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-01-04 01:03:32 +00:00
|
|
|
buffer_free(h->id);
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_free(h->host);
|
|
|
|
buffer_free(h->unixsocket);
|
|
|
|
buffer_free(h->docroot);
|
|
|
|
buffer_free(h->bin_path);
|
2005-11-10 18:22:07 +00:00
|
|
|
buffer_free(h->strip_request_uri);
|
2005-02-20 14:27:00 +00:00
|
|
|
array_free(h->bin_env);
|
|
|
|
array_free(h->bin_env_copy);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fastcgi_process_free(h->first);
|
|
|
|
fastcgi_process_free(h->unused_procs);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(h);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 14:11:41 +00:00
|
|
|
static fcgi_exts *fastcgi_extensions_init(void) {
|
2005-02-20 14:27:00 +00:00
|
|
|
fcgi_exts *f;
|
|
|
|
|
|
|
|
f = calloc(1, sizeof(*f));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static void fastcgi_extensions_free(fcgi_exts *f) {
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!f) return;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; i < f->used; i++) {
|
|
|
|
fcgi_extension *fe;
|
|
|
|
size_t j;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fe = f->exts[i];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (j = 0; j < fe->used; j++) {
|
|
|
|
fcgi_extension_host *h;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
h = fe->hosts[j];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
fastcgi_host_free(h);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_free(fe->key);
|
|
|
|
free(fe->hosts);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(fe);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(f->exts);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
free(f);
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
static int fastcgi_extension_insert(fcgi_exts *ext, buffer *key, fcgi_extension_host *fh) {
|
2005-02-20 14:27:00 +00:00
|
|
|
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));
|
2014-02-16 13:08:20 +00:00
|
|
|
force_assert(fe);
|
2005-02-20 14:27:00 +00:00
|
|
|
fe->key = buffer_init();
|
2007-01-03 09:46:32 +00:00
|
|
|
fe->last_used_ndx = -1;
|
2015-02-08 12:37:10 +00:00
|
|
|
buffer_copy_buffer(fe->key, key);
|
2005-02-20 14:27: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-02-20 14:27: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-02-20 14:27: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-02-20 14:27: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-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
fe->hosts[fe->used++] = fh;
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
return 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INIT_FUNC(mod_fastcgi_init) {
|
|
|
|
plugin_data *p;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p = calloc(1, sizeof(*p));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->fcgi_env = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p->path = buffer_init();
|
2006-01-04 01:03:32 +00:00
|
|
|
|
|
|
|
p->statuskey = buffer_init();
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FREE_FUNC(mod_fastcgi_free) {
|
|
|
|
plugin_data *p = p_d;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
UNUSED(srv);
|
|
|
|
|
|
|
|
buffer_free(p->fcgi_env);
|
|
|
|
buffer_free(p->path);
|
2006-01-04 01:03:32 +00:00
|
|
|
buffer_free(p->statuskey);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27: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];
|
|
|
|
fcgi_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-02-20 14:27:00 +00:00
|
|
|
exts = s->exts;
|
|
|
|
|
|
|
|
for (j = 0; j < exts->used; j++) {
|
|
|
|
fcgi_extension *ex;
|
|