#!/bin/sh
# DF-2803: unbounded udev event-queue growth with an initiated-but-stalled
# reader (kern_udev.c udev_event_insert has no udev_evqlen cap / backpressure).
#
# Runs as ROOT inside the DragonFly guest (after ./build.sh).
# The event SOURCE is unprivileged (user maxx churning ptys via /dev/ptmx).
sample() {
	printf '== sample: %s ==\n' "$1"
	vmstat -m | egrep -w 'propdict|prop_dictionary|propnmbr|propstng|prop_string|propkeysym|udev'
}

cd /tmp
sample baseline

# unprivileged event source: 20s of pty open/close cycles as user maxx
su -m maxx -c '/tmp/churn 20' > /tmp/df2803_churn.log 2>&1 &
CHURN=$!
# root reader: UDEVPROP getdevs (initiates) then STALLS - never reads until
# the very end, where it drains non-blockingly just to COUNT the backlog.
/tmp/harness stallcount 30 > /tmp/df2803_harness.log 2>&1 &
HARN=$!

sleep 6;  sample 't+6s: churn running, reader stalled'
sleep 8;  sample 't+14s: churn running, reader stalled'
sleep 5;  sample 't+19s: churn running, reader stalled'

wait $CHURN; cat /tmp/df2803_churn.log
wait $HARN;  cat /tmp/df2803_harness.log
sleep 3
sample 'after drain (queue empty again)'
echo DONE
