Subject: getppid() and 1!
To: None <>
From: Jorgen Lundman <>
List: netbsd-users
Date: 09/12/2003 10:03:32
I spent all of yesterday trying to find a solution to a situation where apache 
is run as nobody, spawning cgiwrap setuid as root and eventually as the cgi 
user, to detect when apache goes away (user disconnects) as some of the cgi 
scripts hang due to NFS lock leaks.

Anyway, tried most things with trying to detect if stdout is closed (without 
doing any IO since the cgi script talks on it) to doing some signalling madness. 
I could not use kill(  ,0) either since I had no permission (uid mismatch).

In the end it was very trivial, if getppid() == 1 (inherited by init) it appears 
that the parent process is gone.

If only that was mentioned in the man page :) Any man page, I was rooting around 
in many places :)  QINQ on Irc gave me that hint.

Totally unrelated to NetBSD, he also suggested that using kill(pid, 0), when I 
have no permission, would return -1 with errno at EPERM if it exists, and ESRCH 
when the process doesn't exists. However, on Solaris this appears not to work. 
Perhaps it isn't a very portable way to detect if a process exists? (Unless its 
just a dumb bug in my code).

Anyway, I suppose I can update the manpage myself and submit it :)


Jorgen Lundman       | <>
Unix Administrator   | +81 (0)3 -5456-2687 ext 1017 (work)
Shibuya-ku, Tokyo    | +81 (0)90-5578-8500          (cell)
Japan                | +81 (0)3 -3375-1767          (home)