Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 541792 | Differences between
and this patch

Collapse All | Expand All

(-)sh/rc-cgroup.sh.in (-80 / +64 lines)
Lines 1-56 Link Here
1
#!@SHELL@
1
#!@SHELL@
2
# Copyright (c) 2012 Alexander Vershilov <qnikst@gentoo.org>
2
# Copyright (c) 2012 Alexander Vershilov <qnikst@gentoo.org>
3
# Released under the 2-clause BSD license.
3
# Released under the 2-clause BSD license.
4
extra_stopped_commands="${extra_stopped_commands} cgroup_cleanup"
4
extra_stopped_commands="${extra_stopped_commands} cgroup_cleanup"
5
description_cgroup_cleanup="Kill all processes in the cgroup"
5
description_cgroup_cleanup="Kill all processes in the cgroup"
6
6
7
CG_NAME="openrc"
8
CG_ROOT="/sys/fs/cgroup"
9
CG_PATH="$CG_ROOT/$CG_NAME"
10
7
cgroup_find_path()
11
cgroup_find_path()
8
{
12
{
9
	local OIFS n name dir result
13
	local ifs name dir DIR
10
	[ -n "$1" ] || return 0
14
	[ -n "$1" ] || return 0
11
	OIFS="$IFS"
15
	ifs="$IFS" FS=":"
12
	IFS=":"
13
	while read n name dir; do
16
	while read n name dir; do
14
		[ "$name" = "$1" ] && result="$dir"
17
		[ "$name" = "$1" ] && DIR="$dir"
15
	done < /proc/1/cgroup
18
	done < /proc/1/cgroup
16
	IFS="$OIFS"
19
	IFS="$ifs"
17
	echo $result
20
	echo "$DIR"
18
}
21
}
19
22
20
cgroup_get_pids()
23
cgroup_get_pids()
21
{
24
{
22
	local p
25
	local p
23
	pids=
26
	PIDS=
24
	while read p; do
27
	while read p; do
25
		[ $p -eq $$ ] || pids="${pids} ${p}"
28
		[ $p -eq $$ ] || PIDS="$PIDS $p"
26
	done < /sys/fs/cgroup/openrc/${RC_SVCNAME}/tasks
29
	done < $CG_PATH/$RC_SVCNAME/tasks
27
	[ -n "$pids" ]
30
	[ -n "$PIDS" ]
28
}
31
}
29
32
30
cgroup_running()
33
cgroup_get_state()
31
{
34
{
32
	[ -d "/sys/fs/cgroup/openrc/${RC_SVCNAME}" ]
35
	[ -d "$CG_PATH/$RC_SVCNAME" ]
33
}
36
}
34
37
35
cgroup_set_values()
38
cgroup_set_values()
36
{
39
{
37
	[ -n "$1" -a -n "$2" -a -d "/sys/fs/cgroup/$1" ] || return 0
40
	[ -n "$1" -a -n "$2" -a -d "$CG_ROOT/$1" ] || return 0
38
41
39
	local controller="$1" h=$(cgroup_find_path "$1")
42
	local ctrl group head
40
	cgroup="/sys/fs/cgroup/${1}${h}openrc_${RC_SVCNAME}"
43
	ctrl="$1" head=$(cgroup_find_path "$1")
41
	[ -d "$cgroup" ] || mkdir -p "$cgroup"
44
	group="$CG_ROOT/${1}${head}${CG_NAME}_$RC_SVCNAME"
45
	[ -d "$group" ] || mkdir -p "$group"
42
46
43
	set -- $2
47
	set -- $2
44
	local name val
48
	local name val
45
	while [ -n "$1" -a "$controller" != "cpuacct" ]; do
49
	while [ -n "$1" -a "$ctrl" != "cpuacct" ]; do
46
		case "$1" in
50
		case "$1" in
47
			$controller.*)
51
			$ctrl.*)
48
				if [ -n "$name" -a -f "$cgroup/$name" -a -n "$val" ]; then
52
				if [ -n "$name" -a -f "$group/$name" -a -n "$val" ]; then
49
					veinfo "$RC_SVCNAME: Setting $cgroup/$name to $val"
53
					veinfo "$RC_SVCNAME: Setting $group/$name to $val"
50
					echo $val > "$cgroup/$name"
54
					echo "$val" > "$group/$name"
51
				fi
55
				fi
52
				name=$1
56
				name=$1 val=
53
				val=
54
				;;
57
				;;
55
			*)
58
			*)
56
				val="$val $1"
59
				val="$val $1"
Lines 58-136 Link Here
58
		esac
61
		esac
59
		shift
62
		shift
60
	done
63
	done
61
	if [ -n "$name" -a -f "$cgroup/$name" -a -n "$val" ]; then
64
	if [ -n "$name" -a -f "$group/$name" -a -n "$val" ]; then
62
		veinfo "$RC_SVCNAME: Setting $cgroup/$name to $val"
65
		veinfo "$RC_SVCNAME: Setting $group/$name to $val"
63
		echo $val > "$cgroup/$name"
66
		echo "$val" > "$group/$name"
64
	fi
67
	fi
65
68
	if [ -f "$group/tasks" ]; then
66
	if [ -f "$cgroup/tasks" ]; then
69
		veinfo "$RC_SVCNAME: Adding to $group/tasks"
