1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci
3f08c3bdfSopenharmony_ci# Copyright (c) International Business Machines  Corp., 2008
4f08c3bdfSopenharmony_ci# Author: Matt Helsley <matthltc@us.ibm.com>
5f08c3bdfSopenharmony_ci#
6f08c3bdfSopenharmony_ci# This library is free software; you can redistribute it and/or
7f08c3bdfSopenharmony_ci# modify it under the terms of the GNU Lesser General Public
8f08c3bdfSopenharmony_ci# License as published by the Free Software Foundation; either
9f08c3bdfSopenharmony_ci# version 2.1 of the License, or (at your option) any later version.
10f08c3bdfSopenharmony_ci#
11f08c3bdfSopenharmony_ci# This library is distributed in the hope that it will be useful,
12f08c3bdfSopenharmony_ci# but WITHOUT ANY WARRANTY; without even the implied warranty of
13f08c3bdfSopenharmony_ci# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14f08c3bdfSopenharmony_ci# Lesser General Public License for more details.
15f08c3bdfSopenharmony_ci#
16f08c3bdfSopenharmony_ci# You should have received a copy of the GNU Lesser General Public
17f08c3bdfSopenharmony_ci# License along with this library; if not, write to the Free Software
18f08c3bdfSopenharmony_ci# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19f08c3bdfSopenharmony_ci#
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#
22f08c3bdfSopenharmony_ci# This bash script tests freezer code by starting a process with vfork(2).
23f08c3bdfSopenharmony_ci# vfork causes the freezer to wait until the vfork call "returns" to the
24f08c3bdfSopenharmony_ci# parent.
25f08c3bdfSopenharmony_ci#
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci# we need the vfork test binary -- ensure it's been built
28f08c3bdfSopenharmony_ciCGROUPS_TESTROOT=${CGROUPS_TESTROOT:=$(dirname "$0")}
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ciif [ ! -x "$CGROUPS_TESTROOT/vfork" ] ; then
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ci	print_make_message=1
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	# Maintain ease-of-use backwards compatibility so Matt doesn't want to
35f08c3bdfSopenharmony_ci	# hang me for the script change :].
36f08c3bdfSopenharmony_ci	if type make > /dev/null ; then
37f08c3bdfSopenharmony_ci		make all && print_make_message=0
38f08c3bdfSopenharmony_ci	fi
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	if [ $print_make_message -eq 1 ] ; then
41f08c3bdfSopenharmony_ci		cat <<EOF
42f08c3bdfSopenharmony_ci${0##*/}: ERROR: you must run \`make all' in $CGROUPS_TESTROOT before running
43f08c3bdfSopenharmony_cithis script.
44f08c3bdfSopenharmony_ciEOF
45f08c3bdfSopenharmony_ci		exit 1
46f08c3bdfSopenharmony_ci	fi
47f08c3bdfSopenharmony_cifi
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci. "${CGROUPS_TESTROOT}/libcgroup_freezer"
50f08c3bdfSopenharmony_ciSETS_DEFAULTS="${TCID=vfork_freeze.sh} ${TST_COUNT=1} ${TST_TOTAL=1}"
51f08c3bdfSopenharmony_cideclare -r TCID
52f08c3bdfSopenharmony_cideclare -r TST_COUNT
53f08c3bdfSopenharmony_cideclare -r TST_TOTAL
54f08c3bdfSopenharmony_ciexport TCID TST_COUNT TST_TOTAL
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ciTMPDIR=${TMPDIR:=/tmp}
57f08c3bdfSopenharmony_ciTMPLOG="$TMPDIR/${0##*/}.$$.txt"
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci# We replace the normal sample process with a process which uses vfork to
60f08c3bdfSopenharmony_ci# create new processes. The vfork'ed processes then sleep, causing the
61f08c3bdfSopenharmony_ci# parent process ($sample_proc) to enter the TASK_UNINTERRUPTIBLE state
62f08c3bdfSopenharmony_ci# for the duration of the sleep.
63f08c3bdfSopenharmony_civfork_sleep()
64f08c3bdfSopenharmony_ci{
65f08c3bdfSopenharmony_ci	vfork -s$sample_sleep 1 -f "$TMPLOG" &
66f08c3bdfSopenharmony_ci	local rc=$?
67f08c3bdfSopenharmony_ci	export vfork_proc=$!
68f08c3bdfSopenharmony_ci	read sample_proc < "$TMPLOG"
69f08c3bdfSopenharmony_ci	rm -f "$TMPLOG"
70f08c3bdfSopenharmony_ci	export sample_proc
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci	return $rc
73f08c3bdfSopenharmony_ci}
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_cirunning_cgroup_test
76f08c3bdfSopenharmony_cimount_freezer && {
77f08c3bdfSopenharmony_cimake_sample_cgroup && {
78f08c3bdfSopenharmony_ciassert_cgroup_freezer_state "THAWED" \
79f08c3bdfSopenharmony_ci		"ERROR: cgroup freezer started in non-THAWED state" && {
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_civfork_sleep && {
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ciwhile [ 1 ] ; do
84f08c3bdfSopenharmony_ci	trap 'break' ERR
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	add_sample_proc_to_cgroup
87f08c3bdfSopenharmony_ci	"${CG_FILE_WRITE}" $vfork_proc >> tasks # should add to the same cgroup as above
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci	issue_freeze_cmd
90f08c3bdfSopenharmony_ci	wait_until_frozen
91f08c3bdfSopenharmony_ci	assert_sample_proc_is_frozen
92f08c3bdfSopenharmony_ci	assert_task_is_frozen $vfork_proc
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	issue_thaw_cmd
95f08c3bdfSopenharmony_ci	wait_until_thawed
96f08c3bdfSopenharmony_ci	assert_sample_proc_not_frozen
97f08c3bdfSopenharmony_ci	assert_task_not_frozen $vfork_proc
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	result=$FINISHED
100f08c3bdfSopenharmony_ci	break
101f08c3bdfSopenharmony_cidone
102f08c3bdfSopenharmony_citrap '' ERR
103f08c3bdfSopenharmony_cicleanup_cgroup_test
104f08c3bdfSopenharmony_citst_resm TINFO " Cleaning up $0"
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_ci# We need to kill the sample process(es).
107f08c3bdfSopenharmony_cikill_sample_proc ; export sample_proc=$vfork_proc ; kill_sample_proc ; }
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci# no inverse op needed for assert
110f08c3bdfSopenharmony_ci}
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_cirm_sample_cgroup ; }
113f08c3bdfSopenharmony_ciumount_freezer ; }
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_cirm -f "$TMPLOG"
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci# Failsafe cleanup
118f08c3bdfSopenharmony_cicleanup_freezer || /bin/true
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_ciexit $result
121