On Sun, Dec 13, 2020 at 10:42:20PM +0300, Consus wrote:
On Sun, Dec 13, 2020 at 08:27:24PM +0100, Harald Dunkel wrote:
At least OpenBSD is not alone with this problem. On Debian there
is a tool "/bin/pidof", trying to guess the pid of a daemon to kill
by looking at the process list as well.

Some dude from Google came up with a good solution (for Linux of
course). If you open a /proc/[pid] directory this PID cannot be recycled
unless you close the fd. Hence you can do this:

        1. Read /var/run/foobard.lock and get PID
        2. Open /proc/[pid]
        3. Check that PID is in fact belongs to an instance of foobard
        4. kill() it

Easy.

Hmm... My memory served me bad. It's not that PID won't by recycled,
it's that fd referencing it (you can get one from opening /proc/[pid])
will becomes invalid after PID is dead/recycled. So calling
pidfd_send_signal(fd, ...) will fail instead of killing the wrong
process.

Reply via email to