summaryrefslogtreecommitdiff
path: root/src/dged/reactor-epoll.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/dged/reactor-epoll.c')
-rw-r--r--src/dged/reactor-epoll.c59
1 files changed, 57 insertions, 2 deletions
diff --git a/src/dged/reactor-epoll.c b/src/dged/reactor-epoll.c
index e488fef..96c6da4 100644
--- a/src/dged/reactor-epoll.c
+++ b/src/dged/reactor-epoll.c
@@ -1,12 +1,20 @@
+#include "minibuffer.h"
#include "reactor.h"
+#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <sys/epoll.h>
+#include <sys/inotify.h>
+#include <unistd.h>
struct reactor {
int epoll_fd;
- void *events;
+ struct events *events;
+ int inotify_fd;
+ uint32_t inotify_poll_id;
};
struct events {
@@ -20,9 +28,16 @@ struct reactor *reactor_create() {
perror("epoll_create1");
}
+ int inotifyfd = inotify_init1(IN_NONBLOCK);
+ if (inotifyfd == -1) {
+ perror("inotify_init1");
+ }
+
struct reactor *r = (struct reactor *)calloc(1, sizeof(struct reactor));
r->epoll_fd = epollfd;
r->events = calloc(1, sizeof(struct events));
+ r->inotify_fd = inotifyfd;
+ r->inotify_poll_id = reactor_register_interest(r, inotifyfd, ReadInterest);
return r;
}
@@ -64,8 +79,48 @@ bool reactor_poll_event(struct reactor *reactor, uint32_t ev_id) {
return false;
}
+uint32_t reactor_watch_file(struct reactor *reactor, const char *path,
+ uint32_t mask) {
+ // TODO: change if we get more event types
+ mask = IN_MODIFY;
+
+ int fd = inotify_add_watch(reactor->inotify_fd, path, mask);
+ if (fd == -1) {
+ minibuffer_echo_timeout(4, "failed to watch %s: %s", path, strerror(errno));
+ return 0;
+ }
+
+ return (uint32_t)fd;
+}
+
+void reactor_unwatch_file(struct reactor *reactor, uint32_t id) {
+ inotify_rm_watch(reactor->inotify_fd, id);
+}
+
+bool reactor_next_file_event(struct reactor *reactor, struct file_event *out) {
+ if (reactor_poll_event(reactor, reactor->inotify_poll_id)) {
+ ssize_t sz = sizeof(struct inotify_event) + NAME_MAX + 1;
+ uint8_t buf[sz];
+ ssize_t bytes_read = read(reactor->inotify_fd, buf, sz);
+ if (bytes_read < 0) {
+ return false;
+ }
+
+ struct inotify_event *ev = (struct inotify_event *)buf;
+ // TODO: change when adding more of these
+ out->mask = FileWritten;
+ if (ev->mask & IN_IGNORED != 0) {
+ out->mask |= LastEvent;
+ }
+ out->id = (uint32_t)ev->wd;
+ return true;
+ }
+
+ return false;
+}
+
void reactor_update(struct reactor *reactor) {
- struct events *events = (struct events *)reactor->events;
+ struct events *events = reactor->events;
int nfds = epoll_wait(reactor->epoll_fd, events->events, 10, -1);
if (nfds == -1) {