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 46 fi 47fi 48 49. "${CGROUPS_TESTROOT}/libcgroup_freezer" 50SETS_DEFAULTS="${TCID=vfork_freeze.sh} ${TST_COUNT=1} ${TST_TOTAL=1}" 51declare -r TCID 52declare -r TST_COUNT 53declare -r TST_TOTAL 54export TCID TST_COUNT TST_TOTAL 55 56TMPDIR=${TMPDIR:=/tmp} 57TMPLOG="$TMPDIR/${0##*/}.$$.txt" 58 59# We replace the normal sample process with a process which uses vfork to 60# create new processes. The vfork'ed processes then sleep, causing the 61# parent process ($sample_proc) to enter the TASK_UNINTERRUPTIBLE state 62# for the duration of the sleep. 63function vfork_sleep() 64{ 65 vfork -s$sample_sleep 1 -f "$TMPLOG" & 66 local rc=$? 67 export vfork_proc=$! 68 read sample_proc < "$TMPLOG" 69 rm -f "$TMPLOG" 70 export sample_proc 71 72 return $rc 73} 74 75running_cgroup_test 76mount_freezer && { 77make_sample_cgroup && { 78assert_cgroup_freezer_state "THAWED" \ 79 "ERROR: cgroup freezer started in non-THAWED state" && { 80 81vfork_sleep && { 82 83while [ 1 ] ; do 84 trap 'break' ERR 85 86 add_sample_proc_to_cgroup 87 "${CG_FILE_WRITE}" $vfork_proc >> tasks # should add to the same cgroup as above 88 89 issue_freeze_cmd 90 wait_until_frozen 91 assert_sample_proc_is_frozen 92 assert_task_is_frozen $vfork_proc 93 94 issue_thaw_cmd 95 wait_until_thawed 96 assert_sample_proc_not_frozen 97 assert_task_not_frozen $vfork_proc 98 99 result=$FINISHED 100 break 101done 102trap '' ERR 103cleanup_cgroup_test 104tst_resm TINFO " Cleaning up $0" 105 106# We need to kill the sample process(es). 107kill_sample_proc ; export sample_proc=$vfork_proc ; kill_sample_proc ; } 108 109# no inverse op needed for assert 110} 111 112rm_sample_cgroup ; } 113umount_freezer ; } 114 115rm -f "$TMPLOG" 116 117# Failsafe cleanup 118cleanup_freezer || /bin/true 119 120exit $result 121