vfork_freeze.sh revision 2d5ffa60a4e03056631ca117392288b9c7d94261
1#!/bin/sh 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 process with vfork(2). 23# vfork causes the freezer to wait until the vfork call "returns" to the 24# parent. 25# 26 27# we need the vfork test binary -- ensure it's been built 28CGROUPS_TESTROOT=${CGROUPS_TESTROOT:=$(dirname "$0")} 29 30if [ ! -x "$CGROUPS_TESTROOT/vfork" ] ; then 31 32 print_make_message=1 33 34 # Maintain ease-of-use backwards compatibility so Matt doesn't want to 35 # hang me for the script change :]. 36 if type make > /dev/null ; then 37 make all && print_make_message=0 38 fi 39 40 if [ $print_make_message -eq 1 ] ; then 41 cat <<EOF 42${0##*/}: ERROR: you must run \`make all' in $CGROUPS_TESTROOT before running 43this script. 44EOF 45 exit 1 46fi 47 48. "${CGROUPS_TESTROOT}/libcgroup_freezer" 49SETS_DEFAULTS="${TCID=vfork_freeze.sh} ${TST_COUNT=1} ${TST_TOTAL=1}" 50declare -r TCID 51declare -r TST_COUNT 52declare -r TST_TOTAL 53export TCID TST_COUNT TST_TOTAL 54 55TMPDIR=${TMPDIR:=/tmp} 56 57# We replace the normal sample process with a process which uses vfork to 58# create new processes. The vfork'ed processes then sleep, causing the 59# parent process ($sample_proc) to enter the TASK_UNINTERRUPTIBLE state 60# for the duration of the sleep. 61function vfork_sleep() 62{ 63 vfork -s$sample_sleep 1 > "$TMPDIR/${0##*/}.$$.txt" & 64 local rc=$? 65 export vfork_proc=$! 66 read sample_proc < /tmp/tmp.txt 67 rm -f /tmp/tmp.txt 68 export sample_proc 69 70 return $rc 71} 72 73running_cgroup_test 74mount_freezer && { 75make_sample_cgroup && { 76assert_cgroup_freezer_state "THAWED" \ 77 "ERROR: cgroup freezer started in non-THAWED state" && { 78 79vfork_sleep && { 80 81while [ 1 ] ; do 82 trap 'break' ERR 83 84 add_sample_proc_to_cgroup 85 "${CG_FILE_WRITE}" $vfork_proc >> tasks # should add to the same cgroup as above 86 87 issue_freeze_cmd 88 wait_until_frozen 89 assert_sample_proc_is_frozen 90 assert_task_is_frozen $vfork_proc 91 92 issue_thaw_cmd 93 wait_until_thawed 94 assert_sample_proc_not_frozen 95 assert_task_not_frozen $vfork_proc 96 97 result=$FINISHED 98 break 99done 100trap '' ERR 101cleanup_cgroup_test 102tst_resm TINFO " Cleaning up $0" 103 104# We need to kill the sample process(es). 105kill_sample_proc ; export sample_proc=$vfork_proc ; kill_sample_proc ; } 106 107# no inverse op needed for assert 108} 109 110rm_sample_cgroup ; } 111umount_freezer ; } 112 113rm -f "$TMPDIR/${0##*/}.$$.txt" 114 115# Failsafe cleanup 116cleanup_freezer || /bin/true 117 118exit $result 119