diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-12-10 01:45:43 +0100 |
---|---|---|
committer | Anthony G. Basile <blueness@gentoo.org> | 2014-12-11 13:45:16 -0500 |
commit | 11d61581aa4cb74f657fe252132d6f757a88d41f (patch) | |
tree | 9ca3c85950f36834e9f44040cbe8f15cc39ff5dc /src | |
parent | 96ed47b845e22a730e3b7f567f60d552aa8981e6 (diff) |
core: unify how we iterate over inotify events
Let's add some syntactic sugar for iterating through inotify events, and
use it everywhere.
Signed-off-by: Anthony G. Basile <blueness@gentoo.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/shared/macro.h | 1 | ||||
-rw-r--r-- | src/shared/util.h | 7 | ||||
-rw-r--r-- | src/udev/udevd.c | 33 |
3 files changed, 21 insertions, 20 deletions
diff --git a/src/shared/macro.h b/src/shared/macro.h index e2c519cf89..41fe61fb68 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -35,6 +35,7 @@ #define _likely_(x) (__builtin_expect(!!(x),1)) #define _unlikely_(x) (__builtin_expect(!!(x),0)) #define _public_ __attribute__ ((visibility("default"))) +#define _alignas_(x) __attribute__((aligned(__alignof(x)))) #define _cleanup_(x) __attribute__((cleanup(x))) /* Temporarily disable some warnings */ diff --git a/src/shared/util.h b/src/shared/util.h index cbfe787be2..21e977dc51 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -458,3 +458,10 @@ char *tempfn_xxxxxx(const char *p); int is_dir(const char *path, bool follow); int execute_command(const char *command, char *const argv[]); + +#define INOTIFY_EVENT_MAX (sizeof(struct inotify_event) + NAME_MAX + 1) + +#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \ + for ((e) = (struct inotify_event*) (buffer); \ + (uint8_t*) (e) < (uint8_t*) (buffer) + (sz); \ + (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len)) diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 75efa67d22..b519dc4c99 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -807,41 +807,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; } |