Home | History | Annotate | Download | only in scripts
      1 #!/bin/sh
      2 #
      3 # SPDX-License-Identifier: Apache-2.0
      4 #
      5 # Copyright (C) 2015, ARM Limited and contributors.
      6 #
      7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
      8 # not use this file except in compliance with the License.
      9 # You may obtain a copy of the License at
     10 #
     11 # http://www.apache.org/licenses/LICENSE-2.0
     12 #
     13 # Unless required by applicable law or agreed to in writing, software
     14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 # See the License for the specific language governing permissions and
     17 # limitations under the License.
     18 #
     19 
     20 # Control groups mount point
     21 CGMOUNT=${CGMOUNT:-/sys/fs/cgroup/devlib_*}
     22 # The control group we want to run into
     23 CGP=${1}
     24 # The command to run
     25 CMD=${2}
     26 
     27 # Execution under root CGgroup
     28 if [ "x/" == "x$CGP" ]; then
     29 
     30   find $CGMOUNT -type d -maxdepth 0 | \
     31   while read CGPATH; do
     32     # Move this shell into that control group
     33     echo $$ > $CGPATH/cgroup.procs
     34     echo "Moving task into root CGroup ($CGPATH)"
     35   done
     36 
     37 # Execution under specified CGroup
     38 else
     39 
     40   # Check if the required CGroup exists
     41   find $CGMOUNT -type d -mindepth 1 | \
     42   grep $CGP &>/dev/null
     43   if [ $? -ne 0 ]; then
     44     echo "ERROR: could not find any $CGP cgroup under $CGMOUNT"
     45     exit 1
     46   fi
     47 
     48   find $CGMOUNT -type d -mindepth 1 | \
     49   grep $CGP | \
     50   while read CGPATH; do
     51       # Move this shell into that control group
     52       echo $$ > $CGPATH/cgroup.procs
     53       echo "Moving task into $CGPATH"
     54   done
     55 
     56 fi
     57 
     58 # Execute the command
     59 $CMD
     60