--- parser3/src/targets/cgi/parser3.C 2026/10/09 21:38:52 1.377 +++ parser3/src/targets/cgi/parser3.C 2026/10/27 23:05:00 1.387 @@ -5,7 +5,7 @@ Authors: Konstantin Morshnev , Alexandr Petrosian */ -volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.377 2026/10/09 21:38:52 moko Exp $"; +volatile const char * IDENT_PARSER3_C="$Id: parser3.C,v 1.387 2026/10/27 23:05:00 moko Exp $"; #include "pa_config_includes.h" @@ -18,6 +18,8 @@ volatile const char * IDENT_PARSER3_C="$ #include "pa_vconsole.h" #include "pa_sapi_info.h" #include "pa_code_cache.h" +#include "pa_websocket.h" +#include "pa_os.h" #ifdef _MSC_VER #include @@ -70,7 +72,7 @@ const char *parser3_log_filespec() { // const char* slog=log_filespec; if(!slog) { - static char log_spec[MAX_STRING + 12 /* '/parser3.log' */]; + static THREAD_LOCAL char log_spec[MAX_STRING + 12 /* '/parser3.log' */]; pa_strncpy(log_spec, filespec_4log, MAX_STRING); if(char* log_dir_pos=strrchr(log_spec, '/')){ @@ -86,45 +88,54 @@ const char *parser3_log_filespec() { // // SAPI -static void pa_log(const char* fmt, va_list args) { - // use no memory [so that we could log out-of-memory error] - const char* slog=parser3_log_filespec(); - FILE *f=strcmp(slog,"-") ? fopen(slog, "at") : stderr; - - // fallback to stderr - if(!f) - f=stderr; +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#endif +#ifndef STDERR_FILENO +#define STDERR_FILENO 2 +#endif - setbuf(f, 0); // stderr stream is unbuffered by default, but still... +static void pa_log(const char* stamp, const char* fmt, va_list args) { + // use no memory (to log out-of-memory error): the line is built on the stack and written at once + char line[64 + MAX_LOG_STRING + 1024 + 32 + 100]; // prefix + message + request info suffix: uri + method and the rest of it + size_t size=0; // prefix - time_t t=time(0); - if(const char* stamp=ctime(&t)) { // never saw that - if(size_t len=strlen(stamp)) // saw once stamp being ="" - fprintf(f, "[%.*s] [%u] ", (int)len-1, stamp, (unsigned int)pa_get_thread_id() ); - } + if(!stamp) + stamp=pa_ctime(time(0)); + if(size_t len=strlen(stamp)) // saw once stamp being ="" + size=pa_snprintf(line, 64, "[%.*s] [%u] ", (int)len-1 /* exclude \n */, stamp, (unsigned int)pa_get_thread_id() ); // message - char buf[MAX_LOG_STRING]; - size_t size=vsnprintf(buf, MAX_LOG_STRING, fmt, args); - size=remove_crlf(buf, buf+size); - fwrite(buf, size, 1, f); - - if(request_info_4log && request_info_4log->method) { - fprintf(f, " [uri=%s, method=%s, cl=%lu]\n", request_info_4log->uri ? request_info_4log->uri : "", request_info_4log->method, (unsigned long)request_info_4log->content_length); - } else - fputs(" [no request info]\n", f); + size+=remove_crlf(line+size, line+size+pa_vsnprintf(line+size, MAX_LOG_STRING, fmt, args)); - if(f!=stderr) - fclose(f); + // suffix + if(request_info_4log && request_info_4log->method) + size+=pa_snprintf(line+size, sizeof(line)-size, " [uri=%.1024s, method=%.32s, cl=%lu]\n", request_info_4log->uri ? request_info_4log->uri : "", request_info_4log->method, (unsigned long)request_info_4log->content_length); else - fflush(f); + size+=pa_snprintf(line+size, sizeof(line)-size, " [no request info]\n"); + + const char* slog=parser3_log_filespec(); + int fd=strcmp(slog,"-") ? pa_open(slog, O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC, 0666) : -1; + if(fd<0) + fd=STDERR_FILENO; // fallback to stderr + PA_UNUSED int ignore_result=write(fd, line, (unsigned int)size); + if(fd!=STDERR_FILENO) + close(fd); } void pa_log(const char* fmt, ...) { va_list args; va_start(args,fmt); - pa_log(fmt, args); + pa_log(0, fmt, args); + va_end(args); +} + +// for signal handlers, where pa_ctime can't be called: the stamp is prepared in advance +static void pa_log_at(const char* stamp, const char* fmt, ...) { + va_list args; + va_start(args,fmt); + pa_log(stamp, fmt, args); va_end(args); } @@ -132,7 +143,7 @@ void pa_log(const char* fmt, ...) { void SAPI::log(SAPI_Info&, const char* fmt, ...) { va_list args; va_start(args,fmt); - pa_log(fmt, args); + pa_log(0, fmt, args); va_end(args); } @@ -141,13 +152,13 @@ void SAPI::die(const char* fmt, ...) { // logging first, first vsnprintf va_start(args,fmt); - pa_log(fmt, args); + pa_log(0, fmt, args); va_end(args); // inform user, second vsnprintf va_start(args, fmt); char message[MAX_STRING]; - vsnprintf(message, MAX_STRING, fmt, args); + pa_vsnprintf(message, MAX_STRING, fmt, args); SAPI::send_error(sapi_info_4log ? *sapi_info_4log : *sapi_info, message); exit(1); @@ -198,23 +209,9 @@ static const char* full_disk_path(const return pa_strcat(backslashes_to_slashes(getcwd(cwd, MAX_STRING) ? cwd : ""), "/", path); } -static void log_signal(const char* signal_name) { - pa_log("%s received %s processing request", signal_name, request ? "while" : "before or after"); -} - #ifdef SIGPIPE -#define SIGPIPE_NAME "SIGPIPE" -static const String sigpipe_name(SIGPIPE_NAME); -static void SIGPIPE_handler(int /*sig*/){ - Value* sigpipe=0; - if(request) - sigpipe=request->main_class.get_element(sigpipe_name); - if(sigpipe && sigpipe->as_bool()) - log_signal(SIGPIPE_NAME); - - if(request) - request->set_skip(Request::SKIP_INTERRUPTED); -} +// write to a closed socket or pipe fails with EPIPE. Not SIG_IGN, as the ignored signal is inherited by the executed programs +static void SIGPIPE_handler(int /*sig*/){} #endif // requires pa_thread_request() in entry_exists() under Windows @@ -299,6 +296,74 @@ public: } }; +#define RESPONSE_TIMEOUT_GRACE 1 // seconds to finish after the interruption, then the process is killed + +/// $response:timeout: interrupts the request that is not finished in time, the object is created before the request and destroyed after it +class Response_timeout { +public: + static THREAD_LOCAL Response_timeout *current; + + /// @a akill_process: the process will be killed if the request is still running after the grace period + Response_timeout(bool akill_process) : kill_process(akill_process), expired(0), armed(false) { + current=this; + } + ~Response_timeout() { + set(0); + current=0; + } + + /// restarts the timer for the given number of seconds, 0 cancels it + void set(int aseconds) { + if(armed){ + pa_timer_stop(timer); + armed=false; + } + if(!aseconds) + return; + + seconds=aseconds; + expired=0; + if(kill_process) // pa_ctime can't be called from the handler, so preparing for the log in advance + stamp=pa_strdup(pa_ctime(time(0)+seconds+RESPONSE_TIMEOUT_GRACE)); + + armed=pa_timer_start(&timer, seconds, kill_process ? RESPONSE_TIMEOUT_GRACE : 0, handler); + } + +private: + bool kill_process; + volatile sig_atomic_t expired; + bool armed; + pa_timer timer; + int seconds; + const char *stamp; + + static void handler(int) { + Response_timeout *timeout=current; + if(!timeout) + return; + + if(timeout->expired && timeout->kill_process){ + // the request is still running after the grace period + pa_log_at(timeout->stamp, "response timeout (%d s) exceeded, process killed", timeout->seconds); +#ifndef _MSC_VER + kill(getpid(), SIGKILL); +#endif + return; + } + + timeout->expired=1; + if(request) + request->set_skip(Request::SKIP_INTERRUPTED); + } +}; + +THREAD_LOCAL Response_timeout *Response_timeout::current=0; + +void SAPI::set_response_timeout(SAPI_Info&, int seconds) { + if(Response_timeout::current) + Response_timeout::current->set(seconds); +} + class RequestInfoController { public: RequestInfoController(Request_info* rinfo, SAPI_Info* sinfo){ @@ -330,7 +395,7 @@ static void config_handler(SAPI_Info &in #ifdef CODE_CACHE Code_cache_manager::start(); #endif - r.core(config_filespec, false, String::Empty); + r.core(config_filespec, String::Empty); #ifdef CODE_CACHE Code_cache_manager::finish(r); #endif @@ -356,10 +421,16 @@ static void connection_handler(SAPI_Info request_info.mail_received = false; request_info.argv = argv_extra; + // process can be killed on timeout only in parallel mode + Response_timeout response_timeout(HTTPD_Server::mode == HTTPD_Server::PARALLEL); + // prepare to process request Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT)); + RequestController rc(&r); + WebSocket websocket(connection); + r.websocket=&websocket; // process the request - r.core(config_filespec, strcasecmp(request_info.method, "HEAD")==0, main_method_name, &httpd_class_name); + r.core(config_filespec, main_method_name, &httpd_class_name); } catch(const Exception& e) { // exception in connection handling SAPI::log(info, "%s", e.comment()); const char* status = info.exception_http_status(e.type()); @@ -373,6 +444,8 @@ DWORD WINAPI connection_thread(void *arg #else static void *connection_thread(void *arg){ #endif + Temp_gc_references gc_references; // for pointers GC does not see, the very start of the thread + HTTPD_Connection &connection=*(HTTPD_Connection*)arg; SAPI_Info_HTTPD info(connection); @@ -386,32 +459,53 @@ static void *connection_thread(void *arg return 0; } +static void reject_connection(HTTPD_Connection &connection) { + // main thread only + static unsigned long total=0; + static time_t logged=0; + + total++; + time_t now=time(0); + if(now!=logged){ // not more than once a second + logged=now; + pa_log("max-connections limit %d reached, rejecting connection from %s (%lu rejected in total)", HTTPD_Server::max_connections, connection.remote_addr, total); + } + + SAPI_Info_HTTPD info(connection); + SAPI::send_error(info, "too many connections", "503"); +} + static void httpd_mode() { config_handler(*sapi_info); SOCKET sock = HTTPD_Server::bind(httpd_host_port); -#ifdef SIGPIPE - signal(SIGPIPE, SIG_IGN); -#endif - while(1){ #ifndef _MSC_VER pid_t pid=1; - if(HTTPD_Server::mode == HTTPD_Server::PARALLEL) - while (waitpid((pid_t)(-1), 0, WNOHANG) > 0); #endif try { HTTPD_Connection connection; - if(!connection.accept(sock, 500)) + bool accepted=connection.accept(sock, 500); +#ifndef _MSC_VER + if(HTTPD_Server::mode == HTTPD_Server::PARALLEL) + while (waitpid((pid_t)(-1), 0, WNOHANG) > 0) + HTTPD_Server::track_connection(-1); // the child has exited, some of them while we were waiting +#endif + if(!accepted) + continue; + + if(HTTPD_Server::too_many_connections()){ + reject_connection(connection); // no thread or process is started continue; + } switch (HTTPD_Server::mode) { case HTTPD_Server::MULTITHREADED: #ifdef _MSC_VER if (!GC_CreateThread(0, 0, connection_thread, new HTTPD_Connection(connection), 0, 0)) throw Exception("httpd.fork", 0, "thread creation failed"); - connection.sock=INVALID_SOCKET; + connection.sock=INVALID_SOCKET; // thread owns the connection now break; #else #ifdef HAVE_TLS @@ -422,22 +516,25 @@ static void httpd_mode() { if(int result=GC_pthread_create(&thread, &attr, connection_thread, new HTTPD_Connection(connection))) throw Exception("httpd.fork", 0, "thread creation failed (%d)", result); - connection.sock=INVALID_SOCKET; + connection.sock=INVALID_SOCKET; // thread owns the connection now break; #endif case HTTPD_Server::PARALLEL: pid=fork(); if(pid<0) throw Exception("httpd.fork", 0, "fork failed: %s (%d)", strerror(errno), errno); - if(pid>0) - continue; // parent should close connection.sock as well + if(pid>0){ + closesocket(connection.sock); // parent should close connection.sock as well + connection.sock=INVALID_SOCKET; // child owns the connection now + continue; + } #endif case HTTPD_Server::SEQUENTIAL: // and fork child SAPI_Info_HTTPD info(connection); connection_handler(info, connection); } - // closing connection socket in HTTPD_Connection destructor + // SEQUENTIAL or error: closing connection socket in HTTPD_Connection destructor } catch(const Exception& e) { // exception in accept or send_error pa_log("%s", e.comment()); } @@ -534,13 +631,16 @@ static void real_parser_handler(bool cgi #endif { + // CGI process can be killed on timeout + Response_timeout response_timeout(true); + // prepare to process request Request r(*sapi_info, request_info, cgi ? String::Language(String::L_HTML|String::L_OPTIMIZE_BIT) : String::L_AS_IS); // initing ::request ptr for signal handlers RequestController rc(&r); // process the request - r.core(locate_config(config_filespec, parser3_filespec), strcasecmp(request_info.method, "HEAD")==0); - // clearing ::request in RequestController destructor to prevent signal handlers from accessing invalid memory + r.core(locate_config(config_filespec, parser3_filespec)); + // clearing ::request in rc destructor to prevent signal handlers from accessing invalid memory // then the request is destroyed, before the libraries are unloaded: its pool cleanups call them (memcached) } @@ -615,6 +715,8 @@ static void usage(const char* message=NU } int main(int argc, char *argv[]) { + Temp_gc_references gc_references; // for pointers GC does not see + #ifdef PA_DEBUG_CGI_ENTRY_EXIT pa_log("main: entry"); #endif @@ -695,7 +797,7 @@ int main(int argc, char *argv[]) { log_filespec=getenv(PARSER_LOG_ENV_NAME); if(!log_filespec) log_filespec=getenv(REDIRECT_PREFIX PARSER_LOG_ENV_NAME); - if(log_filespec) + if(log_filespec && strcmp(log_filespec, "-")) // "-" is stderr log_filespec=full_disk_path(log_filespec); #ifdef _MSC_VER