Subject: Suggested enhancement to rc.subr for "master daemons"
To: None <tech-userlevel@netbsd.org>
From: Ed Ravin <eravin@panix.com>
List: tech-userlevel
Date: 12/17/2004 03:50:11
In my shop's NetBSD 2.0 environment, we have a daemon that's too dumb to
write a pidfile.  The daemon also forks off children who may run for a while
before they finish their work and quit.  So, when using the rc.d script
(I'm calling the daemon "legacyd" here), it reports all the running daemons:

 /etc/rc.d/legacyd status
 legacyd is running as pid 4696 9508 15730 16870 19216 20215 21171 27041.

This is OK for status, but when it's time to stop the daemon, we only
want to kill the master - the children will exit when their work is done,
and we'll have angry users coming after us with long sharp knives if we
prematurely interrupt them.  Unfortunately, our simple rc.d script:

	# PROVIDE: legacyd
	# REQUIRE: DAEMON

	# . /etc/rc.subr
	. ./rc.subr

	name="legacyd"
	rcvar=$name
	command="/usr/local/sbin/${name}"
	command_args="</dev/null >/dev/null 2>&1 &"

	load_rc_config $name
	run_rc_command "$1"

will kill all the running processes unless we go to great lengths
to write our own stop function.  It occured to someone here that
we could distinguish the master daemon by using ps output to see
if it's parent pid is 1, i.e. init, and it occurred to me that
we might want this feature for more than one daemon.

So below is a proposed patch to /etc/rc.subr that adds a new option,
"master_only" which should cause rc.subr to only report status or kill
a process if its parent pid is 1 when someone puts "master_only=true"
into the rc.d script.

Will this be useful for others, and if so, is it worth putting into NetBSD?

	-- Ed


--- /etc/rc.subr
+++ ./rc.subr	
@@ -193,11 +193,13 @@
 	fi
 
 	_proccheck='
-		ps -o "pid,command" '"$_psargs"' |
-		while read _npid '"$_fp_args"'; do
-			case "$_npid" in
-			    PID)
+		ps -o "pid,ppid,command" '"$_psargs"' |
+		while read _npid _ppid '"$_fp_args"'; do
+			case "$_ppid" in
+			    PPID)
 				continue ;;
+			    1) ;;
+			    *) if ${master_only:-false}; then continue; fi ;;
 			esac ; '"$_fp_match"'
 				echo -n "$_pref$_npid" ;
 				_pref=" "