#! /bin/sh
#
# Webcompiler Helper Script
# Relentlessly checks & kills innocents Pawn compiler processes!
#
# Copyright (C) 2007-2010 NiLuJe  <niluje@ak-team.com>
#
# $Id: webcomp_reaper.sh 6077 2010-04-20 22:26:49Z NiLuJe $
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
##

##
### The PHP script should have passed 2 args to us:
### $1: PID of the innocent process to check
### $2: Timeout (In seconds) after which we should rip appart the poor process if it's still running
#

if (( $# != 2 )) ; then
	## Meep, not enough args!
	exit 1
else
	MyPID="$1"
	MyTimeOut="$2"
fi

##
### Loop for $MyTimeOut seconds, check if $MyPID exists every seconds
### If $MyPID is no longer running, exit nicely,
### if it's still there after $MyTimeOut seconds, shred it to pieces without a second thought!
#

for (( i = 0; i < $MyTimeOut; i++ )) ; do
	## Check if our little friend is alive, continue to loop if it's still there, exits if it's not.
	if [[ -d "/proc/${MyPID}" ]] ; then
		sleep 1
		continue
	else
		exit 0
	fi
done

##
### Time's Out! Time to die!
#

kill -KILL $MyPID
exit 9
