Add discussion of signals.
[mod_fastcgi.git] / fcgi.h
blobeb86b8c72915c530a254c72a7093354971453e7b
1 /*
2 * $Id: fcgi.h,v 1.32 2001/05/29 15:22:12 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 } ServerProcess;
94 * fcgi_server holds info for each AppClass specified in this
95 * Web server's configuration.
97 typedef struct _FastCgiServerInfo {
98 int flush;
99 char *fs_path; /* pathname of executable */
100 array_header *pass_headers; /* names of headers to pass in the env */
101 u_int idle_timeout; /* fs idle secs allowed before aborting */
102 char **envp; /* if NOT NULL, this is the env to send
103 * to the fcgi app when starting a server
104 * managed app. */
105 u_int listenQueueDepth; /* size of listen queue for IPC */
106 u_int appConnectTimeout; /* timeout (sec) for connect() requests */
107 u_int numProcesses; /* max allowed processes of this class,
108 * or for dynamic apps, the number of
109 * processes actually running */
110 time_t restartTime; /* most recent time when the process
111 * manager started a process in this
112 * class. */
113 int initStartDelay; /* min number of seconds to wait between
114 * starting of AppClass processes at init */
115 u_int restartDelay; /* number of seconds to wait between
116 * restarts after failure. Can be zero. */
117 int restartOnExit; /* = TRUE = restart. else terminate/free */
118 u_int numRestarts; /* Total number of restarts */
119 u_int numFailures; /* num restarts due to exit failure */
120 struct sockaddr *socket_addr; /* Socket Address of FCGI app server class */
121 #ifdef WIN32
122 struct sockaddr *dest_addr; /* for local apps on NT need socket address */
123 /* bound to localhost */
124 const char *mutex_env_string; /* string holding the accept mutex handle */
125 #endif
126 int socket_addr_len; /* Length of socket */
127 enum {APP_CLASS_UNKNOWN,
128 APP_CLASS_STANDARD,
129 APP_CLASS_EXTERNAL,
130 APP_CLASS_DYNAMIC}
131 directive; /* AppClass or ExternalAppClass */
132 const char *socket_path; /* Name used to create a socket */
133 const char *host; /* Hostname for externally managed
134 * FastCGI application processes */
135 unsigned short port; /* Port number either for externally
136 * managed FastCGI applications or for
137 * server managed FastCGI applications,
138 * where server became application mngr. */
139 int listenFd; /* Listener socket of FCGI app server
140 * class. Passed to app server process
141 * at process creation. */
142 u_int processPriority; /* If locally server managed process,
143 * this is the priority to run the
144 * processes in this class at. */
145 struct _FcgiProcessInfo *procs; /* Pointer to array of
146 * processes belonging to this class. */
147 int keepConnection; /* = 1 = maintain connection to app. */
148 uid_t uid; /* uid this app should run as (suexec) */
149 gid_t gid; /* gid this app should run as (suexec) */
150 const char *username; /* suexec user arg */
151 const char *group; /* suexec group arg, AND used in comm
152 * between RH and PM */
153 const char *user; /* used in comm between RH and PM */
154 /* Dynamic FastCGI apps configuration parameters */
155 u_long totalConnTime; /* microseconds spent by the web server
156 * waiting while fastcgi app performs
157 * request processing since the last
158 * dynamicUpdateInterval */
159 u_long smoothConnTime; /* exponentially decayed values of the
160 * connection times. */
161 u_long totalQueueTime; /* microseconds spent by the web server
162 * waiting to connect to the fastcgi app
163 * since the last dynamicUpdateInterval. */
164 struct _FastCgiServerInfo *next;
165 } fcgi_server;
169 * fcgi_request holds the state of a particular FastCGI request.
171 typedef struct {
172 #ifdef WIN32
173 SOCKET fd;
174 #else
175 int fd; /* connection to FastCGI server */
176 #endif
177 int gotHeader; /* TRUE if reading content bytes */
178 unsigned char packetType; /* type of packet */
179 int dataLen; /* length of data bytes */
180 int paddingLen; /* record padding after content */
181 fcgi_server *fs; /* FastCGI server info */
182 const char *fs_path; /* fcgi_server path */
183 Buffer *serverInputBuffer; /* input buffer from FastCgi server */
184 Buffer *serverOutputBuffer; /* output buffer to FastCgi server */
185 Buffer *clientInputBuffer; /* client input buffer */
186 Buffer *clientOutputBuffer; /* client output buffer */
187 table *authHeaders; /* headers received from an auth fs */
188 int auth_compat; /* whether the auth request is spec compat */
189 table *saved_subprocess_env; /* subprocess_env before auth handling */
190 #if defined(SIGPIPE) && MODULE_MAGIC_NUMBER < 19990320
191 void (*apache_sigpipe_handler)(int);
192 #endif
193 int expectingClientContent; /* >0 => more content, <=0 => no more */
194 array_header *header;
195 char *fs_stderr;
196 int fs_stderr_len;
197 int parseHeader; /* TRUE iff parsing response headers */
198 request_rec *r;
199 int readingEndRequestBody;
200 FCGI_EndRequestBody endRequestBody;
201 Buffer *erBufPtr;
202 int exitStatus;
203 int exitStatusSet;
204 unsigned int requestId;
205 int eofSent;
206 int role; /* FastCGI Role: Authorizer or Responder */
207 int dynamic; /* whether or not this is a dynamic app */
208 struct timeval startTime; /* dynamic app's connect() attempt start time */
209 struct timeval queueTime; /* dynamic app's connect() complete time */
210 struct timeval completeTime; /* dynamic app's connection close() time */
211 int keepReadingFromFcgiApp; /* still more to read from fcgi app? */
212 const char *user; /* user used to invoke app (suexec) */
213 const char *group; /* group used to invoke app (suexec) */
214 #ifdef WIN32
215 BOOL using_npipe_io; /* named pipe io */
216 #endif
217 } fcgi_request;
219 /* Values of parseHeader field */
220 #define SCAN_CGI_READING_HEADERS 1
221 #define SCAN_CGI_FINISHED 0
222 #define SCAN_CGI_BAD_HEADER -1
223 #define SCAN_CGI_INT_REDIRECT -2
224 #define SCAN_CGI_SRV_REDIRECT -3
226 /* Opcodes for Server->ProcMgr communication */
227 #define FCGI_SERVER_START_JOB 83 /* 'S' - start */
228 #define FCGI_SERVER_RESTART_JOB 82 /* 'R' - restart */
229 #define FCGI_REQUEST_TIMEOUT_JOB 84 /* 'T' - timeout */
230 #define FCGI_REQUEST_COMPLETE_JOB 67 /* 'C' - complete */
232 /* Authorizer types, for auth directives handling */
233 #define FCGI_AUTH_TYPE_AUTHENTICATOR 0
234 #define FCGI_AUTH_TYPE_AUTHORIZER 1
235 #define FCGI_AUTH_TYPE_ACCESS_CHECKER 2
237 /* Bits for auth_options */
238 #define FCGI_AUTHORITATIVE 1
239 #define FCGI_COMPAT 2
241 typedef struct
243 const char *authorizer;
244 u_char authorizer_options;
245 const char *authenticator;
246 u_char authenticator_options;
247 const char *access_checker;
248 u_char access_checker_options;
249 } fcgi_dir_config;
251 #define FCGI_OK 0
252 #define FCGI_FAILED 1
254 #ifdef WIN32
256 #define FCGI_LOG_EMERG_ERRNO __FILE__,__LINE__,APLOG_EMERG /* system is unusable */
257 #define FCGI_LOG_ALERT_ERRNO __FILE__,__LINE__,APLOG_ALERT /* action must be taken immediately */
258 #define FCGI_LOG_CRIT_ERRNO __FILE__,__LINE__,APLOG_CRIT /* critical conditions */
259 #define FCGI_LOG_ERR_ERRNO __FILE__,__LINE__,APLOG_ERR /* error conditions */
260 #define FCGI_LOG_WARN_ERRNO __FILE__,__LINE__,APLOG_WARNING /* warning conditions */
261 #define FCGI_LOG_NOTICE_ERRNO __FILE__,__LINE__,APLOG_NOTICE /* normal but significant condition */
262 #define FCGI_LOG_INFO_ERRNO __FILE__,__LINE__,APLOG_INFO /* informational */
263 #define FCGI_LOG_DEBUG_ERRNO __FILE__,__LINE__,APLOG_DEBUG /* debug-level messages */
265 #define FCGI_LOG_EMERG __FILE__,__LINE__,APLOG_EMERG|APLOG_WIN32ERROR
266 #define FCGI_LOG_ALERT __FILE__,__LINE__,APLOG_ALERT|APLOG_WIN32ERROR
267 #define FCGI_LOG_CRIT __FILE__,__LINE__,APLOG_CRIT|APLOG_WIN32ERROR
268 #define FCGI_LOG_ERR __FILE__,__LINE__,APLOG_ERR|APLOG_WIN32ERROR
269 #define FCGI_LOG_WARN __FILE__,__LINE__,APLOG_WARNING|APLOG_WIN32ERROR
270 #define FCGI_LOG_NOTICE __FILE__,__LINE__,APLOG_NOTICE|APLOG_WIN32ERROR
271 #define FCGI_LOG_INFO __FILE__,__LINE__,APLOG_INFO|APLOG_WIN32ERROR
272 #define FCGI_LOG_DEBUG __FILE__,__LINE__,APLOG_DEBUG|APLOG_WIN32ERROR
274 #else
276 #define FCGI_LOG_EMERG __FILE__,__LINE__,APLOG_EMERG /* system is unusable */
277 #define FCGI_LOG_ALERT __FILE__,__LINE__,APLOG_ALERT /* action must be taken immediately */
278 #define FCGI_LOG_CRIT __FILE__,__LINE__,APLOG_CRIT /* critical conditions */
279 #define FCGI_LOG_ERR __FILE__,__LINE__,APLOG_ERR /* error conditions */
280 #define FCGI_LOG_WARN __FILE__,__LINE__,APLOG_WARNING /* warning conditions */
281 #define FCGI_LOG_NOTICE __FILE__,__LINE__,APLOG_NOTICE /* normal but significant condition */
282 #define FCGI_LOG_INFO __FILE__,__LINE__,APLOG_INFO /* informational */
283 #define FCGI_LOG_DEBUG __FILE__,__LINE__,APLOG_DEBUG /* debug-level messages */
285 #define FCGI_LOG_EMERG_ERRNO FCGI_LOG_EMERG
286 #define FCGI_LOG_ALERT_ERRNO FCGI_LOG_ALERT
287 #define FCGI_LOG_CRIT_ERRNO FCGI_LOG_CRIT
288 #define FCGI_LOG_ERR_ERRNO FCGI_LOG_ERR
289 #define FCGI_LOG_WARN_ERRNO FCGI_LOG_WARN
290 #define FCGI_LOG_NOTICE_ERRNO FCGI_LOG_NOTICE
291 #define FCGI_LOG_INFO_ERRNO FCGI_LOG_INFO
292 #define FCGI_LOG_DEBUG_ERRNO FCGI_LOG_DEBUG
294 #endif
296 #define FCGI_LOG_EMERG_NOERRNO __FILE__,__LINE__,APLOG_EMERG|APLOG_NOERRNO
297 #define FCGI_LOG_ALERT_NOERRNO __FILE__,__LINE__,APLOG_ALERT|APLOG_NOERRNO
298 #define FCGI_LOG_CRIT_NOERRNO __FILE__,__LINE__,APLOG_CRIT|APLOG_NOERRNO
299 #define FCGI_LOG_ERR_NOERRNO __FILE__,__LINE__,APLOG_ERR|APLOG_NOERRNO
300 #define FCGI_LOG_WARN_NOERRNO __FILE__,__LINE__,APLOG_WARNING|APLOG_NOERRNO
301 #define FCGI_LOG_NOTICE_NOERRNO __FILE__,__LINE__,APLOG_NOTICE|APLOG_NOERRNO
302 #define FCGI_LOG_INFO_NOERRNO __FILE__,__LINE__,APLOG_INFO|APLOG_NOERRNO
303 #define FCGI_LOG_DEBUG_NOERRNO __FILE__,__LINE__,APLOG_DEBUG|APLOG_NOERRNO
305 #ifdef FCGI_DEBUG
306 #define FCGIDBG1(a) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a);
307 #define FCGIDBG2(a,b) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b);
308 #define FCGIDBG3(a,b,c) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b,c);
309 #define FCGIDBG4(a,b,c,d) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b,c,d);
310 #define FCGIDBG5(a,b,c,d,e) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b,c,d,e);
311 #define FCGIDBG6(a,b,c,d,e,f) ap_log_error(FCGI_LOG_DEBUG,fcgi_apache_main_server,a,b,c,d,e,f);
312 #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);
313 #else
314 #define FCGIDBG1(a)
315 #define FCGIDBG2(a,b)
316 #define FCGIDBG3(a,b,c)
317 #define FCGIDBG4(a,b,c,d)
318 #define FCGIDBG5(a,b,c,d,e)
319 #define FCGIDBG6(a,b,c,d,e,f)
320 #define FCGIDBG7(a,b,c,d,e,f,g)
321 #endif
324 * Holds the status of the sending of the environment.
325 * A quick hack to dump the static vars for the NT port.
327 typedef struct {
328 enum { PREP, HEADER, NAME, VALUE } pass;
329 char **envp;
330 int headerLen, nameLen, valueLen, totalLen;
331 char *equalPtr;
332 unsigned char headerBuff[8];
333 } env_status;
336 * fcgi_config.c
338 void *fcgi_config_create_dir_config(pool *p, char *dummy);
339 const char *fcgi_config_make_dir(pool *tp, char *path);
340 const char *fcgi_config_make_dynamic_dir(pool *p, const int wax);
341 const char *fcgi_config_new_static_server(cmd_parms *cmd, void *dummy, const char *arg);
342 const char *fcgi_config_new_external_server(cmd_parms *cmd, void *dummy, const char *arg);
343 const char *fcgi_config_set_config(cmd_parms *cmd, void *dummy, const char *arg);
344 const char *fcgi_config_set_fcgi_uid_n_gid(int set);
345 const char *fcgi_config_new_auth_server(cmd_parms * const cmd,
346 fcgi_dir_config *dir_config, const char *fs_path, const char * const compat);
347 const char *fcgi_config_set_authoritative_slot(const cmd_parms * const cmd,
348 fcgi_dir_config * const dir_config, int arg);
349 const char *fcgi_config_set_socket_dir(cmd_parms *cmd, void *dummy, char *arg);
350 const char *fcgi_config_set_wrapper(cmd_parms *cmd, void *dummy, const char *arg);
351 void fcgi_config_reset_globals(void* dummy);
352 const char *fcgi_config_set_env_var(pool *p, char **envp, unsigned int *envc, char * var);
355 * fcgi_pm.c
357 #ifdef WIN32
358 void fcgi_pm_main(void *dummy);
359 #else
360 int fcgi_pm_main(void *dummy, child_info *info);
361 #endif
364 * fcgi_protocol.c
366 void fcgi_protocol_queue_begin_request(fcgi_request *fr);
367 void fcgi_protocol_queue_client_buffer(fcgi_request *fr);
368 int fcgi_protocol_queue_env(request_rec *r, fcgi_request *fr, env_status *env);
369 int fcgi_protocol_dequeue(pool *p, fcgi_request *fr);
372 * fcgi_buf.c
374 #define BufferLength(b) ((b)->length)
375 #define BufferFree(b) ((b)->size - (b)->length)
376 #define BufferSize(b) ((b)->size)
378 void fcgi_buf_check(Buffer *bufPtr);
379 void fcgi_buf_reset(Buffer *bufPtr);
380 Buffer *fcgi_buf_new(pool *p, int size);
381 void BufferDelete(Buffer *bufPtr);
383 #ifdef WIN32
384 int fcgi_buf_add_fd(Buffer *buf, SOCKET fd);
385 int fcgi_buf_get_to_fd(Buffer *bufPtr, SOCKET fd);
386 #else
387 int fcgi_buf_add_fd(Buffer *buf, int fd);
388 int fcgi_buf_get_to_fd(Buffer *bufPtr, int fd);
389 #endif
391 void fcgi_buf_get_block_info(Buffer *bufPtr, char **beginPtr, size_t *countPtr);
392 void fcgi_buf_toss(Buffer *bufPtr, size_t count);
393 void fcgi_buf_get_free_block_info(Buffer *bufPtr, char **endPtr, size_t *countPtr);
394 void fcgi_buf_add_update(Buffer *bufPtr, size_t count);
395 int fcgi_buf_add_block(Buffer *bufPtr, char *data, size_t datalen);
396 int fcgi_buf_add_string(Buffer *bufPtr, char *str);
397 int fcgi_buf_get_to_block(Buffer *bufPtr, char *data, int datalen);
398 void fcgi_buf_get_to_buf(Buffer *toPtr, Buffer *fromPtr, int len);
399 void fcgi_buf_get_to_array(Buffer *buf,array_header *arr, size_t len);
402 * fcgi_util.c
405 char *fcgi_util_socket_hash_filename(pool *p, const char *path,
406 const char *user, const char *group);
407 const char *fcgi_util_socket_make_path_absolute(pool * const p,
408 const char *const file, const int dynamic);
409 #ifndef WIN32
410 const char *fcgi_util_socket_make_domain_addr(pool *p, struct sockaddr_un **socket_addr,
411 int *socket_addr_len, const char *socket_path);
412 #endif
413 const char *fcgi_util_socket_make_inet_addr(pool *p, struct sockaddr_in **socket_addr,
414 int *socket_addr_len, const char *host, unsigned short port);
415 const char *fcgi_util_check_access(pool *tp,
416 const char * const path, const struct stat *statBuf,
417 const int mode, const uid_t uid, const gid_t gid);
418 fcgi_server *fcgi_util_fs_get_by_id(const char *ePath, uid_t uid, gid_t gid);
419 fcgi_server *fcgi_util_fs_get(const char *ePath, const char *user, const char *group);
420 const char *fcgi_util_fs_is_path_ok(pool * const p, const char * const fs_path, struct stat *finfo);
421 fcgi_server *fcgi_util_fs_new(pool *p);
422 void fcgi_util_fs_add(fcgi_server *s);
423 const char *fcgi_util_fs_set_uid_n_gid(pool *p, fcgi_server *s, uid_t uid, gid_t gid);
424 ServerProcess *fcgi_util_fs_create_procs(pool *p, int num);
426 int fcgi_util_gettimeofday(struct timeval *);
428 #ifdef WIN32
429 int fcgi_pm_add_job(fcgi_pm_job *new_job);
430 #endif
434 * Globals
437 extern pool *fcgi_config_pool;
439 extern server_rec *fcgi_apache_main_server;
441 extern const char *fcgi_wrapper; /* wrapper path */
442 extern uid_t fcgi_user_id; /* the run uid of Apache & PM */
443 extern gid_t fcgi_group_id; /* the run gid of Apache & PM */
445 extern fcgi_server *fcgi_servers;
447 extern char *fcgi_socket_dir; /* default FastCgiIpcDir */
449 /* pipe used for comm between the request handlers and the PM */
450 extern int fcgi_pm_pipe[];
452 extern pid_t fcgi_pm_pid;
454 extern char *fcgi_dynamic_dir; /* directory for the dynamic
455 * fastcgi apps' sockets */
457 extern char *fcgi_empty_env;
459 extern int fcgi_dynamic_total_proc_count;
460 extern time_t fcgi_dynamic_epoch;
461 extern time_t fcgi_dynamic_last_analyzed;
463 #ifdef WIN32
464 extern HANDLE *fcgi_dynamic_mbox_mutex;
465 extern HANDLE fcgi_event_handles[3];
466 extern fcgi_pm_job *fcgi_dynamic_mbox;
467 #endif
469 extern u_int dynamicMaxProcs;
470 extern int dynamicMinProcs;
471 extern int dynamicMaxClassProcs;
472 extern u_int dynamicKillInterval;
473 extern u_int dynamicUpdateInterval;
474 extern float dynamicGain;
475 extern int dynamicThreshold1;
476 extern int dynamicThresholdN;
477 extern u_int dynamicPleaseStartDelay;
478 extern u_int dynamicAppConnectTimeout;
479 extern char **dynamicEnvp;
480 extern u_int dynamicProcessSlack;
481 extern int dynamicAutoRestart;
482 extern int dynamicAutoUpdate;
483 extern u_int dynamicListenQueueDepth;
484 extern u_int dynamicInitStartDelay;
485 extern u_int dynamicRestartDelay;
486 extern array_header *dynamic_pass_headers;
487 extern u_int dynamic_idle_timeout;
488 extern int dynamicFlush;
490 extern module MODULE_VAR_EXPORT fastcgi_module;
492 #endif /* FCGI_H */