From 242683f73fe615f5b38446530c73e8cbf4db5343 Mon Sep 17 00:00:00 2001 From: pjd Date: Wed, 24 Aug 2005 19:38:28 +0000 Subject: Modify '-F' option to work nicely with pidfile(3) - a pidfile given as an argument has to be locked. --- usr.bin/pkill/pkill.1 | 8 ++++++++ usr.bin/pkill/pkill.c | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/usr.bin/pkill/pkill.1 b/usr.bin/pkill/pkill.1 index 143bbc4..badeb59 100644 --- a/usr.bin/pkill/pkill.1 +++ b/usr.bin/pkill/pkill.1 @@ -91,6 +91,12 @@ The following options are available: Restrict matches to a process whose PID is stored in the .Ar pidfile file. +The +.Ar pidfile +file must be locked with the +.Xr flock 2 +syscall or created with +.Xr pidfile 3 . .It Fl G Ar gid Restrict matches to processes with a real group ID in the comma-separated list @@ -233,8 +239,10 @@ An internal error occurred. .Xr kill 1 , .Xr killall 1 , .Xr ps 1 , +.Xr flock 2 , .Xr kill 2 , .Xr sigaction 2 , +.Xr pidfile 3 , .Xr re_format 7 .\" Xr signal 7 .Sh HISTORY diff --git a/usr.bin/pkill/pkill.c b/usr.bin/pkill/pkill.c index 68798f1..1048d3a 100644 --- a/usr.bin/pkill/pkill.c +++ b/usr.bin/pkill/pkill.c @@ -641,6 +641,20 @@ takepid(const char *pidfile) if (fh == NULL) err(STATUS_ERROR, "can't open pid file `%s'", pidfile); + /* + * If we can lock pidfile, this means that daemon is not running, + * so better don't kill the process from the pidfile. + */ + if (flock(fileno(fh), LOCK_EX | LOCK_NB) == 0) { + (void)fclose(fh); + errx(STATUS_ERROR, "file '%s' can be locked", pidfile); + } else { + if (errno != EWOULDBLOCK) { + errx(STATUS_ERROR, "error while locking file '%s'", + pidfile); + } + } + if (fgets(line, sizeof(line), fh) == NULL) { if (feof(fh)) { (void)fclose(fh); -- cgit v1.1