summaryrefslogtreecommitdiffstats
path: root/bin/pkill/pkill.c
diff options
context:
space:
mode:
authorbrian <brian@FreeBSD.org>2009-05-17 04:34:14 +0000
committerbrian <brian@FreeBSD.org>2009-05-17 04:34:14 +0000
commita99453c14c89f1e53f3b2534201d14e48d383228 (patch)
treec9c86c3103509e8abeb99ce09f2d0c7cf8281f83 /bin/pkill/pkill.c
parentfc0e3714cc2074d023fc26424c13622c68f23546 (diff)
downloadFreeBSD-src-a99453c14c89f1e53f3b2534201d14e48d383228.zip
FreeBSD-src-a99453c14c89f1e53f3b2534201d14e48d383228.tar.gz
When finding processes, ignore ourself and our ancestors. It is almost
always surprising when you kill a 'sh -c ...' ancestor or when you kill yourself when using -f. Add a -a switch for backwards compatibility. MFC after: 3 weeks
Diffstat (limited to 'bin/pkill/pkill.c')
-rw-r--r--bin/pkill/pkill.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/bin/pkill/pkill.c b/bin/pkill/pkill.c
index da8fbcd..b4b96b8 100644
--- a/bin/pkill/pkill.c
+++ b/bin/pkill/pkill.c
@@ -133,7 +133,7 @@ main(int argc, char **argv)
{
char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q, *pidfile;
const char *execf, *coref;
- int debug_opt;
+ int ancestors, debug_opt;
int i, ch, bestidx, rv, criteria, pidfromfile, pidfilelock;
size_t jsz;
int (*action)(const struct kinfo_proc *);
@@ -142,6 +142,7 @@ main(int argc, char **argv)
struct timeval best_tval;
regex_t reg;
regmatch_t regmatch;
+ pid_t pid;
setlocale(LC_ALL, "");
@@ -174,13 +175,14 @@ main(int argc, char **argv)
}
}
+ ancestors = 0;
criteria = 0;
debug_opt = 0;
pidfile = NULL;
pidfilelock = 0;
execf = coref = _PATH_DEVNULL;
- while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:d:fg:ij:lnos:t:u:vx")) != -1)
+ while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:ad:fg:ij:lnos:t:u:vx")) != -1)
switch (ch) {
case 'D':
debug_opt++;
@@ -220,6 +222,9 @@ main(int argc, char **argv)
makelist(&ruidlist, LT_USER, optarg);
criteria = 1;
break;
+ case 'a':
+ ancestors++;
+ break;
case 'd':
if (!pgrep)
usage();
@@ -468,6 +473,27 @@ main(int argc, char **argv)
selected[i] = 1;
}
+ if (!ancestors) {
+ pid = mypid;
+ while (pid) {
+ for (i = 0, kp = plist; i < nproc; i++, kp++) {
+ if (PSKIP(kp))
+ continue;
+ if (kp->ki_pid == pid) {
+ selected[i] = 0;
+ pid = kp->ki_ppid;
+ break;
+ }
+ }
+ if (i == nproc) {
+ if (pid == mypid)
+ pid = getppid();
+ else
+ break; /* Maybe we're in a jail ? */
+ }
+ }
+ }
+
if (newest || oldest) {
best_tval.tv_sec = 0;
best_tval.tv_usec = 0;
OpenPOWER on IntegriCloud