Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

inotify_init(2)

FieldValue
Numbers (x64 / i386 / arm64-generic / arm32)inotify_init: x64 253 / i386 291 / arm64-generic none / arm32 316; inotify_init1: x64 294 / i386 332 / arm64-generic 26 / arm32 360
Since (kernel)inotify_init 2.6.13; inotify_init1 2.6.27 (man VERSIONS)
Man pageman2/inotify_init.2 covers both (local cache, dated 2020-04-11); man2/inotify_init1.2 is a roff .so alias; the API overview man7/inotify.7 is not in the local cache and was fetched -> source-cache/fetched/inotify.7.html / .txt
glibc wrapperYes; inotify_init() glibc 2.4, inotify_init1() glibc 2.9 (man VERSIONS); declared in <sys/inotify.h> (installed glibc 2.41)
Aliases coveredinotify_init1 (SC-inotify_init1, grouped here)
Ledger IDSC-inotify_init

Purpose

inotify_init() creates a new inotify instance and returns the file descriptor of its event queue; watches are then added with inotify_add_watch(2) and removed with inotify_rm_watch(2). inotify_init1() is the flags-taking variant. The asm-generic table has only the inotify_init1 number (26), which is why the arm64-generic column shows none for the plain call (worklog 1-d and the hub note this).

Synopsis

#include <sys/inotify.h>
int inotify_init(void);
int inotify_init1(int flags);

Arguments and behavior

CallArgumentNotes
inotify_init()noneEquivalent to inotify_init1(0)
inotify_init1()flagsIN_CLOEXEC and/or IN_NONBLOCK, bitwise OR

IN_NONBLOCK sets O_NONBLOCK on the open file description (reads of the queue then fail with EAGAIN when empty instead of blocking); IN_CLOEXEC sets FD_CLOEXEC. Both constants equal O_CLOEXEC / O_NONBLOCK in <linux/inotify.h>, saving extra fcntl(2) calls.

Return value and errors

Returns a new file descriptor on success, -1 on error with errno set. Errors per the man page: EINVAL (inotify_init1 with an invalid flags value), EMFILE (per-user limit on inotify instances reached, or the per-process open file descriptor limit reached), ENFILE (system-wide file table limit), ENOMEM.

Flags and constants

Event and watch-mask bits (values from include/linux/inotify.h, same set as the installed headers; semantics per fetched inotify.7):

ConstantValueClassMeaning
IN_ACCESS0x00000001eventFile was accessed
IN_MODIFY0x00000002eventFile was modified
IN_ATTRIB0x00000004eventMetadata changed
IN_CLOSE_WRITE0x00000008eventWritable file closed
IN_CLOSE_NOWRITE0x00000010eventUnwritable file closed
IN_OPEN0x00000020eventFile opened
IN_MOVED_FROM0x00000040eventMoved out of watched dir (cookie pairs with MOVED_TO)
IN_MOVED_TO0x00000080eventMoved into watched dir
IN_CREATE0x00000100eventSubfile created
IN_DELETE0x00000200eventSubfile deleted
IN_DELETE_SELF0x00000400eventWatched object deleted
IN_MOVE_SELF0x00000800eventWatched object moved
IN_UNMOUNT0x00002000eventBacking filesystem unmounted
IN_Q_OVERFLOW0x4000eventQueue overflowed (wd = -1)
IN_IGNORED0x8000eventWatch removed (see inotify.7)
IN_CLOSEWRITE|NOWRITEmaskShorthand
IN_MOVEMOVED_FROM|MOVED_TOmaskShorthand
IN_ONLYDIR0x01000000watchOnly watch directories
IN_DONT_FOLLOW0x02000000watchDo not dereference symlink
IN_EXCL_UNLINK0x04000000watchSkip events for unlinked children
IN_MASK_CREATE0x10000000watchFail if watch exists (added with inotify_add_watch)
IN_MASK_ADD0x20000000watchOR into an existing watch mask
IN_ISDIR0x40000000eventSubject was a directory
IN_ONESHOT0x80000000watchDeliver one event, then remove watch

Tunables named by inotify.7: /proc/sys/fs/inotify/max_user_instances, max_user_watches, max_queued_events.

Security and notes

No capability is required; limits are per-user and reachable through the /proc/sys/fs/inotify/ tunables above. Events carry an inotify_event struct (wd, mask, cookie, len, name) defined in <sys/inotify.h>. Queue overflow is signaled with IN_Q_OVERFLOW rather than a failed read; IN_IGNORED arrives when a watch is killed (file deleted, filesystem unmounted). Related pages in the hub: inotify_add_watch(2), inotify_rm_watch(2) – overview.md.

References

  • Cached man pages: source-cache/man-pages-sparse/man2/inotify_init.2, .so alias inotify_init1.2
  • Fetched overview: source-cache/fetched/inotify.7.txt (from https://man7.org/linux/man-pages/man7/inotify.7.html)
  • Constants: installed /usr/include/linux/inotify.h, /usr/include/x86_64-linux-gnu/sys/inotify.h
  • Kernel tables: source-cache/kernel/syscall_64.tbl (253, 294), syscall_generic.tbl (26)