summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2005-08-25 20:10:47 +0000
committerpjd <pjd@FreeBSD.org>2005-08-25 20:10:47 +0000
commitfe9f2ab73454ea914fc6c5338b53d5b7a15399ae (patch)
tree9019b1f7f2af404183efe8f1072177cd2721a4eb
parentca020ef784c07ae2e89c04551fe1a5df25a265bf (diff)
downloadFreeBSD-src-fe9f2ab73454ea914fc6c5338b53d5b7a15399ae.zip
FreeBSD-src-fe9f2ab73454ea914fc6c5338b53d5b7a15399ae.tar.gz
Revert previous behaviour of '-F' option and add '-L' option, which will
tell pkill(1)/pgrep(1) to try to flock(2) pidfile before reading PID from there. Discussed with: jhb, gad
-rw-r--r--usr.bin/pkill/pkill.117
-rw-r--r--usr.bin/pkill/pkill.c53
2 files changed, 43 insertions, 27 deletions
diff --git a/usr.bin/pkill/pkill.1 b/usr.bin/pkill/pkill.1
index badeb59..2ece6aa 100644
--- a/usr.bin/pkill/pkill.1
+++ b/usr.bin/pkill/pkill.1
@@ -44,7 +44,7 @@
.Nd find or signal processes by name
.Sh SYNOPSIS
.Nm pgrep
-.Op Fl Sfilnovx
+.Op Fl LSfilnovx
.Op Fl F Ar pidfile
.Op Fl G Ar gid
.Op Fl M Ar core
@@ -60,7 +60,7 @@
.Ar pattern ...
.Nm pkill
.Op Fl Ar signal
-.Op Fl finovx
+.Op Fl Lfinovx
.Op Fl F Ar pidfile
.Op Fl G Ar gid
.Op Fl M Ar core
@@ -91,16 +91,19 @@ The following options are available:
Restrict matches to a process whose PID is stored in the
.Ar pidfile
file.
+.It Fl G Ar gid
+Restrict matches to processes with a real group ID in the comma-separated
+list
+.Ar gid .
+.It Fl L
The
.Ar pidfile
-file must be locked with the
+file given for the
+.Fl F
+option 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
-.Ar gid .
.It Fl M Ar core
Extract values associated with the name list from the specified core
instead of the currently running system.
diff --git a/usr.bin/pkill/pkill.c b/usr.bin/pkill/pkill.c
index 1048d3a..24c21a3 100644
--- a/usr.bin/pkill/pkill.c
+++ b/usr.bin/pkill/pkill.c
@@ -124,17 +124,17 @@ void usage(void);
void killact(struct kinfo_proc *);
void grepact(struct kinfo_proc *);
void makelist(struct listhead *, enum listtype, char *);
-int takepid(const char *);
+int takepid(const char *, int);
int
main(int argc, char **argv)
{
extern char *optarg;
extern int optind;
- char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
+ char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q, *pidfile;
const char *execf, *coref;
int debug_opt;
- int i, ch, bestidx, rv, criteria, pidfromfile;
+ int i, ch, bestidx, rv, criteria, pidfromfile, pidfilelock;
size_t jsz;
void (*action)(struct kinfo_proc *);
struct kinfo_proc *kp;
@@ -176,22 +176,26 @@ main(int argc, char **argv)
criteria = 0;
debug_opt = 0;
- pidfromfile = -1;
+ pidfile = NULL;
+ pidfilelock = 0;
execf = coref = _PATH_DEVNULL;
- while ((ch = getopt(argc, argv, "DF:G:M:N:P:SU:d:fg:ij:lnos:t:u:vx")) != -1)
+ while ((ch = getopt(argc, argv, "DF:G:LM:N:P:SU:d:fg:ij:lnos:t:u:vx")) != -1)
switch (ch) {
case 'D':
debug_opt++;
break;
case 'F':
- pidfromfile = takepid(optarg);
+ pidfile = optarg;
criteria = 1;
break;
case 'G':
makelist(&rgidlist, LT_GROUP, optarg);
criteria = 1;
break;
+ case 'L':
+ pidfilelock = 1;
+ break;
case 'M':
coref = optarg;
break;
@@ -274,6 +278,13 @@ main(int argc, char **argv)
usage();
if (newest && oldest)
errx(STATUS_ERROR, "-n and -o are mutually exclusive");
+ if (pidfile != NULL)
+ pidfromfile = takepid(pidfile, pidfilelock);
+ else {
+ if (pidfilelock)
+ errx(STATUS_ERROR, "-L doesn't make sense without -F");
+ pidfromfile = -1;
+ }
mypid = getpid();
@@ -497,9 +508,9 @@ usage(void)
const char *ustr;
if (pgrep)
- ustr = "[-Sfilnovx] [-d delim]";
+ ustr = "[-LSfilnovx] [-d delim]";
else
- ustr = "[-signal] [-finovx]";
+ ustr = "[-signal] [-Lfinovx]";
fprintf(stderr,
"usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n"
@@ -631,7 +642,7 @@ makelist(struct listhead *head, enum listtype type, char *src)
}
int
-takepid(const char *pidfile)
+takepid(const char *pidfile, int pidfilelock)
{
char *endp, line[BUFSIZ];
FILE *fh;
@@ -641,17 +652,19 @@ 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 (pidfilelock) {
+ /*
+ * If we can lock pidfile, this means that daemon is not
+ * running, so would be better not to kill some random process.
+ */
+ 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);
+ }
}
}
OpenPOWER on IntegriCloud