Re: another slowcgi cleanup

2021-04-16 Thread Todd C . Miller
On Fri, 16 Apr 2021 17:34:38 +0200, Claudio Jeker wrote:

> Every code using SLIST_REMOVE() should just switch to proper LISTs.
> I realized slowcgi was using SLIST where it should use LIST but that was
> only the start of the rabbit hole. There is also no need to double wrap
> the request. So this diff make this all nice and shiny.

Looks good to me.  OK millert@

 - todd



another slowcgi cleanup

2021-04-16 Thread Claudio Jeker
Every code using SLIST_REMOVE() should just switch to proper LISTs.
I realized slowcgi was using SLIST where it should use LIST but that was
only the start of the rabbit hole. There is also no need to double wrap
the request. So this diff make this all nice and shiny.

-- 
:wq Claudio

Index: slowcgi.c
===
RCS file: /cvs/src/usr.sbin/slowcgi/slowcgi.c,v
retrieving revision 1.58
diff -u -p -r1.58 slowcgi.c
--- slowcgi.c   8 Jan 2021 22:05:34 -   1.58
+++ slowcgi.c   16 Apr 2021 15:29:14 -
@@ -113,6 +113,7 @@ struct fcgi_stdin {
 TAILQ_HEAD(fcgi_stdin_head, fcgi_stdin);
 
 struct request {
+   LIST_ENTRY(request) entry;
struct eventev;
struct eventresp_ev;
struct eventtmo;
@@ -139,11 +140,7 @@ struct request {
int inflight_fds_accounted;
 };
 
-struct requests {
-   SLIST_ENTRY(requests)entry;
-   struct request  *request;
-};
-SLIST_HEAD(requests_head, requests);
+LIST_HEAD(requests_head, request);
 
 struct slowcgi_proc {
struct requests_headrequests;
@@ -361,7 +358,7 @@ main(int argc, char *argv[])
if (pledge("stdio rpath unix proc exec", NULL) == -1)
lerr(1, "pledge");
 
-   SLIST_INIT(&slowcgi_proc.requests);
+   LIST_INIT(&slowcgi_proc.requests);
event_init();
 
l = calloc(1, sizeof(*l));
@@ -456,7 +453,6 @@ slowcgi_accept(int fd, short events, voi
struct sockaddr_storage  ss;
struct timeval   backoff;
struct request  *c;
-   struct requests *requests;
socklen_tlen;
int  s;
 
@@ -490,14 +486,6 @@ slowcgi_accept(int fd, short events, voi
cgi_inflight--;
return;
}
-   requests = calloc(1, sizeof(*requests));
-   if (requests == NULL) {
-   lwarn("cannot calloc requests");
-   close(s);
-   cgi_inflight--;
-   free(c);
-   return;
-   }
c->fd = s;
c->buf_pos = 0;
c->buf_len = 0;
@@ -512,8 +500,7 @@ slowcgi_accept(int fd, short events, voi
event_set(&c->resp_ev, s, EV_WRITE | EV_PERSIST, slowcgi_response, c);
evtimer_set(&c->tmo, slowcgi_timeout, c);
evtimer_add(&c->tmo, &timeout);
-   requests->request = c;
-   SLIST_INSERT_HEAD(&slowcgi_proc.requests, requests, entry);
+   LIST_INSERT_HEAD(&slowcgi_proc.requests, c, entry);
 }
 
 void
@@ -526,7 +513,6 @@ void
 slowcgi_sig_handler(int sig, short event, void *arg)
 {
struct request  *c;
-   struct requests *ncs;
struct slowcgi_proc *p;
pid_tpid;
int  status;
@@ -536,12 +522,9 @@ slowcgi_sig_handler(int sig, short event
switch (sig) {
case SIGCHLD:
while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
-   c = NULL;
-   SLIST_FOREACH(ncs, &p->requests, entry)
-   if (ncs->request->script_pid == pid) {
-   c = ncs->request;
+   LIST_FOREACH(c, &p->requests, entry)
+   if (c->script_pid == pid)
break;
-   }
if (c == NULL) {
lwarnx("caught exit of unknown child %i", pid);
continue;
@@ -1133,7 +1116,6 @@ cleanup_request(struct request *c)
struct fcgi_response*resp;
struct fcgi_stdin   *stdin_node;
struct env_val  *env_entry;
-   struct requests *ncs, *tcs;
 
evtimer_del(&c->tmo);
if (event_initialized(&c->ev))
@@ -1171,14 +1153,7 @@ cleanup_request(struct request *c)
TAILQ_REMOVE(&c->stdin_head, stdin_node, entry);
free(stdin_node);
}
-   SLIST_FOREACH_SAFE(ncs, &slowcgi_proc.requests, entry, tcs) {
-   if (ncs->request == c) {
-   SLIST_REMOVE(&slowcgi_proc.requests, ncs, requests,
-   entry);
-   free(ncs);
-   break;
-   }
-   }
+   LIST_REMOVE(c, entry);
if (! c->inflight_fds_accounted)
cgi_inflight--;
free(c);