summaryrefslogtreecommitdiffstats
path: root/usr.bin/pkill
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2005-08-24 19:38:28 +0000
committerpjd <pjd@FreeBSD.org>2005-08-24 19:38:28 +0000
commit242683f73fe615f5b38446530c73e8cbf4db5343 (patch)
tree6ec533e7943ef38b34c0e8818d3ba4805bfad7fb /usr.bin/pkill
parent9de6caadfaaf2914c0753081337f979387c3a7db (diff)
downloadFreeBSD-src-242683f73fe615f5b38446530c73e8cbf4db5343.zip
FreeBSD-src-242683f73fe615f5b38446530c73e8cbf4db5343.tar.gz
Modify '-F' option to work nicely with pidfile(3) - a pidfile given as
an argument has to be locked.
Diffstat (limited to 'usr.bin/pkill')
-rw-r--r--usr.bin/pkill/pkill.18
-rw-r--r--usr.bin/pkill/pkill.c14
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);
OpenPOWER on IntegriCloud