[cygwin] fix mod_proxy and mod_fastcgi ioctl use
[lighttpd.git] / src / mod_fastcgi.c
blobd55679f6b0fa8df564b8cd041e7063b5bf5684f6
1 #include "first.h"
3 #include "buffer.h"
4 #include "server.h"
5 #include "keyvalue.h"
6 #include "log.h"
8 #include "http_chunk.h"
9 #include "fdevent.h"
10 #include "connections.h"
11 #include "response.h"
12 #include "joblist.h"
14 #include "plugin.h"
16 #include "inet_ntop_cache.h"
17 #include "stat_cache.h"
18 #include "status_counter.h"
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <assert.h>
28 #include <signal.h>
30 #ifdef HAVE_FASTCGI_FASTCGI_H
31 # include <fastcgi/fastcgi.h>
32 #else
33 # ifdef HAVE_FASTCGI_H
34 # include <fastcgi.h>
35 # else
36 # include "fastcgi.h"
37 # endif
38 #endif /* HAVE_FASTCGI_FASTCGI_H */
40 #include <stdio.h>
42 #include "sys-socket.h"
44 #ifdef HAVE_SYS_UIO_H
45 #include <sys/uio.h>
46 #endif
47 #ifdef HAVE_SYS_WAIT_H
48 #include <sys/wait.h>
49 #endif
51 #include "version.h"
55 * TODO:
57 * - add timeout for a connect to a non-fastcgi process
58 * (use state_timestamp + state)
62 typedef struct fcgi_proc {
63 size_t id; /* id will be between 1 and max_procs */
64 buffer *unixsocket; /* config.socket + "-" + id */
65 unsigned port; /* config.port + pno */
67 buffer *connection_name; /* either tcp:<host>:<port> or unix:<socket> for debugging purposes */
69 pid_t pid; /* PID of the spawned process (0 if not spawned locally) */
72 size_t load; /* number of requests waiting on this process */
74 size_t requests; /* see max_requests */
75 struct fcgi_proc *prev, *next; /* see first */
77 time_t disabled_until; /* this proc is disabled until, use something else until then */
79 int is_local;
81 enum {
82 PROC_STATE_UNSET, /* init-phase */
83 PROC_STATE_RUNNING, /* alive */
84 PROC_STATE_OVERLOADED, /* listen-queue is full,
85 don't send anything to this proc for the next 2 seconds */
86 PROC_STATE_DIED_WAIT_FOR_PID, /* */
87 PROC_STATE_DIED, /* marked as dead, should be restarted */
88 PROC_STATE_KILLED /* was killed as we don't have the load anymore */
89 } state;
90 } fcgi_proc;
92 typedef struct {
93 /* the key that is used to reference this value */
94 buffer *id;
96 /* list of processes handling this extension
97 * sorted by lowest load
99 * whenever a job is done move it up in the list
100 * until it is sorted, move it down as soon as the
101 * job is started
103 fcgi_proc *first;
104 fcgi_proc *unused_procs;
107 * spawn at least min_procs, at max_procs.
109 * as soon as the load of the first entry
110 * is max_load_per_proc we spawn a new one
111 * and add it to the first entry and give it
112 * the load
116 unsigned short max_procs;
117 size_t num_procs; /* how many procs are started */
118 size_t active_procs; /* how many of them are really running, i.e. state = PROC_STATE_RUNNING */
121 * time after a disabled remote connection is tried to be re-enabled
126 unsigned short disable_time;
129 * some fastcgi processes get a little bit larger
130 * than wanted. max_requests_per_proc kills a
131 * process after a number of handled requests.
134 size_t max_requests_per_proc;
137 /* config */
140 * host:port
142 * if host is one of the local IP adresses the
143 * whole connection is local
145 * if port is not 0, and host is not specified,
146 * "localhost" (INADDR_LOOPBACK) is assumed.
149 buffer *host;
150 unsigned short port;
151 sa_family_t family;
154 * Unix Domain Socket
156 * instead of TCP/IP we can use Unix Domain Sockets
157 * - more secure (you have fileperms to play with)
158 * - more control (on locally)
159 * - more speed (no extra overhead)
161 buffer *unixsocket;
163 /* if socket is local we can start the fastcgi
164 * process ourself
166 * bin-path is the path to the binary
168 * check min_procs and max_procs for the number
169 * of process to start up
171 buffer *bin_path;
173 /* bin-path is set bin-environment is taken to
174 * create the environement before starting the
175 * FastCGI process
178 array *bin_env;
180 array *bin_env_copy;
183 * docroot-translation between URL->phys and the
184 * remote host
186 * reasons:
187 * - different dir-layout if remote
188 * - chroot if local
191 buffer *docroot;
194 * fastcgi-mode:
195 * - responser
196 * - authorizer
199 unsigned short mode;
202 * check_local tells you if the phys file is stat()ed
203 * or not. FastCGI doesn't care if the service is
204 * remote. If the web-server side doesn't contain
205 * the fastcgi-files we should not stat() for them
206 * and say '404 not found'.
208 unsigned short check_local;
211 * append PATH_INFO to SCRIPT_FILENAME
213 * php needs this if cgi.fix_pathinfo is provided
217 unsigned short break_scriptfilename_for_php;
220 * workaround for program when prefix="/"
222 * rule to build PATH_INFO is hardcoded for when check_local is disabled
223 * enable this option to use the workaround
227 unsigned short fix_root_path_name;
230 * If the backend includes X-Sendfile in the response
231 * we use the value as filename and ignore the content.
234 unsigned short xsendfile_allow;
235 array *xsendfile_docroot;
237 ssize_t load; /* replace by host->load */
239 size_t max_id; /* corresponds most of the time to
240 num_procs.
242 only if a process is killed max_id waits for the process itself
243 to die and decrements it afterwards */
245 buffer *strip_request_uri;
247 unsigned short kill_signal; /* we need a setting for this as libfcgi
248 applications prefer SIGUSR1 while the
249 rest of the world would use SIGTERM
250 *sigh* */
252 int listen_backlog;
253 } fcgi_extension_host;
256 * one extension can have multiple hosts assigned
257 * one host can spawn additional processes on the same
258 * socket (if we control it)
260 * ext -> host -> procs
261 * 1:n 1:n
263 * if the fastcgi process is remote that whole goes down
264 * to
266 * ext -> host -> procs
267 * 1:n 1:1
269 * in case of PHP and FCGI_CHILDREN we have again a procs
270 * but we don't control it directly.
274 typedef struct {
275 buffer *key; /* like .php */
277 int note_is_sent;
278 int last_used_ndx;
280 fcgi_extension_host **hosts;
282 size_t used;
283 size_t size;
284 } fcgi_extension;
286 typedef struct {
287 fcgi_extension **exts;
289 size_t used;
290 size_t size;
291 } fcgi_exts;
294 typedef struct {
295 fcgi_exts *exts;
297 array *ext_mapping;
299 unsigned int debug;
300 } plugin_config;
302 typedef struct {
303 char **ptr;
305 size_t size;
306 size_t used;
307 } char_array;
309 /* generic plugin data, shared between all connections */
310 typedef struct {
311 PLUGIN_DATA;
313 buffer *fcgi_env;
315 buffer *path;
317 buffer *statuskey;
319 plugin_config **config_storage;
321 plugin_config conf; /* this is only used as long as no handler_ctx is setup */
322 } plugin_data;
324 /* connection specific data */
325 typedef enum {
326 FCGI_STATE_INIT,
327 FCGI_STATE_CONNECT_DELAYED,
328 FCGI_STATE_PREPARE_WRITE,
329 FCGI_STATE_WRITE,
330 FCGI_STATE_READ
331 } fcgi_connection_state_t;
333 typedef struct {
334 fcgi_proc *proc;
335 fcgi_extension_host *host;
336 fcgi_extension *ext;
338 fcgi_connection_state_t state;
339 time_t state_timestamp;
341 chunkqueue *rb; /* read queue */
342 chunkqueue *wb; /* write queue */
343 off_t wb_reqlen;
345 buffer *response_header;
347 int fd; /* fd to the fastcgi process */
348 int fde_ndx; /* index into the fd-event buffer */
350 pid_t pid;
351 int got_proc;
352 int reconnects; /* number of reconnect attempts */
354 int request_id;
355 int send_content_body;
357 plugin_config conf;
359 connection *remote_conn; /* dumb pointer */
360 plugin_data *plugin_data; /* dumb pointer */
361 } handler_ctx;
364 /* ok, we need a prototype */
365 static handler_t fcgi_handle_fdevent(server *srv, void *ctx, int revents);
367 static void reset_signals(void) {
368 #ifdef SIGTTOU
369 signal(SIGTTOU, SIG_DFL);
370 #endif
371 #ifdef SIGTTIN
372 signal(SIGTTIN, SIG_DFL);
373 #endif
374 #ifdef SIGTSTP
375 signal(SIGTSTP, SIG_DFL);
376 #endif
377 signal(SIGHUP, SIG_DFL);
378 signal(SIGPIPE, SIG_DFL);
379 signal(SIGUSR1, SIG_DFL);
382 static void fastcgi_status_copy_procname(buffer *b, fcgi_extension_host *host, fcgi_proc *proc) {
383 buffer_copy_string_len(b, CONST_STR_LEN("fastcgi.backend."));
384 buffer_append_string_buffer(b, host->id);
385 if (proc) {
386 buffer_append_string_len(b, CONST_STR_LEN("."));
387 buffer_append_int(b, proc->id);
391 static void fcgi_proc_load_inc(server *srv, handler_ctx *hctx) {
392 plugin_data *p = hctx->plugin_data;
393 hctx->proc->load++;
395 status_counter_inc(srv, CONST_STR_LEN("fastcgi.active-requests"));
397 fastcgi_status_copy_procname(p->statuskey, hctx->host, hctx->proc);
398 buffer_append_string_len(p->statuskey, CONST_STR_LEN(".load"));
400 status_counter_set(srv, CONST_BUF_LEN(p->statuskey), hctx->proc->load);
403 static void fcgi_proc_load_dec(server *srv, handler_ctx *hctx) {
404 plugin_data *p = hctx->plugin_data;
405 hctx->proc->load--;
407 status_counter_dec(srv, CONST_STR_LEN("fastcgi.active-requests"));
409 fastcgi_status_copy_procname(p->statuskey, hctx->host, hctx->proc);
410 buffer_append_string_len(p->statuskey, CONST_STR_LEN(".load"));
412 status_counter_set(srv, CONST_BUF_LEN(p->statuskey), hctx->proc->load);
415 static void fcgi_host_assign(server *srv, handler_ctx *hctx, fcgi_extension_host *host) {
416 plugin_data *p = hctx->plugin_data;
417 hctx->host = host;
418 hctx->host->load++;
420 fastcgi_status_copy_procname(p->statuskey, hctx->host, NULL);
421 buffer_append_string_len(p->statuskey, CONST_STR_LEN(".load"));
423 status_counter_set(srv, CONST_BUF_LEN(p->statuskey), hctx->host->load);
426 static void fcgi_host_reset(server *srv, handler_ctx *hctx) {
427 plugin_data *p = hctx->plugin_data;
428 hctx->host->load--;
430 fastcgi_status_copy_procname(p->statuskey, hctx->host, NULL);
431 buffer_append_string_len(p->statuskey, CONST_STR_LEN(".load"));
433 status_counter_set(srv, CONST_BUF_LEN(p->statuskey), hctx->host->load);
435 hctx->host = NULL;
438 static void fcgi_host_disable(server *srv, handler_ctx *hctx) {
439 plugin_data *p = hctx->plugin_data;
441 if (hctx->host->disable_time || hctx->proc->is_local) {
442 if (hctx->proc->state == PROC_STATE_RUNNING) hctx->host->active_procs--;
443 hctx->proc->disabled_until = srv->cur_ts + hctx->host->disable_time;
444 hctx->proc->state = hctx->proc->is_local ? PROC_STATE_DIED_WAIT_FOR_PID : PROC_STATE_DIED;
446 if (p->conf.debug) {
447 log_error_write(srv, __FILE__, __LINE__, "sds",
448 "backend disabled for", hctx->host->disable_time, "seconds");
453 static int fastcgi_status_init(server *srv, buffer *b, fcgi_extension_host *host, fcgi_proc *proc) {
454 #define CLEAN(x) \
455 fastcgi_status_copy_procname(b, host, proc); \
456 buffer_append_string_len(b, CONST_STR_LEN(x)); \
457 status_counter_set(srv, CONST_BUF_LEN(b), 0);
459 CLEAN(".disabled");
460 CLEAN(".died");
461 CLEAN(".overloaded");
462 CLEAN(".connected");
463 CLEAN(".load");
465 #undef CLEAN
467 #define CLEAN(x) \
468 fastcgi_status_copy_procname(b, host, NULL); \
469 buffer_append_string_len(b, CONST_STR_LEN(x)); \
470 status_counter_set(srv, CONST_BUF_LEN(b), 0);
472 CLEAN(".load");
474 #undef CLEAN
476 return 0;
479 static handler_ctx * handler_ctx_init(void) {
480 handler_ctx * hctx;
482 hctx = calloc(1, sizeof(*hctx));
483 force_assert(hctx);
485 hctx->fde_ndx = -1;
487 hctx->response_header = buffer_init();
489 hctx->request_id = 0;
490 hctx->state = FCGI_STATE_INIT;
491 hctx->proc = NULL;
493 hctx->fd = -1;
495 hctx->reconnects = 0;
496 hctx->send_content_body = 1;
498 hctx->rb = chunkqueue_init();
499 hctx->wb = chunkqueue_init();
500 hctx->wb_reqlen = 0;
502 return hctx;
505 static void handler_ctx_free(server *srv, handler_ctx *hctx) {
506 if (hctx->host) {
507 fcgi_host_reset(srv, hctx);
510 buffer_free(hctx->response_header);
512 chunkqueue_free(hctx->rb);
513 chunkqueue_free(hctx->wb);
515 free(hctx);
518 static fcgi_proc *fastcgi_process_init(void) {
519 fcgi_proc *f;
521 f = calloc(1, sizeof(*f));
522 f->unixsocket = buffer_init();
523 f->connection_name = buffer_init();
525 f->prev = NULL;
526 f->next = NULL;
528 return f;
531 static void fastcgi_process_free(fcgi_proc *f) {
532 if (!f) return;
534 fastcgi_process_free(f->next);
536 buffer_free(f->unixsocket);
537 buffer_free(f->connection_name);
539 free(f);
542 static fcgi_extension_host *fastcgi_host_init(void) {
543 fcgi_extension_host *f;
545 f = calloc(1, sizeof(*f));
547 f->id = buffer_init();
548 f->host = buffer_init();
549 f->unixsocket = buffer_init();
550 f->docroot = buffer_init();
551 f->bin_path = buffer_init();
552 f->bin_env = array_init();
553 f->bin_env_copy = array_init();
554 f->strip_request_uri = buffer_init();
555 f->xsendfile_docroot = array_init();
557 return f;
560 static void fastcgi_host_free(fcgi_extension_host *h) {
561 if (!h) return;
563 buffer_free(h->id);
564 buffer_free(h->host);
565 buffer_free(h->unixsocket);
566 buffer_free(h->docroot);
567 buffer_free(h->bin_path);
568 buffer_free(h->strip_request_uri);
569 array_free(h->bin_env);
570 array_free(h->bin_env_copy);
571 array_free(h->xsendfile_docroot);
573 fastcgi_process_free(h->first);
574 fastcgi_process_free(h->unused_procs);
576 free(h);
580 static fcgi_exts *fastcgi_extensions_init(void) {
581 fcgi_exts *f;
583 f = calloc(1, sizeof(*f));
585 return f;
588 static void fastcgi_extensions_free(fcgi_exts *f) {
589 size_t i;
591 if (!f) return;
593 for (i = 0; i < f->used; i++) {
594 fcgi_extension *fe;
595 size_t j;
597 fe = f->exts[i];
599 for (j = 0; j < fe->used; j++) {
600 fcgi_extension_host *h;
602 h = fe->hosts[j];
604 fastcgi_host_free(h);
607 buffer_free(fe->key);
608 free(fe->hosts);
610 free(fe);
613 free(f->exts);
615 free(f);
618 static int fastcgi_extension_insert(fcgi_exts *ext, buffer *key, fcgi_extension_host *fh) {
619 fcgi_extension *fe;
620 size_t i;
622 /* there is something */
624 for (i = 0; i < ext->used; i++) {
625 if (buffer_is_equal(key, ext->exts[i]->key)) {
626 break;
630 if (i == ext->used) {
631 /* filextension is new */
632 fe = calloc(1, sizeof(*fe));
633 force_assert(fe);
634 fe->key = buffer_init();
635 fe->last_used_ndx = -1;
636 buffer_copy_buffer(fe->key, key);
638 /* */
640 if (ext->size == 0) {
641 ext->size = 8;
642 ext->exts = malloc(ext->size * sizeof(*(ext->exts)));
643 force_assert(ext->exts);
644 } else if (ext->used == ext->size) {
645 ext->size += 8;
646 ext->exts = realloc(ext->exts, ext->size * sizeof(*(ext->exts)));
647 force_assert(ext->exts);
649 ext->exts[ext->used++] = fe;
650 } else {
651 fe = ext->exts[i];
654 if (fe->size == 0) {
655 fe->size = 4;
656 fe->hosts = malloc(fe->size * sizeof(*(fe->hosts)));
657 force_assert(fe->hosts);
658 } else if (fe->size == fe->used) {
659 fe->size += 4;
660 fe->hosts = realloc(fe->hosts, fe->size * sizeof(*(fe->hosts)));
661 force_assert(fe->hosts);
664 fe->hosts[fe->used++] = fh;
666 return 0;
670 INIT_FUNC(mod_fastcgi_init) {
671 plugin_data *p;
673 p = calloc(1, sizeof(*p));
675 p->fcgi_env = buffer_init();
677 p->path = buffer_init();
679 p->statuskey = buffer_init();
681 return p;
685 FREE_FUNC(mod_fastcgi_free) {
686 plugin_data *p = p_d;
688 UNUSED(srv);
690 buffer_free(p->fcgi_env);
691 buffer_free(p->path);
692 buffer_free(p->statuskey);
694 if (p->config_storage) {
695 size_t i, j, n;
696 for (i = 0; i < srv->config_context->used; i++) {
697 plugin_config *s = p->config_storage[i];
698 fcgi_exts *exts;
700 if (NULL == s) continue;
702 exts = s->exts;
704 for (j = 0; j < exts->used; j++) {
705 fcgi_extension *ex;
707 ex = exts->exts[j];
709 for (n = 0; n < ex->used; n++) {
710 fcgi_proc *proc;
711 fcgi_extension_host *host;
713 host = ex->hosts[n];
715 for (proc = host->first; proc; proc = proc->next) {
716 if (proc->pid != 0) {
717 kill(proc->pid, host->kill_signal);
720 if (proc->is_local &&
721 !buffer_string_is_empty(proc->unixsocket)) {
722 unlink(proc->unixsocket->ptr);
726 for (proc = host->unused_procs; proc; proc = proc->next) {
727 if (proc->pid != 0) {
728 kill(proc->pid, host->kill_signal);
730 if (proc->is_local &&
731 !buffer_string_is_empty(proc->unixsocket)) {
732 unlink(proc->unixsocket->ptr);
738 fastcgi_extensions_free(s->exts);
739 array_free(s->ext_mapping);
741 free(s);
743 free(p->config_storage);
746 free(p);
748 return HANDLER_GO_ON;
751 static int env_add(char_array *env, const char *key, size_t key_len, const char *val, size_t val_len) {
752 char *dst;
753 size_t i;
755 if (!key || !val) return -1;
757 dst = malloc(key_len + val_len + 3);
758 memcpy(dst, key, key_len);
759 dst[key_len] = '=';
760 memcpy(dst + key_len + 1, val, val_len);
761 dst[key_len + 1 + val_len] = '\0';
763 for (i = 0; i < env->used; i++) {
764 if (0 == strncmp(dst, env->ptr[i], key_len + 1)) {
765 /* don't care about free as we are in a forked child which is going to exec(...) */
766 /* free(env->ptr[i]); */
767 env->ptr[i] = dst;
768 return 0;
772 if (env->size == 0) {
773 env->size = 16;
774 env->ptr = malloc(env->size * sizeof(*env->ptr));
775 } else if (env->size == env->used + 1) {
776 env->size += 16;
777 env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
780 env->ptr[env->used++] = dst;
782 return 0;
785 static int parse_binpath(char_array *env, buffer *b) {
786 char *start;
787 size_t i;
788 /* search for spaces */
790 start = b->ptr;
791 for (i = 0; i < buffer_string_length(b); i++) {
792 switch(b->ptr[i]) {
793 case ' ':
794 case '\t':
795 /* a WS, stop here and copy the argument */
797 if (env->size == 0) {
798 env->size = 16;
799 env->ptr = malloc(env->size * sizeof(*env->ptr));
800 } else if (env->size == env->used) {
801 env->size += 16;
802 env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
805 b->ptr[i] = '\0';
807 env->ptr[env->used++] = start;
809 start = b->ptr + i + 1;
810 break;
811 default:
812 break;
816 if (env->size == 0) {
817 env->size = 16;
818 env->ptr = malloc(env->size * sizeof(*env->ptr));
819 } else if (env->size == env->used) { /* we need one extra for the terminating NULL */
820 env->size += 16;
821 env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
824 /* the rest */
825 env->ptr[env->used++] = start;
827 if (env->size == 0) {
828 env->size = 16;
829 env->ptr = malloc(env->size * sizeof(*env->ptr));
830 } else if (env->size == env->used) { /* we need one extra for the terminating NULL */
831 env->size += 16;
832 env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
835 /* terminate */
836 env->ptr[env->used++] = NULL;
838 return 0;
841 #if !defined(HAVE_FORK)
842 static int fcgi_spawn_connection(server *srv,
843 plugin_data *p,
844 fcgi_extension_host *host,
845 fcgi_proc *proc) {
846 UNUSED(srv);
847 UNUSED(p);
848 UNUSED(host);
849 UNUSED(proc);
850 return -1;
853 #else /* -> defined(HAVE_FORK) */
855 static int fcgi_spawn_connection(server *srv,
856 plugin_data *p,
857 fcgi_extension_host *host,
858 fcgi_proc *proc) {
859 int fcgi_fd;
860 int status;
861 struct timeval tv = { 0, 100 * 1000 };
862 #ifdef HAVE_SYS_UN_H
863 struct sockaddr_un fcgi_addr_un;
864 #endif
865 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
866 struct sockaddr_in6 fcgi_addr_in6;
867 #endif
868 struct sockaddr_in fcgi_addr_in;
869 struct sockaddr *fcgi_addr;
871 socklen_t servlen;
873 if (p->conf.debug) {
874 log_error_write(srv, __FILE__, __LINE__, "sdb",
875 "new proc, socket:", proc->port, proc->unixsocket);
878 if (!buffer_string_is_empty(proc->unixsocket)) {
879 #ifdef HAVE_SYS_UN_H
880 memset(&fcgi_addr_un, 0, sizeof(fcgi_addr_un));
881 fcgi_addr_un.sun_family = AF_UNIX;
882 if (buffer_string_length(proc->unixsocket) + 1 > sizeof(fcgi_addr_un.sun_path)) {
883 log_error_write(srv, __FILE__, __LINE__, "sB",
884 "ERROR: Unix Domain socket filename too long:",
885 proc->unixsocket);
886 return -1;
888 memcpy(fcgi_addr_un.sun_path, proc->unixsocket->ptr, buffer_string_length(proc->unixsocket) + 1);
890 #ifdef SUN_LEN
891 servlen = SUN_LEN(&fcgi_addr_un);
892 #else
893 /* stevens says: */
894 servlen = buffer_string_length(proc->unixsocket) + 1 + sizeof(fcgi_addr_un.sun_family);
895 #endif
896 fcgi_addr = (struct sockaddr *) &fcgi_addr_un;
898 buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("unix:"));
899 buffer_append_string_buffer(proc->connection_name, proc->unixsocket);
901 #else
902 log_error_write(srv, __FILE__, __LINE__, "s",
903 "ERROR: Unix Domain sockets are not supported.");
904 return -1;
905 #endif
906 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
907 } else if (host->family == AF_INET6 && !buffer_string_is_empty(host->host)) {
908 memset(&fcgi_addr_in6, 0, sizeof(fcgi_addr_in6));
909 fcgi_addr_in6.sin6_family = AF_INET6;
910 inet_pton(AF_INET6, host->host->ptr, (char *) &fcgi_addr_in6.sin6_addr);
911 fcgi_addr_in6.sin6_port = htons(proc->port);
912 servlen = sizeof(fcgi_addr_in6);
913 fcgi_addr = (struct sockaddr *) &fcgi_addr_in6;
914 #endif
915 } else {
916 memset(&fcgi_addr_in, 0, sizeof(fcgi_addr_in));
917 fcgi_addr_in.sin_family = AF_INET;
919 if (buffer_string_is_empty(host->host)) {
920 fcgi_addr_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
921 } else {
922 struct hostent *he;
924 /* set a useful default */
925 fcgi_addr_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
928 if (NULL == (he = gethostbyname(host->host->ptr))) {
929 log_error_write(srv, __FILE__, __LINE__,
930 "sdb", "gethostbyname failed: ",
931 h_errno, host->host);
932 return -1;
935 if (he->h_addrtype != AF_INET) {
936 log_error_write(srv, __FILE__, __LINE__, "sd", "addr-type != AF_INET: ", he->h_addrtype);
937 return -1;
940 if (he->h_length != sizeof(struct in_addr)) {
941 log_error_write(srv, __FILE__, __LINE__, "sd", "addr-length != sizeof(in_addr): ", he->h_length);
942 return -1;
945 memcpy(&(fcgi_addr_in.sin_addr.s_addr), he->h_addr_list[0], he->h_length);
948 fcgi_addr_in.sin_port = htons(proc->port);
949 servlen = sizeof(fcgi_addr_in);
951 fcgi_addr = (struct sockaddr *) &fcgi_addr_in;
954 if (buffer_string_is_empty(proc->unixsocket)) {
955 buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("tcp:"));
956 if (!buffer_string_is_empty(host->host)) {
957 buffer_append_string_buffer(proc->connection_name, host->host);
958 } else {
959 buffer_append_string_len(proc->connection_name, CONST_STR_LEN("localhost"));
961 buffer_append_string_len(proc->connection_name, CONST_STR_LEN(":"));
962 buffer_append_int(proc->connection_name, proc->port);
965 if (-1 == (fcgi_fd = socket(fcgi_addr->sa_family, SOCK_STREAM, 0))) {
966 log_error_write(srv, __FILE__, __LINE__, "ss",
967 "failed:", strerror(errno));
968 return -1;
971 if (-1 == connect(fcgi_fd, fcgi_addr, servlen)) {
972 /* server is not up, spawn it */
973 pid_t child;
974 int val;
976 if (errno != ENOENT &&
977 !buffer_string_is_empty(proc->unixsocket)) {
978 unlink(proc->unixsocket->ptr);
981 close(fcgi_fd);
983 /* reopen socket */
984 if (-1 == (fcgi_fd = socket(fcgi_addr->sa_family, SOCK_STREAM, 0))) {
985 log_error_write(srv, __FILE__, __LINE__, "ss",
986 "socket failed:", strerror(errno));
987 return -1;
990 val = 1;
991 if (setsockopt(fcgi_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
992 log_error_write(srv, __FILE__, __LINE__, "ss",
993 "socketsockopt failed:", strerror(errno));
994 close(fcgi_fd);
995 return -1;
998 /* create socket */
999 if (-1 == bind(fcgi_fd, fcgi_addr, servlen)) {
1000 log_error_write(srv, __FILE__, __LINE__, "sbs",
1001 "bind failed for:",
1002 proc->connection_name,
1003 strerror(errno));
1004 close(fcgi_fd);
1005 return -1;
1008 if (-1 == listen(fcgi_fd, host->listen_backlog)) {
1009 log_error_write(srv, __FILE__, __LINE__, "ss",
1010 "listen failed:", strerror(errno));
1011 close(fcgi_fd);
1012 return -1;
1015 switch ((child = fork())) {
1016 case 0: {
1017 size_t i = 0;
1018 char *c;
1019 char_array env;
1020 char_array arg;
1022 /* create environment */
1023 env.ptr = NULL;
1024 env.size = 0;
1025 env.used = 0;
1027 arg.ptr = NULL;
1028 arg.size = 0;
1029 arg.used = 0;
1031 if(fcgi_fd != FCGI_LISTENSOCK_FILENO) {
1032 close(FCGI_LISTENSOCK_FILENO);
1033 dup2(fcgi_fd, FCGI_LISTENSOCK_FILENO);
1034 close(fcgi_fd);
1037 /* we don't need the client socket */
1038 for (i = 3; i < 256; i++) {
1039 close(i);
1042 /* build clean environment */
1043 if (host->bin_env_copy->used) {
1044 for (i = 0; i < host->bin_env_copy->used; i++) {
1045 data_string *ds = (data_string *)host->bin_env_copy->data[i];
1046 char *ge;
1048 if (NULL != (ge = getenv(ds->value->ptr))) {
1049 env_add(&env, CONST_BUF_LEN(ds->value), ge, strlen(ge));
1052 } else {
1053 char ** const e = environ;
1054 for (i = 0; e[i]; ++i) {
1055 char *eq;
1057 if (NULL != (eq = strchr(e[i], '='))) {
1058 env_add(&env, e[i], eq - e[i], eq+1, strlen(eq+1));
1063 /* create environment */
1064 for (i = 0; i < host->bin_env->used; i++) {
1065 data_string *ds = (data_string *)host->bin_env->data[i];
1067 env_add(&env, CONST_BUF_LEN(ds->key), CONST_BUF_LEN(ds->value));
1070 for (i = 0; i < env.used; i++) {
1071 /* search for PHP_FCGI_CHILDREN */
1072 if (0 == strncmp(env.ptr[i], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
1075 /* not found, add a default */
1076 if (i == env.used) {
1077 env_add(&env, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
1080 env.ptr[env.used] = NULL;
1082 parse_binpath(&arg, host->bin_path);
1084 /* chdir into the base of the bin-path,
1085 * search for the last / */
1086 if (NULL != (c = strrchr(arg.ptr[0], '/'))) {
1087 *c = '\0';
1089 /* change to the physical directory */
1090 if (-1 == chdir(arg.ptr[0])) {
1091 *c = '/';
1092 log_error_write(srv, __FILE__, __LINE__, "sss", "chdir failed:", strerror(errno), arg.ptr[0]);
1094 *c = '/';
1097 reset_signals();
1099 /* exec the cgi */
1100 execve(arg.ptr[0], arg.ptr, env.ptr);
1102 /* log_error_write(srv, __FILE__, __LINE__, "sbs",
1103 "execve failed for:", host->bin_path, strerror(errno)); */
1105 _exit(errno);
1107 break;
1109 case -1:
1110 /* error */
1111 close(fcgi_fd);
1112 break;
1113 default:
1114 /* father */
1115 close(fcgi_fd);
1117 /* wait */
1118 select(0, NULL, NULL, NULL, &tv);
1120 switch (waitpid(child, &status, WNOHANG)) {
1121 case 0:
1122 /* child still running after timeout, good */
1123 break;
1124 case -1:
1125 /* no PID found ? should never happen */
1126 log_error_write(srv, __FILE__, __LINE__, "ss",
1127 "pid not found:", strerror(errno));
1128 return -1;
1129 default:
1130 log_error_write(srv, __FILE__, __LINE__, "sbs",
1131 "the fastcgi-backend", host->bin_path, "failed to start:");
1132 /* the child should not terminate at all */
1133 if (WIFEXITED(status)) {
1134 log_error_write(srv, __FILE__, __LINE__, "sdb",
1135 "child exited with status",
1136 WEXITSTATUS(status), host->bin_path);
1137 log_error_write(srv, __FILE__, __LINE__, "s",
1138 "If you're trying to run your app as a FastCGI backend, make sure you're using the FastCGI-enabled version.\n"
1139 "If this is PHP on Gentoo, add 'fastcgi' to the USE flags.");
1140 } else if (WIFSIGNALED(status)) {
1141 log_error_write(srv, __FILE__, __LINE__, "sd",
1142 "terminated by signal:",
1143 WTERMSIG(status));
1145 if (WTERMSIG(status) == 11) {
1146 log_error_write(srv, __FILE__, __LINE__, "s",
1147 "to be exact: it segfaulted, crashed, died, ... you get the idea." );
1148 log_error_write(srv, __FILE__, __LINE__, "s",
1149 "If this is PHP, try removing the bytecode caches for now and try again.");
1151 } else {
1152 log_error_write(srv, __FILE__, __LINE__, "sd",
1153 "child died somehow:",
1154 status);
1156 return -1;
1159 /* register process */
1160 proc->pid = child;
1161 proc->is_local = 1;
1163 break;
1165 } else {
1166 close(fcgi_fd);
1167 proc->is_local = 0;
1168 proc->pid = 0;
1170 if (p->conf.debug) {
1171 log_error_write(srv, __FILE__, __LINE__, "sb",
1172 "(debug) socket is already used; won't spawn:",
1173 proc->connection_name);
1177 proc->state = PROC_STATE_RUNNING;
1178 host->active_procs++;
1180 return 0;
1183 #endif /* HAVE_FORK */
1185 static int unixsocket_is_dup(plugin_data *p, size_t used, buffer *unixsocket) {
1186 size_t i, j, n;
1187 for (i = 0; i < used; ++i) {
1188 fcgi_exts *exts = p->config_storage[i]->exts;
1189 for (j = 0; j < exts->used; ++j) {
1190 fcgi_extension *ex = exts->exts[j];
1191 for (n = 0; n < ex->used; ++n) {
1192 fcgi_extension_host *host = ex->hosts[n];
1193 if (!buffer_string_is_empty(host->unixsocket)
1194 && buffer_is_equal(host->unixsocket, unixsocket))
1195 return 1;
1200 return 0;
1203 SETDEFAULTS_FUNC(mod_fastcgi_set_defaults) {
1204 plugin_data *p = p_d;
1205 data_unset *du;
1206 size_t i = 0;
1207 buffer *fcgi_mode = buffer_init();
1208 fcgi_extension_host *host = NULL;
1210 config_values_t cv[] = {
1211 { "fastcgi.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
1212 { "fastcgi.debug", NULL, T_CONFIG_INT , T_CONFIG_SCOPE_CONNECTION }, /* 1 */
1213 { "fastcgi.map-extensions", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
1214 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
1217 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
1219 for (i = 0; i < srv->config_context->used; i++) {
1220 data_config const* config = (data_config const*)srv->config_context->data[i];
1221 plugin_config *s;
1223 s = malloc(sizeof(plugin_config));
1224 s->exts = fastcgi_extensions_init();
1225 s->debug = 0;
1226 s->ext_mapping = array_init();
1228 cv[0].destination = s->exts;
1229 cv[1].destination = &(s->debug);
1230 cv[2].destination = s->ext_mapping;
1232 p->config_storage[i] = s;
1234 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
1235 goto error;
1239 * <key> = ( ... )
1242 if (NULL != (du = array_get_element(config->value, "fastcgi.server"))) {
1243 size_t j;
1244 data_array *da = (data_array *)du;
1246 if (du->type != TYPE_ARRAY) {
1247 log_error_write(srv, __FILE__, __LINE__, "sss",
1248 "unexpected type for key: ", "fastcgi.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1250 goto error;
1255 * fastcgi.server = ( "<ext>" => ( ... ),
1256 * "<ext>" => ( ... ) )
1259 for (j = 0; j < da->value->used; j++) {
1260 size_t n;
1261 data_array *da_ext = (data_array *)da->value->data[j];
1263 if (da->value->data[j]->type != TYPE_ARRAY) {
1264 log_error_write(srv, __FILE__, __LINE__, "sssbs",
1265 "unexpected type for key: ", "fastcgi.server",
1266 "[", da->value->data[j]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1268 goto error;
1272 * da_ext->key == name of the extension
1276 * fastcgi.server = ( "<ext>" =>
1277 * ( "<host>" => ( ... ),
1278 * "<host>" => ( ... )
1279 * ),
1280 * "<ext>" => ... )
1283 for (n = 0; n < da_ext->value->used; n++) {
1284 data_array *da_host = (data_array *)da_ext->value->data[n];
1286 config_values_t fcv[] = {
1287 { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
1288 { "docroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
1289 { "mode", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
1290 { "socket", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
1291 { "bin-path", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
1293 { "check-local", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
1294 { "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 6 */
1295 { "max-procs", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
1296 { "disable-time", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
1298 { "bin-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
1299 { "bin-copy-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
1301 { "broken-scriptfilename", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
1302 { "allow-x-send-file", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
1303 { "strip-request-uri", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
1304 { "kill-signal", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
1305 { "fix-root-scriptname", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
1306 { "listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
1307 { "x-sendfile", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 17 */
1308 { "x-sendfile-docroot",NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 18 */
1310 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
1313 if (da_host->type != TYPE_ARRAY) {
1314 log_error_write(srv, __FILE__, __LINE__, "ssSBS",
1315 "unexpected type for key:",
1316 "fastcgi.server",
1317 "[", da_host->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1319 goto error;
1322 host = fastcgi_host_init();
1323 buffer_reset(fcgi_mode);
1325 buffer_copy_buffer(host->id, da_host->key);
1327 host->check_local = 1;
1328 host->max_procs = 4;
1329 host->mode = FCGI_RESPONDER;
1330 host->disable_time = 1;
1331 host->break_scriptfilename_for_php = 0;
1332 host->xsendfile_allow = 0;
1333 host->kill_signal = SIGTERM;
1334 host->fix_root_path_name = 0;
1335 host->listen_backlog = 1024;
1337 fcv[0].destination = host->host;
1338 fcv[1].destination = host->docroot;
1339 fcv[2].destination = fcgi_mode;
1340 fcv[3].destination = host->unixsocket;
1341 fcv[4].destination = host->bin_path;
1343 fcv[5].destination = &(host->check_local);
1344 fcv[6].destination = &(host->port);
1345 fcv[7].destination = &(host->max_procs);
1346 fcv[8].destination = &(host->disable_time);
1348 fcv[9].destination = host->bin_env;
1349 fcv[10].destination = host->bin_env_copy;
1350 fcv[11].destination = &(host->break_scriptfilename_for_php);
1351 fcv[12].destination = &(host->xsendfile_allow);
1352 fcv[13].destination = host->strip_request_uri;
1353 fcv[14].destination = &(host->kill_signal);
1354 fcv[15].destination = &(host->fix_root_path_name);
1355 fcv[16].destination = &(host->listen_backlog);
1356 fcv[17].destination = &(host->xsendfile_allow);
1357 fcv[18].destination = host->xsendfile_docroot;
1359 if (0 != config_insert_values_internal(srv, da_host->value, fcv, T_CONFIG_SCOPE_CONNECTION)) {
1360 goto error;
1363 if ((!buffer_string_is_empty(host->host) || host->port) &&
1364 !buffer_string_is_empty(host->unixsocket)) {
1365 log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
1366 "either host/port or socket have to be set in:",
1367 da->key, "= (",
1368 da_ext->key, " => (",
1369 da_host->key, " ( ...");
1371 goto error;
1374 if (!buffer_string_is_empty(host->unixsocket)) {
1375 /* unix domain socket */
1376 struct sockaddr_un un;
1378 if (buffer_string_length(host->unixsocket) + 1 > sizeof(un.sun_path) - 2) {
1379 log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
1380 "unixsocket is too long in:",
1381 da->key, "= (",
1382 da_ext->key, " => (",
1383 da_host->key, " ( ...");
1385 goto error;
1388 if (!buffer_string_is_empty(host->bin_path)
1389 && unixsocket_is_dup(p, i+1, host->unixsocket)) {
1390 log_error_write(srv, __FILE__, __LINE__, "sb",
1391 "duplicate unixsocket path:",
1392 host->unixsocket);
1393 goto error;
1396 host->family = AF_UNIX;
1397 } else {
1398 /* tcp/ip */
1400 if (buffer_string_is_empty(host->host) &&
1401 buffer_string_is_empty(host->bin_path)) {
1402 log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
1403 "host or binpath have to be set in:",
1404 da->key, "= (",
1405 da_ext->key, " => (",
1406 da_host->key, " ( ...");
1408 goto error;
1409 } else if (host->port == 0) {
1410 log_error_write(srv, __FILE__, __LINE__, "sbsbsbs",
1411 "port has to be set in:",
1412 da->key, "= (",
1413 da_ext->key, " => (",
1414 da_host->key, " ( ...");
1416 goto error;
1419 host->family = (!buffer_string_is_empty(host->host) && NULL != strchr(host->host->ptr, ':')) ? AF_INET6 : AF_INET;
1422 if (!buffer_string_is_empty(host->bin_path)) {
1423 /* a local socket + self spawning */
1424 size_t pno;
1426 if (s->debug) {
1427 log_error_write(srv, __FILE__, __LINE__, "ssbsdsbsd",
1428 "--- fastcgi spawning local",
1429 "\n\tproc:", host->bin_path,
1430 "\n\tport:", host->port,
1431 "\n\tsocket", host->unixsocket,
1432 "\n\tmax-procs:", host->max_procs);
1435 for (pno = 0; pno < host->max_procs; pno++) {
1436 fcgi_proc *proc;
1438 proc = fastcgi_process_init();
1439 proc->id = host->num_procs++;
1440 host->max_id++;
1442 if (buffer_string_is_empty(host->unixsocket)) {
1443 proc->port = host->port + pno;
1444 } else {
1445 buffer_copy_buffer(proc->unixsocket, host->unixsocket);
1446 buffer_append_string_len(proc->unixsocket, CONST_STR_LEN("-"));
1447 buffer_append_int(proc->unixsocket, pno);
1450 if (s->debug) {
1451 log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
1452 "--- fastcgi spawning",
1453 "\n\tport:", host->port,
1454 "\n\tsocket", host->unixsocket,
1455 "\n\tcurrent:", pno, "/", host->max_procs);
1458 if (!srv->srvconf.preflight_check
1459 && fcgi_spawn_connection(srv, p, host, proc)) {
1460 log_error_write(srv, __FILE__, __LINE__, "s",
1461 "[ERROR]: spawning fcgi failed.");
1462 fastcgi_process_free(proc);
1463 goto error;
1466 fastcgi_status_init(srv, p->statuskey, host, proc);
1468 proc->next = host->first;
1469 if (host->first) host->first->prev = proc;
1471 host->first = proc;
1473 } else {
1474 fcgi_proc *proc;
1476 proc = fastcgi_process_init();
1477 proc->id = host->num_procs++;
1478 host->max_id++;
1479 host->active_procs++;
1480 proc->state = PROC_STATE_RUNNING;
1482 if (buffer_string_is_empty(host->unixsocket)) {
1483 proc->port = host->port;
1484 } else {
1485 buffer_copy_buffer(proc->unixsocket, host->unixsocket);
1488 fastcgi_status_init(srv, p->statuskey, host, proc);
1490 host->first = proc;
1492 host->max_procs = 1;
1495 if (!buffer_string_is_empty(fcgi_mode)) {
1496 if (strcmp(fcgi_mode->ptr, "responder") == 0) {
1497 host->mode = FCGI_RESPONDER;
1498 } else if (strcmp(fcgi_mode->ptr, "authorizer") == 0) {
1499 host->mode = FCGI_AUTHORIZER;
1500 if (buffer_string_is_empty(host->docroot)) {
1501 log_error_write(srv, __FILE__, __LINE__, "s",
1502 "ERROR: docroot is required for authorizer mode.");
1503 goto error;
1505 } else {
1506 log_error_write(srv, __FILE__, __LINE__, "sbs",
1507 "WARNING: unknown fastcgi mode:",
1508 fcgi_mode, "(ignored, mode set to responder)");
1512 if (host->xsendfile_docroot->used) {
1513 size_t k;
1514 for (k = 0; k < host->xsendfile_docroot->used; ++k) {
1515 data_string *ds = (data_string *)host->xsendfile_docroot->data[k];
1516 if (ds->type != TYPE_STRING) {
1517 log_error_write(srv, __FILE__, __LINE__, "s",
1518 "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
1519 goto error;
1521 if (ds->value->ptr[0] != '/') {
1522 log_error_write(srv, __FILE__, __LINE__, "SBs",
1523 "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds->value, "\"");
1524 goto error;
1526 buffer_path_simplify(ds->value, ds->value);
1527 buffer_append_slash(ds->value);
1531 /* if extension already exists, take it */
1532 fastcgi_extension_insert(s->exts, da_ext->key, host);
1533 host = NULL;
1539 buffer_free(fcgi_mode);
1540 return HANDLER_GO_ON;
1542 error:
1543 if (NULL != host) fastcgi_host_free(host);
1544 buffer_free(fcgi_mode);
1545 return HANDLER_ERROR;
1548 static int fcgi_set_state(server *srv, handler_ctx *hctx, fcgi_connection_state_t state) {
1549 hctx->state = state;
1550 hctx->state_timestamp = srv->cur_ts;
1552 return 0;
1556 static void fcgi_connection_close(server *srv, handler_ctx *hctx) {
1557 plugin_data *p;
1558 connection *con;
1560 p = hctx->plugin_data;
1561 con = hctx->remote_conn;
1563 if (hctx->fd != -1) {
1564 fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
1565 fdevent_unregister(srv->ev, hctx->fd);
1566 close(hctx->fd);
1567 srv->cur_fds--;
1570 if (hctx->host && hctx->proc) {
1571 if (hctx->got_proc) {
1572 /* after the connect the process gets a load */
1573 fcgi_proc_load_dec(srv, hctx);
1575 if (p->conf.debug) {
1576 log_error_write(srv, __FILE__, __LINE__, "ssdsbsd",
1577 "released proc:",
1578 "pid:", hctx->proc->pid,
1579 "socket:", hctx->proc->connection_name,
1580 "load:", hctx->proc->load);
1586 handler_ctx_free(srv, hctx);
1587 con->plugin_ctx[p->id] = NULL;
1589 /* finish response (if not already con->file_started, con->file_finished) */
1590 if (con->mode == p->id) {
1591 http_response_backend_done(srv, con);
1595 static int fcgi_reconnect(server *srv, handler_ctx *hctx) {
1596 plugin_data *p = hctx->plugin_data;
1598 /* child died
1600 * 1.
1602 * connect was ok, connection was accepted
1603 * but the php accept loop checks after the accept if it should die or not.
1605 * if yes we can only detect it at a write()
1607 * next step is resetting this attemp and setup a connection again
1609 * if we have more than 5 reconnects for the same request, die
1611 * 2.
1613 * we have a connection but the child died by some other reason
1617 if (hctx->fd != -1) {
1618 fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
1619 fdevent_unregister(srv->ev, hctx->fd);
1620 close(hctx->fd);
1621 srv->cur_fds--;
1622 hctx->fd = -1;
1625 fcgi_set_state(srv, hctx, FCGI_STATE_INIT);
1627 hctx->request_id = 0;
1628 hctx->reconnects++;
1630 if (p->conf.debug > 2) {
1631 if (hctx->proc) {
1632 log_error_write(srv, __FILE__, __LINE__, "sdb",
1633 "release proc for reconnect:",
1634 hctx->proc->pid, hctx->proc->connection_name);
1635 } else {
1636 log_error_write(srv, __FILE__, __LINE__, "sb",
1637 "release proc for reconnect:",
1638 hctx->host->unixsocket);
1642 if (hctx->proc && hctx->got_proc) {
1643 fcgi_proc_load_dec(srv, hctx);
1646 /* perhaps another host gives us more luck */
1647 fcgi_host_reset(srv, hctx);
1649 return 0;
1653 static handler_t fcgi_connection_reset(server *srv, connection *con, void *p_d) {
1654 plugin_data *p = p_d;
1655 handler_ctx *hctx = con->plugin_ctx[p->id];
1656 if (hctx) fcgi_connection_close(srv, hctx);
1658 return HANDLER_GO_ON;
1662 static int fcgi_env_add(buffer *env, const char *key, size_t key_len, const char *val, size_t val_len) {
1663 size_t len;
1664 char len_enc[8];
1665 size_t len_enc_len = 0;
1667 if (!key || !val) return -1;
1669 len = key_len + val_len;
1671 len += key_len > 127 ? 4 : 1;
1672 len += val_len > 127 ? 4 : 1;
1674 if (buffer_string_length(env) + len >= FCGI_MAX_LENGTH) {
1676 * we can't append more headers, ignore it
1678 return -1;
1682 * field length can be 31bit max
1684 * HINT: this can't happen as FCGI_MAX_LENGTH is only 16bit
1686 force_assert(key_len < 0x7fffffffu);
1687 force_assert(val_len < 0x7fffffffu);
1689 buffer_string_prepare_append(env, len);
1691 if (key_len > 127) {
1692 len_enc[len_enc_len++] = ((key_len >> 24) & 0xff) | 0x80;
1693 len_enc[len_enc_len++] = (key_len >> 16) & 0xff;
1694 len_enc[len_enc_len++] = (key_len >> 8) & 0xff;
1695 len_enc[len_enc_len++] = (key_len >> 0) & 0xff;
1696 } else {
1697 len_enc[len_enc_len++] = (key_len >> 0) & 0xff;
1700 if (val_len > 127) {
1701 len_enc[len_enc_len++] = ((val_len >> 24) & 0xff) | 0x80;
1702 len_enc[len_enc_len++] = (val_len >> 16) & 0xff;
1703 len_enc[len_enc_len++] = (val_len >> 8) & 0xff;
1704 len_enc[len_enc_len++] = (val_len >> 0) & 0xff;
1705 } else {
1706 len_enc[len_enc_len++] = (val_len >> 0) & 0xff;
1709 buffer_append_string_len(env, len_enc, len_enc_len);
1710 buffer_append_string_len(env, key, key_len);
1711 buffer_append_string_len(env, val, val_len);
1713 return 0;
1716 static int fcgi_header(FCGI_Header * header, unsigned char type, int request_id, int contentLength, unsigned char paddingLength) {
1717 force_assert(contentLength <= FCGI_MAX_LENGTH);
1719 header->version = FCGI_VERSION_1;
1720 header->type = type;
1721 header->requestIdB0 = request_id & 0xff;
1722 header->requestIdB1 = (request_id >> 8) & 0xff;
1723 header->contentLengthB0 = contentLength & 0xff;
1724 header->contentLengthB1 = (contentLength >> 8) & 0xff;
1725 header->paddingLength = paddingLength;
1726 header->reserved = 0;
1728 return 0;
1731 typedef enum {
1732 CONNECTION_OK,
1733 CONNECTION_DELAYED, /* retry after event, take same host */
1734 CONNECTION_OVERLOADED, /* disable for 1 second, take another backend */
1735 CONNECTION_DEAD /* disable for 60 seconds, take another backend */
1736 } connection_result_t;
1738 static connection_result_t fcgi_establish_connection(server *srv, handler_ctx *hctx) {
1739 struct sockaddr *fcgi_addr;
1740 struct sockaddr_in fcgi_addr_in;
1741 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1742 struct sockaddr_in6 fcgi_addr_in6;
1743 #endif
1744 #ifdef HAVE_SYS_UN_H
1745 struct sockaddr_un fcgi_addr_un;
1746 #endif
1747 socklen_t servlen;
1749 fcgi_extension_host *host = hctx->host;
1750 fcgi_proc *proc = hctx->proc;
1751 int fcgi_fd = hctx->fd;
1753 if (!buffer_string_is_empty(proc->unixsocket)) {
1754 #ifdef HAVE_SYS_UN_H
1755 /* use the unix domain socket */
1756 memset(&fcgi_addr_un, 0, sizeof(fcgi_addr_un));
1757 fcgi_addr_un.sun_family = AF_UNIX;
1758 if (buffer_string_length(proc->unixsocket) + 1 > sizeof(fcgi_addr_un.sun_path)) {
1759 log_error_write(srv, __FILE__, __LINE__, "sB",
1760 "ERROR: Unix Domain socket filename too long:",
1761 proc->unixsocket);
1762 return -1;
1764 memcpy(fcgi_addr_un.sun_path, proc->unixsocket->ptr, buffer_string_length(proc->unixsocket) + 1);
1766 #ifdef SUN_LEN
1767 servlen = SUN_LEN(&fcgi_addr_un);
1768 #else
1769 /* stevens says: */
1770 servlen = buffer_string_length(proc->unixsocket) + 1 + sizeof(fcgi_addr_un.sun_family);
1771 #endif
1772 fcgi_addr = (struct sockaddr *) &fcgi_addr_un;
1774 if (buffer_string_is_empty(proc->connection_name)) {
1775 /* on remote spawing we have to set the connection-name now */
1776 buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("unix:"));
1777 buffer_append_string_buffer(proc->connection_name, proc->unixsocket);
1779 #else
1780 return CONNECTION_DEAD;
1781 #endif
1782 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1783 } else if (host->family == AF_INET6 && !buffer_string_is_empty(host->host)) {
1784 memset(&fcgi_addr_in6, 0, sizeof(fcgi_addr_in6));
1785 fcgi_addr_in6.sin6_family = AF_INET6;
1786 inet_pton(AF_INET6, host->host->ptr, (char *) &fcgi_addr_in6.sin6_addr);
1787 fcgi_addr_in6.sin6_port = htons(proc->port);
1788 servlen = sizeof(fcgi_addr_in6);
1789 fcgi_addr = (struct sockaddr *) &fcgi_addr_in6;
1790 #endif
1791 } else {
1792 memset(&fcgi_addr_in, 0, sizeof(fcgi_addr_in));
1793 fcgi_addr_in.sin_family = AF_INET;
1794 if (!buffer_string_is_empty(host->host)) {
1795 if (0 == inet_aton(host->host->ptr, &(fcgi_addr_in.sin_addr))) {
1796 log_error_write(srv, __FILE__, __LINE__, "sbs",
1797 "converting IP address failed for", host->host,
1798 "\nBe sure to specify an IP address here");
1800 return CONNECTION_DEAD;
1802 } else {
1803 fcgi_addr_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1805 fcgi_addr_in.sin_port = htons(proc->port);
1806 servlen = sizeof(fcgi_addr_in);
1808 fcgi_addr = (struct sockaddr *) &fcgi_addr_in;
1811 if (buffer_string_is_empty(proc->unixsocket)) {
1812 if (buffer_string_is_empty(proc->connection_name)) {
1813 /* on remote spawing we have to set the connection-name now */
1814 buffer_copy_string_len(proc->connection_name, CONST_STR_LEN("tcp:"));
1815 if (!buffer_string_is_empty(host->host)) {
1816 buffer_append_string_buffer(proc->connection_name, host->host);
1817 } else {
1818 buffer_append_string_len(proc->connection_name, CONST_STR_LEN("localhost"));
1820 buffer_append_string_len(proc->connection_name, CONST_STR_LEN(":"));
1821 buffer_append_int(proc->connection_name, proc->port);
1825 if (-1 == connect(fcgi_fd, fcgi_addr, servlen)) {
1826 if (errno == EINPROGRESS ||
1827 errno == EALREADY ||
1828 errno == EINTR) {
1829 if (hctx->conf.debug > 2) {
1830 log_error_write(srv, __FILE__, __LINE__, "sb",
1831 "connect delayed; will continue later:", proc->connection_name);
1834 return CONNECTION_DELAYED;
1835 } else if (errno == EAGAIN) {
1836 if (hctx->conf.debug) {
1837 log_error_write(srv, __FILE__, __LINE__, "sbsd",
1838 "This means that you have more incoming requests than your FastCGI backend can handle in parallel."
1839 "It might help to spawn more FastCGI backends or PHP children; if not, decrease server.max-connections."
1840 "The load for this FastCGI backend", proc->connection_name, "is", proc->load);
1843 return CONNECTION_OVERLOADED;
1844 } else {
1845 log_error_write(srv, __FILE__, __LINE__, "sssb",
1846 "connect failed:",
1847 strerror(errno), "on",
1848 proc->connection_name);
1850 return CONNECTION_DEAD;
1854 hctx->reconnects = 0;
1855 if (hctx->conf.debug > 1) {
1856 log_error_write(srv, __FILE__, __LINE__, "sd",
1857 "connect succeeded: ", fcgi_fd);
1860 return CONNECTION_OK;
1863 #define FCGI_ENV_ADD_CHECK(ret, con) \
1864 if (ret == -1) { \
1865 con->http_status = 400; \
1866 return -1; \
1868 static int fcgi_env_add_request_headers(server *srv, connection *con, plugin_data *p) {
1869 size_t i;
1871 for (i = 0; i < con->request.headers->used; i++) {
1872 data_string *ds;
1874 ds = (data_string *)con->request.headers->data[i];
1876 if (!buffer_is_empty(ds->value) && !buffer_is_empty(ds->key)) {
1877 buffer_copy_string_encoded_cgi_varnames(srv->tmp_buf, CONST_BUF_LEN(ds->key), 1);
1879 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_BUF_LEN(srv->tmp_buf), CONST_BUF_LEN(ds->value)),con);
1883 for (i = 0; i < con->environment->used; i++) {
1884 data_string *ds;
1886 ds = (data_string *)con->environment->data[i];
1888 if (!buffer_is_empty(ds->value) && !buffer_is_empty(ds->key)) {
1889 buffer_copy_string_encoded_cgi_varnames(srv->tmp_buf, CONST_BUF_LEN(ds->key), 0);
1891 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_BUF_LEN(srv->tmp_buf), CONST_BUF_LEN(ds->value)), con);
1895 return 0;
1898 static void fcgi_stdin_append(server *srv, connection *con, handler_ctx *hctx, int request_id) {
1899 FCGI_Header header;
1900 chunkqueue *req_cq = con->request_content_queue;
1901 plugin_data *p = hctx->plugin_data;
1902 off_t offset, weWant;
1903 const off_t req_cqlen = req_cq->bytes_in - req_cq->bytes_out;
1905 /* something to send ? */
1906 for (offset = 0; offset != req_cqlen; offset += weWant) {
1907 weWant = req_cqlen - offset > FCGI_MAX_LENGTH ? FCGI_MAX_LENGTH : req_cqlen - offset;
1909 /* we announce toWrite octets
1910 * now take all request_content chunks available
1911 * */
1913 fcgi_header(&(header), FCGI_STDIN, request_id, weWant, 0);
1914 chunkqueue_append_mem(hctx->wb, (const char *)&header, sizeof(header));
1915 hctx->wb_reqlen += sizeof(header);
1917 if (p->conf.debug > 10) {
1918 log_error_write(srv, __FILE__, __LINE__, "soso", "tosend:", offset, "/", req_cqlen);
1921 chunkqueue_steal(hctx->wb, req_cq, weWant);
1922 /*(hctx->wb_reqlen already includes content_length)*/
1925 if (hctx->wb->bytes_in == hctx->wb_reqlen) {
1926 /* terminate STDIN */
1927 fcgi_header(&(header), FCGI_STDIN, request_id, 0, 0);
1928 chunkqueue_append_mem(hctx->wb, (const char *)&header, sizeof(header));
1929 hctx->wb_reqlen += (int)sizeof(header);
1933 static int fcgi_create_env(server *srv, handler_ctx *hctx, int request_id) {
1934 FCGI_BeginRequestRecord beginRecord;
1935 FCGI_Header header;
1937 char buf[LI_ITOSTRING_LENGTH];
1938 const char *s;
1939 #ifdef HAVE_IPV6
1940 char b2[INET6_ADDRSTRLEN + 1];
1941 #endif
1943 plugin_data *p = hctx->plugin_data;
1944 fcgi_extension_host *host= hctx->host;
1946 connection *con = hctx->remote_conn;
1947 buffer * const req_uri = (con->error_handler_saved_status >= 0) ? con->request.uri : con->request.orig_uri;
1948 server_socket *srv_sock = con->srv_socket;
1950 sock_addr our_addr;
1951 socklen_t our_addr_len;
1953 /* send FCGI_BEGIN_REQUEST */
1955 fcgi_header(&(beginRecord.header), FCGI_BEGIN_REQUEST, request_id, sizeof(beginRecord.body), 0);
1956 beginRecord.body.roleB0 = host->mode;
1957 beginRecord.body.roleB1 = 0;
1958 beginRecord.body.flags = 0;
1959 memset(beginRecord.body.reserved, 0, sizeof(beginRecord.body.reserved));
1961 /* send FCGI_PARAMS */
1962 buffer_string_prepare_copy(p->fcgi_env, 1023);
1965 if (buffer_is_empty(con->conf.server_tag)) {
1966 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_STR_LEN(PACKAGE_DESC)),con)
1967 } else {
1968 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_BUF_LEN(con->conf.server_tag)),con)
1971 if (!buffer_is_empty(con->server_name)) {
1972 size_t len = buffer_string_length(con->server_name);
1974 if (con->server_name->ptr[0] == '[') {
1975 const char *colon = strstr(con->server_name->ptr, "]:");
1976 if (colon) len = (colon + 1) - con->server_name->ptr;
1977 } else {
1978 const char *colon = strchr(con->server_name->ptr, ':');
1979 if (colon) len = colon - con->server_name->ptr;
1982 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("SERVER_NAME"), con->server_name->ptr, len),con)
1983 } else {
1984 #ifdef HAVE_IPV6
1985 s = inet_ntop(srv_sock->addr.plain.sa_family,
1986 srv_sock->addr.plain.sa_family == AF_INET6 ?
1987 (const void *) &(srv_sock->addr.ipv6.sin6_addr) :
1988 (const void *) &(srv_sock->addr.ipv4.sin_addr),
1989 b2, sizeof(b2)-1);
1990 #else
1991 s = inet_ntoa(srv_sock->addr.ipv4.sin_addr);
1992 #endif
1993 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("SERVER_NAME"), s, strlen(s)),con)
1996 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("GATEWAY_INTERFACE"), CONST_STR_LEN("CGI/1.1")),con)
1998 li_utostrn(buf, sizeof(buf),
1999 #ifdef HAVE_IPV6
2000 ntohs(srv_sock->addr.plain.sa_family ? srv_sock->addr.ipv6.sin6_port : srv_sock->addr.ipv4.sin_port)
2001 #else
2002 ntohs(srv_sock->addr.ipv4.sin_port)
2003 #endif
2006 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("SERVER_PORT"), buf, strlen(buf)),con)
2008 /* get the server-side of the connection to the client */
2009 our_addr_len = sizeof(our_addr);
2011 if (-1 == getsockname(con->fd, (struct sockaddr *)&our_addr, &our_addr_len)
2012 || our_addr_len > (socklen_t)sizeof(our_addr)) {
2013 s = inet_ntop_cache_get_ip(srv, &(srv_sock->addr));
2014 } else {
2015 s = inet_ntop_cache_get_ip(srv, &(our_addr));
2017 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("SERVER_ADDR"), s, strlen(s)),con)
2019 li_utostrn(buf, sizeof(buf),
2020 #ifdef HAVE_IPV6
2021 ntohs(con->dst_addr.plain.sa_family ? con->dst_addr.ipv6.sin6_port : con->dst_addr.ipv4.sin_port)
2022 #else
2023 ntohs(con->dst_addr.ipv4.sin_port)
2024 #endif
2027 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("REMOTE_PORT"), buf, strlen(buf)),con)
2029 s = inet_ntop_cache_get_ip(srv, &(con->dst_addr));
2030 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("REMOTE_ADDR"), s, strlen(s)),con)
2032 if (con->request.content_length > 0 && host->mode != FCGI_AUTHORIZER) {
2033 /* CGI-SPEC 6.1.2 and FastCGI spec 6.3 */
2035 li_itostrn(buf, sizeof(buf), con->request.content_length);
2036 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("CONTENT_LENGTH"), buf, strlen(buf)),con)
2039 if (host->mode != FCGI_AUTHORIZER) {
2041 * SCRIPT_NAME, PATH_INFO and PATH_TRANSLATED according to
2042 * http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html
2043 * (6.1.14, 6.1.6, 6.1.7)
2044 * For AUTHORIZER mode these headers should be omitted.
2047 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("SCRIPT_NAME"), CONST_BUF_LEN(con->uri.path)),con)
2049 if (!buffer_string_is_empty(con->request.pathinfo)) {
2050 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("PATH_INFO"), CONST_BUF_LEN(con->request.pathinfo)),con)
2052 /* PATH_TRANSLATED is only defined if PATH_INFO is set */
2054 if (!buffer_string_is_empty(host->docroot)) {
2055 buffer_copy_buffer(p->path, host->docroot);
2056 } else {
2057 buffer_copy_buffer(p->path, con->physical.basedir);
2059 buffer_append_string_buffer(p->path, con->request.pathinfo);
2060 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("PATH_TRANSLATED"), CONST_BUF_LEN(p->path)),con)
2061 } else {
2062 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("PATH_INFO"), CONST_STR_LEN("")),con)
2067 * SCRIPT_FILENAME and DOCUMENT_ROOT for php. The PHP manual
2068 * http://www.php.net/manual/en/reserved.variables.php
2069 * treatment of PATH_TRANSLATED is different from the one of CGI specs.
2070 * TODO: this code should be checked against cgi.fix_pathinfo php
2071 * parameter.
2074 if (!buffer_string_is_empty(host->docroot)) {
2076 * rewrite SCRIPT_FILENAME
2080 buffer_copy_buffer(p->path, host->docroot);
2081 buffer_append_string_buffer(p->path, con->uri.path);
2083 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(p->path)),con)
2084 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(host->docroot)),con)
2085 } else {
2086 buffer_copy_buffer(p->path, con->physical.path);
2088 /* cgi.fix_pathinfo need a broken SCRIPT_FILENAME to find out what PATH_INFO is itself
2090 * see src/sapi/cgi_main.c, init_request_info()
2092 if (host->break_scriptfilename_for_php) {
2093 buffer_append_string_buffer(p->path, con->request.pathinfo);
2096 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(p->path)),con)
2097 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(con->physical.basedir)),con)
2100 if (!buffer_string_is_empty(host->strip_request_uri)) {
2101 /* we need at least one char to strip off */
2103 * /app1/index/list
2105 * stripping /app1 or /app1/ should lead to
2107 * /index/list
2111 if ('/' != host->strip_request_uri->ptr[buffer_string_length(host->strip_request_uri) - 1]) {
2112 /* fix the user-input to have / as last char */
2113 buffer_append_string_len(host->strip_request_uri, CONST_STR_LEN("/"));
2116 if (buffer_string_length(req_uri) >= buffer_string_length(host->strip_request_uri) &&
2117 0 == strncmp(req_uri->ptr, host->strip_request_uri->ptr, buffer_string_length(host->strip_request_uri))) {
2118 /* the left is the same */
2120 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("REQUEST_URI"),
2121 req_uri->ptr + (buffer_string_length(host->strip_request_uri) - 1),
2122 buffer_string_length(req_uri) - (buffer_string_length(host->strip_request_uri) - 1)), con)
2123 } else {
2124 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(req_uri)),con)
2126 } else {
2127 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(req_uri)),con)
2129 if (!buffer_string_is_empty(con->uri.query)) {
2130 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("QUERY_STRING"), CONST_BUF_LEN(con->uri.query)),con)
2131 } else {
2132 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("QUERY_STRING"), CONST_STR_LEN("")),con)
2135 s = get_http_method_name(con->request.http_method);
2136 force_assert(s);
2137 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("REQUEST_METHOD"), s, strlen(s)),con)
2138 /* set REDIRECT_STATUS for php compiled with --force-redirect
2139 * (if REDIRECT_STATUS has not already been set by error handler) */
2140 if (0 == con->error_handler_saved_status) {
2141 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("REDIRECT_STATUS"), CONST_STR_LEN("200")), con);
2143 s = get_http_version_name(con->request.http_version);
2144 force_assert(s);
2145 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("SERVER_PROTOCOL"), s, strlen(s)),con)
2147 if (buffer_is_equal_caseless_string(con->uri.scheme, CONST_STR_LEN("https"))) {
2148 FCGI_ENV_ADD_CHECK(fcgi_env_add(p->fcgi_env, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on")),con)
2151 FCGI_ENV_ADD_CHECK(fcgi_env_add_request_headers(srv, con, p), con);
2154 buffer *b = buffer_init();
2156 buffer_copy_string_len(b, (const char *)&beginRecord, sizeof(beginRecord));
2158 fcgi_header(&(header), FCGI_PARAMS, request_id, buffer_string_length(p->fcgi_env), 0);
2159 buffer_append_string_len(b, (const char *)&header, sizeof(header));
2160 buffer_append_string_buffer(b, p->fcgi_env);
2162 fcgi_header(&(header), FCGI_PARAMS, request_id, 0, 0);
2163 buffer_append_string_len(b, (const char *)&header, sizeof(header));
2165 hctx->wb_reqlen = buffer_string_length(b);
2166 chunkqueue_append_buffer(hctx->wb, b);
2167 buffer_free(b);
2170 hctx->wb_reqlen += con->request.content_length;/* (eventual) (minimal) total request size, not necessarily including all fcgi_headers around content length yet */
2171 fcgi_stdin_append(srv, con, hctx, request_id);
2173 return 0;
2176 static int fcgi_response_parse(server *srv, connection *con, plugin_data *p, buffer *in) {
2177 char *s, *ns;
2179 handler_ctx *hctx = con->plugin_ctx[p->id];
2180 fcgi_extension_host *host= hctx->host;
2181 int have_sendfile2 = 0;
2182 off_t sendfile2_content_length = 0;
2184 UNUSED(srv);
2186 /* search for \n */
2187 for (s = in->ptr; NULL != (ns = strchr(s, '\n')); s = ns + 1) {
2188 char *key, *value;
2189 int key_len;
2191 /* a good day. Someone has read the specs and is sending a \r\n to us */
2193 if (ns > in->ptr &&
2194 *(ns-1) == '\r') {
2195 *(ns-1) = '\0';
2198 ns[0] = '\0';
2200 key = s;
2201 if (NULL == (value = strchr(s, ':'))) {
2202 /* we expect: "<key>: <value>\n" */
2203 continue;
2206 key_len = value - key;
2208 value++;
2209 /* strip WS */
2210 while (*value == ' ' || *value == '\t') value++;
2212 if (host->mode != FCGI_AUTHORIZER ||
2213 !(con->http_status == 0 ||
2214 con->http_status == 200)) {
2215 /* authorizers shouldn't affect the response headers sent back to the client */
2217 /* don't forward Status: */
2218 if (0 != strncasecmp(key, "Status", key_len)) {
2219 data_string *ds;
2220 if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
2221 ds = data_response_init();
2223 buffer_copy_string_len(ds->key, key, key_len);
2224 buffer_copy_string(ds->value, value);
2226 array_insert_unique(con->response.headers, (data_unset *)ds);
2230 switch(key_len) {
2231 case 4:
2232 if (0 == strncasecmp(key, "Date", key_len)) {
2233 con->parsed_response |= HTTP_DATE;
2235 break;
2236 case 6:
2237 if (0 == strncasecmp(key, "Status", key_len)) {
2238 int status = strtol(value, NULL, 10);
2239 if (status >= 100 && status < 1000) {
2240 con->http_status = status;
2241 con->parsed_response |= HTTP_STATUS;
2242 } else {
2243 con->http_status = 502;
2246 break;
2247 case 8:
2248 if (0 == strncasecmp(key, "Location", key_len)) {
2249 con->parsed_response |= HTTP_LOCATION;
2251 break;
2252 case 10:
2253 if (0 == strncasecmp(key, "Connection", key_len)) {
2254 con->response.keep_alive = (0 == strcasecmp(value, "Keep-Alive")) ? 1 : 0;
2255 con->parsed_response |= HTTP_CONNECTION;
2257 break;
2258 case 11:
2259 if (host->xsendfile_allow && 0 == strncasecmp(key, "X-Sendfile2", key_len) && hctx->send_content_body) {
2260 char *pos = value;
2261 have_sendfile2 = 1;
2263 while (*pos) {
2264 char *filename, *range;
2265 stat_cache_entry *sce;
2266 off_t begin_range, end_range, range_len;
2268 while (' ' == *pos) pos++;
2269 if (!*pos) break;
2271 filename = pos;
2272 if (NULL == (range = strchr(pos, ' '))) {
2273 /* missing range */
2274 if (p->conf.debug) {
2275 log_error_write(srv, __FILE__, __LINE__, "ss", "Couldn't find range after filename:", filename);
2277 return 502;
2279 buffer_copy_string_len(srv->tmp_buf, filename, range - filename);
2281 /* find end of range */
2282 for (pos = ++range; *pos && *pos != ' ' && *pos != ','; pos++) ;
2284 buffer_urldecode_path(srv->tmp_buf);
2285 buffer_path_simplify(srv->tmp_buf, srv->tmp_buf);
2286 if (con->conf.force_lowercase_filenames) {
2287 buffer_to_lower(srv->tmp_buf);
2289 if (host->xsendfile_docroot->used) {
2290 size_t i, xlen = buffer_string_length(srv->tmp_buf);
2291 for (i = 0; i < host->xsendfile_docroot->used; ++i) {
2292 data_string *ds = (data_string *)host->xsendfile_docroot->data[i];
2293 size_t dlen = buffer_string_length(ds->value);
2294 if (dlen <= xlen
2295 && (!con->conf.force_lowercase_filenames
2296 ? 0 == memcmp(srv->tmp_buf->ptr, ds->value->ptr, dlen)
2297 : 0 == strncasecmp(srv->tmp_buf->ptr, ds->value->ptr, dlen))) {
2298 break;
2301 if (i == host->xsendfile_docroot->used) {
2302 log_error_write(srv, __FILE__, __LINE__, "SBs",
2303 "X-Sendfile2 (", srv->tmp_buf,
2304 ") not under configured x-sendfile-docroot(s)");
2305 return 403;
2309 if (HANDLER_ERROR == stat_cache_get_entry(srv, con, srv->tmp_buf, &sce)) {
2310 if (p->conf.debug) {
2311 log_error_write(srv, __FILE__, __LINE__, "sb",
2312 "send-file error: couldn't get stat_cache entry for X-Sendfile2:",
2313 srv->tmp_buf);
2315 return 404;
2316 } else if (!S_ISREG(sce->st.st_mode)) {
2317 if (p->conf.debug) {
2318 log_error_write(srv, __FILE__, __LINE__, "sb",
2319 "send-file error: wrong filetype for X-Sendfile2:",
2320 srv->tmp_buf);
2322 return 502;
2324 /* found the file */
2326 /* parse range */
2327 begin_range = 0; end_range = sce->st.st_size - 1;
2329 char *rpos = NULL;
2330 errno = 0;
2331 begin_range = strtoll(range, &rpos, 10);
2332 if (errno != 0 || begin_range < 0 || rpos == range) goto range_failed;
2333 if ('-' != *rpos++) goto range_failed;
2334 if (rpos != pos) {
2335 range = rpos;
2336 end_range = strtoll(range, &rpos, 10);
2337 if (errno != 0 || end_range < 0 || rpos == range) goto range_failed;
2339 if (rpos != pos) goto range_failed;
2341 goto range_success;
2343 range_failed:
2344 if (p->conf.debug) {
2345 log_error_write(srv, __FILE__, __LINE__, "ss", "Couldn't decode range after filename:", filename);
2347 return 502;
2349 range_success: ;
2352 /* no parameters accepted */
2354 while (*pos == ' ') pos++;
2355 if (*pos != '\0' && *pos != ',') return 502;
2357 range_len = end_range - begin_range + 1;
2358 if (range_len < 0) return 502;
2359 if (range_len != 0) {
2360 if (0 != http_chunk_append_file_range(srv, con, srv->tmp_buf, begin_range, range_len)) {
2361 return 502;
2364 sendfile2_content_length += range_len;
2366 if (*pos == ',') pos++;
2369 break;
2370 case 14:
2371 if (0 == strncasecmp(key, "Content-Length", key_len)) {
2372 con->response.content_length = strtoul(value, NULL, 10);
2373 con->parsed_response |= HTTP_CONTENT_LENGTH;
2375 if (con->response.content_length < 0) con->response.content_length = 0;
2377 break;
2378 default:
2379 break;
2383 if (have_sendfile2) {
2384 data_string *dcls;
2386 /* fix content-length */
2387 if (NULL == (dcls = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
2388 dcls = data_response_init();
2391 buffer_copy_string_len(dcls->key, "Content-Length", sizeof("Content-Length")-1);
2392 buffer_copy_int(dcls->value, sendfile2_content_length);
2393 array_replace(con->response.headers, (data_unset *)dcls);
2395 con->parsed_response |= HTTP_CONTENT_LENGTH;
2396 con->response.content_length = sendfile2_content_length;
2397 return 200;
2400 /* CGI/1.1 rev 03 - 7.2.1.2 */
2401 if ((con->parsed_response & HTTP_LOCATION) &&
2402 !(con->parsed_response & HTTP_STATUS)) {
2403 con->http_status = 302;
2406 return 0;
2409 typedef struct {
2410 buffer *b;
2411 unsigned int len;
2412 int type;
2413 int padding;
2414 int request_id;
2415 } fastcgi_response_packet;
2417 static int fastcgi_get_packet(server *srv, handler_ctx *hctx, fastcgi_response_packet *packet) {
2418 chunk *c;
2419 size_t offset;
2420 size_t toread;
2421 FCGI_Header *header;
2423 if (!hctx->rb->first) return -1;
2425 packet->b = buffer_init();
2426 packet->len = 0;
2427 packet->type = 0;
2428 packet->padding = 0;
2429 packet->request_id = 0;
2431 offset = 0; toread = 8;
2432 /* get at least the FastCGI header */
2433 for (c = hctx->rb->first; c; c = c->next) {
2434 size_t weHave = buffer_string_length(c->mem) - c->offset;
2436 if (weHave > toread) weHave = toread;
2438 buffer_append_string_len(packet->b, c->mem->ptr + c->offset, weHave);
2439 toread -= weHave;
2440 offset = weHave; /* skip offset bytes in chunk for "real" data */
2442 if (0 == toread) break;
2445 if (buffer_string_length(packet->b) < sizeof(FCGI_Header)) {
2446 /* no header */
2447 if (hctx->plugin_data->conf.debug) {
2448 log_error_write(srv, __FILE__, __LINE__, "sdsds", "FastCGI: header too small:", buffer_string_length(packet->b), "bytes <", sizeof(FCGI_Header), "bytes, waiting for more data");
2451 buffer_free(packet->b);
2453 return -1;
2456 /* we have at least a header, now check how much me have to fetch */
2457 header = (FCGI_Header *)(packet->b->ptr);
2459 packet->len = (header->contentLengthB0 | (header->contentLengthB1 << 8)) + header->paddingLength;
2460 packet->request_id = (header->requestIdB0 | (header->requestIdB1 << 8));
2461 packet->type = header->type;
2462 packet->padding = header->paddingLength;
2464 /* ->b should only be the content */
2465 buffer_string_set_length(packet->b, 0);
2467 if (packet->len) {
2468 /* copy the content */
2469 for (; c && (buffer_string_length(packet->b) < packet->len); c = c->next) {
2470 size_t weWant = packet->len - buffer_string_length(packet->b);
2471 size_t weHave = buffer_string_length(c->mem) - c->offset - offset;
2473 if (weHave > weWant) weHave = weWant;
2475 buffer_append_string_len(packet->b, c->mem->ptr + c->offset + offset, weHave);
2477 /* we only skipped the first bytes as they belonged to the fcgi header */
2478 offset = 0;
2481 if (buffer_string_length(packet->b) < packet->len) {
2482 /* we didn't get the full packet */
2484 buffer_free(packet->b);
2485 return -1;
2488 buffer_string_set_length(packet->b, buffer_string_length(packet->b) - packet->padding);
2491 chunkqueue_mark_written(hctx->rb, packet->len + sizeof(FCGI_Header));
2493 return 0;
2496 static int fcgi_demux_response(server *srv, handler_ctx *hctx) {
2497 int fin = 0;
2498 int toread, ret;
2499 ssize_t r = 0;
2501 plugin_data *p = hctx->plugin_data;
2502 connection *con = hctx->remote_conn;
2503 int fcgi_fd = hctx->fd;
2504 fcgi_extension_host *host= hctx->host;
2505 fcgi_proc *proc = hctx->proc;
2508 * check how much we have to read
2510 #if !defined(_WIN32) && !defined(__CYGWIN__)
2511 if (ioctl(hctx->fd, FIONREAD, &toread)) {
2512 if (errno == EAGAIN) {
2513 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
2514 return 0;
2516 log_error_write(srv, __FILE__, __LINE__, "sd",
2517 "unexpected end-of-file (perhaps the fastcgi process died):",
2518 fcgi_fd);
2519 return -1;
2521 #else
2522 toread = 4096;
2523 #endif
2525 if (toread > 0) {
2526 char *mem;
2527 size_t mem_len;
2529 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)) {
2530 off_t cqlen = chunkqueue_length(hctx->rb);
2531 if (cqlen + toread > 65536 + (int)sizeof(FCGI_Header)) { /*(max size of FastCGI packet + 1)*/
2532 if (cqlen < 65536 + (int)sizeof(FCGI_Header)) {
2533 toread = 65536 + (int)sizeof(FCGI_Header) - cqlen;
2534 } else { /* should not happen */
2535 toread = toread < 1024 ? toread : 1024;
2540 chunkqueue_get_memory(hctx->rb, &mem, &mem_len, 0, toread);
2541 r = read(hctx->fd, mem, mem_len);
2542 chunkqueue_use_memory(hctx->rb, r > 0 ? r : 0);
2544 if (-1 == r) {
2545 if (errno == EAGAIN) {
2546 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
2547 return 0;
2549 log_error_write(srv, __FILE__, __LINE__, "sds",
2550 "unexpected end-of-file (perhaps the fastcgi process died):",
2551 fcgi_fd, strerror(errno));
2552 return -1;
2555 if (0 == r) {
2556 log_error_write(srv, __FILE__, __LINE__, "ssdsb",
2557 "unexpected end-of-file (perhaps the fastcgi process died):",
2558 "pid:", proc->pid,
2559 "socket:", proc->connection_name);
2561 return -1;
2565 * parse the fastcgi packets and forward the content to the write-queue
2568 while (fin == 0) {
2569 fastcgi_response_packet packet;
2571 /* check if we have at least one packet */
2572 if (0 != fastcgi_get_packet(srv, hctx, &packet)) {
2573 /* no full packet */
2574 break;
2577 switch(packet.type) {
2578 case FCGI_STDOUT:
2579 if (packet.len == 0) break;
2581 /* is the header already finished */
2582 if (0 == con->file_started) {
2583 char *c;
2584 data_string *ds;
2586 /* search for header terminator
2588 * if we start with \r\n check if last packet terminated with \r\n
2589 * if we start with \n check if last packet terminated with \n
2590 * search for \r\n\r\n
2591 * search for \n\n
2594 buffer_append_string_buffer(hctx->response_header, packet.b);
2596 if (NULL != (c = buffer_search_string_len(hctx->response_header, CONST_STR_LEN("\r\n\r\n")))) {
2597 char *hend = c + 4; /* header end == body start */
2598 size_t hlen = hend - hctx->response_header->ptr;
2599 buffer_copy_string_len(packet.b, hend, buffer_string_length(hctx->response_header) - hlen);
2600 buffer_string_set_length(hctx->response_header, hlen);
2601 } else if (NULL != (c = buffer_search_string_len(hctx->response_header, CONST_STR_LEN("\n\n")))) {
2602 char *hend = c + 2; /* header end == body start */
2603 size_t hlen = hend - hctx->response_header->ptr;
2604 buffer_copy_string_len(packet.b, hend, buffer_string_length(hctx->response_header) - hlen);
2605 buffer_string_set_length(hctx->response_header, hlen);
2606 } else {
2607 /* no luck, no header found */
2608 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
2609 if (buffer_string_length(hctx->response_header) > MAX_HTTP_REQUEST_HEADER) {
2610 log_error_write(srv, __FILE__, __LINE__, "sb", "response headers too large for", con->uri.path);
2611 con->http_status = 502; /* Bad Gateway */
2612 con->mode = DIRECT;
2613 fin = 1;
2615 break;
2618 /* parse the response header */
2619 if ((ret = fcgi_response_parse(srv, con, p, hctx->response_header))) {
2620 if (200 != ret) { /*(200 returned for X-Sendfile2 handled)*/
2621 con->http_status = ret;
2622 con->mode = DIRECT;
2624 con->file_started = 1;
2625 hctx->send_content_body = 0;
2626 fin = 1;
2627 break;
2630 con->file_started = 1;
2632 if (host->mode == FCGI_AUTHORIZER &&
2633 (con->http_status == 0 ||
2634 con->http_status == 200)) {
2635 /* a authorizer with approved the static request, ignore the content here */
2636 hctx->send_content_body = 0;
2639 if (host->xsendfile_allow && hctx->send_content_body &&
2640 (NULL != (ds = (data_string *) array_get_element(con->response.headers, "X-LIGHTTPD-send-file"))
2641 || NULL != (ds = (data_string *) array_get_element(con->response.headers, "X-Sendfile")))) {
2642 http_response_xsendfile(srv, con, ds->value, host->xsendfile_docroot);
2643 if (con->mode == DIRECT) {
2644 fin = 1;
2647 hctx->send_content_body = 0; /* ignore the content */
2648 break;
2652 if (hctx->send_content_body && !buffer_string_is_empty(packet.b)) {
2653 if (0 != http_chunk_append_buffer(srv, con, packet.b)) {
2654 /* error writing to tempfile;
2655 * truncate response or send 500 if nothing sent yet */
2656 fin = 1;
2657 break;
2659 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
2660 && chunkqueue_length(con->write_queue) > 65536 - 4096) {
2661 if (!con->is_writable) {
2662 /*(defer removal of FDEVENT_IN interest since
2663 * connection_state_machine() might be able to send data
2664 * immediately, unless !con->is_writable, where
2665 * connection_state_machine() might not loop back to call
2666 * mod_fastcgi_handle_subrequest())*/
2667 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
2671 break;
2672 case FCGI_STDERR:
2673 if (packet.len == 0) break;
2675 log_error_write_multiline_buffer(srv, __FILE__, __LINE__, packet.b, "s",
2676 "FastCGI-stderr:");
2678 break;
2679 case FCGI_END_REQUEST:
2680 fin = 1;
2681 break;
2682 default:
2683 log_error_write(srv, __FILE__, __LINE__, "sd",
2684 "FastCGI: header.type not handled: ", packet.type);
2685 break;
2687 buffer_free(packet.b);
2690 return fin;
2693 static int fcgi_restart_dead_procs(server *srv, plugin_data *p, fcgi_extension_host *host) {
2694 fcgi_proc *proc;
2696 for (proc = host->first; proc; proc = proc->next) {
2697 int status;
2699 if (p->conf.debug > 2) {
2700 log_error_write(srv, __FILE__, __LINE__, "sbdddd",
2701 "proc:",
2702 proc->connection_name,
2703 proc->state,
2704 proc->is_local,
2705 proc->load,
2706 proc->pid);
2710 * if the remote side is overloaded, we check back after <n> seconds
2713 switch (proc->state) {
2714 case PROC_STATE_KILLED:
2715 case PROC_STATE_UNSET:
2716 /* this should never happen as long as adaptive spawing is disabled */
2717 force_assert(0);
2719 break;
2720 case PROC_STATE_RUNNING:
2721 break;
2722 case PROC_STATE_OVERLOADED:
2723 if (srv->cur_ts <= proc->disabled_until) break;
2725 proc->state = PROC_STATE_RUNNING;
2726 host->active_procs++;
2728 log_error_write(srv, __FILE__, __LINE__, "sbdb",
2729 "fcgi-server re-enabled:",
2730 host->host, host->port,
2731 host->unixsocket);
2732 break;
2733 case PROC_STATE_DIED_WAIT_FOR_PID:
2734 /* non-local procs don't have PIDs to wait for */
2735 if (!proc->is_local) {
2736 proc->state = PROC_STATE_DIED;
2737 } else {
2738 /* the child should not terminate at all */
2740 for ( ;; ) {
2741 switch(waitpid(proc->pid, &status, WNOHANG)) {
2742 case 0:
2743 /* child is still alive */
2744 if (srv->cur_ts <= proc->disabled_until) break;
2746 proc->state = PROC_STATE_RUNNING;
2747 host->active_procs++;
2749 log_error_write(srv, __FILE__, __LINE__, "sbdb",
2750 "fcgi-server re-enabled:",
2751 host->host, host->port,
2752 host->unixsocket);
2753 break;
2754 case -1:
2755 if (errno == EINTR) continue;
2757 log_error_write(srv, __FILE__, __LINE__, "sd",
2758 "child died somehow, waitpid failed:",
2759 errno);
2760 proc->state = PROC_STATE_DIED;
2761 break;
2762 default:
2763 if (WIFEXITED(status)) {
2764 #if 0
2765 log_error_write(srv, __FILE__, __LINE__, "sdsd",
2766 "child exited, pid:", proc->pid,
2767 "status:", WEXITSTATUS(status));
2768 #endif
2769 } else if (WIFSIGNALED(status)) {
2770 log_error_write(srv, __FILE__, __LINE__, "sd",
2771 "child signaled:",
2772 WTERMSIG(status));
2773 } else {
2774 log_error_write(srv, __FILE__, __LINE__, "sd",
2775 "child died somehow:",
2776 status);
2779 proc->state = PROC_STATE_DIED;
2780 break;
2782 break;
2786 /* fall through if we have a dead proc now */
2787 if (proc->state != PROC_STATE_DIED) break;
2789 case PROC_STATE_DIED:
2790 /* local procs get restarted by us,
2791 * remote ones hopefully by the admin */
2793 if (!buffer_string_is_empty(host->bin_path)) {
2794 /* we still have connections bound to this proc,
2795 * let them terminate first */
2796 if (proc->load != 0) break;
2798 /* restart the child */
2800 if (p->conf.debug) {
2801 log_error_write(srv, __FILE__, __LINE__, "ssbsdsd",
2802 "--- fastcgi spawning",
2803 "\n\tsocket", proc->connection_name,
2804 "\n\tcurrent:", 1, "/", host->max_procs);
2807 if (fcgi_spawn_connection(srv, p, host, proc)) {
2808 log_error_write(srv, __FILE__, __LINE__, "s",
2809 "ERROR: spawning fcgi failed.");
2810 return HANDLER_ERROR;
2812 } else {
2813 if (srv->cur_ts <= proc->disabled_until) break;
2815 proc->state = PROC_STATE_RUNNING;
2816 host->active_procs++;
2818 log_error_write(srv, __FILE__, __LINE__, "sb",
2819 "fcgi-server re-enabled:",
2820 proc->connection_name);
2822 break;
2826 return 0;
2829 static handler_t fcgi_write_request(server *srv, handler_ctx *hctx) {
2830 plugin_data *p = hctx->plugin_data;
2831 fcgi_extension_host *host= hctx->host;
2832 connection *con = hctx->remote_conn;
2833 fcgi_proc *proc;
2835 int ret;
2837 /* sanity check:
2838 * - host != NULL
2839 * - either:
2840 * - tcp socket (do not check host->host->uses, as it may be not set which means INADDR_LOOPBACK)
2841 * - unix socket
2843 if (!host) {
2844 log_error_write(srv, __FILE__, __LINE__, "s", "fatal error: host = NULL");
2845 return HANDLER_ERROR;
2847 if ((!host->port && buffer_string_is_empty(host->unixsocket))) {
2848 log_error_write(srv, __FILE__, __LINE__, "s", "fatal error: neither host->port nor host->unixsocket is set");
2849 return HANDLER_ERROR;
2852 /* we can't handle this in the switch as we have to fall through in it */
2853 if (hctx->state == FCGI_STATE_CONNECT_DELAYED) {
2854 int socket_error;
2855 socklen_t socket_error_len = sizeof(socket_error);
2857 /* try to finish the connect() */
2858 if (0 != getsockopt(hctx->fd, SOL_SOCKET, SO_ERROR, &socket_error, &socket_error_len)) {
2859 log_error_write(srv, __FILE__, __LINE__, "ss",
2860 "getsockopt failed:", strerror(errno));
2862 fcgi_host_disable(srv, hctx);
2864 return HANDLER_ERROR;
2866 if (socket_error != 0) {
2867 if (!hctx->proc->is_local || p->conf.debug) {
2868 /* local procs get restarted */
2870 log_error_write(srv, __FILE__, __LINE__, "sssb",
2871 "establishing connection failed:", strerror(socket_error),
2872 "socket:", hctx->proc->connection_name);
2875 fcgi_host_disable(srv, hctx);
2876 log_error_write(srv, __FILE__, __LINE__, "sdssdsd",
2877 "backend is overloaded; we'll disable it for", hctx->host->disable_time, "seconds and send the request to another backend instead:",
2878 "reconnects:", hctx->reconnects,
2879 "load:", host->load);
2881 fastcgi_status_copy_procname(p->statuskey, hctx->host, hctx->proc);
2882 buffer_append_string_len(p->statuskey, CONST_STR_LEN(".died"));
2884 status_counter_inc(srv, CONST_BUF_LEN(p->statuskey));
2886 return HANDLER_ERROR;
2888 /* go on with preparing the request */
2889 hctx->state = FCGI_STATE_PREPARE_WRITE;
2893 switch(hctx->state) {
2894 case FCGI_STATE_CONNECT_DELAYED:
2895 /* should never happen */
2896 return HANDLER_WAIT_FOR_EVENT;
2897 case FCGI_STATE_INIT:
2898 /* do we have a running process for this host (max-procs) ? */
2899 hctx->proc = NULL;
2901 for (proc = hctx->host->first;
2902 proc && proc->state != PROC_STATE_RUNNING;
2903 proc = proc->next);
2905 /* all children are dead */
2906 if (proc == NULL) {
2907 hctx->fde_ndx = -1;
2909 return HANDLER_ERROR;
2912 hctx->proc = proc;
2914 /* check the other procs if they have a lower load */
2915 for (proc = proc->next; proc; proc = proc->next) {
2916 if (proc->state != PROC_STATE_RUNNING) continue;
2917 if (proc->load < hctx->proc->load) hctx->proc = proc;
2920 if (-1 == (hctx->fd = socket(host->family, SOCK_STREAM, 0))) {
2921 if (errno == EMFILE ||
2922 errno == EINTR) {
2923 log_error_write(srv, __FILE__, __LINE__, "sd",
2924 "wait for fd at connection:", con->fd);
2926 return HANDLER_WAIT_FOR_FD;
2929 log_error_write(srv, __FILE__, __LINE__, "ssdd",
2930 "socket failed:", strerror(errno), srv->cur_fds, srv->max_fds);
2931 return HANDLER_ERROR;
2933 hctx->fde_ndx = -1;
2935 srv->cur_fds++;
2937 fdevent_register(srv->ev, hctx->fd, fcgi_handle_fdevent, hctx);
2939 if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
2940 log_error_write(srv, __FILE__, __LINE__, "ss",
2941 "fcntl failed:", strerror(errno));
2943 return HANDLER_ERROR;
2946 if (hctx->proc->is_local) {
2947 hctx->pid = hctx->proc->pid;
2950 switch (fcgi_establish_connection(srv, hctx)) {
2951 case CONNECTION_DELAYED:
2952 /* connection is in progress, wait for an event and call getsockopt() below */
2954 fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2956 fcgi_set_state(srv, hctx, FCGI_STATE_CONNECT_DELAYED);
2957 return HANDLER_WAIT_FOR_EVENT;
2958 case CONNECTION_OVERLOADED:
2959 /* cool down the backend, it is overloaded
2960 * -> EAGAIN */
2962 if (hctx->host->disable_time) {
2963 log_error_write(srv, __FILE__, __LINE__, "sdssdsd",
2964 "backend is overloaded; we'll disable it for", hctx->host->disable_time, "seconds and send the request to another backend instead:",
2965 "reconnects:", hctx->reconnects,
2966 "load:", host->load);
2968 hctx->proc->disabled_until = srv->cur_ts + hctx->host->disable_time;
2969 if (hctx->proc->state == PROC_STATE_RUNNING) hctx->host->active_procs--;
2970 hctx->proc->state = PROC_STATE_OVERLOADED;
2973 fastcgi_status_copy_procname(p->statuskey, hctx->host, hctx->proc);
2974 buffer_append_string_len(p->statuskey, CONST_STR_LEN(".overloaded"));
2976 status_counter_inc(srv, CONST_BUF_LEN(p->statuskey));
2978 return HANDLER_ERROR;
2979 case CONNECTION_DEAD:
2980 /* we got a hard error from the backend like
2981 * - ECONNREFUSED for tcp-ip sockets
2982 * - ENOENT for unix-domain-sockets
2984 * for check if the host is back in hctx->host->disable_time seconds
2985 * */
2987 fcgi_host_disable(srv, hctx);
2989 log_error_write(srv, __FILE__, __LINE__, "sdssdsd",
2990 "backend died; we'll disable it for", hctx->host->disable_time, "seconds and send the request to another backend instead:",
2991 "reconnects:", hctx->reconnects,
2992 "load:", host->load);
2994 fastcgi_status_copy_procname(p->statuskey, hctx->host, hctx->proc);
2995 buffer_append_string_len(p->statuskey, CONST_STR_LEN(".died"));
2997 status_counter_inc(srv, CONST_BUF_LEN(p->statuskey));
2999 return HANDLER_ERROR;
3000 case CONNECTION_OK:
3001 /* everything is ok, go on */
3003 fcgi_set_state(srv, hctx, FCGI_STATE_PREPARE_WRITE);
3005 break;
3007 /* fallthrough */
3008 case FCGI_STATE_PREPARE_WRITE:
3009 /* ok, we have the connection */
3011 fcgi_proc_load_inc(srv, hctx);
3012 hctx->got_proc = 1;
3014 status_counter_inc(srv, CONST_STR_LEN("fastcgi.requests"));
3016 fastcgi_status_copy_procname(p->statuskey, hctx->host, hctx->proc);
3017 buffer_append_string_len(p->statuskey, CONST_STR_LEN(".connected"));
3019 status_counter_inc(srv, CONST_BUF_LEN(p->statuskey));
3021 if (p->conf.debug) {
3022 log_error_write(srv, __FILE__, __LINE__, "ssdsbsd",
3023 "got proc:",
3024 "pid:", hctx->proc->pid,
3025 "socket:", hctx->proc->connection_name,
3026 "load:", hctx->proc->load);
3029 /* move the proc-list entry down the list */
3030 if (hctx->request_id == 0) {
3031 hctx->request_id = 1; /* always use id 1 as we don't use multiplexing */
3032 } else {
3033 log_error_write(srv, __FILE__, __LINE__, "sd",
3034 "fcgi-request is already in use:", hctx->request_id);
3037 if (-1 == fcgi_create_env(srv, hctx, hctx->request_id)) return HANDLER_ERROR;
3039 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
3040 fcgi_set_state(srv, hctx, FCGI_STATE_WRITE);
3041 /* fall through */
3042 case FCGI_STATE_WRITE:
3043 ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb, MAX_WRITE_LIMIT);
3045 chunkqueue_remove_finished_chunks(hctx->wb);
3047 if (ret < 0) {
3048 switch(errno) {
3049 case EPIPE:
3050 case ENOTCONN:
3051 case ECONNRESET:
3052 /* the connection got dropped after accept()
3053 * we don't care about that - if you accept() it, you have to handle it.
3056 log_error_write(srv, __FILE__, __LINE__, "ssosb",
3057 "connection was dropped after accept() (perhaps the fastcgi process died),",
3058 "write-offset:", hctx->wb->bytes_out,
3059 "socket:", hctx->proc->connection_name);
3061 return HANDLER_ERROR;
3062 default:
3063 log_error_write(srv, __FILE__, __LINE__, "ssd",
3064 "write failed:", strerror(errno), errno);
3066 return HANDLER_ERROR;
3070 if (hctx->wb->bytes_out == hctx->wb_reqlen) {
3071 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
3072 fcgi_set_state(srv, hctx, FCGI_STATE_READ);
3073 } else {
3074 off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
3075 if (hctx->wb->bytes_in < hctx->wb_reqlen && wblen < 65536 - 16384) {
3076 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
3077 if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
3078 con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
3079 con->is_readable = 1; /* trigger optimistic read from client */
3082 if (0 == wblen) {
3083 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
3084 } else {
3085 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
3089 return HANDLER_WAIT_FOR_EVENT;
3090 case FCGI_STATE_READ:
3091 /* waiting for a response */
3092 return HANDLER_WAIT_FOR_EVENT;
3093 default:
3094 log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
3095 return HANDLER_ERROR;
3100 /* might be called on fdevent after a connect() is delay too
3101 * */
3102 static handler_t fcgi_send_request(server *srv, handler_ctx *hctx) {
3103 fcgi_extension_host *host;
3104 handler_t rc;
3106 /* we don't have a host yet, choose one
3107 * -> this happens in the first round
3108 * and when the host died and we have to select a new one */
3109 if (hctx->host == NULL) {
3110 size_t k;
3111 int ndx, used = -1;
3113 /* check if the next server has no load. */
3114 ndx = hctx->ext->last_used_ndx + 1;
3115 if(ndx >= (int) hctx->ext->used || ndx < 0) ndx = 0;
3116 host = hctx->ext->hosts[ndx];
3117 if (host->load > 0) {
3118 /* get backend with the least load. */
3119 for (k = 0, ndx = -1; k < hctx->ext->used; k++) {
3120 host = hctx->ext->hosts[k];
3122 /* we should have at least one proc that can do something */
3123 if (host->active_procs == 0) continue;
3125 if (used == -1 || host->load < used) {
3126 used = host->load;
3128 ndx = k;
3133 /* found a server */
3134 if (ndx == -1) {
3135 /* all hosts are down */
3137 fcgi_connection_close(srv, hctx);
3139 return HANDLER_FINISHED;
3142 hctx->ext->last_used_ndx = ndx;
3143 host = hctx->ext->hosts[ndx];
3146 * if check-local is disabled, use the uri.path handler
3150 /* init handler-context */
3152 /* we put a connection on this host, move the other new connections to other hosts
3154 * as soon as hctx->host is unassigned, decrease the load again */
3155 fcgi_host_assign(srv, hctx, host);
3156 hctx->proc = NULL;
3157 } else {
3158 host = hctx->host;
3161 /* ok, create the request */
3162 rc = fcgi_write_request(srv, hctx);
3163 if (HANDLER_ERROR != rc) {
3164 return rc;
3165 } else {
3166 plugin_data *p = hctx->plugin_data;
3167 connection *con = hctx->remote_conn;
3169 if (hctx->state == FCGI_STATE_INIT ||
3170 hctx->state == FCGI_STATE_CONNECT_DELAYED) {
3171 fcgi_restart_dead_procs(srv, p, host);
3173 /* cleanup this request and let the request handler start this request again */
3174 if (hctx->reconnects < 5) {
3175 fcgi_reconnect(srv, hctx);
3177 return HANDLER_COMEBACK;
3178 } else {
3179 fcgi_connection_close(srv, hctx);
3180 con->http_status = 503;
3182 return HANDLER_FINISHED;
3184 } else {
3185 int status = con->http_status;
3186 fcgi_connection_close(srv, hctx);
3187 con->http_status = (status == 400) ? 400 : 503; /* see FCGI_ENV_ADD_CHECK() for 400 error */
3189 return HANDLER_FINISHED;
3195 static handler_t fcgi_recv_response(server *srv, handler_ctx *hctx);
3198 SUBREQUEST_FUNC(mod_fastcgi_handle_subrequest) {
3199 plugin_data *p = p_d;
3201 handler_ctx *hctx = con->plugin_ctx[p->id];
3203 if (NULL == hctx) return HANDLER_GO_ON;
3205 /* not my job */
3206 if (con->mode != p->id) return HANDLER_GO_ON;
3208 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
3209 && con->file_started) {
3210 if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
3211 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
3212 } else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)) {
3213 /* optimistic read from backend, which might re-enable FDEVENT_IN */
3214 handler_t rc = fcgi_recv_response(srv, hctx); /*(might invalidate hctx)*/
3215 if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
3219 if (0 == hctx->wb->bytes_in
3220 ? con->state == CON_STATE_READ_POST
3221 : hctx->wb->bytes_in < hctx->wb_reqlen) {
3222 /*(64k - 4k to attempt to avoid temporary files
3223 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
3224 if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096
3225 && (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN)){
3226 con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
3227 if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
3228 } else {
3229 handler_t r = connection_handle_read_post_state(srv, con);
3230 chunkqueue *req_cq = con->request_content_queue;
3231 if (0 != hctx->wb->bytes_in && !chunkqueue_is_empty(req_cq)) {
3232 fcgi_stdin_append(srv, con, hctx, hctx->request_id);
3233 if (fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_OUT) {
3234 return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
3237 if (r != HANDLER_GO_ON) return r;
3241 return (0 == hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
3242 ? fcgi_send_request(srv, hctx)
3243 : HANDLER_WAIT_FOR_EVENT;
3247 static handler_t fcgi_recv_response(server *srv, handler_ctx *hctx) {
3248 connection *con = hctx->remote_conn;
3249 plugin_data *p = hctx->plugin_data;
3251 fcgi_proc *proc = hctx->proc;
3252 fcgi_extension_host *host= hctx->host;
3254 switch (fcgi_demux_response(srv, hctx)) {
3255 case 0:
3256 break;
3257 case 1:
3259 if (host->mode == FCGI_AUTHORIZER &&
3260 (con->http_status == 200 ||
3261 con->http_status == 0)) {
3263 * If we are here in AUTHORIZER mode then a request for authorizer
3264 * was processed already, and status 200 has been returned. We need
3265 * now to handle authorized request.
3268 buffer_copy_buffer(con->physical.doc_root, host->docroot);
3269 buffer_copy_buffer(con->physical.basedir, host->docroot);
3271 buffer_copy_buffer(con->physical.path, host->docroot);
3272 buffer_append_string_buffer(con->physical.path, con->uri.path);
3274 con->mode = DIRECT;/*(avoid changing con->state, con->http_status)*/
3275 fcgi_connection_close(srv, hctx);
3276 con->http_status = 0;
3277 con->file_started = 1; /* fcgi_extension won't touch the request afterwards */
3278 } else {
3279 /* we are done */
3280 fcgi_connection_close(srv, hctx);
3283 return HANDLER_FINISHED;
3284 case -1:
3285 if (proc->pid && proc->state != PROC_STATE_DIED) {
3286 int status;
3288 /* only fetch the zombie if it is not already done */
3290 switch(waitpid(proc->pid, &status, WNOHANG)) {
3291 case 0:
3292 /* child is still alive */
3293 break;
3294 case -1:
3295 break;
3296 default:
3297 /* the child should not terminate at all */
3298 if (WIFEXITED(status)) {
3299 log_error_write(srv, __FILE__, __LINE__, "sdsd",
3300 "child exited, pid:", proc->pid,
3301 "status:", WEXITSTATUS(status));
3302 } else if (WIFSIGNALED(status)) {
3303 log_error_write(srv, __FILE__, __LINE__, "sd",
3304 "child signaled:",
3305 WTERMSIG(status));
3306 } else {
3307 log_error_write(srv, __FILE__, __LINE__, "sd",
3308 "child died somehow:",
3309 status);
3312 if (p->conf.debug) {
3313 log_error_write(srv, __FILE__, __LINE__, "ssbsdsd",
3314 "--- fastcgi spawning",
3315 "\n\tsocket", proc->connection_name,
3316 "\n\tcurrent:", 1, "/", host->max_procs);
3319 if (fcgi_spawn_connection(srv, p, host, proc)) {
3320 /* respawning failed, retry later */
3321 proc->state = PROC_STATE_DIED;
3323 log_error_write(srv, __FILE__, __LINE__, "s",
3324 "respawning failed, will retry later");
3327 break;
3331 if (con->file_started == 0) {
3332 /* nothing has been sent out yet, try to use another child */
3334 if (hctx->wb->bytes_out == 0 &&
3335 hctx->reconnects < 5) {
3337 log_error_write(srv, __FILE__, __LINE__, "ssbsBSBs",
3338 "response not received, request not sent",
3339 "on socket:", proc->connection_name,
3340 "for", con->uri.path, "?", con->uri.query, ", reconnecting");
3342 fcgi_reconnect(srv, hctx);
3344 return HANDLER_COMEBACK;
3347 log_error_write(srv, __FILE__, __LINE__, "sosbsBSBs",
3348 "response not received, request sent:", hctx->wb->bytes_out,
3349 "on socket:", proc->connection_name,
3350 "for", con->uri.path, "?", con->uri.query, ", closing connection");
3351 } else {
3352 log_error_write(srv, __FILE__, __LINE__, "ssbsBSBs",
3353 "response already sent out, but backend returned error",
3354 "on socket:", proc->connection_name,
3355 "for", con->uri.path, "?", con->uri.query, ", terminating connection");
3358 http_response_backend_error(srv, con);
3359 fcgi_connection_close(srv, hctx);
3360 return HANDLER_FINISHED;
3363 return HANDLER_GO_ON;
3367 static handler_t fcgi_handle_fdevent(server *srv, void *ctx, int revents) {
3368 handler_ctx *hctx = ctx;
3369 connection *con = hctx->remote_conn;
3371 joblist_append(srv, con);
3373 if (revents & FDEVENT_IN) {
3374 handler_t rc = fcgi_recv_response(srv, hctx);/*(might invalidate hctx)*/
3375 if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
3378 if (revents & FDEVENT_OUT) {
3379 return fcgi_send_request(srv, hctx); /*(might invalidate hctx)*/
3382 /* perhaps this issue is already handled */
3383 if (revents & FDEVENT_HUP) {
3384 if (hctx->state == FCGI_STATE_CONNECT_DELAYED) {
3385 /* getoptsock will catch this one (right ?)
3387 * if we are in connect we might get an EINPROGRESS
3388 * in the first call and an FDEVENT_HUP in the
3389 * second round
3391 * FIXME: as it is a bit ugly.
3394 fcgi_send_request(srv, hctx);
3395 } else if (con->file_started) {
3396 /* drain any remaining data from kernel pipe buffers
3397 * even if (con->conf.stream_response_body
3398 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
3399 * since event loop will spin on fd FDEVENT_HUP event
3400 * until unregistered. */
3401 handler_t rc;
3402 do {
3403 rc = fcgi_recv_response(srv,hctx);/*(might invalidate hctx)*/
3404 } while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
3405 return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
3406 } else {
3407 fcgi_proc *proc = hctx->proc;
3408 log_error_write(srv, __FILE__, __LINE__, "sBSbsbsd",
3409 "error: unexpected close of fastcgi connection for",
3410 con->uri.path, "?", con->uri.query,
3411 "(no fastcgi process on socket:", proc->connection_name, "?)",
3412 hctx->state);
3414 fcgi_connection_close(srv, hctx);
3416 } else if (revents & FDEVENT_ERR) {
3417 log_error_write(srv, __FILE__, __LINE__, "s",
3418 "fcgi: got a FDEVENT_ERR. Don't know why.");
3420 http_response_backend_error(srv, con);
3421 fcgi_connection_close(srv, hctx);
3424 return HANDLER_FINISHED;
3427 #define PATCH(x) \
3428 p->conf.x = s->x;
3429 static int fcgi_patch_connection(server *srv, connection *con, plugin_data *p) {
3430 size_t i, j;
3431 plugin_config *s = p->config_storage[0];
3433 PATCH(exts);
3434 PATCH(debug);
3435 PATCH(ext_mapping);
3437 /* skip the first, the global context */
3438 for (i = 1; i < srv->config_context->used; i++) {
3439 data_config *dc = (data_config *)srv->config_context->data[i];
3440 s = p->config_storage[i];
3442 /* condition didn't match */
3443 if (!config_check_cond(srv, con, dc)) continue;
3445 /* merge config */
3446 for (j = 0; j < dc->value->used; j++) {
3447 data_unset *du = dc->value->data[j];
3449 if (buffer_is_equal_string(du->key, CONST_STR_LEN("fastcgi.server"))) {
3450 PATCH(exts);
3451 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("fastcgi.debug"))) {
3452 PATCH(debug);
3453 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("fastcgi.map-extensions"))) {
3454 PATCH(ext_mapping);
3459 return 0;
3461 #undef PATCH
3464 static handler_t fcgi_check_extension(server *srv, connection *con, void *p_d, int uri_path_handler) {
3465 plugin_data *p = p_d;
3466 size_t s_len;
3467 size_t k;
3468 buffer *fn;
3469 fcgi_extension *extension = NULL;
3470 fcgi_extension_host *host = NULL;
3472 if (con->mode != DIRECT) return HANDLER_GO_ON;
3474 /* Possibly, we processed already this request */
3475 if (con->file_started == 1) return HANDLER_GO_ON;
3477 fn = uri_path_handler ? con->uri.path : con->physical.path;
3479 if (buffer_string_is_empty(fn)) return HANDLER_GO_ON;
3481 s_len = buffer_string_length(fn);
3483 fcgi_patch_connection(srv, con, p);
3485 /* fastcgi.map-extensions maps extensions to existing fastcgi.server entries
3487 * fastcgi.map-extensions = ( ".php3" => ".php" )
3489 * fastcgi.server = ( ".php" => ... )
3491 * */
3493 /* check if extension-mapping matches */
3494 for (k = 0; k < p->conf.ext_mapping->used; k++) {
3495 data_string *ds = (data_string *)p->conf.ext_mapping->data[k];
3496 size_t ct_len; /* length of the config entry */
3498 if (buffer_is_empty(ds->key)) continue;
3500 ct_len = buffer_string_length(ds->key);
3502 if (s_len < ct_len) continue;
3504 /* found a mapping */
3505 if (0 == strncmp(fn->ptr + s_len - ct_len, ds->key->ptr, ct_len)) {
3506 /* check if we know the extension */
3508 /* we can reuse k here */
3509 for (k = 0; k < p->conf.exts->used; k++) {
3510 extension = p->conf.exts->exts[k];
3512 if (buffer_is_equal(ds->value, extension->key)) {
3513 break;
3517 if (k == p->conf.exts->used) {
3518 /* found nothign */
3519 extension = NULL;
3521 break;
3525 if (extension == NULL) {
3526 size_t uri_path_len = buffer_string_length(con->uri.path);
3528 /* check if extension matches */
3529 for (k = 0; k < p->conf.exts->used; k++) {
3530 size_t ct_len; /* length of the config entry */
3531 fcgi_extension *ext = p->conf.exts->exts[k];
3533 if (buffer_is_empty(ext->key)) continue;
3535 ct_len = buffer_string_length(ext->key);
3537 /* check _url_ in the form "/fcgi_pattern" */
3538 if (ext->key->ptr[0] == '/') {
3539 if ((ct_len <= uri_path_len) &&
3540 (strncmp(con->uri.path->ptr, ext->key->ptr, ct_len) == 0)) {
3541 extension = ext;
3542 break;
3544 } else if ((ct_len <= s_len) && (0 == strncmp(fn->ptr + s_len - ct_len, ext->key->ptr, ct_len))) {
3545 /* check extension in the form ".fcg" */
3546 extension = ext;
3547 break;
3550 /* extension doesn't match */
3551 if (NULL == extension) {
3552 return HANDLER_GO_ON;
3556 /* check if we have at least one server for this extension up and running */
3557 for (k = 0; k < extension->used; k++) {
3558 fcgi_extension_host *h = extension->hosts[k];
3560 /* we should have at least one proc that can do something */
3561 if (h->active_procs == 0) {
3562 continue;
3565 /* we found one host that is alive */
3566 host = h;
3567 break;
3570 if (!host) {
3571 /* sorry, we don't have a server alive for this ext */
3572 con->http_status = 500;
3573 con->mode = DIRECT;
3575 /* only send the 'no handler' once */
3576 if (!extension->note_is_sent) {
3577 extension->note_is_sent = 1;
3579 log_error_write(srv, __FILE__, __LINE__, "sBSbsbs",
3580 "all handlers for", con->uri.path, "?", con->uri.query,
3581 "on", extension->key,
3582 "are down.");
3585 return HANDLER_FINISHED;
3588 /* a note about no handler is not sent yet */
3589 extension->note_is_sent = 0;
3592 * if check-local is disabled, use the uri.path handler
3596 /* init handler-context */
3597 if (uri_path_handler) {
3598 if (host->check_local == 0) {
3599 handler_ctx *hctx;
3600 char *pathinfo;
3602 hctx = handler_ctx_init();
3604 hctx->remote_conn = con;
3605 hctx->plugin_data = p;
3606 hctx->proc = NULL;
3607 hctx->ext = extension;
3610 hctx->conf.exts = p->conf.exts;
3611 hctx->conf.debug = p->conf.debug;
3613 con->plugin_ctx[p->id] = hctx;
3615 con->mode = p->id;
3617 if (con->conf.log_request_handling) {
3618 log_error_write(srv, __FILE__, __LINE__, "s",
3619 "handling it in mod_fastcgi");
3622 /* do not split path info for authorizer */
3623 if (host->mode != FCGI_AUTHORIZER) {
3624 /* the prefix is the SCRIPT_NAME,
3625 * everything from start to the next slash
3626 * this is important for check-local = "disable"
3628 * if prefix = /admin.fcgi
3630 * /admin.fcgi/foo/bar
3632 * SCRIPT_NAME = /admin.fcgi
3633 * PATH_INFO = /foo/bar
3635 * if prefix = /fcgi-bin/
3637 * /fcgi-bin/foo/bar
3639 * SCRIPT_NAME = /fcgi-bin/foo
3640 * PATH_INFO = /bar
3642 * if prefix = /, and fix-root-path-name is enable
3644 * /fcgi-bin/foo/bar
3646 * SCRIPT_NAME = /fcgi-bin/foo
3647 * PATH_INFO = /bar
3651 /* the rewrite is only done for /prefix/? matches */
3652 if (host->fix_root_path_name && extension->key->ptr[0] == '/' && extension->key->ptr[1] == '\0') {
3653 buffer_copy_string(con->request.pathinfo, con->uri.path->ptr);
3654 buffer_string_set_length(con->uri.path, 0);
3655 } else if (extension->key->ptr[0] == '/' &&
3656 buffer_string_length(con->uri.path) > buffer_string_length(extension->key) &&
3657 NULL != (pathinfo = strchr(con->uri.path->ptr + buffer_string_length(extension->key), '/'))) {
3658 /* rewrite uri.path and pathinfo */
3660 buffer_copy_string(con->request.pathinfo, pathinfo);
3661 buffer_string_set_length(con->uri.path, buffer_string_length(con->uri.path) - buffer_string_length(con->request.pathinfo));
3665 } else {
3666 handler_ctx *hctx;
3667 hctx = handler_ctx_init();
3669 hctx->remote_conn = con;
3670 hctx->plugin_data = p;
3671 hctx->proc = NULL;
3672 hctx->ext = extension;
3674 hctx->conf.exts = p->conf.exts;
3675 hctx->conf.debug = p->conf.debug;
3677 con->plugin_ctx[p->id] = hctx;
3679 con->mode = p->id;
3681 if (con->conf.log_request_handling) {
3682 log_error_write(srv, __FILE__, __LINE__, "s", "handling it in mod_fastcgi");
3686 return HANDLER_GO_ON;
3689 /* uri-path handler */
3690 static handler_t fcgi_check_extension_1(server *srv, connection *con, void *p_d) {
3691 return fcgi_check_extension(srv, con, p_d, 1);
3694 /* start request handler */
3695 static handler_t fcgi_check_extension_2(server *srv, connection *con, void *p_d) {
3696 return fcgi_check_extension(srv, con, p_d, 0);
3700 TRIGGER_FUNC(mod_fastcgi_handle_trigger) {
3701 plugin_data *p = p_d;
3702 size_t i, j, n;
3705 /* perhaps we should kill a connect attempt after 10-15 seconds
3707 * currently we wait for the TCP timeout which is 180 seconds on Linux
3713 /* check all children if they are still up */
3715 for (i = 0; i < srv->config_context->used; i++) {
3716 plugin_config *conf;
3717 fcgi_exts *exts;
3719 conf = p->config_storage[i];
3721 exts = conf->exts;
3723 for (j = 0; j < exts->used; j++) {
3724 fcgi_extension *ex;
3726 ex = exts->exts[j];
3728 for (n = 0; n < ex->used; n++) {
3730 fcgi_proc *proc;
3731 fcgi_extension_host *host;
3733 host = ex->hosts[n];
3735 fcgi_restart_dead_procs(srv, p, host);
3737 for (proc = host->unused_procs; proc; proc = proc->next) {
3738 int status;
3740 if (proc->pid == 0) continue;
3742 switch (waitpid(proc->pid, &status, WNOHANG)) {
3743 case 0:
3744 /* child still running after timeout, good */
3745 break;
3746 case -1:
3747 if (errno != EINTR) {
3748 /* no PID found ? should never happen */
3749 log_error_write(srv, __FILE__, __LINE__, "sddss",
3750 "pid ", proc->pid, proc->state,
3751 "not found:", strerror(errno));
3753 #if 0
3754 if (errno == ECHILD) {
3755 /* someone else has cleaned up for us */
3756 proc->pid = 0;
3757 proc->state = PROC_STATE_UNSET;
3759 #endif
3761 break;
3762 default:
3763 /* the child should not terminate at all */
3764 if (WIFEXITED(status)) {
3765 if (proc->state != PROC_STATE_KILLED) {
3766 log_error_write(srv, __FILE__, __LINE__, "sdb",
3767 "child exited:",
3768 WEXITSTATUS(status), proc->connection_name);
3770 } else if (WIFSIGNALED(status)) {
3771 if (WTERMSIG(status) != SIGTERM) {
3772 log_error_write(srv, __FILE__, __LINE__, "sd",
3773 "child signaled:",
3774 WTERMSIG(status));
3776 } else {
3777 log_error_write(srv, __FILE__, __LINE__, "sd",
3778 "child died somehow:",
3779 status);
3781 proc->pid = 0;
3782 if (proc->state == PROC_STATE_RUNNING) host->active_procs--;
3783 proc->state = PROC_STATE_UNSET;
3784 host->max_id--;
3791 return HANDLER_GO_ON;
3795 int mod_fastcgi_plugin_init(plugin *p);
3796 int mod_fastcgi_plugin_init(plugin *p) {
3797 p->version = LIGHTTPD_VERSION_ID;
3798 p->name = buffer_init_string("fastcgi");
3800 p->init = mod_fastcgi_init;
3801 p->cleanup = mod_fastcgi_free;
3802 p->set_defaults = mod_fastcgi_set_defaults;
3803 p->connection_reset = fcgi_connection_reset;
3804 p->handle_connection_close = fcgi_connection_reset;
3805 p->handle_uri_clean = fcgi_check_extension_1;
3806 p->handle_subrequest_start = fcgi_check_extension_2;
3807 p->handle_subrequest = mod_fastcgi_handle_subrequest;
3808 p->handle_trigger = mod_fastcgi_handle_trigger;
3810 p->data = NULL;
3812 return 0;