core: fix reading subset of available events
authorKent Gibson <warthog618@gmail.com>
Sat, 12 Sep 2020 08:11:05 +0000 (16:11 +0800)
committerBartosz Golaszewski <bgolaszewski@baylibre.com>
Mon, 14 Sep 2020 08:30:40 +0000 (10:30 +0200)
Only read the requested number of events from the kernel rather than
reading up to 16 and quietly discarding any surplus.

The previous behavour is particularly bad for reading single events as
userspace must read the events as quickly as they arrive, effectively
negating the presence of the kernel event kfifo.

Fixes: 44921ecc9a00 ("core: provide functions for reading multiple line events at once")
Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
lib/core.c

index ad7605170107256b689404a86937c9f9b5c60ad6..b9642723a2e42ea743328e746893dd7abeed513b 100644 (file)
@@ -1090,7 +1090,10 @@ int gpiod_line_event_read_fd_multiple(int fd, struct gpiod_line_event *events,
 
        memset(evdata, 0, sizeof(evdata));
 
-       rd = read(fd, evdata, sizeof(evdata));
+       if (num_events > 16)
+               num_events = 16;
+
+       rd = read(fd, evdata, num_events * sizeof(*evdata));
        if (rd < 0) {
                return -1;
        } else if ((unsigned int)rd < sizeof(*evdata)) {