67
		veinfo "$RC_SVCNAME: adding to $cgroup/tasks"
70
		echo $$ > "$group/tasks"
68
		echo 0 > "$cgroup/tasks"
69
	fi
71
	fi
70
71
	return 0
72
	return 0
72
}
73
}
73
74
74
cgroup_add_service()
75
cgroup_add_service()
75
{
76
{
76
    # relocate starting process to the top of the cgroup
77
	# FIXME:
77
    # it prevents from unwanted inheriting of the user
78
	#  Relocating process to the top of the CGroup to prevent
78
    # cgroups. But may lead to a problems where that inheriting
79
	#  unwanted inheriting user CGroup may lead to issue when
79
    # is needed.
80
	#  inheriting may be necessary.
80
	for d in /sys/fs/cgroup/* ; do
81
	if yesno ${rc_cgroup_inherit:-${RC_CGROUP_INHERIT:-Yes}}; then
81
		[ -f "${d}"/tasks ] && echo 0 > "${d}"/tasks
82
		local dir
82
	done
83
		for dir in $CG_ROOT/*; do
83
84
			[ -f "$dir"/tasks ] && echo $$ > "$dir"/tasks
84
	openrc_cgroup=/sys/fs/cgroup/openrc
85
		done
85
	if [ -d "$openrc_cgroup" ]; then
86
	fi
86
		cgroup="$openrc_cgroup/$RC_SVCNAME"
87
	if [ -d "$CG_PATH" ]; then
87
		mkdir -p "$cgroup"
88
		local group
88
		[ -f "$cgroup/tasks" ] && echo 0 > "$cgroup/tasks"
89
		group="$CG_PATH/$RC_SVCNAME"
90
		mkdir -p "$group"
91
		[ -f "$group/tasks" ] && echo $$ > "$group/tasks"
89
	fi
92
	fi
90
}
93
}
91
94
92
cgroup_set_limits()
95
cgroup_set_limits()
93
{
96
{
94
	local blkio="${rc_cgroup_blkio:-$RC_CGROUP_BLKIO}"
97
	local ctrl blkio cpu cpuacct cpuset devices memory net_prio
95
	[ -n "$blkio" ] && cgroup_set_values blkio "$blkio"
96
97
	local cpu="${rc_cgroup_cpu:-$RC_CGROUP_CPU}"
98
	[ -n "$cpu" ] && cgroup_set_values cpu "$cpu"
99
100
	local cpuacct="${rc_cgroup_cpuacct:-$RC_CGROUP_CPUACCT}"
101
	[ -n "$cpuacct" ] && cgroup_set_values cpuacct "$cpuacct"
102
103
	local cpuset="${rc_cgroup_cpuset:-$RC_CGROUP_cpuset}"
104
	[ -n "$cpuset" ] && cgroup_set_values cpuset "$cpuset"
105
106
	local devices="${rc_cgroup_devices:-$RC_CGROUP_DEVICES}"
107
	[ -n "$devices" ] && cgroup_set_values devices "$devices"
108
109
	local memory="${rc_cgroup_memory:-$RC_CGROUP_MEMORY}"
110
	[ -n "$memory" ] && cgroup_set_values memory "$memory"
111
112
	local net_prio="${rc_cgroup_net_prio:-$RC_CGROUP_NET_PRIO}"
113
	[ -n "$net_prio" ] && cgroup_set_values net_prio "$net_prio"
114
98
99
	for ctrl in blkio:BLKIO cpu:CPU cpuacct:CPUACCT cpuset:CPUSET \
100
		devices:DEVICES memory:MEMORY net_prio:NET_PRIO; do
101
		eval ${ctrl%:*}=\"\${rc_cgroup_${ctrl%:*}:-\$RC_CGROUP_${ctrl#*:}}\"
102
		eval test -n \"\$${ctrl%:*}\" && cgroup_set_values "${ctrl%:*}" "\$${ctrl%:*}"
103
	done
115
	return 0
104
	return 0
116
}
105
}
117
106
118
cgroup_cleanup()
107
cgroup_cleanup()
119
{
108
{
120
	cgroup_running || return 0
109
	cgroup_get_state || return 0
121
	ebegin "starting cgroups cleanup"
110
	local CGROUP_GET_PIDS SIG
122
	for sig in TERM QUIT INT; do
111
	CGROUP_GET_PIDS='cgroup_get_pids || { eend 0; return 0; };'
123
		cgroup_get_pids || { eend 0 "finished" ; return 0 ; }
112
124
		for i in 0 1; do
113
	ebegin "Cleaning up CGroup"
125
			kill -s $sig $pids
114
	for SIG in TERM QUIT INT; do
126
			for j in 0 1 2; do
115
		eval $CGROUP_GET_PIDS
127
				cgroup_get_pids || { eend 0 "finished" ; return 0 ; }
116
		kill -$SIG $PIDS
128
				sleep 1
129
			done
130
		done 2>/dev/null
131
	done
117
	done
132
	cgroup_get_pids || { eend 0 "finished" ; return 0; }
118
	eval $CGROUP_GET_PIDS
133
	kill -9 $pids
119
	kill -TERM $PIDS
134
	eend $(cgroup_running && echo 1 || echo 0) "fail to stop all processes"
135
}
120
}
136

Return to bug 541792