Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 15 Jan 2014 20:06:04 +0100
From:      =?ISO-8859-2?Q?Tomek_Wa=B3aszek?= <tmwalaszek@gmail.com>
To:        freebsd-rc@freebsd.org
Subject:   Stopping services by rc.d scripts
Message-ID:  <CAN3T69vTz24E94JS9JzhC75x1=Ao8Q6ogWkcA5h0_xOg0naBjA@mail.gmail.com>

next in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
Hello,
I`ve seen some strange behaviour of the rc scripts when I was stopping
openntpd and opensmtpd. When I was stopping one of those services I saw
this:


[root@shewolf ~]# ptree -C ntpd -l
2690 ntpd: [priv]
  2691 ntpd: ntp engine
    2692 ntpd: dns engine



*[root@shewolf ~]# /usr/local/etc/rc.d/openntpd stopStopping openntpd.kill:
2691: No such processkill: 2692: No such process*

[root@shewolf ~]# uname -a
FreeBSD shewolf.com.pl 9.2-RELEASE-p2 FreeBSD 9.2-RELEASE-p2 #0 r259233:
Thu Dec 12 19:57:26 CET 2013
root@shewolf.com.pl:/usr/obj/usr/src/sys/SHEWOLF
i386


I was wondering why there are those 'No such process' errors.
So in short words what is happening, rc framework will collect all openntpd
pids (openntpd does not support pidfile :( ) and send SIGTERM to them. When
the first process of the openntpd gets SIGTERM it will first kill his
children and quit. So this is the reason why we are seeing 'No such
process'. This problem does not occurs when a daemon supports pid file
becuase rc will get only one pid and send SIGTERM only to it.
It was quite annoying for me so I decided to do something with it. The
simplest solution would be to add pidfile into the
/usr/local/etc/rc.d/openntpd but there is no pidfile in this service. So I
wrote a patch for /etc/rc.subr, you can defined variable *leader* (bool) in
the rc script. If leader will be set to true ("YES" :)) it will kill only
the process group leader, so we will not get any 'No such process' errors
and the process group leader will kill necessary processes. If you will set
it to NO or dont define it in the script it will be running with the
default behaviour.

Maybe there are some other methods to do this but I dont know them.
Patch is in the attachment, I`ve deployed it on my server and everything is
working but anyway be careful ;). If you found it useful thats good but if
you think that my problem is ridiculous and my solution is terrible just
ignore this post ;).


Tomek

[-- Attachment #2 --]
diff --git a/rc.subr b/rc.subr
index 850bc03..668ea14 100644
--- a/rc.subr
+++ b/rc.subr
@@ -252,7 +252,7 @@ check_pidfile()
 		debug "pid file ($_pidfile): no pid in file."
 		return
 	fi
-	_find_processes $_procname $_interpreter '-p '"$_pid" ${_leader:-"NO"}
+	_find_processes $_procname ${_interpreter:-.} '-p '"$_pid"
 }
 
 #
@@ -265,12 +265,10 @@ check_process()
 {
 	_procname=$1
 	_interpreter=$2
-	_leader=$3
-
 	if [ -z "$_procname" ]; then
-		err 3 'USAGE: check_process procname [interpreter leader]'
+		err 3 'USAGE: check_process procname [interpreter]'
 	fi
-	_find_processes $_procname $_interpreter '-ax' $_leader
+	_find_processes $_procname ${_interpreter:-.} '-ax'
 }
 
 #
@@ -295,13 +293,12 @@ check_process()
 #
 _find_processes()
 {
-	if [ $# -ne 4 ]; then
+	if [ $# -ne 3 ]; then
 		err 3 'USAGE: _find_processes procname interpreter psargs'
 	fi
 	_procname=$1
 	_interpreter=$2
 	_psargs=$3
-	_leader=$4
 
 	_pref=
 	if [ $_interpreter != "." ]; then	# an interpreted script
@@ -341,22 +338,14 @@ _find_processes()
 		_fp_match='case "$_arg0" in
 		    $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")'
 	fi
-	
+
 	_proccheck="\
-		$PS 2>/dev/null -o pid= -o pgid= -o jid= -o command= $_psargs"' |
-		while read _npid _pgid _jid '"$_fp_args"'; do
+		$PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' |
+		while read _npid _jid '"$_fp_args"'; do
 			'"$_fp_match"'
-				if checkyesno _leader;
-				then 
-					if [ "$JID" -eq "$_jid" -a "$_npid" -eq "$_pgid"  ]; then
-						echo -n "$_pref$_npid";
-						_pref=" ";
-					fi
-				else
-					if [ "$JID" -eq "$_jid" ]; then
-						echo -n "$_pref$_npid";
-						_pref=" ";
-					fi
+				if [ "$JID" -eq "$_jid" ];
+				then echo -n "$_pref$_npid";
+				_pref=" ";
 				fi
 				;;
 			esac
@@ -670,15 +659,13 @@ run_rc_command()
 	rc_pid=
 	_pidcmd=
 	_procname=${procname:-${command}}
-	command_interpreter=${command_interpreter:-.}
-	_leader=${leader:-"NO"}
 
 					# setup pid check command
 	if [ -n "$_procname" ]; then
 		if [ -n "$pidfile" ]; then
 			_pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')'
 		else
-			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter $_leader"')'
+			_pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')'
 		fi
 		if [ -n "$_pidcmd" ]; then
 			_keywords="${_keywords} status poll"

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAN3T69vTz24E94JS9JzhC75x1=Ao8Q6ogWkcA5h0_xOg0naBjA>