[core] support Transfer-Encoding: chunked req body (fixes #2156)
[lighttpd.git] / src / mod_scgi.c
blob449b5fce33d471105cbbc3b2c2bf233641197a4c
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 *parse_response;
296 plugin_config **config_storage;
298 plugin_config conf; /* this is only used as long as no handler_ctx is setup */
299 } plugin_data;
301 /* connection specific data */
302 typedef enum { FCGI_STATE_INIT, FCGI_STATE_CONNECT, FCGI_STATE_PREPARE_WRITE,
303 FCGI_STATE_WRITE, FCGI_STATE_READ
304 } scgi_connection_state_t;
306 typedef struct {
307 buffer *response;
309 scgi_proc *proc;
310 scgi_extension_host *host;
312 scgi_connection_state_t state;
313 time_t state_timestamp;
315 chunkqueue *wb;
316 off_t wb_reqlen;
318 buffer *response_header;
320 int fd; /* fd to the scgi process */
321 int fde_ndx; /* index into the fd-event buffer */
323 pid_t pid;
324 int got_proc;
325 int reconnects; /* number of reconnect attempts */
327 plugin_config conf;
329 connection *remote_conn; /* dumb pointer */
330 plugin_data *plugin_data; /* dumb pointer */
331 scgi_extension *ext;
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->parse_response = buffer_init();
549 return p;
553 FREE_FUNC(mod_scgi_free) {
554 plugin_data *p = p_d;
556 UNUSED(srv);
558 buffer_free(p->scgi_env);
559 buffer_free(p->parse_response);
561 if (p->config_storage) {
562 size_t i, j, n;
563 for (i = 0; i < srv->config_context->used; i++) {
564 plugin_config *s = p->config_storage[i];
565 scgi_exts *exts;
567 if (NULL == s) continue;
569 exts = s->exts;
571 for (j = 0; j < exts->used; j++) {
572 scgi_extension *ex;
574 ex = exts->exts[j];
576 for (n = 0; n < ex->used; n++) {
577 scgi_proc *proc;
578 scgi_extension_host *host;
580 host = ex->hosts[n];
582 for (proc = host->first; proc; proc = proc->next) {
583 if (proc->pid != 0) kill(proc->pid, SIGTERM);
585 if (proc->is_local &&
586 !buffer_string_is_empty(proc->socket)) {
587 unlink(proc->socket->ptr);
591 for (proc = host->unused_procs; proc; proc = proc->next) {
592 if (proc->pid != 0) kill(proc->pid, SIGTERM);
594 if (proc->is_local &&
595 !buffer_string_is_empty(proc->socket)) {
596 unlink(proc->socket->ptr);
602 scgi_extensions_free(s->exts);
604 free(s);
606 free(p->config_storage);
609 free(p);
611 return HANDLER_GO_ON;
614 static int env_add(char_array *env, const char *key, size_t key_len, const char *val, size_t val_len) {
615 char *dst;
616 size_t i;
618 if (!key || !val) return -1;
620 dst = malloc(key_len + val_len + 3);
621 force_assert(dst);
622 memcpy(dst, key, key_len);
623 dst[key_len] = '=';
624 /* add the \0 from the value */
625 memcpy(dst + key_len + 1, val, val_len + 1);
627 for (i = 0; i < env->used; i++) {
628 if (0 == strncmp(dst, env->ptr[i], key_len + 1)) {
629 /* don't care about free as we are in a forked child which is going to exec(...) */
630 /* free(env->ptr[i]); */
631 env->ptr[i] = dst;
632 return 0;
636 if (env->size == 0) {
637 env->size = 16;
638 env->ptr = malloc(env->size * sizeof(*env->ptr));
639 force_assert(env->ptr);
640 } else if (env->size == env->used) {
641 env->size += 16;
642 env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
643 force_assert(env->ptr);
646 env->ptr[env->used++] = dst;
648 return 0;
651 #if !defined(HAVE_FORK)
652 static int scgi_spawn_connection(server *srv,
653 plugin_data *p,
654 scgi_extension_host *host,
655 scgi_proc *proc) {
656 UNUSED(srv);
657 UNUSED(p);
658 UNUSED(host);
659 UNUSED(proc);
660 return -1;
663 #else /* -> defined(HAVE_FORK) */
665 static int scgi_spawn_connection(server *srv,
666 plugin_data *p,
667 scgi_extension_host *host,
668 scgi_proc *proc) {
669 int scgi_fd;
670 int status;
671 struct timeval tv = { 0, 100 * 1000 };
672 #ifdef HAVE_SYS_UN_H
673 struct sockaddr_un scgi_addr_un;
674 #endif
675 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
676 struct sockaddr_in6 scgi_addr_in6;
677 #endif
678 struct sockaddr_in scgi_addr_in;
679 struct sockaddr *scgi_addr;
681 socklen_t servlen;
683 if (p->conf.debug) {
684 log_error_write(srv, __FILE__, __LINE__, "sdb",
685 "new proc, socket:", proc->port, proc->socket);
689 if (!buffer_string_is_empty(proc->socket)) {
690 #ifdef HAVE_SYS_UN_H
691 memset(&scgi_addr_un, 0, sizeof(scgi_addr_un));
692 scgi_addr_un.sun_family = AF_UNIX;
693 if (buffer_string_length(proc->socket) + 1 > sizeof(scgi_addr_un.sun_path)) {
694 log_error_write(srv, __FILE__, __LINE__, "sB",
695 "ERROR: Unix Domain socket filename too long:",
696 proc->socket);
697 return -1;
699 memcpy(scgi_addr_un.sun_path, proc->socket->ptr, buffer_string_length(proc->socket) + 1);
701 #ifdef SUN_LEN
702 servlen = SUN_LEN(&scgi_addr_un);
703 #else
704 /* stevens says: */
705 servlen = buffer_string_length(proc->socket) + 1 + sizeof(scgi_addr_un.sun_family);
706 #endif
707 scgi_addr = (struct sockaddr *) &scgi_addr_un;
708 #else
709 log_error_write(srv, __FILE__, __LINE__, "s",
710 "ERROR: Unix Domain sockets are not supported.");
711 return -1;
712 #endif
713 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
714 } else if (host->family == AF_INET6 && !buffer_string_is_empty(host->host)) {
715 memset(&scgi_addr_in6, 0, sizeof(scgi_addr_in6));
716 scgi_addr_in6.sin6_family = AF_INET6;
717 inet_pton(AF_INET6, host->host->ptr, (char *) &scgi_addr_in6.sin6_addr);
718 scgi_addr_in6.sin6_port = htons(proc->port);
719 servlen = sizeof(scgi_addr_in6);
720 scgi_addr = (struct sockaddr *) &scgi_addr_in6;
721 #endif
722 } else {
723 memset(&scgi_addr_in, 0, sizeof(scgi_addr_in));
724 scgi_addr_in.sin_family = AF_INET;
726 if (buffer_string_is_empty(host->host)) {
727 scgi_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
728 } else {
729 struct hostent *he;
731 /* set a usefull default */
732 scgi_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
735 if (NULL == (he = gethostbyname(host->host->ptr))) {
736 log_error_write(srv, __FILE__, __LINE__,
737 "sdb", "gethostbyname failed: ",
738 h_errno, host->host);
739 return -1;
742 if (he->h_addrtype != AF_INET) {
743 log_error_write(srv, __FILE__, __LINE__, "sd", "addr-type != AF_INET: ", he->h_addrtype);
744 return -1;
747 if (he->h_length != sizeof(struct in_addr)) {
748 log_error_write(srv, __FILE__, __LINE__, "sd", "addr-length != sizeof(in_addr): ", he->h_length);
749 return -1;
752 memcpy(&(scgi_addr_in.sin_addr.s_addr), he->h_addr_list[0], he->h_length);
755 scgi_addr_in.sin_port = htons(proc->port);
756 servlen = sizeof(scgi_addr_in);
758 scgi_addr = (struct sockaddr *) &scgi_addr_in;
761 if (-1 == (scgi_fd = fdevent_socket_cloexec(scgi_addr->sa_family, SOCK_STREAM, 0))) {
762 log_error_write(srv, __FILE__, __LINE__, "ss",
763 "failed:", strerror(errno));
764 return -1;
767 if (-1 == connect(scgi_fd, scgi_addr, servlen)) {
768 /* server is not up, spawn in */
769 pid_t child;
770 int val;
772 if (!buffer_string_is_empty(proc->socket)) {
773 unlink(proc->socket->ptr);
776 close(scgi_fd);
778 /* reopen socket */
779 if (-1 == (scgi_fd = fdevent_socket_cloexec(scgi_addr->sa_family, SOCK_STREAM, 0))) {
780 log_error_write(srv, __FILE__, __LINE__, "ss",
781 "socket failed:", strerror(errno));
782 return -1;
785 val = 1;
786 if (setsockopt(scgi_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
787 log_error_write(srv, __FILE__, __LINE__, "ss",
788 "socketsockopt failed:", strerror(errno));
789 close(scgi_fd);
790 return -1;
793 /* create socket */
794 if (-1 == bind(scgi_fd, scgi_addr, servlen)) {
795 log_error_write(srv, __FILE__, __LINE__, "sbds",
796 "bind failed for:",
797 proc->socket,
798 proc->port,
799 strerror(errno));
800 close(scgi_fd);
801 return -1;
804 if (-1 == listen(scgi_fd, host->listen_backlog)) {
805 log_error_write(srv, __FILE__, __LINE__, "ss",
806 "listen failed:", strerror(errno));
807 close(scgi_fd);
808 return -1;
811 switch ((child = fork())) {
812 case 0: {
813 buffer *b;
814 size_t i = 0;
815 int fd = 0;
816 char_array env;
819 /* create environment */
820 env.ptr = NULL;
821 env.size = 0;
822 env.used = 0;
824 if (scgi_fd != 0) {
825 dup2(scgi_fd, 0);
826 close(scgi_fd);
828 #ifdef SOCK_CLOEXEC
829 else
830 (void)fcntl(scgi_fd, F_SETFD, 0); /* clear cloexec */
831 #endif
833 /* we don't need the client socket */
834 for (fd = 3; fd < 256; fd++) {
835 close(fd);
838 /* build clean environment */
839 if (host->bin_env_copy->used) {
840 for (i = 0; i < host->bin_env_copy->used; i++) {
841 data_string *ds = (data_string *)host->bin_env_copy->data[i];
842 char *ge;
844 if (NULL != (ge = getenv(ds->value->ptr))) {
845 env_add(&env, CONST_BUF_LEN(ds->value), ge, strlen(ge));
848 } else {
849 char ** const e = environ;
850 for (i = 0; e[i]; ++i) {
851 char *eq;
853 if (NULL != (eq = strchr(e[i], '='))) {
854 env_add(&env, e[i], eq - e[i], eq+1, strlen(eq+1));
859 /* create environment */
860 for (i = 0; i < host->bin_env->used; i++) {
861 data_string *ds = (data_string *)host->bin_env->data[i];
863 env_add(&env, CONST_BUF_LEN(ds->key), CONST_BUF_LEN(ds->value));
866 for (i = 0; i < env.used; i++) {
867 /* search for PHP_FCGI_CHILDREN */
868 if (0 == strncmp(env.ptr[i], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
871 /* not found, add a default */
872 if (i == env.used) {
873 env_add(&env, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
876 env.ptr[env.used] = NULL;
878 b = buffer_init();
879 buffer_copy_string_len(b, CONST_STR_LEN("exec "));
880 buffer_append_string_buffer(b, host->bin_path);
882 reset_signals();
884 /* exec the cgi */
885 execle("/bin/sh", "sh", "-c", b->ptr, (char *)NULL, env.ptr);
887 log_error_write(srv, __FILE__, __LINE__, "sbs",
888 "execl failed for:", host->bin_path, strerror(errno));
890 _exit(errno);
892 break;
894 case -1:
895 /* error */
896 close(scgi_fd);
897 break;
898 default:
899 /* father */
900 close(scgi_fd);
902 /* wait */
903 select(0, NULL, NULL, NULL, &tv);
905 switch (waitpid(child, &status, WNOHANG)) {
906 case 0:
907 /* child still running after timeout, good */
908 break;
909 case -1:
910 /* no PID found ? should never happen */
911 log_error_write(srv, __FILE__, __LINE__, "ss",
912 "pid not found:", strerror(errno));
913 return -1;
914 default:
915 /* the child should not terminate at all */
916 if (WIFEXITED(status)) {
917 log_error_write(srv, __FILE__, __LINE__, "sd",
918 "child exited (is this a SCGI binary ?):",
919 WEXITSTATUS(status));
920 } else if (WIFSIGNALED(status)) {
921 log_error_write(srv, __FILE__, __LINE__, "sd",
922 "child signaled:",
923 WTERMSIG(status));
924 } else {
925 log_error_write(srv, __FILE__, __LINE__, "sd",
926 "child died somehow:",
927 status);
929 return -1;
932 /* register process */
933 proc->pid = child;
934 proc->last_used = srv->cur_ts;
935 proc->is_local = 1;
937 break;
939 } else {
940 close(scgi_fd);
942 proc->is_local = 0;
943 proc->pid = 0;
945 if (p->conf.debug) {
946 log_error_write(srv, __FILE__, __LINE__, "sb",
947 "(debug) socket is already used, won't spawn:",
948 proc->socket);
952 proc->state = PROC_STATE_RUNNING;
953 host->active_procs++;
955 return 0;
958 #endif /* HAVE_FORK */
960 static scgi_extension_host * unixsocket_is_dup(plugin_data *p, size_t used, buffer *unixsocket) {
961 size_t i, j, n;
962 for (i = 0; i < used; ++i) {
963 scgi_exts *exts = p->config_storage[i]->exts;
964 for (j = 0; j < exts->used; ++j) {
965 scgi_extension *ex = exts->exts[j];
966 for (n = 0; n < ex->used; ++n) {
967 scgi_extension_host *host = ex->hosts[n];
968 if (!buffer_string_is_empty(host->unixsocket)
969 && buffer_is_equal(host->unixsocket, unixsocket)
970 && !buffer_string_is_empty(host->bin_path))
971 return host;
976 return NULL;
979 SETDEFAULTS_FUNC(mod_scgi_set_defaults) {
980 plugin_data *p = p_d;
981 data_unset *du;
982 size_t i = 0;
983 scgi_extension_host *df = NULL;
985 config_values_t cv[] = {
986 { "scgi.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
987 { "scgi.debug", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
988 { "scgi.protocol", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
989 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
992 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
993 force_assert(p->config_storage);
995 for (i = 0; i < srv->config_context->used; i++) {
996 data_config const* config = (data_config const*)srv->config_context->data[i];
997 plugin_config *s;
999 s = malloc(sizeof(plugin_config));
1000 force_assert(s);
1001 s->exts = scgi_extensions_init();
1002 s->debug = 0;
1003 s->proto = LI_PROTOCOL_SCGI;
1005 cv[0].destination = s->exts;
1006 cv[1].destination = &(s->debug);
1007 cv[2].destination = NULL; /* T_CONFIG_LOCAL */
1009 p->config_storage[i] = s;
1011 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
1012 goto error;
1016 * <key> = ( ... )
1019 if (NULL != (du = array_get_element(config->value, "scgi.protocol"))) {
1020 data_string *ds = (data_string *)du;
1021 if (du->type == TYPE_STRING
1022 && buffer_is_equal_string(ds->value, CONST_STR_LEN("scgi"))) {
1023 s->proto = LI_PROTOCOL_SCGI;
1024 } else if (du->type == TYPE_STRING
1025 && buffer_is_equal_string(ds->value, CONST_STR_LEN("uwsgi"))) {
1026 s->proto = LI_PROTOCOL_UWSGI;
1027 } else {
1028 log_error_write(srv, __FILE__, __LINE__, "sss",
1029 "unexpected type for key: ", "scgi.protocol", "expected \"scgi\" or \"uwsgi\"");
1031 goto error;
1035 if (NULL != (du = array_get_element(config->value, "scgi.server"))) {
1036 size_t j;
1037 data_array *da = (data_array *)du;
1039 if (du->type != TYPE_ARRAY) {
1040 log_error_write(srv, __FILE__, __LINE__, "sss",
1041 "unexpected type for key: ", "scgi.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1043 goto error;
1048 * scgi.server = ( "<ext>" => ( ... ),
1049 * "<ext>" => ( ... ) )
1052 for (j = 0; j < da->value->used; j++) {
1053 size_t n;
1054 data_array *da_ext = (data_array *)da->value->data[j];
1056 if (da->value->data[j]->type != TYPE_ARRAY) {
1057 log_error_write(srv, __FILE__, __LINE__, "sssbs",
1058 "unexpected type for key: ", "scgi.server",
1059 "[", da->value->data[j]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1061 goto error;
1065 * da_ext->key == name of the extension
1069 * scgi.server = ( "<ext>" =>
1070 * ( "<host>" => ( ... ),
1071 * "<host>" => ( ... )
1072 * ),
1073 * "<ext>" => ... )
1076 for (n = 0; n < da_ext->value->used; n++) {
1077 data_array *da_host = (data_array *)da_ext->value->data[n];
1079 config_values_t fcv[] = {
1080 { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
1081 { "docroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
1082 { "socket", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
1083 { "bin-path", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
1085 { "check-local", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
1086 { "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
1087 { "min-procs-not-working", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 this is broken for now */
1088 { "max-procs", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
1089 { "max-load-per-proc", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
1090 { "idle-timeout", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
1091 { "disable-time", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
1093 { "bin-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
1094 { "bin-copy-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
1095 { "fix-root-scriptname", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
1096 { "listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
1097 { "x-sendfile", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
1098 { "x-sendfile-docroot",NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
1101 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
1104 if (da_host->type != TYPE_ARRAY) {
1105 log_error_write(srv, __FILE__, __LINE__, "ssSBS",
1106 "unexpected type for key:",
1107 "scgi.server",
1108 "[", da_host->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1110 goto error;
1113 df = scgi_host_init();
1115 df->check_local = 1;
1116 df->min_procs = 4;
1117 df->max_procs = 4;
1118 df->max_load_per_proc = 1;
1119 df->idle_timeout = 60;
1120 df->disable_time = 60;
1121 df->fix_root_path_name = 0;
1122 df->listen_backlog = 1024;
1123 df->xsendfile_allow = 0;
1124 df->refcount = 0;
1126 fcv[0].destination = df->host;
1127 fcv[1].destination = df->docroot;
1128 fcv[2].destination = df->unixsocket;
1129 fcv[3].destination = df->bin_path;
1131 fcv[4].destination = &(df->check_local);
1132 fcv[5].destination = &(df->port);
1133 fcv[6].destination = &(df->min_procs);
1134 fcv[7].destination = &(df->max_procs);
1135 fcv[8].destination = &(df->max_load_per_proc);
1136 fcv[9].destination = &(df->idle_timeout);
1137 fcv[10].destination = &(df->disable_time);
1139 fcv[11].destination = df->bin_env;
1140 fcv[12].destination = df->bin_env_copy;
1141 fcv[13].destination = &(df->fix_root_path_name);
1142 fcv[14].destination = &(df->listen_backlog);
1143 fcv[15].destination = &(df->xsendfile_allow);
1144 fcv[16].destination = df->xsendfile_docroot;
1147 if (0 != config_insert_values_internal(srv, da_host->value, fcv, T_CONFIG_SCOPE_CONNECTION)) {
1148 goto error;
1151 if ((!buffer_string_is_empty(df->host) || df->port) &&
1152 !buffer_string_is_empty(df->unixsocket)) {
1153 log_error_write(srv, __FILE__, __LINE__, "s",
1154 "either host+port or socket");
1156 goto error;
1159 if (!buffer_string_is_empty(df->unixsocket)) {
1160 /* unix domain socket */
1161 struct sockaddr_un un;
1163 if (buffer_string_length(df->unixsocket) + 1 > sizeof(un.sun_path) - 2) {
1164 log_error_write(srv, __FILE__, __LINE__, "s",
1165 "path of the unixdomain socket is too large");
1166 goto error;
1169 if (!buffer_string_is_empty(df->bin_path)) {
1170 scgi_extension_host *duplicate = unixsocket_is_dup(p, i+1, df->unixsocket);
1171 if (NULL != duplicate) {
1172 if (!buffer_is_equal(df->bin_path, duplicate->bin_path)) {
1173 log_error_write(srv, __FILE__, __LINE__, "sb",
1174 "duplicate unixsocket path:",
1175 df->unixsocket);
1176 goto error;
1178 scgi_host_free(df);
1179 df = duplicate;
1180 ++df->refcount;
1184 df->family = AF_UNIX;
1185 } else {
1186 /* tcp/ip */
1188 if (buffer_string_is_empty(df->host) &&
1189 buffer_string_is_empty(df->bin_path)) {
1190 log_error_write(srv, __FILE__, __LINE__, "sbbbs",
1191 "missing key (string):",
1192 da->key,
1193 da_ext->key,
1194 da_host->key,
1195 "host");
1197 goto error;
1198 } else if (df->port == 0) {
1199 log_error_write(srv, __FILE__, __LINE__, "sbbbs",
1200 "missing key (short):",
1201 da->key,
1202 da_ext->key,
1203 da_host->key,
1204 "port");
1205 goto error;
1208 df->family = (!buffer_string_is_empty(df->host) && NULL != strchr(df->host->ptr, ':')) ? AF_INET6 : AF_INET;
1211 if (df->refcount) {
1212 /* already init'd; skip spawning */
1213 } else if (!buffer_string_is_empty(df->bin_path)) {
1214 /* a local socket + self spawning */
1215 size_t pno;
1217 struct stat st;
1218 size_t nchars = strcspn(df->bin_path->ptr, " \t");
1219 char c = df->bin_path->ptr[nchars];
1220 df->bin_path->ptr[nchars] = '\0';
1221 if (0 == nchars || 0 != stat(df->bin_path->ptr, &st) || !S_ISREG(st.st_mode) || !(st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
1222 df->bin_path->ptr[nchars] = c;
1223 log_error_write(srv, __FILE__, __LINE__, "SSs",
1224 "invalid \"bin-path\" => \"", df->bin_path->ptr,
1225 "\" (check that file exists, is regular file, and is executable by lighttpd)");
1227 df->bin_path->ptr[nchars] = c;
1229 /* HACK: just to make sure the adaptive spawing is disabled */
1230 df->min_procs = df->max_procs;
1232 if (df->min_procs > df->max_procs) df->max_procs = df->min_procs;
1233 if (df->max_load_per_proc < 1) df->max_load_per_proc = 0;
1235 if (s->debug) {
1236 log_error_write(srv, __FILE__, __LINE__, "ssbsdsbsdsd",
1237 "--- scgi spawning local",
1238 "\n\tproc:", df->bin_path,
1239 "\n\tport:", df->port,
1240 "\n\tsocket", df->unixsocket,
1241 "\n\tmin-procs:", df->min_procs,
1242 "\n\tmax-procs:", df->max_procs);
1245 for (pno = 0; pno < df->min_procs; pno++) {
1246 scgi_proc *proc;
1248 proc = scgi_process_init();
1249 proc->id = df->num_procs++;
1250 df->max_id++;
1252 if (buffer_string_is_empty(df->unixsocket)) {
1253 proc->port = df->port + pno;
1254 } else {
1255 buffer_copy_buffer(proc->socket, df->unixsocket);
1256 buffer_append_string_len(proc->socket, CONST_STR_LEN("-"));
1257 buffer_append_int(proc->socket, pno);
1260 if (s->debug) {
1261 log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
1262 "--- scgi spawning",
1263 "\n\tport:", df->port,
1264 "\n\tsocket", df->unixsocket,
1265 "\n\tcurrent:", pno, "/", df->min_procs);
1268 if (!srv->srvconf.preflight_check
1269 && scgi_spawn_connection(srv, p, df, proc)) {
1270 log_error_write(srv, __FILE__, __LINE__, "s",
1271 "[ERROR]: spawning fcgi failed.");
1272 scgi_process_free(proc);
1273 goto error;
1276 proc->next = df->first;
1277 if (df->first) df->first->prev = proc;
1279 df->first = proc;
1281 } else {
1282 scgi_proc *fp;
1284 fp = scgi_process_init();
1285 fp->id = df->num_procs++;
1286 df->max_id++;
1287 df->active_procs++;
1288 fp->state = PROC_STATE_RUNNING;
1290 if (buffer_string_is_empty(df->unixsocket)) {
1291 fp->port = df->port;
1292 } else {
1293 buffer_copy_buffer(fp->socket, df->unixsocket);
1296 df->first = fp;
1298 df->min_procs = 1;
1299 df->max_procs = 1;
1302 if (df->xsendfile_docroot->used) {
1303 size_t k;
1304 for (k = 0; k < df->xsendfile_docroot->used; ++k) {
1305 data_string *ds = (data_string *)df->xsendfile_docroot->data[k];
1306 if (ds->type != TYPE_STRING) {
1307 log_error_write(srv, __FILE__, __LINE__, "s",
1308 "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
1309 goto error;
1311 if (ds->value->ptr[0] != '/') {
1312 log_error_write(srv, __FILE__, __LINE__, "SBs",
1313 "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds->value, "\"");
1314 goto error;
1316 buffer_path_simplify(ds->value, ds->value);
1317 buffer_append_slash(ds->value);
1321 /* if extension already exists, take it */
1322 scgi_extension_insert(s->exts, da_ext->key, df);
1323 df = NULL;
1329 return HANDLER_GO_ON;
1331 error:
1332 if (NULL != df) scgi_host_free(df);
1333 return HANDLER_ERROR;
1336 static int scgi_set_state(server *srv, handler_ctx *hctx, scgi_connection_state_t state) {
1337 hctx->state = state;
1338 hctx->state_timestamp = srv->cur_ts;
1340 return 0;
1344 static void scgi_backend_close(server *srv, handler_ctx *hctx) {
1345 if (hctx->fd != -1) {
1346 fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
1347 fdevent_unregister(srv->ev, hctx->fd);
1348 fdevent_sched_close(srv->ev, hctx->fd, 1);
1349 hctx->fd = -1;
1350 hctx->fde_ndx = -1;
1353 if (hctx->host) {
1354 if (hctx->proc) {
1355 /* after the connect the process gets a load */
1356 if (hctx->got_proc) hctx->proc->load--;
1357 scgi_proclist_sort_down(srv, hctx->host, hctx->proc);
1359 if (hctx->conf.debug) {
1360 log_error_write(srv, __FILE__, __LINE__, "sddb",
1361 "release proc:",
1362 hctx->fd,
1363 hctx->proc->pid, hctx->proc->socket);
1367 hctx->host->load--;
1368 hctx->host = NULL;
1372 static scgi_extension_host * scgi_extension_host_get(server *srv, connection *con, plugin_data *p, scgi_extension *extension) {
1373 int used = -1;
1374 scgi_extension_host *host = NULL;
1375 UNUSED(p);
1377 /* get best server */
1378 for (size_t k = 0; k < extension->used; ++k) {
1379 scgi_extension_host *h = extension->hosts[k];
1381 /* we should have at least one proc that can do something */
1382 if (h->active_procs == 0) {
1383 continue;
1386 if (used == -1 || h->load < used) {
1387 used = h->load;
1389 host = h;
1393 if (!host) {
1394 /* sorry, we don't have a server alive for this ext */
1395 con->http_status = 503; /* Service Unavailable */
1396 con->mode = DIRECT;
1398 /* only send the 'no handler' once */
1399 if (!extension->note_is_sent) {
1400 extension->note_is_sent = 1;
1402 log_error_write(srv, __FILE__, __LINE__, "sbsbs",
1403 "all handlers for ", con->uri.path,
1404 "on", extension->key,
1405 "are down.");
1409 return host;
1412 static void scgi_connection_close(server *srv, handler_ctx *hctx) {
1413 plugin_data *p;
1414 connection *con;
1416 p = hctx->plugin_data;
1417 con = hctx->remote_conn;
1419 scgi_backend_close(srv, hctx);
1420 handler_ctx_free(hctx);
1421 con->plugin_ctx[p->id] = NULL;
1423 /* finish response (if not already con->file_started, con->file_finished) */
1424 if (con->mode == p->id) {
1425 http_response_backend_done(srv, con);
1429 static handler_t scgi_reconnect(server *srv, handler_ctx *hctx) {
1430 scgi_backend_close(srv, hctx);
1432 hctx->host = scgi_extension_host_get(srv, hctx->remote_conn, hctx->plugin_data, hctx->ext);
1433 if (NULL == hctx->host) return HANDLER_FINISHED;
1435 hctx->host->load++;
1436 scgi_set_state(srv, hctx, FCGI_STATE_INIT);
1437 return HANDLER_COMEBACK;
1441 static handler_t scgi_connection_reset(server *srv, connection *con, void *p_d) {
1442 plugin_data *p = p_d;
1443 handler_ctx *hctx = con->plugin_ctx[p->id];
1444 if (hctx) scgi_connection_close(srv, hctx);
1446 return HANDLER_GO_ON;
1450 static int scgi_env_add_scgi(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
1451 buffer *env = venv;
1452 size_t len;
1454 if (!key || !val) return -1;
1456 len = key_len + val_len + 2;
1458 buffer_string_prepare_append(env, len);
1460 buffer_append_string_len(env, key, key_len);
1461 buffer_append_string_len(env, "", 1);
1462 buffer_append_string_len(env, val, val_len);
1463 buffer_append_string_len(env, "", 1);
1465 return 0;
1469 #ifdef __LITTLE_ENDIAN__
1470 #define uwsgi_htole16(x) (x)
1471 #else /* __BIG_ENDIAN__ */
1472 #define uwsgi_htole16(x) ((uint16_t) (((x) & 0xff) << 8 | ((x) & 0xff00) >> 8))
1473 #endif
1476 static int scgi_env_add_uwsgi(void *venv, const char *key, size_t key_len, const char *val, size_t val_len) {
1477 buffer *env = venv;
1478 size_t len;
1479 uint16_t uwlen;
1481 if (!key || !val) return -1;
1482 if (key_len > USHRT_MAX || val_len > USHRT_MAX) return -1;
1484 len = 2 + key_len + 2 + val_len;
1486 buffer_string_prepare_append(env, len);
1488 uwlen = uwsgi_htole16((uint16_t)key_len);
1489 buffer_append_string_len(env, (char *)&uwlen, 2);
1490 buffer_append_string_len(env, key, key_len);
1491 uwlen = uwsgi_htole16((uint16_t)val_len);
1492 buffer_append_string_len(env, (char *)&uwlen, 2);
1493 buffer_append_string_len(env, val, val_len);
1495 return 0;
1501 * returns
1502 * -1 error
1503 * 0 connected
1504 * 1 not connected yet
1507 static int scgi_establish_connection(server *srv, handler_ctx *hctx) {
1508 struct sockaddr *scgi_addr;
1509 struct sockaddr_in scgi_addr_in;
1510 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1511 struct sockaddr_in6 scgi_addr_in6;
1512 #endif
1513 #ifdef HAVE_SYS_UN_H
1514 struct sockaddr_un scgi_addr_un;
1515 #endif
1516 socklen_t servlen;
1518 scgi_extension_host *host = hctx->host;
1519 scgi_proc *proc = hctx->proc;
1520 int scgi_fd = hctx->fd;
1522 if (!buffer_string_is_empty(proc->socket)) {
1523 #ifdef HAVE_SYS_UN_H
1524 /* use the unix domain socket */
1525 memset(&scgi_addr_un, 0, sizeof(scgi_addr_un));
1526 scgi_addr_un.sun_family = AF_UNIX;
1527 if (buffer_string_length(proc->socket) + 1 > sizeof(scgi_addr_un.sun_path)) {
1528 log_error_write(srv, __FILE__, __LINE__, "sB",
1529 "ERROR: Unix Domain socket filename too long:",
1530 proc->socket);
1531 return -1;
1533 memcpy(scgi_addr_un.sun_path, proc->socket->ptr, buffer_string_length(proc->socket) + 1);
1535 #ifdef SUN_LEN
1536 servlen = SUN_LEN(&scgi_addr_un);
1537 #else
1538 /* stevens says: */
1539 servlen = buffer_string_length(proc->socket) + 1 + sizeof(scgi_addr_un.sun_family);
1540 #endif
1541 scgi_addr = (struct sockaddr *) &scgi_addr_un;
1542 #else
1543 return -1;
1544 #endif
1545 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1546 } else if (host->family == AF_INET6 && !buffer_string_is_empty(host->host)) {
1547 memset(&scgi_addr_in6, 0, sizeof(scgi_addr_in6));
1548 scgi_addr_in6.sin6_family = AF_INET6;
1549 inet_pton(AF_INET6, host->host->ptr, (char *) &scgi_addr_in6.sin6_addr);
1550 scgi_addr_in6.sin6_port = htons(proc->port);
1551 servlen = sizeof(scgi_addr_in6);
1552 scgi_addr = (struct sockaddr *) &scgi_addr_in6;
1553 #endif
1554 } else {
1555 memset(&scgi_addr_in, 0, sizeof(scgi_addr_in));
1556 scgi_addr_in.sin_family = AF_INET;
1557 if (0 == inet_aton(host->host->ptr, &(scgi_addr_in.sin_addr))) {
1558 log_error_write(srv, __FILE__, __LINE__, "sbs",
1559 "converting IP-adress failed for", host->host,
1560 "\nBe sure to specify an IP address here");
1562 return -1;
1564 scgi_addr_in.sin_port = htons(proc->port);
1565 servlen = sizeof(scgi_addr_in);
1567 scgi_addr = (struct sockaddr *) &scgi_addr_in;
1570 if (-1 == connect(scgi_fd, scgi_addr, servlen)) {
1571 if (errno == EINPROGRESS ||
1572 errno == EALREADY ||
1573 errno == EINTR) {
1574 if (hctx->conf.debug) {
1575 log_error_write(srv, __FILE__, __LINE__, "sd",
1576 "connect delayed, will continue later:", scgi_fd);
1579 return 1;
1580 } else {
1581 log_error_write(srv, __FILE__, __LINE__, "sdsddb",
1582 "connect failed:", scgi_fd,
1583 strerror(errno), errno,
1584 proc->port, proc->socket);
1586 if (errno == EAGAIN) {
1587 /* this is Linux only */
1589 log_error_write(srv, __FILE__, __LINE__, "s",
1590 "If this happend on Linux: You have been run out of local ports. "
1591 "Check the manual, section Performance how to handle this.");
1594 return -1;
1597 if (hctx->conf.debug > 1) {
1598 log_error_write(srv, __FILE__, __LINE__, "sd",
1599 "connect succeeded: ", scgi_fd);
1604 return 0;
1608 static int scgi_create_env(server *srv, handler_ctx *hctx) {
1609 buffer *b;
1611 plugin_data *p = hctx->plugin_data;
1612 scgi_extension_host *host= hctx->host;
1614 connection *con = hctx->remote_conn;
1616 http_cgi_opts opts = { 0, 0, host->docroot, NULL };
1618 http_cgi_header_append_cb scgi_env_add = hctx->conf.proto == LI_PROTOCOL_SCGI
1619 ? scgi_env_add_scgi
1620 : scgi_env_add_uwsgi;
1622 buffer_string_prepare_copy(p->scgi_env, 1023);
1624 if (0 != http_cgi_headers(srv, con, &opts, scgi_env_add, p->scgi_env)) {
1625 con->http_status = 400;
1626 return -1;
1629 if (hctx->conf.proto == LI_PROTOCOL_SCGI) {
1630 scgi_env_add(p->scgi_env, CONST_STR_LEN("SCGI"), CONST_STR_LEN("1"));
1631 b = buffer_init();
1632 buffer_append_int(b, buffer_string_length(p->scgi_env));
1633 buffer_append_string_len(b, CONST_STR_LEN(":"));
1634 buffer_append_string_buffer(b, p->scgi_env);
1635 buffer_append_string_len(b, CONST_STR_LEN(","));
1636 } else { /* LI_PROTOCOL_UWSGI */
1637 /* http://uwsgi-docs.readthedocs.io/en/latest/Protocol.html */
1638 size_t len = buffer_string_length(p->scgi_env);
1639 uint32_t uwsgi_header;
1640 if (len > USHRT_MAX) {
1641 con->http_status = 431; /* Request Header Fields Too Large */
1642 con->mode = DIRECT;
1643 return -1; /* trigger return of HANDLER_FINISHED */
1645 b = buffer_init();
1646 buffer_string_prepare_copy(b, 4 + len);
1647 uwsgi_header = ((uint32_t)uwsgi_htole16((uint16_t)len)) << 8;
1648 memcpy(b->ptr, (char *)&uwsgi_header, 4);
1649 buffer_commit(b, 4);
1650 buffer_append_string_buffer(b, p->scgi_env);
1653 hctx->wb_reqlen = buffer_string_length(b);
1654 chunkqueue_append_buffer(hctx->wb, b);
1655 buffer_free(b);
1657 if (con->request.content_length) {
1658 chunkqueue_append_chunkqueue(hctx->wb, con->request_content_queue);
1659 hctx->wb_reqlen += con->request.content_length;/* (eventual) total request size */
1662 return 0;
1665 static int scgi_response_parse(server *srv, connection *con, plugin_data *p, buffer *in, int eol) {
1666 char *ns;
1667 const char *s;
1668 int line = 0;
1670 UNUSED(srv);
1672 buffer_copy_buffer(p->parse_response, in);
1674 for (s = p->parse_response->ptr;
1675 NULL != (ns = (eol == EOL_RN ? strstr(s, "\r\n") : strchr(s, '\n')));
1676 s = ns + (eol == EOL_RN ? 2 : 1), line++) {
1677 const char *key, *value;
1678 int key_len;
1679 data_string *ds;
1681 ns[0] = '\0';
1683 if (line == 0 &&
1684 0 == strncmp(s, "HTTP/1.", 7)) {
1685 /* non-parsed header ... we parse them anyway */
1687 if ((s[7] == '1' ||
1688 s[7] == '0') &&
1689 s[8] == ' ') {
1690 int status;
1691 /* after the space should be a status code for us */
1693 status = strtol(s+9, NULL, 10);
1695 if (status >= 100 && status < 1000) {
1696 /* we expected 3 digits got them */
1697 con->parsed_response |= HTTP_STATUS;
1698 con->http_status = status;
1701 } else {
1703 key = s;
1704 if (NULL == (value = strchr(s, ':'))) {
1705 /* we expect: "<key>: <value>\r\n" */
1706 continue;
1709 key_len = value - key;
1710 value += 1;
1712 /* skip LWS */
1713 while (*value == ' ' || *value == '\t') value++;
1715 if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
1716 ds = data_response_init();
1718 buffer_copy_string_len(ds->key, key, key_len);
1719 buffer_copy_string(ds->value, value);
1721 array_insert_unique(con->response.headers, (data_unset *)ds);
1723 switch(key_len) {
1724 case 4:
1725 if (0 == strncasecmp(key, "Date", key_len)) {
1726 con->parsed_response |= HTTP_DATE;
1728 break;
1729 case 6:
1730 if (0 == strncasecmp(key, "Status", key_len)) {
1731 int status = strtol(value, NULL, 10);
1732 if (status >= 100 && status < 1000) {
1733 con->http_status = status;
1734 con->parsed_response |= HTTP_STATUS;
1735 } else {
1736 con->http_status = 502;
1739 break;
1740 case 8:
1741 if (0 == strncasecmp(key, "Location", key_len)) {
1742 con->parsed_response |= HTTP_LOCATION;
1744 break;
1745 case 10:
1746 if (0 == strncasecmp(key, "Connection", key_len)) {
1747 con->response.keep_alive = (0 == strcasecmp(value, "Keep-Alive")) ? 1 : 0;
1748 con->parsed_response |= HTTP_CONNECTION;
1750 break;
1751 case 14:
1752 if (0 == strncasecmp(key, "Content-Length", key_len)) {
1753 con->response.content_length = strtoul(value, NULL, 10);
1754 con->parsed_response |= HTTP_CONTENT_LENGTH;
1756 break;
1757 default:
1758 break;
1763 /* CGI/1.1 rev 03 - 7.2.1.2 */
1764 if ((con->parsed_response & HTTP_LOCATION) &&
1765 !(con->parsed_response & HTTP_STATUS)) {
1766 con->http_status = 302;
1769 return 0;
1773 static int scgi_demux_response(server *srv, handler_ctx *hctx) {
1774 plugin_data *p = hctx->plugin_data;
1775 connection *con = hctx->remote_conn;
1777 while(1) {
1778 int n;
1780 buffer_string_prepare_copy(hctx->response, 1023);
1781 if (-1 == (n = read(hctx->fd, hctx->response->ptr, hctx->response->size - 1))) {
1782 if (errno == EAGAIN || errno == EINTR) {
1783 /* would block, wait for signal */
1784 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
1785 return 0;
1787 /* error */
1788 log_error_write(srv, __FILE__, __LINE__, "sdd", strerror(errno), con->fd, hctx->fd);
1789 return -1;
1792 if (n == 0) {
1793 /* read finished */
1794 return 1;
1797 buffer_commit(hctx->response, n);
1799 /* split header from body */
1801 if (con->file_started == 0) {
1802 char *c;
1803 int in_header = 0;
1804 int header_end = 0;
1805 int cp, eol = EOL_UNSET;
1806 size_t used = 0;
1807 size_t hlen = 0;
1809 buffer_append_string_buffer(hctx->response_header, hctx->response);
1811 /* nph (non-parsed headers) */
1812 if (0 == strncmp(hctx->response_header->ptr, "HTTP/1.", 7)) in_header = 1;
1814 /* search for the \r\n\r\n or \n\n in the string */
1815 for (c = hctx->response_header->ptr, cp = 0, used = buffer_string_length(hctx->response_header); used; c++, cp++, used--) {
1816 if (*c == ':') in_header = 1;
1817 else if (*c == '\n') {
1818 if (in_header == 0) {
1819 /* got a response without a response header */
1821 c = NULL;
1822 header_end = 1;
1823 break;
1826 if (eol == EOL_UNSET) eol = EOL_N;
1828 if (*(c+1) == '\n') {
1829 header_end = 1;
1830 hlen = cp + 2;
1831 break;
1834 } else if (used > 1 && *c == '\r' && *(c+1) == '\n') {
1835 if (in_header == 0) {
1836 /* got a response without a response header */
1838 c = NULL;
1839 header_end = 1;
1840 break;
1843 if (eol == EOL_UNSET) eol = EOL_RN;
1845 if (used > 3 &&
1846 *(c+2) == '\r' &&
1847 *(c+3) == '\n') {
1848 header_end = 1;
1849 hlen = cp + 4;
1850 break;
1853 /* skip the \n */
1854 c++;
1855 cp++;
1856 used--;
1860 if (header_end) {
1861 if (c == NULL) {
1862 /* no header, but a body */
1863 if (0 != http_chunk_append_buffer(srv, con, hctx->response_header)) {
1864 /* error writing to tempfile;
1865 * truncate response or send 500 if nothing sent yet */
1866 return 1;
1868 } else {
1869 size_t blen = buffer_string_length(hctx->response_header) - hlen;
1871 /* a small hack: terminate after at the second \r */
1872 buffer_string_set_length(hctx->response_header, hlen - 1);
1874 /* parse the response header */
1875 scgi_response_parse(srv, con, p, hctx->response_header, eol);
1877 if (hctx->host->xsendfile_allow) {
1878 data_string *ds;
1879 if (NULL != (ds = (data_string *) array_get_element(con->response.headers, "X-Sendfile"))) {
1880 http_response_xsendfile(srv, con, ds->value, hctx->host->xsendfile_docroot);
1881 return 1;
1885 if (blen > 0) {
1886 if (0 != http_chunk_append_mem(srv, con, hctx->response_header->ptr + hlen, blen)) {
1887 /* error writing to tempfile;
1888 * truncate response or send 500 if nothing sent yet */
1889 return 1;
1894 con->file_started = 1;
1895 } else {
1896 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
1897 if (buffer_string_length(hctx->response_header) > MAX_HTTP_REQUEST_HEADER) {
1898 log_error_write(srv, __FILE__, __LINE__, "sb", "response headers too large for", con->uri.path);
1899 con->http_status = 502; /* Bad Gateway */
1900 con->mode = DIRECT;
1901 return 1;
1904 } else {
1905 if (0 != http_chunk_append_buffer(srv, con, hctx->response)) {
1906 /* error writing to tempfile;
1907 * truncate response or send 500 if nothing sent yet */
1908 return 1;
1910 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
1911 && chunkqueue_length(con->write_queue) > 65536 - 4096) {
1912 if (!con->is_writable) {
1913 /*(defer removal of FDEVENT_IN interest since
1914 * connection_state_machine() might be able to send data
1915 * immediately, unless !con->is_writable, where
1916 * connection_state_machine() might not loop back to call
1917 * mod_scgi_handle_subrequest())*/
1918 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
1920 break;
1924 #if 0
1925 log_error_write(srv, __FILE__, __LINE__, "ddss", con->fd, hctx->fd, connection_get_state(con->state), b->ptr);
1926 #endif
1929 return 0;
1933 static int scgi_proclist_sort_up(server *srv, scgi_extension_host *host, scgi_proc *proc) {
1934 scgi_proc *p;
1936 UNUSED(srv);
1938 /* we have been the smallest of the current list
1939 * and we want to insert the node sorted as soon
1940 * possible
1942 * 1 0 0 0 1 1 1
1943 * | ^
1944 * | |
1945 * +------+
1949 /* nothing to sort, only one element */
1950 if (host->first == proc && proc->next == NULL) return 0;
1952 for (p = proc; p->next && p->next->load < proc->load; p = p->next);
1954 /* no need to move something
1956 * 1 2 2 2 3 3 3
1962 if (p == proc) return 0;
1964 if (host->first == proc) {
1965 /* we have been the first elememt */
1967 host->first = proc->next;
1968 host->first->prev = NULL;
1971 /* disconnect proc */
1973 if (proc->prev) proc->prev->next = proc->next;
1974 if (proc->next) proc->next->prev = proc->prev;
1976 /* proc should be right of p */
1978 proc->next = p->next;
1979 proc->prev = p;
1980 if (p->next) p->next->prev = proc;
1981 p->next = proc;
1982 #if 0
1983 for(p = host->first; p; p = p->next) {
1984 log_error_write(srv, __FILE__, __LINE__, "dd",
1985 p->pid, p->load);
1987 #else
1988 UNUSED(srv);
1989 #endif
1991 return 0;
1994 int scgi_proclist_sort_down(server *srv, scgi_extension_host *host, scgi_proc *proc) {
1995 scgi_proc *p;
1997 UNUSED(srv);
1999 /* we have been the smallest of the current list
2000 * and we want to insert the node sorted as soon
2001 * possible
2003 * 0 0 0 0 1 0 1
2004 * ^ |
2005 * | |
2006 * +----------+
2009 * the basic is idea is:
2010 * - the last active scgi process should be still
2011 * in ram and is not swapped out yet
2012 * - processes that are not reused will be killed
2013 * after some time by the trigger-handler
2014 * - remember it as:
2015 * everything > 0 is hot
2016 * all unused procs are colder the more right they are
2017 * ice-cold processes are propably unused since more
2018 * than 'unused-timeout', are swaped out and won't be
2019 * reused in the next seconds anyway.
2023 /* nothing to sort, only one element */
2024 if (host->first == proc && proc->next == NULL) return 0;
2026 for (p = host->first; p != proc && p->load < proc->load; p = p->next);
2029 /* no need to move something
2031 * 1 2 2 2 3 3 3
2037 if (p == proc) return 0;
2039 /* we have to move left. If we are already the first element
2040 * we are done */
2041 if (host->first == proc) return 0;
2043 /* release proc */
2044 if (proc->prev) proc->prev->next = proc->next;
2045 if (proc->next) proc->next->prev = proc->prev;
2047 /* proc should be left of p */
2048 proc->next = p;
2049 proc->prev = p->prev;
2050 if (p->prev) p->prev->next = proc;
2051 p->prev = proc;
2053 if (proc->prev == NULL) host->first = proc;
2054 #if 0
2055 for(p = host->first; p; p = p->next) {
2056 log_error_write(srv, __FILE__, __LINE__, "dd",
2057 p->pid, p->load);
2059 #else
2060 UNUSED(srv);
2061 #endif
2063 return 0;
2066 static int scgi_restart_dead_procs(server *srv, plugin_data *p, scgi_extension_host *host) {
2067 scgi_proc *proc;
2069 for (proc = host->first; proc; proc = proc->next) {
2070 if (p->conf.debug) {
2071 log_error_write(srv, __FILE__, __LINE__, "sbdbdddd",
2072 "proc:",
2073 host->host, proc->port,
2074 proc->socket,
2075 proc->state,
2076 proc->is_local,
2077 proc->load,
2078 proc->pid);
2081 if (0 == proc->is_local) {
2083 * external servers might get disabled
2085 * enable the server again, perhaps it is back again
2088 if ((proc->state == PROC_STATE_DISABLED) &&
2089 (srv->cur_ts - proc->disable_ts > host->disable_time)) {
2090 proc->state = PROC_STATE_RUNNING;
2091 host->active_procs++;
2093 log_error_write(srv, __FILE__, __LINE__, "sbdb",
2094 "fcgi-server re-enabled:",
2095 host->host, host->port,
2096 host->unixsocket);
2098 } else {
2099 /* the child should not terminate at all */
2100 int status;
2102 if (proc->state == PROC_STATE_DIED_WAIT_FOR_PID) {
2103 switch(waitpid(proc->pid, &status, WNOHANG)) {
2104 case 0:
2105 /* child is still alive */
2106 break;
2107 case -1:
2108 break;
2109 default:
2110 if (WIFEXITED(status)) {
2111 #if 0
2112 log_error_write(srv, __FILE__, __LINE__, "sdsd",
2113 "child exited, pid:", proc->pid,
2114 "status:", WEXITSTATUS(status));
2115 #endif
2116 } else if (WIFSIGNALED(status)) {
2117 log_error_write(srv, __FILE__, __LINE__, "sd",
2118 "child signaled:",
2119 WTERMSIG(status));
2120 } else {
2121 log_error_write(srv, __FILE__, __LINE__, "sd",
2122 "child died somehow:",
2123 status);
2126 proc->state = PROC_STATE_DIED;
2127 break;
2132 * local servers might died, but we restart them
2135 if (proc->state == PROC_STATE_DIED &&
2136 proc->load == 0) {
2137 /* restart the child */
2139 if (p->conf.debug) {
2140 log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
2141 "--- scgi spawning",
2142 "\n\tport:", host->port,
2143 "\n\tsocket", host->unixsocket,
2144 "\n\tcurrent:", 1, "/", host->min_procs);
2147 if (scgi_spawn_connection(srv, p, host, proc)) {
2148 log_error_write(srv, __FILE__, __LINE__, "s",
2149 "ERROR: spawning fcgi failed.");
2150 return HANDLER_ERROR;
2153 scgi_proclist_sort_down(srv, host, proc);
2158 return 0;
2162 static handler_t scgi_write_request(server *srv, handler_ctx *hctx) {
2163 scgi_extension_host *host= hctx->host;
2164 connection *con = hctx->remote_conn;
2166 int ret;
2168 switch(hctx->state) {
2169 case FCGI_STATE_INIT:
2170 if (-1 == (hctx->fd = fdevent_socket_nb_cloexec(host->family, SOCK_STREAM, 0))) {
2171 if (errno == EMFILE ||
2172 errno == EINTR) {
2173 log_error_write(srv, __FILE__, __LINE__, "sd",
2174 "wait for fd at connection:", con->fd);
2176 return HANDLER_WAIT_FOR_FD;
2179 log_error_write(srv, __FILE__, __LINE__, "ssdd",
2180 "socket failed:", strerror(errno), srv->cur_fds, srv->max_fds);
2181 return HANDLER_ERROR;
2183 hctx->fde_ndx = -1;
2185 srv->cur_fds++;
2187 fdevent_register(srv->ev, hctx->fd, scgi_handle_fdevent, hctx);
2189 if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
2190 log_error_write(srv, __FILE__, __LINE__, "ss",
2191 "fcntl failed: ", strerror(errno));
2192 return HANDLER_ERROR;
2195 /* fall through */
2196 case FCGI_STATE_CONNECT:
2197 if (hctx->state == FCGI_STATE_INIT) {
2198 for (hctx->proc = hctx->host->first;
2199 hctx->proc && hctx->proc->state != PROC_STATE_RUNNING;
2200 hctx->proc = hctx->proc->next);
2202 /* all childs are dead */
2203 if (hctx->proc == NULL) {
2204 hctx->fde_ndx = -1;
2206 return HANDLER_ERROR;
2209 if (hctx->proc->is_local) {
2210 hctx->pid = hctx->proc->pid;
2213 switch (scgi_establish_connection(srv, hctx)) {
2214 case 1:
2215 scgi_set_state(srv, hctx, FCGI_STATE_CONNECT);
2217 /* connection is in progress, wait for an event and call getsockopt() below */
2219 fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2221 return HANDLER_WAIT_FOR_EVENT;
2222 case -1:
2223 /* if ECONNREFUSED; choose another connection */
2224 hctx->fde_ndx = -1;
2226 return HANDLER_ERROR;
2227 default:
2228 /* everything is ok, go on */
2229 break;
2233 } else {
2234 int socket_error;
2235 socklen_t socket_error_len = sizeof(socket_error);
2237 /* try to finish the connect() */
2238 if (0 != getsockopt(hctx->fd, SOL_SOCKET, SO_ERROR, &socket_error, &socket_error_len)) {
2239 log_error_write(srv, __FILE__, __LINE__, "ss",
2240 "getsockopt failed:", strerror(errno));
2242 return HANDLER_ERROR;
2244 if (socket_error != 0) {
2245 if (!hctx->proc->is_local || hctx->conf.debug) {
2246 /* local procs get restarted */
2248 log_error_write(srv, __FILE__, __LINE__, "ss",
2249 "establishing connection failed:", strerror(socket_error),
2250 "port:", hctx->proc->port);
2253 return HANDLER_ERROR;
2257 /* ok, we have the connection */
2259 hctx->proc->load++;
2260 hctx->proc->last_used = srv->cur_ts;
2261 hctx->got_proc = 1;
2263 if (hctx->conf.debug) {
2264 log_error_write(srv, __FILE__, __LINE__, "sddbdd",
2265 "got proc:",
2266 hctx->fd,
2267 hctx->proc->pid,
2268 hctx->proc->socket,
2269 hctx->proc->port,
2270 hctx->proc->load);
2273 /* move the proc-list entry down the list */
2274 scgi_proclist_sort_up(srv, hctx->host, hctx->proc);
2276 scgi_set_state(srv, hctx, FCGI_STATE_PREPARE_WRITE);
2277 /* fall through */
2278 case FCGI_STATE_PREPARE_WRITE:
2279 if (0 != scgi_create_env(srv, hctx)) {
2280 return HANDLER_FINISHED;
2283 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
2284 scgi_set_state(srv, hctx, FCGI_STATE_WRITE);
2286 /* fall through */
2287 case FCGI_STATE_WRITE:
2288 ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb, MAX_WRITE_LIMIT);
2290 chunkqueue_remove_finished_chunks(hctx->wb);
2292 if (ret < 0) {
2293 if (errno == ENOTCONN || ret == -2) {
2294 /* the connection got dropped after accept()
2296 * this is most of the time a PHP which dies
2297 * after PHP_FCGI_MAX_REQUESTS
2300 if (hctx->wb->bytes_out == 0 &&
2301 hctx->reconnects++ < 5) {
2302 usleep(10000); /* take away the load of the webserver
2303 * to let the php a chance to restart
2306 return scgi_reconnect(srv, hctx);
2309 /* not reconnected ... why
2311 * far@#lighttpd report this for FreeBSD
2315 log_error_write(srv, __FILE__, __LINE__, "ssosd",
2316 "connection was dropped after accept(). reconnect() denied:",
2317 "write-offset:", hctx->wb->bytes_out,
2318 "reconnect attempts:", hctx->reconnects);
2320 return HANDLER_ERROR;
2321 } else {
2322 /* -1 == ret => error on our side */
2323 log_error_write(srv, __FILE__, __LINE__, "ssd",
2324 "write failed:", strerror(errno), errno);
2326 return HANDLER_ERROR;
2330 if (hctx->wb->bytes_out == hctx->wb_reqlen) {
2331 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2332 scgi_set_state(srv, hctx, FCGI_STATE_READ);
2333 } else {
2334 off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
2335 if (hctx->wb->bytes_in < hctx->wb_reqlen && wblen < 65536 - 16384) {
2336 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
2337 if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
2338 con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
2339 con->is_readable = 1; /* trigger optimistic read from client */
2342 if (0 == wblen) {
2343 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2344 } else {
2345 fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2349 return HANDLER_WAIT_FOR_EVENT;
2350 case FCGI_STATE_READ:
2351 /* waiting for a response */
2352 return HANDLER_WAIT_FOR_EVENT;
2353 default:
2354 log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
2355 return HANDLER_ERROR;
2359 static handler_t scgi_send_request(server *srv, handler_ctx *hctx) {
2360 /* ok, create the request */
2361 handler_t rc = scgi_write_request(srv, hctx);
2362 if (HANDLER_ERROR != rc) {
2363 return rc;
2364 } else {
2365 scgi_proc *proc = hctx->proc;
2366 scgi_extension_host *host = hctx->host;
2367 plugin_data *p = hctx->plugin_data;
2368 connection *con = hctx->remote_conn;
2370 if (proc &&
2371 0 == proc->is_local &&
2372 proc->state != PROC_STATE_DISABLED) {
2373 /* only disable remote servers as we don't manage them*/
2375 log_error_write(srv, __FILE__, __LINE__, "sbdb", "fcgi-server disabled:",
2376 host->host,
2377 proc->port,
2378 proc->socket);
2380 /* disable this server */
2381 proc->disable_ts = srv->cur_ts;
2382 proc->state = PROC_STATE_DISABLED;
2383 host->active_procs--;
2386 if (hctx->state == FCGI_STATE_INIT ||
2387 hctx->state == FCGI_STATE_CONNECT) {
2388 /* connect() or getsockopt() failed,
2389 * restart the request-handling
2391 if (proc && proc->is_local) {
2393 if (hctx->conf.debug) {
2394 log_error_write(srv, __FILE__, __LINE__, "sbdb", "connect() to scgi failed, restarting the request-handling:",
2395 host->host,
2396 proc->port,
2397 proc->socket);
2401 * several hctx might reference the same proc
2403 * Only one of them should mark the proc as dead all the other
2404 * ones should just take a new one.
2406 * If a new proc was started with the old struct this might lead
2407 * the mark a perfect proc as dead otherwise
2410 if (proc->state == PROC_STATE_RUNNING &&
2411 hctx->pid == proc->pid) {
2412 proc->state = PROC_STATE_DIED_WAIT_FOR_PID;
2415 scgi_restart_dead_procs(srv, p, host);
2417 return scgi_reconnect(srv, hctx);
2418 } else {
2419 scgi_connection_close(srv, hctx);
2420 con->http_status = 503;
2422 return HANDLER_FINISHED;
2428 static handler_t scgi_recv_response(server *srv, handler_ctx *hctx);
2431 SUBREQUEST_FUNC(mod_scgi_handle_subrequest) {
2432 plugin_data *p = p_d;
2434 handler_ctx *hctx = con->plugin_ctx[p->id];
2436 if (NULL == hctx) return HANDLER_GO_ON;
2438 /* not my job */
2439 if (con->mode != p->id) return HANDLER_GO_ON;
2441 if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
2442 && con->file_started) {
2443 if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
2444 fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
2445 } else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)) {
2446 /* optimistic read from backend, which might re-enable FDEVENT_IN */
2447 handler_t rc = scgi_recv_response(srv, hctx); /*(might invalidate hctx)*/
2448 if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
2452 if (0 == hctx->wb->bytes_in
2453 ? con->state == CON_STATE_READ_POST
2454 : hctx->wb->bytes_in < hctx->wb_reqlen) {
2455 /*(64k - 4k to attempt to avoid temporary files
2456 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
2457 if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096
2458 && (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN)){
2459 con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
2460 if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
2461 } else {
2462 handler_t r = connection_handle_read_post_state(srv, con);
2463 chunkqueue *req_cq = con->request_content_queue;
2464 if (0 != hctx->wb->bytes_in && !chunkqueue_is_empty(req_cq)) {
2465 chunkqueue_append_chunkqueue(hctx->wb, req_cq);
2466 if (fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_OUT) {
2467 return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
2470 if (r != HANDLER_GO_ON) return r;
2472 /* SCGI requires that Content-Length be set.
2473 * Send 411 Length Required if Content-Length missing.
2474 * (occurs here if client sends Transfer-Encoding: chunked
2475 * and module is flagged to stream request body to backend) */
2476 if (-1 == con->request.content_length) {
2477 return connection_handle_read_post_error(srv, con, 411);
2482 return ((0 == hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
2483 && hctx->state != FCGI_STATE_CONNECT)
2484 ? scgi_send_request(srv, hctx)
2485 : HANDLER_WAIT_FOR_EVENT;
2489 static handler_t scgi_recv_response(server *srv, handler_ctx *hctx) {
2491 switch (scgi_demux_response(srv, hctx)) {
2492 case 0:
2493 break;
2494 case 1:
2495 /* we are done */
2496 scgi_connection_close(srv, hctx);
2498 return HANDLER_FINISHED;
2499 case -1: {
2500 connection *con = hctx->remote_conn;
2501 plugin_data *p = hctx->plugin_data;
2503 scgi_proc *proc = hctx->proc;
2504 scgi_extension_host *host= hctx->host;
2506 if (proc->pid && proc->state != PROC_STATE_DIED) {
2507 int status;
2509 /* only fetch the zombie if it is not already done */
2511 switch(waitpid(proc->pid, &status, WNOHANG)) {
2512 case 0:
2513 /* child is still alive */
2514 break;
2515 case -1:
2516 break;
2517 default:
2518 /* the child should not terminate at all */
2519 if (WIFEXITED(status)) {
2520 log_error_write(srv, __FILE__, __LINE__, "sdsd",
2521 "child exited, pid:", proc->pid,
2522 "status:", WEXITSTATUS(status));
2523 } else if (WIFSIGNALED(status)) {
2524 log_error_write(srv, __FILE__, __LINE__, "sd",
2525 "child signaled:",
2526 WTERMSIG(status));
2527 } else {
2528 log_error_write(srv, __FILE__, __LINE__, "sd",
2529 "child died somehow:",
2530 status);
2533 if (hctx->conf.debug) {
2534 log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
2535 "--- scgi spawning",
2536 "\n\tport:", host->port,
2537 "\n\tsocket", host->unixsocket,
2538 "\n\tcurrent:", 1, "/", host->min_procs);
2541 if (scgi_spawn_connection(srv, p, host, proc)) {
2542 /* child died */
2543 proc->state = PROC_STATE_DIED;
2544 } else {
2545 scgi_proclist_sort_down(srv, host, proc);
2548 break;
2552 if (con->file_started == 0) {
2553 /* nothing has been send out yet, try to use another child */
2555 if (hctx->wb->bytes_out == 0 &&
2556 hctx->reconnects++ < 5) {
2558 log_error_write(srv, __FILE__, __LINE__, "ssdsd",
2559 "response not sent, request not sent, reconnection.",
2560 "connection-fd:", con->fd,
2561 "fcgi-fd:", hctx->fd);
2563 return scgi_reconnect(srv, hctx);
2566 log_error_write(srv, __FILE__, __LINE__, "sosdsd",
2567 "response not sent, request sent:", hctx->wb->bytes_out,
2568 "connection-fd:", con->fd,
2569 "fcgi-fd:", hctx->fd);
2570 } else {
2571 log_error_write(srv, __FILE__, __LINE__, "ssdsd",
2572 "response already sent out, termination connection",
2573 "connection-fd:", con->fd,
2574 "fcgi-fd:", hctx->fd);
2577 http_response_backend_error(srv, con);
2578 scgi_connection_close(srv, hctx);
2579 return HANDLER_FINISHED;
2583 return HANDLER_GO_ON;
2587 static handler_t scgi_handle_fdevent(server *srv, void *ctx, int revents) {
2588 handler_ctx *hctx = ctx;
2589 connection *con = hctx->remote_conn;
2591 joblist_append(srv, con);
2593 if (revents & FDEVENT_IN) {
2594 handler_t rc = scgi_recv_response(srv, hctx);/*(might invalidate hctx)*/
2595 if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
2598 if (revents & FDEVENT_OUT) {
2599 return scgi_send_request(srv, hctx); /*(might invalidate hctx)*/
2602 /* perhaps this issue is already handled */
2603 if (revents & FDEVENT_HUP) {
2604 if (hctx->state == FCGI_STATE_CONNECT) {
2605 /* getoptsock will catch this one (right ?)
2607 * if we are in connect we might get a EINPROGRESS
2608 * in the first call and a FDEVENT_HUP in the
2609 * second round
2611 * FIXME: as it is a bit ugly.
2614 scgi_send_request(srv, hctx);
2615 } else if (con->file_started) {
2616 /* drain any remaining data from kernel pipe buffers
2617 * even if (con->conf.stream_response_body
2618 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
2619 * since event loop will spin on fd FDEVENT_HUP event
2620 * until unregistered. */
2621 handler_t rc;
2622 do {
2623 rc = scgi_recv_response(srv,hctx);/*(might invalidate hctx)*/
2624 } while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
2625 return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
2626 } else {
2627 scgi_extension_host *host= hctx->host;
2628 log_error_write(srv, __FILE__, __LINE__, "sbSBSDSd",
2629 "error: unexpected close of scgi connection for",
2630 con->uri.path,
2631 "(no scgi process on host: ",
2632 host->host,
2633 ", port: ",
2634 host->port,
2635 " ?)",
2636 hctx->state);
2638 scgi_connection_close(srv, hctx);
2640 } else if (revents & FDEVENT_ERR) {
2641 log_error_write(srv, __FILE__, __LINE__, "s",
2642 "fcgi: got a FDEVENT_ERR. Don't know why.");
2644 http_response_backend_error(srv, con);
2645 scgi_connection_close(srv, hctx);
2648 return HANDLER_FINISHED;
2650 #define PATCH(x) \
2651 p->conf.x = s->x;
2652 static int scgi_patch_connection(server *srv, connection *con, plugin_data *p) {
2653 size_t i, j;
2654 plugin_config *s = p->config_storage[0];
2656 PATCH(exts);
2657 PATCH(proto);
2658 PATCH(debug);
2660 /* skip the first, the global context */
2661 for (i = 1; i < srv->config_context->used; i++) {
2662 data_config *dc = (data_config *)srv->config_context->data[i];
2663 s = p->config_storage[i];
2665 /* condition didn't match */
2666 if (!config_check_cond(srv, con, dc)) continue;
2668 /* merge config */
2669 for (j = 0; j < dc->value->used; j++) {
2670 data_unset *du = dc->value->data[j];
2672 if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.server"))) {
2673 PATCH(exts);
2674 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.protocol"))) {
2675 PATCH(proto);
2676 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.debug"))) {
2677 PATCH(debug);
2682 return 0;
2684 #undef PATCH
2687 static handler_t scgi_check_extension(server *srv, connection *con, void *p_d, int uri_path_handler) {
2688 plugin_data *p = p_d;
2689 size_t s_len, uri_path_len;
2690 size_t k;
2691 buffer *fn;
2692 scgi_extension *extension = NULL;
2693 scgi_extension_host *host = NULL;
2695 if (con->mode != DIRECT) return HANDLER_GO_ON;
2697 /* Possibly, we processed already this request */
2698 if (con->file_started == 1) return HANDLER_GO_ON;
2700 fn = uri_path_handler ? con->uri.path : con->physical.path;
2702 if (buffer_string_is_empty(fn)) return HANDLER_GO_ON;
2704 s_len = buffer_string_length(fn);
2705 uri_path_len = buffer_string_length(con->uri.path);
2707 scgi_patch_connection(srv, con, p);
2709 /* check if extension matches */
2710 for (k = 0; k < p->conf.exts->used; k++) {
2711 size_t ct_len;
2712 scgi_extension *ext = p->conf.exts->exts[k];
2714 if (buffer_is_empty(ext->key)) continue;
2716 ct_len = buffer_string_length(ext->key);
2718 /* check _url_ in the form "/scgi_pattern" */
2719 if (ext->key->ptr[0] == '/') {
2720 if (ct_len <= uri_path_len
2721 && 0 == strncmp(con->uri.path->ptr, ext->key->ptr, ct_len)) {
2722 extension = ext;
2723 break;
2725 } else if (ct_len <= s_len
2726 && 0 == strncmp(fn->ptr + s_len - ct_len, ext->key->ptr, ct_len)) {
2727 /* check extension in the form ".fcg" */
2728 extension = ext;
2729 break;
2733 /* extension doesn't match */
2734 if (NULL == extension) {
2735 return HANDLER_GO_ON;
2738 /* get best server */
2739 host = scgi_extension_host_get(srv, con, p, extension);
2740 if (NULL == host) {
2741 return HANDLER_FINISHED;
2744 /* a note about no handler is not sent yet */
2745 extension->note_is_sent = 0;
2748 * if check-local is disabled, use the uri.path handler
2752 /* init handler-context */
2753 if (uri_path_handler) {
2754 if (host->check_local == 0) {
2755 handler_ctx *hctx;
2756 char *pathinfo;
2758 hctx = handler_ctx_init();
2760 hctx->remote_conn = con;
2761 hctx->plugin_data = p;
2762 hctx->host = host;
2763 hctx->proc = NULL;
2764 hctx->ext = extension;
2766 /*hctx->conf.exts = p->conf.exts;*/
2767 hctx->conf.proto = p->conf.proto;
2768 hctx->conf.debug = p->conf.debug;
2770 con->plugin_ctx[p->id] = hctx;
2772 host->load++;
2774 con->mode = p->id;
2776 if (con->conf.log_request_handling) {
2777 log_error_write(srv, __FILE__, __LINE__, "s",
2778 "handling it in mod_scgi");
2781 /* the prefix is the SCRIPT_NAME,
2782 * everything from start to the next slash
2783 * this is important for check-local = "disable"
2785 * if prefix = /admin.fcgi
2787 * /admin.fcgi/foo/bar
2789 * SCRIPT_NAME = /admin.fcgi
2790 * PATH_INFO = /foo/bar
2792 * if prefix = /fcgi-bin/
2794 * /fcgi-bin/foo/bar
2796 * SCRIPT_NAME = /fcgi-bin/foo
2797 * PATH_INFO = /bar
2801 /* the rewrite is only done for /prefix/? matches */
2802 if (host->fix_root_path_name && extension->key->ptr[0] == '/' && extension->key->ptr[1] == '\0') {
2803 buffer_copy_string(con->request.pathinfo, con->uri.path->ptr);
2804 buffer_string_set_length(con->uri.path, 0);
2805 } else if (extension->key->ptr[0] == '/' &&
2806 buffer_string_length(con->uri.path) > buffer_string_length(extension->key) &&
2807 NULL != (pathinfo = strchr(con->uri.path->ptr + buffer_string_length(extension->key), '/'))) {
2808 /* rewrite uri.path and pathinfo */
2810 buffer_copy_string(con->request.pathinfo, pathinfo);
2811 buffer_string_set_length(con->uri.path, buffer_string_length(con->uri.path) - buffer_string_length(con->request.pathinfo));
2814 } else {
2815 handler_ctx *hctx;
2816 hctx = handler_ctx_init();
2818 hctx->remote_conn = con;
2819 hctx->plugin_data = p;
2820 hctx->host = host;
2821 hctx->proc = NULL;
2822 hctx->ext = extension;
2824 /*hctx->conf.exts = p->conf.exts;*/
2825 hctx->conf.proto = p->conf.proto;
2826 hctx->conf.debug = p->conf.debug;
2828 con->plugin_ctx[p->id] = hctx;
2830 host->load++;
2832 con->mode = p->id;
2834 if (con->conf.log_request_handling) {
2835 log_error_write(srv, __FILE__, __LINE__, "s", "handling it in mod_scgi");
2839 return HANDLER_GO_ON;
2842 /* uri-path handler */
2843 static handler_t scgi_check_extension_1(server *srv, connection *con, void *p_d) {
2844 return scgi_check_extension(srv, con, p_d, 1);
2847 /* start request handler */
2848 static handler_t scgi_check_extension_2(server *srv, connection *con, void *p_d) {
2849 return scgi_check_extension(srv, con, p_d, 0);
2853 TRIGGER_FUNC(mod_scgi_handle_trigger) {
2854 plugin_data *p = p_d;
2855 size_t i, j, n;
2858 /* perhaps we should kill a connect attempt after 10-15 seconds
2860 * currently we wait for the TCP timeout which is on Linux 180 seconds
2866 /* check all childs if they are still up */
2868 for (i = 0; i < srv->config_context->used; i++) {
2869 plugin_config *conf;
2870 scgi_exts *exts;
2872 conf = p->config_storage[i];
2874 exts = conf->exts;
2876 for (j = 0; j < exts->used; j++) {
2877 scgi_extension *ex;
2879 ex = exts->exts[j];
2881 for (n = 0; n < ex->used; n++) {
2883 scgi_proc *proc;
2884 unsigned long sum_load = 0;
2885 scgi_extension_host *host;
2887 host = ex->hosts[n];
2889 scgi_restart_dead_procs(srv, p, host);
2891 for (proc = host->first; proc; proc = proc->next) {
2892 sum_load += proc->load;
2895 if (host->num_procs &&
2896 host->num_procs < host->max_procs &&
2897 (sum_load / host->num_procs) > host->max_load_per_proc) {
2898 /* overload, spawn new child */
2899 scgi_proc *fp = NULL;
2901 if (p->conf.debug) {
2902 log_error_write(srv, __FILE__, __LINE__, "s",
2903 "overload detected, spawning a new child");
2906 for (fp = host->unused_procs; fp && fp->pid != 0; fp = fp->next);
2908 if (fp) {
2909 if (fp == host->unused_procs) host->unused_procs = fp->next;
2911 if (fp->next) fp->next->prev = NULL;
2913 host->max_id++;
2914 } else {
2915 fp = scgi_process_init();
2916 fp->id = host->max_id++;
2919 host->num_procs++;
2921 if (buffer_string_is_empty(host->unixsocket)) {
2922 fp->port = host->port + fp->id;
2923 } else {
2924 buffer_copy_buffer(fp->socket, host->unixsocket);
2925 buffer_append_string_len(fp->socket, CONST_STR_LEN("-"));
2926 buffer_append_int(fp->socket, fp->id);
2929 if (scgi_spawn_connection(srv, p, host, fp)) {
2930 log_error_write(srv, __FILE__, __LINE__, "s",
2931 "ERROR: spawning fcgi failed.");
2932 scgi_process_free(fp);
2933 return HANDLER_ERROR;
2936 fp->prev = NULL;
2937 fp->next = host->first;
2938 if (host->first) {
2939 host->first->prev = fp;
2941 host->first = fp;
2944 for (proc = host->first; proc; proc = proc->next) {
2945 if (proc->load != 0) break;
2946 if (host->num_procs <= host->min_procs) break;
2947 if (proc->pid == 0) continue;
2949 if (srv->cur_ts - proc->last_used > host->idle_timeout) {
2950 /* a proc is idling for a long time now,
2951 * terminated it */
2953 if (p->conf.debug) {
2954 log_error_write(srv, __FILE__, __LINE__, "ssbsd",
2955 "idle-timeout reached, terminating child:",
2956 "socket:", proc->socket,
2957 "pid", proc->pid);
2961 if (proc->next) proc->next->prev = proc->prev;
2962 if (proc->prev) proc->prev->next = proc->next;
2964 if (proc->prev == NULL) host->first = proc->next;
2966 proc->prev = NULL;
2967 proc->next = host->unused_procs;
2969 if (host->unused_procs) host->unused_procs->prev = proc;
2970 host->unused_procs = proc;
2972 kill(proc->pid, SIGTERM);
2974 proc->state = PROC_STATE_KILLED;
2976 log_error_write(srv, __FILE__, __LINE__, "ssbsd",
2977 "killed:",
2978 "socket:", proc->socket,
2979 "pid", proc->pid);
2981 host->num_procs--;
2983 /* proc is now in unused, let the next second handle the next process */
2984 break;
2988 for (proc = host->unused_procs; proc; proc = proc->next) {
2989 int status;
2991 if (proc->pid == 0) continue;
2993 switch (waitpid(proc->pid, &status, WNOHANG)) {
2994 case 0:
2995 /* child still running after timeout, good */
2996 break;
2997 case -1:
2998 if (errno != EINTR) {
2999 /* no PID found ? should never happen */
3000 log_error_write(srv, __FILE__, __LINE__, "sddss",
3001 "pid ", proc->pid, proc->state,
3002 "not found:", strerror(errno));
3004 #if 0
3005 if (errno == ECHILD) {
3006 /* someone else has cleaned up for us */
3007 proc->pid = 0;
3008 proc->state = PROC_STATE_UNSET;
3010 #endif
3012 break;
3013 default:
3014 /* the child should not terminate at all */
3015 if (WIFEXITED(status)) {
3016 if (proc->state != PROC_STATE_KILLED) {
3017 log_error_write(srv, __FILE__, __LINE__, "sdb",
3018 "child exited:",
3019 WEXITSTATUS(status), proc->socket);
3021 } else if (WIFSIGNALED(status)) {
3022 if (WTERMSIG(status) != SIGTERM) {
3023 log_error_write(srv, __FILE__, __LINE__, "sd",
3024 "child signaled:",
3025 WTERMSIG(status));
3027 } else {
3028 log_error_write(srv, __FILE__, __LINE__, "sd",
3029 "child died somehow:",
3030 status);
3032 proc->pid = 0;
3033 proc->state = PROC_STATE_UNSET;
3034 host->max_id--;
3041 return HANDLER_GO_ON;
3045 int mod_scgi_plugin_init(plugin *p);
3046 int mod_scgi_plugin_init(plugin *p) {
3047 p->version = LIGHTTPD_VERSION_ID;
3048 p->name = buffer_init_string("scgi");
3050 p->init = mod_scgi_init;
3051 p->cleanup = mod_scgi_free;
3052 p->set_defaults = mod_scgi_set_defaults;
3053 p->connection_reset = scgi_connection_reset;
3054 p->handle_connection_close = scgi_connection_reset;
3055 p->handle_uri_clean = scgi_check_extension_1;
3056 p->handle_subrequest_start = scgi_check_extension_2;
3057 p->handle_subrequest = mod_scgi_handle_subrequest;
3058 p->handle_trigger = mod_scgi_handle_trigger;
3060 p->data = NULL;
3062 return 0;