From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id E389C1381F3 for ; Thu, 25 Apr 2013 22:02:10 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 91087E0A6B; Thu, 25 Apr 2013 22:02:07 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 2001BE0A6B for ; Thu, 25 Apr 2013 22:02:07 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 8821133DED2 for ; Thu, 25 Apr 2013 22:02:05 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 1A740E408C for ; Thu, 25 Apr 2013 22:02:04 +0000 (UTC) From: "William Hubbs" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "William Hubbs" Message-ID: <1366927159.c984506537fbb8a3adefd2d57dd4dc831eeb79c3.WilliamH@OpenRC> Subject: [gentoo-commits] proj/openrc:master commit in: sh/, etc/ X-VCS-Repository: proj/openrc X-VCS-Files: etc/rc.conf.Linux sh/rc-cgroup.sh.in sh/runscript.sh.in X-VCS-Directories: sh/ etc/ X-VCS-Committer: WilliamH X-VCS-Committer-Name: William Hubbs X-VCS-Revision: c984506537fbb8a3adefd2d57dd4dc831eeb79c3 X-VCS-Branch: master Date: Thu, 25 Apr 2013 22:02:04 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: f43d830b-fbea-4897-bb7c-233fa71c0f26 X-Archives-Hash: f29ef5a5fb6b5edeb5b14ee482d9ec62 commit: c984506537fbb8a3adefd2d57dd4dc831eeb79c3 Author: Alexander Vershilov gmail com> AuthorDate: Tue Apr 16 05:52:33 2013 +0000 Commit: William Hubbs gentoo org> CommitDate: Thu Apr 25 21:59:19 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/openrc.git;a=commit;h=c9845065 add cgroup cleanup support This adds the ability to kill all processes within a service's cgroup when that service is stopped or restarted. --- etc/rc.conf.Linux | 10 ++++++++++ sh/rc-cgroup.sh.in | 35 +++++++++++++++++++++++++++++++++++ sh/runscript.sh.in | 2 ++ 3 files changed, 47 insertions(+), 0 deletions(-) diff --git a/etc/rc.conf.Linux b/etc/rc.conf.Linux index 4fb48ae..79bd971 100644 --- a/etc/rc.conf.Linux +++ b/etc/rc.conf.Linux @@ -66,3 +66,13 @@ rc_tty_number=12 # Set the net_prio controller settings for this service. #rc_cgroup_net_prio="" + +# Set this to YES if yu want all of the processes in a service's cgroup +# killed when the service is stopped or restarted. +# This should not be set globally because it kills all of the service's +# child processes, and most of the time this is undesirable. Please set +# it in /etc/conf.d/. +# To perform this cleanup manually for a stopped service, you can +# execute cgroup_cleanup with /etc/init.d/ cgroup_cleanup or +# rc-service cgroup_cleanup. +# rc_cgroup_cleanup="NO" diff --git a/sh/rc-cgroup.sh.in b/sh/rc-cgroup.sh.in index 449c1d3..c057fc8 100644 --- a/sh/rc-cgroup.sh.in +++ b/sh/rc-cgroup.sh.in @@ -1,6 +1,7 @@ #!@SHELL@ # Copyright (c) 2012 Alexander Vershilov # Released under the 2-clause BSD license. +extra_stopped_commands="${extra_stopped_commands} cgroup_cleanup" cgroup_find_path() { @@ -15,6 +16,21 @@ cgroup_find_path() echo $result } +cgroup_get_pids() +{ + local p + pids= + while read p; do + [ $p -eq $$ ] || $pids="${pids} ${p}" + done < /sys/fs/cgroup/openrc/${RC_SVCNAME}/tasks + [ -n "$pids" ] +} + +cgroup_running() +{ + [ -d "/sys/fs/cgroup/openrc/${RC_SVCNAME}" ] +} + cgroup_set_values() { [ -n "$1" -a -n "$2" -a -d "/sys/fs/cgroup/$1" ] || return 0 @@ -86,3 +102,22 @@ cgroup_set_limits() return 0 } + +cgroup_cleanup() +{ + yesno "${rc_cgroup_cleanup:-no}" && cgroup_running || return 0 + 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 + done + done + cgroup_get_pids || { eend 0 "finished" ; return 0; } + kill -9 $pids + eend $(cgroup_running && echo 1 || echo 0) "fail to stop all processes" +} diff --git a/sh/runscript.sh.in b/sh/runscript.sh.in index c9a3745..dcddd9b 100644 --- a/sh/runscript.sh.in +++ b/sh/runscript.sh.in @@ -302,6 +302,8 @@ while [ -n "$1" ]; do then "$1"_post || exit $? fi + [ "$(command -v cgroup_cleanup)" = "cgroup_cleanup" ] && \ + cgroup_cleanup shift continue 2 else