From 9900764488bc34caa9d83c881c0e7d2a6ffc9064 Mon Sep 17 00:00:00 2001 From: Christopher Wellons Date: Tue, 5 Feb 2019 23:56:40 -0500 Subject: [PATCH] Fix some of the queue logic --- endlessh.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/endlessh.c b/endlessh.c index 1bf116c..ce24614 100644 --- a/endlessh.c +++ b/endlessh.c @@ -140,13 +140,19 @@ queue_remove(struct queue *q, int fd) * is virtually always one of the first few elements. */ struct client *c; - struct client **prev = &q->head; - for (c = q->head; c; prev = &c->next, c = c->next) { + struct client *prev = 0; + for (c = q->head; c; prev = c, c = c->next) { if (c->fd == fd) { - q->length--; - if (q->tail == c) - q->tail = 0; - *prev = c->next; + if (!--q->length) { + q->head = q->tail = 0; + } else if (q->tail == c) { + q->tail = prev; + prev->next = 0; + } else if (prev) { + prev->next = c->next; + } else { + q->head = c->next; + } c->next = 0; break; } @@ -666,10 +672,10 @@ main(int argc, char **argv) fprintf(stderr, "endlessh: warning: out of memory\n"); close(fd); } + queue_append(queue, client); logmsg(LOG_INFO, "ACCEPT host=%s port=%d fd=%d n=%d/%d", client->ipaddr, client->port, client->fd, queue->length, config.max_clients); - queue_append(queue, client); } }