Subject: [PATCH 2/3] rc.d: completely stop the vkernels

From: Rumko <[email-masked]>
Date: Sun, 10 Oct 2010 19:12:33 +0200

When stopping the vkernel, after a set timeout if the vkernel is still
alive, send a SIGKILL.
Also redirect STDIN when starting the vkernels.
---
 etc/defaults/rc.conf |    3 +++
 etc/rc.d/vkernel     |   28 +++++++++++++++++++++++++---
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/etc/defaults/rc.conf b/etc/defaults/rc.conf
index 683c4c6..52d66c4 100644
--- a/etc/defaults/rc.conf
+++ b/etc/defaults/rc.conf
@@ -458,6 +458,7 @@ jail_sysvipc_allow="NO"	# Allow SystemV IPC use from within a jail
 vkernel_enable="NO"	# Set to YES to enable starting of vkernels
 vkernel_list=""		# Space separated list of names of vkernels
 #vkernel_bin="/boot/kernel.VKERNEL"	# Default path to the vkernel binary
+#vkernel_kill_timeout="45"	# Default timeout before a SIGKILL is issued
 
 #
 # Create an entry for each vkernel specified in vkernel_list
@@ -475,6 +476,8 @@ vkernel_list=""		# Space separated list of names of vkernels
 	# Optional: path to the console log file
 #vkernel_example_flags="-U"
 	# Optional: aditional flags to start the vkernel with
+#vkernel_example_kill_timeout="45"
+	# Optional: timeout before SIGKILL is issued when stopping the vkernel
 
 ##############################################################
 ### Define source_rc_confs, the mechanism used by /etc/rc.* ##
diff --git a/etc/rc.d/vkernel b/etc/rc.d/vkernel
index 35c8a43..e3a0316 100644
--- a/etc/rc.d/vkernel
+++ b/etc/rc.d/vkernel
@@ -74,7 +74,7 @@ vkernel_start()
 		# Configure optional flags
 		eval flags=\"\${vkernel_${_vkernel}_flags}\"
 
-		eval "daemon ${bin} ${memsize} ${rootimgs} ${ifaces} ${flags} -p ${pidfile} >>${logfile} 2>&1"
+		eval "daemon ${bin} ${memsize} ${rootimgs} ${ifaces} ${flags} -p ${pidfile} >>${logfile} 2>&1 </dev/null"
 		echo -n " ${_vkernel}"
 	done
 	echo '.'
@@ -90,8 +90,30 @@ vkernel_stop()
 		fi
 		if [ -z "${pid}" ]; then
 			warn "vkernel '${_vkernel}' not running?"
-		else
-			eval kill -TERM ${pid}
+			continue
+		fi
+		eval kill -TERM ${pid}
+
+		eval _kill_timeout=\"\${vkernel_${_vkernel}_kill_timeout}\"
+		if [ -z "${_kill_timeout}" ]; then
+			eval _kill_timeout=\"\${vkernel_kill_timeout}\"
+			if [ -z "${_kill_timeout}" ]; then
+				_kill_timeout=60
+			fi
+		fi
+
+		# Check that the vkernel ended properly
+		cnt=1
+		eval temp_pid=`ps axwwp ${pid} | grep -wE "^[ ]+${pid}" | grep -v grep | awk '{print $1}'`
+		while [ ! -z "${temp_pid}" -a "${cnt}" -lt "${_kill_timeout}" ]; do
+			eval temp_pid=`ps axwwp ${pid} | grep -wE "^[ ]+${pid}" | grep -v grep | awk '{print $1}'`
+			cnt=`expr $cnt + 1`
+			sleep 1
+		done
+		
+		if [ ! -z "${temp_pid}" ]; then
+			eval kill -KILL ${pid}
+			warn "'${_vkernel}' had to be forcefully killed."
 		fi
 	done
 }
-- 
1.7.0.5