[projects/rc-scripts/lxc] filter_chroot: filter also by pid namespace

glen glen at pld-linux.org
Wed Nov 20 11:18:08 CET 2013


commit 1c1b41b15f72f2d167cfde5abac1b9b696fb3292
Author: Elan Ruusamäe <glen at delfi.ee>
Date:   Wed Nov 20 12:18:01 2013 +0200

    filter_chroot: filter also by pid namespace

 lib/functions | 33 ++++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)
---
diff --git a/lib/functions b/lib/functions
index 58efafe..f7af7de 100644
--- a/lib/functions
+++ b/lib/functions
@@ -493,14 +493,23 @@ checkpid() {
 # (note: some processes like named are chrooted but run outside chroot)
 # - do nothing inside vserver
 filter_chroot() {
+	# filter by pid namespace if such dir exists for current process
+	# we do filter in containers as stacked containers are possible with LXC
+	if [ -d /proc/$$/ns ]; then
+		local pids
+		pids=$(filter_ns "$@") && set -- "$pids"
+	fi
+
 	if is_yes "$VSERVER"; then
 		echo $@
 		return
 	fi
+
 	if [ $# -lt 1 -o ! -d /proc/1 ]; then
 		echo $@
 		return
 	fi
+
 	local root_dir good_pids="" good_add_pid
 	for root_pid in $@; do
 		root_dir=$(resolvesymlink /proc/${root_pid}/root)
@@ -519,6 +528,24 @@ filter_chroot() {
 	echo $good_pids
 }
 
+# similar to filter_chroot, but filter based on /proc/PID/ns/pid value
+filter_ns() {
+	local cur_ns=$(resolvesymlink /proc/$$/ns/pid)
+	[ "$cur_ns" ] || return 1
+
+	local pid ns pids=""
+	for pid in "$@"; do
+		ns=$(resolvesymlink /proc/$pid/ns/pid)
+		# add if matches current pid namespace
+		# also add if empty ns found (for dead pids?) FIXME
+		if [ -z "$ns" -o "$ns" = "$cur_ns" ]; then
+			pids="$pids $pid"
+		fi
+	done
+	echo $pids
+	return 0
+}
+
 # Usage:
 # run_cmd Message command_to_run
 # run_cmd -a Message command_to_run
@@ -932,7 +959,7 @@ pidofproc() {
 
 	# Next try "pidof"
 	[ -z "$pid" ] && pidof -o $$ -o $PPID -o %PPID -x "$1"
-	pid=$(filter_chroot "$pid")
+	pid=$(filter_chroot $pid)
 	echo $pid
 }
 
@@ -964,7 +991,7 @@ status() {
 	else
 		pid=$(pidof -o $$ -o $PPID -o %PPID -x $daemon)
 	fi
-	pid=$(filter_chroot "$pid")
+	pid=$(filter_chroot $pid)
 
 	if [ "$pid" ]; then
 		cpuset_msg="..."
@@ -983,7 +1010,7 @@ status() {
 	local base=${daemon##*/}
 	if [ -z "$pidfile" -a -f /var/run/${base}.pid ]; then
 		read pid < /var/run/${base}.pid
-		pid=$(filter_chroot "$pid")
+		pid=$(filter_chroot $pid)
 		if [ "$pid" ]; then
 			nls "%s dead but pid file (%s) exists" "$subsys" /var/run/${base}.pid
 			return 1
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/rc-scripts.git/commitdiff/1c1b41b15f72f2d167cfde5abac1b9b696fb3292



More information about the pld-cvs-commit mailing list