1#!/bin/sh
2
3################################################################################
4##                                                                            ##
5## Copyright (c) 2009 FUJITSU LIMITED                                         ##
6##  Author: Shi Weihua <shiwh@cn.fujitsu.com>                                 ##
7## Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>                          ##
8## Copyright (c) 2015-2016 Cyril Hrubis <chrubis@suse.cz>                     ##
9##                                                                            ##
10## This program is free software;  you can redistribute it and#or modify      ##
11## it under the terms of the GNU General Public License as published by       ##
12## the Free Software Foundation; either version 2 of the License, or          ##
13## (at your option) any later version.                                        ##
14##                                                                            ##
15## This program is distributed in the hope that it will be useful, but        ##
16## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
17## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
18## for more details.                                                          ##
19##                                                                            ##
20## You should have received a copy of the GNU General Public License          ##
21## along with this program;  if not, write to the Free Software Foundation,   ##
22## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA           ##
23##                                                                            ##
24################################################################################
25
26for arg; do
27    TCID="${TCID}_$arg"
28done
29
30. test.sh
31
32exist_subsystem()
33{
34    local subsystem="$1"
35    local exist=`grep -w $subsystem /proc/cgroups | cut -f1`
36
37    if [ -z "$exist" ]; then
38        tst_brkm TCONF "Subsystem $subsystem not supported"
39    fi
40}
41
42attach_and_check()
43{
44    local pid="$1"
45    local path="$2"
46    local task
47    shift
48
49    tst_resm TINFO "Attaching task $pid to $path"
50
51    ROD echo "$pid" \> "$path/tasks"
52
53    for task in $(cat "$path/tasks"); do
54        if [ "$task" -ne "$pid" ]; then
55            tst_resm TINFO "Unexpected pid $task in $path/tasks, expected $pid"
56            return 1
57        fi
58    done
59
60    return 0
61}
62
63create_subgroup()
64{
65    path="$1"
66
67    ROD mkdir "$path"
68
69    # cpuset.cpus and cpuset.mems must be initialized with suitable value
70    # before any pids are attached
71    if [ "$subsystem" = "cpuset" ]; then
72        if [ -e "$mount_point/cpus" ]; then
73            ROD cat "$mount_point/cpus" \> "$path/cpus"
74            ROD cat "$mount_point/mems" \> "$path/mems"
75        else
76            ROD cat "$mount_point/cpuset.cpus" \> "$path/cpuset.cpus"
77            ROD cat "$mount_point/cpuset.mems" \> "$path/cpuset.mems"
78        fi
79    fi
80}
81
82
83setup()
84{
85    tst_require_root
86    tst_require_cmds killall
87
88    if [ ! -f /proc/cgroups ]; then
89        tst_brkm TCONF "Kernel does not support for control groups"
90    fi
91
92    exist_subsystem "$subsystem"
93
94    tst_tmpdir
95    TST_CLEANUP=cleanup
96
97    mount_point=`grep -w $subsystem /proc/mounts | grep -w "cgroup" | \
98	cut -f 2 | cut -d " " -f2`
99
100    if [ -z "$mount_point" ]; then
101        try_umount=1
102        mount_point="/dev/cgroup"
103	tst_resm TINFO "Subsystem $subsystem is not mounted, mounting it at $mount_point"
104        ROD mkdir $mount_point
105        ROD mount -t cgroup -o "$subsystem" "ltp_cgroup" "$mount_point"
106    else
107	tst_resm TINFO "Subsystem $subsystem is mounted at $mount_point"
108    fi
109}
110
111cleanup()
112{
113    tst_rmdir
114
115    killall -9 cgroup_fj_proc >/dev/null 2>&1
116
117    tst_resm TINFO "Removing all ltp subgroups..."
118
119    find "$mount_point/ltp/" -depth -type d -exec rmdir '{}' \;
120
121    if [ -z "$try_umount" ]; then
122	return
123    fi
124
125    if grep -q "$mount_point" /proc/mounts; then
126        umount "$mount_point"
127    fi
128
129    if [ -e "$mount_point" ]; then
130        rmdir "$mount_point"
131    fi
132}
133