diff options
-rw-r--r-- | usr.bin/pkill/pkill.1 | 8 | ||||
-rw-r--r-- | usr.bin/pkill/pkill.c | 14 |
2 files changed, 22 insertions, 0 deletions
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); |