From 2f35b6aa00d5b756cf4345ade163c222bdde6a6b Mon Sep 17 00:00:00 2001 From: pjd Date: Wed, 16 Nov 2005 10:36:44 +0000 Subject: I often find myself doing: % pgrep [to verify which processes match] % pkill To speed such operation up, add -I option which works like rm(1)'s -i option (unfortunately -i is already used in pkill(1)), ie. pkill will ask for confirmation before killing each matching process. After adding -j, -F, -i, -S, -o and -L options and other improvements, I think I can add myself to the copyright header. Glanced at by: maintainer (gad) --- usr.bin/pkill/pkill.1 | 4 +++- usr.bin/pkill/pkill.c | 56 ++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 45 insertions(+), 15 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/pkill/pkill.1 b/usr.bin/pkill/pkill.1 index 2ece6aa..38c929f 100644 --- a/usr.bin/pkill/pkill.1 +++ b/usr.bin/pkill/pkill.1 @@ -60,7 +60,7 @@ .Ar pattern ... .Nm pkill .Op Fl Ar signal -.Op Fl Lfinovx +.Op Fl ILfinovx .Op Fl F Ar pidfile .Op Fl G Ar gid .Op Fl M Ar core @@ -95,6 +95,8 @@ file. Restrict matches to processes with a real group ID in the comma-separated list .Ar gid . +.It Fl I +Request confirmation before attempting to kill each process. .It Fl L The .Ar pidfile diff --git a/usr.bin/pkill/pkill.c b/usr.bin/pkill/pkill.c index 24c21a3..65d140b 100644 --- a/usr.bin/pkill/pkill.c +++ b/usr.bin/pkill/pkill.c @@ -2,6 +2,7 @@ /*- * Copyright (c) 2002 The NetBSD Foundation, Inc. + * Copyright (c) 2005 Pawel Jakub Dawidek * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation @@ -101,6 +102,7 @@ int pgrep; int signum = SIGTERM; int newest; int oldest; +int interactive; int inverse; int longfmt; int matchargs; @@ -180,7 +182,7 @@ main(int argc, char **argv) pidfilelock = 0; execf = coref = _PATH_DEVNULL; - while ((ch = getopt(argc, argv, "DF:G:LM:N:P:SU:d:fg:ij:lnos:t:u:vx")) != -1) + while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:d:fg:ij:lnos:t:u:vx")) != -1) switch (ch) { case 'D': debug_opt++; @@ -193,6 +195,11 @@ main(int argc, char **argv) makelist(&rgidlist, LT_GROUP, optarg); criteria = 1; break; + case 'I': + if (pgrep) + usage(); + interactive = 1; + break; case 'L': pidfilelock = 1; break; @@ -510,7 +517,7 @@ usage(void) if (pgrep) ustr = "[-LSfilnovx] [-d delim]"; else - ustr = "[-signal] [-Lfinovx]"; + ustr = "[-signal] [-ILfinovx]"; fprintf(stderr, "usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n" @@ -521,20 +528,12 @@ usage(void) exit(STATUS_ERROR); } -void -killact(struct kinfo_proc *kp) -{ - - if (kill(kp->ki_pid, signum) == -1) - err(STATUS_ERROR, "signalling pid %d", (int)kp->ki_pid); -} - -void -grepact(struct kinfo_proc *kp) +static void +show_process(struct kinfo_proc *kp) { char **argv; - if (longfmt && matchargs && + if ((longfmt || !pgrep) && matchargs && (argv = kvm_getargv(kd, kp, 0)) != NULL) { printf("%d ", (int)kp->ki_pid); for (; *argv != NULL; argv++) { @@ -542,11 +541,40 @@ grepact(struct kinfo_proc *kp) if (argv[1] != NULL) putchar(' '); } - } else if (longfmt) + } else if (longfmt || !pgrep) printf("%d %s", (int)kp->ki_pid, kp->ki_comm); else printf("%d", (int)kp->ki_pid); +} + +void +killact(struct kinfo_proc *kp) +{ + int ch, first; + + if (interactive) { + /* + * Be careful, ask before killing. + */ + printf("kill "); + show_process(kp); + printf("? "); + fflush(stdout); + first = ch = getchar(); + while (ch != '\n' && ch != EOF) + ch = getchar(); + if (first != 'y' && first != 'Y') + return; + } + if (kill(kp->ki_pid, signum) == -1) + err(STATUS_ERROR, "signalling pid %d", (int)kp->ki_pid); +} + +void +grepact(struct kinfo_proc *kp) +{ + show_process(kp); printf("%s", delim); } -- cgit v1.1