I wish I was able to answer with something smarter, but the only way I found to count the inotify(7)
watches set by a user is this:
find /proc/*/fd -user "$USER" -lname anon_inode:inotify \
-printf '%hinfo/%f\n' 2>/dev/null |
xargs cat | grep -c '^inotify'
Change "$USER"
with the actual user you want info about; "`id -u`"
for the current user. Notice that the max_user_watches
is a per-user limit, not per process, or per inotify instance / file descriptor.
This will first find
all open file descriptors created by inotify_init*(2)
, and will then look into the corresponding /proc/PID/fdinfo/FD
file for the info about the watch descriptors added with inotify_add_watch(2)
to each of them (look into the proc(5)
manpage under /proc/[pid]/fdinfo/
for a description of the inotify-specific entries). The -c
option of grep
will let it print a count of all lines that match a pattern.
Note: the counter for inotify_watches
is kept just like other per-user counters in a struct ucounts
referenced from the user namespace structure -- see include/linux/user_namespace.h
; but I wasn't able to find any interface exposing those counters to userland.