1: #include "first.h"
2:
3: #include "buffer.h"
4: #include "server.h"
5: #include "keyvalue.h"
6: #include "log.h"
7:
8: #include "http_chunk.h"
9: #include "fdevent.h"
10: #include "connections.h"
11: #include "response.h"
12: #include "joblist.h"
13:
14: #include "plugin.h"
15:
16: #include "inet_ntop_cache.h"
17:
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>
27:
28: #include <stdio.h>
29:
30: #include "sys-socket.h"
31:
32: #ifdef HAVE_SYS_UIO_H
33: # include <sys/uio.h>
34: #endif
35:
36: #ifdef HAVE_SYS_WAIT_H
37: # include <sys/wait.h>
38: #endif
39:
40: enum {EOL_UNSET, EOL_N, EOL_RN};
41:
42: /*
43: *
44: * TODO:
45: *
46: * - add timeout for a connect to a non-scgi process
47: * (use state_timestamp + state)
48: *
49: */
50:
51: typedef struct scgi_proc {
52: size_t id; /* id will be between 1 and max_procs */
53: buffer *socket; /* config.socket + "-" + id */
54: unsigned port; /* config.port + pno */
55:
56: pid_t pid; /* PID of the spawned process (0 if not spawned locally) */
57:
58:
59: size_t load; /* number of requests waiting on this process */
60:
61: time_t last_used; /* see idle_timeout */
62: size_t requests; /* see max_requests */
63: struct scgi_proc *prev, *next; /* see first */
64:
65: time_t disable_ts; /* replace by host->something */
66:
67: int is_local;
68:
69: enum { PROC_STATE_UNSET, /* init-phase */
70: PROC_STATE_RUNNING, /* alive */
71: PROC_STATE_DIED_WAIT_FOR_PID,
72: PROC_STATE_KILLED, /* was killed as we don't have the load anymore */
73: PROC_STATE_DIED, /* marked as dead, should be restarted */
74: PROC_STATE_DISABLED /* proc disabled as it resulted in an error */
75: } state;
76: } scgi_proc;
77:
78: typedef struct {
79: /* list of processes handling this extension
80: * sorted by lowest load
81: *
82: * whenever a job is done move it up in the list
83: * until it is sorted, move it down as soon as the
84: * job is started
85: */
86: scgi_proc *first;
87: scgi_proc *unused_procs;
88:
89: /*
90: * spawn at least min_procs, at max_procs.
91: *
92: * as soon as the load of the first entry
93: * is max_load_per_proc we spawn a new one
94: * and add it to the first entry and give it
95: * the load
96: *
97: */
98:
99: unsigned short min_procs;
100: unsigned short max_procs;
101: size_t num_procs; /* how many procs are started */
102: size_t active_procs; /* how many of them are really running */
103:
104: unsigned short max_load_per_proc;
105:
106: /*
107: * kick the process from the list if it was not
108: * used for idle_timeout until min_procs is
109: * reached. this helps to get the processlist
110: * small again we had a small peak load.
111: *
112: */
113:
114: unsigned short idle_timeout;
115:
116: /*
117: * time after a disabled remote connection is tried to be re-enabled
118: *
119: *
120: */
121:
122: unsigned short disable_time;
123:
124: /*
125: * same scgi processes get a little bit larger
126: * than wanted. max_requests_per_proc kills a
127: * process after a number of handled requests.
128: *
129: */
130: size_t max_requests_per_proc;
131:
132:
133: /* config */
134:
135: /*
136: * host:port
137: *
138: * if host is one of the local IP adresses the
139: * whole connection is local
140: *
141: * if tcp/ip should be used host AND port have
142: * to be specified
143: *
144: */
145: buffer *host;
146: unsigned short port;
147: sa_family_t family;
148:
149: /*
150: * Unix Domain Socket
151: *
152: * instead of TCP/IP we can use Unix Domain Sockets
153: * - more secure (you have fileperms to play with)
154: * - more control (on locally)
155: * - more speed (no extra overhead)
156: */
157: buffer *unixsocket;
158:
159: /* if socket is local we can start the scgi
160: * process ourself
161: *
162: * bin-path is the path to the binary
163: *
164: * check min_procs and max_procs for the number
165: * of process to start-up
166: */
167: buffer *bin_path;
168:
169: /* bin-path is set bin-environment is taken to
170: * create the environement before starting the
171: * FastCGI process
172: *
173: */
174: array *bin_env;
175:
176: array *bin_env_copy;
177:
178: /*
179: * docroot-translation between URL->phys and the
180: * remote host
181: *
182: * reasons:
183: * - different dir-layout if remote
184: * - chroot if local
185: *
186: */
187: buffer *docroot;
188:
189: /*
190: * check_local tell you if the phys file is stat()ed
191: * or not. FastCGI doesn't care if the service is
192: * remote. If the web-server side doesn't contain
193: * the scgi-files we should not stat() for them
194: * and say '404 not found'.
195: */
196: unsigned short check_local;
197:
198: /*
199: * append PATH_INFO to SCRIPT_FILENAME
200: *
201: * php needs this if cgi.fix_pathinfo is provied
202: *
203: */
204:
205: /*
206: * workaround for program when prefix="/"
207: *
208: * rule to build PATH_INFO is hardcoded for when check_local is disabled
209: * enable this option to use the workaround
210: *
211: */
212:
213: unsigned short fix_root_path_name;
214:
215: /*
216: * If the backend includes X-Sendfile in the response
217: * we use the value as filename and ignore the content.
218: *
219: */
220: unsigned short xsendfile_allow;
221: array *xsendfile_docroot;
222:
223: ssize_t load; /* replace by host->load */
224:
225: size_t max_id; /* corresponds most of the time to
226: num_procs.
227:
228: only if a process is killed max_id waits for the process itself
229: to die and decrements its afterwards */
230:
231: int listen_backlog;
232: int refcount;
233: } scgi_extension_host;
234:
235: /*
236: * one extension can have multiple hosts assigned
237: * one host can spawn additional processes on the same
238: * socket (if we control it)
239: *
240: * ext -> host -> procs
241: * 1:n 1:n
242: *
243: * if the scgi process is remote that whole goes down
244: * to
245: *
246: * ext -> host -> procs
247: * 1:n 1:1
248: *
249: * in case of PHP and FCGI_CHILDREN we have again a procs
250: * but we don't control it directly.
251: *
252: */
253:
254: typedef struct {
255: buffer *key; /* like .php */
256:
257: int note_is_sent;
258: scgi_extension_host **hosts;
259:
260: size_t used;
261: size_t size;
262: } scgi_extension;
263:
264: typedef struct {
265: scgi_extension **exts;
266:
267: size_t used;
268: size_t size;
269: } scgi_exts;
270:
271:
272: typedef struct {
273: scgi_exts *exts;
274:
275: int debug;
276: } plugin_config;
277:
278: typedef struct {
279: char **ptr;
280:
281: size_t size;
282: size_t used;
283: } char_array;
284:
285: /* generic plugin data, shared between all connections */
286: typedef struct {
287: PLUGIN_DATA;
288:
289: buffer *scgi_env;
290:
291: buffer *path;
292: buffer *parse_response;
293:
294: plugin_config **config_storage;
295:
296: plugin_config conf; /* this is only used as long as no handler_ctx is setup */
297: } plugin_data;
298:
299: /* connection specific data */
300: typedef enum { FCGI_STATE_INIT, FCGI_STATE_CONNECT, FCGI_STATE_PREPARE_WRITE,
301: FCGI_STATE_WRITE, FCGI_STATE_READ
302: } scgi_connection_state_t;
303:
304: typedef struct {
305: buffer *response;
306:
307: scgi_proc *proc;
308: scgi_extension_host *host;
309:
310: scgi_connection_state_t state;
311: time_t state_timestamp;
312:
313: chunkqueue *wb;
314: off_t wb_reqlen;
315:
316: buffer *response_header;
317:
318: int fd; /* fd to the scgi process */
319: int fde_ndx; /* index into the fd-event buffer */
320:
321: pid_t pid;
322: int got_proc;
323: int reconnects; /* number of reconnect attempts */
324:
325: plugin_config conf;
326:
327: connection *remote_conn; /* dumb pointer */
328: plugin_data *plugin_data; /* dumb pointer */
329: } handler_ctx;
330:
331:
332: /* ok, we need a prototype */
333: static handler_t scgi_handle_fdevent(server *srv, void *ctx, int revents);
334:
335: int scgi_proclist_sort_down(server *srv, scgi_extension_host *host, scgi_proc *proc);
336:
337: static void reset_signals(void) {
338: #ifdef SIGTTOU
339: signal(SIGTTOU, SIG_DFL);
340: #endif
341: #ifdef SIGTTIN
342: signal(SIGTTIN, SIG_DFL);
343: #endif
344: #ifdef SIGTSTP
345: signal(SIGTSTP, SIG_DFL);
346: #endif
347: signal(SIGHUP, SIG_DFL);
348: signal(SIGPIPE, SIG_DFL);
349: signal(SIGUSR1, SIG_DFL);
350: }
351:
352: static handler_ctx * handler_ctx_init(void) {
353: handler_ctx * hctx;
354:
355: hctx = calloc(1, sizeof(*hctx));
356: force_assert(hctx);
357:
358: hctx->fde_ndx = -1;
359:
360: hctx->response = buffer_init();
361: hctx->response_header = buffer_init();
362:
363: hctx->state = FCGI_STATE_INIT;
364: hctx->proc = NULL;
365:
366: hctx->fd = -1;
367:
368: hctx->reconnects = 0;
369:
370: hctx->wb = chunkqueue_init();
371: hctx->wb_reqlen = 0;
372:
373: return hctx;
374: }
375:
376: static void handler_ctx_free(handler_ctx *hctx) {
377: buffer_free(hctx->response);
378: buffer_free(hctx->response_header);
379:
380: chunkqueue_free(hctx->wb);
381:
382: free(hctx);
383: }
384:
385: static scgi_proc *scgi_process_init(void) {
386: scgi_proc *f;
387:
388: f = calloc(1, sizeof(*f));
389: force_assert(f);
390: f->socket = buffer_init();
391:
392: f->prev = NULL;
393: f->next = NULL;
394:
395: return f;
396: }
397:
398: static void scgi_process_free(scgi_proc *f) {
399: if (!f) return;
400:
401: scgi_process_free(f->next);
402:
403: buffer_free(f->socket);
404:
405: free(f);
406: }
407:
408: static scgi_extension_host *scgi_host_init(void) {
409: scgi_extension_host *f;
410:
411: f = calloc(1, sizeof(*f));
412:
413: f->host = buffer_init();
414: f->unixsocket = buffer_init();
415: f->docroot = buffer_init();
416: f->bin_path = buffer_init();
417: f->bin_env = array_init();
418: f->bin_env_copy = array_init();
419: f->xsendfile_docroot = array_init();
420:
421: return f;
422: }
423:
424: static void scgi_host_free(scgi_extension_host *h) {
425: if (!h) return;
426: if (h->refcount) {
427: --h->refcount;
428: return;
429: }
430:
431: buffer_free(h->host);
432: buffer_free(h->unixsocket);
433: buffer_free(h->docroot);
434: buffer_free(h->bin_path);
435: array_free(h->bin_env);
436: array_free(h->bin_env_copy);
437: array_free(h->xsendfile_docroot);
438:
439: scgi_process_free(h->first);
440: scgi_process_free(h->unused_procs);
441:
442: free(h);
443:
444: }
445:
446: static scgi_exts *scgi_extensions_init(void) {
447: scgi_exts *f;
448:
449: f = calloc(1, sizeof(*f));
450: force_assert(f);
451:
452: return f;
453: }
454:
455: static void scgi_extensions_free(scgi_exts *f) {
456: size_t i;
457:
458: if (!f) return;
459:
460: for (i = 0; i < f->used; i++) {
461: scgi_extension *fe;
462: size_t j;
463:
464: fe = f->exts[i];
465:
466: for (j = 0; j < fe->used; j++) {
467: scgi_extension_host *h;
468:
469: h = fe->hosts[j];
470:
471: scgi_host_free(h);
472: }
473:
474: buffer_free(fe->key);
475: free(fe->hosts);
476:
477: free(fe);
478: }
479:
480: free(f->exts);
481:
482: free(f);
483: }
484:
485: static int scgi_extension_insert(scgi_exts *ext, buffer *key, scgi_extension_host *fh) {
486: scgi_extension *fe;
487: size_t i;
488:
489: /* there is something */
490:
491: for (i = 0; i < ext->used; i++) {
492: if (buffer_is_equal(key, ext->exts[i]->key)) {
493: break;
494: }
495: }
496:
497: if (i == ext->used) {
498: /* filextension is new */
499: fe = calloc(1, sizeof(*fe));
500: force_assert(fe);
501: fe->key = buffer_init();
502: buffer_copy_buffer(fe->key, key);
503:
504: /* */
505:
506: if (ext->size == 0) {
507: ext->size = 8;
508: ext->exts = malloc(ext->size * sizeof(*(ext->exts)));
509: force_assert(ext->exts);
510: } else if (ext->used == ext->size) {
511: ext->size += 8;
512: ext->exts = realloc(ext->exts, ext->size * sizeof(*(ext->exts)));
513: force_assert(ext->exts);
514: }
515: ext->exts[ext->used++] = fe;
516: } else {
517: fe = ext->exts[i];
518: }
519:
520: if (fe->size == 0) {
521: fe->size = 4;
522: fe->hosts = malloc(fe->size * sizeof(*(fe->hosts)));
523: force_assert(fe->hosts);
524: } else if (fe->size == fe->used) {
525: fe->size += 4;
526: fe->hosts = realloc(fe->hosts, fe->size * sizeof(*(fe->hosts)));
527: force_assert(fe->hosts);
528: }
529:
530: fe->hosts[fe->used++] = fh;
531:
532: return 0;
533:
534: }
535:
536: INIT_FUNC(mod_scgi_init) {
537: plugin_data *p;
538:
539: p = calloc(1, sizeof(*p));
540: force_assert(p);
541:
542: p->scgi_env = buffer_init();
543:
544: p->path = buffer_init();
545: p->parse_response = buffer_init();
546:
547: return p;
548: }
549:
550:
551: FREE_FUNC(mod_scgi_free) {
552: plugin_data *p = p_d;
553:
554: UNUSED(srv);
555:
556: buffer_free(p->scgi_env);
557: buffer_free(p->path);
558: buffer_free(p->parse_response);
559:
560: if (p->config_storage) {
561: size_t i, j, n;
562: for (i = 0; i < srv->config_context->used; i++) {
563: plugin_config *s = p->config_storage[i];
564: scgi_exts *exts;
565:
566: if (NULL == s) continue;
567:
568: exts = s->exts;
569:
570: for (j = 0; j < exts->used; j++) {
571: scgi_extension *ex;
572:
573: ex = exts->exts[j];
574:
575: for (n = 0; n < ex->used; n++) {
576: scgi_proc *proc;
577: scgi_extension_host *host;
578:
579: host = ex->hosts[n];
580:
581: for (proc = host->first; proc; proc = proc->next) {
582: if (proc->pid != 0) kill(proc->pid, SIGTERM);
583:
584: if (proc->is_local &&
585: !buffer_string_is_empty(proc->socket)) {
586: unlink(proc->socket->ptr);
587: }
588: }
589:
590: for (proc = host->unused_procs; proc; proc = proc->next) {
591: if (proc->pid != 0) kill(proc->pid, SIGTERM);
592:
593: if (proc->is_local &&
594: !buffer_string_is_empty(proc->socket)) {
595: unlink(proc->socket->ptr);
596: }
597: }
598: }
599: }
600:
601: scgi_extensions_free(s->exts);
602:
603: free(s);
604: }
605: free(p->config_storage);
606: }
607:
608: free(p);
609:
610: return HANDLER_GO_ON;
611: }
612:
613: static int env_add(char_array *env, const char *key, size_t key_len, const char *val, size_t val_len) {
614: char *dst;
615: size_t i;
616:
617: if (!key || !val) return -1;
618:
619: dst = malloc(key_len + val_len + 3);
620: force_assert(dst);
621: memcpy(dst, key, key_len);
622: dst[key_len] = '=';
623: /* add the \0 from the value */
624: memcpy(dst + key_len + 1, val, val_len + 1);
625:
626: for (i = 0; i < env->used; i++) {
627: if (0 == strncmp(dst, env->ptr[i], key_len + 1)) {
628: /* don't care about free as we are in a forked child which is going to exec(...) */
629: /* free(env->ptr[i]); */
630: env->ptr[i] = dst;
631: return 0;
632: }
633: }
634:
635: if (env->size == 0) {
636: env->size = 16;
637: env->ptr = malloc(env->size * sizeof(*env->ptr));
638: force_assert(env->ptr);
639: } else if (env->size == env->used) {
640: env->size += 16;
641: env->ptr = realloc(env->ptr, env->size * sizeof(*env->ptr));
642: force_assert(env->ptr);
643: }
644:
645: env->ptr[env->used++] = dst;
646:
647: return 0;
648: }
649:
650: #if !defined(HAVE_FORK)
651: static int scgi_spawn_connection(server *srv,
652: plugin_data *p,
653: scgi_extension_host *host,
654: scgi_proc *proc) {
655: UNUSED(srv);
656: UNUSED(p);
657: UNUSED(host);
658: UNUSED(proc);
659: return -1;
660: }
661:
662: #else /* -> defined(HAVE_FORK) */
663:
664: static int scgi_spawn_connection(server *srv,
665: plugin_data *p,
666: scgi_extension_host *host,
667: scgi_proc *proc) {
668: int scgi_fd;
669: int status;
670: struct timeval tv = { 0, 100 * 1000 };
671: #ifdef HAVE_SYS_UN_H
672: struct sockaddr_un scgi_addr_un;
673: #endif
674: #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
675: struct sockaddr_in6 scgi_addr_in6;
676: #endif
677: struct sockaddr_in scgi_addr_in;
678: struct sockaddr *scgi_addr;
679:
680: socklen_t servlen;
681:
682: if (p->conf.debug) {
683: log_error_write(srv, __FILE__, __LINE__, "sdb",
684: "new proc, socket:", proc->port, proc->socket);
685: }
686:
687:
688: if (!buffer_string_is_empty(proc->socket)) {
689: #ifdef HAVE_SYS_UN_H
690: memset(&scgi_addr_un, 0, sizeof(scgi_addr_un));
691: scgi_addr_un.sun_family = AF_UNIX;
692: if (buffer_string_length(proc->socket) + 1 > sizeof(scgi_addr_un.sun_path)) {
693: log_error_write(srv, __FILE__, __LINE__, "sB",
694: "ERROR: Unix Domain socket filename too long:",
695: proc->socket);
696: return -1;
697: }
698: memcpy(scgi_addr_un.sun_path, proc->socket->ptr, buffer_string_length(proc->socket) + 1);
699:
700: #ifdef SUN_LEN
701: servlen = SUN_LEN(&scgi_addr_un);
702: #else
703: /* stevens says: */
704: servlen = buffer_string_length(proc->socket) + 1 + sizeof(scgi_addr_un.sun_family);
705: #endif
706: scgi_addr = (struct sockaddr *) &scgi_addr_un;
707: #else
708: log_error_write(srv, __FILE__, __LINE__, "s",
709: "ERROR: Unix Domain sockets are not supported.");
710: return -1;
711: #endif
712: #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
713: } else if (host->family == AF_INET6 && !buffer_string_is_empty(host->host)) {
714: memset(&scgi_addr_in6, 0, sizeof(scgi_addr_in6));
715: scgi_addr_in6.sin6_family = AF_INET6;
716: inet_pton(AF_INET6, host->host->ptr, (char *) &scgi_addr_in6.sin6_addr);
717: scgi_addr_in6.sin6_port = htons(proc->port);
718: servlen = sizeof(scgi_addr_in6);
719: scgi_addr = (struct sockaddr *) &scgi_addr_in6;
720: #endif
721: } else {
722: memset(&scgi_addr_in, 0, sizeof(scgi_addr_in));
723: scgi_addr_in.sin_family = AF_INET;
724:
725: if (buffer_string_is_empty(host->host)) {
726: scgi_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
727: } else {
728: struct hostent *he;
729:
730: /* set a usefull default */
731: scgi_addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
732:
733:
734: if (NULL == (he = gethostbyname(host->host->ptr))) {
735: log_error_write(srv, __FILE__, __LINE__,
736: "sdb", "gethostbyname failed: ",
737: h_errno, host->host);
738: return -1;
739: }
740:
741: if (he->h_addrtype != AF_INET) {
742: log_error_write(srv, __FILE__, __LINE__, "sd", "addr-type != AF_INET: ", he->h_addrtype);
743: return -1;
744: }
745:
746: if (he->h_length != sizeof(struct in_addr)) {
747: log_error_write(srv, __FILE__, __LINE__, "sd", "addr-length != sizeof(in_addr): ", he->h_length);
748: return -1;
749: }
750:
751: memcpy(&(scgi_addr_in.sin_addr.s_addr), he->h_addr_list[0], he->h_length);
752:
753: }
754: scgi_addr_in.sin_port = htons(proc->port);
755: servlen = sizeof(scgi_addr_in);
756:
757: scgi_addr = (struct sockaddr *) &scgi_addr_in;
758: }
759:
760: if (-1 == (scgi_fd = socket(scgi_addr->sa_family, SOCK_STREAM, 0))) {
761: log_error_write(srv, __FILE__, __LINE__, "ss",
762: "failed:", strerror(errno));
763: return -1;
764: }
765:
766: if (-1 == connect(scgi_fd, scgi_addr, servlen)) {
767: /* server is not up, spawn in */
768: pid_t child;
769: int val;
770:
771: if (!buffer_string_is_empty(proc->socket)) {
772: unlink(proc->socket->ptr);
773: }
774:
775: close(scgi_fd);
776:
777: /* reopen socket */
778: if (-1 == (scgi_fd = socket(scgi_addr->sa_family, SOCK_STREAM, 0))) {
779: log_error_write(srv, __FILE__, __LINE__, "ss",
780: "socket failed:", strerror(errno));
781: return -1;
782: }
783:
784: val = 1;
785: if (setsockopt(scgi_fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
786: log_error_write(srv, __FILE__, __LINE__, "ss",
787: "socketsockopt failed:", strerror(errno));
788: close(scgi_fd);
789: return -1;
790: }
791:
792: /* create socket */
793: if (-1 == bind(scgi_fd, scgi_addr, servlen)) {
794: log_error_write(srv, __FILE__, __LINE__, "sbds",
795: "bind failed for:",
796: proc->socket,
797: proc->port,
798: strerror(errno));
799: close(scgi_fd);
800: return -1;
801: }
802:
803: if (-1 == listen(scgi_fd, host->listen_backlog)) {
804: log_error_write(srv, __FILE__, __LINE__, "ss",
805: "listen failed:", strerror(errno));
806: close(scgi_fd);
807: return -1;
808: }
809:
810: switch ((child = fork())) {
811: case 0: {
812: buffer *b;
813: size_t i = 0;
814: int fd = 0;
815: char_array env;
816:
817:
818: /* create environment */
819: env.ptr = NULL;
820: env.size = 0;
821: env.used = 0;
822:
823: if (scgi_fd != 0) {
824: dup2(scgi_fd, 0);
825: close(scgi_fd);
826: }
827:
828: /* we don't need the client socket */
829: for (fd = 3; fd < 256; fd++) {
830: close(fd);
831: }
832:
833: /* build clean environment */
834: if (host->bin_env_copy->used) {
835: for (i = 0; i < host->bin_env_copy->used; i++) {
836: data_string *ds = (data_string *)host->bin_env_copy->data[i];
837: char *ge;
838:
839: if (NULL != (ge = getenv(ds->value->ptr))) {
840: env_add(&env, CONST_BUF_LEN(ds->value), ge, strlen(ge));
841: }
842: }
843: } else {
844: char ** const e = environ;
845: for (i = 0; e[i]; ++i) {
846: char *eq;
847:
848: if (NULL != (eq = strchr(e[i], '='))) {
849: env_add(&env, e[i], eq - e[i], eq+1, strlen(eq+1));
850: }
851: }
852: }
853:
854: /* create environment */
855: for (i = 0; i < host->bin_env->used; i++) {
856: data_string *ds = (data_string *)host->bin_env->data[i];
857:
858: env_add(&env, CONST_BUF_LEN(ds->key), CONST_BUF_LEN(ds->value));
859: }
860:
861: for (i = 0; i < env.used; i++) {
862: /* search for PHP_FCGI_CHILDREN */
863: if (0 == strncmp(env.ptr[i], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
864: }
865:
866: /* not found, add a default */
867: if (i == env.used) {
868: env_add(&env, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
869: }
870:
871: env.ptr[env.used] = NULL;
872:
873: b = buffer_init();
874: buffer_copy_string_len(b, CONST_STR_LEN("exec "));
875: buffer_append_string_buffer(b, host->bin_path);
876:
877: reset_signals();
878:
879: /* exec the cgi */
880: execle("/bin/sh", "sh", "-c", b->ptr, (char *)NULL, env.ptr);
881:
882: log_error_write(srv, __FILE__, __LINE__, "sbs",
883: "execl failed for:", host->bin_path, strerror(errno));
884:
885: _exit(errno);
886:
887: break;
888: }
889: case -1:
890: /* error */
891: close(scgi_fd);
892: break;
893: default:
894: /* father */
895: close(scgi_fd);
896:
897: /* wait */
898: select(0, NULL, NULL, NULL, &tv);
899:
900: switch (waitpid(child, &status, WNOHANG)) {
901: case 0:
902: /* child still running after timeout, good */
903: break;
904: case -1:
905: /* no PID found ? should never happen */
906: log_error_write(srv, __FILE__, __LINE__, "ss",
907: "pid not found:", strerror(errno));
908: return -1;
909: default:
910: /* the child should not terminate at all */
911: if (WIFEXITED(status)) {
912: log_error_write(srv, __FILE__, __LINE__, "sd",
913: "child exited (is this a SCGI binary ?):",
914: WEXITSTATUS(status));
915: } else if (WIFSIGNALED(status)) {
916: log_error_write(srv, __FILE__, __LINE__, "sd",
917: "child signaled:",
918: WTERMSIG(status));
919: } else {
920: log_error_write(srv, __FILE__, __LINE__, "sd",
921: "child died somehow:",
922: status);
923: }
924: return -1;
925: }
926:
927: /* register process */
928: proc->pid = child;
929: proc->last_used = srv->cur_ts;
930: proc->is_local = 1;
931:
932: break;
933: }
934: } else {
935: close(scgi_fd);
936:
937: proc->is_local = 0;
938: proc->pid = 0;
939:
940: if (p->conf.debug) {
941: log_error_write(srv, __FILE__, __LINE__, "sb",
942: "(debug) socket is already used, won't spawn:",
943: proc->socket);
944: }
945: }
946:
947: proc->state = PROC_STATE_RUNNING;
948: host->active_procs++;
949:
950: return 0;
951: }
952:
953: #endif /* HAVE_FORK */
954:
955: static scgi_extension_host * unixsocket_is_dup(plugin_data *p, size_t used, buffer *unixsocket) {
956: size_t i, j, n;
957: for (i = 0; i < used; ++i) {
958: scgi_exts *exts = p->config_storage[i]->exts;
959: for (j = 0; j < exts->used; ++j) {
960: scgi_extension *ex = exts->exts[j];
961: for (n = 0; n < ex->used; ++n) {
962: scgi_extension_host *host = ex->hosts[n];
963: if (!buffer_string_is_empty(host->unixsocket)
964: && buffer_is_equal(host->unixsocket, unixsocket)
965: && !buffer_string_is_empty(host->bin_path))
966: return host;
967: }
968: }
969: }
970:
971: return NULL;
972: }
973:
974: SETDEFAULTS_FUNC(mod_scgi_set_defaults) {
975: plugin_data *p = p_d;
976: data_unset *du;
977: size_t i = 0;
978: scgi_extension_host *df = NULL;
979:
980: config_values_t cv[] = {
981: { "scgi.server", NULL, T_CONFIG_LOCAL, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
982: { "scgi.debug", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
983: { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
984: };
985:
986: p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
987: force_assert(p->config_storage);
988:
989: for (i = 0; i < srv->config_context->used; i++) {
990: data_config const* config = (data_config const*)srv->config_context->data[i];
991: plugin_config *s;
992:
993: s = malloc(sizeof(plugin_config));
994: force_assert(s);
995: s->exts = scgi_extensions_init();
996: s->debug = 0;
997:
998: cv[0].destination = s->exts;
999: cv[1].destination = &(s->debug);
1000:
1001: p->config_storage[i] = s;
1002:
1003: if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
1004: goto error;
1005: }
1006:
1007: /*
1008: * <key> = ( ... )
1009: */
1010:
1011: if (NULL != (du = array_get_element(config->value, "scgi.server"))) {
1012: size_t j;
1013: data_array *da = (data_array *)du;
1014:
1015: if (du->type != TYPE_ARRAY) {
1016: log_error_write(srv, __FILE__, __LINE__, "sss",
1017: "unexpected type for key: ", "scgi.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1018:
1019: goto error;
1020: }
1021:
1022:
1023: /*
1024: * scgi.server = ( "<ext>" => ( ... ),
1025: * "<ext>" => ( ... ) )
1026: */
1027:
1028: for (j = 0; j < da->value->used; j++) {
1029: size_t n;
1030: data_array *da_ext = (data_array *)da->value->data[j];
1031:
1032: if (da->value->data[j]->type != TYPE_ARRAY) {
1033: log_error_write(srv, __FILE__, __LINE__, "sssbs",
1034: "unexpected type for key: ", "scgi.server",
1035: "[", da->value->data[j]->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1036:
1037: goto error;
1038: }
1039:
1040: /*
1041: * da_ext->key == name of the extension
1042: */
1043:
1044: /*
1045: * scgi.server = ( "<ext>" =>
1046: * ( "<host>" => ( ... ),
1047: * "<host>" => ( ... )
1048: * ),
1049: * "<ext>" => ... )
1050: */
1051:
1052: for (n = 0; n < da_ext->value->used; n++) {
1053: data_array *da_host = (data_array *)da_ext->value->data[n];
1054:
1055: config_values_t fcv[] = {
1056: { "host", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
1057: { "docroot", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
1058: { "socket", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
1059: { "bin-path", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 3 */
1060:
1061: { "check-local", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 4 */
1062: { "port", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 5 */
1063: { "min-procs-not-working", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 this is broken for now */
1064: { "max-procs", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 7 */
1065: { "max-load-per-proc", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 8 */
1066: { "idle-timeout", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 9 */
1067: { "disable-time", NULL, T_CONFIG_SHORT, T_CONFIG_SCOPE_CONNECTION }, /* 10 */
1068:
1069: { "bin-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 11 */
1070: { "bin-copy-environment", NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 12 */
1071: { "fix-root-scriptname", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 13 */
1072: { "listen-backlog", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 14 */
1073: { "x-sendfile", NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_CONNECTION }, /* 15 */
1074: { "x-sendfile-docroot",NULL, T_CONFIG_ARRAY, T_CONFIG_SCOPE_CONNECTION }, /* 16 */
1075:
1076:
1077: { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
1078: };
1079:
1080: if (da_host->type != TYPE_ARRAY) {
1081: log_error_write(srv, __FILE__, __LINE__, "ssSBS",
1082: "unexpected type for key:",
1083: "scgi.server",
1084: "[", da_host->key, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1085:
1086: goto error;
1087: }
1088:
1089: df = scgi_host_init();
1090:
1091: df->check_local = 1;
1092: df->min_procs = 4;
1093: df->max_procs = 4;
1094: df->max_load_per_proc = 1;
1095: df->idle_timeout = 60;
1096: df->disable_time = 60;
1097: df->fix_root_path_name = 0;
1098: df->listen_backlog = 1024;
1099: df->xsendfile_allow = 0;
1100: df->refcount = 0;
1101:
1102: fcv[0].destination = df->host;
1103: fcv[1].destination = df->docroot;
1104: fcv[2].destination = df->unixsocket;
1105: fcv[3].destination = df->bin_path;
1106:
1107: fcv[4].destination = &(df->check_local);
1108: fcv[5].destination = &(df->port);
1109: fcv[6].destination = &(df->min_procs);
1110: fcv[7].destination = &(df->max_procs);
1111: fcv[8].destination = &(df->max_load_per_proc);
1112: fcv[9].destination = &(df->idle_timeout);
1113: fcv[10].destination = &(df->disable_time);
1114:
1115: fcv[11].destination = df->bin_env;
1116: fcv[12].destination = df->bin_env_copy;
1117: fcv[13].destination = &(df->fix_root_path_name);
1118: fcv[14].destination = &(df->listen_backlog);
1119: fcv[15].destination = &(df->xsendfile_allow);
1120: fcv[16].destination = df->xsendfile_docroot;
1121:
1122:
1123: if (0 != config_insert_values_internal(srv, da_host->value, fcv, T_CONFIG_SCOPE_CONNECTION)) {
1124: goto error;
1125: }
1126:
1127: if ((!buffer_string_is_empty(df->host) || df->port) &&
1128: !buffer_string_is_empty(df->unixsocket)) {
1129: log_error_write(srv, __FILE__, __LINE__, "s",
1130: "either host+port or socket");
1131:
1132: goto error;
1133: }
1134:
1135: if (!buffer_string_is_empty(df->unixsocket)) {
1136: /* unix domain socket */
1137: struct sockaddr_un un;
1138:
1139: if (buffer_string_length(df->unixsocket) + 1 > sizeof(un.sun_path) - 2) {
1140: log_error_write(srv, __FILE__, __LINE__, "s",
1141: "path of the unixdomain socket is too large");
1142: goto error;
1143: }
1144:
1145: if (!buffer_string_is_empty(df->bin_path)) {
1146: scgi_extension_host *duplicate = unixsocket_is_dup(p, i+1, df->unixsocket);
1147: if (NULL != duplicate) {
1148: if (!buffer_is_equal(df->bin_path, duplicate->bin_path)) {
1149: log_error_write(srv, __FILE__, __LINE__, "sb",
1150: "duplicate unixsocket path:",
1151: df->unixsocket);
1152: goto error;
1153: }
1154: scgi_host_free(df);
1155: df = duplicate;
1156: ++df->refcount;
1157: }
1158: }
1159:
1160: df->family = AF_UNIX;
1161: } else {
1162: /* tcp/ip */
1163:
1164: if (buffer_string_is_empty(df->host) &&
1165: buffer_string_is_empty(df->bin_path)) {
1166: log_error_write(srv, __FILE__, __LINE__, "sbbbs",
1167: "missing key (string):",
1168: da->key,
1169: da_ext->key,
1170: da_host->key,
1171: "host");
1172:
1173: goto error;
1174: } else if (df->port == 0) {
1175: log_error_write(srv, __FILE__, __LINE__, "sbbbs",
1176: "missing key (short):",
1177: da->key,
1178: da_ext->key,
1179: da_host->key,
1180: "port");
1181: goto error;
1182: }
1183:
1184: df->family = (!buffer_string_is_empty(df->host) && NULL != strchr(df->host->ptr, ':')) ? AF_INET6 : AF_INET;
1185: }
1186:
1187: if (df->refcount) {
1188: /* already init'd; skip spawning */
1189: } else if (!buffer_string_is_empty(df->bin_path)) {
1190: /* a local socket + self spawning */
1191: size_t pno;
1192:
1193: /* HACK: just to make sure the adaptive spawing is disabled */
1194: df->min_procs = df->max_procs;
1195:
1196: if (df->min_procs > df->max_procs) df->max_procs = df->min_procs;
1197: if (df->max_load_per_proc < 1) df->max_load_per_proc = 0;
1198:
1199: if (s->debug) {
1200: log_error_write(srv, __FILE__, __LINE__, "ssbsdsbsdsd",
1201: "--- scgi spawning local",
1202: "\n\tproc:", df->bin_path,
1203: "\n\tport:", df->port,
1204: "\n\tsocket", df->unixsocket,
1205: "\n\tmin-procs:", df->min_procs,
1206: "\n\tmax-procs:", df->max_procs);
1207: }
1208:
1209: for (pno = 0; pno < df->min_procs; pno++) {
1210: scgi_proc *proc;
1211:
1212: proc = scgi_process_init();
1213: proc->id = df->num_procs++;
1214: df->max_id++;
1215:
1216: if (buffer_string_is_empty(df->unixsocket)) {
1217: proc->port = df->port + pno;
1218: } else {
1219: buffer_copy_buffer(proc->socket, df->unixsocket);
1220: buffer_append_string_len(proc->socket, CONST_STR_LEN("-"));
1221: buffer_append_int(proc->socket, pno);
1222: }
1223:
1224: if (s->debug) {
1225: log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
1226: "--- scgi spawning",
1227: "\n\tport:", df->port,
1228: "\n\tsocket", df->unixsocket,
1229: "\n\tcurrent:", pno, "/", df->min_procs);
1230: }
1231:
1232: if (!srv->srvconf.preflight_check
1233: && scgi_spawn_connection(srv, p, df, proc)) {
1234: log_error_write(srv, __FILE__, __LINE__, "s",
1235: "[ERROR]: spawning fcgi failed.");
1236: scgi_process_free(proc);
1237: goto error;
1238: }
1239:
1240: proc->next = df->first;
1241: if (df->first) df->first->prev = proc;
1242:
1243: df->first = proc;
1244: }
1245: } else {
1246: scgi_proc *fp;
1247:
1248: fp = scgi_process_init();
1249: fp->id = df->num_procs++;
1250: df->max_id++;
1251: df->active_procs++;
1252: fp->state = PROC_STATE_RUNNING;
1253:
1254: if (buffer_string_is_empty(df->unixsocket)) {
1255: fp->port = df->port;
1256: } else {
1257: buffer_copy_buffer(fp->socket, df->unixsocket);
1258: }
1259:
1260: df->first = fp;
1261:
1262: df->min_procs = 1;
1263: df->max_procs = 1;
1264: }
1265:
1266: if (df->xsendfile_docroot->used) {
1267: size_t k;
1268: for (k = 0; k < df->xsendfile_docroot->used; ++k) {
1269: data_string *ds = (data_string *)df->xsendfile_docroot->data[k];
1270: if (ds->type != TYPE_STRING) {
1271: log_error_write(srv, __FILE__, __LINE__, "s",
1272: "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
1273: goto error;
1274: }
1275: if (ds->value->ptr[0] != '/') {
1276: log_error_write(srv, __FILE__, __LINE__, "SBs",
1277: "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds->value, "\"");
1278: goto error;
1279: }
1280: buffer_path_simplify(ds->value, ds->value);
1281: buffer_append_slash(ds->value);
1282: }
1283: }
1284:
1285: /* if extension already exists, take it */
1286: scgi_extension_insert(s->exts, da_ext->key, df);
1287: df = NULL;
1288: }
1289: }
1290: }
1291: }
1292:
1293: return HANDLER_GO_ON;
1294:
1295: error:
1296: if (NULL != df) scgi_host_free(df);
1297: return HANDLER_ERROR;
1298: }
1299:
1300: static int scgi_set_state(server *srv, handler_ctx *hctx, scgi_connection_state_t state) {
1301: hctx->state = state;
1302: hctx->state_timestamp = srv->cur_ts;
1303:
1304: return 0;
1305: }
1306:
1307:
1308: static void scgi_connection_close(server *srv, handler_ctx *hctx) {
1309: plugin_data *p;
1310: connection *con;
1311:
1312: p = hctx->plugin_data;
1313: con = hctx->remote_conn;
1314:
1315: if (hctx->fd != -1) {
1316: fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
1317: fdevent_unregister(srv->ev, hctx->fd);
1318: close(hctx->fd);
1319: srv->cur_fds--;
1320: }
1321:
1322: if (hctx->host && hctx->proc) {
1323: hctx->host->load--;
1324:
1325: if (hctx->got_proc) {
1326: /* after the connect the process gets a load */
1327: hctx->proc->load--;
1328:
1329: if (p->conf.debug) {
1330: log_error_write(srv, __FILE__, __LINE__, "sddb",
1331: "release proc:",
1332: hctx->fd,
1333: hctx->proc->pid, hctx->proc->socket);
1334: }
1335: }
1336:
1337: scgi_proclist_sort_down(srv, hctx->host, hctx->proc);
1338: }
1339:
1340:
1341: handler_ctx_free(hctx);
1342: con->plugin_ctx[p->id] = NULL;
1343:
1344: /* finish response (if not already con->file_started, con->file_finished) */
1345: if (con->mode == p->id) {
1346: http_response_backend_done(srv, con);
1347: }
1348: }
1349:
1350: static int scgi_reconnect(server *srv, handler_ctx *hctx) {
1351: plugin_data *p = hctx->plugin_data;
1352:
1353: /* child died
1354: *
1355: * 1.
1356: *
1357: * connect was ok, connection was accepted
1358: * but the php accept loop checks after the accept if it should die or not.
1359: *
1360: * if yes we can only detect it at a write()
1361: *
1362: * next step is resetting this attemp and setup a connection again
1363: *
1364: * if we have more then 5 reconnects for the same request, die
1365: *
1366: * 2.
1367: *
1368: * we have a connection but the child died by some other reason
1369: *
1370: */
1371:
1372: fdevent_event_del(srv->ev, &(hctx->fde_ndx), hctx->fd);
1373: fdevent_unregister(srv->ev, hctx->fd);
1374: close(hctx->fd);
1375: srv->cur_fds--;
1376:
1377: scgi_set_state(srv, hctx, FCGI_STATE_INIT);
1378:
1379: hctx->reconnects++;
1380:
1381: if (p->conf.debug) {
1382: log_error_write(srv, __FILE__, __LINE__, "sddb",
1383: "release proc:",
1384: hctx->fd,
1385: hctx->proc->pid, hctx->proc->socket);
1386: }
1387:
1388: hctx->proc->load--;
1389: scgi_proclist_sort_down(srv, hctx->host, hctx->proc);
1390:
1391: return 0;
1392: }
1393:
1394:
1395: static handler_t scgi_connection_reset(server *srv, connection *con, void *p_d) {
1396: plugin_data *p = p_d;
1397: handler_ctx *hctx = con->plugin_ctx[p->id];
1398: if (hctx) scgi_connection_close(srv, hctx);
1399:
1400: return HANDLER_GO_ON;
1401: }
1402:
1403:
1404: static int scgi_env_add(buffer *env, const char *key, size_t key_len, const char *val, size_t val_len) {
1405: size_t len;
1406:
1407: if (!key || !val) return -1;
1408:
1409: len = key_len + val_len + 2;
1410:
1411: buffer_string_prepare_append(env, len);
1412:
1413: buffer_append_string_len(env, key, key_len);
1414: buffer_append_string_len(env, "", 1);
1415: buffer_append_string_len(env, val, val_len);
1416: buffer_append_string_len(env, "", 1);
1417:
1418: return 0;
1419: }
1420:
1421:
1422: /**
1423: *
1424: * returns
1425: * -1 error
1426: * 0 connected
1427: * 1 not connected yet
1428: */
1429:
1430: static int scgi_establish_connection(server *srv, handler_ctx *hctx) {
1431: struct sockaddr *scgi_addr;
1432: struct sockaddr_in scgi_addr_in;
1433: #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1434: struct sockaddr_in6 scgi_addr_in6;
1435: #endif
1436: #ifdef HAVE_SYS_UN_H
1437: struct sockaddr_un scgi_addr_un;
1438: #endif
1439: socklen_t servlen;
1440:
1441: scgi_extension_host *host = hctx->host;
1442: scgi_proc *proc = hctx->proc;
1443: int scgi_fd = hctx->fd;
1444:
1445: if (!buffer_string_is_empty(proc->socket)) {
1446: #ifdef HAVE_SYS_UN_H
1447: /* use the unix domain socket */
1448: memset(&scgi_addr_un, 0, sizeof(scgi_addr_un));
1449: scgi_addr_un.sun_family = AF_UNIX;
1450: if (buffer_string_length(proc->socket) + 1 > sizeof(scgi_addr_un.sun_path)) {
1451: log_error_write(srv, __FILE__, __LINE__, "sB",
1452: "ERROR: Unix Domain socket filename too long:",
1453: proc->socket);
1454: return -1;
1455: }
1456: memcpy(scgi_addr_un.sun_path, proc->socket->ptr, buffer_string_length(proc->socket) + 1);
1457:
1458: #ifdef SUN_LEN
1459: servlen = SUN_LEN(&scgi_addr_un);
1460: #else
1461: /* stevens says: */
1462: servlen = buffer_string_length(proc->socket) + 1 + sizeof(scgi_addr_un.sun_family);
1463: #endif
1464: scgi_addr = (struct sockaddr *) &scgi_addr_un;
1465: #else
1466: return -1;
1467: #endif
1468: #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1469: } else if (host->family == AF_INET6 && !buffer_string_is_empty(host->host)) {
1470: memset(&scgi_addr_in6, 0, sizeof(scgi_addr_in6));
1471: scgi_addr_in6.sin6_family = AF_INET6;
1472: inet_pton(AF_INET6, host->host->ptr, (char *) &scgi_addr_in6.sin6_addr);
1473: scgi_addr_in6.sin6_port = htons(proc->port);
1474: servlen = sizeof(scgi_addr_in6);
1475: scgi_addr = (struct sockaddr *) &scgi_addr_in6;
1476: #endif
1477: } else {
1478: memset(&scgi_addr_in, 0, sizeof(scgi_addr_in));
1479: scgi_addr_in.sin_family = AF_INET;
1480: if (0 == inet_aton(host->host->ptr, &(scgi_addr_in.sin_addr))) {
1481: log_error_write(srv, __FILE__, __LINE__, "sbs",
1482: "converting IP-adress failed for", host->host,
1483: "\nBe sure to specify an IP address here");
1484:
1485: return -1;
1486: }
1487: scgi_addr_in.sin_port = htons(proc->port);
1488: servlen = sizeof(scgi_addr_in);
1489:
1490: scgi_addr = (struct sockaddr *) &scgi_addr_in;
1491: }
1492:
1493: if (-1 == connect(scgi_fd, scgi_addr, servlen)) {
1494: if (errno == EINPROGRESS ||
1495: errno == EALREADY ||
1496: errno == EINTR) {
1497: if (hctx->conf.debug) {
1498: log_error_write(srv, __FILE__, __LINE__, "sd",
1499: "connect delayed, will continue later:", scgi_fd);
1500: }
1501:
1502: return 1;
1503: } else {
1504: log_error_write(srv, __FILE__, __LINE__, "sdsddb",
1505: "connect failed:", scgi_fd,
1506: strerror(errno), errno,
1507: proc->port, proc->socket);
1508:
1509: if (errno == EAGAIN) {
1510: /* this is Linux only */
1511:
1512: log_error_write(srv, __FILE__, __LINE__, "s",
1513: "If this happend on Linux: You have been run out of local ports. "
1514: "Check the manual, section Performance how to handle this.");
1515: }
1516:
1517: return -1;
1518: }
1519: }
1520: if (hctx->conf.debug > 1) {
1521: log_error_write(srv, __FILE__, __LINE__, "sd",
1522: "connect succeeded: ", scgi_fd);
1523: }
1524:
1525:
1526:
1527: return 0;
1528: }
1529:
1530: static int scgi_env_add_request_headers(server *srv, connection *con, plugin_data *p) {
1531: size_t i;
1532:
1533: for (i = 0; i < con->request.headers->used; i++) {
1534: data_string *ds;
1535:
1536: ds = (data_string *)con->request.headers->data[i];
1537:
1538: if (!buffer_is_empty(ds->value) && !buffer_is_empty(ds->key)) {
1539: /* Do not emit HTTP_PROXY in environment.
1540: * Some executables use HTTP_PROXY to configure
1541: * outgoing proxy. See also https://httpoxy.org/ */
1542: if (buffer_is_equal_caseless_string(ds->key, CONST_STR_LEN("Proxy"))) {
1543: continue;
1544: }
1545:
1546: buffer_copy_string_encoded_cgi_varnames(srv->tmp_buf, CONST_BUF_LEN(ds->key), 1);
1547:
1548: scgi_env_add(p->scgi_env, CONST_BUF_LEN(srv->tmp_buf), CONST_BUF_LEN(ds->value));
1549: }
1550: }
1551:
1552: for (i = 0; i < con->environment->used; i++) {
1553: data_string *ds;
1554:
1555: ds = (data_string *)con->environment->data[i];
1556:
1557: if (!buffer_is_empty(ds->value) && !buffer_is_empty(ds->key)) {
1558: buffer_copy_string_encoded_cgi_varnames(srv->tmp_buf, CONST_BUF_LEN(ds->key), 0);
1559:
1560: scgi_env_add(p->scgi_env, CONST_BUF_LEN(srv->tmp_buf), CONST_BUF_LEN(ds->value));
1561: }
1562: }
1563:
1564: return 0;
1565: }
1566:
1567:
1568: static int scgi_create_env(server *srv, handler_ctx *hctx) {
1569: char buf[LI_ITOSTRING_LENGTH];
1570: const char *s;
1571: #ifdef HAVE_IPV6
1572: char b2[INET6_ADDRSTRLEN + 1];
1573: #endif
1574: buffer *b;
1575:
1576: plugin_data *p = hctx->plugin_data;
1577: scgi_extension_host *host= hctx->host;
1578:
1579: connection *con = hctx->remote_conn;
1580: server_socket *srv_sock = con->srv_socket;
1581:
1582: sock_addr our_addr;
1583: socklen_t our_addr_len;
1584:
1585: buffer_string_prepare_copy(p->scgi_env, 1023);
1586:
1587: /* CGI-SPEC 6.1.2, FastCGI spec 6.3 and SCGI spec */
1588:
1589: li_itostrn(buf, sizeof(buf), con->request.content_length);
1590: scgi_env_add(p->scgi_env, CONST_STR_LEN("CONTENT_LENGTH"), buf, strlen(buf));
1591: scgi_env_add(p->scgi_env, CONST_STR_LEN("SCGI"), CONST_STR_LEN("1"));
1592:
1593: scgi_env_add(p->scgi_env, CONST_STR_LEN("SERVER_SOFTWARE"), CONST_BUF_LEN(con->conf.server_tag));
1594:
1595: if (!buffer_is_empty(con->server_name)) {
1596: size_t len = buffer_string_length(con->server_name);
1597:
1598: if (con->server_name->ptr[0] == '[') {
1599: const char *colon = strstr(con->server_name->ptr, "]:");
1600: if (colon) len = (colon + 1) - con->server_name->ptr;
1601: } else {
1602: const char *colon = strchr(con->server_name->ptr, ':');
1603: if (colon) len = colon - con->server_name->ptr;
1604: }
1605:
1606: scgi_env_add(p->scgi_env, CONST_STR_LEN("SERVER_NAME"), con->server_name->ptr, len);
1607: } else {
1608: #ifdef HAVE_IPV6
1609: s = inet_ntop(srv_sock->addr.plain.sa_family,
1610: srv_sock->addr.plain.sa_family == AF_INET6 ?
1611: (const void *) &(srv_sock->addr.ipv6.sin6_addr) :
1612: (const void *) &(srv_sock->addr.ipv4.sin_addr),
1613: b2, sizeof(b2)-1);
1614: #else
1615: s = inet_ntoa(srv_sock->addr.ipv4.sin_addr);
1616: #endif
1617: force_assert(s);
1618: scgi_env_add(p->scgi_env, CONST_STR_LEN("SERVER_NAME"), s, strlen(s));
1619: }
1620:
1621: scgi_env_add(p->scgi_env, CONST_STR_LEN("GATEWAY_INTERFACE"), CONST_STR_LEN("CGI/1.1"));
1622:
1623: li_utostrn(buf, sizeof(buf),
1624: #ifdef HAVE_IPV6
1625: ntohs(srv_sock->addr.plain.sa_family ? srv_sock->addr.ipv6.sin6_port : srv_sock->addr.ipv4.sin_port)
1626: #else
1627: ntohs(srv_sock->addr.ipv4.sin_port)
1628: #endif
1629: );
1630:
1631: scgi_env_add(p->scgi_env, CONST_STR_LEN("SERVER_PORT"), buf, strlen(buf));
1632:
1633: /* get the server-side of the connection to the client */
1634: our_addr_len = sizeof(our_addr);
1635:
1636: if (-1 == getsockname(con->fd, (struct sockaddr *)&our_addr, &our_addr_len)
1637: || our_addr_len > (socklen_t)sizeof(our_addr)) {
1638: s = inet_ntop_cache_get_ip(srv, &(srv_sock->addr));
1639: } else {
1640: s = inet_ntop_cache_get_ip(srv, &(our_addr));
1641: }
1642: scgi_env_add(p->scgi_env, CONST_STR_LEN("SERVER_ADDR"), s, strlen(s));
1643:
1644: li_utostrn(buf, sizeof(buf),
1645: #ifdef HAVE_IPV6
1646: ntohs(con->dst_addr.plain.sa_family ? con->dst_addr.ipv6.sin6_port : con->dst_addr.ipv4.sin_port)
1647: #else
1648: ntohs(con->dst_addr.ipv4.sin_port)
1649: #endif
1650: );
1651:
1652: scgi_env_add(p->scgi_env, CONST_STR_LEN("REMOTE_PORT"), buf, strlen(buf));
1653:
1654: s = inet_ntop_cache_get_ip(srv, &(con->dst_addr));
1655: force_assert(s);
1656: scgi_env_add(p->scgi_env, CONST_STR_LEN("REMOTE_ADDR"), s, strlen(s));
1657:
1658: /*
1659: * SCRIPT_NAME, PATH_INFO and PATH_TRANSLATED according to
1660: * http://cgi-spec.golux.com/draft-coar-cgi-v11-03-clean.html
1661: * (6.1.14, 6.1.6, 6.1.7)
1662: */
1663:
1664: scgi_env_add(p->scgi_env, CONST_STR_LEN("SCRIPT_NAME"), CONST_BUF_LEN(con->uri.path));
1665:
1666: if (!buffer_string_is_empty(con->request.pathinfo)) {
1667: scgi_env_add(p->scgi_env, CONST_STR_LEN("PATH_INFO"), CONST_BUF_LEN(con->request.pathinfo));
1668:
1669: /* PATH_TRANSLATED is only defined if PATH_INFO is set */
1670:
1671: if (!buffer_string_is_empty(host->docroot)) {
1672: buffer_copy_buffer(p->path, host->docroot);
1673: } else {
1674: buffer_copy_buffer(p->path, con->physical.basedir);
1675: }
1676: buffer_append_string_buffer(p->path, con->request.pathinfo);
1677: scgi_env_add(p->scgi_env, CONST_STR_LEN("PATH_TRANSLATED"), CONST_BUF_LEN(p->path));
1678: } else {
1679: scgi_env_add(p->scgi_env, CONST_STR_LEN("PATH_INFO"), CONST_STR_LEN(""));
1680: }
1681:
1682: /*
1683: * SCRIPT_FILENAME and DOCUMENT_ROOT for php. The PHP manual
1684: * http://www.php.net/manual/en/reserved.variables.php
1685: * treatment of PATH_TRANSLATED is different from the one of CGI specs.
1686: * TODO: this code should be checked against cgi.fix_pathinfo php
1687: * parameter.
1688: */
1689:
1690: if (!buffer_string_is_empty(host->docroot)) {
1691: /*
1692: * rewrite SCRIPT_FILENAME
1693: *
1694: */
1695:
1696: buffer_copy_buffer(p->path, host->docroot);
1697: buffer_append_string_buffer(p->path, con->uri.path);
1698:
1699: scgi_env_add(p->scgi_env, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(p->path));
1700: scgi_env_add(p->scgi_env, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(host->docroot));
1701: } else {
1702: buffer_copy_buffer(p->path, con->physical.path);
1703:
1704: scgi_env_add(p->scgi_env, CONST_STR_LEN("SCRIPT_FILENAME"), CONST_BUF_LEN(p->path));
1705: scgi_env_add(p->scgi_env, CONST_STR_LEN("DOCUMENT_ROOT"), CONST_BUF_LEN(con->physical.basedir));
1706: }
1707: scgi_env_add(p->scgi_env, CONST_STR_LEN("REQUEST_URI"), CONST_BUF_LEN(con->request.orig_uri));
1708: if (!buffer_is_equal(con->request.uri, con->request.orig_uri)) {
1709: scgi_env_add(p->scgi_env, CONST_STR_LEN("REDIRECT_URI"), CONST_BUF_LEN(con->request.uri));
1710: }
1711: if (!buffer_string_is_empty(con->uri.query)) {
1712: scgi_env_add(p->scgi_env, CONST_STR_LEN("QUERY_STRING"), CONST_BUF_LEN(con->uri.query));
1713: } else {
1714: scgi_env_add(p->scgi_env, CONST_STR_LEN("QUERY_STRING"), CONST_STR_LEN(""));
1715: }
1716:
1717: s = get_http_method_name(con->request.http_method);
1718: force_assert(s);
1719: scgi_env_add(p->scgi_env, CONST_STR_LEN("REQUEST_METHOD"), s, strlen(s));
1720: /* set REDIRECT_STATUS for php compiled with --force-redirect
1721: * (if REDIRECT_STATUS has not already been set by error handler) */
1722: if (0 == con->error_handler_saved_status) {
1723: scgi_env_add(p->scgi_env, CONST_STR_LEN("REDIRECT_STATUS"), CONST_STR_LEN("200"));
1724: }
1725: s = get_http_version_name(con->request.http_version);
1726: force_assert(s);
1727: scgi_env_add(p->scgi_env, CONST_STR_LEN("SERVER_PROTOCOL"), s, strlen(s));
1728:
1729: #ifdef USE_OPENSSL
1730: if (buffer_is_equal_caseless_string(con->uri.scheme, CONST_STR_LEN("https"))) {
1731: scgi_env_add(p->scgi_env, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on"));
1732: }
1733: #endif
1734:
1735: scgi_env_add_request_headers(srv, con, p);
1736:
1737: b = buffer_init();
1738:
1739: buffer_append_int(b, buffer_string_length(p->scgi_env));
1740: buffer_append_string_len(b, CONST_STR_LEN(":"));
1741: buffer_append_string_buffer(b, p->scgi_env);
1742: buffer_append_string_len(b, CONST_STR_LEN(","));
1743:
1744: hctx->wb_reqlen = buffer_string_length(b);
1745: chunkqueue_append_buffer(hctx->wb, b);
1746: buffer_free(b);
1747:
1748: if (con->request.content_length) {
1749: chunkqueue_append_chunkqueue(hctx->wb, con->request_content_queue);
1750: hctx->wb_reqlen += con->request.content_length;/* (eventual) total request size */
1751: }
1752:
1753: return 0;
1754: }
1755:
1756: static int scgi_response_parse(server *srv, connection *con, plugin_data *p, buffer *in, int eol) {
1757: char *ns;
1758: const char *s;
1759: int line = 0;
1760:
1761: UNUSED(srv);
1762:
1763: buffer_copy_buffer(p->parse_response, in);
1764:
1765: for (s = p->parse_response->ptr;
1766: NULL != (ns = (eol == EOL_RN ? strstr(s, "\r\n") : strchr(s, '\n')));
1767: s = ns + (eol == EOL_RN ? 2 : 1), line++) {
1768: const char *key, *value;
1769: int key_len;
1770: data_string *ds;
1771:
1772: ns[0] = '\0';
1773:
1774: if (line == 0 &&
1775: 0 == strncmp(s, "HTTP/1.", 7)) {
1776: /* non-parsed header ... we parse them anyway */
1777:
1778: if ((s[7] == '1' ||
1779: s[7] == '0') &&
1780: s[8] == ' ') {
1781: int status;
1782: /* after the space should be a status code for us */
1783:
1784: status = strtol(s+9, NULL, 10);
1785:
1786: if (status >= 100 && status < 1000) {
1787: /* we expected 3 digits got them */
1788: con->parsed_response |= HTTP_STATUS;
1789: con->http_status = status;
1790: }
1791: }
1792: } else {
1793:
1794: key = s;
1795: if (NULL == (value = strchr(s, ':'))) {
1796: /* we expect: "<key>: <value>\r\n" */
1797: continue;
1798: }
1799:
1800: key_len = value - key;
1801: value += 1;
1802:
1803: /* skip LWS */
1804: while (*value == ' ' || *value == '\t') value++;
1805:
1806: if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
1807: ds = data_response_init();
1808: }
1809: buffer_copy_string_len(ds->key, key, key_len);
1810: buffer_copy_string(ds->value, value);
1811:
1812: array_insert_unique(con->response.headers, (data_unset *)ds);
1813:
1814: switch(key_len) {
1815: case 4:
1816: if (0 == strncasecmp(key, "Date", key_len)) {
1817: con->parsed_response |= HTTP_DATE;
1818: }
1819: break;
1820: case 6:
1821: if (0 == strncasecmp(key, "Status", key_len)) {
1822: int status = strtol(value, NULL, 10);
1823: if (status >= 100 && status < 1000) {
1824: con->http_status = status;
1825: con->parsed_response |= HTTP_STATUS;
1826: } else {
1827: con->http_status = 502;
1828: }
1829: }
1830: break;
1831: case 8:
1832: if (0 == strncasecmp(key, "Location", key_len)) {
1833: con->parsed_response |= HTTP_LOCATION;
1834: }
1835: break;
1836: case 10:
1837: if (0 == strncasecmp(key, "Connection", key_len)) {
1838: con->response.keep_alive = (0 == strcasecmp(value, "Keep-Alive")) ? 1 : 0;
1839: con->parsed_response |= HTTP_CONNECTION;
1840: }
1841: break;
1842: case 14:
1843: if (0 == strncasecmp(key, "Content-Length", key_len)) {
1844: con->response.content_length = strtoul(value, NULL, 10);
1845: con->parsed_response |= HTTP_CONTENT_LENGTH;
1846: }
1847: break;
1848: default:
1849: break;
1850: }
1851: }
1852: }
1853:
1854: /* CGI/1.1 rev 03 - 7.2.1.2 */
1855: if ((con->parsed_response & HTTP_LOCATION) &&
1856: !(con->parsed_response & HTTP_STATUS)) {
1857: con->http_status = 302;
1858: }
1859:
1860: return 0;
1861: }
1862:
1863:
1864: static int scgi_demux_response(server *srv, handler_ctx *hctx) {
1865: plugin_data *p = hctx->plugin_data;
1866: connection *con = hctx->remote_conn;
1867:
1868: while(1) {
1869: int n;
1870:
1871: buffer_string_prepare_copy(hctx->response, 1023);
1872: if (-1 == (n = read(hctx->fd, hctx->response->ptr, hctx->response->size - 1))) {
1873: if (errno == EAGAIN || errno == EINTR) {
1874: /* would block, wait for signal */
1875: fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
1876: return 0;
1877: }
1878: /* error */
1879: log_error_write(srv, __FILE__, __LINE__, "sdd", strerror(errno), con->fd, hctx->fd);
1880: return -1;
1881: }
1882:
1883: if (n == 0) {
1884: /* read finished */
1885: return 1;
1886: }
1887:
1888: buffer_commit(hctx->response, n);
1889:
1890: /* split header from body */
1891:
1892: if (con->file_started == 0) {
1893: char *c;
1894: int in_header = 0;
1895: int header_end = 0;
1896: int cp, eol = EOL_UNSET;
1897: size_t used = 0;
1898: size_t hlen = 0;
1899:
1900: buffer_append_string_buffer(hctx->response_header, hctx->response);
1901:
1902: /* nph (non-parsed headers) */
1903: if (0 == strncmp(hctx->response_header->ptr, "HTTP/1.", 7)) in_header = 1;
1904:
1905: /* search for the \r\n\r\n or \n\n in the string */
1906: for (c = hctx->response_header->ptr, cp = 0, used = buffer_string_length(hctx->response_header); used; c++, cp++, used--) {
1907: if (*c == ':') in_header = 1;
1908: else if (*c == '\n') {
1909: if (in_header == 0) {
1910: /* got a response without a response header */
1911:
1912: c = NULL;
1913: header_end = 1;
1914: break;
1915: }
1916:
1917: if (eol == EOL_UNSET) eol = EOL_N;
1918:
1919: if (*(c+1) == '\n') {
1920: header_end = 1;
1921: hlen = cp + 2;
1922: break;
1923: }
1924:
1925: } else if (used > 1 && *c == '\r' && *(c+1) == '\n') {
1926: if (in_header == 0) {
1927: /* got a response without a response header */
1928:
1929: c = NULL;
1930: header_end = 1;
1931: break;
1932: }
1933:
1934: if (eol == EOL_UNSET) eol = EOL_RN;
1935:
1936: if (used > 3 &&
1937: *(c+2) == '\r' &&
1938: *(c+3) == '\n') {
1939: header_end = 1;
1940: hlen = cp + 4;
1941: break;
1942: }
1943:
1944: /* skip the \n */
1945: c++;
1946: cp++;
1947: used--;
1948: }
1949: }
1950:
1951: if (header_end) {
1952: if (c == NULL) {
1953: /* no header, but a body */
1954: if (0 != http_chunk_append_buffer(srv, con, hctx->response_header)) {
1955: /* error writing to tempfile;
1956: * truncate response or send 500 if nothing sent yet */
1957: return 1;
1958: }
1959: } else {
1960: size_t blen = buffer_string_length(hctx->response_header) - hlen;
1961:
1962: /* a small hack: terminate after at the second \r */
1963: buffer_string_set_length(hctx->response_header, hlen - 1);
1964:
1965: /* parse the response header */
1966: scgi_response_parse(srv, con, p, hctx->response_header, eol);
1967:
1968: if (hctx->host->xsendfile_allow) {
1969: data_string *ds;
1970: if (NULL != (ds = (data_string *) array_get_element(con->response.headers, "X-Sendfile"))) {
1971: http_response_xsendfile(srv, con, ds->value, hctx->host->xsendfile_docroot);
1972: return 1;
1973: }
1974: }
1975:
1976: if (blen > 0) {
1977: if (0 != http_chunk_append_mem(srv, con, hctx->response_header->ptr + hlen, blen)) {
1978: /* error writing to tempfile;
1979: * truncate response or send 500 if nothing sent yet */
1980: return 1;
1981: }
1982: }
1983: }
1984:
1985: con->file_started = 1;
1986: } else {
1987: /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
1988: if (buffer_string_length(hctx->response_header) > MAX_HTTP_REQUEST_HEADER) {
1989: log_error_write(srv, __FILE__, __LINE__, "sb", "response headers too large for", con->uri.path);
1990: con->http_status = 502; /* Bad Gateway */
1991: con->mode = DIRECT;
1992: return 1;
1993: }
1994: }
1995: } else {
1996: if (0 != http_chunk_append_buffer(srv, con, hctx->response)) {
1997: /* error writing to tempfile;
1998: * truncate response or send 500 if nothing sent yet */
1999: return 1;
2000: }
2001: if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
2002: && chunkqueue_length(con->write_queue) > 65536 - 4096) {
2003: if (!con->is_writable) {
2004: /*(defer removal of FDEVENT_IN interest since
2005: * connection_state_machine() might be able to send data
2006: * immediately, unless !con->is_writable, where
2007: * connection_state_machine() might not loop back to call
2008: * mod_scgi_handle_subrequest())*/
2009: fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
2010: }
2011: break;
2012: }
2013: }
2014:
2015: #if 0
2016: log_error_write(srv, __FILE__, __LINE__, "ddss", con->fd, hctx->fd, connection_get_state(con->state), b->ptr);
2017: #endif
2018: }
2019:
2020: return 0;
2021: }
2022:
2023:
2024: static int scgi_proclist_sort_up(server *srv, scgi_extension_host *host, scgi_proc *proc) {
2025: scgi_proc *p;
2026:
2027: UNUSED(srv);
2028:
2029: /* we have been the smallest of the current list
2030: * and we want to insert the node sorted as soon
2031: * possible
2032: *
2033: * 1 0 0 0 1 1 1
2034: * | ^
2035: * | |
2036: * +------+
2037: *
2038: */
2039:
2040: /* nothing to sort, only one element */
2041: if (host->first == proc && proc->next == NULL) return 0;
2042:
2043: for (p = proc; p->next && p->next->load < proc->load; p = p->next);
2044:
2045: /* no need to move something
2046: *
2047: * 1 2 2 2 3 3 3
2048: * ^
2049: * |
2050: * +
2051: *
2052: */
2053: if (p == proc) return 0;
2054:
2055: if (host->first == proc) {
2056: /* we have been the first elememt */
2057:
2058: host->first = proc->next;
2059: host->first->prev = NULL;
2060: }
2061:
2062: /* disconnect proc */
2063:
2064: if (proc->prev) proc->prev->next = proc->next;
2065: if (proc->next) proc->next->prev = proc->prev;
2066:
2067: /* proc should be right of p */
2068:
2069: proc->next = p->next;
2070: proc->prev = p;
2071: if (p->next) p->next->prev = proc;
2072: p->next = proc;
2073: #if 0
2074: for(p = host->first; p; p = p->next) {
2075: log_error_write(srv, __FILE__, __LINE__, "dd",
2076: p->pid, p->load);
2077: }
2078: #else
2079: UNUSED(srv);
2080: #endif
2081:
2082: return 0;
2083: }
2084:
2085: int scgi_proclist_sort_down(server *srv, scgi_extension_host *host, scgi_proc *proc) {
2086: scgi_proc *p;
2087:
2088: UNUSED(srv);
2089:
2090: /* we have been the smallest of the current list
2091: * and we want to insert the node sorted as soon
2092: * possible
2093: *
2094: * 0 0 0 0 1 0 1
2095: * ^ |
2096: * | |
2097: * +----------+
2098: *
2099: *
2100: * the basic is idea is:
2101: * - the last active scgi process should be still
2102: * in ram and is not swapped out yet
2103: * - processes that are not reused will be killed
2104: * after some time by the trigger-handler
2105: * - remember it as:
2106: * everything > 0 is hot
2107: * all unused procs are colder the more right they are
2108: * ice-cold processes are propably unused since more
2109: * than 'unused-timeout', are swaped out and won't be
2110: * reused in the next seconds anyway.
2111: *
2112: */
2113:
2114: /* nothing to sort, only one element */
2115: if (host->first == proc && proc->next == NULL) return 0;
2116:
2117: for (p = host->first; p != proc && p->load < proc->load; p = p->next);
2118:
2119:
2120: /* no need to move something
2121: *
2122: * 1 2 2 2 3 3 3
2123: * ^
2124: * |
2125: * +
2126: *
2127: */
2128: if (p == proc) return 0;
2129:
2130: /* we have to move left. If we are already the first element
2131: * we are done */
2132: if (host->first == proc) return 0;
2133:
2134: /* release proc */
2135: if (proc->prev) proc->prev->next = proc->next;
2136: if (proc->next) proc->next->prev = proc->prev;
2137:
2138: /* proc should be left of p */
2139: proc->next = p;
2140: proc->prev = p->prev;
2141: if (p->prev) p->prev->next = proc;
2142: p->prev = proc;
2143:
2144: if (proc->prev == NULL) host->first = proc;
2145: #if 0
2146: for(p = host->first; p; p = p->next) {
2147: log_error_write(srv, __FILE__, __LINE__, "dd",
2148: p->pid, p->load);
2149: }
2150: #else
2151: UNUSED(srv);
2152: #endif
2153:
2154: return 0;
2155: }
2156:
2157: static int scgi_restart_dead_procs(server *srv, plugin_data *p, scgi_extension_host *host) {
2158: scgi_proc *proc;
2159:
2160: for (proc = host->first; proc; proc = proc->next) {
2161: if (p->conf.debug) {
2162: log_error_write(srv, __FILE__, __LINE__, "sbdbdddd",
2163: "proc:",
2164: host->host, proc->port,
2165: proc->socket,
2166: proc->state,
2167: proc->is_local,
2168: proc->load,
2169: proc->pid);
2170: }
2171:
2172: if (0 == proc->is_local) {
2173: /*
2174: * external servers might get disabled
2175: *
2176: * enable the server again, perhaps it is back again
2177: */
2178:
2179: if ((proc->state == PROC_STATE_DISABLED) &&
2180: (srv->cur_ts - proc->disable_ts > host->disable_time)) {
2181: proc->state = PROC_STATE_RUNNING;
2182: host->active_procs++;
2183:
2184: log_error_write(srv, __FILE__, __LINE__, "sbdb",
2185: "fcgi-server re-enabled:",
2186: host->host, host->port,
2187: host->unixsocket);
2188: }
2189: } else {
2190: /* the child should not terminate at all */
2191: int status;
2192:
2193: if (proc->state == PROC_STATE_DIED_WAIT_FOR_PID) {
2194: switch(waitpid(proc->pid, &status, WNOHANG)) {
2195: case 0:
2196: /* child is still alive */
2197: break;
2198: case -1:
2199: break;
2200: default:
2201: if (WIFEXITED(status)) {
2202: #if 0
2203: log_error_write(srv, __FILE__, __LINE__, "sdsd",
2204: "child exited, pid:", proc->pid,
2205: "status:", WEXITSTATUS(status));
2206: #endif
2207: } else if (WIFSIGNALED(status)) {
2208: log_error_write(srv, __FILE__, __LINE__, "sd",
2209: "child signaled:",
2210: WTERMSIG(status));
2211: } else {
2212: log_error_write(srv, __FILE__, __LINE__, "sd",
2213: "child died somehow:",
2214: status);
2215: }
2216:
2217: proc->state = PROC_STATE_DIED;
2218: break;
2219: }
2220: }
2221:
2222: /*
2223: * local servers might died, but we restart them
2224: *
2225: */
2226: if (proc->state == PROC_STATE_DIED &&
2227: proc->load == 0) {
2228: /* restart the child */
2229:
2230: if (p->conf.debug) {
2231: log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
2232: "--- scgi spawning",
2233: "\n\tport:", host->port,
2234: "\n\tsocket", host->unixsocket,
2235: "\n\tcurrent:", 1, "/", host->min_procs);
2236: }
2237:
2238: if (scgi_spawn_connection(srv, p, host, proc)) {
2239: log_error_write(srv, __FILE__, __LINE__, "s",
2240: "ERROR: spawning fcgi failed.");
2241: return HANDLER_ERROR;
2242: }
2243:
2244: scgi_proclist_sort_down(srv, host, proc);
2245: }
2246: }
2247: }
2248:
2249: return 0;
2250: }
2251:
2252:
2253: static handler_t scgi_write_request(server *srv, handler_ctx *hctx) {
2254: plugin_data *p = hctx->plugin_data;
2255: scgi_extension_host *host= hctx->host;
2256: connection *con = hctx->remote_conn;
2257:
2258: int ret;
2259:
2260: /* sanity check */
2261: if (!host) {
2262: log_error_write(srv, __FILE__, __LINE__, "s", "fatal error: host = NULL");
2263: return HANDLER_ERROR;
2264: }
2265: if (((buffer_string_is_empty(host->host) || !host->port) && buffer_string_is_empty(host->unixsocket))) {
2266: log_error_write(srv, __FILE__, __LINE__, "sxddd",
2267: "write-req: error",
2268: host,
2269: buffer_string_length(host->host),
2270: host->port,
2271: buffer_string_length(host->unixsocket));
2272: return HANDLER_ERROR;
2273: }
2274:
2275:
2276: switch(hctx->state) {
2277: case FCGI_STATE_INIT:
2278: if (-1 == (hctx->fd = socket(host->family, SOCK_STREAM, 0))) {
2279: if (errno == EMFILE ||
2280: errno == EINTR) {
2281: log_error_write(srv, __FILE__, __LINE__, "sd",
2282: "wait for fd at connection:", con->fd);
2283:
2284: return HANDLER_WAIT_FOR_FD;
2285: }
2286:
2287: log_error_write(srv, __FILE__, __LINE__, "ssdd",
2288: "socket failed:", strerror(errno), srv->cur_fds, srv->max_fds);
2289: return HANDLER_ERROR;
2290: }
2291: hctx->fde_ndx = -1;
2292:
2293: srv->cur_fds++;
2294:
2295: fdevent_register(srv->ev, hctx->fd, scgi_handle_fdevent, hctx);
2296:
2297: if (-1 == fdevent_fcntl_set(srv->ev, hctx->fd)) {
2298: log_error_write(srv, __FILE__, __LINE__, "ss",
2299: "fcntl failed: ", strerror(errno));
2300:
2301: return HANDLER_ERROR;
2302: }
2303:
2304: /* fall through */
2305: case FCGI_STATE_CONNECT:
2306: if (hctx->state == FCGI_STATE_INIT) {
2307: for (hctx->proc = hctx->host->first;
2308: hctx->proc && hctx->proc->state != PROC_STATE_RUNNING;
2309: hctx->proc = hctx->proc->next);
2310:
2311: /* all childs are dead */
2312: if (hctx->proc == NULL) {
2313: hctx->fde_ndx = -1;
2314:
2315: return HANDLER_ERROR;
2316: }
2317:
2318: if (hctx->proc->is_local) {
2319: hctx->pid = hctx->proc->pid;
2320: }
2321:
2322: switch (scgi_establish_connection(srv, hctx)) {
2323: case 1:
2324: scgi_set_state(srv, hctx, FCGI_STATE_CONNECT);
2325:
2326: /* connection is in progress, wait for an event and call getsockopt() below */
2327:
2328: fdevent_event_set(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2329:
2330: return HANDLER_WAIT_FOR_EVENT;
2331: case -1:
2332: /* if ECONNREFUSED choose another connection -> FIXME */
2333: hctx->fde_ndx = -1;
2334:
2335: return HANDLER_ERROR;
2336: default:
2337: /* everything is ok, go on */
2338: break;
2339: }
2340:
2341:
2342: } else {
2343: int socket_error;
2344: socklen_t socket_error_len = sizeof(socket_error);
2345:
2346: /* try to finish the connect() */
2347: if (0 != getsockopt(hctx->fd, SOL_SOCKET, SO_ERROR, &socket_error, &socket_error_len)) {
2348: log_error_write(srv, __FILE__, __LINE__, "ss",
2349: "getsockopt failed:", strerror(errno));
2350:
2351: return HANDLER_ERROR;
2352: }
2353: if (socket_error != 0) {
2354: if (!hctx->proc->is_local || p->conf.debug) {
2355: /* local procs get restarted */
2356:
2357: log_error_write(srv, __FILE__, __LINE__, "ss",
2358: "establishing connection failed:", strerror(socket_error),
2359: "port:", hctx->proc->port);
2360: }
2361:
2362: return HANDLER_ERROR;
2363: }
2364: }
2365:
2366: /* ok, we have the connection */
2367:
2368: hctx->proc->load++;
2369: hctx->proc->last_used = srv->cur_ts;
2370: hctx->got_proc = 1;
2371:
2372: if (p->conf.debug) {
2373: log_error_write(srv, __FILE__, __LINE__, "sddbdd",
2374: "got proc:",
2375: hctx->fd,
2376: hctx->proc->pid,
2377: hctx->proc->socket,
2378: hctx->proc->port,
2379: hctx->proc->load);
2380: }
2381:
2382: /* move the proc-list entry down the list */
2383: scgi_proclist_sort_up(srv, hctx->host, hctx->proc);
2384:
2385: scgi_set_state(srv, hctx, FCGI_STATE_PREPARE_WRITE);
2386: /* fall through */
2387: case FCGI_STATE_PREPARE_WRITE:
2388: scgi_create_env(srv, hctx);
2389:
2390: fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
2391: scgi_set_state(srv, hctx, FCGI_STATE_WRITE);
2392:
2393: /* fall through */
2394: case FCGI_STATE_WRITE:
2395: ret = srv->network_backend_write(srv, con, hctx->fd, hctx->wb, MAX_WRITE_LIMIT);
2396:
2397: chunkqueue_remove_finished_chunks(hctx->wb);
2398:
2399: if (ret < 0) {
2400: if (errno == ENOTCONN || ret == -2) {
2401: /* the connection got dropped after accept()
2402: *
2403: * this is most of the time a PHP which dies
2404: * after PHP_FCGI_MAX_REQUESTS
2405: *
2406: */
2407: if (hctx->wb->bytes_out == 0 &&
2408: hctx->reconnects < 5) {
2409: usleep(10000); /* take away the load of the webserver
2410: * to let the php a chance to restart
2411: */
2412:
2413: scgi_reconnect(srv, hctx);
2414:
2415: return HANDLER_COMEBACK;
2416: }
2417:
2418: /* not reconnected ... why
2419: *
2420: * far@#lighttpd report this for FreeBSD
2421: *
2422: */
2423:
2424: log_error_write(srv, __FILE__, __LINE__, "ssosd",
2425: "connection was dropped after accept(). reconnect() denied:",
2426: "write-offset:", hctx->wb->bytes_out,
2427: "reconnect attempts:", hctx->reconnects);
2428:
2429: return HANDLER_ERROR;
2430: } else {
2431: /* -1 == ret => error on our side */
2432: log_error_write(srv, __FILE__, __LINE__, "ssd",
2433: "write failed:", strerror(errno), errno);
2434:
2435: return HANDLER_ERROR;
2436: }
2437: }
2438:
2439: if (hctx->wb->bytes_out == hctx->wb_reqlen) {
2440: fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2441: #if (defined(__APPLE__) && defined(__MACH__)) \
2442: || defined(__FreeBSD__) || defined(__NetBSD__) \
2443: || defined(__OpenBSD__) || defined(__DragonflyBSD__)
2444: /*(*BSD stack on remote might signal POLLHUP and remote
2445: * might treat as socket error instead of half-close)*/
2446: #else
2447: /*(remote could be different machine running affected OS,
2448: * so only issue shutdown for known local sockets)*/
2449: if ( '/' == host->host->ptr[0]
2450: || buffer_is_equal_string(host->host, CONST_STR_LEN("127.0.0.1"))
2451: || buffer_is_equal_string(host->host, CONST_STR_LEN("::1"))) {
2452: shutdown(hctx->fd, SHUT_WR);
2453: }
2454: #endif
2455: scgi_set_state(srv, hctx, FCGI_STATE_READ);
2456: } else {
2457: off_t wblen = hctx->wb->bytes_in - hctx->wb->bytes_out;
2458: if (hctx->wb->bytes_in < hctx->wb_reqlen && wblen < 65536 - 16384) {
2459: /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
2460: if (!(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_POLLIN)) {
2461: con->conf.stream_request_body |= FDEVENT_STREAM_REQUEST_POLLIN;
2462: con->is_readable = 1; /* trigger optimistic read from client */
2463: }
2464: }
2465: if (0 == wblen) {
2466: fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2467: } else {
2468: fdevent_event_add(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_OUT);
2469: }
2470: }
2471:
2472: return HANDLER_WAIT_FOR_EVENT;
2473: case FCGI_STATE_READ:
2474: /* waiting for a response */
2475: return HANDLER_WAIT_FOR_EVENT;
2476: default:
2477: log_error_write(srv, __FILE__, __LINE__, "s", "(debug) unknown state");
2478: return HANDLER_ERROR;
2479: }
2480: }
2481:
2482: static handler_t scgi_send_request(server *srv, handler_ctx *hctx) {
2483: /* ok, create the request */
2484: handler_t rc = scgi_write_request(srv, hctx);
2485: if (HANDLER_ERROR != rc) {
2486: return rc;
2487: } else {
2488: scgi_proc *proc = hctx->proc;
2489: scgi_extension_host *host = hctx->host;
2490: plugin_data *p = hctx->plugin_data;
2491: connection *con = hctx->remote_conn;
2492:
2493: if (proc &&
2494: 0 == proc->is_local &&
2495: proc->state != PROC_STATE_DISABLED) {
2496: /* only disable remote servers as we don't manage them*/
2497:
2498: log_error_write(srv, __FILE__, __LINE__, "sbdb", "fcgi-server disabled:",
2499: host->host,
2500: proc->port,
2501: proc->socket);
2502:
2503: /* disable this server */
2504: proc->disable_ts = srv->cur_ts;
2505: proc->state = PROC_STATE_DISABLED;
2506: host->active_procs--;
2507: }
2508:
2509: if (hctx->state == FCGI_STATE_INIT ||
2510: hctx->state == FCGI_STATE_CONNECT) {
2511: /* connect() or getsockopt() failed,
2512: * restart the request-handling
2513: */
2514: if (proc && proc->is_local) {
2515:
2516: if (p->conf.debug) {
2517: log_error_write(srv, __FILE__, __LINE__, "sbdb", "connect() to scgi failed, restarting the request-handling:",
2518: host->host,
2519: proc->port,
2520: proc->socket);
2521: }
2522:
2523: /*
2524: * several hctx might reference the same proc
2525: *
2526: * Only one of them should mark the proc as dead all the other
2527: * ones should just take a new one.
2528: *
2529: * If a new proc was started with the old struct this might lead
2530: * the mark a perfect proc as dead otherwise
2531: *
2532: */
2533: if (proc->state == PROC_STATE_RUNNING &&
2534: hctx->pid == proc->pid) {
2535: proc->state = PROC_STATE_DIED_WAIT_FOR_PID;
2536: }
2537: }
2538: scgi_restart_dead_procs(srv, p, host);
2539:
2540: con->mode = DIRECT;/*(avoid changing con->state, con->http_status)*/
2541: scgi_connection_close(srv, hctx);
2542: con->mode = p->id;
2543:
2544: return HANDLER_COMEBACK;
2545: } else {
2546: scgi_connection_close(srv, hctx);
2547: con->http_status = 503;
2548:
2549: return HANDLER_FINISHED;
2550: }
2551: }
2552: }
2553:
2554:
2555: static handler_t scgi_recv_response(server *srv, handler_ctx *hctx);
2556:
2557:
2558: SUBREQUEST_FUNC(mod_scgi_handle_subrequest) {
2559: plugin_data *p = p_d;
2560:
2561: handler_ctx *hctx = con->plugin_ctx[p->id];
2562:
2563: if (NULL == hctx) return HANDLER_GO_ON;
2564:
2565: /* not my job */
2566: if (con->mode != p->id) return HANDLER_GO_ON;
2567:
2568: if ((con->conf.stream_response_body & FDEVENT_STREAM_RESPONSE_BUFMIN)
2569: && con->file_started) {
2570: if (chunkqueue_length(con->write_queue) > 65536 - 4096) {
2571: fdevent_event_clr(srv->ev, &(hctx->fde_ndx), hctx->fd, FDEVENT_IN);
2572: } else if (!(fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_IN)) {
2573: /* optimistic read from backend, which might re-enable FDEVENT_IN */
2574: handler_t rc = scgi_recv_response(srv, hctx); /*(might invalidate hctx)*/
2575: if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
2576: }
2577: }
2578:
2579: if (0 == hctx->wb->bytes_in
2580: ? con->state == CON_STATE_READ_POST
2581: : hctx->wb->bytes_in < hctx->wb_reqlen) {
2582: /*(64k - 4k to attempt to avoid temporary files
2583: * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
2584: if (hctx->wb->bytes_in - hctx->wb->bytes_out > 65536 - 4096
2585: && (con->conf.stream_request_body & FDEVENT_STREAM_REQUEST_BUFMIN)){
2586: con->conf.stream_request_body &= ~FDEVENT_STREAM_REQUEST_POLLIN;
2587: if (0 != hctx->wb->bytes_in) return HANDLER_WAIT_FOR_EVENT;
2588: } else {
2589: handler_t r = connection_handle_read_post_state(srv, con);
2590: chunkqueue *req_cq = con->request_content_queue;
2591: if (0 != hctx->wb->bytes_in && !chunkqueue_is_empty(req_cq)) {
2592: chunkqueue_append_chunkqueue(hctx->wb, req_cq);
2593: if (fdevent_event_get_interest(srv->ev, hctx->fd) & FDEVENT_OUT) {
2594: return (r == HANDLER_GO_ON) ? HANDLER_WAIT_FOR_EVENT : r;
2595: }
2596: }
2597: if (r != HANDLER_GO_ON) return r;
2598: }
2599: }
2600:
2601: return ((0 == hctx->wb->bytes_in || !chunkqueue_is_empty(hctx->wb))
2602: && hctx->state != FCGI_STATE_CONNECT)
2603: ? scgi_send_request(srv, hctx)
2604: : HANDLER_WAIT_FOR_EVENT;
2605: }
2606:
2607:
2608: static handler_t scgi_recv_response(server *srv, handler_ctx *hctx) {
2609:
2610: switch (scgi_demux_response(srv, hctx)) {
2611: case 0:
2612: break;
2613: case 1:
2614: /* we are done */
2615: scgi_connection_close(srv, hctx);
2616:
2617: return HANDLER_FINISHED;
2618: case -1: {
2619: connection *con = hctx->remote_conn;
2620: plugin_data *p = hctx->plugin_data;
2621:
2622: scgi_proc *proc = hctx->proc;
2623: scgi_extension_host *host= hctx->host;
2624:
2625: if (proc->pid && proc->state != PROC_STATE_DIED) {
2626: int status;
2627:
2628: /* only fetch the zombie if it is not already done */
2629:
2630: switch(waitpid(proc->pid, &status, WNOHANG)) {
2631: case 0:
2632: /* child is still alive */
2633: break;
2634: case -1:
2635: break;
2636: default:
2637: /* the child should not terminate at all */
2638: if (WIFEXITED(status)) {
2639: log_error_write(srv, __FILE__, __LINE__, "sdsd",
2640: "child exited, pid:", proc->pid,
2641: "status:", WEXITSTATUS(status));
2642: } else if (WIFSIGNALED(status)) {
2643: log_error_write(srv, __FILE__, __LINE__, "sd",
2644: "child signaled:",
2645: WTERMSIG(status));
2646: } else {
2647: log_error_write(srv, __FILE__, __LINE__, "sd",
2648: "child died somehow:",
2649: status);
2650: }
2651:
2652: if (p->conf.debug) {
2653: log_error_write(srv, __FILE__, __LINE__, "ssdsbsdsd",
2654: "--- scgi spawning",
2655: "\n\tport:", host->port,
2656: "\n\tsocket", host->unixsocket,
2657: "\n\tcurrent:", 1, "/", host->min_procs);
2658: }
2659:
2660: if (scgi_spawn_connection(srv, p, host, proc)) {
2661: /* child died */
2662: proc->state = PROC_STATE_DIED;
2663: } else {
2664: scgi_proclist_sort_down(srv, host, proc);
2665: }
2666:
2667: break;
2668: }
2669: }
2670:
2671: if (con->file_started == 0) {
2672: /* nothing has been send out yet, try to use another child */
2673:
2674: if (hctx->wb->bytes_out == 0 &&
2675: hctx->reconnects < 5) {
2676:
2677: log_error_write(srv, __FILE__, __LINE__, "ssdsd",
2678: "response not sent, request not sent, reconnection.",
2679: "connection-fd:", con->fd,
2680: "fcgi-fd:", hctx->fd);
2681:
2682: scgi_reconnect(srv, hctx);
2683:
2684: return HANDLER_COMEBACK;
2685: }
2686:
2687: log_error_write(srv, __FILE__, __LINE__, "sosdsd",
2688: "response not sent, request sent:", hctx->wb->bytes_out,
2689: "connection-fd:", con->fd,
2690: "fcgi-fd:", hctx->fd);
2691: } else {
2692: log_error_write(srv, __FILE__, __LINE__, "ssdsd",
2693: "response already sent out, termination connection",
2694: "connection-fd:", con->fd,
2695: "fcgi-fd:", hctx->fd);
2696: }
2697:
2698: http_response_backend_error(srv, con);
2699: scgi_connection_close(srv, hctx);
2700: return HANDLER_FINISHED;
2701: }
2702: }
2703:
2704: return HANDLER_GO_ON;
2705: }
2706:
2707:
2708: static handler_t scgi_handle_fdevent(server *srv, void *ctx, int revents) {
2709: handler_ctx *hctx = ctx;
2710: connection *con = hctx->remote_conn;
2711:
2712: joblist_append(srv, con);
2713:
2714: if (revents & FDEVENT_IN) {
2715: handler_t rc = scgi_recv_response(srv, hctx);/*(might invalidate hctx)*/
2716: if (rc != HANDLER_GO_ON) return rc; /*(unless HANDLER_GO_ON)*/
2717: }
2718:
2719: if (revents & FDEVENT_OUT) {
2720: return scgi_send_request(srv, hctx); /*(might invalidate hctx)*/
2721: }
2722:
2723: /* perhaps this issue is already handled */
2724: if (revents & FDEVENT_HUP) {
2725: if (hctx->state == FCGI_STATE_CONNECT) {
2726: /* getoptsock will catch this one (right ?)
2727: *
2728: * if we are in connect we might get a EINPROGRESS
2729: * in the first call and a FDEVENT_HUP in the
2730: * second round
2731: *
2732: * FIXME: as it is a bit ugly.
2733: *
2734: */
2735: scgi_send_request(srv, hctx);
2736: } else if (con->file_started) {
2737: /* drain any remaining data from kernel pipe buffers
2738: * even if (con->conf.stream_response_body
2739: * & FDEVENT_STREAM_RESPONSE_BUFMIN)
2740: * since event loop will spin on fd FDEVENT_HUP event
2741: * until unregistered. */
2742: handler_t rc;
2743: do {
2744: rc = scgi_recv_response(srv,hctx);/*(might invalidate hctx)*/
2745: } while (rc == HANDLER_GO_ON); /*(unless HANDLER_GO_ON)*/
2746: return rc; /* HANDLER_FINISHED or HANDLER_ERROR */
2747: } else {
2748: scgi_extension_host *host= hctx->host;
2749: log_error_write(srv, __FILE__, __LINE__, "sbSBSDSd",
2750: "error: unexpected close of scgi connection for",
2751: con->uri.path,
2752: "(no scgi process on host: ",
2753: host->host,
2754: ", port: ",
2755: host->port,
2756: " ?)",
2757: hctx->state);
2758:
2759: scgi_connection_close(srv, hctx);
2760: }
2761: } else if (revents & FDEVENT_ERR) {
2762: log_error_write(srv, __FILE__, __LINE__, "s",
2763: "fcgi: got a FDEVENT_ERR. Don't know why.");
2764:
2765: http_response_backend_error(srv, con);
2766: scgi_connection_close(srv, hctx);
2767: }
2768:
2769: return HANDLER_FINISHED;
2770: }
2771: #define PATCH(x) \
2772: p->conf.x = s->x;
2773: static int scgi_patch_connection(server *srv, connection *con, plugin_data *p) {
2774: size_t i, j;
2775: plugin_config *s = p->config_storage[0];
2776:
2777: PATCH(exts);
2778: PATCH(debug);
2779:
2780: /* skip the first, the global context */
2781: for (i = 1; i < srv->config_context->used; i++) {
2782: data_config *dc = (data_config *)srv->config_context->data[i];
2783: s = p->config_storage[i];
2784:
2785: /* condition didn't match */
2786: if (!config_check_cond(srv, con, dc)) continue;
2787:
2788: /* merge config */
2789: for (j = 0; j < dc->value->used; j++) {
2790: data_unset *du = dc->value->data[j];
2791:
2792: if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.server"))) {
2793: PATCH(exts);
2794: } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("scgi.debug"))) {
2795: PATCH(debug);
2796: }
2797: }
2798: }
2799:
2800: return 0;
2801: }
2802: #undef PATCH
2803:
2804:
2805: static handler_t scgi_check_extension(server *srv, connection *con, void *p_d, int uri_path_handler) {
2806: plugin_data *p = p_d;
2807: size_t s_len;
2808: int used = -1;
2809: size_t k;
2810: buffer *fn;
2811: scgi_extension *extension = NULL;
2812: scgi_extension_host *host = NULL;
2813:
2814: if (con->mode != DIRECT) return HANDLER_GO_ON;
2815:
2816: /* Possibly, we processed already this request */
2817: if (con->file_started == 1) return HANDLER_GO_ON;
2818:
2819: fn = uri_path_handler ? con->uri.path : con->physical.path;
2820:
2821: if (buffer_string_is_empty(fn)) return HANDLER_GO_ON;
2822:
2823: s_len = buffer_string_length(fn);
2824:
2825: scgi_patch_connection(srv, con, p);
2826:
2827: /* check if extension matches */
2828: for (k = 0; k < p->conf.exts->used; k++) {
2829: size_t ct_len;
2830: scgi_extension *ext = p->conf.exts->exts[k];
2831:
2832: if (buffer_is_empty(ext->key)) continue;
2833:
2834: ct_len = buffer_string_length(ext->key);
2835:
2836: if (s_len < ct_len) continue;
2837:
2838: /* check extension in the form "/scgi_pattern" */
2839: if (*(ext->key->ptr) == '/') {
2840: if (strncmp(fn->ptr, ext->key->ptr, ct_len) == 0) {
2841: extension = ext;
2842: break;
2843: }
2844: } else if (0 == strncmp(fn->ptr + s_len - ct_len, ext->key->ptr, ct_len)) {
2845: /* check extension in the form ".fcg" */
2846: extension = ext;
2847: break;
2848: }
2849: }
2850:
2851: /* extension doesn't match */
2852: if (NULL == extension) {
2853: return HANDLER_GO_ON;
2854: }
2855:
2856: /* get best server */
2857: for (k = 0; k < extension->used; k++) {
2858: scgi_extension_host *h = extension->hosts[k];
2859:
2860: /* we should have at least one proc that can do something */
2861: if (h->active_procs == 0) {
2862: continue;
2863: }
2864:
2865: if (used == -1 || h->load < used) {
2866: used = h->load;
2867:
2868: host = h;
2869: }
2870: }
2871:
2872: if (!host) {
2873: /* sorry, we don't have a server alive for this ext */
2874: con->http_status = 500;
2875: con->mode = DIRECT;
2876:
2877: /* only send the 'no handler' once */
2878: if (!extension->note_is_sent) {
2879: extension->note_is_sent = 1;
2880:
2881: log_error_write(srv, __FILE__, __LINE__, "sbsbs",
2882: "all handlers for ", con->uri.path,
2883: "on", extension->key,
2884: "are down.");
2885: }
2886:
2887: return HANDLER_FINISHED;
2888: }
2889:
2890: /* a note about no handler is not sent yet */
2891: extension->note_is_sent = 0;
2892:
2893: /* SCGI requires that Content-Length be set.
2894: * Send 411 Length Required if Content-Length missing.
2895: * (Alternatively, collect full request body before proceeding
2896: * in mod_scgi_handle_subrequest()) */
2897: if (0 == con->request.content_length
2898: && array_get_element(con->request.headers, "Transfer-Encoding")) {
2899: con->keep_alive = 0;
2900: con->http_status = 411; /* Length Required */
2901: con->mode = DIRECT;
2902: return HANDLER_FINISHED;
2903: }
2904:
2905: /*
2906: * if check-local is disabled, use the uri.path handler
2907: *
2908: */
2909:
2910: /* init handler-context */
2911: if (uri_path_handler) {
2912: if (host->check_local == 0) {
2913: handler_ctx *hctx;
2914: char *pathinfo;
2915:
2916: hctx = handler_ctx_init();
2917:
2918: hctx->remote_conn = con;
2919: hctx->plugin_data = p;
2920: hctx->host = host;
2921: hctx->proc = NULL;
2922:
2923: hctx->conf.exts = p->conf.exts;
2924: hctx->conf.debug = p->conf.debug;
2925:
2926: con->plugin_ctx[p->id] = hctx;
2927:
2928: host->load++;
2929:
2930: con->mode = p->id;
2931:
2932: if (con->conf.log_request_handling) {
2933: log_error_write(srv, __FILE__, __LINE__, "s",
2934: "handling it in mod_scgi");
2935: }
2936:
2937: /* the prefix is the SCRIPT_NAME,
2938: * everything from start to the next slash
2939: * this is important for check-local = "disable"
2940: *
2941: * if prefix = /admin.fcgi
2942: *
2943: * /admin.fcgi/foo/bar
2944: *
2945: * SCRIPT_NAME = /admin.fcgi
2946: * PATH_INFO = /foo/bar
2947: *
2948: * if prefix = /fcgi-bin/
2949: *
2950: * /fcgi-bin/foo/bar
2951: *
2952: * SCRIPT_NAME = /fcgi-bin/foo
2953: * PATH_INFO = /bar
2954: *
2955: */
2956:
2957: /* the rewrite is only done for /prefix/? matches */
2958: if (host->fix_root_path_name && extension->key->ptr[0] == '/' && extension->key->ptr[1] == '\0') {
2959: buffer_copy_string(con->request.pathinfo, con->uri.path->ptr);
2960: buffer_string_set_length(con->uri.path, 0);
2961: } else if (extension->key->ptr[0] == '/' &&
2962: buffer_string_length(con->uri.path) > buffer_string_length(extension->key) &&
2963: NULL != (pathinfo = strchr(con->uri.path->ptr + buffer_string_length(extension->key), '/'))) {
2964: /* rewrite uri.path and pathinfo */
2965:
2966: buffer_copy_string(con->request.pathinfo, pathinfo);
2967: buffer_string_set_length(con->uri.path, buffer_string_length(con->uri.path) - buffer_string_length(con->request.pathinfo));
2968: }
2969: }
2970: } else {
2971: handler_ctx *hctx;
2972: hctx = handler_ctx_init();
2973:
2974: hctx->remote_conn = con;
2975: hctx->plugin_data = p;
2976: hctx->host = host;
2977: hctx->proc = NULL;
2978:
2979: hctx->conf.exts = p->conf.exts;
2980: hctx->conf.debug = p->conf.debug;
2981:
2982: con->plugin_ctx[p->id] = hctx;
2983:
2984: host->load++;
2985:
2986: con->mode = p->id;
2987:
2988: if (con->conf.log_request_handling) {
2989: log_error_write(srv, __FILE__, __LINE__, "s", "handling it in mod_scgi");
2990: }
2991: }
2992:
2993: return HANDLER_GO_ON;
2994: }
2995:
2996: /* uri-path handler */
2997: static handler_t scgi_check_extension_1(server *srv, connection *con, void *p_d) {
2998: return scgi_check_extension(srv, con, p_d, 1);
2999: }
3000:
3001: /* start request handler */
3002: static handler_t scgi_check_extension_2(server *srv, connection *con, void *p_d) {
3003: return scgi_check_extension(srv, con, p_d, 0);
3004: }
3005:
3006:
3007: TRIGGER_FUNC(mod_scgi_handle_trigger) {
3008: plugin_data *p = p_d;
3009: size_t i, j, n;
3010:
3011:
3012: /* perhaps we should kill a connect attempt after 10-15 seconds
3013: *
3014: * currently we wait for the TCP timeout which is on Linux 180 seconds
3015: *
3016: *
3017: *
3018: */
3019:
3020: /* check all childs if they are still up */
3021:
3022: for (i = 0; i < srv->config_context->used; i++) {
3023: plugin_config *conf;
3024: scgi_exts *exts;
3025:
3026: conf = p->config_storage[i];
3027:
3028: exts = conf->exts;
3029:
3030: for (j = 0; j < exts->used; j++) {
3031: scgi_extension *ex;
3032:
3033: ex = exts->exts[j];
3034:
3035: for (n = 0; n < ex->used; n++) {
3036:
3037: scgi_proc *proc;
3038: unsigned long sum_load = 0;
3039: scgi_extension_host *host;
3040:
3041: host = ex->hosts[n];
3042:
3043: scgi_restart_dead_procs(srv, p, host);
3044:
3045: for (proc = host->first; proc; proc = proc->next) {
3046: sum_load += proc->load;
3047: }
3048:
3049: if (host->num_procs &&
3050: host->num_procs < host->max_procs &&
3051: (sum_load / host->num_procs) > host->max_load_per_proc) {
3052: /* overload, spawn new child */
3053: scgi_proc *fp = NULL;
3054:
3055: if (p->conf.debug) {
3056: log_error_write(srv, __FILE__, __LINE__, "s",
3057: "overload detected, spawning a new child");
3058: }
3059:
3060: for (fp = host->unused_procs; fp && fp->pid != 0; fp = fp->next);
3061:
3062: if (fp) {
3063: if (fp == host->unused_procs) host->unused_procs = fp->next;
3064:
3065: if (fp->next) fp->next->prev = NULL;
3066:
3067: host->max_id++;
3068: } else {
3069: fp = scgi_process_init();
3070: fp->id = host->max_id++;
3071: }
3072:
3073: host->num_procs++;
3074:
3075: if (buffer_string_is_empty(host->unixsocket)) {
3076: fp->port = host->port + fp->id;
3077: } else {
3078: buffer_copy_buffer(fp->socket, host->unixsocket);
3079: buffer_append_string_len(fp->socket, CONST_STR_LEN("-"));
3080: buffer_append_int(fp->socket, fp->id);
3081: }
3082:
3083: if (scgi_spawn_connection(srv, p, host, fp)) {
3084: log_error_write(srv, __FILE__, __LINE__, "s",
3085: "ERROR: spawning fcgi failed.");
3086: scgi_process_free(fp);
3087: return HANDLER_ERROR;
3088: }
3089:
3090: fp->prev = NULL;
3091: fp->next = host->first;
3092: if (host->first) {
3093: host->first->prev = fp;
3094: }
3095: host->first = fp;
3096: }
3097:
3098: for (proc = host->first; proc; proc = proc->next) {
3099: if (proc->load != 0) break;
3100: if (host->num_procs <= host->min_procs) break;
3101: if (proc->pid == 0) continue;
3102:
3103: if (srv->cur_ts - proc->last_used > host->idle_timeout) {
3104: /* a proc is idling for a long time now,
3105: * terminated it */
3106:
3107: if (p->conf.debug) {
3108: log_error_write(srv, __FILE__, __LINE__, "ssbsd",
3109: "idle-timeout reached, terminating child:",
3110: "socket:", proc->socket,
3111: "pid", proc->pid);
3112: }
3113:
3114:
3115: if (proc->next) proc->next->prev = proc->prev;
3116: if (proc->prev) proc->prev->next = proc->next;
3117:
3118: if (proc->prev == NULL) host->first = proc->next;
3119:
3120: proc->prev = NULL;
3121: proc->next = host->unused_procs;
3122:
3123: if (host->unused_procs) host->unused_procs->prev = proc;
3124: host->unused_procs = proc;
3125:
3126: kill(proc->pid, SIGTERM);
3127:
3128: proc->state = PROC_STATE_KILLED;
3129:
3130: log_error_write(srv, __FILE__, __LINE__, "ssbsd",
3131: "killed:",
3132: "socket:", proc->socket,
3133: "pid", proc->pid);
3134:
3135: host->num_procs--;
3136:
3137: /* proc is now in unused, let the next second handle the next process */
3138: break;
3139: }
3140: }
3141:
3142: for (proc = host->unused_procs; proc; proc = proc->next) {
3143: int status;
3144:
3145: if (proc->pid == 0) continue;
3146:
3147: switch (waitpid(proc->pid, &status, WNOHANG)) {
3148: case 0:
3149: /* child still running after timeout, good */
3150: break;
3151: case -1:
3152: if (errno != EINTR) {
3153: /* no PID found ? should never happen */
3154: log_error_write(srv, __FILE__, __LINE__, "sddss",
3155: "pid ", proc->pid, proc->state,
3156: "not found:", strerror(errno));
3157:
3158: #if 0
3159: if (errno == ECHILD) {
3160: /* someone else has cleaned up for us */
3161: proc->pid = 0;
3162: proc->state = PROC_STATE_UNSET;
3163: }
3164: #endif
3165: }
3166: break;
3167: default:
3168: /* the child should not terminate at all */
3169: if (WIFEXITED(status)) {
3170: if (proc->state != PROC_STATE_KILLED) {
3171: log_error_write(srv, __FILE__, __LINE__, "sdb",
3172: "child exited:",
3173: WEXITSTATUS(status), proc->socket);
3174: }
3175: } else if (WIFSIGNALED(status)) {
3176: if (WTERMSIG(status) != SIGTERM) {
3177: log_error_write(srv, __FILE__, __LINE__, "sd",
3178: "child signaled:",
3179: WTERMSIG(status));
3180: }
3181: } else {
3182: log_error_write(srv, __FILE__, __LINE__, "sd",
3183: "child died somehow:",
3184: status);
3185: }
3186: proc->pid = 0;
3187: proc->state = PROC_STATE_UNSET;
3188: host->max_id--;
3189: }
3190: }
3191: }
3192: }
3193: }
3194:
3195: return HANDLER_GO_ON;
3196: }
3197:
3198:
3199: int mod_scgi_plugin_init(plugin *p);
3200: int mod_scgi_plugin_init(plugin *p) {
3201: p->version = LIGHTTPD_VERSION_ID;
3202: p->name = buffer_init_string("scgi");
3203:
3204: p->init = mod_scgi_init;
3205: p->cleanup = mod_scgi_free;
3206: p->set_defaults = mod_scgi_set_defaults;
3207: p->connection_reset = scgi_connection_reset;
3208: p->handle_connection_close = scgi_connection_reset;
3209: p->handle_uri_clean = scgi_check_extension_1;
3210: p->handle_subrequest_start = scgi_check_extension_2;
3211: p->handle_subrequest = mod_scgi_handle_subrequest;
3212: p->handle_trigger = mod_scgi_handle_trigger;
3213:
3214: p->data = NULL;
3215:
3216: return 0;
3217: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>