#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use CondorTest;
use CondorUtils;

$cmd = 'lib_procapi_pidtracking-byenv.cmd';
$testdesc =  'We cleanup correctly after our kids. - vanilla U';
$testname = "lib_procapi_pidtracking-byenv";
$datafile = "lib_procapi_pidtracking-byenv.data";
$kiddatafile = "lib_procapi_pidtracking-byenv.data.kids";
$worked = "yes";

$execute = sub
{
	CondorTest::debug(
		"Goood, job is running so we'll start the timer....\n", 1);
	CondorTest::RegisterTimed($testname, $timed, 300); # give it 5 minutes
};

$timed = sub
{
	print scalar localtime() . "\n";
	CondorTest::debug(
		"lib_procapi_pidtracking-byenv HUNG !!!!!!!!!!!!!!!!!!\n", 1);
	exit(1);
};

$ExitSuccess = sub
{
	my %info = @_;
	my $line;
	my $pidline;
	my @pids;
	my @kidpids;

	open(PIN, "<$datafile") or die "Could not open data file '$datafile': $?";
	while(<PIN>)
	{
		CondorUtils::fullchomp();
		$line = $_;
		if( $line =~ /\s*Relationship:\s*(\d+)\s*created\s*(\d+).*$/ )
		{
			CondorTest::debug( "Process $1 created $2\n",1);
			push @pids, $2;
		}
		CondorTest::debug( "$line\n",1);
	}
	close(PIN);
	open(PIN, "<$kiddatafile") or
		die "Could not open data file '$kiddatafile': $?";
	while(<PIN>)
	{
		CondorUtils::fullchomp();
		$line = $_;
		if( $line =~ /\s*Relationship:\s*(\d+)\s*child\s+created\s*(\d+).*$/ )
		{
			CondorTest::debug( "Process $1 created $2\n",1);
			push @kidpids, $2;
		}
		CondorTest::debug( "$line\n",1);
	}
	close(PIN);
	my $ps = `which ps`;
	CondorUtils::fullchomp( $ps );
	my $psflags = "-ef";
	open( PULL, "$ps $psflags | wc -l 2>&1 |" )
		or die "'$ps $psflags' pid failed: $!\n";
	$_ = <PULL>;
	my $ps_lines = 0;
	if ( /^(\d+)/ ) {
		$ps_lines = $1;
	}
	if($ps_lines == 0) {
		CondorTest::debug("Wrong ps options '$psflags', trying 'ax'\n",1);
		$psflags = "ax";
	}
	else {
		CondorTest::debug("ps delivered $ps_lines too check!\n",1);
	}
	close( PULL );

	open(PULL, "$ps $psflags 2>&1 |") || die "'$ps $psflags' failed: $!\n";
	while(<PULL>)
	{
		my $thispid;
		CondorUtils::fullchomp();
		$pidline = $_;
		if( $psflags eq "ax") # with pid first
		{
			if($pidline =~ /\s*(\d+)\s*.*$/ )
			{
				$thispid = $1;
				CondorTest::debug(
					"Checking againt list of removed pids --$thispid--\n", 1);
				foreach my $pidcheck (@pids)
				{
					if($thispid eq $pidcheck)
					{
						CondorTest::debug(
							"Test fails because pid $pidcheck should have ".
							"been removed\n", 1);
						$worked = "no";
						runcmd("kill -9 $pidcheck");
					}
				}
				foreach my $pidcheck (@kidpids)
				{
					if($thispid eq $pidcheck)
					{
						CondorTest::debug(
							"Test fails because pid $pidcheck should have ".
							"been removed\n", 1);
						$worked = "no";
						runcmd("kill -9 $pidcheck");
					}
				}
			}
		}
		else # ps -ef with uid first
		{
			if($pidline =~ /\s*[A-Za-z]+\s*(\d+).*$/ )
			{
				$thispid = $1;
				CondorTest::debug(
					"Checking againt list of removed pids --$thispid--\n", 1);
				foreach my $pidcheck (@pids)
				{
					if($thispid eq $pidcheck)
					{
						CondorTest::debug(
							"Test fails because pid $pidcheck should have ".
							"been removed\n", 1);
						$worked = "no";
						runcmd("kill -9 $pidcheck");
					}
				}
				foreach my $pidcheck (@kidpids)
				{
					if($thispid eq $pidcheck)
					{
						CondorTest::debug(
							"Test fails because pid $pidcheck should have ".
							"been removed\n", 1);
						$worked = "no";
						runcmd("kill -9 $pidcheck");
					}
				}
			}
		}
		#print "$pidline\n";
	}
	close(PULL);

	if($worked eq "no")
	{
		CondorTest::debug("Test failed......\n",1);
		exit(1);
	}
	else
	{
		CondorTest::debug("Test Worked......\n",1);
	}

};


# before the test let's throw some weird crap into the environment

CondorTest::RegisterExitedSuccess( $testname, $ExitSuccess );
CondorTest::RegisterExecute($testname, $execute);

#empty local environment and add only a few things that way......

if( CondorTest::RunTest($testname, $cmd, 0) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}

