commit 3bb393883567c99ededd076f1e491c1a39cc3a2f
parent 267141d354bfa43906d60dfe61445601089e41de
Author: hhvn <dev@hhvn.uk>
Date: Sun, 6 Jun 2021 00:06:13 +0100
main.c handler.c: exit & reap children properly
Diffstat:
2 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/handler.c b/handler.c
@@ -5,6 +5,7 @@
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
+#include <stdlib.h>
#include <pwd.h>
#include <sys/types.h>
#include "handler.h"
@@ -102,7 +103,7 @@ get_plan(int fd, char *user) {
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
execl(path, path, NULL);
- break;
+ exit(EXIT_SUCCESS);
default:
verbose("Executing %s as PID %d\n", path, pid);
waitpid(pid, &status, 0);
diff --git a/main.c b/main.c
@@ -8,6 +8,7 @@
#include <stdarg.h>
#include <netdb.h>
#include <errno.h>
+#include <signal.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/socket.h>
@@ -118,6 +119,17 @@ handoff(int fd) {
get_plan(fd, user);
}
+void
+sighandler(int signal) {
+ switch (signal) {
+ case SIGCHLD:
+ while (waitpid(-1, NULL, WNOHANG) == 0);
+ break;
+ default:
+ exit(EXIT_SUCCESS);
+ }
+}
+
int
main(int argc, char *argv[]) {
struct sockaddr *addr;
@@ -150,6 +162,9 @@ main(int argc, char *argv[]) {
hints.ai_socktype = SOCK_STREAM;
sock = getsock(&hints, host, port);
+ /* reap children */
+ signal(SIGCHLD, sighandler);
+
for (;;) {
if ((handle = accept(sock, addr, (socklen_t *)sizeof(struct sockaddr))) == -1)
die(1, "accept(): %s\n", strerror(errno));
@@ -165,7 +180,7 @@ main(int argc, char *argv[]) {
handoff(handle);
shutdown(handle, SHUT_RDWR);
close(handle);
- break;
+ exit(EXIT_SUCCESS);
default:
verbose("Forking handle %d to PID %d\n", handle, pid);
}