Home | History | Annotate | Download | only in pids
      1 #!/bin/sh
      2 
      3 ################################################################################
      4 ##                                                                            ##
      5 ## Copyright (c) 2015 SUSE                                                    ##
      6 ##                                                                            ##
      7 ## This program is free software;  you can redistribute it and#or modify      ##
      8 ## it under the terms of the GNU General Public License as published by       ##
      9 ## the Free Software Foundation; either version 2 of the License, or          ##
     10 ## (at your option) any later version.                                        ##
     11 ##                                                                            ##
     12 ## This program is distributed in the hope that it will be useful, but        ##
     13 ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
     14 ## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
     15 ## for more details.                                                          ##
     16 ##                                                                            ##
     17 ## You should have received a copy of the GNU General Public License          ##
     18 ## along with this program;  if not, write to the Free Software               ##
     19 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301   ##
     20 ## USA                                                                        ##
     21 ##                                                                            ##
     22 ## Author: Cedric Hnyda <chnyda (at] suse.com>                                     ##
     23 ##                                                                            ##
     24 ################################################################################
     25 
     26 # Usage
     27 # ./pids.sh caseno max
     28 #
     29 TST_CLEANUP=cleanup
     30 TST_SETUP=setup
     31 TST_TESTFUNC=do_test
     32 TST_POS_ARGS=2
     33 TST_USAGE=usage
     34 TST_NEEDS_ROOT=1
     35 
     36 . tst_test.sh
     37 
     38 caseno=$1
     39 max=$2
     40 mounted=1
     41 
     42 usage()
     43 {
     44 	cat << EOF
     45 usage: $0 caseno max_processes
     46 
     47 caseno        - testcase number from interval 1-5
     48 max_processes - maximal number of processes to attach
     49                 (applicable to testcase 1-4)
     50 OPTIONS
     51 EOF
     52 }
     53 
     54 cleanup()
     55 {
     56 	killall -9 pids_task2 >/dev/null 2>&1
     57 
     58 	tst_res TINFO "removing created directories"
     59 	rmdir $testpath
     60 	if [ "$mounted" -ne "1" ]; then
     61 		tst_res TINFO "Umounting pids"
     62 		umount $mount_point
     63 		rmdir $mount_point
     64 	fi
     65 }
     66 
     67 setup()
     68 {
     69 	exist=`grep -w pids /proc/cgroups | cut -f1`;
     70 	if [ "$exist" = "" ]; then
     71 		tst_brk TCONF NULL "pids not supported"
     72 	fi
     73 
     74 	mount_point=`grep -w pids /proc/mounts | cut -f 2 | cut -d " " -f2`
     75 
     76 	if [ "$mount_point" = "" ]; then
     77 		mounted=0
     78 		mount_point=/dev/cgroup
     79 	fi
     80 
     81 	testpath=$mount_point/ltp_$TCID
     82 
     83 	if [ "$mounted" -eq "0" ]; then
     84 		ROD mkdir -p $mount_point
     85 		ROD mount -t cgroup -o pids none $mount_point
     86 	fi
     87 	ROD mkdir -p $testpath
     88 }
     89 
     90 start_pids_tasks2()
     91 {
     92 	nb=$1
     93 	for i in `seq 1 $nb`; do
     94 		pids_task2 &
     95 		echo $! > $testpath/tasks
     96 	done
     97 
     98 	if [ $(cat "$testpath/tasks" | wc -l) -ne $nb ]; then
     99 		tst_brk TBROK "failed to attach process"
    100 	fi
    101 }
    102 
    103 stop_pids_tasks()
    104 {
    105 	for i in `cat $testpath/tasks`; do
    106 		ROD kill -9 $i
    107 		wait $i
    108 	done
    109 }
    110 
    111 case1()
    112 {
    113 	start_pids_tasks2 $max
    114 
    115 	# should return 0 because there is no limit
    116 	pids_task1 "$testpath/tasks"
    117 	ret=$?
    118 
    119 	if [ "$ret" -eq "2" ]; then
    120 		tst_res TFAIL "fork failed unexpectedly"
    121 	elif [ "$ret" -eq "0" ]; then
    122 		tst_res TPASS "fork didn't fail"
    123 	else
    124 		tst_res TBROK "pids_task1 failed"
    125 	fi
    126 
    127 	stop_pids_tasks
    128 }
    129 
    130 case2()
    131 {
    132 	tmp=$((max - 1))
    133 	tst_res TINFO "limit the number of pid to $max"
    134 	ROD echo $max \> $testpath/pids.max
    135 
    136 	start_pids_tasks2 $tmp
    137 
    138 	# should return 2 because the limit of pids is reached
    139 	pids_task1 "$testpath/tasks"
    140 	ret=$?
    141 
    142 	if [ "$ret" -eq "2" ]; then
    143 		tst_res TPASS "fork failed as expected"
    144 	elif [ "$ret" -eq "0" ]; then
    145 		tst_res TFAIL "fork didn't fail despite the limit"
    146 	else
    147 		tst_res TBROK "pids_task1 failed"
    148 	fi
    149 
    150 	stop_pids_tasks
    151 }
    152 
    153 case3()
    154 {
    155 	lim=$((max + 2))
    156 	tst_res TINFO "limit the number of avalaible pid to $lim"
    157 	ROD echo $lim \> $testpath/pids.max
    158 
    159 	start_pids_tasks2 $max
    160 
    161 	pids_task1 "$testpath/tasks"
    162 	ret=$?
    163 
    164 	if [ "$ret" -eq "2" ]; then
    165 		tst_res TFAIL "fork failed unexpectedly"
    166 	elif [ "$ret" -eq "0" ]; then
    167 		tst_res TPASS "fork worked as expected"
    168 	else
    169 		tst_res TBROK "pids_task1 failed"
    170 	fi
    171 
    172 	stop_pids_tasks
    173 }
    174 
    175 case4()
    176 {
    177 	tst_res TINFO "limit the number of avalaible pid to 0"
    178 	ROD echo 0 \> $testpath/pids.max
    179 
    180 	start_pids_tasks2 $max
    181 
    182 	tst_res TPASS "all process were attached"
    183 
    184 	stop_pids_tasks
    185 }
    186 
    187 case5()
    188 {
    189 	tst_res TINFO "try to limit the number of avalaible pid to -1"
    190 	echo -1 > $testpath/pids.max
    191 
    192 	if [ "$?" -eq "0" ]; then
    193 		tst_res TFAIL "managed to set the limit to -1"
    194 	else
    195 		tst_res TPASS "didn't manage to set the limit to -1"
    196 	fi
    197 }
    198 
    199 do_test()
    200 {
    201 	tst_res TINFO "Running testcase $caseno with $max processes"
    202 	case$caseno
    203 }
    204 
    205 tst_run
    206