[mod_accesslog] %{ratio}n logs compression ratio (fixes #2133)
[lighttpd.git] / src / mod_scgi.c
blob0adf12c4464e24baf9e371a4919860b801797fe1
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"
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <assert.h>
26 #include <signal.h>
28 #include <stdio.h>
30 #include "sys-socket.h"
31 #include "sys-endian.h"
33 #ifdef HAVE_SYS_UIO_H
34 # include <sys/uio.h>
35 #endif
37 #ifdef HAVE_SYS_WAIT_H
38 # include <sys/wait.h>
39 #endif
41 enum {EOL_UNSET, EOL_N, EOL_RN};
45 * TODO:
47 * - add timeout for a connect to a non-scgi process
48 * (use state_timestamp + state)
52 typedef struct scgi_proc {
53 size_t id; /* id will be between 1 and max_procs */
54 buffer *socket; /* config.socket + "-" + id */
55 unsigned port; /* config.port + pno */
57 pid_t pid; /* PID of the spawned process (0 if not spawned locally) */
60 size_t load; /* number of requests waiting on this process */
62 time_t last_used; /* see idle_timeout */
63 size_t requests; /* see max_requests */
64 struct scgi_proc *prev, *next; /* see first */
66 time_t disable_ts; /* replace by host->something */
68 int is_local;
70 enum { PROC_STATE_UNSET, /* init-phase */
71 PROC_STATE_RUNNING, /* alive */
72 PROC_STATE_DIED_WAIT_FOR_PID,
73 PROC_STATE_KILLED, /* was killed as we don't have the load anymore */
74 PROC_STATE_DIED, /* marked as dead, should be restarted */
75 PROC_STATE_DISABLED /* proc disabled as it resulted in an error */
76 } state;
77 } scgi_proc;
79 typedef struct {
80 /* list of processes handling this extension
81 * sorted by lowest load
83 * whenever a job is done move it up in the list
84 * until it is sorted, move it down as soon as the
85 * job is started
87 scgi_proc *first;
88 scgi_proc *unused_procs;
91 * spawn at least min_procs, at max_procs.
93 * as soon as the load of the first entry
94 * is max_load_per_proc we spawn a new one
95 * and add it to the first entry and give it
96 * the load
100 unsigned short min_procs;
101 unsigned short max_procs;
102 size_t num_procs; /* how many procs are started */
103 size_t active_procs; /* how many of them are really running */
105 unsigned short max_load_per_proc;
108 * kick the process from the list if it was not
109 * used for idle_timeout until min_procs is
110 * reached. this helps to get the processlist
111 * small again we had a small peak load.
115 unsigned short idle_timeout;
118 * time after a disabled remote connection is tried to be re-enabled
123 unsigned short disable_time;
126 * same scgi processes get a little bit larger
127 * than wanted. max_requests_per_proc kills a
128 * process after a number of handled requests.
131 size_t max_requests_per_proc;
134 /* config */
137 * host:port
139 * if host is one of the local IP adresses the
140 * whole connection is local
142 * if tcp/ip should be used host AND port have
143 * to be specified
146 buffer *host;
147 unsigned short port;
148 sa_family_t family;
151 * Unix Domain Socket
153 * instead of TCP/IP we can use Unix Domain Sockets
154 * - more secure (you have fileperms to play with)
155 * - more control (on locally)
156 * - more speed (no extra overhead)
158 buffer *unixsocket;
160 /* if socket is local we can start the scgi
161 * process ourself
163 * bin-path is the path to the binary
165 * check min_procs and max_procs for the number
166 * of process to start-up
168 buffer *bin_path;
170 /* bin-path is set bin-environment is taken to
171 * create the environement before starting the
172 * FastCGI process
175 array *bin_env;
177 array *bin_env_copy;
180 * docroot-translation between URL->phys and the
181 * remote host
183 * reasons:
184 * - different dir-layout if remote
185 * - chroot if local
188 buffer *docroot;
191 * check_local tell you if the phys file is stat()ed
192 * or not. FastCGI doesn't care if the service is
193 * remote. If the web-server side doesn't contain
194 * the scgi-files we should not stat() for them
195 * and say '404 not found'.
197 unsigned short check_local;
200 * append PATH_INFO to SCRIPT_FILENAME
202 * php needs this if cgi.fix_pathinfo is provied
207 * workaround for program when prefix="/"
209 * rule to build PATH_INFO is hardcoded for when check_local is disabled
210 * enable this option to use the workaround
214 unsigned short fix_root_path_name;
217 * If the backend includes X-Sendfile in the response
218 * we use the value as filename and ignore the content.
221 unsigned short xsendfile_allow;
222 array *xsendfile_docroot;
224 ssize_t load; /* replace by host->load */
226 size_t max_id; /* corresponds most of the time to
227 num_procs.
229 only if a process is killed max_id waits for the process itself
230 to die and decrements its afterwards */
232 int listen_backlog;
233 int refcount;
234 } scgi_extension_host;
237 * one extension can have multiple hosts assigned
238 * one host can spawn additional processes on the same
239 * socket (if we control it)
241 * ext -> host -> procs
242 * 1:n 1:n
244 * if the scgi process is remote that whole goes down
245 * to
247 * ext -> host -> procs
248 * 1:n 1:1
250 * in case of PHP and FCGI_CHILDREN we have again a procs
251 * but we don't control it directly.
255 typedef struct {
256 buffer *key; /* like .php */
258 int note_is_sent;
259 scgi_extension_host **hosts;
261 size_t used;
262 size_t size;
263 } scgi_extension;
265 typedef struct {
266 scgi_extension **exts;
268 size_t used;
269 size_t size;
270 } scgi_exts;
272 enum { LI_PROTOCOL_SCGI, LI_PROTOCOL_UWSGI };
274 typedef struct {
275 scgi_exts *exts;
277 int proto;
278 int debug;
279 } plugin_config;
281 typedef struct {
282 char **ptr;
284 size_t size;
285 size_t used;
286 } char_array;
288 /* generic plugin data, shared between all connections */
289 typedef struct {
290 PLUGIN_DATA;
292 buffer *scgi_env;
294 buffer *path;
295 buffer *parse_response;
297 plugin_config **config_storage;
299 plugin_config conf; /* this is only used as long as no handler_ctx is setup */
300 } plugin_data;
302 /* connection specific data */
303 typedef enum { FCGI_STATE_INIT, FCGI_STATE_CONNECT, FCGI_STATE_PREPARE_WRITE,
304 FCGI_STATE_WRITE, FCGI_STATE_READ
305 } scgi_connection_state_t;
307 typedef struct {
308 buffer *response;
310 scgi_proc *proc;
311 scgi_extension_host *host;
313 scgi_connection_state_t state;
314 time_t state_timestamp;
316 chunkqueue *wb;
317 off_t wb_reqlen;
319 buffer *response_header;
321 int fd; /* fd to the scgi process */
322 int fde_ndx; /* index into the fd-event buffer */
324 pid_t pid;
325 int got_proc;
326 int reconnects; /* number of reconnect attempts */
328 plugin_config conf;
330 connection *remote_conn; /* dumb pointer */
331 plugin_data *plugin_data; /* dumb pointer */
332 } handler_ctx;
335 /* ok, we need a prototype */
336 static handler_t scgi_handle_fdevent(server *srv, void *ctx, int revents);
338 int scgi_proclist_sort_down(server *srv, scgi_extension_host *host, scgi_proc *proc);
340 static void reset_signals(void) {
341 #ifdef SIGTTOU
342 signal(SIGTTOU, SIG_DFL);
343 #endif
344 #ifdef SIGTTIN
345 signal(SIGTTIN, SIG_DFL);
346 #endif
347 #ifdef SIGTSTP
348 signal(SIGTSTP, SIG_DFL);
349 #endif
350 signal(SIGHUP, SIG_DFL);
351 signal(SIGPIPE, SIG_DFL);
352 signal(SIGUSR1, SIG_DFL);
355 static handler_ctx * handler_ctx_init(void) {
356 handler_ctx * hctx;
358 hctx = calloc(1, sizeof(*hctx));
359 force_assert(hctx);
361 hctx->fde_ndx = -1;
363 hctx->response = buffer_init();
364 hctx->response_header = buffer_init();
366 hctx->state = FCGI_STATE_INIT;
367 hctx->proc = NULL;
369 hctx->fd = -1;
371 hctx->reconnects = 0;
373 hctx->wb = chunkqueue_init();
374 hctx->wb_reqlen = 0;
376 return hctx;
379 static void handler_ctx_free(handler_ctx *hctx) {
380 buffer_free(hctx->response);
381 buffer_free(hctx->response_header);
383 chunkqueue_free(hctx->wb);
385 free(hctx);
388 static scgi_proc *scgi_process_init(void) {
389 scgi_proc *f;
391 f = calloc(1, sizeof(*f));
392 force_assert(f);
393 f->socket = buffer_init();
395 f->prev = NULL;
396 f->next = NULL;
398 return f;
401 static void scgi_process_free(scgi_proc *f) {
402 if (!f) return;
404 scgi_process_free(f->next);
406 buffer_free(f->socket);
408 free(f);
411 static scgi_extension_host *scgi_host_init(void) {
412 scgi_extension_host *f;
414 f = calloc(1, sizeof(*f));
416 f->host = buffer_init();
417 f->unixsocket = buffer_init();
418 f->docroot = buffer_init();
419 f->bin_path = buffer_init();
420 f->bin_env = array_init();
421 f->bin_env_copy = array_init();
422 f->xsendfile_docroot = array_init();
424 return f;
427 static void scgi_host_free(scgi_extension_host *h) {
428 if (!h) return;
429 if (h->refcount) {
430 --h->refcount;
431 return;
434 buffer_free(h->host);
435 buffer_free(h->unixsocket);
436 buffer_free(h->docroot);
437 buffer_free(h->bin_path);
438 array_free(h->bin_env);
439 array_free(h->bin_env_copy);
440 array_free(h->xsendfile_docroot);
442 scgi_process_free(h->first);
443 scgi_process_free(h->unused_procs);
445 free(h);
449 static scgi_exts *scgi_extensions_init(void) {
450 scgi_exts *f;
452 f = calloc(1, sizeof(*f));
453 force_assert(f);
455 return f;
458 static void scgi_extensions_free(scgi_exts *f) {
459 size_t i;
461 if (!f) return;
463 for (i = 0; i < f->used; i++) {
464 scgi_extension *fe;
465 size_t j;
467 fe = f->exts[i];
469 for (j = 0; j < fe->used; j++) {
470 scgi_extension_host *h;
472 h = fe->hosts[j];
474 scgi_host_free(h);
477 buffer_free(fe->key);
478 free(fe->hosts);
480 free(fe);
483 free(f->exts);
485 free(f);
488 static int scgi_extension_insert(scgi_exts *ext, buffer *key, scgi_extension_host *fh) {
489 scgi_extension *fe;
490 size_t i;
492 /* there is something */
494 for (i = 0; i < ext->used; i++) {
495 if (buffer_is_equal(key, ext->exts[i]->key)) {
496 break;
500 if (i == ext->used) {
501 /* filextension is new */
502 fe = calloc(1, sizeof(*fe));
503 force_assert(fe);
504 fe->key = buffer_init();
505 buffer_copy_buffer(fe->key, key);
507 /* */
509 if (ext->size == 0) {
510 ext->size = 8;
511 ext->exts = malloc(ext->size * sizeof(*(ext->exts)));
512 force_assert(ext->exts);
513 } else if (ext->used == ext->size) {
514 ext->size += 8;
515 ext->exts = realloc(ext->exts, ext->size * sizeof(*(ext->exts)));
516 force_assert(ext->exts);
518 ext->exts[ext->used++] = fe;
519 } else {
520 fe = ext->exts[i];
523 if (fe->size == 0) {
524 fe->size = 4;
525 fe->hosts = malloc(fe->size * sizeof(*(fe->hosts)));
526 force_assert(fe->hosts);
527 } else if (fe->size == fe->used) {
528 fe->size += 4;
529 fe->hosts = realloc(fe->hosts, fe->size * sizeof(*(fe->hosts)));
530 force_assert(fe->hosts);
533 fe->hosts[fe->used++] = fh;
535 return 0;
539 INIT_FUNC(mod_scgi_init) {
540 plugin_data *p;
542 p = calloc(1, sizeof(*p));
543 force_assert(p);
545 p->scgi_env = buffer_init();
547 p->path = buffer_init();
548 p->parse_response = buffer_init();
550 return p;
554 FREE_FUNC(mod_scgi_free) {
555 plugin_data *p = p_d;
557 UNUSED(srv);
559 buffer_free(p->scgi_env);
560 buffer_free(p->path);
561 buffer_free(p->parse_response);
563 if (p->config_storage) {
564 size_t i, j, n;
565 for (i = 0; i < srv->config_context->used; i++) {
566 plugin_config *s = p->config_storage[i];
567 scgi_exts *exts;
569 if (NULL == s) continue;
571 exts = s->exts;
573 for (j = 0; j < exts->used; j++) {
574 scgi_extension *ex;
576 ex = exts->exts[j];
578 for (n = 0; n < ex->used; n++) {
579 scgi_proc *proc;
580 scgi_extension_host *host;
582 host = ex->hosts[n];
584 for (proc = host->first; proc; proc = proc->next) {
585 if (proc->pid != 0) kill(proc->pid, SIGTERM);
587 if (proc->is_local &&
588 !buffer_string_is_empty(proc->socket)) {
589 unlink(proc->socket->ptr);
593 for (proc = host->unused_procs; proc; proc = proc->next) {
594 if (proc->pid != 0) kill(proc->pid, SIGTERM);
596 if (proc->is_local &&
597 !buffer_string_is_empty(proc->socket)) {
598 unlink(proc->socket->ptr);
604 scgi_extensions_free(s->exts);
606 free(s);
608 free(p->config_storage);
611 free(p);
613 return HANDLER_GO_ON;
616 static int env_add(char_array *env, const char *key, size_t key_len, const char *val, size_t val_len) {
617 char *dst;
618 size_t i;
620 if (!key || !val) return -1;
622 dst = malloc(key_len + val_len + 3);
623 force_assert(dst);
624 memcpy(dst, key, key_len);
625 dst[key_len] = '=';
626 /* add the \0 from the value */
627 memcpy(dst + key_len + 1, val, val_len + 1);
629 for (i = 0; i < env->used; i++) {
630 if (0 == strncmp(dst, env->ptr[i], key_len + 1)) {
631 /* don't care about free as we are in a forked child which is going to exec(...) */
632 /* free(env->ptr[i]); */
633 env->ptr[i] = dst;
634 return 0;
638 if (env->size == 0) {
639 env->size = 16;
640 env->ptr = malloc(env->size * sizeof(*env->ptr));
641 force_assert(env->ptr);
642 } else if (env->size == env->used) {
643 env->size += 16;
644 env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
645 force_assert(env->ptr);
648 env->ptr[env->used++] = dst;
650 return 0;
653 #if !defined(HAVE_FORK)
654 static int scgi_spawn_connection(server *srv,
655 plugin_data *p,
656 scgi_extension_host *host,
657 scgi_proc *proc) {
658 UNUSED(srv);
659 UNUSED(p);
660 UNUSED(host);
661 UNUSED(proc);
662 return -1;
665 #else /* -> defined(HAVE_FORK) */
667 static int scgi_spawn_connection(server *srv,
668 plugin_data *p,
669 scgi_extension_host *host,
670 scgi_proc *proc) {
671 int scgi_fd;
672 int status;
673 struct timeval tv = { 0, 100 * 1000 };
674 #ifdef HAVE_SYS_UN_H
675 struct sockaddr_un scgi_addr_un;
676 #endif
677 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
678 struct sockaddr_in6 scgi_addr_in6;
679 #endif
680 struct sockaddr_in scgi_addr_in;
681 struct sockaddr *scgi_addr;
683 socklen_t servlen;
685 if (p->conf.debug) {
686 log_error_write(srv, __FILE__, __LINE__, "sdb",
687 "new proc, socket:", proc->port, proc->socket);
691 if (!buffer_string_is_empty(proc->socket)) {
692 #ifdef HAVE_SYS_UN_H
693 memset(&scgi_addr_un, 0, sizeof(scgi_addr_un));
694 scgi_addr_un.sun_family = AF_UNIX;
695 if (buffer_string_length(proc->socket) + 1 > sizeof(scgi_addr_un.sun_path)) {
696 log_error_write(srv, __FILE__, __LINE__, "sB",
697 "ERROR: Unix Domain socket filename too long:",
698 proc->socket);
699 return -1;
701 memcpy(scgi_addr_un.sun_path, proc->socket->ptr, buffer_string_length(proc->socket) + 1);
703 #ifdef SUN_LEN
704 servlen = SUN_LEN(&scgi_addr_un);
705 #else
706 /* stevens says: */
707 servlen = buffer_string_length(proc->socket) + 1 + sizeof(scgi_addr_un.sun_family);
708 #endif
709 scgi_addr = (struct sockaddr *) &scgi_addr_un;
710 #else
711 log_error_write(srv, __FILE__, __LINE__, "s",
712 "ERROR: Unix Domain sockets are not supported.");
713 return -1;
714 #endif
715 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
716 } else if (host->family == AF_INET6 && !buffer_string_is_empty(host->host)) {
717 memset(&scgi_addr_in6, 0, sizeof(scgi_addr_in6));
718 scgi_addr_in6.sin6_family = AF_INET6;
719 inet_pton(AF_INET6, host->host->ptr, (char *) &scgi_addr_in6.sin6_addr);
720 scgi_addr_in6.sin6_port = htons(proc->port);
721 servlen = sizeof(scgi_addr_in6);
722 scgi_addr = (struct sockaddr *) &scgi_addr_in6;
723 #endif
724 } else {
725 memset(&scgi_addr_in, 0, sizeof(scgi_addr_in));
726 scgi_addr_in.sin_family = AF_INET;
728 if (buffer_string_is_empty(host->host)) {
729 scgi_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
730 } else {
731 struct hostent *he;
733 /* set a usefull default */
734 scgi_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
737 if (NULL == (he = gethostbyname(host->host->ptr))) {
738 log_error_write(srv, __FILE__, __LINE__,
739 "sdb", "gethostbyname failed: ",
740 h_errno, host->host);
741 return -1;
744 if (he->h_addrtype != AF_INET) {
745 log_error_write(srv, __FILE__, __LINE__, "sd", "addr-type != AF_INET: ", he->h_addrtype);
746 return -1;
749 if (he->h_length != sizeof(struct in_addr)) {
750 log_error_write(srv, __FILE__, __LINE__, "sd", "addr-length != sizeof(in_addr): ", he->h_length);
751 return -1;
754 memcpy(&(scgi_addr_in.sin_addr.s_addr), he->h_addr_list[0], he->h_length);
757 scgi_addr_in.sin_port = htons(proc->port);
758 servlen = sizeof(scgi_addr_in);
760 scgi_addr = (struct sockaddr *) &scgi_addr_in;
763 if (-1 == (scgi_fd = fdevent_socket_cloexec(scgi_addr->sa_family, SOCK_STREAM, 0))) {
764 log_error_write(srv, __FILE__, __LINE__, "ss",
765 "failed:", strerror(errno));
766 return -1;
769 if (-1 == connect(scgi_fd, scgi_addr, servlen)) {
770 /* server is not up, spawn in */
771 pid_t child;
772 int val;
774 if (!buffer_string_is_empty(proc->socket)) {
775 unlink(proc->socket->ptr);
778 close(scgi_fd);
780 /* reopen socket */
781 if (-1 == (scgi_fd = fdevent_socket_cloexec(scgi_addr->sa_family, SOCK_STREAM, 0))) {
782 log_error_write(srv, __FILE__, __LINE__, "ss",
783 "socket failed:", strerror(errno));
784 return -1;
787 val = 1;
788 if (setsockopt(scgi_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
789 log_error_write(srv, __FILE__, __LINE__, "ss",
790 "socketsockopt failed:", strerror(errno));
791 close(scgi_fd);
792 return -1;
795 /* create socket */
796 if (-1 == bind(scgi_fd, scgi_addr, servlen)) {
797 log_error_write(srv, __FILE__, __LINE__, "sbds",
798 "bind failed for:",
799 proc->socket,
800 proc->port,
801 strerror(errno));
802 close(scgi_fd);
803 return -1;
806 if (-1 == listen(scgi_fd, host->listen_backlog)) {
807 log_error_write(srv, __FILE__, __LINE__, "ss",
808 "listen failed:", strerror(errno));
809 close(scgi_fd);
810 return -1;
813 switch ((child = fork())) {
814 case 0: {
815 buffer *b;
816 size_t i = 0;
817 int fd = 0;
818 char_array env;
821 /* create environment */
822 env.ptr = NULL;
823 env.size = 0;
824 env.used = 0;
826 if (scgi_fd != 0) {
827 dup2(scgi_fd, 0);
828 close(scgi_fd);
830 #ifdef SOCK_CLOEXEC
831 else
832 (void)fcntl(scgi_fd, F_SETFD, 0); /* clear cloexec */
833 #endif
835 /* we don't need the client socket */
836 for (fd = 3; fd < 256; fd++) {
837 close(fd);
840 /* build clean environment */
841 if (host->bin_env_copy->used) {
842 for (i = 0; i < host->bin_env_copy->used; i++) {
843 data_string *ds = (data_string *)host->bin_env_copy->data[i];
844 char *ge;
846 if (NULL != (ge = getenv(ds->value->ptr))) {
847 env_add(&env, CONST_BUF_LEN(ds->value), ge, strlen(ge));
850 } else {
851 char ** const e = environ;
852 for (i = 0; e[i]; ++i) {
853 char *eq;
855 if (NULL != (eq = strchr(e[i], '='))) {
856 env_add(&env, e[i], eq - e[i], eq+1, strlen(eq+1));
861 /* create environment */
862 for (i = 0; i < host->bin_env->used; i++) {
863 data_string *ds = (data_string *)host->bin_env->data[i];
865 env_add(&env, CONST_BUF_LEN(ds->key), CONST_BUF_LEN(ds->value));
868 for (i = 0; i < env.used; i++) {
869 /* search for PHP_FCGI_CHILDREN */
870 if (0 == strncmp(env.ptr[i], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
873 /* not found, add a default */
874 if (i == env.used) {
875 env_add(&env, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
878 env.ptr[env.used] = NULL;
880 b = buffer_init();
881 buffer_copy_string_len(b, CONST_STR_LEN("exec "));
882 buffer_append_string_buffer(b, host->bin_path);
884 reset_signals();
886 /* exec the cgi */
887 execle("/bin/sh", "sh", "-c", b->ptr, (char *)NULL, env.ptr);
889 log_error_write(srv, __FILE__, __LINE__, "sbs",
890 "execl failed for:", host->bin_path, strerror(errno));
892 _exit(errno);
894 break;
896 case -1:
897 /* error */
898 close(scgi_fd);
899 break;
900 default:
901 /* father */
902 close(scgi_fd);
904 /* wait */
905 select(0, NULL, NULL, NULL, &tv);
907 switch (waitpid(child, &status, WNOHANG)) {
908 case 0:
909 /* child still running after timeout, good */
910 break;
911 case -1:
912 /* no PID found ? should never happen */
913 log_error_write(srv, __FILE__, __LINE__, "ss",
914 "pid not found:", strerror(errno));
915 return -1;
916 default:
917 /* the child should not terminate at all */
918 if (WIFEXITED(status)) {
919 log_error_write(srv, __FILE__, __LINE__, "sd",
920 "child exited (is this a SCGI binary ?):",
921 WEXITSTATUS(status));
922 } else if (WIFSIGNALED(status)) {
923 log_error_write(srv, __FILE__, __LINE__, "sd",
924 "child signaled:",
925 WTERMSIG(status));
926 } else {
927 log_error_write(srv, __FILE__, __LINE__, "sd",
928 "child died somehow:",
929 status);
931 return -1;
934 /* register process */
935 proc->pid = child;
936 proc->last_used = srv->cur_ts;
937 proc->is_local = 1;
939 break;
941 } else {
942 close(scgi_fd);
944 proc->is_local = 0;
945 proc->pid = 0;
947 if (p->conf.debug) {
948 log_error_write(srv, __FILE__, __LINE__, "sb",
949 "(debug) socket is already used, won't spawn:",
950 proc->socket);
954 proc->state = PROC_STATE_RUNNING;
955 host->active_procs++;
957 return 0;
960 #endif /* HAVE_FORK */
962 static scgi_extension_host * unixsocket_is_dup(plugin_data *p, size_t used, buffer *unixsocket) {
963 size_t i, j, n;
964 for (i = 0; i < used; ++i) {
965 scgi_exts *exts = p->config_storage[i]->exts;
966 for (j = 0; j < exts->used; ++j) {
967 scgi_extension *ex = exts->exts[j];
968 for (n = 0; n < ex->used; ++n) {
969 scgi_extension_host *host = ex->hosts[n];
970 if (!buffer_string_is_empty(host->unixsocket)
971 && buffer_is_equal(host->unixsocket, unixsocket)
972 && !buffer_string_is_empty(host->bin_path))
973 return host;
978 return NULL;
981 SETDEFAULTS_FUNC(mod_scgi_set_defaults) {
982 plugin_data *p = p_d;
983 data_unset *du;
984 size_t i = 0;
985 scgi_extension_host *df = NULL;
987 config_values_t cv[] = {
988 { "scgi.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
989 { "scgi.debug", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
990 { "scgi.protocol", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
991 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
994 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
995 force_assert(p->config_storage);
997 for (i = 0; i < srv->config_context->used; i++) {
998 data_config const* config = (data_config const*)srv->config_context->data[i];
999 plugin_config *s;
1001 s = malloc(sizeof(plugin_config));
1002 force_assert(s);
1003 s->exts = scgi_extensions_init();
1004 s->debug = 0;
1005 s->proto = LI_PROTOCOL_SCGI;
1007 cv[0].destination = s->exts;
1008 cv[1].destination = &(s->debug);
1009 cv[2].destination = NULL; /* T_CONFIG_LOCAL */
1011 p->config_storage[i] = s;
1013 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
1014 goto error;
1018 * <key> = ( ... )
1021 if (NULL != (du = array_get_element(config->value, "scgi.protocol"))) {
1022 data_string *ds = (data_string *)du;
1023 if (du->type == TYPE_STRING
1024 && buffer_is_equal_string(ds->value, CONST_STR_LEN("scgi"))) {
1025 s->proto = LI_PROTOCOL_SCGI;
1026 } else if (du->type == TYPE_STRING
1027 && buffer_is_equal_string(ds->value, CONST_STR_LEN("uwsgi"))) {
1028 s->proto = LI_PROTOCOL_UWSGI;
1029 } else {
1030 log_error_write(srv, __FILE__, __LINE__, "sss",
1031 "unexpected type for key: ", "scgi.protocol", "expected \"scgi\" or \"uwsgi\"");
1033 goto error;
1037 if (NULL != (du = array_get_element(config->value, "scgi.server"))) {
1038 size_t j;
1039 data_array *da = (data_array *)du;
1041 if (du->type != TYPE_ARRAY) {
1042 log_error_write(srv, __FILE__, __LINE__, "sss",
1043 "unexpected type for key: ", "scgi.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1045 goto error;
1050 * scgi.server = ( "<ext>" => ( ... ),
1051 * "<ext>" => ( ... ) )
1054 for (j = 0; j < da->value->used; j++) {
1055 size_t n;
1056 data_array *da_ext = (data_array *)da->value->data[j];
1058 if (da->value->data[j]->type != TYPE_ARRAY) {
1059 log_error_write(srv, __FILE__, __LINE__, "sssbs",
1060 "unexpected type for key: ", "scgi.server",
1061 "[", da->value->data[j]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1063 goto error;
1067 * da_ext->key == name of the extension
1071 * scgi.server = ( "<ext>" =>
1072 * ( "<host>" => ( ... ),
1073 * "<host>" => ( ... )
1074 * ),
1075 * "<ext>" => ... )
1078 for (n = 0; n < da_ext->value->used; n++) {
1079 data_array *da_host = (data_array *)da_ext->value->data[n];
1081 config_values_t fcv[] = {
1082 { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
1083 { "docroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
1084 { "socket", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
1085 { "bin-path", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
1087 { "check-local", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
1088 { "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
1089 { "min-procs-not-working", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 this is broken for now */
1090 { "max-procs", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
1091 { "max-load-per-proc", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
1092 { "idle-timeout", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
1093 { "disable-time", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
1095 { "bin-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
1096 { "bin-copy-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
1097 { "fix-root-scriptname", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
1098 { "listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
1099 { "x-sendfile", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
1100 { "x-sendfile-docroot",NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
1103 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
1106 if (da_host->type != TYPE_ARRAY) {
1107 log_error_write(srv, __FILE__, __LINE__, "ssSBS",
1108 "unexpected type for key:",
1109 "scgi.server",
1110 "[", da_host->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1112 goto error;
1115 df = scgi_host_init();
1117 df->check_local = 1;
1118 df->min_procs = 4;
1119 df->max_procs = 4;
1120 df->max_load_per_proc = 1;
1121 df->idle_timeout = 60;
1122 df->disable_time = 60;
1123 df->fix_root_path_name = 0;
1124 df->listen_backlog = 1024;
1125 df->xsendfile_allow = 0;
1126 df->refcount = 0;
1128 fcv[0].destination = df->host;
1129 fcv[1].destination = df->docroot;
1130 fcv[2].destination = df->unixsocket;
1131 fcv[3].destination = df->bin_path;
1133 fcv[4].destination = &(df->check_local);
1134 fcv[5].destination = &(df->port);
1135 fcv[6].destination = &(df->min_procs);
1136 fcv[7].destination = &(df->max_procs);
1137 fcv[8].destination = &(df->max_load_per_proc);
1138 fcv[9].destination = &(df->idle_timeout);
1139 fcv[10].destination = &(df->disable_time);
1141 fcv[11].destination = df->bin_env;
1142 fcv[12].destination = df->bin_env_copy;
1143 fcv[13].destination = &(df->fix_root_path_name);
1144 fcv[14].destination = &(df->listen_backlog);
1145 fcv[15].destination = &(df->xsendfile_allow);
1146 fcv[16].destination = df->xsendfile_docroot;
1149 if (0 != config_insert_values_internal(srv, da_host->value, fcv, T_CONFIG_SCOPE_CONNECTION)) {
1150 goto error;
1153 if ((!buffer_string_is_empty(df->host) || df->port) &&
1154 !buffer_string_is_empty(df->unixsocket)) {
1155 log_error_write(srv, __FILE__, __LINE__, "s",
1156 "either host+port or socket");
1158 goto error;
1161 if (!buffer_string_is_empty(df->unixsocket)) {
1162 /* unix domain socket */
1163 struct sockaddr_un un;
1165 if (buffer_string_length(df->unixsocket) + 1 > sizeof(un.sun_path) - 2) {
1166 log_error_write(srv, __FILE__, __LINE__, "s",
1167 "path of the unixdomain socket is too large");
1168 goto error;
1171 if (!buffer_string_is_empty(df->bin_path)) {
1172 scgi_extension_host *duplicate = unixsocket_is_dup(p, i+1, df->unixsocket);
1173 if (NULL != duplicate) {
1174 if (!buffer_is_equal(df->bin_path, duplicate->bin_path)) {
1175 log_error_write(srv, __FILE__, __LINE__, "sb",
1176 "duplicate unixsocket path:",
1177 df->unixsocket);
1178 goto error;
1180 scgi_host_free(df);
1181 df = duplicate;
1182 ++df->refcount;
1186 df->family = AF_UNIX;
1187 } else {
1188 /* tcp/ip */
1190 if (buffer_string_is_empty(df->host) &&
1191 buffer_string_is_empty(df->bin_path)) {
1192 log_error_write(srv, __FILE__, __LINE__, "sbbbs",
1193 "missing key (string):",
1194 da->key,
1195 da_ext->key,
1196 da_host->key,
1197 "host");
1199 goto error;
1200 } else if (df->port == 0) {
1201 log_error_write(srv, __FILE__, __LINE__, "sbbbs",
1202 "missing key (short):",
1203 da->key,
1204 da_ext->key,
1205 da_host->key,
1206 "port");
1207 goto error;
1210 df->family = (!buffer_string_is_empty(df->host) && NULL != strchr(df->host->ptr, ':')) ? AF_INET6 : AF_INET;
1213 if (df->refcount) {
1214 /* already init'd; skip spawning */
1215 } else if (!buffer_string_is_empty(df->bin_path)) {
1216 /* a local socket + self spawning */
1217 size_t pno;
1219 /* HACK: just to make sure the adaptive spawing is disabled */
1220 df->min_procs = df->max_procs;
1222 if (df->min_procs > df->max_procs) df->max_procs = df->min_procs;
1223 if (df->max_load_per_proc < 1) df->max_load_per_proc = 0;
1225 if (s->debug) {
1226 log_error_write(srv, __FILE__, __LINE__, "ssbsdsbsdsd",
1227 "--- scgi spawning local",
1228 "\n\tproc:", df->bin_path,
1229 "\n\tport:", df->port,
1230 "\n\tsocket", df->unixsocket,
1231 "\n\tmin-procs:", df->min_procs,
1232 "\n\tmax-procs:", df->max_procs);
1235 for (pno = 0; pno < df->min_procs; pno++) {
1236 scgi_proc *proc;
1238 proc = scgi_process_init();
1239 proc->id = df->num_procs++;
1240 df->max_id++;
1242 if (buffer_string_is_empty(df->unixsocket)) {
1243 proc->port = df->port + pno;
1244 } else {
1245 buffer_copy_buffer(proc->socket, df->unixsocket);
1246 buffer_append_string_len(proc->socket, CONST_STR_LEN("-"));
1247 buffer_append_int(proc->socket, pno);
1250 if (s->debug) {
1251 log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
1252 "--- scgi spawning",
1253 "\n\tport:", df->port,
1254 "\n\tsocket", df->unixsocket,
1255 "\n\tcurrent:", pno, "/", df->min_procs);
1258 if (!srv->srvconf.preflight_check
1259 && scgi_spawn_connection(srv, p, df, proc)) {
1260 log_error_write(srv, __FILE__, __LINE__, "s",
1261 "[ERROR]: spawning fcgi failed.");
1262 scgi_process_free(proc);
1263 goto error;
1266 proc->next = df->first;
1267 if (df->first) df->first->prev = proc;
1269 df->first = proc;
1271 } else {
1272 scgi_proc *fp;
1274 fp = scgi_process_init();
1275 fp->id = df->num_procs++;
1276 df->max_id++;
1277 df->active_procs++;
1278 fp->state = PROC_STATE_RUNNING;
1280 if (buffer_string_is_empty(df->unixsocket)) {
1281 fp->port = df->port;
1282 } else {
1283 buffer_copy_buffer(fp->socket, df->unixsocket);
1286 df->first = fp;
1288 df->min_procs = 1;
1289 df->max_procs = 1;
1292 if (df->xsendfile_docroot->used) {
1293 size_t k;
1294 for (k = 0; k < df->xsendfile_docroot->used; ++k) {
1295 data_string *ds = (data_string *)df->xsendfile_docroot->data[k];
1296 if (ds->type != TYPE_STRING) {
1297 log_error_write(srv, __FILE__, __LINE__, "s",
1298 "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
1299 goto error;
1301 if (ds->value->ptr[0] != '/') {
1302 log_error_write(srv, __FILE__, __LINE__, "SBs",
1303 "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds->value, "\"");
1304 goto error;
1306 buffer_path_simplify(ds->value, ds->value);
1307 buffer_append_slash(ds->value);
1311 /* if extension already exists, take it */
1312 scgi_extension_insert(s->exts, da_ext->key, df);
1313 df = NULL;
1319 return HANDLER_GO_ON;
1321 error:
1322 if (NULL != df) scgi_host_free(df);
1323 return HANDLER_ERROR;
1326 static int scgi_set_state(server *srv, handler_ctx *hctx, scgi_connection_state_t state) {
1327 hctx->state = state;
1328 hctx->state_timestamp = srv->cur_ts;
1330 return 0;
1334 static void scgi_connection_close(server *srv, handler_ctx *hctx) {
1335 plugin_data *p;
1336 connection *con;
1338 p = hctx->plugin_data;
1339 con = hctx->remote_conn;
1341 if (hctx->fd != -1) {
1342 fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
1343 fdevent_unregister(srv->ev, hctx->fd);
1344 fdevent_sched_close(srv->ev, hctx->fd, 1);
1347 if (hctx->host && hctx->proc) {
1348 hctx->host->load--;
1350 if (hctx->got_proc) {
1351 /* after the connect the process gets a load */
1352 hctx->proc->load--;
1354 if (hctx->conf.debug) {
1355 log_error_write(srv, __FILE__, __LINE__, "sddb",
1356 "release proc:",
1357 hctx->fd,
1358 hctx->proc->pid, hctx->proc->socket);
1362 scgi_proclist_sort_down(srv, hctx->host, hctx->proc);
1366 handler_ctx_free(hctx);
1367 con->plugin_ctx[p->id] = NULL;
1369 /* finish response (if not already con->file_started, con->file_finished) */
1370 if (con->mode == p->id) {
1371 http_response_backend_done(srv, con);
1375 static int scgi_reconnect(server *srv, handler_ctx *hctx) {
1376 /* child died
1378 * 1.
1380 * connect was ok, connection was accepted
1381 * but the php accept loop checks after the accept if it should die or not.
1383 * if yes we can only detect it at a write()
1385 * next step is resetting this attemp and setup a connection again
1387 * if we have more then 5 reconnects for the same request, die
1389 * 2.
1391 * we have a connection but the child died by some other reason
1395 fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
1396 fdevent_unregister(srv->ev, hctx->fd);
1397 fdevent_sched_close(srv->ev, hctx->fd, 1);
1399 scgi_set_state(srv, hctx, FCGI_STATE_INIT);
1401 hctx->reconnects++;
1403 if (hctx->conf.debug) {
1404 log_error_write(srv, __FILE__, __LINE__, "sddb",
1405 "release proc:",
1406 hctx->fd,
1407 hctx->proc->pid, hctx->proc->socket);
1410 hctx->proc->load--;
1411 scgi_proclist_sort_down(srv, hctx->host, hctx->proc);
1413 return 0;
1417 static handler_t scgi_connection_reset(server *srv, connection *con, void *p_d) {
1418 plugin_data *p = p_d;
1419 handler_ctx *hctx = con->plugin_ctx[p->id];
1420 if (hctx) scgi_connection_close(srv, hctx);
1422 return HANDLER_GO_ON;
1426 static int scgi_env_add_scgi(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
1427 buffer *env = venv;
1428 size_t len;
1430 if (!key || !val) return -1;
1432 len = key_len + val_len + 2;
1434 buffer_string_prepare_append(env, len);
1436 buffer_append_string_len(env, key, key_len);
1437 buffer_append_string_len(env, "", 1);
1438 buffer_append_string_len(env, val, val_len);
1439 buffer_append_string_len(env, "", 1);
1441 return 0;
1445 #ifdef __LITTLE_ENDIAN__
1446 #define uwsgi_htole16(x) (x)
1447 #else /* __BIG_ENDIAN__ */
1448 #define uwsgi_htole16(x) ((uint16_t) (((x) & 0xff) << 8 | ((x) & 0xff00) >> 8))
1449 #endif
1452 static int scgi_env_add_uwsgi(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
1453 buffer *env = venv;
1454 size_t len;
1455 uint16_t uwlen;
1457 if (!key || !val) return -1;
1458 if (key_len > USHRT_MAX || val_len > USHRT_MAX) return -1;
1460 len = 2 + key_len + 2 + val_len;
1462 buffer_string_prepare_append(env, len);
1464 uwlen = uwsgi_htole16((uint16_t)key_len);
1465 buffer_append_string_len(env, (char *)&uwlen, 2);
1466 buffer_append_string_len(env, key, key_len);
1467 uwlen = uwsgi_htole16((uint16_t)val_len);
1468 buffer_append_string_len(env, (char *)&uwlen, 2);
1469 buffer_append_string_len(env, val, val_len);
1471 return 0;
1477 * returns
1478 * -1 error
1479 * 0 connected
1480 * 1 not connected yet
1483 static int scgi_establish_connection(server *srv, handler_ctx *hctx) {
1484 struct sockaddr *scgi_addr;
1485 struct sockaddr_in scgi_addr_in;
1486 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1487 struct sockaddr_in6 scgi_addr_in6;
1488 #endif
1489 #ifdef HAVE_SYS_UN_H
1490 struct sockaddr_un scgi_addr_un;
1491 #endif
1492 socklen_t servlen;
1494 scgi_extension_host *host = hctx->host;
1495 scgi_proc *proc = hctx->proc;
1496 int scgi_fd = hctx->fd;
1498 if (!buffer_string_is_empty(proc->socket)) {
1499 #ifdef HAVE_SYS_UN_H
1500 /* use the unix domain socket */
1501 memset(&scgi_addr_un, 0, sizeof(scgi_addr_un));
1502 scgi_addr_un.sun_family = AF_UNIX;
1503 if (buffer_string_length(proc->socket) + 1 > sizeof(scgi_addr_un.sun_path)) {
1504 log_error_write(srv, __FILE__, __LINE__, "sB",
1505 "ERROR: Unix Domain socket filename too long:",
1506 proc->socket);
1507 return -1;
1509 memcpy(scgi_addr_un.sun_path, proc->socket->ptr, buffer_string_length(proc->socket) + 1);
1511 #ifdef SUN_LEN
1512 servlen = SUN_LEN(&scgi_addr_un);
1513 #else
1514 /* stevens says: */
1515 servlen = buffer_string_length(proc->socket) + 1 + sizeof(scgi_addr_un.sun_family);
1516 #endif
1517 scgi_addr = (struct sockaddr *) &scgi_addr_un;
1518 #else
1519 return -1;
1520 #endif
1521 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1522 } else if (host->family == AF_INET6 && !buffer_string_is_empty(host->host)) {
1523 memset(&scgi_addr_in6, 0, sizeof(scgi_addr_in6));
1524 scgi_addr_in6.sin6_family = AF_INET6;
1525 inet_pton(AF_INET6, host->host->ptr, (char *) &scgi_addr_in6.sin6_addr);
1526 scgi_addr_in6.sin6_port = htons(proc->port);
1527 servlen = sizeof(scgi_addr_in6);
1528 scgi_addr = (struct sockaddr *) &scgi_addr_in6;
1529 #endif
1530 } else {
1531 memset(&scgi_addr_in, 0, sizeof(scgi_addr_in));
1532 scgi_addr_in.sin_family = AF_INET;
1533 if (0 == inet_aton(host->host->ptr, &(scgi_addr_in.sin_addr))) {
1534 log_error_write(srv, __FILE__, __LINE__, "sbs",
1535 "converting IP-adress failed for", host->host,
1536 "\nBe sure to specify an IP address here");
1538 return -1;
1540 scgi_addr_in.sin_port = htons(proc->port);
1541 servlen = sizeof(scgi_addr_in);
1543 scgi_addr = (struct sockaddr *) &scgi_addr_in;
1546 if (-1 == connect(scgi_fd, scgi_addr, servlen)) {
1547 if (errno == EINPROGRESS ||
1548 errno == EALREADY ||
1549 errno == EINTR) {
1550 if (hctx->conf.debug) {
1551 log_error_write(srv, __FILE__, __LINE__, "sd",
1552 "connect delayed, will continue later:", scgi_fd);
1555 return 1;
1556 } else {
1557 log_error_write(srv, __FILE__, __LINE__, "sdsddb",
1558 "connect failed:", scgi_fd,
1559 strerror(errno), errno,
1560 proc->port, proc->socket);
1562 if (errno == EAGAIN) {
1563 /* this is Linux only */
1565 log_error_write(srv, __FILE__, __LINE__, "s",
1566 "If this happend on Linux: You have been run out of local ports. "
1567 "Check the manual, section Performance how to handle this.");
1570 return -1;
1573 if (hctx->conf.debug > 1) {
1574 log_error_write(srv, __FILE__, __LINE__, "sd",
1575 "connect succeeded: ", scgi_fd);
1580 return 0;
1584 static int scgi_create_env(server *srv, handler_ctx *hctx) {
1585 buffer *b;
1587 plugin_data *p = hctx->plugin_data;
1588 scgi_extension_host *host= hctx->host;
1590 connection *con = hctx->remote_conn;
1592 http_cgi_opts opts = { 0, 0, host->docroot, NULL };
1594 http_cgi_header_append_cb scgi_env_add = p->conf.proto == LI_PROTOCOL_SCGI
1595 ? scgi_env_add_scgi
1596 : scgi_env_add_uwsgi;
1598 buffer_string_prepare_copy(p->scgi_env, 1023);
1600 if (0 != http_cgi_headers(srv, con, &opts, scgi_env_add, p->scgi_env)) {
1601 con->http_status = 400;
1602 return -1;
1605 if (p->conf.proto == LI_PROTOCOL_SCGI) {
1606 scgi_env_add(p->scgi_env, CONST_STR_LEN("SCGI"), CONST_STR_LEN("1"));
1607 b = buffer_init();
1608 buffer_append_int(b, buffer_string_length(p->scgi_env));
1609 buffer_append_string_len(b, CONST_STR_LEN(":"));
1610 buffer_append_string_buffer(b, p->scgi_env);
1611 buffer_append_string_len(b, CONST_STR_LEN(","));
1612 } else { /* LI_PROTOCOL_UWSGI */
1613 /* http://uwsgi-docs.readthedocs.io/en/latest/Protocol.html */
1614 size_t len = buffer_string_length(p->scgi_env);
1615 uint32_t uwsgi_header;
1616 if (len > USHRT_MAX) {
1617 con->http_status = 431; /* Request Header Fields Too Large */
1618 con->mode = DIRECT;
1619 return -1; /* trigger return of HANDLER_FINISHED */
1621 b = buffer_init();
1622 buffer_string_prepare_copy(b, 4 + len);
1623 uwsgi_header = ((uint32_t)uwsgi_htole16((uint16_t)len)) << 8;
1624 memcpy(b->ptr, (char *)&uwsgi_header, 4);
1625 buffer_commit(b, 4);
1626 buffer_append_string_buffer(b, p->scgi_env);
1629 hctx->wb_reqlen = buffer_string_length(b);
1630 chunkqueue_append_buffer(hctx->wb, b);
1631 buffer_free(b);
1633 if (con->request.content_length) {
1634 chunkqueue_append_chunkqueue(hctx->wb, con->request_content_queue);
1635 hctx->wb_reqlen += con->request.content_length;/* (eventual) total request size */
1638 return 0;
1641 static int scgi_response_parse(server *srv, connection *con, plugin_data *p, buffer *in, int eol) {
1642 char *ns;
1643 const char *s;
1644 int line = 0;
1646 UNUSED(srv);
1648 buffer_copy_buffer(p->parse_response, in);
1650 for (s = p->parse_response->ptr;
1651 NULL != (ns = (eol == EOL_RN ? strstr(s, "\r\n") : strchr(s, '\n')));
1652 s = ns + (eol == EOL_RN ? 2 : 1), line++) {
1653 const char *key, *value;
1654 int key_len;
1655 data_string *ds;
1657 ns[0] = '\0';
1659 if (line == 0 &&
1660 0 == strncmp(s, "HTTP/1.", 7)) {
1661 /* non-parsed header ... we parse them anyway */
1663 if ((s[7] == '1' ||
1664 s[7] == '0') &&
1665 s[8] == ' ') {
1666 int status;
1667 /* after the space should be a status code for us */
1669 status = strtol(s+9, NULL, 10);
1671 if (status >= 100 && status < 1000) {
1672 /* we expected 3 digits got them */
1673 con->parsed_response |= HTTP_STATUS;
1674 con->http_status = status;
1677 } else {
1679 key = s;
1680 if (NULL == (value = strchr(s, ':'))) {
1681 /* we expect: "<key>: <value>\r\n" */
1682 continue;
1685 key_len = value - key;
1686 value += 1;
1688 /* skip LWS */
1689 while (*value == ' ' || *value == '\t') value++;
1691 if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
1692 ds = data_response_init();
1694 buffer_copy_string_len(ds->key, key, key_len);
1695 buffer_copy_string(ds->value, value);
1697 array_insert_unique(con->response.headers, (data_unset *)ds);
1699 switch(key_len) {
1700 case 4:
1701 if (0 == strncasecmp(key, "Date", key_len)) {
1702 con->parsed_response |= HTTP_DATE;
1704 break;
1705 case 6:
1706 if (0 == strncasecmp(key, "Status", key_len)) {
1707 int status = strtol(value, NULL, 10);
1708 if (status >= 100 && status < 1000) {
1709 con->http_status = status;
1710 con->parsed_response |= HTTP_STATUS;
1711 } else {
1712 con->http_status = 502;
1715 break;
1716 case 8:
1717 if (0 == strncasecmp(key, "Location", key_len)) {
1718 con->parsed_response |= HTTP_LOCATION;
1720 break;
1721 case 10:
1722 if (0 == strncasecmp(key, "Connection", key_len)) {
1723 con->response.keep_alive = (0 == strcasecmp(value, "Keep-Alive")) ? 1 : 0;
1724 con->parsed_response |= HTTP_CONNECTION;
1726 break;
1727 case 14:
1728 if (0 == strncasecmp(key, "Content-Length", key_len)) {
1729 con->response.content_length = strtoul(value, NULL, 10);
1730 con->parsed_response |= HTTP_CONTENT_LENGTH;
1732 break;
1733 default:
1734 break;
1739 /* CGI/1.1 rev 03 - 7.2.1.2 */
1740 if ((con->parsed_response & HTTP_LOCATION) &&
1741 !(con->parsed_response & HTTP_STATUS)) {
1742 con->http_status = 302;
1745 return 0;
1749 static int scgi_demux_response(server *srv, handler_ctx *hctx) {
1750 plugin_data *p = hctx->plugin_data;
1751 connection *con = hctx->remote_conn;
1753 while(1) {
1754 int n;
1756 buffer_string_prepare_copy(hctx->response, 1023);
1757 if (-1 == (n = read(hctx->fd, hctx->response->ptr, hctx->response->size - 1))) {
1758 if (errno == EAGAIN || errno == EINTR) {
1759 /* would block, wait for signal */
1760 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
1761 return 0;
1763 /* error */
1764 log_error_write(srv, __FILE__, __LINE__, "sdd", strerror(errno), con->fd, hctx->fd);
1765 return -1;
1768 if (n == 0) {
1769 /* read finished */
1770 return 1;
1773 buffer_commit(hctx->response, n);
1775 /* split header from body */
1777 if (con->file_started == 0) {
1778 char *c;
1779 int in_header = 0;
1780 int header_end = 0;
1781 int cp, eol = EOL_UNSET;
1782 size_t used = 0;
1783 size_t hlen = 0;
1785 buffer_append_string_buffer(hctx->response_header, hctx->response);
1787 /* nph (non-parsed headers) */
1788 if (0 == strncmp(hctx->response_header->ptr, "HTTP/1.", 7)) in_header = 1;
1790 /* search for the \r\n\r\n or \n\n in the string */
1791 for (c = hctx->response_header->ptr, cp = 0, used = buffer_string_length(hctx->response_header); used; c++, cp++, used--) {
1792 if (*c == ':') in_header = 1;
1793 else if (*c == '\n') {
1794 if (in_header == 0) {
1795 /* got a response without a response header */
1797 c = NULL;
1798 header_end = 1;
1799 break;
1802 if (eol == EOL_UNSET) eol = EOL_N;
1804 if (*(c+1) == '\n') {
1805 header_end = 1;
1806 hlen = cp + 2;
1807 break;
1810 } else if (used > 1 && *c == '\r' && *(c+1) == '\n') {
1811 if (in_header == 0) {
1812 /* got a response without a response header */
1814 c = NULL;
1815 header_end = 1;
1816 break;
1819 if (eol == EOL_UNSET) eol = EOL_RN;
1821 if (used > 3 &&
1822 *(c+2) == '\r' &&
1823 *(c+3) == '\n') {
1824 header_end = 1;
1825 hlen = cp + 4;
1826 break;
1829 /* skip the \n */
1830 c++;
1831 cp++;
1832 used--;
1836 if (header_end) {
1837 if (c == NULL) {
1838 /* no header, but a body */
1839 if (0 != http_chunk_append_buffer(srv, con, hctx->response_header)) {
1840 /* error writing to tempfile;
1841 * truncate response or send 500 if nothing sent yet */
1842 return 1;
1844 } else {
1845 size_t blen = buffer_string_length(hctx->response_header) - hlen;
1847 /* a small hack: terminate after at the second \r */
1848 buffer_string_set_length(hctx->response_header, hlen - 1);
1850 /* parse the response header */
1851 scgi_response_parse(srv, con, p, hctx->response_header, eol);
1853 if (hctx->host->xsendfile_allow) {
1854 data_string *ds;
1855 if (NULL != (ds = (data_string *) array_get_element(con->response.headers, "X-Sendfile"))) {
1856 http_response_xsendfile(srv, con, ds->value, hctx->host->xsendfile_docroot);
1857 return 1;
1861 if (blen > 0) {
1862 if (0 != http_chunk_append_mem(srv, con, hctx->response_header->ptr + hlen, blen)) {
1863 /* error writing to tempfile;
1864 * truncate response or send 500 if nothing sent yet */
1865 return 1;
1870 con->file_started = 1;
1871 } else {
1872 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
1873 if (buffer_string_length(hctx->response_header) > MAX_HTTP_REQUEST_HEADER) {
1874 log_error_write(srv, __FILE__, __LINE__, "sb", "response headers too large for", con->uri.path);
1875 con->http_status = 502; /* Bad Gateway */
1876 con->mode = DIRECT;
1877 return 1;
1880 } else {
1881 if (0 != http_chunk_append_buffer(srv, con, hctx->response)) {
1882 /* error writing to tempfile;
1883 * truncate response or send 500 if nothing sent yet */
1884 return 1;
1886 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
1887 && chunkqueue_length(con->write_queue) > 65536 - 4096) {
1888 if (!con->is_writable) {
1889 /*(defer removal of FDEVENT_IN interest since
1890 * connection_state_machine() might be able to send data
1891 * immediately, unless !con->is_writable, where
1892 * connection_state_machine() might not loop back to call
1893 * mod_scgi_handle_subrequest())*/
1894 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
1896 break;
1900 #if 0
1901 log_error_write(srv, __FILE__, __LINE__, "ddss", con->fd, hctx->fd, connection_get_state(con->state), b->ptr);
1902 #endif
1905 return 0;
1909 static int scgi_proclist_sort_up(server *srv, scgi_extension_host *host, scgi_proc *proc) {
1910 scgi_proc *p;
1912 UNUSED(srv);
1914 /* we have been the smallest of the current list
1915 * and we want to insert the node sorted as soon
1916 * possible
1918 * 1 0 0 0 1 1 1
1919 * | ^
1920 * | |
1921 * +------+
1925 /* nothing to sort, only one element */
1926 if (host->first == proc && proc->next == NULL) return 0;
1928 for (p = proc; p->next && p->next->load < proc->load; p = p->next);
1930 /* no need to move something
1932 * 1 2 2 2 3 3 3
1938 if (p == proc) return 0;
1940 if (host->first == proc) {
1941 /* we have been the first elememt */
1943 host->first = proc->next;
1944 host->first->prev = NULL;
1947 /* disconnect proc */
1949 if (proc->prev) proc->prev->next = proc->next;
1950 if (proc->next) proc->next->prev = proc->prev;
1952 /* proc should be right of p */
1954 proc->next = p->next;
1955 proc->prev = p;
1956 if (p->next) p->next->prev = proc;
1957 p->next = proc;
1958 #if 0
1959 for(p = host->first; p; p = p->next) {
1960 log_error_write(srv, __FILE__, __LINE__, "dd",
1961 p->pid, p->load);
1963 #else
1964 UNUSED(srv);
1965 #endif
1967 return 0;
1970 int scgi_proclist_sort_down(server *srv, scgi_extension_host *host, scgi_proc *proc) {
1971 scgi_proc *p;
1973 UNUSED(srv);
1975 /* we have been the smallest of the current list
1976 * and we want to insert the node sorted as soon
1977 * possible
1979 * 0 0 0 0 1 0 1
1980 * ^ |
1981 * | |
1982 * +----------+
1985 * the basic is idea is:
1986 * - the last active scgi process should be still
1987 * in ram and is not swapped out yet
1988 * - processes that are not reused will be killed
1989 * after some time by the trigger-handler
1990 * - remember it as:
1991 * everything > 0 is hot
1992 * all unused procs are colder the more right they are
1993 * ice-cold processes are propably unused since more
1994 * than 'unused-timeout', are swaped out and won't be
1995 * reused in the next seconds anyway.
1999 /* nothing to sort, only one element */
2000 if (host->first == proc && proc->next == NULL) return 0;
2002 for (p = host->first; p != proc && p->load < proc->load; p = p->next);
2005 /* no need to move something
2007 * 1 2 2 2 3 3 3
2013 if (p == proc) return 0;
2015 /* we have to move left. If we are already the first element
2016 * we are done */
2017 if (host->first == proc) return 0;
2019 /* release proc */
2020 if (proc->prev) proc->prev->next = proc->next;
2021 if (proc->next) proc->next->prev = proc->prev;
2023 /* proc should be left of p */
2024 proc->next = p;
2025 proc->prev = p->prev;
2026 if (p->prev) p->prev->next = proc;
2027 p->prev = proc;
2029 if (proc->prev == NULL) host->first = proc;
2030 #if 0
2031 for(p = host->first; p; p = p->next) {
2032 log_error_write(srv, __FILE__, __LINE__, "dd",
2033 p->pid, p->load);
2035 #else
2036 UNUSED(srv);
2037 #endif
2039 return 0;
2042 static int scgi_restart_dead_procs(server *srv, plugin_data *p, scgi_extension_host *host) {
2043 scgi_proc *proc;
2045 for (proc = host->first; proc; proc = proc->next) {
2046 if (p->conf.debug) {
2047 log_error_write(srv, __FILE__, __LINE__, "sbdbdddd",
2048 "proc:",
2049 host->host, proc->port,
2050 proc->socket,
2051 proc->state,
2052 proc->is_local,
2053 proc->load,
2054 proc->pid);
2057 if (0 == proc->is_local) {
2059 * external servers might get disabled
2061 * enable the server again, perhaps it is back again
2064 if ((proc->state == PROC_STATE_DISABLED) &&
2065 (srv->cur_ts - proc->disable_ts > host->disable_time)) {
2066 proc->state = PROC_STATE_RUNNING;
2067 host->active_procs++;
2069 log_error_write(srv, __FILE__, __LINE__, "sbdb",
2070 "fcgi-server re-enabled:",
2071 host->host, host->port,
2072 host->unixsocket);
2074 } else {
2075 /* the child should not terminate at all */
2076 int status;
2078 if (proc->state == PROC_STATE_DIED_WAIT_FOR_PID) {
2079 switch(waitpid(proc->pid, &status, WNOHANG)) {
2080 case 0:
2081 /* child is still alive */
2082 break;
2083 case -1:
2084 break;
2085 default:
2086 if (WIFEXITED(status)) {
2087 #if 0
2088 log_error_write(srv, __FILE__, __LINE__, "sdsd",
2089 "child exited, pid:", proc->pid,
2090 "status:", WEXITSTATUS(status));
2091 #endif
2092 } else if (WIFSIGNALED(status)) {
2093 log_error_write(srv, __FILE__, __LINE__, "sd",
2094 "child signaled:",
2095 WTERMSIG(status));
2096 } else {
2097 log_error_write(srv, __FILE__, __LINE__, "sd",
2098 "child died somehow:",
2099 status);
2102 proc->state = PROC_STATE_DIED;
2103 break;
2108 * local servers might died, but we restart them
2111 if (proc->state == PROC_STATE_DIED &&
2112 proc->load == 0) {
2113 /* restart the child */
2115 if (p->conf.debug) {
2116 log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
2117 "--- scgi spawning",
2118 "\n\tport:", host->port,
2119 "\n\tsocket", host->unixsocket,
2120 "\n\tcurrent:", 1, "/", host->min_procs);
2123 if (scgi_spawn_connection(srv, p, host, proc)) {
2124 log_error_write(srv, __FILE__, __LINE__, "s",
2125 "ERROR: spawning fcgi failed.");
2126 return HANDLER_ERROR;
2129 scgi_proclist_sort_down(srv, host, proc);
2134 return 0;
2138 static handler_t scgi_write_request(server *srv, handler_ctx *hctx) {
2139 scgi_extension_host *host= hctx->host;
2140 connection *con = hctx->remote_conn;
2142 int ret;
2144 /* sanity check */
2145 if (!host) {
2146 log_error_write(srv, __FILE__, __LINE__, "s", "fatal error: host = NULL");
2147 return HANDLER_ERROR;
2149 if (((buffer_string_is_empty(host->host) || !host->port) && buffer_string_is_empty(host->unixsocket))) {
2150 log_error_write(srv, __FILE__, __LINE__, "sxddd",
2151 "write-req: error",
2152 host,
2153 buffer_string_length(host->host),
2154 host->port,
2155 buffer_string_length(host->unixsocket));
2156 return HANDLER_ERROR;
2160 switch(hctx->state) {
2161 case FCGI_STATE_INIT:
2162 if (-1 == (hctx->fd = fdevent_socket_nb_cloexec(host->family, SOCK_STREAM, 0))) {
2163 if (errno == EMFILE ||
2164 errno == EINTR) {
2165 log_error_write(srv, __FILE__, __LINE__, "sd",
2166 "wait for fd at connection:", con->fd);
2168 return HANDLER_WAIT_FOR_FD;
2171 log_error_write(srv, __FILE__, __LINE__, "ssdd",
2172 "socket failed:", strerror(errno), srv->cur_fds, srv->max_fds);
2173 return HANDLER_ERROR;
2175 hctx->fde_ndx = -1;
2177 srv->cur_fds++;
2179 fdevent_register(srv->ev, hctx->fd, scgi_handle_fdevent, hctx);
2181 if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
2182 log_error_write(srv, __FILE__, __LINE__, "ss",
2183 "fcntl failed: ", strerror(errno));
2184 return HANDLER_ERROR;
2187 /* fall through */
2188 case FCGI_STATE_CONNECT:
2189 if (hctx->state == FCGI_STATE_INIT) {
2190 for (hctx->proc = hctx->host->first;
2191 hctx->proc && hctx->proc->state != PROC_STATE_RUNNING;
2192 hctx->proc = hctx->proc->next);
2194 /* all childs are dead */
2195 if (hctx->proc == NULL) {
2196 hctx->fde_ndx = -1;
2198 return HANDLER_ERROR;
2201 if (hctx->proc->is_local) {
2202 hctx->pid = hctx->proc->pid;
2205 switch (scgi_establish_connection(srv, hctx)) {
2206 case 1:
2207 scgi_set_state(srv, hctx, FCGI_STATE_CONNECT);
2209 /* connection is in progress, wait for an event and call getsockopt() below */
2211 fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2213 return HANDLER_WAIT_FOR_EVENT;
2214 case -1:
2215 /* if ECONNREFUSED; choose another connection */
2216 hctx->fde_ndx = -1;
2218 return HANDLER_ERROR;
2219 default:
2220 /* everything is ok, go on */
2221 break;
2225 } else {
2226 int socket_error;
2227 socklen_t socket_error_len = sizeof(socket_error);
2229 /* try to finish the connect() */
2230 if (0 != getsockopt(hctx->fd, SOL_SOCKET, SO_ERROR, &socket_error, &socket_error_len)) {
2231 log_error_write(srv, __FILE__, __LINE__, "ss",
2232 "getsockopt failed:", strerror(errno));
2234 return HANDLER_ERROR;
2236 if (socket_error != 0) {
2237 if (!hctx->proc->is_local || hctx->conf.debug) {
2238 /* local procs get restarted */
2240 log_error_write(srv, __FILE__, __LINE__, "ss",
2241 "establishing connection failed:", strerror(socket_error),
2242 "port:", hctx->proc->port);
2245 return HANDLER_ERROR;
2249 /* ok, we have the connection */
2251 hctx->proc->load++;
2252 hctx->proc->last_used = srv->cur_ts;
2253 hctx->got_proc = 1;
2255 if (hctx->conf.debug) {
2256 log_error_write(srv, __FILE__, __LINE__, "sddbdd",
2257 "got proc:",
2258 hctx->fd,
2259 hctx->proc->pid,
2260 hctx->proc->socket,
2261 hctx->proc->port,
2262 hctx->proc->load);
2265 /* move the proc-list entry down the list */
2266 scgi_proclist_sort_up(srv, hctx->host, hctx->proc);
2268 scgi_set_state(srv, hctx, FCGI_STATE_PREPARE_WRITE);
2269 /* fall through */
2270 case FCGI_STATE_PREPARE_WRITE:
2271 if (0 != scgi_create_env(srv, hctx)) {
2272 return HANDLER_FINISHED;
2275 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
2276 scgi_set_state(srv, hctx, FCGI_STATE_WRITE);
2278 /* fall through */
2279 case FCGI_STATE_WRITE:
2280 ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb, MAX_WRITE_LIMIT);
2282 chunkqueue_remove_finished_chunks(hctx->wb);
2284 if (ret < 0) {
2285 if (errno == ENOTCONN || ret == -2) {
2286 /* the connection got dropped after accept()
2288 * this is most of the time a PHP which dies
2289 * after PHP_FCGI_MAX_REQUESTS
2292 if (hctx->wb->bytes_out == 0 &&
2293 hctx->reconnects < 5) {
2294 usleep(10000); /* take away the load of the webserver
2295 * to let the php a chance to restart
2298 scgi_reconnect(srv, hctx);
2300 return HANDLER_COMEBACK;
2303 /* not reconnected ... why
2305 * far@#lighttpd report this for FreeBSD
2309 log_error_write(srv, __FILE__, __LINE__, "ssosd",
2310 "connection was dropped after accept(). reconnect() denied:",
2311 "write-offset:", hctx->wb->bytes_out,
2312 "reconnect attempts:", hctx->reconnects);
2314 return HANDLER_ERROR;
2315 } else {
2316 /* -1 == ret => error on our side */
2317 log_error_write(srv, __FILE__, __LINE__, "ssd",
2318 "write failed:", strerror(errno), errno);
2320 return HANDLER_ERROR;
2324 if (hctx->wb->bytes_out == hctx->wb_reqlen) {
2325 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2326 scgi_set_state(srv, hctx, FCGI_STATE_READ);
2327 } else {
2328 off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
2329 if (hctx->wb->bytes_in < hctx->wb_reqlen && wblen < 65536 - 16384) {
2330 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
2331 if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
2332 con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
2333 con->is_readable = 1; /* trigger optimistic read from client */
2336 if (0 == wblen) {
2337 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2338 } else {
2339 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2343 return HANDLER_WAIT_FOR_EVENT;
2344 case FCGI_STATE_READ:
2345 /* waiting for a response */
2346 return HANDLER_WAIT_FOR_EVENT;
2347 default:
2348 log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
2349 return HANDLER_ERROR;
2353 static handler_t scgi_send_request(server *srv, handler_ctx *hctx) {
2354 /* ok, create the request */
2355 handler_t rc = scgi_write_request(srv, hctx);
2356 if (HANDLER_ERROR != rc) {
2357 return rc;
2358 } else {
2359 scgi_proc *proc = hctx->proc;
2360 scgi_extension_host *host = hctx->host;
2361 plugin_data *p = hctx->plugin_data;
2362 connection *con = hctx->remote_conn;
2364 if (proc &&
2365 0 == proc->is_local &&
2366 proc->state != PROC_STATE_DISABLED) {
2367 /* only disable remote servers as we don't manage them*/
2369 log_error_write(srv, __FILE__, __LINE__, "sbdb", "fcgi-server disabled:",
2370 host->host,
2371 proc->port,
2372 proc->socket);
2374 /* disable this server */
2375 proc->disable_ts = srv->cur_ts;
2376 proc->state = PROC_STATE_DISABLED;
2377 host->active_procs--;
2380 if (hctx->state == FCGI_STATE_INIT ||
2381 hctx->state == FCGI_STATE_CONNECT) {
2382 /* connect() or getsockopt() failed,
2383 * restart the request-handling
2385 if (proc && proc->is_local) {
2387 if (hctx->conf.debug) {
2388 log_error_write(srv, __FILE__, __LINE__, "sbdb", "connect() to scgi failed, restarting the request-handling:",
2389 host->host,
2390 proc->port,
2391 proc->socket);
2395 * several hctx might reference the same proc
2397 * Only one of them should mark the proc as dead all the other
2398 * ones should just take a new one.
2400 * If a new proc was started with the old struct this might lead
2401 * the mark a perfect proc as dead otherwise
2404 if (proc->state == PROC_STATE_RUNNING &&
2405 hctx->pid == proc->pid) {
2406 proc->state = PROC_STATE_DIED_WAIT_FOR_PID;
2409 scgi_restart_dead_procs(srv, p, host);
2411 con->mode = DIRECT;/*(avoid changing con->state, con->http_status)*/
2412 scgi_connection_close(srv, hctx);
2413 con->mode = p->id;
2415 return HANDLER_COMEBACK;
2416 } else {
2417 scgi_connection_close(srv, hctx);
2418 con->http_status = 503;
2420 return HANDLER_FINISHED;
2426 static handler_t scgi_recv_response(server *srv, handler_ctx *hctx);
2429 SUBREQUEST_FUNC(mod_scgi_handle_subrequest) {
2430 plugin_data *p = p_d;
2432 handler_ctx *hctx = con->plugin_ctx[p->id];
2434 if (NULL == hctx) return HANDLER_GO_ON;
2436 /* not my job */
2437 if (con->mode != p->id) return HANDLER_GO_ON;
2439 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
2440 && con->file_started) {
2441 if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
2442 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
2443 } else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)) {
2444 /* optimistic read from backend, which might re-enable FDEVENT_IN */
2445 handler_t rc = scgi_recv_response(srv, hctx); /*(might invalidate hctx)*/
2446 if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
2450 if (0 == hctx->wb->bytes_in
2451 ? con->state == CON_STATE_READ_POST
2452 : hctx->wb->bytes_in < hctx->wb_reqlen) {
2453 /*(64k - 4k to attempt to avoid temporary files
2454 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
2455 if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096
2456 && (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN)){
2457 con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
2458 if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
2459 } else {
2460 handler_t r = connection_handle_read_post_state(srv, con);
2461 chunkqueue *req_cq = con->request_content_queue;
2462 if (0 != hctx->wb->bytes_in && !chunkqueue_is_empty(req_cq)) {
2463 chunkqueue_append_chunkqueue(hctx->wb, req_cq);
2464 if (fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_OUT) {
2465 return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
2468 if (r != HANDLER_GO_ON) return r;
2472 return ((0 == hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
2473 && hctx->state != FCGI_STATE_CONNECT)
2474 ? scgi_send_request(srv, hctx)
2475 : HANDLER_WAIT_FOR_EVENT;
2479 static handler_t scgi_recv_response(server *srv, handler_ctx *hctx) {
2481 switch (scgi_demux_response(srv, hctx)) {
2482 case 0:
2483 break;
2484 case 1:
2485 /* we are done */
2486 scgi_connection_close(srv, hctx);
2488 return HANDLER_FINISHED;
2489 case -1: {
2490 connection *con = hctx->remote_conn;
2491 plugin_data *p = hctx->plugin_data;
2493 scgi_proc *proc = hctx->proc;
2494 scgi_extension_host *host= hctx->host;
2496 if (proc->pid && proc->state != PROC_STATE_DIED) {
2497 int status;
2499 /* only fetch the zombie if it is not already done */
2501 switch(waitpid(proc->pid, &status, WNOHANG)) {
2502 case 0:
2503 /* child is still alive */
2504 break;
2505 case -1:
2506 break;
2507 default:
2508 /* the child should not terminate at all */
2509 if (WIFEXITED(status)) {
2510 log_error_write(srv, __FILE__, __LINE__, "sdsd",
2511 "child exited, pid:", proc->pid,
2512 "status:", WEXITSTATUS(status));
2513 } else if (WIFSIGNALED(status)) {
2514 log_error_write(srv, __FILE__, __LINE__, "sd",
2515 "child signaled:",
2516 WTERMSIG(status));
2517 } else {
2518 log_error_write(srv, __FILE__, __LINE__, "sd",
2519 "child died somehow:",
2520 status);
2523 if (hctx->conf.debug) {
2524 log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
2525 "--- scgi spawning",
2526 "\n\tport:", host->port,
2527 "\n\tsocket", host->unixsocket,
2528 "\n\tcurrent:", 1, "/", host->min_procs);
2531 if (scgi_spawn_connection(srv, p, host, proc)) {
2532 /* child died */
2533 proc->state = PROC_STATE_DIED;
2534 } else {
2535 scgi_proclist_sort_down(srv, host, proc);
2538 break;
2542 if (con->file_started == 0) {
2543 /* nothing has been send out yet, try to use another child */
2545 if (hctx->wb->bytes_out == 0 &&
2546 hctx->reconnects < 5) {
2548 log_error_write(srv, __FILE__, __LINE__, "ssdsd",
2549 "response not sent, request not sent, reconnection.",
2550 "connection-fd:", con->fd,
2551 "fcgi-fd:", hctx->fd);
2553 scgi_reconnect(srv, hctx);
2555 return HANDLER_COMEBACK;
2558 log_error_write(srv, __FILE__, __LINE__, "sosdsd",
2559 "response not sent, request sent:", hctx->wb->bytes_out,
2560 "connection-fd:", con->fd,
2561 "fcgi-fd:", hctx->fd);
2562 } else {
2563 log_error_write(srv, __FILE__, __LINE__, "ssdsd",
2564 "response already sent out, termination connection",
2565 "connection-fd:", con->fd,
2566 "fcgi-fd:", hctx->fd);
2569 http_response_backend_error(srv, con);
2570 scgi_connection_close(srv, hctx);
2571 return HANDLER_FINISHED;
2575 return HANDLER_GO_ON;
2579 static handler_t scgi_handle_fdevent(server *srv, void *ctx, int revents) {
2580 handler_ctx *hctx = ctx;
2581 connection *con = hctx->remote_conn;
2583 joblist_append(srv, con);
2585 if (revents & FDEVENT_IN) {
2586 handler_t rc = scgi_recv_response(srv, hctx);/*(might invalidate hctx)*/
2587 if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
2590 if (revents & FDEVENT_OUT) {
2591 return scgi_send_request(srv, hctx); /*(might invalidate hctx)*/
2594 /* perhaps this issue is already handled */
2595 if (revents & FDEVENT_HUP) {
2596 if (hctx->state == FCGI_STATE_CONNECT) {
2597 /* getoptsock will catch this one (right ?)
2599 * if we are in connect we might get a EINPROGRESS
2600 * in the first call and a FDEVENT_HUP in the
2601 * second round
2603 * FIXME: as it is a bit ugly.
2606 scgi_send_request(srv, hctx);
2607 } else if (con->file_started) {
2608 /* drain any remaining data from kernel pipe buffers
2609 * even if (con->conf.stream_response_body
2610 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
2611 * since event loop will spin on fd FDEVENT_HUP event
2612 * until unregistered. */
2613 handler_t rc;
2614 do {
2615 rc = scgi_recv_response(srv,hctx);/*(might invalidate hctx)*/
2616 } while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
2617 return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
2618 } else {
2619 scgi_extension_host *host= hctx->host;
2620 log_error_write(srv, __FILE__, __LINE__, "sbSBSDSd",
2621 "error: unexpected close of scgi connection for",
2622 con->uri.path,
2623 "(no scgi process on host: ",
2624 host->host,
2625 ", port: ",
2626 host->port,
2627 " ?)",
2628 hctx->state);
2630 scgi_connection_close(srv, hctx);
2632 } else if (revents & FDEVENT_ERR) {
2633 log_error_write(srv, __FILE__, __LINE__, "s",
2634 "fcgi: got a FDEVENT_ERR. Don't know why.");
2636 http_response_backend_error(srv, con);
2637 scgi_connection_close(srv, hctx);
2640 return HANDLER_FINISHED;
2642 #define PATCH(x) \
2643 p->conf.x = s->x;
2644 static int scgi_patch_connection(server *srv, connection *con, plugin_data *p) {
2645 size_t i, j;
2646 plugin_config *s = p->config_storage[0];
2648 PATCH(exts);
2649 PATCH(proto);
2650 PATCH(debug);
2652 /* skip the first, the global context */
2653 for (i = 1; i < srv->config_context->used; i++) {
2654 data_config *dc = (data_config *)srv->config_context->data[i];
2655 s = p->config_storage[i];
2657 /* condition didn't match */
2658 if (!config_check_cond(srv, con, dc)) continue;
2660 /* merge config */
2661 for (j = 0; j < dc->value->used; j++) {
2662 data_unset *du = dc->value->data[j];
2664 if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.server"))) {
2665 PATCH(exts);
2666 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.protocol"))) {
2667 PATCH(proto);
2668 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.debug"))) {
2669 PATCH(debug);
2674 return 0;
2676 #undef PATCH
2679 static handler_t scgi_check_extension(server *srv, connection *con, void *p_d, int uri_path_handler) {
2680 plugin_data *p = p_d;
2681 size_t s_len;
2682 int used = -1;
2683 size_t k;
2684 buffer *fn;
2685 scgi_extension *extension = NULL;
2686 scgi_extension_host *host = NULL;
2688 if (con->mode != DIRECT) return HANDLER_GO_ON;
2690 /* Possibly, we processed already this request */
2691 if (con->file_started == 1) return HANDLER_GO_ON;
2693 fn = uri_path_handler ? con->uri.path : con->physical.path;
2695 if (buffer_string_is_empty(fn)) return HANDLER_GO_ON;
2697 s_len = buffer_string_length(fn);
2699 scgi_patch_connection(srv, con, p);
2701 /* check if extension matches */
2702 for (k = 0; k < p->conf.exts->used; k++) {
2703 size_t ct_len;
2704 scgi_extension *ext = p->conf.exts->exts[k];
2706 if (buffer_is_empty(ext->key)) continue;
2708 ct_len = buffer_string_length(ext->key);
2710 if (s_len < ct_len) continue;
2712 /* check extension in the form "/scgi_pattern" */
2713 if (*(ext->key->ptr) == '/') {
2714 if (strncmp(fn->ptr, ext->key->ptr, ct_len) == 0) {
2715 extension = ext;
2716 break;
2718 } else if (0 == strncmp(fn->ptr + s_len - ct_len, ext->key->ptr, ct_len)) {
2719 /* check extension in the form ".fcg" */
2720 extension = ext;
2721 break;
2725 /* extension doesn't match */
2726 if (NULL == extension) {
2727 return HANDLER_GO_ON;
2730 /* get best server */
2731 for (k = 0; k < extension->used; k++) {
2732 scgi_extension_host *h = extension->hosts[k];
2734 /* we should have at least one proc that can do something */
2735 if (h->active_procs == 0) {
2736 continue;
2739 if (used == -1 || h->load < used) {
2740 used = h->load;
2742 host = h;
2746 if (!host) {
2747 /* sorry, we don't have a server alive for this ext */
2748 con->http_status = 500;
2749 con->mode = DIRECT;
2751 /* only send the 'no handler' once */
2752 if (!extension->note_is_sent) {
2753 extension->note_is_sent = 1;
2755 log_error_write(srv, __FILE__, __LINE__, "sbsbs",
2756 "all handlers for ", con->uri.path,
2757 "on", extension->key,
2758 "are down.");
2761 return HANDLER_FINISHED;
2764 /* a note about no handler is not sent yet */
2765 extension->note_is_sent = 0;
2767 /* SCGI requires that Content-Length be set.
2768 * Send 411 Length Required if Content-Length missing.
2769 * (Alternatively, collect full request body before proceeding
2770 * in mod_scgi_handle_subrequest()) */
2771 if (0 == con->request.content_length
2772 && array_get_element(con->request.headers, "Transfer-Encoding")) {
2773 con->keep_alive = 0;
2774 con->http_status = 411; /* Length Required */
2775 con->mode = DIRECT;
2776 return HANDLER_FINISHED;
2780 * if check-local is disabled, use the uri.path handler
2784 /* init handler-context */
2785 if (uri_path_handler) {
2786 if (host->check_local == 0) {
2787 handler_ctx *hctx;
2788 char *pathinfo;
2790 hctx = handler_ctx_init();
2792 hctx->remote_conn = con;
2793 hctx->plugin_data = p;
2794 hctx->host = host;
2795 hctx->proc = NULL;
2797 hctx->conf.exts = p->conf.exts;
2798 hctx->conf.debug = p->conf.debug;
2800 con->plugin_ctx[p->id] = hctx;
2802 host->load++;
2804 con->mode = p->id;
2806 if (con->conf.log_request_handling) {
2807 log_error_write(srv, __FILE__, __LINE__, "s",
2808 "handling it in mod_scgi");
2811 /* the prefix is the SCRIPT_NAME,
2812 * everything from start to the next slash
2813 * this is important for check-local = "disable"
2815 * if prefix = /admin.fcgi
2817 * /admin.fcgi/foo/bar
2819 * SCRIPT_NAME = /admin.fcgi
2820 * PATH_INFO = /foo/bar
2822 * if prefix = /fcgi-bin/
2824 * /fcgi-bin/foo/bar
2826 * SCRIPT_NAME = /fcgi-bin/foo
2827 * PATH_INFO = /bar
2831 /* the rewrite is only done for /prefix/? matches */
2832 if (host->fix_root_path_name && extension->key->ptr[0] == '/' && extension->key->ptr[1] == '\0') {
2833 buffer_copy_string(con->request.pathinfo, con->uri.path->ptr);
2834 buffer_string_set_length(con->uri.path, 0);
2835 } else if (extension->key->ptr[0] == '/' &&
2836 buffer_string_length(con->uri.path) > buffer_string_length(extension->key) &&
2837 NULL != (pathinfo = strchr(con->uri.path->ptr + buffer_string_length(extension->key), '/'))) {
2838 /* rewrite uri.path and pathinfo */
2840 buffer_copy_string(con->request.pathinfo, pathinfo);
2841 buffer_string_set_length(con->uri.path, buffer_string_length(con->uri.path) - buffer_string_length(con->request.pathinfo));
2844 } else {
2845 handler_ctx *hctx;
2846 hctx = handler_ctx_init();
2848 hctx->remote_conn = con;
2849 hctx->plugin_data = p;
2850 hctx->host = host;
2851 hctx->proc = NULL;
2853 hctx->conf.exts = p->conf.exts;
2854 hctx->conf.debug = p->conf.debug;
2856 con->plugin_ctx[p->id] = hctx;
2858 host->load++;
2860 con->mode = p->id;
2862 if (con->conf.log_request_handling) {
2863 log_error_write(srv, __FILE__, __LINE__, "s", "handling it in mod_scgi");
2867 return HANDLER_GO_ON;
2870 /* uri-path handler */
2871 static handler_t scgi_check_extension_1(server *srv, connection *con, void *p_d) {
2872 return scgi_check_extension(srv, con, p_d, 1);
2875 /* start request handler */
2876 static handler_t scgi_check_extension_2(server *srv, connection *con, void *p_d) {
2877 return scgi_check_extension(srv, con, p_d, 0);
2881 TRIGGER_FUNC(mod_scgi_handle_trigger) {
2882 plugin_data *p = p_d;
2883 size_t i, j, n;
2886 /* perhaps we should kill a connect attempt after 10-15 seconds
2888 * currently we wait for the TCP timeout which is on Linux 180 seconds
2894 /* check all childs if they are still up */
2896 for (i = 0; i < srv->config_context->used; i++) {
2897 plugin_config *conf;
2898 scgi_exts *exts;
2900 conf = p->config_storage[i];
2902 exts = conf->exts;
2904 for (j = 0; j < exts->used; j++) {
2905 scgi_extension *ex;
2907 ex = exts->exts[j];
2909 for (n = 0; n < ex->used; n++) {
2911 scgi_proc *proc;
2912 unsigned long sum_load = 0;
2913 scgi_extension_host *host;
2915 host = ex->hosts[n];
2917 scgi_restart_dead_procs(srv, p, host);
2919 for (proc = host->first; proc; proc = proc->next) {
2920 sum_load += proc->load;
2923 if (host->num_procs &&
2924 host->num_procs < host->max_procs &&
2925 (sum_load / host->num_procs) > host->max_load_per_proc) {
2926 /* overload, spawn new child */
2927 scgi_proc *fp = NULL;
2929 if (p->conf.debug) {
2930 log_error_write(srv, __FILE__, __LINE__, "s",
2931 "overload detected, spawning a new child");
2934 for (fp = host->unused_procs; fp && fp->pid != 0; fp = fp->next);
2936 if (fp) {
2937 if (fp == host->unused_procs) host->unused_procs = fp->next;
2939 if (fp->next) fp->next->prev = NULL;
2941 host->max_id++;
2942 } else {
2943 fp = scgi_process_init();
2944 fp->id = host->max_id++;
2947 host->num_procs++;
2949 if (buffer_string_is_empty(host->unixsocket)) {
2950 fp->port = host->port + fp->id;
2951 } else {
2952 buffer_copy_buffer(fp->socket, host->unixsocket);
2953 buffer_append_string_len(fp->socket, CONST_STR_LEN("-"));
2954 buffer_append_int(fp->socket, fp->id);
2957 if (scgi_spawn_connection(srv, p, host, fp)) {
2958 log_error_write(srv, __FILE__, __LINE__, "s",
2959 "ERROR: spawning fcgi failed.");
2960 scgi_process_free(fp);
2961 return HANDLER_ERROR;
2964 fp->prev = NULL;
2965 fp->next = host->first;
2966 if (host->first) {
2967 host->first->prev = fp;
2969 host->first = fp;
2972 for (proc = host->first; proc; proc = proc->next) {
2973 if (proc->load != 0) break;
2974 if (host->num_procs <= host->min_procs) break;
2975 if (proc->pid == 0) continue;
2977 if (srv->cur_ts - proc->last_used > host->idle_timeout) {
2978 /* a proc is idling for a long time now,
2979 * terminated it */
2981 if (p->conf.debug) {
2982 log_error_write(srv, __FILE__, __LINE__, "ssbsd",
2983 "idle-timeout reached, terminating child:",
2984 "socket:", proc->socket,
2985 "pid", proc->pid);
2989 if (proc->next) proc->next->prev = proc->prev;
2990 if (proc->prev) proc->prev->next = proc->next;
2992 if (proc->prev == NULL) host->first = proc->next;
2994 proc->prev = NULL;
2995 proc->next = host->unused_procs;
2997 if (host->unused_procs) host->unused_procs->prev = proc;
2998 host->unused_procs = proc;
3000 kill(proc->pid, SIGTERM);
3002 proc->state = PROC_STATE_KILLED;
3004 log_error_write(srv, __FILE__, __LINE__, "ssbsd",
3005 "killed:",
3006 "socket:", proc->socket,
3007 "pid", proc->pid);
3009 host->num_procs--;
3011 /* proc is now in unused, let the next second handle the next process */
3012 break;
3016 for (proc = host->unused_procs; proc; proc = proc->next) {
3017 int status;
3019 if (proc->pid == 0) continue;
3021 switch (waitpid(proc->pid, &status, WNOHANG)) {
3022 case 0:
3023 /* child still running after timeout, good */
3024 break;
3025 case -1:
3026 if (errno != EINTR) {
3027 /* no PID found ? should never happen */
3028 log_error_write(srv, __FILE__, __LINE__, "sddss",
3029 "pid ", proc->pid, proc->state,
3030 "not found:", strerror(errno));
3032 #if 0
3033 if (errno == ECHILD) {
3034 /* someone else has cleaned up for us */
3035 proc->pid = 0;
3036 proc->state = PROC_STATE_UNSET;
3038 #endif
3040 break;
3041 default:
3042 /* the child should not terminate at all */
3043 if (WIFEXITED(status)) {
3044 if (proc->state != PROC_STATE_KILLED) {
3045 log_error_write(srv, __FILE__, __LINE__, "sdb",
3046 "child exited:",
3047 WEXITSTATUS(status), proc->socket);
3049 } else if (WIFSIGNALED(status)) {
3050 if (WTERMSIG(status) != SIGTERM) {
3051 log_error_write(srv, __FILE__, __LINE__, "sd",
3052 "child signaled:",
3053 WTERMSIG(status));
3055 } else {
3056 log_error_write(srv, __FILE__, __LINE__, "sd",
3057 "child died somehow:",
3058 status);
3060 proc->pid = 0;
3061 proc->state = PROC_STATE_UNSET;
3062 host->max_id--;
3069 return HANDLER_GO_ON;
3073 int mod_scgi_plugin_init(plugin *p);
3074 int mod_scgi_plugin_init(plugin *p) {
3075 p->version = LIGHTTPD_VERSION_ID;
3076 p->name = buffer_init_string("scgi");
3078 p->init = mod_scgi_init;
3079 p->cleanup = mod_scgi_free;
3080 p->set_defaults = mod_scgi_set_defaults;
3081 p->connection_reset = scgi_connection_reset;
3082 p->handle_connection_close = scgi_connection_reset;
3083 p->handle_uri_clean = scgi_check_extension_1;
3084 p->handle_subrequest_start = scgi_check_extension_2;
3085 p->handle_subrequest = mod_scgi_handle_subrequest;
3086 p->handle_trigger = mod_scgi_handle_trigger;
3088 p->data = NULL;
3090 return 0;