#!/bin/bash
# This script runs trinity with default parameters

DURATION=300
GROUP=
SYSCALL=


P=trinity-bench
DEFAULT_VERSION=0
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--duration)
		DURATION=$2
		shift 2
		;;
	--group)
		GROUP=$2
		shift 2
		;;
	--syscall)
		SYSCALL=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

if [ "$INSTALL_FORCE" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/trinity-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/trinity-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-trinity -v ${VERSION}  || die trinity install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/trinity-${VERSION}-installed || die Failed to cd to trinity install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo trinity installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

GROUP_PARAM=
if [ "$GROUP" != "" ]; then
	GROUP_PARAM="-g $GROUP"
fi

SYSCALL_PARAM=
if [ "$SYSCALL" != "" ]; then
	SYSCALL_PARAM="-c $SYSCALL"
fi

if [ "$SYSCALL" != "" -a "$GROUP" != "" ]; then
	die Cannot specify both syscall and groups
fi

# Run trinity in the background
echo Launching trinity
cd $SHELLPACK_DATA || die "Failed to cd to $SHELLPACK_DATA"
mkdir tmp
chmod 777 tmp
TAINT=`cat /proc/sys/kernel/tainted`
STARTTIME=`date +%s`
ENDTIME=$((STARTTIME+$DURATION))
su -s /bin/bash nobody -c "$SHELLPACK_SOURCES/trinity-$VERSION-installed/trinity -q $SYSCALL_PARAM $GROUP_PARAM" 2>&1 | tee $LOGDIR_RESULTS/trinity.log &
TRINITY_PID=$!

echo -n Waiting for trinity to exit, $DURATION seconds or a kernel taint
CURRENTTIME=`date +%s`
while [ $CURRENTTIME -lt $ENDTIME ]; do
	if [ "`ps h --pid $TRINITY_PID`" = "" ]; then
		dmesg > $LOGDIR_RESULTS/dmesg.log
		cat $LOGDIR_RESULTS/trinity.log
		die Trinity died unexpectedly
	fi
	if [ "`cat /proc/sys/kernel/tainted`" != $TAINT ]; then
		cat $LOGDIR_RESULTS/trinity.log
		echo ERROR: Taint flag changed `cat /proc/sys/kernel/tainted`
		kill -9 $TRINITY_PID
		dmesg > $LOGDIR_RESULTS/dmesg.log
		die Unable to continue due to taint
	fi

	echo MARK: Approximately $((($ENDTIME - $CURRENTTIME)/60)) minutes to go
	sleep 60
	CURRENTTIME=`date +%s`
done
echo

shutdown_pid trinity $TRINITY_PID

exit $SHELLPACK_SUCCESS
#### Description trinity fuzzing tool
#### Details trinity-bench 13
