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_add_watch(2)

FieldValue
Numbers (x64 / i386 / arm64-generic / arm32)x64 254 / i386 292 / arm64-generic 27 / arm32 317
Since (kernel)2.6.13 (inotify merged in 2.6.13; man VERSIONS)
Man pageman2/inotify_add_watch.2 (cached, 2021-03-22); mask bits documented in inotify.7 (fetched, man-pages 6.19)
glibc wrapperYes; <sys/inotify.h> (glibc 2.4)
Aliases coverednone
Ledger IDSC-inotify_add_watch

Purpose

inotify_add_watch() adds a new watch, or modifies an existing watch, for the file named by pathname, on the inotify instance referred to by fd. It returns the watch descriptor (wd) that later read(2)s report inside inotify_event structures. The companion calls are inotify_init.md (create the instance) and inotify_rm_watch.md (remove a watch).

Synopsis

#include <sys/inotify.h>
int inotify_add_watch(int fd, const char *pathname, uint32_t mask);

Arguments and behavior

fd must be an inotify instance descriptor; the caller needs read permission on pathname. mask is an OR of IN_* event bits plus the control bits in the table below. If the inode is already watched by this instance (possibly via a different link), the existing wd is returned and the mask is updated (ORed with IN_MASK_ADD, replaced otherwise). If the object was not watched, a new wd is allocated. Event delivery and the queue tunables (/proc/sys/fs/inotify/max_user_watches, max_queued_events, max_user_instances) are covered in inotify.7 and on the inotify_init.md page.

Return value and errors

Returns the watch descriptor on success, -1 on error: EACCES, EBADF, EEXIST (IN_MASK_CREATE on an already watched object), EFAULT, EINVAL (no valid event bits; both IN_MASK_ADD and IN_MASK_CREATE; fd not an inotify descriptor), ENAMETOOLONG, ENOENT, ENOMEM, ENOSPC (per-user watch limit reached), ENOTDIR (IN_ONLYDIR on a non-directory).

Flags and constants (mask bits, values from <linux/inotify.h>)

ConstantValueMeaningSince
IN_ACCESS0x00000001File was accessed2.6.13
IN_MODIFY0x00000002File was modified2.6.13
IN_ATTRIB0x00000004Metadata changed2.6.13
IN_CLOSE_WRITE0x00000008Writable file closed2.6.13
IN_CLOSE_NOWRITE0x00000010Unwritable file closed2.6.13
IN_OPEN0x00000020File was opened2.6.13
IN_MOVED_FROM0x00000040Moved out of watched dir2.6.13
IN_MOVED_TO0x00000080Moved into watched dir2.6.13
IN_CREATE0x00000100File created in watched dir2.6.13
IN_DELETE0x00000200File deleted in watched dir2.6.13
IN_DELETE_SELF0x00000400Watched object itself deleted2.6.13
IN_MOVE_SELF0x00000800Watched object itself moved2.6.13
IN_UNMOUNT0x00002000Backing filesystem unmounted2.6.13
IN_Q_OVERFLOW0x00004000Event queue overflowed2.6.13
IN_IGNORED0x00008000Watch was removed2.6.13
IN_ONLYDIR0x01000000Watch only directories2.6.15
IN_DONT_FOLLOW0x02000000Do not follow symlinks2.6.15
IN_EXCL_UNLINK0x04000000Skip events for unlinked objects2.6.36
IN_MASK_CREATE0x10000000Fail with EEXIST instead of re-watching4.18
IN_MASK_ADD0x20000000OR into the existing watch maskheader (glibc 2.5; no kernel date pinned in sources)
IN_ISDIR0x40000000Event subject is a directory (returned, not settable)2.6.13
IN_ONESHOT0x80000000Deliver one event, then remove the watch2.6.13

The header also composes IN_CLOSE, IN_MOVE and IN_ALL_EVENTS. Removing a watch (explicitly or by deleting the object) generates IN_IGNORED (inotify_rm_watch.md); before 2.6.36 IN_ONESHOT did not emit that follow-up event, since 2.6.36 it does (inotify.7 BUGS).

Security and notes

This is a Linux-specific API. Hard limits (max_user_watches etc.) and the IN_IGNORED-after-unlink behavior are described in the fetched inotify.7. Fanotify is the privileged, mount-wide alternative: fanotify_init.md.

References

  • Cached man page: source-cache/man-pages-sparse/man2/inotify_add_watch.2
  • Online: https://man7.org/linux/man-pages/man2/inotify_add_watch.2.html
  • Mask overview: source-cache/fetched/inotify.7.txt (man-pages 6.19)
  • Constants: /usr/include/x86_64-linux-gnu/linux/inotify.h, sys/inotify.h