Skip to content
Advertisement

Are inotify event reads guaranteed not to be split across buffers?

All of the code examples for inotify do something like this:

#define EVENT_SIZE  (sizeof (struct inotify_event))

#define BUF_LEN        (1024 * (EVENT_SIZE + NAME_MAX + 1))

char buf[BUF_LEN];

num = read(_fd, buf, BUF_LEN);

So presumably this allows for a single read() call to return up to 1024 events.

What if there are actually more events than that in the queue? Will inotify guarantee to return a whole number of events, even if that means a short read? Or will inotify split an event across two reads?

Advertisement

Answer

From the man page for inotify:

The behavior when the buffer given to read(2) is too small to return information about the next event depends on the kernel version: in kernels before 2.6.21, read(2) returns 0; since kernel 2.6.21, read(2) fails with the error EINVAL. Specifying a buffer of size

sizeof(struct inotify_event) + NAME_MAX + 1

will be sufficient to read at least one event.

So historically it rounded down, and at this point, it sounds like it would fail with EINVAL.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement