aboutsummaryrefslogtreecommitdiff
path: root/c/sigfd
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2023-12-10 00:39:16 +0100
committerThomas Voss <mail@thomasvoss.com> 2023-12-10 00:39:37 +0100
commit03f8544691a8de9f77a803fd8cd2eeadb919ec03 (patch)
tree4d3431d678b892cb057af72a982ac73d313d60f9 /c/sigfd
Genesis commit
Diffstat (limited to 'c/sigfd')
-rw-r--r--c/sigfd/.gitignore1
-rw-r--r--c/sigfd/Makefile8
-rw-r--r--c/sigfd/main.c42
3 files changed, 51 insertions, 0 deletions
diff --git a/c/sigfd/.gitignore b/c/sigfd/.gitignore
new file mode 100644
index 0000000..067fa65
--- /dev/null
+++ b/c/sigfd/.gitignore
@@ -0,0 +1 @@
+sigfd
diff --git a/c/sigfd/Makefile b/c/sigfd/Makefile
new file mode 100644
index 0000000..f126000
--- /dev/null
+++ b/c/sigfd/Makefile
@@ -0,0 +1,8 @@
+include ../base.mk
+
+all: sigfd
+sigfd: main.c
+ $(CC) $(CFLAGS) -o $@ $<
+
+clean:
+ rm -f sigfd
diff --git a/c/sigfd/main.c b/c/sigfd/main.c
new file mode 100644
index 0000000..6721f60
--- /dev/null
+++ b/c/sigfd/main.c
@@ -0,0 +1,42 @@
+#include <sys/poll.h>
+#include <sys/signalfd.h>
+
+#include <err.h>
+#include <poll.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#define die(...) err(EXIT_FAILURE, __VA_ARGS__)
+#define lengthof(a) (sizeof(a) / sizeof(*(a)))
+
+static char buf[4096];
+
+int
+main(void)
+{
+ sigset_t set;
+ sigemptyset(&set);
+ sigaddset(&set, SIGINT);
+ sigaddset(&set, SIGQUIT);
+ sigprocmask(SIG_BLOCK, &set, NULL);
+
+ int fd = signalfd(-1, &set, 0);
+ if (fd == -1)
+ die("signalfd");
+
+ struct pollfd fds[] = {
+ {.fd = fd, .events = POLL_IN},
+ };
+ for (;;) {
+ if (poll(fds, lengthof(fds), -1) == -1)
+ die("poll");
+ if (fds[0].revents & POLL_IN) {
+ fputs("Caught signal!\n", stderr);
+ read(fds[0].fd, buf, sizeof(buf));
+ }
+ }
+
+ return EXIT_SUCCESS;
+}