From: Fredrik Tolf Date: Sun, 5 Apr 2026 22:47:30 +0000 (+0200) Subject: httrcall: Fix limiter bug. X-Git-Url: http://www.dolda2000.com/gitweb/?a=commitdiff_plain;h=cad7f72d02c7ff05482643d5167a2387bac90a87;p=ashd.git httrcall: Fix limiter bug. --- diff --git a/src/httrcall.c b/src/httrcall.c index c3e7e0d..e63bb12 100644 --- a/src/httrcall.c +++ b/src/httrcall.c @@ -33,36 +33,25 @@ #include #include -struct current { - struct current *next, *prev; - pid_t pid; -}; - static char **prog; -static struct current *running = NULL; -static int nrunning = 0, limit = 0; +typedbuf(pid_t) running; +static int limit = 0; static volatile int exited; static void checkexit(int block) { + int i, st, any; pid_t pid; - int st; - struct current *rec; exited = 0; - while((pid = waitpid(-1, &st, block?0:WNOHANG)) > 0) { + any = 0; + while((pid = waitpid(-1, &st, (block && !any) ? 0 : WNOHANG)) > 0) { + any = 1; if(WCOREDUMP(st)) flog(LOG_WARNING, "child process %i dumped core", pid); - for(rec = running; rec != NULL; rec = rec->next) { - if(rec->pid == pid) { - if(rec->next) - rec->next->prev = rec->prev; - if(rec->prev) - rec->prev->next = rec->next; - if(rec == running) - running = rec->next; - free(rec); - nrunning--; + for(i = 0; i < running.d; i++) { + if(running.b[i] == pid) { + running.b[i] = running.b[--running.d]; break; } } @@ -72,21 +61,14 @@ static void checkexit(int block) static void serve(struct hthead *req, int fd) { pid_t new; - struct current *rec; - while((limit > 0) && (nrunning >= limit)) + while((limit > 0) && (running.d >= limit)) checkexit(1); if((new = stdforkserve(prog, req, fd, NULL, NULL)) < 0) { simpleerror(fd, 500, "Server Error", "The server appears to be overloaded."); return; } - omalloc(rec); - rec->pid = new; - rec->next = running; - if(running != NULL) - running->prev = rec; - running = rec; - nrunning++; + bufadd(running, new); } static void chldhandler(int sig)