http://perforce.freebsd.org/chv.cgi?CH=72970 Change 72970 by pjd@pjd_anger on 2005/03/12 14:11:55 - Add '-S' flag for pgrep(1) which allows to include system processes in output. - Replace IS_KERNPROC() macro with PSKIP() macro, which skips not only system processes (when '-S' is not specified), but also running pgrep/pkill process. This is much cleaner. - When matching against full argument lists and we cannot get argument list (e.g. for kernel threads) just use process name instead of skipping process entirely. Affected files ... .. //depot/user/pjd/pkill/usr.bin/pkill/pkill.1#6 edit .. //depot/user/pjd/pkill/usr.bin/pkill/pkill.c#8 edit Differences ... ==== //depot/user/pjd/pkill/usr.bin/pkill/pkill.1#6 (text+ko) ==== @@ -44,7 +44,7 @@ .Nd find or signal processes by name .Sh SYNOPSIS .Nm pgrep -.Op Fl filnvx +.Op Fl Sfilnvx .Op Fl F Ar pidfile .Op Fl G Ar gid .Op Fl M Ar core @@ -105,6 +105,8 @@ Restrict matches to processes with a parent process ID in the comma-separated list .Ar ppid . +.It Fl S +Search also in system processes (kernel threads). .It Fl U Ar uid Restrict matches to processes with a real user ID in the comma-separated list ==== //depot/user/pjd/pkill/usr.bin/pkill/pkill.c#8 (text+ko) ==== @@ -72,8 +72,9 @@ #define MIN_PID 5 #define MAX_PID 99999 -/* Check for system-processes which should always be ignored. */ -#define IS_KERNPROC(kp) ((kp)->ki_flag & P_KTHREAD) +/* Ignore system-processes (if '-S' flag is not specified) and myself. */ +#define PSKIP(kp) ((kp)->ki_pid == mypid || \ + (!kthreads && ((kp)->ki_flag & P_KTHREAD) != 0)) enum listtype { LT_GENERIC, @@ -102,6 +103,7 @@ int longfmt; int matchargs; int fullmatch; +int kthreads; int cflags = REG_EXTENDED; kvm_t *kd; pid_t mypid; @@ -175,7 +177,7 @@ pidfromfile = -1; execf = coref = _PATH_DEVNULL; - while ((ch = getopt(argc, argv, "DF:G:M:N:P:U:d:fg:ij:lns:t:u:vx")) != -1) + while ((ch = getopt(argc, argv, "DF:G:M:N:P:SU:d:fg:ij:lns:t:u:vx")) != -1) switch (ch) { case 'D': debug_opt++; @@ -198,6 +200,11 @@ makelist(&ppidlist, LT_GENERIC, optarg); criteria = 1; break; + case 'S': + if (!pgrep) + usage(); + kthreads = 1; + break; case 'U': makelist(&ruidlist, LT_USER, optarg); criteria = 1; @@ -295,17 +302,15 @@ } for (i = 0, kp = plist; i < nproc; i++, kp++) { - if (IS_KERNPROC(kp) != 0) { + if (PSKIP(kp)) { if (debug_opt > 0) fprintf(stderr, "* Skipped %5d %3d %s\n", kp->ki_pid, kp->ki_uid, kp->ki_comm); continue; } - if (matchargs) { - if ((pargv = kvm_getargv(kd, kp, 0)) == NULL) - continue; - + if (matchargs && + (pargv = kvm_getargv(kd, kp, 0)) != NULL) { jsz = 0; while (jsz < sizeof(buf) && *pargv != NULL) { jsz += snprintf(buf + jsz, @@ -314,7 +319,6 @@ pargv[0]); pargv++; } - mstr = buf; } else mstr = kp->ki_comm; @@ -345,7 +349,7 @@ } for (i = 0, kp = plist; i < nproc; i++, kp++) { - if (IS_KERNPROC(kp) != 0) + if (PSKIP(kp)) continue; if (pidfromfile >= 0 && kp->ki_pid != pidfromfile) { @@ -436,8 +440,6 @@ bestidx = -1; for (i = 0, kp = plist; i < nproc; i++, kp++) { - if (kp->ki_pid == mypid) - continue; if (!selected[i]) continue; @@ -459,17 +461,13 @@ * Take the appropriate action for each matched process, if any. */ for (i = 0, rv = 0, kp = plist; i < nproc; i++, kp++) { - if (kp->ki_pid == mypid) + if (PSKIP(kp)) continue; if (selected[i]) { if (inverse) continue; } else if (!inverse) continue; - - if (IS_KERNPROC(kp) != 0) - continue; - rv = 1; (*action)(kp); } @@ -483,7 +481,7 @@ const char *ustr; if (pgrep) - ustr = "[-filnvx] [-d delim]"; + ustr = "[-Sfilnvx] [-d delim]"; else ustr = "[-signal] [-finvx]"; @@ -509,10 +507,8 @@ { char **argv; - if (longfmt && matchargs) { - if ((argv = kvm_getargv(kd, kp, 0)) == NULL) - return; - + if (longfmt && matchargs && + (argv = kvm_getargv(kd, kp, 0)) != NULL) { printf("%d ", (int)kp->ki_pid); for (; *argv != NULL; argv++) { printf("%s", *argv);