github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/linux_backend/skeleton/destroy.sh (about)

     1  #!/bin/bash
     2  
     3  [ -n "$DEBUG" ] && set -o xtrace
     4  set -o nounset
     5  set -o errexit
     6  shopt -s nullglob
     7  
     8  cd $(dirname $0)
     9  
    10  source ./etc/config
    11  
    12  cgroup_path="${GARDEN_CGROUP_PATH}"
    13  
    14  if [ -f ./run/wshd.pid ]
    15  then
    16    pid=$(cat ./run/wshd.pid)
    17  
    18    # Arbitrarily pick the cpu substem to check for live tasks.
    19    cgroup_path_segment=$(cat /proc/self/cgroup | grep cpu: | cut -d ':' -f 3)
    20    path=${cgroup_path}/cpu${cgroup_path_segment}/instance-$id
    21    tasks=$path/tasks
    22  
    23    if [ -d $path ]
    24    then
    25      # Kill the container's init pid; the kernel will reap all tasks.
    26      kill -9 $pid || echo "wshd process seems to be gone already..."
    27  
    28      # Wait while there are tasks in one of the instance's cgroups.
    29      #
    30      # Even though we've technically killed the root of the pid namespace,
    31      # it can take a brief period of time for the kernel to reap.
    32      while [ -f $tasks ] && [ -n "$(cat $tasks)" ]; do
    33        sleep 0.1
    34      done
    35    fi
    36  
    37    # Done, remove pid
    38    rm -f ./run/wshd.pid
    39  
    40    # Remove cgroups
    41    for subsystem in {cpuset,cpu,cpuacct,devices,memory}
    42    do
    43      cgroup_path_segment=$(cat /proc/self/cgroup | grep ${subsystem}: | cut -d ':' -f 3)
    44      path=${cgroup_path}/${subsystem}${cgroup_path_segment}/instance-$id
    45  
    46      if [ -d $path ]
    47      then
    48        # Recursively remove all cgroup trees under (and including) the instance.
    49        #
    50        # Running another containerization tool in the container may create these,
    51        # and the parent cannot be removed until they're removed first.
    52        #
    53        # find .. -delete ensures that it processes them depth-first.
    54        find $path -type d -delete
    55      fi
    56    done
    57  
    58    exit 0
    59  fi
    60