commit 4761f90bc5e5e5a15e0b160e0a2a486969d7a223
parent 7ee4156793eb3f5f7f47528ab32721be4c507324
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Wed, 23 Dec 2020 22:11:22 -0800
Handle signals and zombies
Diffstat:
2 files changed, 29 insertions(+), 7 deletions(-)
diff --git a/config.def.h b/config.def.h
@@ -3,6 +3,9 @@
/* default gopher hole (path, host, port): */
static const char *default_address[] = { "/", "localhost", "70" };
+/* signals to handle */
+static const int signals[] = { SIGABRT, SIGCHLD, SIGINT, SIGTERM };
+
#define IFS ":\t " /* input field seporator */
#define ISS ";\n" /* input statement seporator */
diff --git a/main.c b/main.c
@@ -17,6 +17,7 @@
#include <sys/socket.h>
#include <sys/stat.h>
+#include <sys/wait.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
@@ -24,6 +25,7 @@
#include <limits.h>
#include <netdb.h>
#include <poll.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -665,31 +667,48 @@ gawk(const char **addr)
--depth;
- if (ferror(stdin)) {
+ if (ferror(stdin) && !fatal) {
warn("'%s'", "/dev/stdin");
return fatal = -1;
}
return done - 1;
}
+void
+sighandler(int sig)
+{
+ switch (sig) {
+ case SIGCHLD:
+ while (waitpid(WAIT_ANY, NULL, WNOHANG) > 0);
+ break;
+ default:
+ fclose(stdin);
+ fatal = 1;
+ }
+}
+
int
main(int argc, char *argv[])
{
+ int i;
int status;
+ if (argc > 1 && *argv[1] == '-') {
+ fprintf(stderr, "usage: %s [path [host [port]]]\n", getprogname());
+ return 1;
+ }
+
#ifdef __OpenBSD__
if (pledge("stdio rpath wpath cpath inet dns proc exec", NULL) == -1)
err(1, "pledge");
-#endif
+#endif /* __OpenBSD__ */
+
+ for (i = 0; i < LEN(signals); ++i)
+ signal(signals[i], sighandler);
if (mkdtemp(tmpdir) == NULL)
err(1, "mkdtemp '%s'", tmpdir);
-
status = sgoto(argc - 1, (const char **)argv + 1, 0, default_address);
- if (status)
- fprintf(stderr, "usage: %s [path [host [port]]]\n",
- getprogname());
-
if (rmdir(tmpdir) == -1)
err(1, "rmdir '%s'", tmpdir);