8 #include "http_chunk.h"
10 #include "connections.h"
16 #include "inet_ntop_cache.h"
18 #include <sys/types.h>
30 #include "sys-socket.h"
31 #include "sys-endian.h"
37 #ifdef HAVE_SYS_WAIT_H
38 # include <sys/wait.h>
41 enum {EOL_UNSET
, EOL_N
, EOL_RN
};
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 */
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 */
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
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
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
;
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
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)
160 /* if socket is local we can start the scgi
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
170 /* bin-path is set bin-environment is taken to
171 * create the environement before starting the
180 * docroot-translation between URL->phys and the
184 * - different dir-layout if remote
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
229 only if a process is killed max_id waits for the process itself
230 to die and decrements its afterwards */
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
244 * if the scgi process is remote that whole goes down
247 * ext -> host -> procs
250 * in case of PHP and FCGI_CHILDREN we have again a procs
251 * but we don't control it directly.
256 buffer
*key
; /* like .php */
259 scgi_extension_host
**hosts
;
266 scgi_extension
**exts
;
272 enum { LI_PROTOCOL_SCGI
, LI_PROTOCOL_UWSGI
};
288 /* generic plugin data, shared between all connections */
295 buffer
*parse_response
;
297 plugin_config
**config_storage
;
299 plugin_config conf
; /* this is only used as long as no handler_ctx is setup */
302 /* connection specific data */
303 typedef enum { FCGI_STATE_INIT
, FCGI_STATE_CONNECT
, FCGI_STATE_PREPARE_WRITE
,
304 FCGI_STATE_WRITE
, FCGI_STATE_READ
305 } scgi_connection_state_t
;
311 scgi_extension_host
*host
;
313 scgi_connection_state_t state
;
314 time_t state_timestamp
;
319 buffer
*response_header
;
321 int fd
; /* fd to the scgi process */
322 int fde_ndx
; /* index into the fd-event buffer */
326 int reconnects
; /* number of reconnect attempts */
330 connection
*remote_conn
; /* dumb pointer */
331 plugin_data
*plugin_data
; /* dumb pointer */
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) {
342 signal(SIGTTOU
, SIG_DFL
);
345 signal(SIGTTIN
, SIG_DFL
);
348 signal(SIGTSTP
, SIG_DFL
);
350 signal(SIGHUP
, SIG_DFL
);
351 signal(SIGPIPE
, SIG_DFL
);
352 signal(SIGUSR1
, SIG_DFL
);
355 static handler_ctx
* handler_ctx_init(void) {
358 hctx
= calloc(1, sizeof(*hctx
));
363 hctx
->response
= buffer_init();
364 hctx
->response_header
= buffer_init();
366 hctx
->state
= FCGI_STATE_INIT
;
371 hctx
->reconnects
= 0;
373 hctx
->wb
= chunkqueue_init();
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
);
388 static scgi_proc
*scgi_process_init(void) {
391 f
= calloc(1, sizeof(*f
));
393 f
->socket
= buffer_init();
401 static void scgi_process_free(scgi_proc
*f
) {
404 scgi_process_free(f
->next
);
406 buffer_free(f
->socket
);
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();
427 static void scgi_host_free(scgi_extension_host
*h
) {
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
);
449 static scgi_exts
*scgi_extensions_init(void) {
452 f
= calloc(1, sizeof(*f
));
458 static void scgi_extensions_free(scgi_exts
*f
) {
463 for (i
= 0; i
< f
->used
; i
++) {
469 for (j
= 0; j
< fe
->used
; j
++) {
470 scgi_extension_host
*h
;
477 buffer_free(fe
->key
);
488 static int scgi_extension_insert(scgi_exts
*ext
, buffer
*key
, scgi_extension_host
*fh
) {
492 /* there is something */
494 for (i
= 0; i
< ext
->used
; i
++) {
495 if (buffer_is_equal(key
, ext
->exts
[i
]->key
)) {
500 if (i
== ext
->used
) {
501 /* filextension is new */
502 fe
= calloc(1, sizeof(*fe
));
504 fe
->key
= buffer_init();
505 buffer_copy_buffer(fe
->key
, key
);
509 if (ext
->size
== 0) {
511 ext
->exts
= malloc(ext
->size
* sizeof(*(ext
->exts
)));
512 force_assert(ext
->exts
);
513 } else if (ext
->used
== ext
->size
) {
515 ext
->exts
= realloc(ext
->exts
, ext
->size
* sizeof(*(ext
->exts
)));
516 force_assert(ext
->exts
);
518 ext
->exts
[ext
->used
++] = fe
;
525 fe
->hosts
= malloc(fe
->size
* sizeof(*(fe
->hosts
)));
526 force_assert(fe
->hosts
);
527 } else if (fe
->size
== fe
->used
) {
529 fe
->hosts
= realloc(fe
->hosts
, fe
->size
* sizeof(*(fe
->hosts
)));
530 force_assert(fe
->hosts
);
533 fe
->hosts
[fe
->used
++] = fh
;
539 INIT_FUNC(mod_scgi_init
) {
542 p
= calloc(1, sizeof(*p
));
545 p
->scgi_env
= buffer_init();
547 p
->path
= buffer_init();
548 p
->parse_response
= buffer_init();
554 FREE_FUNC(mod_scgi_free
) {
555 plugin_data
*p
= p_d
;
559 buffer_free(p
->scgi_env
);
560 buffer_free(p
->path
);
561 buffer_free(p
->parse_response
);
563 if (p
->config_storage
) {
565 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
566 plugin_config
*s
= p
->config_storage
[i
];
569 if (NULL
== s
) continue;
573 for (j
= 0; j
< exts
->used
; j
++) {
578 for (n
= 0; n
< ex
->used
; n
++) {
580 scgi_extension_host
*host
;
584 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
585 if (proc
->pid
!= 0) kill(proc
->pid
, SIGTERM
);
587 if (proc
->is_local
&&
588 !buffer_string_is_empty(proc
->socket
)) {
589 unlink(proc
->socket
->ptr
);
593 for (proc
= host
->unused_procs
; proc
; proc
= proc
->next
) {
594 if (proc
->pid
!= 0) kill(proc
->pid
, SIGTERM
);
596 if (proc
->is_local
&&
597 !buffer_string_is_empty(proc
->socket
)) {
598 unlink(proc
->socket
->ptr
);
604 scgi_extensions_free(s
->exts
);
608 free(p
->config_storage
);
613 return HANDLER_GO_ON
;
616 static int env_add(char_array
*env
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
620 if (!key
|| !val
) return -1;
622 dst
= malloc(key_len
+ val_len
+ 3);
624 memcpy(dst
, key
, key_len
);
626 /* add the \0 from the value */
627 memcpy(dst
+ key_len
+ 1, val
, val_len
+ 1);
629 for (i
= 0; i
< env
->used
; i
++) {
630 if (0 == strncmp(dst
, env
->ptr
[i
], key_len
+ 1)) {
631 /* don't care about free as we are in a forked child which is going to exec(...) */
632 /* free(env->ptr[i]); */
638 if (env
->size
== 0) {
640 env
->ptr
= malloc(env
->size
* sizeof(*env
->ptr
));
641 force_assert(env
->ptr
);
642 } else if (env
->size
== env
->used
) {
644 env
->ptr
= realloc(env
->ptr
, env
->size
* sizeof(*env
->ptr
));
645 force_assert(env
->ptr
);
648 env
->ptr
[env
->used
++] = dst
;
653 #if !defined(HAVE_FORK)
654 static int scgi_spawn_connection(server
*srv
,
656 scgi_extension_host
*host
,
665 #else /* -> defined(HAVE_FORK) */
667 static int scgi_spawn_connection(server
*srv
,
669 scgi_extension_host
*host
,
673 struct timeval tv
= { 0, 100 * 1000 };
675 struct sockaddr_un scgi_addr_un
;
677 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
678 struct sockaddr_in6 scgi_addr_in6
;
680 struct sockaddr_in scgi_addr_in
;
681 struct sockaddr
*scgi_addr
;
686 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
687 "new proc, socket:", proc
->port
, proc
->socket
);
691 if (!buffer_string_is_empty(proc
->socket
)) {
693 memset(&scgi_addr_un
, 0, sizeof(scgi_addr_un
));
694 scgi_addr_un
.sun_family
= AF_UNIX
;
695 if (buffer_string_length(proc
->socket
) + 1 > sizeof(scgi_addr_un
.sun_path
)) {
696 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
697 "ERROR: Unix Domain socket filename too long:",
701 memcpy(scgi_addr_un
.sun_path
, proc
->socket
->ptr
, buffer_string_length(proc
->socket
) + 1);
704 servlen
= SUN_LEN(&scgi_addr_un
);
707 servlen
= buffer_string_length(proc
->socket
) + 1 + sizeof(scgi_addr_un
.sun_family
);
709 scgi_addr
= (struct sockaddr
*) &scgi_addr_un
;
711 log_error_write(srv
, __FILE__
, __LINE__
, "s",
712 "ERROR: Unix Domain sockets are not supported.");
715 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
716 } else if (host
->family
== AF_INET6
&& !buffer_string_is_empty(host
->host
)) {
717 memset(&scgi_addr_in6
, 0, sizeof(scgi_addr_in6
));
718 scgi_addr_in6
.sin6_family
= AF_INET6
;
719 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &scgi_addr_in6
.sin6_addr
);
720 scgi_addr_in6
.sin6_port
= htons(proc
->port
);
721 servlen
= sizeof(scgi_addr_in6
);
722 scgi_addr
= (struct sockaddr
*) &scgi_addr_in6
;
725 memset(&scgi_addr_in
, 0, sizeof(scgi_addr_in
));
726 scgi_addr_in
.sin_family
= AF_INET
;
728 if (buffer_string_is_empty(host
->host
)) {
729 scgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
733 /* set a usefull default */
734 scgi_addr_in
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
737 if (NULL
== (he
= gethostbyname(host
->host
->ptr
))) {
738 log_error_write(srv
, __FILE__
, __LINE__
,
739 "sdb", "gethostbyname failed: ",
740 h_errno
, host
->host
);
744 if (he
->h_addrtype
!= AF_INET
) {
745 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "addr-type != AF_INET: ", he
->h_addrtype
);
749 if (he
->h_length
!= sizeof(struct in_addr
)) {
750 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "addr-length != sizeof(in_addr): ", he
->h_length
);
754 memcpy(&(scgi_addr_in
.sin_addr
.s_addr
), he
->h_addr_list
[0], he
->h_length
);
757 scgi_addr_in
.sin_port
= htons(proc
->port
);
758 servlen
= sizeof(scgi_addr_in
);
760 scgi_addr
= (struct sockaddr
*) &scgi_addr_in
;
763 if (-1 == (scgi_fd
= fdevent_socket_cloexec(scgi_addr
->sa_family
, SOCK_STREAM
, 0))) {
764 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
765 "failed:", strerror(errno
));
769 if (-1 == connect(scgi_fd
, scgi_addr
, servlen
)) {
770 /* server is not up, spawn in */
774 if (!buffer_string_is_empty(proc
->socket
)) {
775 unlink(proc
->socket
->ptr
);
781 if (-1 == (scgi_fd
= fdevent_socket_cloexec(scgi_addr
->sa_family
, SOCK_STREAM
, 0))) {
782 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
783 "socket failed:", strerror(errno
));
788 if (setsockopt(scgi_fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
)) < 0) {
789 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
790 "socketsockopt failed:", strerror(errno
));
796 if (-1 == bind(scgi_fd
, scgi_addr
, servlen
)) {
797 log_error_write(srv
, __FILE__
, __LINE__
, "sbds",
806 if (-1 == listen(scgi_fd
, host
->listen_backlog
)) {
807 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
808 "listen failed:", strerror(errno
));
813 switch ((child
= fork())) {
821 /* create environment */
832 (void)fcntl(scgi_fd
, F_SETFD
, 0); /* clear cloexec */
835 /* we don't need the client socket */
836 for (fd
= 3; fd
< 256; fd
++) {
840 /* build clean environment */
841 if (host
->bin_env_copy
->used
) {
842 for (i
= 0; i
< host
->bin_env_copy
->used
; i
++) {
843 data_string
*ds
= (data_string
*)host
->bin_env_copy
->data
[i
];
846 if (NULL
!= (ge
= getenv(ds
->value
->ptr
))) {
847 env_add(&env
, CONST_BUF_LEN(ds
->value
), ge
, strlen(ge
));
851 char ** const e
= environ
;
852 for (i
= 0; e
[i
]; ++i
) {
855 if (NULL
!= (eq
= strchr(e
[i
], '='))) {
856 env_add(&env
, e
[i
], eq
- e
[i
], eq
+1, strlen(eq
+1));
861 /* create environment */
862 for (i
= 0; i
< host
->bin_env
->used
; i
++) {
863 data_string
*ds
= (data_string
*)host
->bin_env
->data
[i
];
865 env_add(&env
, CONST_BUF_LEN(ds
->key
), CONST_BUF_LEN(ds
->value
));
868 for (i
= 0; i
< env
.used
; i
++) {
869 /* search for PHP_FCGI_CHILDREN */
870 if (0 == strncmp(env
.ptr
[i
], "PHP_FCGI_CHILDREN=", sizeof("PHP_FCGI_CHILDREN=") - 1)) break;
873 /* not found, add a default */
875 env_add(&env
, CONST_STR_LEN("PHP_FCGI_CHILDREN"), CONST_STR_LEN("1"));
878 env
.ptr
[env
.used
] = NULL
;
881 buffer_copy_string_len(b
, CONST_STR_LEN("exec "));
882 buffer_append_string_buffer(b
, host
->bin_path
);
887 execle("/bin/sh", "sh", "-c", b
->ptr
, (char *)NULL
, env
.ptr
);
889 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
890 "execl failed for:", host
->bin_path
, strerror(errno
));
905 select(0, NULL
, NULL
, NULL
, &tv
);
907 switch (waitpid(child
, &status
, WNOHANG
)) {
909 /* child still running after timeout, good */
912 /* no PID found ? should never happen */
913 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
914 "pid not found:", strerror(errno
));
917 /* the child should not terminate at all */
918 if (WIFEXITED(status
)) {
919 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
920 "child exited (is this a SCGI binary ?):",
921 WEXITSTATUS(status
));
922 } else if (WIFSIGNALED(status
)) {
923 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
927 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
928 "child died somehow:",
934 /* register process */
936 proc
->last_used
= srv
->cur_ts
;
948 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
949 "(debug) socket is already used, won't spawn:",
954 proc
->state
= PROC_STATE_RUNNING
;
955 host
->active_procs
++;
960 #endif /* HAVE_FORK */
962 static scgi_extension_host
* unixsocket_is_dup(plugin_data
*p
, size_t used
, buffer
*unixsocket
) {
964 for (i
= 0; i
< used
; ++i
) {
965 scgi_exts
*exts
= p
->config_storage
[i
]->exts
;
966 for (j
= 0; j
< exts
->used
; ++j
) {
967 scgi_extension
*ex
= exts
->exts
[j
];
968 for (n
= 0; n
< ex
->used
; ++n
) {
969 scgi_extension_host
*host
= ex
->hosts
[n
];
970 if (!buffer_string_is_empty(host
->unixsocket
)
971 && buffer_is_equal(host
->unixsocket
, unixsocket
)
972 && !buffer_string_is_empty(host
->bin_path
))
981 SETDEFAULTS_FUNC(mod_scgi_set_defaults
) {
982 plugin_data
*p
= p_d
;
985 scgi_extension_host
*df
= NULL
;
987 config_values_t cv
[] = {
988 { "scgi.server", NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
989 { "scgi.debug", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
990 { "scgi.protocol", NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
991 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
994 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
995 force_assert(p
->config_storage
);
997 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
998 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
1001 s
= malloc(sizeof(plugin_config
));
1003 s
->exts
= scgi_extensions_init();
1005 s
->proto
= LI_PROTOCOL_SCGI
;
1007 cv
[0].destination
= s
->exts
;
1008 cv
[1].destination
= &(s
->debug
);
1009 cv
[2].destination
= NULL
; /* T_CONFIG_LOCAL */
1011 p
->config_storage
[i
] = s
;
1013 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
1021 if (NULL
!= (du
= array_get_element(config
->value
, "scgi.protocol"))) {
1022 data_string
*ds
= (data_string
*)du
;
1023 if (du
->type
== TYPE_STRING
1024 && buffer_is_equal_string(ds
->value
, CONST_STR_LEN("scgi"))) {
1025 s
->proto
= LI_PROTOCOL_SCGI
;
1026 } else if (du
->type
== TYPE_STRING
1027 && buffer_is_equal_string(ds
->value
, CONST_STR_LEN("uwsgi"))) {
1028 s
->proto
= LI_PROTOCOL_UWSGI
;
1030 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
1031 "unexpected type for key: ", "scgi.protocol", "expected \"scgi\" or \"uwsgi\"");
1037 if (NULL
!= (du
= array_get_element(config
->value
, "scgi.server"))) {
1039 data_array
*da
= (data_array
*)du
;
1041 if (du
->type
!= TYPE_ARRAY
) {
1042 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
1043 "unexpected type for key: ", "scgi.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1050 * scgi.server = ( "<ext>" => ( ... ),
1051 * "<ext>" => ( ... ) )
1054 for (j
= 0; j
< da
->value
->used
; j
++) {
1056 data_array
*da_ext
= (data_array
*)da
->value
->data
[j
];
1058 if (da
->value
->data
[j
]->type
!= TYPE_ARRAY
) {
1059 log_error_write(srv
, __FILE__
, __LINE__
, "sssbs",
1060 "unexpected type for key: ", "scgi.server",
1061 "[", da
->value
->data
[j
]->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1067 * da_ext->key == name of the extension
1071 * scgi.server = ( "<ext>" =>
1072 * ( "<host>" => ( ... ),
1073 * "<host>" => ( ... )
1078 for (n
= 0; n
< da_ext
->value
->used
; n
++) {
1079 data_array
*da_host
= (data_array
*)da_ext
->value
->data
[n
];
1081 config_values_t fcv
[] = {
1082 { "host", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
1083 { "docroot", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
1084 { "socket", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
1085 { "bin-path", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
1087 { "check-local", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 4 */
1088 { "port", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 5 */
1089 { "min-procs-not-working", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 this is broken for now */
1090 { "max-procs", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 7 */
1091 { "max-load-per-proc", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 8 */
1092 { "idle-timeout", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 9 */
1093 { "disable-time", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 10 */
1095 { "bin-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 11 */
1096 { "bin-copy-environment", NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 12 */
1097 { "fix-root-scriptname", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 13 */
1098 { "listen-backlog", NULL
, T_CONFIG_INT
, T_CONFIG_SCOPE_CONNECTION
}, /* 14 */
1099 { "x-sendfile", NULL
, T_CONFIG_BOOLEAN
, T_CONFIG_SCOPE_CONNECTION
}, /* 15 */
1100 { "x-sendfile-docroot",NULL
, T_CONFIG_ARRAY
, T_CONFIG_SCOPE_CONNECTION
}, /* 16 */
1103 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
1106 if (da_host
->type
!= TYPE_ARRAY
) {
1107 log_error_write(srv
, __FILE__
, __LINE__
, "ssSBS",
1108 "unexpected type for key:",
1110 "[", da_host
->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
1115 df
= scgi_host_init();
1117 df
->check_local
= 1;
1120 df
->max_load_per_proc
= 1;
1121 df
->idle_timeout
= 60;
1122 df
->disable_time
= 60;
1123 df
->fix_root_path_name
= 0;
1124 df
->listen_backlog
= 1024;
1125 df
->xsendfile_allow
= 0;
1128 fcv
[0].destination
= df
->host
;
1129 fcv
[1].destination
= df
->docroot
;
1130 fcv
[2].destination
= df
->unixsocket
;
1131 fcv
[3].destination
= df
->bin_path
;
1133 fcv
[4].destination
= &(df
->check_local
);
1134 fcv
[5].destination
= &(df
->port
);
1135 fcv
[6].destination
= &(df
->min_procs
);
1136 fcv
[7].destination
= &(df
->max_procs
);
1137 fcv
[8].destination
= &(df
->max_load_per_proc
);
1138 fcv
[9].destination
= &(df
->idle_timeout
);
1139 fcv
[10].destination
= &(df
->disable_time
);
1141 fcv
[11].destination
= df
->bin_env
;
1142 fcv
[12].destination
= df
->bin_env_copy
;
1143 fcv
[13].destination
= &(df
->fix_root_path_name
);
1144 fcv
[14].destination
= &(df
->listen_backlog
);
1145 fcv
[15].destination
= &(df
->xsendfile_allow
);
1146 fcv
[16].destination
= df
->xsendfile_docroot
;
1149 if (0 != config_insert_values_internal(srv
, da_host
->value
, fcv
, T_CONFIG_SCOPE_CONNECTION
)) {
1153 if ((!buffer_string_is_empty(df
->host
) || df
->port
) &&
1154 !buffer_string_is_empty(df
->unixsocket
)) {
1155 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1156 "either host+port or socket");
1161 if (!buffer_string_is_empty(df
->unixsocket
)) {
1162 /* unix domain socket */
1163 struct sockaddr_un un
;
1165 if (buffer_string_length(df
->unixsocket
) + 1 > sizeof(un
.sun_path
) - 2) {
1166 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1167 "path of the unixdomain socket is too large");
1171 if (!buffer_string_is_empty(df
->bin_path
)) {
1172 scgi_extension_host
*duplicate
= unixsocket_is_dup(p
, i
+1, df
->unixsocket
);
1173 if (NULL
!= duplicate
) {
1174 if (!buffer_is_equal(df
->bin_path
, duplicate
->bin_path
)) {
1175 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
1176 "duplicate unixsocket path:",
1186 df
->family
= AF_UNIX
;
1190 if (buffer_string_is_empty(df
->host
) &&
1191 buffer_string_is_empty(df
->bin_path
)) {
1192 log_error_write(srv
, __FILE__
, __LINE__
, "sbbbs",
1193 "missing key (string):",
1200 } else if (df
->port
== 0) {
1201 log_error_write(srv
, __FILE__
, __LINE__
, "sbbbs",
1202 "missing key (short):",
1210 df
->family
= (!buffer_string_is_empty(df
->host
) && NULL
!= strchr(df
->host
->ptr
, ':')) ? AF_INET6
: AF_INET
;
1214 /* already init'd; skip spawning */
1215 } else if (!buffer_string_is_empty(df
->bin_path
)) {
1216 /* a local socket + self spawning */
1219 /* HACK: just to make sure the adaptive spawing is disabled */
1220 df
->min_procs
= df
->max_procs
;
1222 if (df
->min_procs
> df
->max_procs
) df
->max_procs
= df
->min_procs
;
1223 if (df
->max_load_per_proc
< 1) df
->max_load_per_proc
= 0;
1226 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsdsbsdsd",
1227 "--- scgi spawning local",
1228 "\n\tproc:", df
->bin_path
,
1229 "\n\tport:", df
->port
,
1230 "\n\tsocket", df
->unixsocket
,
1231 "\n\tmin-procs:", df
->min_procs
,
1232 "\n\tmax-procs:", df
->max_procs
);
1235 for (pno
= 0; pno
< df
->min_procs
; pno
++) {
1238 proc
= scgi_process_init();
1239 proc
->id
= df
->num_procs
++;
1242 if (buffer_string_is_empty(df
->unixsocket
)) {
1243 proc
->port
= df
->port
+ pno
;
1245 buffer_copy_buffer(proc
->socket
, df
->unixsocket
);
1246 buffer_append_string_len(proc
->socket
, CONST_STR_LEN("-"));
1247 buffer_append_int(proc
->socket
, pno
);
1251 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsdsd",
1252 "--- scgi spawning",
1253 "\n\tport:", df
->port
,
1254 "\n\tsocket", df
->unixsocket
,
1255 "\n\tcurrent:", pno
, "/", df
->min_procs
);
1258 if (!srv
->srvconf
.preflight_check
1259 && scgi_spawn_connection(srv
, p
, df
, proc
)) {
1260 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1261 "[ERROR]: spawning fcgi failed.");
1262 scgi_process_free(proc
);
1266 proc
->next
= df
->first
;
1267 if (df
->first
) df
->first
->prev
= proc
;
1274 fp
= scgi_process_init();
1275 fp
->id
= df
->num_procs
++;
1278 fp
->state
= PROC_STATE_RUNNING
;
1280 if (buffer_string_is_empty(df
->unixsocket
)) {
1281 fp
->port
= df
->port
;
1283 buffer_copy_buffer(fp
->socket
, df
->unixsocket
);
1292 if (df
->xsendfile_docroot
->used
) {
1294 for (k
= 0; k
< df
->xsendfile_docroot
->used
; ++k
) {
1295 data_string
*ds
= (data_string
*)df
->xsendfile_docroot
->data
[k
];
1296 if (ds
->type
!= TYPE_STRING
) {
1297 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1298 "unexpected type for x-sendfile-docroot; expected: \"x-sendfile-docroot\" => ( \"/allowed/path\", ... )");
1301 if (ds
->value
->ptr
[0] != '/') {
1302 log_error_write(srv
, __FILE__
, __LINE__
, "SBs",
1303 "x-sendfile-docroot paths must begin with '/'; invalid: \"", ds
->value
, "\"");
1306 buffer_path_simplify(ds
->value
, ds
->value
);
1307 buffer_append_slash(ds
->value
);
1311 /* if extension already exists, take it */
1312 scgi_extension_insert(s
->exts
, da_ext
->key
, df
);
1319 return HANDLER_GO_ON
;
1322 if (NULL
!= df
) scgi_host_free(df
);
1323 return HANDLER_ERROR
;
1326 static int scgi_set_state(server
*srv
, handler_ctx
*hctx
, scgi_connection_state_t state
) {
1327 hctx
->state
= state
;
1328 hctx
->state_timestamp
= srv
->cur_ts
;
1334 static void scgi_connection_close(server
*srv
, handler_ctx
*hctx
) {
1338 p
= hctx
->plugin_data
;
1339 con
= hctx
->remote_conn
;
1341 if (hctx
->fd
!= -1) {
1342 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
1343 fdevent_unregister(srv
->ev
, hctx
->fd
);
1344 fdevent_sched_close(srv
->ev
, hctx
->fd
, 1);
1347 if (hctx
->host
&& hctx
->proc
) {
1350 if (hctx
->got_proc
) {
1351 /* after the connect the process gets a load */
1354 if (hctx
->conf
.debug
) {
1355 log_error_write(srv
, __FILE__
, __LINE__
, "sddb",
1358 hctx
->proc
->pid
, hctx
->proc
->socket
);
1362 scgi_proclist_sort_down(srv
, hctx
->host
, hctx
->proc
);
1366 handler_ctx_free(hctx
);
1367 con
->plugin_ctx
[p
->id
] = NULL
;
1369 /* finish response (if not already con->file_started, con->file_finished) */
1370 if (con
->mode
== p
->id
) {
1371 http_response_backend_done(srv
, con
);
1375 static int scgi_reconnect(server
*srv
, handler_ctx
*hctx
) {
1380 * connect was ok, connection was accepted
1381 * but the php accept loop checks after the accept if it should die or not.
1383 * if yes we can only detect it at a write()
1385 * next step is resetting this attemp and setup a connection again
1387 * if we have more then 5 reconnects for the same request, die
1391 * we have a connection but the child died by some other reason
1395 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
1396 fdevent_unregister(srv
->ev
, hctx
->fd
);
1397 fdevent_sched_close(srv
->ev
, hctx
->fd
, 1);
1399 scgi_set_state(srv
, hctx
, FCGI_STATE_INIT
);
1403 if (hctx
->conf
.debug
) {
1404 log_error_write(srv
, __FILE__
, __LINE__
, "sddb",
1407 hctx
->proc
->pid
, hctx
->proc
->socket
);
1411 scgi_proclist_sort_down(srv
, hctx
->host
, hctx
->proc
);
1417 static handler_t
scgi_connection_reset(server
*srv
, connection
*con
, void *p_d
) {
1418 plugin_data
*p
= p_d
;
1419 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1420 if (hctx
) scgi_connection_close(srv
, hctx
);
1422 return HANDLER_GO_ON
;
1426 static int scgi_env_add_scgi(void *venv
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
1430 if (!key
|| !val
) return -1;
1432 len
= key_len
+ val_len
+ 2;
1434 buffer_string_prepare_append(env
, len
);
1436 buffer_append_string_len(env
, key
, key_len
);
1437 buffer_append_string_len(env
, "", 1);
1438 buffer_append_string_len(env
, val
, val_len
);
1439 buffer_append_string_len(env
, "", 1);
1445 #ifdef __LITTLE_ENDIAN__
1446 #define uwsgi_htole16(x) (x)
1447 #else /* __BIG_ENDIAN__ */
1448 #define uwsgi_htole16(x) ((uint16_t) (((x) & 0xff) << 8 | ((x) & 0xff00) >> 8))
1452 static int scgi_env_add_uwsgi(void *venv
, const char *key
, size_t key_len
, const char *val
, size_t val_len
) {
1457 if (!key
|| !val
) return -1;
1458 if (key_len
> USHRT_MAX
|| val_len
> USHRT_MAX
) return -1;
1460 len
= 2 + key_len
+ 2 + val_len
;
1462 buffer_string_prepare_append(env
, len
);
1464 uwlen
= uwsgi_htole16((uint16_t)key_len
);
1465 buffer_append_string_len(env
, (char *)&uwlen
, 2);
1466 buffer_append_string_len(env
, key
, key_len
);
1467 uwlen
= uwsgi_htole16((uint16_t)val_len
);
1468 buffer_append_string_len(env
, (char *)&uwlen
, 2);
1469 buffer_append_string_len(env
, val
, val_len
);
1480 * 1 not connected yet
1483 static int scgi_establish_connection(server
*srv
, handler_ctx
*hctx
) {
1484 struct sockaddr
*scgi_addr
;
1485 struct sockaddr_in scgi_addr_in
;
1486 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1487 struct sockaddr_in6 scgi_addr_in6
;
1489 #ifdef HAVE_SYS_UN_H
1490 struct sockaddr_un scgi_addr_un
;
1494 scgi_extension_host
*host
= hctx
->host
;
1495 scgi_proc
*proc
= hctx
->proc
;
1496 int scgi_fd
= hctx
->fd
;
1498 if (!buffer_string_is_empty(proc
->socket
)) {
1499 #ifdef HAVE_SYS_UN_H
1500 /* use the unix domain socket */
1501 memset(&scgi_addr_un
, 0, sizeof(scgi_addr_un
));
1502 scgi_addr_un
.sun_family
= AF_UNIX
;
1503 if (buffer_string_length(proc
->socket
) + 1 > sizeof(scgi_addr_un
.sun_path
)) {
1504 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
1505 "ERROR: Unix Domain socket filename too long:",
1509 memcpy(scgi_addr_un
.sun_path
, proc
->socket
->ptr
, buffer_string_length(proc
->socket
) + 1);
1512 servlen
= SUN_LEN(&scgi_addr_un
);
1515 servlen
= buffer_string_length(proc
->socket
) + 1 + sizeof(scgi_addr_un
.sun_family
);
1517 scgi_addr
= (struct sockaddr
*) &scgi_addr_un
;
1521 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
1522 } else if (host
->family
== AF_INET6
&& !buffer_string_is_empty(host
->host
)) {
1523 memset(&scgi_addr_in6
, 0, sizeof(scgi_addr_in6
));
1524 scgi_addr_in6
.sin6_family
= AF_INET6
;
1525 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &scgi_addr_in6
.sin6_addr
);
1526 scgi_addr_in6
.sin6_port
= htons(proc
->port
);
1527 servlen
= sizeof(scgi_addr_in6
);
1528 scgi_addr
= (struct sockaddr
*) &scgi_addr_in6
;
1531 memset(&scgi_addr_in
, 0, sizeof(scgi_addr_in
));
1532 scgi_addr_in
.sin_family
= AF_INET
;
1533 if (0 == inet_aton(host
->host
->ptr
, &(scgi_addr_in
.sin_addr
))) {
1534 log_error_write(srv
, __FILE__
, __LINE__
, "sbs",
1535 "converting IP-adress failed for", host
->host
,
1536 "\nBe sure to specify an IP address here");
1540 scgi_addr_in
.sin_port
= htons(proc
->port
);
1541 servlen
= sizeof(scgi_addr_in
);
1543 scgi_addr
= (struct sockaddr
*) &scgi_addr_in
;
1546 if (-1 == connect(scgi_fd
, scgi_addr
, servlen
)) {
1547 if (errno
== EINPROGRESS
||
1548 errno
== EALREADY
||
1550 if (hctx
->conf
.debug
) {
1551 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1552 "connect delayed, will continue later:", scgi_fd
);
1557 log_error_write(srv
, __FILE__
, __LINE__
, "sdsddb",
1558 "connect failed:", scgi_fd
,
1559 strerror(errno
), errno
,
1560 proc
->port
, proc
->socket
);
1562 if (errno
== EAGAIN
) {
1563 /* this is Linux only */
1565 log_error_write(srv
, __FILE__
, __LINE__
, "s",
1566 "If this happend on Linux: You have been run out of local ports. "
1567 "Check the manual, section Performance how to handle this.");
1573 if (hctx
->conf
.debug
> 1) {
1574 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
1575 "connect succeeded: ", scgi_fd
);
1584 static int scgi_create_env(server
*srv
, handler_ctx
*hctx
) {
1587 plugin_data
*p
= hctx
->plugin_data
;
1588 scgi_extension_host
*host
= hctx
->host
;
1590 connection
*con
= hctx
->remote_conn
;
1592 http_cgi_opts opts
= { 0, 0, host
->docroot
, NULL
};
1594 http_cgi_header_append_cb scgi_env_add
= p
->conf
.proto
== LI_PROTOCOL_SCGI
1596 : scgi_env_add_uwsgi
;
1598 buffer_string_prepare_copy(p
->scgi_env
, 1023);
1600 if (0 != http_cgi_headers(srv
, con
, &opts
, scgi_env_add
, p
->scgi_env
)) {
1601 con
->http_status
= 400;
1605 if (p
->conf
.proto
== LI_PROTOCOL_SCGI
) {
1606 scgi_env_add(p
->scgi_env
, CONST_STR_LEN("SCGI"), CONST_STR_LEN("1"));
1608 buffer_append_int(b
, buffer_string_length(p
->scgi_env
));
1609 buffer_append_string_len(b
, CONST_STR_LEN(":"));
1610 buffer_append_string_buffer(b
, p
->scgi_env
);
1611 buffer_append_string_len(b
, CONST_STR_LEN(","));
1612 } else { /* LI_PROTOCOL_UWSGI */
1613 /* http://uwsgi-docs.readthedocs.io/en/latest/Protocol.html */
1614 size_t len
= buffer_string_length(p
->scgi_env
);
1615 uint32_t uwsgi_header
;
1616 if (len
> USHRT_MAX
) {
1617 con
->http_status
= 431; /* Request Header Fields Too Large */
1619 return -1; /* trigger return of HANDLER_FINISHED */
1622 buffer_string_prepare_copy(b
, 4 + len
);
1623 uwsgi_header
= ((uint32_t)uwsgi_htole16((uint16_t)len
)) << 8;
1624 memcpy(b
->ptr
, (char *)&uwsgi_header
, 4);
1625 buffer_commit(b
, 4);
1626 buffer_append_string_buffer(b
, p
->scgi_env
);
1629 hctx
->wb_reqlen
= buffer_string_length(b
);
1630 chunkqueue_append_buffer(hctx
->wb
, b
);
1633 if (con
->request
.content_length
) {
1634 chunkqueue_append_chunkqueue(hctx
->wb
, con
->request_content_queue
);
1635 hctx
->wb_reqlen
+= con
->request
.content_length
;/* (eventual) total request size */
1641 static int scgi_response_parse(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*in
, int eol
) {
1648 buffer_copy_buffer(p
->parse_response
, in
);
1650 for (s
= p
->parse_response
->ptr
;
1651 NULL
!= (ns
= (eol
== EOL_RN
? strstr(s
, "\r\n") : strchr(s
, '\n')));
1652 s
= ns
+ (eol
== EOL_RN
? 2 : 1), line
++) {
1653 const char *key
, *value
;
1660 0 == strncmp(s
, "HTTP/1.", 7)) {
1661 /* non-parsed header ... we parse them anyway */
1667 /* after the space should be a status code for us */
1669 status
= strtol(s
+9, NULL
, 10);
1671 if (status
>= 100 && status
< 1000) {
1672 /* we expected 3 digits got them */
1673 con
->parsed_response
|= HTTP_STATUS
;
1674 con
->http_status
= status
;
1680 if (NULL
== (value
= strchr(s
, ':'))) {
1681 /* we expect: "<key>: <value>\r\n" */
1685 key_len
= value
- key
;
1689 while (*value
== ' ' || *value
== '\t') value
++;
1691 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
1692 ds
= data_response_init();
1694 buffer_copy_string_len(ds
->key
, key
, key_len
);
1695 buffer_copy_string(ds
->value
, value
);
1697 array_insert_unique(con
->response
.headers
, (data_unset
*)ds
);
1701 if (0 == strncasecmp(key
, "Date", key_len
)) {
1702 con
->parsed_response
|= HTTP_DATE
;
1706 if (0 == strncasecmp(key
, "Status", key_len
)) {
1707 int status
= strtol(value
, NULL
, 10);
1708 if (status
>= 100 && status
< 1000) {
1709 con
->http_status
= status
;
1710 con
->parsed_response
|= HTTP_STATUS
;
1712 con
->http_status
= 502;
1717 if (0 == strncasecmp(key
, "Location", key_len
)) {
1718 con
->parsed_response
|= HTTP_LOCATION
;
1722 if (0 == strncasecmp(key
, "Connection", key_len
)) {
1723 con
->response
.keep_alive
= (0 == strcasecmp(value
, "Keep-Alive")) ? 1 : 0;
1724 con
->parsed_response
|= HTTP_CONNECTION
;
1728 if (0 == strncasecmp(key
, "Content-Length", key_len
)) {
1729 con
->response
.content_length
= strtoul(value
, NULL
, 10);
1730 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
1739 /* CGI/1.1 rev 03 - 7.2.1.2 */
1740 if ((con
->parsed_response
& HTTP_LOCATION
) &&
1741 !(con
->parsed_response
& HTTP_STATUS
)) {
1742 con
->http_status
= 302;
1749 static int scgi_demux_response(server
*srv
, handler_ctx
*hctx
) {
1750 plugin_data
*p
= hctx
->plugin_data
;
1751 connection
*con
= hctx
->remote_conn
;
1756 buffer_string_prepare_copy(hctx
->response
, 1023);
1757 if (-1 == (n
= read(hctx
->fd
, hctx
->response
->ptr
, hctx
->response
->size
- 1))) {
1758 if (errno
== EAGAIN
|| errno
== EINTR
) {
1759 /* would block, wait for signal */
1760 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1764 log_error_write(srv
, __FILE__
, __LINE__
, "sdd", strerror(errno
), con
->fd
, hctx
->fd
);
1773 buffer_commit(hctx
->response
, n
);
1775 /* split header from body */
1777 if (con
->file_started
== 0) {
1781 int cp
, eol
= EOL_UNSET
;
1785 buffer_append_string_buffer(hctx
->response_header
, hctx
->response
);
1787 /* nph (non-parsed headers) */
1788 if (0 == strncmp(hctx
->response_header
->ptr
, "HTTP/1.", 7)) in_header
= 1;
1790 /* search for the \r\n\r\n or \n\n in the string */
1791 for (c
= hctx
->response_header
->ptr
, cp
= 0, used
= buffer_string_length(hctx
->response_header
); used
; c
++, cp
++, used
--) {
1792 if (*c
== ':') in_header
= 1;
1793 else if (*c
== '\n') {
1794 if (in_header
== 0) {
1795 /* got a response without a response header */
1802 if (eol
== EOL_UNSET
) eol
= EOL_N
;
1804 if (*(c
+1) == '\n') {
1810 } else if (used
> 1 && *c
== '\r' && *(c
+1) == '\n') {
1811 if (in_header
== 0) {
1812 /* got a response without a response header */
1819 if (eol
== EOL_UNSET
) eol
= EOL_RN
;
1838 /* no header, but a body */
1839 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response_header
)) {
1840 /* error writing to tempfile;
1841 * truncate response or send 500 if nothing sent yet */
1845 size_t blen
= buffer_string_length(hctx
->response_header
) - hlen
;
1847 /* a small hack: terminate after at the second \r */
1848 buffer_string_set_length(hctx
->response_header
, hlen
- 1);
1850 /* parse the response header */
1851 scgi_response_parse(srv
, con
, p
, hctx
->response_header
, eol
);
1853 if (hctx
->host
->xsendfile_allow
) {
1855 if (NULL
!= (ds
= (data_string
*) array_get_element(con
->response
.headers
, "X-Sendfile"))) {
1856 http_response_xsendfile(srv
, con
, ds
->value
, hctx
->host
->xsendfile_docroot
);
1862 if (0 != http_chunk_append_mem(srv
, con
, hctx
->response_header
->ptr
+ hlen
, blen
)) {
1863 /* error writing to tempfile;
1864 * truncate response or send 500 if nothing sent yet */
1870 con
->file_started
= 1;
1872 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
1873 if (buffer_string_length(hctx
->response_header
) > MAX_HTTP_REQUEST_HEADER
) {
1874 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "response headers too large for", con
->uri
.path
);
1875 con
->http_status
= 502; /* Bad Gateway */
1881 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response
)) {
1882 /* error writing to tempfile;
1883 * truncate response or send 500 if nothing sent yet */
1886 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
1887 && chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
1888 if (!con
->is_writable
) {
1889 /*(defer removal of FDEVENT_IN interest since
1890 * connection_state_machine() might be able to send data
1891 * immediately, unless !con->is_writable, where
1892 * connection_state_machine() might not loop back to call
1893 * mod_scgi_handle_subrequest())*/
1894 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1901 log_error_write(srv
, __FILE__
, __LINE__
, "ddss", con
->fd
, hctx
->fd
, connection_get_state(con
->state
), b
->ptr
);
1909 static int scgi_proclist_sort_up(server
*srv
, scgi_extension_host
*host
, scgi_proc
*proc
) {
1914 /* we have been the smallest of the current list
1915 * and we want to insert the node sorted as soon
1925 /* nothing to sort, only one element */
1926 if (host
->first
== proc
&& proc
->next
== NULL
) return 0;
1928 for (p
= proc
; p
->next
&& p
->next
->load
< proc
->load
; p
= p
->next
);
1930 /* no need to move something
1938 if (p
== proc
) return 0;
1940 if (host
->first
== proc
) {
1941 /* we have been the first elememt */
1943 host
->first
= proc
->next
;
1944 host
->first
->prev
= NULL
;
1947 /* disconnect proc */
1949 if (proc
->prev
) proc
->prev
->next
= proc
->next
;
1950 if (proc
->next
) proc
->next
->prev
= proc
->prev
;
1952 /* proc should be right of p */
1954 proc
->next
= p
->next
;
1956 if (p
->next
) p
->next
->prev
= proc
;
1959 for(p
= host
->first
; p
; p
= p
->next
) {
1960 log_error_write(srv
, __FILE__
, __LINE__
, "dd",
1970 int scgi_proclist_sort_down(server
*srv
, scgi_extension_host
*host
, scgi_proc
*proc
) {
1975 /* we have been the smallest of the current list
1976 * and we want to insert the node sorted as soon
1985 * the basic is idea is:
1986 * - the last active scgi process should be still
1987 * in ram and is not swapped out yet
1988 * - processes that are not reused will be killed
1989 * after some time by the trigger-handler
1991 * everything > 0 is hot
1992 * all unused procs are colder the more right they are
1993 * ice-cold processes are propably unused since more
1994 * than 'unused-timeout', are swaped out and won't be
1995 * reused in the next seconds anyway.
1999 /* nothing to sort, only one element */
2000 if (host
->first
== proc
&& proc
->next
== NULL
) return 0;
2002 for (p
= host
->first
; p
!= proc
&& p
->load
< proc
->load
; p
= p
->next
);
2005 /* no need to move something
2013 if (p
== proc
) return 0;
2015 /* we have to move left. If we are already the first element
2017 if (host
->first
== proc
) return 0;
2020 if (proc
->prev
) proc
->prev
->next
= proc
->next
;
2021 if (proc
->next
) proc
->next
->prev
= proc
->prev
;
2023 /* proc should be left of p */
2025 proc
->prev
= p
->prev
;
2026 if (p
->prev
) p
->prev
->next
= proc
;
2029 if (proc
->prev
== NULL
) host
->first
= proc
;
2031 for(p
= host
->first
; p
; p
= p
->next
) {
2032 log_error_write(srv
, __FILE__
, __LINE__
, "dd",
2042 static int scgi_restart_dead_procs(server
*srv
, plugin_data
*p
, scgi_extension_host
*host
) {
2045 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
2046 if (p
->conf
.debug
) {
2047 log_error_write(srv
, __FILE__
, __LINE__
, "sbdbdddd",
2049 host
->host
, proc
->port
,
2057 if (0 == proc
->is_local
) {
2059 * external servers might get disabled
2061 * enable the server again, perhaps it is back again
2064 if ((proc
->state
== PROC_STATE_DISABLED
) &&
2065 (srv
->cur_ts
- proc
->disable_ts
> host
->disable_time
)) {
2066 proc
->state
= PROC_STATE_RUNNING
;
2067 host
->active_procs
++;
2069 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb",
2070 "fcgi-server re-enabled:",
2071 host
->host
, host
->port
,
2075 /* the child should not terminate at all */
2078 if (proc
->state
== PROC_STATE_DIED_WAIT_FOR_PID
) {
2079 switch(waitpid(proc
->pid
, &status
, WNOHANG
)) {
2081 /* child is still alive */
2086 if (WIFEXITED(status
)) {
2088 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
2089 "child exited, pid:", proc
->pid
,
2090 "status:", WEXITSTATUS(status
));
2092 } else if (WIFSIGNALED(status
)) {
2093 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2097 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2098 "child died somehow:",
2102 proc
->state
= PROC_STATE_DIED
;
2108 * local servers might died, but we restart them
2111 if (proc
->state
== PROC_STATE_DIED
&&
2113 /* restart the child */
2115 if (p
->conf
.debug
) {
2116 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsdsd",
2117 "--- scgi spawning",
2118 "\n\tport:", host
->port
,
2119 "\n\tsocket", host
->unixsocket
,
2120 "\n\tcurrent:", 1, "/", host
->min_procs
);
2123 if (scgi_spawn_connection(srv
, p
, host
, proc
)) {
2124 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2125 "ERROR: spawning fcgi failed.");
2126 return HANDLER_ERROR
;
2129 scgi_proclist_sort_down(srv
, host
, proc
);
2138 static handler_t
scgi_write_request(server
*srv
, handler_ctx
*hctx
) {
2139 scgi_extension_host
*host
= hctx
->host
;
2140 connection
*con
= hctx
->remote_conn
;
2146 log_error_write(srv
, __FILE__
, __LINE__
, "s", "fatal error: host = NULL");
2147 return HANDLER_ERROR
;
2149 if (((buffer_string_is_empty(host
->host
) || !host
->port
) && buffer_string_is_empty(host
->unixsocket
))) {
2150 log_error_write(srv
, __FILE__
, __LINE__
, "sxddd",
2153 buffer_string_length(host
->host
),
2155 buffer_string_length(host
->unixsocket
));
2156 return HANDLER_ERROR
;
2160 switch(hctx
->state
) {
2161 case FCGI_STATE_INIT
:
2162 if (-1 == (hctx
->fd
= fdevent_socket_nb_cloexec(host
->family
, SOCK_STREAM
, 0))) {
2163 if (errno
== EMFILE
||
2165 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2166 "wait for fd at connection:", con
->fd
);
2168 return HANDLER_WAIT_FOR_FD
;
2171 log_error_write(srv
, __FILE__
, __LINE__
, "ssdd",
2172 "socket failed:", strerror(errno
), srv
->cur_fds
, srv
->max_fds
);
2173 return HANDLER_ERROR
;
2179 fdevent_register(srv
->ev
, hctx
->fd
, scgi_handle_fdevent
, hctx
);
2181 if (-1 == fdevent_fcntl_set(srv
->ev
, hctx
->fd
)) {
2182 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2183 "fcntl failed: ", strerror(errno
));
2184 return HANDLER_ERROR
;
2188 case FCGI_STATE_CONNECT
:
2189 if (hctx
->state
== FCGI_STATE_INIT
) {
2190 for (hctx
->proc
= hctx
->host
->first
;
2191 hctx
->proc
&& hctx
->proc
->state
!= PROC_STATE_RUNNING
;
2192 hctx
->proc
= hctx
->proc
->next
);
2194 /* all childs are dead */
2195 if (hctx
->proc
== NULL
) {
2198 return HANDLER_ERROR
;
2201 if (hctx
->proc
->is_local
) {
2202 hctx
->pid
= hctx
->proc
->pid
;
2205 switch (scgi_establish_connection(srv
, hctx
)) {
2207 scgi_set_state(srv
, hctx
, FCGI_STATE_CONNECT
);
2209 /* connection is in progress, wait for an event and call getsockopt() below */
2211 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2213 return HANDLER_WAIT_FOR_EVENT
;
2215 /* if ECONNREFUSED; choose another connection */
2218 return HANDLER_ERROR
;
2220 /* everything is ok, go on */
2227 socklen_t socket_error_len
= sizeof(socket_error
);
2229 /* try to finish the connect() */
2230 if (0 != getsockopt(hctx
->fd
, SOL_SOCKET
, SO_ERROR
, &socket_error
, &socket_error_len
)) {
2231 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2232 "getsockopt failed:", strerror(errno
));
2234 return HANDLER_ERROR
;
2236 if (socket_error
!= 0) {
2237 if (!hctx
->proc
->is_local
|| hctx
->conf
.debug
) {
2238 /* local procs get restarted */
2240 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
2241 "establishing connection failed:", strerror(socket_error
),
2242 "port:", hctx
->proc
->port
);
2245 return HANDLER_ERROR
;
2249 /* ok, we have the connection */
2252 hctx
->proc
->last_used
= srv
->cur_ts
;
2255 if (hctx
->conf
.debug
) {
2256 log_error_write(srv
, __FILE__
, __LINE__
, "sddbdd",
2265 /* move the proc-list entry down the list */
2266 scgi_proclist_sort_up(srv
, hctx
->host
, hctx
->proc
);
2268 scgi_set_state(srv
, hctx
, FCGI_STATE_PREPARE_WRITE
);
2270 case FCGI_STATE_PREPARE_WRITE
:
2271 if (0 != scgi_create_env(srv
, hctx
)) {
2272 return HANDLER_FINISHED
;
2275 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
2276 scgi_set_state(srv
, hctx
, FCGI_STATE_WRITE
);
2279 case FCGI_STATE_WRITE
:
2280 ret
= srv
->network_backend_write(srv
, con
, hctx
->fd
, hctx
->wb
, MAX_WRITE_LIMIT
);
2282 chunkqueue_remove_finished_chunks(hctx
->wb
);
2285 if (errno
== ENOTCONN
|| ret
== -2) {
2286 /* the connection got dropped after accept()
2288 * this is most of the time a PHP which dies
2289 * after PHP_FCGI_MAX_REQUESTS
2292 if (hctx
->wb
->bytes_out
== 0 &&
2293 hctx
->reconnects
< 5) {
2294 usleep(10000); /* take away the load of the webserver
2295 * to let the php a chance to restart
2298 scgi_reconnect(srv
, hctx
);
2300 return HANDLER_COMEBACK
;
2303 /* not reconnected ... why
2305 * far@#lighttpd report this for FreeBSD
2309 log_error_write(srv
, __FILE__
, __LINE__
, "ssosd",
2310 "connection was dropped after accept(). reconnect() denied:",
2311 "write-offset:", hctx
->wb
->bytes_out
,
2312 "reconnect attempts:", hctx
->reconnects
);
2314 return HANDLER_ERROR
;
2316 /* -1 == ret => error on our side */
2317 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
2318 "write failed:", strerror(errno
), errno
);
2320 return HANDLER_ERROR
;
2324 if (hctx
->wb
->bytes_out
== hctx
->wb_reqlen
) {
2325 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2326 scgi_set_state(srv
, hctx
, FCGI_STATE_READ
);
2328 off_t wblen
= hctx
->wb
->bytes_in
- hctx
->wb
->bytes_out
;
2329 if (hctx
->wb
->bytes_in
< hctx
->wb_reqlen
&& wblen
< 65536 - 16384) {
2330 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
2331 if (!(con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_POLLIN
)) {
2332 con
->conf
.stream_request_body
|= FDEVENT_STREAM_REQUEST_POLLIN
;
2333 con
->is_readable
= 1; /* trigger optimistic read from client */
2337 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2339 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
2343 return HANDLER_WAIT_FOR_EVENT
;
2344 case FCGI_STATE_READ
:
2345 /* waiting for a response */
2346 return HANDLER_WAIT_FOR_EVENT
;
2348 log_error_write(srv
, __FILE__
, __LINE__
, "s", "(debug) unknown state");
2349 return HANDLER_ERROR
;
2353 static handler_t
scgi_send_request(server
*srv
, handler_ctx
*hctx
) {
2354 /* ok, create the request */
2355 handler_t rc
= scgi_write_request(srv
, hctx
);
2356 if (HANDLER_ERROR
!= rc
) {
2359 scgi_proc
*proc
= hctx
->proc
;
2360 scgi_extension_host
*host
= hctx
->host
;
2361 plugin_data
*p
= hctx
->plugin_data
;
2362 connection
*con
= hctx
->remote_conn
;
2365 0 == proc
->is_local
&&
2366 proc
->state
!= PROC_STATE_DISABLED
) {
2367 /* only disable remote servers as we don't manage them*/
2369 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb", "fcgi-server disabled:",
2374 /* disable this server */
2375 proc
->disable_ts
= srv
->cur_ts
;
2376 proc
->state
= PROC_STATE_DISABLED
;
2377 host
->active_procs
--;
2380 if (hctx
->state
== FCGI_STATE_INIT
||
2381 hctx
->state
== FCGI_STATE_CONNECT
) {
2382 /* connect() or getsockopt() failed,
2383 * restart the request-handling
2385 if (proc
&& proc
->is_local
) {
2387 if (hctx
->conf
.debug
) {
2388 log_error_write(srv
, __FILE__
, __LINE__
, "sbdb", "connect() to scgi failed, restarting the request-handling:",
2395 * several hctx might reference the same proc
2397 * Only one of them should mark the proc as dead all the other
2398 * ones should just take a new one.
2400 * If a new proc was started with the old struct this might lead
2401 * the mark a perfect proc as dead otherwise
2404 if (proc
->state
== PROC_STATE_RUNNING
&&
2405 hctx
->pid
== proc
->pid
) {
2406 proc
->state
= PROC_STATE_DIED_WAIT_FOR_PID
;
2409 scgi_restart_dead_procs(srv
, p
, host
);
2411 con
->mode
= DIRECT
;/*(avoid changing con->state, con->http_status)*/
2412 scgi_connection_close(srv
, hctx
);
2415 return HANDLER_COMEBACK
;
2417 scgi_connection_close(srv
, hctx
);
2418 con
->http_status
= 503;
2420 return HANDLER_FINISHED
;
2426 static handler_t
scgi_recv_response(server
*srv
, handler_ctx
*hctx
);
2429 SUBREQUEST_FUNC(mod_scgi_handle_subrequest
) {
2430 plugin_data
*p
= p_d
;
2432 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
2434 if (NULL
== hctx
) return HANDLER_GO_ON
;
2437 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
2439 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
2440 && con
->file_started
) {
2441 if (chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
2442 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
2443 } else if (!(fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_IN
)) {
2444 /* optimistic read from backend, which might re-enable FDEVENT_IN */
2445 handler_t rc
= scgi_recv_response(srv
, hctx
); /*(might invalidate hctx)*/
2446 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
2450 if (0 == hctx
->wb
->bytes_in
2451 ? con
->state
== CON_STATE_READ_POST
2452 : hctx
->wb
->bytes_in
< hctx
->wb_reqlen
) {
2453 /*(64k - 4k to attempt to avoid temporary files
2454 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
2455 if (hctx
->wb
->bytes_in
- hctx
->wb
->bytes_out
> 65536 - 4096
2456 && (con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_BUFMIN
)){
2457 con
->conf
.stream_request_body
&= ~FDEVENT_STREAM_REQUEST_POLLIN
;
2458 if (0 != hctx
->wb
->bytes_in
) return HANDLER_WAIT_FOR_EVENT
;
2460 handler_t r
= connection_handle_read_post_state(srv
, con
);
2461 chunkqueue
*req_cq
= con
->request_content_queue
;
2462 if (0 != hctx
->wb
->bytes_in
&& !chunkqueue_is_empty(req_cq
)) {
2463 chunkqueue_append_chunkqueue(hctx
->wb
, req_cq
);
2464 if (fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_OUT
) {
2465 return (r
== HANDLER_GO_ON
) ? HANDLER_WAIT_FOR_EVENT
: r
;
2468 if (r
!= HANDLER_GO_ON
) return r
;
2472 return ((0 == hctx
->wb
->bytes_in
|| !chunkqueue_is_empty(hctx
->wb
))
2473 && hctx
->state
!= FCGI_STATE_CONNECT
)
2474 ? scgi_send_request(srv
, hctx
)
2475 : HANDLER_WAIT_FOR_EVENT
;
2479 static handler_t
scgi_recv_response(server
*srv
, handler_ctx
*hctx
) {
2481 switch (scgi_demux_response(srv
, hctx
)) {
2486 scgi_connection_close(srv
, hctx
);
2488 return HANDLER_FINISHED
;
2490 connection
*con
= hctx
->remote_conn
;
2491 plugin_data
*p
= hctx
->plugin_data
;
2493 scgi_proc
*proc
= hctx
->proc
;
2494 scgi_extension_host
*host
= hctx
->host
;
2496 if (proc
->pid
&& proc
->state
!= PROC_STATE_DIED
) {
2499 /* only fetch the zombie if it is not already done */
2501 switch(waitpid(proc
->pid
, &status
, WNOHANG
)) {
2503 /* child is still alive */
2508 /* the child should not terminate at all */
2509 if (WIFEXITED(status
)) {
2510 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
2511 "child exited, pid:", proc
->pid
,
2512 "status:", WEXITSTATUS(status
));
2513 } else if (WIFSIGNALED(status
)) {
2514 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2518 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
2519 "child died somehow:",
2523 if (hctx
->conf
.debug
) {
2524 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsbsdsd",
2525 "--- scgi spawning",
2526 "\n\tport:", host
->port
,
2527 "\n\tsocket", host
->unixsocket
,
2528 "\n\tcurrent:", 1, "/", host
->min_procs
);
2531 if (scgi_spawn_connection(srv
, p
, host
, proc
)) {
2533 proc
->state
= PROC_STATE_DIED
;
2535 scgi_proclist_sort_down(srv
, host
, proc
);
2542 if (con
->file_started
== 0) {
2543 /* nothing has been send out yet, try to use another child */
2545 if (hctx
->wb
->bytes_out
== 0 &&
2546 hctx
->reconnects
< 5) {
2548 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsd",
2549 "response not sent, request not sent, reconnection.",
2550 "connection-fd:", con
->fd
,
2551 "fcgi-fd:", hctx
->fd
);
2553 scgi_reconnect(srv
, hctx
);
2555 return HANDLER_COMEBACK
;
2558 log_error_write(srv
, __FILE__
, __LINE__
, "sosdsd",
2559 "response not sent, request sent:", hctx
->wb
->bytes_out
,
2560 "connection-fd:", con
->fd
,
2561 "fcgi-fd:", hctx
->fd
);
2563 log_error_write(srv
, __FILE__
, __LINE__
, "ssdsd",
2564 "response already sent out, termination connection",
2565 "connection-fd:", con
->fd
,
2566 "fcgi-fd:", hctx
->fd
);
2569 http_response_backend_error(srv
, con
);
2570 scgi_connection_close(srv
, hctx
);
2571 return HANDLER_FINISHED
;
2575 return HANDLER_GO_ON
;
2579 static handler_t
scgi_handle_fdevent(server
*srv
, void *ctx
, int revents
) {
2580 handler_ctx
*hctx
= ctx
;
2581 connection
*con
= hctx
->remote_conn
;
2583 joblist_append(srv
, con
);
2585 if (revents
& FDEVENT_IN
) {
2586 handler_t rc
= scgi_recv_response(srv
, hctx
);/*(might invalidate hctx)*/
2587 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
2590 if (revents
& FDEVENT_OUT
) {
2591 return scgi_send_request(srv
, hctx
); /*(might invalidate hctx)*/
2594 /* perhaps this issue is already handled */
2595 if (revents
& FDEVENT_HUP
) {
2596 if (hctx
->state
== FCGI_STATE_CONNECT
) {
2597 /* getoptsock will catch this one (right ?)
2599 * if we are in connect we might get a EINPROGRESS
2600 * in the first call and a FDEVENT_HUP in the
2603 * FIXME: as it is a bit ugly.
2606 scgi_send_request(srv
, hctx
);
2607 } else if (con
->file_started
) {
2608 /* drain any remaining data from kernel pipe buffers
2609 * even if (con->conf.stream_response_body
2610 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
2611 * since event loop will spin on fd FDEVENT_HUP event
2612 * until unregistered. */
2615 rc
= scgi_recv_response(srv
,hctx
);/*(might invalidate hctx)*/
2616 } while (rc
== HANDLER_GO_ON
); /*(unless HANDLER_GO_ON)*/
2617 return rc
; /* HANDLER_FINISHED or HANDLER_ERROR */
2619 scgi_extension_host
*host
= hctx
->host
;
2620 log_error_write(srv
, __FILE__
, __LINE__
, "sbSBSDSd",
2621 "error: unexpected close of scgi connection for",
2623 "(no scgi process on host: ",
2630 scgi_connection_close(srv
, hctx
);
2632 } else if (revents
& FDEVENT_ERR
) {
2633 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2634 "fcgi: got a FDEVENT_ERR. Don't know why.");
2636 http_response_backend_error(srv
, con
);
2637 scgi_connection_close(srv
, hctx
);
2640 return HANDLER_FINISHED
;
2644 static int scgi_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
2646 plugin_config
*s
= p
->config_storage
[0];
2652 /* skip the first, the global context */
2653 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
2654 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
2655 s
= p
->config_storage
[i
];
2657 /* condition didn't match */
2658 if (!config_check_cond(srv
, con
, dc
)) continue;
2661 for (j
= 0; j
< dc
->value
->used
; j
++) {
2662 data_unset
*du
= dc
->value
->data
[j
];
2664 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("scgi.server"))) {
2666 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("scgi.protocol"))) {
2668 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("scgi.debug"))) {
2679 static handler_t
scgi_check_extension(server
*srv
, connection
*con
, void *p_d
, int uri_path_handler
) {
2680 plugin_data
*p
= p_d
;
2685 scgi_extension
*extension
= NULL
;
2686 scgi_extension_host
*host
= NULL
;
2688 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
2690 /* Possibly, we processed already this request */
2691 if (con
->file_started
== 1) return HANDLER_GO_ON
;
2693 fn
= uri_path_handler
? con
->uri
.path
: con
->physical
.path
;
2695 if (buffer_string_is_empty(fn
)) return HANDLER_GO_ON
;
2697 s_len
= buffer_string_length(fn
);
2699 scgi_patch_connection(srv
, con
, p
);
2701 /* check if extension matches */
2702 for (k
= 0; k
< p
->conf
.exts
->used
; k
++) {
2704 scgi_extension
*ext
= p
->conf
.exts
->exts
[k
];
2706 if (buffer_is_empty(ext
->key
)) continue;
2708 ct_len
= buffer_string_length(ext
->key
);
2710 if (s_len
< ct_len
) continue;
2712 /* check extension in the form "/scgi_pattern" */
2713 if (*(ext
->key
->ptr
) == '/') {
2714 if (strncmp(fn
->ptr
, ext
->key
->ptr
, ct_len
) == 0) {
2718 } else if (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ext
->key
->ptr
, ct_len
)) {
2719 /* check extension in the form ".fcg" */
2725 /* extension doesn't match */
2726 if (NULL
== extension
) {
2727 return HANDLER_GO_ON
;
2730 /* get best server */
2731 for (k
= 0; k
< extension
->used
; k
++) {
2732 scgi_extension_host
*h
= extension
->hosts
[k
];
2734 /* we should have at least one proc that can do something */
2735 if (h
->active_procs
== 0) {
2739 if (used
== -1 || h
->load
< used
) {
2747 /* sorry, we don't have a server alive for this ext */
2748 con
->http_status
= 500;
2751 /* only send the 'no handler' once */
2752 if (!extension
->note_is_sent
) {
2753 extension
->note_is_sent
= 1;
2755 log_error_write(srv
, __FILE__
, __LINE__
, "sbsbs",
2756 "all handlers for ", con
->uri
.path
,
2757 "on", extension
->key
,
2761 return HANDLER_FINISHED
;
2764 /* a note about no handler is not sent yet */
2765 extension
->note_is_sent
= 0;
2767 /* SCGI requires that Content-Length be set.
2768 * Send 411 Length Required if Content-Length missing.
2769 * (Alternatively, collect full request body before proceeding
2770 * in mod_scgi_handle_subrequest()) */
2771 if (0 == con
->request
.content_length
2772 && array_get_element(con
->request
.headers
, "Transfer-Encoding")) {
2773 con
->keep_alive
= 0;
2774 con
->http_status
= 411; /* Length Required */
2776 return HANDLER_FINISHED
;
2780 * if check-local is disabled, use the uri.path handler
2784 /* init handler-context */
2785 if (uri_path_handler
) {
2786 if (host
->check_local
== 0) {
2790 hctx
= handler_ctx_init();
2792 hctx
->remote_conn
= con
;
2793 hctx
->plugin_data
= p
;
2797 hctx
->conf
.exts
= p
->conf
.exts
;
2798 hctx
->conf
.debug
= p
->conf
.debug
;
2800 con
->plugin_ctx
[p
->id
] = hctx
;
2806 if (con
->conf
.log_request_handling
) {
2807 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2808 "handling it in mod_scgi");
2811 /* the prefix is the SCRIPT_NAME,
2812 * everything from start to the next slash
2813 * this is important for check-local = "disable"
2815 * if prefix = /admin.fcgi
2817 * /admin.fcgi/foo/bar
2819 * SCRIPT_NAME = /admin.fcgi
2820 * PATH_INFO = /foo/bar
2822 * if prefix = /fcgi-bin/
2826 * SCRIPT_NAME = /fcgi-bin/foo
2831 /* the rewrite is only done for /prefix/? matches */
2832 if (host
->fix_root_path_name
&& extension
->key
->ptr
[0] == '/' && extension
->key
->ptr
[1] == '\0') {
2833 buffer_copy_string(con
->request
.pathinfo
, con
->uri
.path
->ptr
);
2834 buffer_string_set_length(con
->uri
.path
, 0);
2835 } else if (extension
->key
->ptr
[0] == '/' &&
2836 buffer_string_length(con
->uri
.path
) > buffer_string_length(extension
->key
) &&
2837 NULL
!= (pathinfo
= strchr(con
->uri
.path
->ptr
+ buffer_string_length(extension
->key
), '/'))) {
2838 /* rewrite uri.path and pathinfo */
2840 buffer_copy_string(con
->request
.pathinfo
, pathinfo
);
2841 buffer_string_set_length(con
->uri
.path
, buffer_string_length(con
->uri
.path
) - buffer_string_length(con
->request
.pathinfo
));
2846 hctx
= handler_ctx_init();
2848 hctx
->remote_conn
= con
;
2849 hctx
->plugin_data
= p
;
2853 hctx
->conf
.exts
= p
->conf
.exts
;
2854 hctx
->conf
.debug
= p
->conf
.debug
;
2856 con
->plugin_ctx
[p
->id
] = hctx
;
2862 if (con
->conf
.log_request_handling
) {
2863 log_error_write(srv
, __FILE__
, __LINE__
, "s", "handling it in mod_scgi");
2867 return HANDLER_GO_ON
;
2870 /* uri-path handler */
2871 static handler_t
scgi_check_extension_1(server
*srv
, connection
*con
, void *p_d
) {
2872 return scgi_check_extension(srv
, con
, p_d
, 1);
2875 /* start request handler */
2876 static handler_t
scgi_check_extension_2(server
*srv
, connection
*con
, void *p_d
) {
2877 return scgi_check_extension(srv
, con
, p_d
, 0);
2881 TRIGGER_FUNC(mod_scgi_handle_trigger
) {
2882 plugin_data
*p
= p_d
;
2886 /* perhaps we should kill a connect attempt after 10-15 seconds
2888 * currently we wait for the TCP timeout which is on Linux 180 seconds
2894 /* check all childs if they are still up */
2896 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
2897 plugin_config
*conf
;
2900 conf
= p
->config_storage
[i
];
2904 for (j
= 0; j
< exts
->used
; j
++) {
2909 for (n
= 0; n
< ex
->used
; n
++) {
2912 unsigned long sum_load
= 0;
2913 scgi_extension_host
*host
;
2915 host
= ex
->hosts
[n
];
2917 scgi_restart_dead_procs(srv
, p
, host
);
2919 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
2920 sum_load
+= proc
->load
;
2923 if (host
->num_procs
&&
2924 host
->num_procs
< host
->max_procs
&&
2925 (sum_load
/ host
->num_procs
) > host
->max_load_per_proc
) {
2926 /* overload, spawn new child */
2927 scgi_proc
*fp
= NULL
;
2929 if (p
->conf
.debug
) {
2930 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2931 "overload detected, spawning a new child");
2934 for (fp
= host
->unused_procs
; fp
&& fp
->pid
!= 0; fp
= fp
->next
);
2937 if (fp
== host
->unused_procs
) host
->unused_procs
= fp
->next
;
2939 if (fp
->next
) fp
->next
->prev
= NULL
;
2943 fp
= scgi_process_init();
2944 fp
->id
= host
->max_id
++;
2949 if (buffer_string_is_empty(host
->unixsocket
)) {
2950 fp
->port
= host
->port
+ fp
->id
;
2952 buffer_copy_buffer(fp
->socket
, host
->unixsocket
);
2953 buffer_append_string_len(fp
->socket
, CONST_STR_LEN("-"));
2954 buffer_append_int(fp
->socket
, fp
->id
);
2957 if (scgi_spawn_connection(srv
, p
, host
, fp
)) {
2958 log_error_write(srv
, __FILE__
, __LINE__
, "s",
2959 "ERROR: spawning fcgi failed.");
2960 scgi_process_free(fp
);
2961 return HANDLER_ERROR
;
2965 fp
->next
= host
->first
;
2967 host
->first
->prev
= fp
;
2972 for (proc
= host
->first
; proc
; proc
= proc
->next
) {
2973 if (proc
->load
!= 0) break;
2974 if (host
->num_procs
<= host
->min_procs
) break;
2975 if (proc
->pid
== 0) continue;
2977 if (srv
->cur_ts
- proc
->last_used
> host
->idle_timeout
) {
2978 /* a proc is idling for a long time now,
2981 if (p
->conf
.debug
) {
2982 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsd",
2983 "idle-timeout reached, terminating child:",
2984 "socket:", proc
->socket
,
2989 if (proc
->next
) proc
->next
->prev
= proc
->prev
;
2990 if (proc
->prev
) proc
->prev
->next
= proc
->next
;
2992 if (proc
->prev
== NULL
) host
->first
= proc
->next
;
2995 proc
->next
= host
->unused_procs
;
2997 if (host
->unused_procs
) host
->unused_procs
->prev
= proc
;
2998 host
->unused_procs
= proc
;
3000 kill(proc
->pid
, SIGTERM
);
3002 proc
->state
= PROC_STATE_KILLED
;
3004 log_error_write(srv
, __FILE__
, __LINE__
, "ssbsd",
3006 "socket:", proc
->socket
,
3011 /* proc is now in unused, let the next second handle the next process */
3016 for (proc
= host
->unused_procs
; proc
; proc
= proc
->next
) {
3019 if (proc
->pid
== 0) continue;
3021 switch (waitpid(proc
->pid
, &status
, WNOHANG
)) {
3023 /* child still running after timeout, good */
3026 if (errno
!= EINTR
) {
3027 /* no PID found ? should never happen */
3028 log_error_write(srv
, __FILE__
, __LINE__
, "sddss",
3029 "pid ", proc
->pid
, proc
->state
,
3030 "not found:", strerror(errno
));
3033 if (errno
== ECHILD
) {
3034 /* someone else has cleaned up for us */
3036 proc
->state
= PROC_STATE_UNSET
;
3042 /* the child should not terminate at all */
3043 if (WIFEXITED(status
)) {
3044 if (proc
->state
!= PROC_STATE_KILLED
) {
3045 log_error_write(srv
, __FILE__
, __LINE__
, "sdb",
3047 WEXITSTATUS(status
), proc
->socket
);
3049 } else if (WIFSIGNALED(status
)) {
3050 if (WTERMSIG(status
) != SIGTERM
) {
3051 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3056 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
3057 "child died somehow:",
3061 proc
->state
= PROC_STATE_UNSET
;
3069 return HANDLER_GO_ON
;
3073 int mod_scgi_plugin_init(plugin
*p
);
3074 int mod_scgi_plugin_init(plugin
*p
) {
3075 p
->version
= LIGHTTPD_VERSION_ID
;
3076 p
->name
= buffer_init_string("scgi");
3078 p
->init
= mod_scgi_init
;
3079 p
->cleanup
= mod_scgi_free
;
3080 p
->set_defaults
= mod_scgi_set_defaults
;
3081 p
->connection_reset
= scgi_connection_reset
;
3082 p
->handle_connection_close
= scgi_connection_reset
;
3083 p
->handle_uri_clean
= scgi_check_extension_1
;
3084 p
->handle_subrequest_start
= scgi_check_extension_2
;
3085 p
->handle_subrequest
= mod_scgi_handle_subrequest
;
3086 p
->handle_trigger
= mod_scgi_handle_trigger
;