2015-12-05 04:22:19 +05:30
|
|
|
# Copyright (c) 2012-2015 The OpenRC Authors.
|
|
|
|
# See the Authors file at the top-level directory of this distribution and
|
|
|
|
# https://github.com/OpenRC/openrc/blob/master/AUTHORS
|
|
|
|
#
|
|
|
|
# This file is part of OpenRC. It is subject to the license terms in
|
|
|
|
# the LICENSE file found in the top-level directory of this
|
|
|
|
# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
|
|
|
|
# This file may not be copied, modified, propagated, or distributed
|
|
|
|
# except according to the terms contained in the LICENSE file.
|
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()
|
|
|
|
{
|
2017-09-14 21:25:06 +05:30
|
|
|
local OIFS name dir result
|
2013-02-04 03:31:16 +05:30
|
|
|
[ -n "$1" ] || return 0
|
|
|
|
OIFS="$IFS"
|
|
|
|
IFS=":"
|
2017-09-14 21:25:06 +05:30
|
|
|
while read -r _ name dir; do
|
2013-02-04 03:31:16 +05:30
|
|
|
[ "$name" = "$1" ] && result="$dir"
|
|
|
|
done < /proc/1/cgroup
|
|
|
|
IFS="$OIFS"
|
2017-09-14 21:25:06 +05:30
|
|
|
printf "%s" "${result}"
|
2013-02-04 03:31:16 +05:30
|
|
|
}
|
|
|
|
|
2013-04-16 11:22:33 +05:30
|
|
|
cgroup_get_pids()
|
|
|
|
{
|
2017-09-14 21:25:06 +05:30
|
|
|
local cgroup_procs p pids
|
|
|
|
cgroup_procs="$(cgroup2_find_path)"
|
|
|
|
[ -n "${cgroup_procs}" ] &&
|
|
|
|
cgroup_procs="${cgroup_procs}/${RC_SVCNAME}/cgroup.procs" ||
|
|
|
|
cgroup_procs="/sys/fs/cgroup/openrc/${RC_SVCNAME}/tasks"
|
|
|
|
[ -f "${cgroup_procs}" ] || return 0
|
|
|
|
while read -r p; do
|
|
|
|
[ "$p" -eq $$ ] || pids="${pids} ${p}"
|
|
|
|
done < "${cgroup_procs}"
|
|
|
|
printf "%s" "${pids}"
|
|
|
|
return 0
|
2013-04-16 11:22:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
cgroup_running()
|
|
|
|
{
|
2017-09-14 21:25:06 +05:30
|
|
|
[ -d "/sys/fs/cgroup/unified/${RC_SVCNAME}" ] ||
|
|
|
|
[ -d "/sys/fs/cgroup/${RC_SVCNAME}" ] ||
|
|
|
|
[ -d "/sys/fs/cgroup/openrc/${RC_SVCNAME}" ]
|
2013-04-16 11:22:33 +05:30
|
|
|
}
|
|
|
|
|
2013-02-18 02:44:06 +05:30
|
|
|
cgroup_set_values()
|
2013-02-04 03:31:16 +05:30
|
|
|
{
|
2017-09-14 21:25:06 +05:30
|
|
|
[ -n "$1" ] && [ -n "$2" ] && [ -d "/sys/fs/cgroup/$1" ] || return 0
|
2013-02-18 02:44:06 +05:30
|
|
|
|
2017-09-14 21:25:06 +05:30
|
|
|
local controller h
|
|
|
|
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
|
2017-09-14 21:25:06 +05:30
|
|
|
while [ -n "$1" ] && [ "$controller" != "cpuacct" ]; do
|
2013-02-18 02:44:06 +05:30
|
|
|
case "$1" in
|
|
|
|
$controller.*)
|
2018-10-13 03:46:23 +05:30
|
|
|
if [ -n "${name}" ] && [ -w "${cgroup}/${name}" ] &&
|
2017-09-14 21:25:06 +05:30
|
|
|
[ -n "${val}" ]; then
|
2013-02-18 02:44:06 +05:30
|
|
|
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
|
2017-09-14 21:25:06 +05:30
|
|
|
if [ -n "${name}" ] && [ -w "${cgroup}/${name}" ] && [ -n "${val}" ]; then
|
2013-02-18 02:44:06 +05:30
|
|
|
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
|
|
|
|
|
2016-09-14 21:38:48 +05:30
|
|
|
if [ -w "$cgroup/tasks" ]; then
|
2013-02-18 02:44:06 +05:30
|
|
|
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
|
2016-09-14 21:38:48 +05:30
|
|
|
[ -w "${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"
|
2016-09-14 21:38:48 +05:30
|
|
|
[ -w "$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
|
|
|
|
2017-09-14 21:08:10 +05:30
|
|
|
cgroup2_find_path()
|
|
|
|
{
|
2017-09-17 03:16:42 +05:30
|
|
|
if grep -qw cgroup2 /proc/filesystems; then
|
|
|
|
case "${rc_cgroup_mode:-hybrid}" in
|
|
|
|
hybrid) printf "/sys/fs/cgroup/unified" ;;
|
|
|
|
unified) printf "/sys/fs/cgroup" ;;
|
2017-09-14 21:08:10 +05:30
|
|
|
esac
|
2017-09-17 03:16:42 +05:30
|
|
|
fi
|
2017-09-14 21:08:10 +05:30
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroup2_remove()
|
|
|
|
{
|
2017-09-14 21:25:06 +05:30
|
|
|
local cgroup_path rc_cgroup_path
|
|
|
|
cgroup_path="$(cgroup2_find_path)"
|
2017-09-14 21:08:10 +05:30
|
|
|
[ -z "${cgroup_path}" ] && return 0
|
|
|
|
rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}"
|
|
|
|
[ ! -d "${rc_cgroup_path}" ] ||
|
|
|
|
[ ! -e "${rc_cgroup_path}"/cgroup.events ] &&
|
|
|
|
return 0
|
|
|
|
grep -qx "$$" "${rc_cgroup_path}/cgroup.procs" &&
|
2017-09-29 23:20:05 +05:30
|
|
|
printf "%d" 0 > "${cgroup_path}/cgroup.procs"
|
2017-09-14 21:08:10 +05:30
|
|
|
local key populated vvalue
|
2017-09-14 21:25:06 +05:30
|
|
|
while read -r key value; do
|
2017-09-14 21:08:10 +05:30
|
|
|
case "${key}" in
|
|
|
|
populated) populated=${value} ;;
|
|
|
|
*) ;;
|
|
|
|
esac
|
|
|
|
done < "${rc_cgroup_path}/cgroup.events"
|
|
|
|
[ "${populated}" = 1 ] && return 0
|
|
|
|
rmdir "${rc_cgroup_path}"
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroup2_set_limits()
|
|
|
|
{
|
2017-09-14 21:25:06 +05:30
|
|
|
local cgroup_path
|
|
|
|
cgroup_path="$(cgroup2_find_path)"
|
2017-09-17 03:16:42 +05:30
|
|
|
[ -d "${cgroup_path}" ] || return 0
|
2017-09-14 21:08:10 +05:30
|
|
|
rc_cgroup_path="${cgroup_path}/${RC_SVCNAME}"
|
|
|
|
[ ! -d "${rc_cgroup_path}" ] && mkdir "${rc_cgroup_path}"
|
2018-03-12 08:10:57 +05:30
|
|
|
[ -f "${rc_cgroup_path}"/cgroup.procs ] &&
|
|
|
|
printf 0 > "${rc_cgroup_path}"/cgroup.procs
|
|
|
|
[ -z "${rc_cgroup_settings}" ] && return 0
|
|
|
|
echo "${rc_cgroup_settings}" | while read -r key value; do
|
|
|
|
[ -z "${key}" ] && continue
|
|
|
|
[ -z "${value}" ] && continue
|
|
|
|
[ ! -f "${rc_cgroup_path}/${key}" ] && continue
|
|
|
|
veinfo "${RC_SVCNAME}: cgroups: setting ${key} to ${value}"
|
|
|
|
printf "%s\n" "${value}" > "${rc_cgroup_path}/${key}"
|
2017-09-14 21:08:10 +05:30
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
2017-09-14 21:14:52 +05:30
|
|
|
|
|
|
|
cgroup_cleanup()
|
|
|
|
{
|
|
|
|
cgroup_running || return 0
|
|
|
|
ebegin "starting cgroups cleanup"
|
2017-10-26 07:49:15 +05:30
|
|
|
local pids loops=0
|
2017-09-14 21:25:06 +05:30
|
|
|
pids="$(cgroup_get_pids)"
|
|
|
|
if [ -n "${pids}" ]; then
|
2017-09-15 23:57:49 +05:30
|
|
|
kill -s CONT ${pids} 2> /dev/null
|
2017-10-26 07:49:15 +05:30
|
|
|
kill -s "${stopsig:-TERM}" ${pids} 2> /dev/null
|
2017-09-14 22:10:26 +05:30
|
|
|
yesno "${rc_send_sighup:-no}" &&
|
2017-09-15 23:57:49 +05:30
|
|
|
kill -s HUP ${pids} 2> /dev/null
|
2017-10-26 07:49:15 +05:30
|
|
|
kill -s "${stopsig:-TERM}" ${pids} 2> /dev/null
|
|
|
|
while [ -n "$(cgroup_get_pids)" ] &&
|
|
|
|
[ "${loops}" -lt "${rc_timeout_stopsec:-90}" ]; do
|
|
|
|
loops=$((loops+1))
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
pids="$(cgroup_get_pids)"
|
|
|
|
[ -n "${pids}" ] && yesno "${rc_send_sigkill:-yes}" &&
|
2017-09-15 23:57:49 +05:30
|
|
|
kill -s KILL ${pids} 2> /dev/null
|
2017-09-14 21:25:06 +05:30
|
|
|
fi
|
2017-09-16 00:52:34 +05:30
|
|
|
cgroup2_remove
|
2017-09-16 00:12:50 +05:30
|
|
|
[ -z "$(cgroup_get_pids)" ]
|
|
|
|
eend $? "Unable to stop all processes"
|
|
|
|
return 0
|
2017-09-14 21:14:52 +05:30
|
|
|
}
|