diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-12-10 01:45:43 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-12-10 01:48:39 +0100 |
commit | f7c1ad4fd4190bee32db0aa26c8e9fe7e19d8816 (patch) | |
tree | 5a0ea84fa0c4a33eb9ff43f16f09024e1c595ef7 /src/udev/udevd.c | |
parent | df63dda6d4b4fc90f895cfd40d54e15928671624 (diff) |
core: unify how we iterate over inotify events
Let's add some syntactic sugar for iterating through inotify events, and
use it everywhere.
Diffstat (limited to 'src/udev/udevd.c')
-rw-r--r-- | src/udev/udevd.c | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 6a8dda3272..8bec03e77f 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -816,41 +816,34 @@ static int synthesize_change(struct udev_device *dev) { } static int handle_inotify(struct udev *udev) { - int nbytes, pos; - char *buf; - struct inotify_event *ev; - int r; + uint8_t buffer[INOTIFY_EVENT_MAX] _alignas_(struct inotify_event); + struct inotify_event *e; + ssize_t l; - r = ioctl(fd_inotify, FIONREAD, &nbytes); - if (r < 0 || nbytes <= 0) - return -errno; + l = read(fd_inotify, buffer, sizeof(buffer)); + if (l < 0) { + if (errno == EAGAIN || errno == EINTR) + return 0; - buf = malloc(nbytes); - if (!buf) { - log_error("error getting buffer for inotify"); - return -ENOMEM; + return log_error_errno(errno, "Failed to read inotify fd: %m"); } - nbytes = read(fd_inotify, buf, nbytes); - - for (pos = 0; pos < nbytes; pos += sizeof(struct inotify_event) + ev->len) { + FOREACH_INOTIFY_EVENT(e, buffer, l) { struct udev_device *dev; - ev = (struct inotify_event *)(buf + pos); - dev = udev_watch_lookup(udev, ev->wd); + dev = udev_watch_lookup(udev, e->wd); if (!dev) continue; - log_debug("inotify event: %x for %s", ev->mask, udev_device_get_devnode(dev)); - if (ev->mask & IN_CLOSE_WRITE) + log_debug("inotify event: %x for %s", e->mask, udev_device_get_devnode(dev)); + if (e->mask & IN_CLOSE_WRITE) synthesize_change(dev); - else if (ev->mask & IN_IGNORED) + else if (e->mask & IN_IGNORED) udev_watch_end(udev, dev); udev_device_unref(dev); } - free(buf); return 0; } |