2013-02-20 04:22:49 +05:30
|
|
|
#!@SHELL@
|
2013-02-04 03:31:16 +05:30
|
|
|
# Copyright (c) 2012 Alexander Vershilov <qnikst@gentoo.org>
|
|
|
|
# Released under the 2-clause BSD license.
|
2015-04-13 21:45:58 +05:30
|
|
|
|
2013-04-16 11:22:33 +05:30
|
|
|
extra_stopped_commands="${extra_stopped_commands} cgroup_cleanup"
|
2015-01-13 02:07:10 +05:30
|
|
|
description_cgroup_cleanup="Kill all processes in the cgroup"
|
2013-02-04 03:31:16 +05:30
|
|
|
|
|
|
|
cgroup_find_path()
|
|
|
|
{
|
|
|
|
local OIFS n name dir result
|
|
|
|
[ -n "$1" ] || return 0
|
|
|
|
OIFS="$IFS"
|
|
|
|
IFS=":"
|
|
|
|
while read n name dir; do
|
|
|
|
[ "$name" = "$1" ] && result="$dir"
|
|
|
|
done < /proc/1/cgroup
|
|
|
|
IFS="$OIFS"
|
|
|
|
echo $result
|
|
|
|
}
|
|
|
|
|
2013-04-16 11:22:33 +05:30
|
|
|
cgroup_get_pids()
|
|
|
|
{
|
|
|
|
local p
|
|
|
|
pids=
|
|
|
|
while read p; do
|
2013-05-25 19:07:56 +05:30
|
|
|
[ $p -eq $$ ] || pids="${pids} ${p}"
|
2013-04-16 11:22:33 +05:30
|
|
|
done < /sys/fs/cgroup/openrc/${RC_SVCNAME}/tasks
|
|
|
|
[ -n "$pids" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroup_running()
|
|
|
|
{
|
|
|
|
[ -d "/sys/fs/cgroup/openrc/${RC_SVCNAME}" ]
|
|
|
|
}
|
|
|
|
|
2013-02-18 02:44:06 +05:30
|
|
|
cgroup_set_values()
|
2013-02-04 03:31:16 +05:30
|
|
|
{
|
2013-02-18 02:44:06 +05:30
|
|
|
[ -n "$1" -a -n "$2" -a -d "/sys/fs/cgroup/$1" ] || return 0
|
|
|
|
|
|
|
|
local controller="$1" h=$(cgroup_find_path "$1")
|
2013-02-04 03:31:16 +05:30
|
|
|
cgroup="/sys/fs/cgroup/${1}${h}openrc_${RC_SVCNAME}"
|
2013-02-18 02:44:06 +05:30
|
|
|
[ -d "$cgroup" ] || mkdir -p "$cgroup"
|
2013-02-04 03:31:16 +05:30
|
|
|
|
2013-02-18 02:44:06 +05:30
|
|
|
set -- $2
|
|
|
|
local name val
|
|
|
|
while [ -n "$1" -a "$controller" != "cpuacct" ]; do
|
|
|
|
case "$1" in
|
|
|
|
$controller.*)
|
|
|
|
if [ -n "$name" -a -f "$cgroup/$name" -a -n "$val" ]; then
|
|
|
|
veinfo "$RC_SVCNAME: Setting $cgroup/$name to $val"
|
2015-04-13 21:45:58 +05:30
|
|
|
printf "%s" "$val" > "$cgroup/$name"
|
2013-02-18 02:44:06 +05:30
|
|
|
fi
|
|
|
|
name=$1
|
|
|
|
val=
|
|
|
|
;;
|
|
|
|
*)
|
2015-10-06 22:29:55 +05:30
|
|
|
[ -n "$val" ] &&
|
|
|
|
val="$val $1" ||
|
|
|
|
val="$1"
|
2013-02-18 02:44:06 +05:30
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
if [ -n "$name" -a -f "$cgroup/$name" -a -n "$val" ]; then
|
|
|
|
veinfo "$RC_SVCNAME: Setting $cgroup/$name to $val"
|
2015-04-13 21:45:58 +05:30
|
|
|
printf "%s" "$val" > "$cgroup/$name"
|
2013-02-18 02:44:06 +05:30
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f "$cgroup/tasks" ]; then
|
|
|
|
veinfo "$RC_SVCNAME: adding to $cgroup/tasks"
|
2015-04-13 21:45:58 +05:30
|
|
|
printf "%d" 0 > "$cgroup/tasks"
|
2013-02-18 02:44:06 +05:30
|
|
|
fi
|
2013-02-04 03:31:16 +05:30
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2013-06-25 21:20:13 +05:30
|
|
|
cgroup_add_service()
|
2013-02-04 03:31:16 +05:30
|
|
|
{
|
2013-06-30 18:02:30 +05:30
|
|
|
# relocate starting process to the top of the cgroup
|
|
|
|
# it prevents from unwanted inheriting of the user
|
|
|
|
# cgroups. But may lead to a problems where that inheriting
|
|
|
|
# is needed.
|
|
|
|
for d in /sys/fs/cgroup/* ; do
|
2015-04-13 21:45:58 +05:30
|
|
|
[ -f "${d}"/tasks ] && printf "%d" 0 > "${d}"/tasks
|
2013-06-30 18:02:30 +05:30
|
|
|
done
|
|
|
|
|
2013-02-04 03:31:16 +05:30
|
|
|
openrc_cgroup=/sys/fs/cgroup/openrc
|
2013-02-18 02:44:06 +05:30
|
|
|
if [ -d "$openrc_cgroup" ]; then
|
|
|
|
cgroup="$openrc_cgroup/$RC_SVCNAME"
|
|
|
|
mkdir -p "$cgroup"
|
2015-04-13 21:45:58 +05:30
|
|
|
[ -f "$cgroup/tasks" ] && printf "%d" 0 > "$cgroup/tasks"
|
2013-02-04 03:31:16 +05:30
|
|
|
fi
|
2013-06-25 21:20:13 +05:30
|
|
|
}
|
2013-02-04 03:31:16 +05:30
|
|
|
|
2013-06-25 21:20:13 +05:30
|
|
|
cgroup_set_limits()
|
|
|
|
{
|
2013-02-18 02:44:06 +05:30
|
|
|
local blkio="${rc_cgroup_blkio:-$RC_CGROUP_BLKIO}"
|
|
|
|
[ -n "$blkio" ] && cgroup_set_values blkio "$blkio"
|
|
|
|
|
|
|
|
local cpu="${rc_cgroup_cpu:-$RC_CGROUP_CPU}"
|
|
|
|
[ -n "$cpu" ] && cgroup_set_values cpu "$cpu"
|
|
|
|
|
|
|
|
local cpuacct="${rc_cgroup_cpuacct:-$RC_CGROUP_CPUACCT}"
|
|
|
|
[ -n "$cpuacct" ] && cgroup_set_values cpuacct "$cpuacct"
|
|
|
|
|
|
|
|
local cpuset="${rc_cgroup_cpuset:-$RC_CGROUP_cpuset}"
|
|
|
|
[ -n "$cpuset" ] && cgroup_set_values cpuset "$cpuset"
|
|
|
|
|
|
|
|
local devices="${rc_cgroup_devices:-$RC_CGROUP_DEVICES}"
|
|
|
|
[ -n "$devices" ] && cgroup_set_values devices "$devices"
|
|
|
|
|
2015-10-07 01:32:28 +05:30
|
|
|
local hugetlb="${rc_cgroup_hugetlb:-$RC_CGROUP_HUGETLB}"
|
|
|
|
[ -n "$hugetlb" ] && cgroup_set_values hugetlb "$hugetlb"
|
|
|
|
|
2013-02-18 02:44:06 +05:30
|
|
|
local memory="${rc_cgroup_memory:-$RC_CGROUP_MEMORY}"
|
|
|
|
[ -n "$memory" ] && cgroup_set_values memory "$memory"
|
|
|
|
|
2015-10-07 01:32:28 +05:30
|
|
|
local net_cls="${rc_cgroup_net_cls:-$RC_CGROUP_NET_CLS}"
|
|
|
|
[ -n "$net_cls" ] && cgroup_set_values net_cls "$net_cls"
|
|
|
|
|
2013-02-18 02:44:06 +05:30
|
|
|
local net_prio="${rc_cgroup_net_prio:-$RC_CGROUP_NET_PRIO}"
|
|
|
|
[ -n "$net_prio" ] && cgroup_set_values net_prio "$net_prio"
|
2013-02-04 03:31:16 +05:30
|
|
|
|
2015-10-07 01:32:28 +05:30
|
|
|
local pids="${rc_cgroup_pids:-$RC_CGROUP_PIDS}"
|
|
|
|
[ -n "$pids" ] && cgroup_set_values pids "$pids"
|
|
|
|
|
2013-02-04 03:31:16 +05:30
|
|
|
return 0
|
|
|
|
}
|
2013-04-16 11:22:33 +05:30
|
|
|
|
|
|
|
cgroup_cleanup()
|
|
|
|
{
|
2013-09-26 06:35:41 +05:30
|
|
|
cgroup_running || return 0
|
2013-04-16 11:22:33 +05:30
|
|
|
ebegin "starting cgroups cleanup"
|
|
|
|
for sig in TERM QUIT INT; do
|
|
|
|
cgroup_get_pids || { eend 0 "finished" ; return 0 ; }
|
|
|
|
for i in 0 1; do
|
|
|
|
kill -s $sig $pids
|
|
|
|
for j in 0 1 2; do
|
|
|
|
cgroup_get_pids || { eend 0 "finished" ; return 0 ; }
|
|
|
|
sleep 1
|
|
|
|
done
|
2013-05-25 19:07:56 +05:30
|
|
|
done 2>/dev/null
|
2013-04-16 11:22:33 +05:30
|
|
|
done
|
|
|
|
cgroup_get_pids || { eend 0 "finished" ; return 0; }
|
|
|
|
kill -9 $pids
|
|
|
|
eend $(cgroup_running && echo 1 || echo 0) "fail to stop all processes"
|
|
|
|
}
|