From uspoerlein@gmail.com  Wed Oct 18 20:01:00 2006
Return-Path: <uspoerlein@gmail.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id D648C16A4A0
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 18 Oct 2006 20:01:00 +0000 (UTC)
	(envelope-from uspoerlein@gmail.com)
Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.170])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 4874D43D6E
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 18 Oct 2006 20:00:53 +0000 (GMT)
	(envelope-from uspoerlein@gmail.com)
Received: by ug-out-1314.google.com with SMTP id m2so242412uge
        for <FreeBSD-gnats-submit@freebsd.org>; Wed, 18 Oct 2006 13:00:51 -0700 (PDT)
Received: by 10.66.216.20 with SMTP id o20mr12464215ugg;
        Wed, 18 Oct 2006 13:00:50 -0700 (PDT)
Received: from coyote.q.local ( [85.180.136.25])
        by mx.google.com with ESMTP id b23sm1442326ugd.2006.10.18.13.00.49;
        Wed, 18 Oct 2006 13:00:50 -0700 (PDT)
Message-Id: <20061018200049.GA69724@coyote.q.local>
Date: Wed, 18 Oct 2006 22:00:49 +0200
From: Ulrich Spoerlein <uspoerlein@gmail.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc: freebsd-rc@freebsd.org
Subject: rc.d/nfsd needs special _find_processes function

>Number:         104549
>Category:       conf
>Synopsis:       [patch] rc.d/nfsd needs special _find_processes function
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-rc
>State:          feedback
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Oct 18 20:10:17 GMT 2006
>Closed-Date:    
>Last-Modified:  Thu Nov 01 19:24:19 UTC 2012
>Originator:     Ulrich Spoerlein
>Release:        FreeBSD 6.2-PRERELEASE i386
>Organization:
>Environment:
>Description:
When running multiple nfsd processes, there is one master process and several slave processes.
As soon as the master process receives a SIGUSR1 all nfsd will terminate. This results in a
race condition when shutting down nfsd. The kill signal is not send to a list of PIDs, but
a list of PIDs is iterated and kill(1) called for every PID. 

As soon as the kill-loop hits the master nfsd, all subsequent kills will go to stale PIDs
eventually killing of other innocent processes.
>How-To-Repeat:
Crank up the number of processes to make it more obvious:
nfs_server_enable="YES"
nfs_server_flags="-u -t -n 64"

run /etc/rc.d/nfsd start and /etc/rc.d/nfsd stop. You will most probably see several errors
from kill. Check $?
>Fix:

This is only a proof of concept as I'm not sure if this should be kludged into rc.subr or
if we should provide a minimal override in rc.d/nfsd.

You might wanna diff the two functions to see what I did. (Change _fp_args, extend the if test
to if-master-and-same-jailid).

--- nfsd.diff begins here ---
--- nfsd	2006-10-18 11:15:50.000000000 +0200
+++ nfsd.new	2006-10-18 11:15:47.000000000 +0200
@@ -44,4 +44,42 @@
 	return 0
 }
 
+# Overwrite the _find_processes() function. We are only interested in the
+# nfsd master process. Only this one should get the kill signal.
+
+_find_processes()
+{
+	if [ $# -ne 3 ]; then
+		err 3 'USAGE: _find_processes procname interpreter psargs'
+	fi
+	_procname=$1
+	_interpreter=$2
+	_psargs=$3
+
+	_pref=
+		_procnamebn=${_procname##*/}
+		_fp_args='_arg0 _argv _x'
+		_fp_match='case "$_arg0" in
+		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
+
+	_proccheck='
+		ps 2>/dev/null -o "pid,jid,command" '"$_psargs"' |
+		while read _npid _jid '"$_fp_args"'; do
+			case "$_npid" in
+			PID)
+				continue;;
+			esac; '"$_fp_match"'
+				if [ "$_argv" = "master" -a "$JID" -eq "$_jid" ];
+				then echo -n "$_pref$_npid";
+				_pref=" ";
+				fi
+				;;
+			esac
+		done'
+
+#	debug "in _find_processes: proccheck is ($_proccheck)."
+	eval $_proccheck
+}
+
+
 run_rc_command "$1"
--- nfsd.diff ends here ---



--h31gzZEtNLTqOjlF
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="nfsd.diff"

--- nfsd	2006-10-18 11:15:50.000000000 +0200
+++ nfsd.new	2006-10-18 11:15:47.000000000 +0200
@@ -44,4 +44,42 @@
 	return 0
 }
 
+# Overwrite the _find_processes() function. We are only interested in the
+# nfsd master process. Only this one should get the kill signal.
+
+_find_processes()
+{
+	if [ $# -ne 3 ]; then
+		err 3 'USAGE: _find_processes procname interpreter psargs'
+	fi
+	_procname=$1
+	_interpreter=$2
+	_psargs=$3
+
+	_pref=
+		_procnamebn=${_procname##*/}
+		_fp_args='_arg0 _argv _x'
+		_fp_match='case "$_arg0" in
+		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
+
+	_proccheck='
+		ps 2>/dev/null -o "pid,jid,command" '"$_psargs"' |
+		while read _npid _jid '"$_fp_args"'; do
+			case "$_npid" in
+			PID)
+				continue;;
+			esac; '"$_fp_match"'
+				if [ "$_argv" = "master" -a "$JID" -eq "$_jid" ];
+				then echo -n "$_pref$_npid";
+				_pref=" ";
+				fi
+				;;
+			esac
+		done'
+
+#	debug "in _find_processes: proccheck is ($_proccheck)."
+	eval $_proccheck
+}
+
+
 run_rc_command "$1"

--h31gzZEtNLTqOjlF--
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->freebsd-rc 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Wed Oct 18 20:26:33 UTC 2006 
Responsible-Changed-Why:  
Over to maintainer(s). 

http://www.freebsd.org/cgi/query-pr.cgi?pr=104549 
State-Changed-From-To: open->feedback 
State-Changed-By: crees 
State-Changed-When: Thu Nov 1 19:24:19 UTC 2012 
State-Changed-Why:  
I think that using a pidfile for the nfsd master sprocess would be 
cleaner.  I'm reluctant to rewrite start_cmd for that purpose though... 
perhaps we should make a generic macro for rc to echo $$ > $pidfile; it 
seems commonly required. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=104549 
>Unformatted:
 --h31gzZEtNLTqOjlF
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 
