Comment 6 for bug 246735

Revision history for this message
Matt Zimmerman (mdz) wrote : Re: [Bug 246735] Re: status_of_proc() calls pidofproc() which calls kill, requiring ownership privileges on the process

On Wed, Jul 09, 2008 at 03:16:24PM -0000, Dustin Kirkland wrote:
> If a pidfile is available, pidofproc() uses the following to determine if a process is active:
> ...
> if $(kill -0 "${pid:-}" 2> /dev/null); then
> echo "$pid"
> return 0
> return 1 # program is dead and /var/run pid file exists
> fi
> ...
>
> I assume this is to account for zombie processes, where a process exists
> in the process table, but it will not receive a -0 signal.

Whatever its reasons, it is buggy and should probably be fixed. :-)

> The "kill -0" operation is restricted to the owner of the process (and
> root).

And rightly so. kill(..., 0) is meant to tell the caller whether it's
possible for them to send a signal to the specified PID, without actually
sending any. As is clear from the problem you found, this is not the same
thing as testing whether the process is running.

> I've attached another patch, that detects an "Operation not permitted"
> error in the pidofproc() kill call, and returns 100 (in order to
> preserve a non-zero return code for legacy callers of pidofproc()).
> This patch does keep a status_of_proc() around, to call pidofproc(), and
> detect the "0 or 100" return code.

How about using ps(1) instead of kill(1) to look for the process? That
should work regardless of the caller's ability to send signals, and doesn't
require interpreting the error code.

--
 - mdz