#!/usr/bin/env bash # verify-inotify-watch-file-system-events.sh # Falsifies the claims on https://heatware.net/linux/inotify-watch-file-system-events/ # # The article is deliberately written as CITED, not executed: inotify is a Linux kernel # interface and the machine that produced the page runs macOS. This script therefore does # the honest thing in both directions: # - on Linux it actually reads the kernel's limits and, if inotify-tools is present, # exercises the watch-exhaustion failure; # - on anything else it SKIPs those and runs the macOS kqueue analogue instead. # It exits 0 standalone in an empty directory. Missing inputs are SKIP, never FAIL. # Executed checks count in PASS/FAIL; greps of the article count in CONTENT. set -u PASS=0; FAIL=0; SKIP=0; CONTENT=0 pass() { echo "PASS $*"; PASS=$((PASS+1)); } fail() { echo "FAIL $*"; FAIL=$((FAIL+1)); } skip() { echo "SKIP $*"; SKIP=$((SKIP+1)); } cpass() { echo "CONTENT-OK $*"; CONTENT=$((CONTENT+1)); } WORK=$(mktemp -d "${TMPDIR:-/tmp}/hw_inotify.XXXXXX") || exit 1 trap 'rm -rf "$WORK"' EXIT OS=$(uname -s) echo "# environment" echo "# uname: $OS $(uname -m)" echo "# python3: $(command -v python3 2>/dev/null || echo '')" echo # ------------------------------------------------------- 1. the documented limits if [ "$OS" = "Linux" ] && [ -r /proc/sys/fs/inotify/max_user_watches ]; then W=$(cat /proc/sys/fs/inotify/max_user_watches) Q=$(cat /proc/sys/fs/inotify/max_queued_events 2>/dev/null || echo "?") I=$(cat /proc/sys/fs/inotify/max_user_instances 2>/dev/null || echo "?") echo "# max_user_watches=$W max_queued_events=$Q max_user_instances=$I" if [ "$W" -ge 8192 ] && [ "$W" -le 1048576 ]; then pass "max_user_watches=$W is inside the kernel's documented clamp [8192, 1048576]" else fail "max_user_watches=$W is outside [8192, 1048576]; either it was tuned locally or the article's clamp is wrong" fi if [ "$W" -gt 8192 ]; then pass "max_user_watches ($W) is above 8192 - confirms 8192 is the floor, not 'the default'" else skip "the 'default is not 8192' point (this kernel clamped to the 8192 floor, which is consistent with the article)" fi else skip "reading /proc/sys/fs/inotify/* (needs Linux; this is $OS)" skip "the 'default is not 8192' point (needs Linux)" fi # ------------------------------- 2. inotify watch exhaustion, if the tools exist if [ "$OS" = "Linux" ] && command -v inotifywait >/dev/null 2>&1; then mkdir -p "$WORK/tree" i=0; while [ $i -lt 64 ]; do mkdir -p "$WORK/tree/d$i"; i=$((i+1)); done if inotifywait -q -r -t 1 -e create "$WORK/tree" >/dev/null 2>&1; then pass "inotifywait established recursive watches on a 64-directory tree" else rc=$? if [ $rc -eq 2 ]; then pass "inotifywait established recursive watches and timed out with no events (exit 2, as documented)" else skip "inotifywait recursive watch (exit $rc - inotify may be unavailable in this container)" fi fi else skip "inotifywait behaviour (needs Linux with inotify-tools installed)" fi # ------------------- 3. the sysctl form the article recommends is the persistent one if [ "$OS" = "Linux" ]; then if [ -d /etc/sysctl.d ]; then pass "/etc/sysctl.d exists - the persistent form the article recommends is available here" else skip "/etc/sysctl.d (not present on this system; use /etc/sysctl.conf instead)" fi # Never write to /proc or /etc from a verification script; just show the read. echo "# (this script never writes to /proc or /etc - read-only by design)" else skip "/etc/sysctl.d persistence check (needs Linux)" fi # --------- 4. THE ANALOGUE, executed anywhere python3 has kqueue (BSD/macOS) ------ # Same class of failure as max_user_watches exhaustion: a recursive watcher silently # covers a fraction of the tree once a per-process resource cap is hit. if command -v python3 >/dev/null 2>&1; then cat > "$WORK/kq.py" <<'PY' import os, sys, resource try: import select kqf = select.kqueue except (ImportError, AttributeError): print("NOKQUEUE"); sys.exit(0) root = sys.argv[1] dirs = [dp for dp, dn, fn in os.walk(root)] kq = kqf() fds = []; err = None for d in dirs: try: fd = os.open(d, os.O_RDONLY) except OSError as e: err = e; break fds.append(fd) kq.control([select.kevent(fd, select.KQ_FILTER_VNODE, select.KQ_EV_ADD | select.KQ_EV_ENABLE | select.KQ_EV_CLEAR, select.KQ_NOTE_WRITE | select.KQ_NOTE_DELETE)], 0, 0) for fd in fds: os.close(fd) kq.close() print("DIRS=%d WATCHED=%d ERR=%s" % (len(dirs), len(fds), "yes" if err else "no")) PY # Build a tree with more directories than the descriptor limit we will impose. mkdir -p "$WORK/big" i=0; while [ $i -lt 400 ]; do mkdir -p "$WORK/big/d$i"; i=$((i+1)); done out=$( ulimit -n 128 2>/dev/null; python3 "$WORK/kq.py" "$WORK/big" 2>&1 ) if printf '%s' "$out" | grep -q NOKQUEUE; then skip "kqueue watcher analogue (this python3 has no select.kqueue - Linux build)" elif printf '%s' "$out" | grep -q "ERR=yes"; then d=$(printf '%s' "$out" | sed 's/.*DIRS=\([0-9]*\).*/\1/') w=$(printf '%s' "$out" | sed 's/.*WATCHED=\([0-9]*\).*/\1/') pass "kqueue watcher covered $w of $d directories before running out of descriptors, and kept running - the same silent partial coverage inotify has at max_user_watches" else skip "kqueue descriptor-exhaustion analogue (the descriptor limit could not be lowered here, so the whole tree fit)" fi else skip "kqueue watcher analogue (needs python3)" fi # ----------- 5. the shell-loop parsing bug the article calls out, run for real ---- # A space in the WATCHED PATH shifts every field of `read path action file`. shifted=$(echo "/srv/my data/ MODIFY report.txt" | { read -r p a f; echo "$a"; }) if [ "$shifted" = "data/" ]; then pass "a space in the watched path shifts the columns: the 'event type' field held [$shifted], not MODIFY" else fail "expected the action field to hold 'data/'; got [$shifted]" fi # Trailing whitespace on the last field is stripped even when nothing shifts. trimmed=$(printf '%s\n' '/tmp/ MODIFY trailing ' | { read -r p a f; printf '[%s]' "$f"; }) if [ "$trimmed" = "[trailing]" ]; then pass "trailing whitespace in the reported name is silently stripped by read: $trimmed" else skip "trailing-whitespace stripping (this shell reported $trimmed)" fi # Without -r, backslashes in the name are eaten. noR=$(printf '%s\n' '/tmp/ MODIFY back\slash.txt' | { read p a f; printf '%s' "$f"; }) withR=$(printf '%s\n' '/tmp/ MODIFY back\slash.txt' | { read -r p a f; printf '%s' "$f"; }) if [ "$noR" = "backslash.txt" ] && [ "$withR" = 'back\slash.txt' ]; then pass "read without -r turned back\\slash.txt into [$noR]; read -r kept [$withR]" else fail "backslash handling: without -r got [$noR], with -r got [$withR]" fi # ------------------------------------------------------ article content guards --- ART="" for cand in "$(dirname "$0")/inotify-watch-file-system-events.html" ./inotify-watch-file-system-events.html; do [ -f "$cand" ] && { ART=$cand; break; } done if [ -n "$ART" ]; then grep -q "1048576" "$ART" && cpass "article states the kernel clamp upper bound" \ || { echo "FAIL kernel clamp missing from article"; FAIL=$((FAIL+1)); } grep -q "no information about the user or process" "$ART" && cpass "article quotes inotify(7) on the missing attribution" \ || { echo "FAIL attribution quote missing"; FAIL=$((FAIL+1)); } grep -q "the event queue can overflow" "$ART" && cpass "article quotes inotify(7) on queue overflow" \ || { echo "FAIL overflow quote missing"; FAIL=$((FAIL+1)); } grep -q "sysctl.d" "$ART" && cpass "article gives the persistent sysctl form" \ || { echo "FAIL persistent sysctl form missing"; FAIL=$((FAIL+1)); } grep -q "cited, not executed" "$ART" && cpass "article states plainly that it is cited, not executed" \ || { echo "FAIL the cited-not-executed disclosure is missing"; FAIL=$((FAIL+1)); } else skip "article self-checks (inotify-watch-file-system-events.html not next to this script)" fi echo echo "PASS=$PASS FAIL=$FAIL SKIP=$SKIP CONTENT=$CONTENT" [ "$FAIL" -eq 0 ] || exit 1 exit 0