#!/bin/sh
# PCP QA Test No. 723
# Exercise Linux kernel proc.psinfo.label metric
#
# Copyright (c) 2013 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"
# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

pid=$$

test $PCP_VER -ge 30805 || _notrun "No support for new proc label metric"
test $PCP_PLATFORM = linux || _notrun "Test unsupported on $PCP_PLATFORM"
test -f /proc/$pid/attr/current || _notrun "No kernel support for labels"
# for some kernels, /proc/$pid/attr/current exists, but attempts to
# access the "file" produce and Invalid argument error
#
cat /proc/$$/attr/current >/dev/null 2>&1 || _notrun "Incomplete kernel support for labels"


status=1	# failure is the default!
$sudo rm -rf $tmp.* $seq.full
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

# real QA test starts here
$sudo rm -f $seq.full
#debug# ls -l /proc/$pid/attr/current
syslabel=`cat /proc/$pid/attr/current`
echo "SYS Label for process $pid is: $syslabel" >> $seq.full

pminfo -f proc.psinfo.labels > $tmp.labels
pcplabel=`grep "^    inst \[$pid or " $tmp.labels \
	| $PCP_AWK_PROG '{ print $NF }' \
	| sed -e 's/^"//' -e 's/"$//'`
echo "PCP Label for process $pid is: $pcplabel" >> $seq.full
echo "Extracted from list:" >> $seq.full
cat $tmp.labels >> $seq.full

if [ "$pcplabel" = "$syslabel" ]
then
    echo "Security label for current process checks out"
    status=0
else
    echo "Mismatch on security labels:"
    echo "PCP Label: $pcplabel"
    echo "SYS Label: $syslabel"
    status=1
fi

exit
