From 03f8544691a8de9f77a803fd8cd2eeadb919ec03 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 10 Dec 2023 00:39:16 +0100 Subject: Genesis commit --- c/sigfd/.gitignore | 1 + c/sigfd/Makefile | 8 ++++++++ c/sigfd/main.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 c/sigfd/.gitignore create mode 100644 c/sigfd/Makefile create mode 100644 c/sigfd/main.c (limited to 'c/sigfd') 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 +#include + +#include +#include +#include +#include +#include +#include + +#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; +} -- cgit v1.2.3