#!/usr/bin/perl
#########################################################################
#
# LOMAC - Low Water-Mark Mandatory Access Control for Linux 
# Copyright (C) 1999 TIS Labs at Network Associates, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.  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., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
#
# testsetpgid
#
# This script tests LOMAC's handling of the following system calls:
#    sys_setpgid()
#
# This should be run at level 2.
#
#########################################################################

use lomacpred;

#
# 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_setpgid(). ***\n" );

#
# create a low-level file
#
print( "Creating low-level file:\n" );
$lowfilename = "/tmp/testpgrplowfile";
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 );
}
    

#
# Create a child.
#
if( $child_pid = fork ) {
    #parent

    # wait for child to exit
    $ret_val = waitpid( $child_pid, 0 );

    # remove low-level file
    unlink( $lowfilename );

    exit( $ret_val );

} elsif( $child_pid == 0 ) {
    #child

    # save parent's pgrp
    $parent_pgrp = getpgrp( 0 );

    # Put child in its own new pgrp.
    print( "Test level inheritance on setpgrp:\n" );
    setpgrp( 0, $$ );                   # put child in its own pgrp
    if( ( $$ == getpgrp( 0 ) ) &&
	( &ProcAtLevelPred( 2 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    # return child to parent's pgrp.
    print( "Test pgrp change among identical levels:\n" );
    setpgrp( $$, $parent_pgrp );
    if( $parent_pgrp == getpgrp( 0 ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    # put child into its own pgrp again.
    setpgrp( 0, $$ );

    # demote child.
    print( "Demoting child:\n" );
    open( LOWFILE, "< $lowfilename" ) || die "child can't open $lowfile: $!\n";
    $garbage = <LOWFILE>;
    close( LOWFILE );
    if( &ProcAtLevelPred( 1 ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    # put child back into its parent's pgrp (should be denied).
    print( "Testing move from low to high pgrp:\n" );
    setpgrp( $$, $parent_pgrp );
    if( ( $$ == getpgrp( 0 ) ) &&
	( &ProcAtLevelPred( 1 ) ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    # try to change parent's pgrp (should be denied).
    print( "Testing low child modification of high parent's pgrp:\n" );
    setpgrp( getppid(), $$ );
    if( $parent_pgrp == getpgrp( getppid ) ) {
	print( "\tOK.\n" );
    } else {
	print( "\tFAILED.\n" );
	exit( -1 );
    }

    exit( 0 );

} else {
    die "ERROR: unable to fork\n";
}

