Three new flags for pkill/pgrep.
Pawel Jakub Dawidek
pjd at FreeBSD.org
Sat Mar 12 06:19:45 PST 2005
On Fri, Mar 11, 2005 at 08:14:46PM +0100, Pawel Jakub Dawidek wrote:
+> Hi.
+>
+> I'm attaching patches (directly from perforce) which implements three
+> new flags:
+>
+> -F pidfile Restrict matches to process which pid is stored in
+> pidfile file.
+>
+> -i Ignore case distinctions in both the process table and
+> the supplied pattern.
+>
+> -j jid Restrict matches to processes inside jails with a jail ID in
+> the comma-separated list jid. The value zero is taken to
+> mean any jail ID.
+>
+> The '-F' option will allow for more safe kill `cat /var/run/daemon.pid`,
+> because one can call it as: pkill -F /var/run/sshd.pid sshd, so if pid
+> from the file not belongs to sshd daemon, it won't be killed.
+>
+> The '-i' flag was obtained from Jonathan Perkin's patch posted on NetBSD
+> mailing list.
+>
+> The '-j' option is simlar to Solaris' '-z' option (for Solaris zones).
+>
+> In addition, there is a patch which allows to print process jail ID
+> from ps(1).
Two more patches:
pkill_09.patch - Fix '-n' option.
pkill_10.patch - Adds '-S' option to include kernel threads (only in pgrep).
--
Pawel Jakub Dawidek http://www.wheel.pl
pjd at FreeBSD.org http://www.FreeBSD.org
FreeBSD committer Am I Evil? Yes, I Am!
-------------- next part --------------
http://perforce.freebsd.org/chv.cgi?CH=72968
Change 72968 by pjd at pjd_anger on 2005/03/12 13:08:30
Fix a bug in '-n' option. This option gives us the most recently
created process, but this process is pgrep/pkill itself and because
pgrep/pkill skips itself later, it always returns nothing.
So when looking for the newest process, skip myself.
Affected files ...
.. //depot/user/pjd/pkill/usr.bin/pkill/pkill.c#7 edit
Differences ...
==== //depot/user/pjd/pkill/usr.bin/pkill/pkill.c#7 (text+ko) ====
@@ -436,6 +436,8 @@
bestidx = -1;
for (i = 0, kp = plist; i < nproc; i++, kp++) {
+ if (kp->ki_pid == mypid)
+ continue;
if (!selected[i])
continue;
-------------- next part --------------
http://perforce.freebsd.org/chv.cgi?CH=72970
Change 72970 by pjd at 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);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-current/attachments/20050312/a1708903/attachment.bin
More information about the freebsd-current
mailing list