#!/bin/sh
# DF-1326 pass-3 rig: build the KLD that dispatches the REAL production
# mpr ioctl handler, plus the unprivileged trigger.
#
# Run as root on the DragonFly guest (all paths in-guest):
#   sh build.sh
set -e
cd "$(dirname "$0")"

# kernel module: dispatch rig (makefile is 'Makefile')
make SYSDIR=/usr/src/sys MACHINE=x86_64 MACHINE_ARCH=x86_64

# userspace trigger (build for unprivileged use)
cc -O -Wall -o trigger df1326rig_trigger.c
chmod 755 trigger

# load + prime: resolve the REAL handler from the RUNNING kernel binary
kldload ./df1326rig.ko
ADDR=$(nm /boot/kernel/kernel | awk '/ t mpr_ioctl_devsw$/{print $1}')
echo "mpr_ioctl_devsw = 0x$ADDR"
sysctl -w kern.df1326rig_target=0x$ADDR
ls -l /dev/df1326rig
echo "READY: as an unprivileged operator-group user run:"
echo "   su operator -c '/root/rig/trigger probe'"
