1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/* * Copyright (c) 2013 Eric Radman <ericshane@eradman.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/inotify.h>
#include <sys/event.h>
#include <errno.h>
#include <limits.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "compat.h"
#include "../data.h"
/* globals */
extern WatchFile **files;
/* utility forwards */
static WatchFile * file_by_descriptor(int fd);
/* utility functions */
static WatchFile *
file_by_descriptor(int wd) {
int i;
for (i=0; files[i] != NULL; i++) {
if (files[i]->fd == wd)
return files[i];
}
return NULL; /* lookup failed */
}
/* interface */
#define EVENT_SIZE (sizeof (struct inotify_event))
#define EVENT_BUF_LEN (32 * (EVENT_SIZE + 16))
/*
* Conveniently inotify and kqueue ids both have the type `int`
*/
int
kqueue(void) {
return inotify_init();
}
/*
* Emulate kqueue(2). Only the flags used in entr.c are considered
* Returns the number of eventlist structs filled by this call
*/
int
kevent(int kq, const struct kevent *changelist, int nchanges, struct
kevent *eventlist, int nevents, const struct timespec *timeout) {
int n;
int wd;
WatchFile *file;
char buf[EVENT_BUF_LEN];
ssize_t len;
int pos;
struct inotify_event *iev;
u_int fflags;
const struct kevent *kev;
int ignored;
struct pollfd pfd;
if (nchanges > 0) {
ignored = 0;
for (n=0; n<nchanges; n++) {
kev = changelist + (sizeof(struct kevent)*n);
file = (WatchFile *)kev->udata;
if (kev->flags & EV_DELETE) {
inotify_rm_watch(kq /* ifd */, kev->ident);
file->fd = -1; /* invalidate */
}
else if (kev->flags & EV_ADD) {
wd = inotify_add_watch(kq /* ifd */, file->fn,
IN_CLOSE_WRITE|IN_DELETE_SELF|IN_MODIFY|IN_MOVE_SELF);
if (wd < 0)
return -1;
close(file->fd);
file->fd = wd; /* replace with watch descriptor */
}
else
ignored++;
}
return nchanges - ignored;
}
pfd.fd = kq;
pfd.events = POLLIN;
if (timeout != 0 && (poll(&pfd, 1, timeout->tv_nsec/1000000) == 0))
return 0;
/* Consolidate events over 50ms since some Linux apps write to a
file before deleting it */
n = 0;
do {
pos = 0;
len = read(kq /* ifd */, &buf, EVENT_BUF_LEN);
if (len < 0) {
/* SA_RESTART doesn't work for inotify fds */
if (errno == EINTR)
continue;
else
perror("read");
}
while ((pos < len) && (n < nevents)) {
iev = (struct inotify_event *) &buf[pos];
pos += EVENT_SIZE + iev->len;
#ifdef DEBUG
fprintf(stderr, "wd: %d mask: 0x%x\n", iev->wd, iev->mask);
#endif
/* convert iev->mask; to comparable kqueue flags */
fflags = 0;
if (iev->mask & IN_DELETE_SELF) fflags |= NOTE_DELETE;
if (iev->mask & IN_CLOSE_WRITE) fflags |= NOTE_WRITE;
if (iev->mask & IN_MOVE_SELF) fflags |= NOTE_RENAME;
if (fflags == 0) continue;
eventlist[n].ident = iev->wd;
eventlist[n].filter = EVFILT_VNODE;
eventlist[n].flags = 0;
eventlist[n].fflags = fflags;
eventlist[n].data = 0;
eventlist[n].udata = file_by_descriptor(iev->wd);
n++;
}
}
while ((poll(&pfd, 1, 50) > 0));
return n;
}
|