Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Mar 2005 15:19:41 +0100
From:      Pawel Jakub Dawidek <pjd@FreeBSD.org>
To:        freebsd-current@freebsd.org
Cc:        gad@freebsd.org
Subject:   Re: Three new flags for pkill/pgrep.
Message-ID:  <20050312141941.GJ9291@darkness.comp.waw.pl>
In-Reply-To: <20050311191446.GG9291@darkness.comp.waw.pl>
References:  <20050311191446.GG9291@darkness.comp.waw.pl>

next in thread | previous in thread | raw e-mail | index | archive | help

--PCSS5jcV3Z4INoC2
Content-Type: multipart/mixed; boundary="84JQvybYWNxrNqac"
Content-Disposition: inline


--84JQvybYWNxrNqac
Content-Type: text/plain; charset=iso-8859-2
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Mar 11, 2005 at 08:14:46PM +0100, Pawel Jakub Dawidek wrote:
+> Hi.
+>=20
+> I'm attaching patches (directly from perforce) which implements three
+> new flags:
+>=20
+> -F pidfile	Restrict matches to process which pid is stored in
+> 		pidfile file.
+>=20
+> -i		Ignore case distinctions in both the process table and
+> 		the supplied pattern.
+>=20
+> -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.
+>=20
+> 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.
+>=20
+> The '-i' flag was obtained from Jonathan Perkin's patch posted on NetBSD
+> mailing list.
+>=20
+> The '-j' option is simlar to Solaris' '-z' option (for Solaris zones).
+>=20
+> In addition, there is a patch which allows to print process jail ID=20
+> 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).

--=20
Pawel Jakub Dawidek                       http://www.wheel.pl
pjd@FreeBSD.org                           http://www.FreeBSD.org
FreeBSD committer                         Am I Evil? Yes, I Am!

--84JQvybYWNxrNqac
Content-Type: text/plain; charset=iso-8859-2
Content-Disposition: attachment; filename="pkill_09.patch"
Content-Transfer-Encoding: quoted-printable

http://perforce.freebsd.org/chv.cgi?CH=3D72968

Change 72968 by pjd@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 ...

=2E. //depot/user/pjd/pkill/usr.bin/pkill/pkill.c#7 edit

Differences ...

=3D=3D=3D=3D //depot/user/pjd/pkill/usr.bin/pkill/pkill.c#7 (text+ko) =3D=
=3D=3D=3D

@@ -436,6 +436,8 @@
 		bestidx =3D -1;
