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
87    if [ ! -f /proc/cgroups ]; then
88        tst_brkm TCONF "Kernel does not support for control groups"
89    fi
90
91    exist_subsystem "$subsystem"
92
93    tst_tmpdir
94    TST_CLEANUP=cleanup
95
96    mount_point=`grep -w $subsystem /proc/mounts | grep -w "cgroup" | \
97	cut -f 2 | cut -d " " -f2`
98
99    if [ -z "$mount_point" ]; then
100        try_umount=1
101        mount_point="/dev/cgroup"
102	tst_resm TINFO "Subsystem $subsystem is not mounted, mounting it at $mount_point"
103        ROD mkdir $mount_point
104        ROD mount -t cgroup -o "$subsystem" "ltp_cgroup" "$mount_point"
105    else
106	tst_resm TINFO "Subsystem $subsystem is mounted at $mount_point"
107    fi
108}
109
110cleanup()
111{
112    tst_rmdir
113
114    killall -9 cgroup_fj_proc >/dev/null 2>&1
115
116    tst_resm TINFO "Removing all ltp subgroups..."
117
118    find "$mount_point/ltp/" -depth -type d -exec rmdir '{}' \;
119
120    if [ -z "$try_umount" ]; then
121	return
122    fi
123
124    if grep -q "$mount_point" /proc/mounts; then
125        umount "$mount_point"
126    fi
127
128    if [ -e "$mount_point" ]; then
129        rmdir "$mount_point"
130    fi
131}
132