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@suse.com>                                     ##
23##                                                                            ##
24################################################################################
25
26# Usage
27# ./pids.sh caseno max
28#
29
30caseno=$1
31max=$2
32export TCID="pids_$1_$2"
33export TST_TOTAL=1
34mounted=1
35
36. test.sh
37
38cleanup()
39{
40	killall -9 pids_task2 >/dev/null 2>&1
41
42	tst_resm TINFO "removing created directories"
43	rmdir $testpath
44	if [ "$mounted" -ne "1" ]; then
45		tst_resm TINFO "Umounting pids"
46		umount $mount_point
47		rmdir $mount_point
48	fi
49}
50
51setup()
52{
53	tst_require_root
54
55	exist=`grep -w pids /proc/cgroups | cut -f1`;
56	if [ "$exist" = "" ]; then
57		tst_brkm TCONF NULL "pids not supported"
58	fi
59
60	mount_point=`grep -w pids /proc/mounts | cut -f 2 | cut -d " " -f2`
61
62	if [ "$mount_point" = "" ]; then
63		mounted=0
64		mount_point=/dev/cgroup
65	fi
66
67	TST_CLEANUP=cleanup
68
69	testpath=$mount_point/ltp_$TCID
70
71	if [ "$mounted" -eq "0" ]; then
72		ROD mkdir -p $mount_point
73		ROD mount -t cgroup -o pids none $mount_point
74	fi
75	ROD mkdir -p $testpath
76}
77
78start_pids_tasks2()
79{
80	nb=$1
81	for i in `seq 1 $nb`; do
82		pids_task2 &
83		echo $! > $testpath/tasks
84	done
85
86	if [ $(cat "$testpath/tasks" | wc -l) -ne $nb ]; then
87		tst_resm TBROK "failed to attach process"
88	fi
89}
90
91case1()
92{
93	start_pids_tasks2 $max
94
95	# should return 0 because there is no limit
96	pids_task1 "$testpath/tasks"
97	ret=$?
98
99	if [ "$ret" -eq "2" ]; then
100		tst_resm TFAIL "fork failed unexpectedly"
101	elif [ "$ret" -eq "0" ]; then
102		tst_resm TPASS "fork didn't fail"
103	else
104		tst_resm TBROK "pids_task1 failed"
105	fi
106}
107
108case2()
109{
110	tmp=$((max - 1))
111	tst_resm TINFO "limit the number of pid to $max"
112	ROD echo $max \> $testpath/pids.max
113
114	start_pids_tasks2 $tmp
115
116	# should return 2 because the limit of pids is reached
117	pids_task1 "$testpath/tasks"
118	ret=$?
119
120	if [ "$ret" -eq "2" ]; then
121		tst_resm TPASS "fork failed as expected"
122	elif [ "$ret" -eq "0" ]; then
123		tst_resm TFAIL "fork didn't fail despite the limit"
124	else
125		tst_resm TBROK "pids_task1 failed"
126	fi
127}
128
129case3()
130{
131	lim=$((max + 2))
132	tst_resm TINFO "limit the number of avalaible pid to $lim"
133	ROD echo $lim \> $testpath/pids.max
134
135	start_pids_tasks2 $max
136
137	pids_task1 "$testpath/tasks"
138	ret=$?
139
140	if [ "$ret" -eq "2" ]; then
141		tst_resm TFAIL "fork failed unexpectedly"
142	elif [ "$ret" -eq "0" ]; then
143		tst_resm TPASS "fork worked as expected"
144	else
145		tst_resm TBROK "pids_task1 failed"
146	fi
147}
148
149case4()
150{
151	tst_resm TINFO "limit the number of avalaible pid to 0"
152	ROD echo 0 \> $testpath/pids.max
153
154	start_pids_tasks2 $max
155
156	tst_resm TPASS "all process were attached"
157}
158
159case5()
160{
161	tst_resm TINFO "try to limit the number of avalaible pid to -1"
162	echo -1 > $testpath/pids.max
163
164	if [ "$?" -eq "0" ]; then
165		tst_resm TFAIL "managed to set the limit to -1"
166	else
167		tst_resm TPASS "didn't manage to set the limit to -1"
168	fi
169}
170
171setup
172
173if [ "$#" -ne "2" ]; then
174	tst_resm TFAIL "invalid parameters, usage: ./pids01 caseno max"
175else
176	case$caseno
177fi
178
179tst_exit
180