#!/usr/bin/perl
#########################################################################
#
# LOMAC - Low Water-Mark Mandatory Access Control for Linux
# Copyright (c) 1999, 2000, 2001, 2002 Networks Associates
# Technology, Inc.  All rights reserved.
# 
# This file is part of LOMAC.
#
# LOMAC is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# LOMAC 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 LOMAC; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# testsignal1
#
# This script tests LOMAC's handling of the following system calls:
#    sys_kill(individual process)
#
# This should be run at level 2.
#
#########################################################################

use lomacpred;

$lowfilename = "/tmp/testsignal1lowfile";

#
# Ensure that LOMAC is running and that we are running at level 2.
#
&ProcAtLevelPred( 2 ) || die "ERROR: You must run this test at level 2.\n";
print( "*** Testing sys_kill(individual process). ***\n" );

unlink( $lowfilename );

#
# create a low-level file
#
print( "Creating low-level file:\n" );
open( LOWFILE, "> $lowfilename" ) || die "can't create $lowfilename: $!\n";
print LOWFILE "garbage\n";
close LOWFILE;
if( FileAtLevelPred( $lowfilename, 1 ) ) {
    print( "\tOK.\n" );
} else {
    print( "\tFAILED.\n" );
    exit( -1 );
}

#
# Test high process signalling high and low targets
#

# fork off high target
if( 0 == ( $hightarget_pid = fork ) ) {
    # high target
    setpgrp( 0, $$ );  # Put high target into its own new pgrp.

    while( 1 ) {
	sleep( 10 );
    }
    
} # high target

# fork off low target
if( 0 == ( $lowtarget_pid = fork ) ) {
    # low target
    setpgrp( 0, $$ );  # Put low target into its own new pgrp.

    #demote this target
    open( LOWFILE, $lowfilename );
    $garbage = <LOWFILE>;
 
    while( 1 ) {
	sleep( 10 );
    }
} # low target

#
# Fork off test client
#

if( 0 == ( $client_pid = fork ) ) {
    # client

    setpgrp( 0, $$ );  # Put client into its own new pgrp.

    # this program has a race condition - it may signal before
    # the target processes are ready.  Very annoying.  This sleep
    # helps, but doesn't eliminate the problem.
    sleep(2);

    print( "Test high process signalling high target:\n" );
    if( 1 == kill( 'TERM', $hightarget_pid ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test high process signalling low target:\n" );
    if( 1 == kill( 'TERM', $lowtarget_pid ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }
    exit( 0 );
}

#
# Overall parent program.
#

# wait for client to exit.
$ret_val = waitpid( $client_pid, 0 );

# clean up
kill 'TERM', $hightarget_pid;
kill 'TERM', $lowtarget_pid;


#
# Test low process signalling high and low targets
#

# fork off high target
if( 0 == ( $hightarget_pid = fork ) ) {
    # high target
    setpgrp( 0, $$ );  # Put high target into its own new pgrp.

    while( 1 ) {
	sleep( 10 );
    }
    
} # high target

# fork off low target
if( 0 == ( $lowtarget_pid = fork ) ) {
    # low target
    setpgrp( 0, $$ );  # Put low target into its own new pgrp.

    #demote this target
    open( LOWFILE, $lowfilename );
    $garbage = <LOWFILE>;
 
    while( 1 ) {
	sleep( 10 );
    }
} # low target

#
# Fork off test client
#

if( 0 == ( $client_pid = fork ) ) {
    # client

    setpgrp( 0, $$ );  # Put client into its own new pgrp.

    # this program has a race condition - it may signal before
    # the target processes are ready.  Very annoying.  This sleep
    # helps, but doesn't eliminate the problem.
    sleep(2);
    
    # demote the test client
    open( LOWFILE, $lowfilename );
    $garbage = <LOWFILE>;

    print( "Test low process signalling low target:\n" );
    if( 1 == kill( 'TERM', $lowtarget_pid ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    print( "Test low process signalling high target:\n" );
    if( 0 == kill( 'TERM', $hightarget_pid ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    exit( 0 );
}

# wait for client to exit.
$ret_val = waitpid( $client_pid, 0 );

# clean up
kill 'TERM', $hightarget_pid;
kill 'TERM', $lowtarget_pid;

unlink( $lowfilename );

exit( $ret_val );
