1#!/bin/bash
2
3# Copyright (c) International Business Machines  Corp., 2008
4# Author: Matt Helsley <matthltc@us.ibm.com>
5#
6# This library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# This library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this library; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19#
20
21#
22# This bash script tests freezer code by starting a long subshell process.
23# The subshell process sleeps and then freezes the control group it is a
24# part of. We then thaw the subshell process. We expect the unthawed subshell
25# process to need cleanup afterwards (allows us to test successfull thawing).
26#
27
28. "${CGROUPS_TESTROOT}/libcgroup_freezer"
29SETS_DEFAULTS="${TCID=freeze_self_thaw.sh} ${TST_COUNT=1} ${TST_TOTAL=1}"
30declare -r TCID
31declare -r TST_COUNT
32declare -r TST_TOTAL
33export TCID TST_COUNT TST_TOTAL
34
35# We replace the normal sample process with a process which sleeps, issues
36# the freeze command, and then sleeps some more. Little does it know that it
37# will be freezing the cgroup it's been assigned to.
38function sleep_freeze_self_sleep()
39{
40	( export sample_proc=$! ; \
41		add_sample_proc_to_cgroup ; \
42		tst_resm TINFO " $sample_proc freezing itself" ; \
43		"${CG_FILE_WRITE}" "${FREEZE}" > freezer.state ; \
44		tst_resm TINFO " $sample_proc thawed successfully" ; \
45		exec sleep $sample_sleep ) &
46	local rc=$?
47	export sample_proc=$!
48
49	return $rc
50}
51
52running_cgroup_test
53mount_freezer && {
54make_sample_cgroup && {
55assert_cgroup_freezer_state "THAWED" \
56		"ERROR: cgroup freezer started in non-THAWED state" && {
57
58sleep_freeze_self_sleep && {
59
60while /bin/true ; do
61	trap 'break' ERR
62	tst_resm TINFO " Waiting for $sample_proc to freeze itself"
63
64	# WAIT until FROZEN (see FREEZE self above)
65	wait_until_frozen
66	assert_sample_proc_is_frozen
67
68	# THAW
69	issue_thaw_cmd
70	wait_until_thawed
71	assert_sample_proc_exists
72	assert_sample_proc_not_frozen
73
74	result=$FINISHED
75	break
76done
77trap '' ERR
78cleanup_cgroup_test
79tst_resm TINFO " Cleaning up $0"
80
81# We need to stop the sample process.
82kill_sample_proc ; }
83# no inverse op needed for assert
84}
85rm_sample_cgroup ; }
86umount_freezer ; }
87
88# Failsafe cleanup
89cleanup_freezer || /bin/true
90
91exit $result
92