RE: Syscall kill() can send signal to thread ID

David Laight
2022-09-23T11:25:35Z

...
> And yes, I'm tracking a bug. A service monitor, like systemd or
> some watchdog, uses kill() to check if a pid is valid or not:
> 1. Store service pid into cache.
> 2. Check if pid in cache is valid by kill(pid, 0).
> 3. Check if pid in cache is the service to watch.
>
> So if kill(pid, 0) returns success but no process info shows on 'ps'
> command, the service monitor could be confused. The monitor could
> check if pid is tid, but this means the odd behavior would be used
> intentionally. And this workaround may be unsafe on other OS?

That looks pretty broken to me.
On Linux a pid can be reused immediately a process exits.
So there is really no guarantee that the pid is the one you want.
IIRC there are some recent changes that mean opening /proc/<pid>
will stop the pid being reused - allowing checks before sending a signal.
(Netbsd won't reuse a pid for a reasonable number of forks
and then uses a semi-random pid allocator.
Don't know whether any other 'bsd picked up that change.)

Also using signals in multi-threaded programs is pretty much
non-portable.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)