=20
 		for (i =3D 0, kp =3D plist; i < nproc; i++, kp++) {
+			if (kp->ki_pid =3D=3D mypid)
+				continue;
 			if (!selected[i])
 				continue;
=20

--84JQvybYWNxrNqac
Content-Type: text/plain; charset=iso-8859-2
Content-Disposition: attachment; filename="pkill_10.patch"
Content-Transfer-Encoding: quoted-printable

http://perforce.freebsd.org/chv.cgi?CH=3D72970

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 ...

=2E. //depot/user/pjd/pkill/usr.bin/pkill/pkill.1#6 edit
=2E. //depot/user/pjd/pkill/usr.bin/pkill/pkill.c#8 edit

Differences ...

=3D=3D=3D=3D //depot/user/pjd/pkill/usr.bin/pkill/pkill.1#6 (text+ko) =3D=
=3D=3D=3D

@@ -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

=3D=3D=3D=3D //depot/user/pjd/pkill/usr.bin/pkill/pkill.c#8 (text+ko) =3D=
=3D=3D=3D

@@ -72,8 +72,9 @@
 #define	MIN_PID		5
 #define	MAX_PID		99999
=20
-/* 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 =3D=3D mypid ||			\
+			 (!kthreads && ((kp)->ki_flag & P_KTHREAD) !=3D 0))
=20
 enum listtype {
 	LT_GENERIC,
@@ -102,6 +103,7 @@
 int	longfmt;
 int	matchargs;
 int	fullmatch;
+int	kthreads;
 int	cflags =3D REG_EXTENDED;
 kvm_t	*kd;
 pid_t	mypid;
@@ -175,7 +177,7 @@
 	pidfromfile =3D -1;
 	execf =3D coref =3D _PATH_DEVNULL;
=20
-	while ((ch =3D getopt(argc, argv, "DF:G:M:N:P:U:d:fg:ij:lns:t:u:vx")) !=
=3D -1)
+	while ((ch =3D getopt(argc, argv, "DF:G:M:N:P:SU:d:fg:ij:lns:t:u:vx")) !=
=3D -1)
 		switch (ch) {
 		case 'D':
 			debug_opt++;
@@ -198,6 +200,11 @@
 			makelist(&ppidlist, LT_GENERIC, optarg);
 			criteria =3D 1;
 			break;
+		case 'S':
+			if (!pgrep)
+				usage();
+			kthreads =3D 1;
+			break;
 		case 'U':
 			makelist(&ruidlist, LT_USER, optarg);
 			criteria =3D 1;
@@ -295,17 +302,15 @@
 		}
=20
 		for (i =3D 0, kp =3D plist; i < nproc; i++, kp++) {
-			if (IS_KERNPROC(kp) !=3D 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;
 			}
=20
-			if (matchargs) {
-				if ((pargv =3D kvm_getargv(kd, kp, 0)) =3D=3D NULL)
-					continue;
-
+			if (matchargs &&
+			    (pargv =3D kvm_getargv(kd, kp, 0)) !=3D NULL) {
 				jsz =3D 0;
 				while (jsz < sizeof(buf) && *pargv !=3D NULL) {
 					jsz +=3D snprintf(buf + jsz,
@@ -314,7 +319,6 @@
 					    pargv[0]);
 					pargv++;
 				}
-
 				mstr =3D buf;
 			} else
 				mstr =3D kp->ki_comm;
@@ -345,7 +349,7 @@
 	}
=20
 	for (i =3D 0, kp =3D plist; i < nproc; i++, kp++) {
-		if (IS_KERNPROC(kp) !=3D 0)
+		if (PSKIP(kp))
 			continue;
=20
 		if (pidfromfile >=3D 0 && kp->ki_pid !=3D pidfromfile) {
@@ -436,8 +440,6 @@
 		bestidx =3D -1;
=20
 		for (i =3D 0, kp =3D plist; i < nproc; i++, kp++) {
-			if (kp->ki_pid =3D=3D mypid)
-				continue;
 			if (!selected[i])
 				continue;
=20
@@ -459,17 +461,13 @@
 	 * Take the appropriate action for each matched process, if any.
 	 */
 	for (i =3D 0, rv =3D 0, kp =3D plist; i < nproc; i++, kp++) {
-		if (kp->ki_pid =3D=3D mypid)
+		if (PSKIP(kp))
 			continue;
 		if (selected[i]) {
 			if (inverse)
 				continue;
 		} else if (!inverse)
 			continue;
-
-		if (IS_KERNPROC(kp) !=3D 0)
-			continue;
-
 		rv =3D 1;
 		(*action)(kp);
 	}
@@ -483,7 +481,7 @@
 	const char *ustr;
=20
 	if (pgrep)
-		ustr =3D "[-filnvx] [-d delim]";
+		ustr =3D "[-Sfilnvx] [-d delim]";
 	else
 		ustr =3D "[-signal] [-finvx]";
=20
@@ -509,10 +507,8 @@
 {
 	char **argv;
=20
-	if (longfmt && matchargs) {
-		if ((argv =3D kvm_getargv(kd, kp, 0)) =3D=3D NULL)
-			return;
-
+	if (longfmt && matchargs &&
+	    (argv =3D kvm_getargv(kd, kp, 0)) !=3D NULL) {
 		printf("%d ", (int)kp->ki_pid);
 		for (; *argv !=3D NULL; argv++) {
 			printf("%s", *argv);

--84JQvybYWNxrNqac--

--PCSS5jcV3Z4INoC2
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (FreeBSD)

iD8DBQFCMvp9ForvXbEpPzQRAto0AJ9I/V2AySzclrStbH1yV4bkXOXc0ACfbEHe
KuunMuygaxUm1PVvUCo0JCE=
=hHwt
-----END PGP SIGNATURE-----

--PCSS5jcV3Z4INoC2--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050312141941.GJ9291>