#!/bin/bash
modulename=hide_pid

# add some randomness to the script
for _ in $(seq "$(od -An -N1 -i /dev/urandom | tr -d ' ')"); do
	sleep 0
done
sleep 200 &

pid=$!

if [ -z "$pid" ]; then
	echo "Error: pid is null"
	exit 1
fi

if ! ps -e | grep -q "$pid"; then
	echo "Process cannot be found!"
	exit 1
fi

if ! modprobe $modulename target="$pid"; then
	echo "Could not load module $modulename.ko!"
	exit 1
fi

if ps -e | grep -q "$pid"; then
	echo "Process can still be found!"
	exit 1
fi

if ! [ -d "/proc/$pid" ]; then
	echo "Process can still be found!"
	exit 1
fi

if ! jobs | grep -q Running ; then
	echo "Job has been killed!"
	exit 1
fi

# are you crashing the kernel?
ls -R /proc > /dev/null

if ! rmmod $modulename; then
	echo "Fail while unloading module"
	exit 1
fi

# are you crashing the kernel?
ls -R /proc > /dev/null

if ! ps -e | grep -q "$pid"; then
	echo "Process cannot be found after removing module!"
	exit 1
fi

if ! jobs | grep -q Running ; then
	echo "Job has been killed!"
	exit 1
fi

echo scan > /sys/kernel/debug/kmemleak
sleep 60
cat /sys/kernel/debug/kmemleak
