examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

commit 6f49ef2b8f25abbe4a5702b0b767aea9ecdeb97e
parent 70a7ce89334ef1a40ece89c42834308be751d7b5
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Fri, 26 Aug 2022 23:41:06 +0100

epoll: add inotify event

Diffstat:
Mmakefile | 5++++-
Msrc/epoll.c | 40+++++++++++++++++++++++++++++-----------
2 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/makefile b/makefile @@ -25,4 +25,7 @@ pulse-async-client: -lpulse pulse-simple-client: -lpulse-simple -lm alsa-simple: -lasound -.PHONY: clean all all-c all-c++ +wait: + inotifywait -qe close_write makefile src + +.PHONY: clean all all-c all-c++ wait diff --git a/src/epoll.c b/src/epoll.c @@ -1,6 +1,7 @@ #include <sys/epoll.h> #include <sys/timerfd.h> #include <sys/signalfd.h> +#include <sys/inotify.h> #include <signal.h> #include <unistd.h> #include <stdio.h> @@ -11,12 +12,12 @@ #include <err.h> #include <errno.h> -static int timer, signals, event_queue; +static int timer, signals, notifications, event_queue; static void check_err(char const* msg) { if (errno) - err(1, msg); + err(1, "%s", msg); } static void stdin_event(void) @@ -43,7 +44,7 @@ static void timerfd_event(void) uint64_t new_ticks; read(timer, &new_ticks, sizeof new_ticks); ticks += new_ticks; - printf("Tick: +%lu %lu\n", new_ticks, ticks); + printf("Timer: +%lu %lu\n", new_ticks, ticks); } static void signalfd_event(void) @@ -52,25 +53,35 @@ static void signalfd_event(void) read(signals, &buf, sizeof buf); check_err("read"); printf("Signal: %s\n", strsignal(buf.ssi_signo)); + exit(0); +} + +static void inotify_event(void) +{ + struct inotify_event buf; + read(notifications, &buf, sizeof buf); + check_err("read"); + puts("Executable updated, exiting..."); + exit(0); } static void add_epollin(int fd, void(*cb)()) { epoll_ctl(event_queue, EPOLL_CTL_ADD, fd, &(struct epoll_event){ - .events = EPOLLIN, - .data.ptr = cb, - }); + .events = EPOLLIN, + .data.ptr = cb, + }); check_err("epoll_ctl"); } -int main() +int main(int, char* argv[]) { timer = timerfd_create(CLOCK_MONOTONIC, 0); check_err("timerfd_create"); timerfd_settime(timer, 0, &(struct itimerspec){ - .it_interval = {1, 0}, /* Interval for periodic timer */ - .it_value = {1, 0}, /* Initial expiration */ - }, NULL); + .it_interval = {1, 0}, /* Interval for periodic timer */ + .it_value = {1, 0}, /* Initial expiration */ + }, NULL); check_err("timerfd_settime"); sigset_t sigset; @@ -83,12 +94,19 @@ int main() signals = signalfd(-1, &sigset, 0); check_err("signalfd"); + notifications = inotify_init(); + check_err("inotify_init"); + inotify_add_watch(notifications, argv[0], IN_ATTRIB | IN_MASK_CREATE); + check_err("inotify_add_watch"); + event_queue = epoll_create1(0); - check_err("epoll_create2"); + check_err("epoll_create1"); add_epollin(STDIN_FILENO, stdin_event); add_epollin(timer, timerfd_event); add_epollin(signals, signalfd_event); + add_epollin(notifications, inotify_event); + puts("\nepoll: processing events..."); for (;;) { struct epoll_event events[8];