vfork_freeze.sh 3.1 KB

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