Add support for backing off attempts to start applications that continuously
[mod_fastcgi.git] / fcgi.h
blob0d3a640e28b4ff8bd42e66f45a450af21410f0fa
1 /*
2 * $Id: fcgi.h,v 1.35 2002/02/28 22:52:50 robs Exp $
3 */
5 #ifndef FCGI_H
6 #define FCGI_H
8 #ifdef WIN32
9 #pragma warning( disable : 4115 )
10 #endif
12 /* Apache header files */
13 #include "httpd.h"
14 #include "http_config.h"
15 #include "http_request.h"
16 #include "http_core.h"
17 #include "http_protocol.h"
18 #include "http_main.h"
19 #include "http_log.h"
20 #include "util_script.h"
21 #include "http_conf_globals.h"
22 #include "util_md5.h"
24 #if MODULE_MAGIC_NUMBER < 19980806
25 #error "This version of mod_fastcgi is incompatible with Apache versions 1.3.1 and earlier."
26 #error "Please upgrade, or try the last Apache 1.2 compatible release, mod_fastcgi 2.0.18 (no DSO support)."
27 #endif
29 #ifndef NO_WRITEV
30 #include <sys/uio.h>
31 #endif
33 #ifdef WIN32
34 #include "multithread.h"
35 #pragma warning( default : 4115)
36 #else
37 #include <sys/un.h>
38 #endif
40 /* FastCGI header files */
41 #include "mod_fastcgi.h"
42 /* @@@ This should go away when fcgi_protocol is re-written */
43 #include "fcgi_protocol.h"
45 typedef struct {
46 int size; /* size of entire buffer */
47 int length; /* number of bytes in current buffer */
48 char *begin; /* begining of valid data */
49 char *end; /* end of valid data */
50 char data[1]; /* buffer data */
51 } Buffer;
53 #ifdef WIN32
54 #define READER 0
55 #define WRITER 1
57 #define MBOX_EVENT 0 /* mboc is ready to be read */
58 #define TERM_EVENT 1 /* termination event */
59 #define WAKE_EVENT 2 /* notification of child Fserver dieing */
61 typedef struct _fcgi_pm_job {
62 char id;
63 char *fs_path;
64 char *user;
65 char * group;
66 unsigned long qsec;
67 unsigned long start_time;
68 struct _fcgi_pm_job *next;
69 } fcgi_pm_job;
70 #endif
72 enum process_state {
73 FCGI_RUNNING_STATE, /* currently running */
74 FCGI_START_STATE, /* needs to be started by PM */
75 FCGI_VICTIM_STATE, /* SIGTERM was sent by PM */
76 FCGI_KILLED_STATE, /* a wait() collected VICTIM */
77 FCGI_READY_STATE /* empty cell, init state */
81 * ServerProcess holds data for each process associated with
82 * a class. It is embedded in fcgi_server below.
84 typedef struct _FcgiProcessInfo {
85 #ifdef WIN32
86 HANDLE handle; /* process handle */
87 HANDLE terminationEvent; /* Event used to signal process termination */
88 #endif
89 pid_t pid; /* pid of associated process */
90 enum process_state state; /* state of the process */
91 time_t start_time; /* time the process was started */
92 } ServerProcess;
95 * fcgi_server holds info for each AppClass specified in this
96 * Web server's configuration.
98 typedef struct _FastCgiServerInfo {
99 int flush;
100 char *fs_path; /* pathname of executable */
101 array_header *pass_headers; /* names of headers to pass in the env */
102 u_int idle_timeout; /* fs idle secs allowed before aborting */
103 char **envp; /* if NOT NULL, this is the env to send
104 * to the fcgi app when starting a server
105 * managed app. */
106 u_int listenQueueDepth; /* size of listen queue for IPC */
107 u_int appConnectTimeout; /* timeout (sec) for connect() requests */
108 u_int numProcesses; /* max allowed processes of this class,
109 * or for dynamic apps, the number of
110 * processes actually running */
111 time_t restartTime; /* most recent time when the process
112 * manager started a process in this
113 * class. */
114 int initStartDelay; /* min number of seconds to wait between
115 * starting of AppClass processes at init */
116 u_int restartDelay; /* number of seconds to wait between
117 * restarts after failure. Can be zero. */
118 int restartOnExit; /* = TRUE = restart. else terminate/free */
119 u_int numFailures; /* num restarts due to exit failure */
120 int bad; /* is [not] having start problems */
121 struct sockaddr *socket_addr; /* Socket Address of FCGI app server class */
122 #ifdef WIN32
123 struct sockaddr *dest_addr; /* for local apps on NT need socket address */
124 /* bound to localhost */
125 const char *mutex_env_string; /* string holding the accept mutex handle */
126 #endif
127 int socket_addr_len; /* Length of socket */
128 enum {APP_CLASS_UNKNOWN,
129 APP_CLASS_STANDARD,
130 APP_CLASS_EXTERNAL,
131 APP_CLASS_DYNAMIC}
132 directive; /* AppClass or ExternalAppClass */
133 const char *socket_path; /* Name used to create a socket */
134 const char *host; /* Hostname for externally managed
135 * FastCGI application processes */
136 unsigned short port; /* Port number either for externally
137 * managed FastCGI applications or for
138 * server managed FastCGI applications,
139 * where server became application mngr. */
140 int listenFd; /* Listener socket of FCGI app server
141 * class. Passed to app server process
142 * at process creation. */
143 u_int processPriority; /* If locally server managed process,
144 * this is the priority to run the
145 * processes in this class at. */
146 struct _FcgiProcessInfo *procs; /* Pointer to array of
147 * processes belonging to this class. */
148 int keepConnection; /* = 1 = maintain connection to app. */
149 uid_t uid; /* uid this app should run as (suexec) */
150 gid_t gid; /* gid this app should run as (suexec) */
151 const char *username; /* suexec user arg */
152 const char *group; /* suexec group arg, AND used in comm
153 * between RH and PM */
154 const char *user; /* used in comm between RH and PM */
155 /* Dynamic FastCGI apps configuration parameters */
156 u_long totalConnTime; /* microseconds spent by the web server
157 * waiting while fastcgi app performs
158 * request processing since the last
159 * dynamicUpdateInterval */
160 u_long smoothConnTime; /* exponentially decayed values of the
161 * connection times. */
162 u_long totalQueueTime; /* microseconds spent by the web server
163 * waiting to connect to the fastcgi app
164 * since the last dynamicUpdateInterval. */
165 struct _FastCgiServerInfo *next;
166 } fcgi_server;
170 * fcgi_request holds the state of a particular FastCGI request.
172 typedef struct {
173 #ifdef WIN32
174 SOCKET fd;
175 #else
176 int fd; /* connection to FastCGI server */
177 #endif
178 int gotHeader; /* TRUE if reading content bytes */
179 unsigned char packetType; /* type of packet */
180 int dataLen; /* length of data bytes */
181 int paddingLen; /* record padding after content */
182 fcgi_server *fs; /* FastCGI server info */
183 const char *fs_path; /* fcgi_server path */
184 Buffer *serverInputBuffer; /* input buffer from FastCgi server */
185 Buffer *serverOutputBuffer; /* output buffer to FastCgi server */
186 Buffer *clientInputBuffer; /* client input buffer */
187 Buffer *clientOutputBuffer; /* client output buffer */
188 table *authHeaders; /* headers received from an auth fs */
189 int auth_compat; /* whether the auth request is spec compat */
190 table *saved_subprocess_env; /* subprocess_env before auth handling */
191 #if defined(SIGPIPE) && MODULE_MAGIC_NUMBER < 19990320
192 void (*apache_sigpipe_handler)(int);
193 #endif
194 int expectingClientContent; /* >0 => more content, <=0 => no more */
195 array_header *header;
196 char *fs_stderr;
197 int fs_stderr_len;
198 int parseHeader; /* TRUE iff parsing response headers */
199 request_rec *r;
200 int readingEndRequestBody;
201 FCGI_EndRequestBody endRequestBody;
202 Buffer *erBufPtr;
203 int exitStatus;
204 int exitStatusSet;
205 unsigned int requestId;
206 int eofSent;
207 int role; /* FastCGI Role: Authorizer or Responder */
208 int dynamic; /* whether or not this is a dynamic app */
209 struct timeval startTime; /* dynamic app's connect() attempt start time */
210 struct timeval queueTime; /* dynamic app's connect() complete time */
211 struct timeval completeTime; /* dynamic app's connection close() time */
212 int keepReadingFromFcgiApp; /* still more to read from fcgi app? */
213 const char *user; /* user used to invoke app (suexec) */
214 const char *group; /* group used to invoke app (suexec) */
215 #ifdef WIN32
216 BOOL using_npipe_io; /* named pipe io */
217 #endif
218 } fcgi_request;
220 /* Values of parseHeader field */
221 #define SCAN_CGI_READING_HEADERS 1
222 #define SCAN_CGI_FINISHED 0
223 #define SCAN_CGI_BAD_HEADER -1
224 #define SCAN_CGI_INT_REDIRECT -2
225 #define SCAN_CGI_SRV_REDIRECT -3
227 /* Opcodes for Server->ProcMgr communication */
228 #define FCGI_SERVER_START_JOB 83 /* 'S' - start */
229 #define FCGI_SERVER_RESTART_JOB 82 /* 'R' - restart */
230 #define FCGI_REQUEST_TIMEOUT_JOB 84 /* 'T' - timeout */
231 #define FCGI_REQUEST_COMPLETE_JOB 67 /* 'C' - complete */
233 /* Authorizer types, for auth directives handling */
234 #define FCGI_AUTH_TYPE_AUTHENTICATOR 0
235 #define FCGI_AUTH_TYPE_AUTHORIZER 1
236 #define FCGI_AUTH_TYPE_ACCESS_CHECKER 2
238 /* Bits for auth_options */
239 #define FCGI_AUTHORITATIVE 1
240 #define FCGI_COMPAT 2
242 typedef struct
244 const char *authorizer;
245 u_char authorizer_options;
246 const char *authenticator;
247 u_char authenticator_options;
248 const char *access_checker;
249 u_char access_checker_options;
250 } fcgi_dir_config;
252 #define FCGI_OK 0
253 #define FCGI_FAILED 1
255 #ifdef WIN32
257 #define FCGI_LOG_EMERG_ERRNO __FILE__,__LINE__,APLOG_EMERG /* system is unusable */
258 #define FCGI_LOG_ALERT_ERRNO __FILE__,__LINE__,APLOG_ALERT /* action must be taken immediately */
259 #define FCGI_LOG_CRIT_ERRNO __FILE__,__LINE__,APLOG_CRIT /* critical conditions */
260 #define FCGI_LOG_ERR_ERRNO __FILE__,__LINE__,APLOG_ERR /* error conditions */
261 #define FCGI_LOG_WARN_ERRNO __FILE__,__LINE__,APLOG_WARNING /* warning conditions */
262 #define FCGI_LOG_NOTICE_ERRNO __FILE__,__LINE__,APLOG_NOTICE /* normal but significant condition */
263 #define FCGI_LOG_INFO_ERRNO __FILE__,__LINE__,APLOG_INFO /* informational */
264 #define FCGI_LOG_DEBUG_ERRNO __FILE__,__LINE__,APLOG_DEBUG /* debug-level messages */
266 #define FCGI_LOG_EMERG __FILE__,__LINE__,APLOG_EMERG|APLOG_WIN32ERROR
267 #define FCGI_LOG_ALERT __FILE__,__LINE__,APLOG_ALERT|APLOG_WIN32ERROR
268 #define FCGI_LOG_CRIT __FILE__,__LINE__,APLOG_CRIT|APLOG_WIN32ERROR
269 #define FCGI_LOG_ERR __FILE__,__LINE__,APLOG_ERR|APLOG_WIN32ERROR
270 #define FCGI_LOG_WARN __FILE__,__LINE__,APLOG_WARNING|APLOG_WIN32ERROR
271 #define FCGI_LOG_NOTICE __FILE__,__LINE__,APLOG_NOTICE|APLOG_WIN32ERROR
272 #define FCGI_LOG_INFO __FILE__,__LINE__,APLOG_INFO|APLOG_WIN32ERROR
273 #define FCGI_LOG_DEBUG __FILE__,__LINE__,APLOG_DEBUG|APLOG_WIN32ERROR
275 #else
277 #define FCGI_LOG_EMERG __FILE__,__LINE__,APLOG_EMERG /* system is unusable */
278 #define FCGI_LOG_ALERT __FILE__,__LINE__,APLOG_ALERT /* action must be taken immediately */
279 #define FCGI_LOG_CRIT __FILE__,__LINE__,APLOG_CRIT /* critical conditions */
280 #define FCGI_LOG_ERR __FILE__,__LINE__,APLOG_ERR /* error conditions */
281 #define FCGI_LOG_WARN __FILE__,__LINE__,APLOG_WARNING /* warning conditions */
282 #define FCGI_LOG_NOTICE __FILE__,__LINE__,APLOG_NOTICE /* normal but significant condition */
283 #define FCGI_LOG_INFO __FILE__,__LINE__,APLOG_INFO /* informational */
284 #define FCGI_LOG_DEBUG __FILE__,__LINE__,APLOG_DEBUG /* debug-level messages */
286 #define FCGI_LOG_EMERG_ERRNO FCGI_LOG_EMERG
287 #define FCGI_LOG_ALERT_ERRNO FCGI_LOG_ALERT
288 #define FCGI_LOG_CRIT_ERRNO FCGI_LOG_CRIT
289 #define FCGI_LOG_ERR_ERRNO FCGI_LOG_ERR
290 #define FCGI_LOG_WARN_ERRNO FCGI_LOG_WARN
291 #define FCGI_LOG_NOTICE_ERRNO FCGI_LOG_NOTICE
292 #define FCGI_LOG_INFO_ERRNO FCGI_LOG_INFO
293 #define FCGI_LOG_DEBUG_ERRNO FCGI_LOG_DEBUG
295 #endif
297 #define FCGI_LOG_EMERG_NOERRNO __FILE__,__LINE__,APLOG_EMERG|APLOG_NOERRNO
298 #define FCGI_LOG_ALERT_NOERRNO __FILE__,__LINE__,APLOG_ALERT|APLOG_NOERRNO
299 #define FCGI_LOG_CRIT_NOERRNO __FILE__,__LINE__,APLOG_CRIT|APLOG_NOERRNO
300 #define FCGI_LOG_ERR_NOERRNO __FILE__,__LINE__,APLOG_ERR|APLOG_NOERRNO
301 #define FCGI_LOG_WARN_NOERRNO __FILE__,__LINE__,APLOG_WARNING|APLOG_NOERRNO
302 #define FCGI_LOG_NOTICE_NOERRNO __FILE__,__LINE__,APLOG_NOTICE|APLOG_NOERRNO
303 #define FCGI_LOG_INFO_NOERRNO __FILE__,__LINE__,APLOG_INFO|APLOG_NOERRNO
304 #define FCGI_LOG_DEBUG_NOERRNO __FILE__,__LINE__,APLOG_DEBUG|APLOG_NOERRNO
306 #ifdef FCGI_DEBUG
307 #define FCGIDBG1(a) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a);
308 #define FCGIDBG2(a,b) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b);
309 #define FCGIDBG3(a,b,c) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b,c);
310 #define FCGIDBG4(a,b,c,d) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b,c,d);
311 #define FCGIDBG5(a,b,c,d,e) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b,c,d,e);
312 #define FCGIDBG6(a,b,c,d,e,f) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b,c,d,e,f);
313 #define FCGIDBG7(a,b,c,d,e,f,g) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b,c,d,e,f,g);
314 #else
315 #define FCGIDBG1(a)
316 #define FCGIDBG2(a,b)
317 #define FCGIDBG3(a,b,c)
318 #define FCGIDBG4(a,b,c,d)
319 #define FCGIDBG5(a,b,c,d,e)
320 #define FCGIDBG6(a,b,c,d,e,f)
321 #define FCGIDBG7(a,b,c,d,e,f,g)
322 #endif
325 * Holds the status of the sending of the environment.
326 * A quick hack to dump the static vars for the NT port.
328 typedef struct {
329 enum { PREP, HEADER, NAME, VALUE } pass;
330 char **envp;
331 int headerLen, nameLen, valueLen, totalLen;
332 char *equalPtr;
333 unsigned char headerBuff[8];
334 } env_status;
337 * fcgi_config.c
339 void *fcgi_config_create_dir_config(pool *p, char *dummy);
340 const char *fcgi_config_make_dir(pool *tp, char *path);
341 const char *fcgi_config_make_dynamic_dir(pool *p, const int wax);
342 const char *fcgi_config_new_static_server(cmd_parms *cmd, void *dummy, const char *arg);
343 const char *fcgi_config_new_external_server(cmd_parms *cmd, void *dummy, const char *arg);
344 const char *fcgi_config_set_config(cmd_parms *cmd, void *dummy, const char *arg);
345 const char *fcgi_config_set_fcgi_uid_n_gid(int set);
346 const char *fcgi_config_new_auth_server(cmd_parms * const cmd,
347 fcgi_dir_config *dir_config, const char *fs_path, const char * const compat);
348 const char *fcgi_config_set_authoritative_slot(const cmd_parms * const cmd,
349 fcgi_dir_config * const dir_config, int arg);
350 const char *fcgi_config_set_socket_dir(cmd_parms *cmd, void *dummy, char *arg);
351 const char *fcgi_config_set_wrapper(cmd_parms *cmd, void *dummy, const char *arg);
352 void fcgi_config_reset_globals(void* dummy);
353 const char *fcgi_config_set_env_var(pool *p, char **envp, unsigned int *envc, char * var);
356 * fcgi_pm.c
358 #ifdef WIN32
359 void fcgi_pm_main(void *dummy);
360 #else
361 int fcgi_pm_main(void *dummy, child_info *info);
362 #endif
365 * fcgi_protocol.c
367 void fcgi_protocol_queue_begin_request(fcgi_request *fr);
368 void fcgi_protocol_queue_client_buffer(fcgi_request *fr);
369 int fcgi_protocol_queue_env(request_rec *r, fcgi_request *fr, env_status *env);
370 int fcgi_protocol_dequeue(pool *p, fcgi_request *fr);
373 * fcgi_buf.c
375 #define BufferLength(b) ((b)->length)
376 #define BufferFree(b) ((b)->size - (b)->length)
377 #define BufferSize(b) ((b)->size)
379 void fcgi_buf_check(Buffer *bufPtr);
380 void fcgi_buf_reset(Buffer *bufPtr);
381 Buffer *fcgi_buf_new(pool *p, int size);
382 void BufferDelete(Buffer *bufPtr);
384 #ifdef WIN32
385 int fcgi_buf_add_fd(Buffer *buf, SOCKET fd);
386 int fcgi_buf_get_to_fd(Buffer *bufPtr, SOCKET fd);
387 #else
388 int fcgi_buf_add_fd(Buffer *buf, int fd);
389 int fcgi_buf_get_to_fd(Buffer *bufPtr, int fd);
390 #endif
392 void fcgi_buf_get_block_info(Buffer *bufPtr, char **beginPtr, int *countPtr);
393 void fcgi_buf_toss(Buffer *bufPtr, int count);
394 void fcgi_buf_get_free_block_info(Buffer *bufPtr, char **endPtr, int *countPtr);
395 void fcgi_buf_add_update(Buffer *bufPtr, int count);
396 int fcgi_buf_add_block(Buffer *bufPtr, char *data, int datalen);
397 int fcgi_buf_add_string(Buffer *bufPtr, char *str);
398 int fcgi_buf_get_to_block(Buffer *bufPtr, char *data, int datalen);
399 void fcgi_buf_get_to_buf(Buffer *toPtr, Buffer *fromPtr, int len);
400 void fcgi_buf_get_to_array(Buffer *buf, array_header *arr, int len);
403 * fcgi_util.c
406 char *fcgi_util_socket_hash_filename(pool *p, const char *path,
407 const char *user, const char *group);
408 const char *fcgi_util_socket_make_path_absolute(pool * const p,
409 const char *const file, const int dynamic);
410 #ifndef WIN32
411 const char *fcgi_util_socket_make_domain_addr(pool *p, struct sockaddr_un **socket_addr,
412 int *socket_addr_len, const char *socket_path);
413 #endif
414 const char *fcgi_util_socket_make_inet_addr(pool *p, struct sockaddr_in **socket_addr,
415 int *socket_addr_len, const char *host, unsigned short port);
416 const char *fcgi_util_check_access(pool *tp,
417 const char * const path, const struct stat *statBuf,
418 const int mode, const uid_t uid, const gid_t gid);
419 fcgi_server *fcgi_util_fs_get_by_id(const char *ePath, uid_t uid, gid_t gid);
420 fcgi_server *fcgi_util_fs_get(const char *ePath, const char *user, const char *group);
421 const char *fcgi_util_fs_is_path_ok(pool * const p, const char * const fs_path, struct stat *finfo);
422 fcgi_server *fcgi_util_fs_new(pool *p);
423 void fcgi_util_fs_add(fcgi_server *s);
424 const char *fcgi_util_fs_set_uid_n_gid(pool *p, fcgi_server *s, uid_t uid, gid_t gid);
425 ServerProcess *fcgi_util_fs_create_procs(pool *p, int num);
427 int fcgi_util_ticks(struct timeval *);
429 #ifdef WIN32
430 int fcgi_pm_add_job(fcgi_pm_job *new_job);
431 #endif
435 * Globals
438 extern pool *fcgi_config_pool;
440 extern server_rec *fcgi_apache_main_server;
442 extern const char *fcgi_wrapper; /* wrapper path */
443 extern uid_t fcgi_user_id; /* the run uid of Apache & PM */
444 extern gid_t fcgi_group_id; /* the run gid of Apache & PM */
446 extern fcgi_server *fcgi_servers;
448 extern char *fcgi_socket_dir; /* default FastCgiIpcDir */
450 /* pipe used for comm between the request handlers and the PM */
451 extern int fcgi_pm_pipe[];
453 extern pid_t fcgi_pm_pid;
455 extern char *fcgi_dynamic_dir; /* directory for the dynamic
456 * fastcgi apps' sockets */
458 extern char *fcgi_empty_env;
460 extern int fcgi_dynamic_total_proc_count;
461 extern time_t fcgi_dynamic_epoch;
462 extern time_t fcgi_dynamic_last_analyzed;
464 #ifdef WIN32
465 extern HANDLE *fcgi_dynamic_mbox_mutex;
466 extern HANDLE fcgi_event_handles[3];
467 extern fcgi_pm_job *fcgi_dynamic_mbox;
468 #endif
470 extern u_int dynamicMaxProcs;
471 extern int dynamicMinProcs;
472 extern int dynamicMaxClassProcs;
473 extern u_int dynamicKillInterval;
474 extern u_int dynamicUpdateInterval;
475 extern float dynamicGain;
476 extern int dynamicThreshold1;
477 extern int dynamicThresholdN;
478 extern u_int dynamicPleaseStartDelay;
479 extern u_int dynamicAppConnectTimeout;
480 extern char **dynamicEnvp;
481 extern u_int dynamicProcessSlack;
482 extern int dynamicAutoRestart;
483 extern int dynamicAutoUpdate;
484 extern u_int dynamicListenQueueDepth;
485 extern u_int dynamicInitStartDelay;
486 extern u_int dynamicRestartDelay;
487 extern array_header *dynamic_pass_headers;
488 extern u_int dynamic_idle_timeout;
489 extern int dynamicFlush;
491 extern module MODULE_VAR_EXPORT fastcgi_module;
493 #endif /* FCGI_H */