Subject: Re: /etc/rc.subr wait_for_pids (CVS commit: basesrc)
To: None <lukem@netbsd.org>
From: Greg A. Woods <woods@weird.com>
List: tech-userlevel
Date: 04/19/2001 17:36:16
[[ re-directed to tech-userlevel ]]

[ On Wednesday, April 18, 2001 at 04:31:42 (+0300), Luke Mewburn wrote: ]
> Subject: CVS commit: basesrc
>
> Log Message:
> * provide wait_for_pids(), which spins until all the given pids don't exist

Cool, but that description does not match your implementation.  :-(

However that description is correct and the implmentation is broken
(IMNSHO).

I invented an almost identical scheme for XDM some years ago, and
re-used it for squid more recently.  In both of those cases the only
correct thing to do is to wait until the PIDs don't exist.  No if's,
and's, or but's because in those cases manual intervention is always
possible and at least in the case of squid, necessary should the process
fail to die or have the wrong PID registered, etc.

	# XXX Shouldn't the default stop_cmd be this patient?
	#
	stop_nicely ()
	{
	        if [ -f ${pidfile} -a -s ${pidfile} ] ; then
	                read _PID __junk < ${pidfile}
	                if [ "$_PID" -ne 0 ]; then
		                echo -n "Stopping ${name} nicely"
	                        kill -${sig_stop:-TERM} $_PID
	                        echo -n '['
	                        while kill -0 $_PID >/dev/null 2>&1; do
	                                sleep 2
	                                echo -n '.'
	                        done
	                        echo ']'
			else
				echo "${name} has no valid PID in ${pidfile}?"
	                fi
	        else
	                echo "${name} not running?"
	        fi
	}

Certainly even 65 seconds (the approximate limit in the current
implementation) is not necessarily long enough for a very large squid
server to shut down, and it is very critical that squid shut itself down
properly in the case of a normal shutdown lest its databse be corrupted
which in some cases might require the cache directories be re-created
from scratch before it is restarted -- not a pretty sight.

> * run_rc_command():
> 	- add support for "poll", which by default, uses wait_for_pids() on
> 	  the list of matching processes
> 	- in the "restart" case, call "poll" between "stop" and "start"

I'm not so sure that's a logical way to do things.  I'd just always call
wait_for_pids in the "stop" case if there's a $pidfile defined and
presently containing a valid PID -- it's the only safe thing to do.

In any case this is critical for at least squid, and I'd argue critical
for xdm too.

For squid of course you also need to make sure its watcher script dies
only at the right time, which is easiest with --enable-kill-parent-hack
with ``stop_cmd="squid -k shutdown; wait_for_pids $(cat $pidfile)"''.

-- 
							Greg A. Woods

+1 416 218-0098      VE3TCP      <gwoods@acm.org>     <woods@robohack.ca>
Planix, Inc. <woods@planix.com>;   Secrets of the Weird <woods@weird.com>