Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 12 Mar 2005 17:19:09 +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:  <20050312161909.GM9291@darkness.comp.waw.pl>
In-Reply-To: <20050312141941.GJ9291@darkness.comp.waw.pl>
References:  <20050311191446.GG9291@darkness.comp.waw.pl> <20050312141941.GJ9291@darkness.comp.waw.pl>

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

--LMyZIZ/7CYeQWiVH
Content-Type: multipart/mixed; boundary="LF8A4izpY2Fai3uI"
Content-Disposition: inline


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

On Sat, Mar 12, 2005 at 03:19:41PM +0100, Pawel Jakub Dawidek wrote:
+> Two more patches:
+>=20
+> pkill_09.patch - Fix '-n' option.
+> pkill_10.patch - Adds '-S' option to include kernel threads (only in pgr=
ep).

One more:

pkill_11.patch - Add '-o' option - Select only the oldest (least recently
	started) of the matching processes.

'-o' option for pgrep/pkill can be also found in Solaris and Linux.

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

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

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

Change 72975 by pjd@pjd_anger on 2005/03/12 15:51:12

	Add '-o' option which allows to select the oldest of the matching
	processes.
	This option exists in Solaris and Linux.

Affected files ...

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

Differences ...

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

@@ -147,7 +147,9 @@
 .Nm pgrep
 command.
 .It Fl n
-Match only the most recently created process, if any.
+Select only the newest (most recently started) of the matching processes.
+.It Fl o
+Select only the oldest (least recently started) of the matching processes.
 .It Fl s Ar sid
 Restrict matches to processes with a session ID in the comma-separated
 list

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

@@ -99,6 +99,7 @@
 int	pgrep;
 int	signum =3D SIGTERM;
 int	newest;
+int	oldest;
 int	inverse;
 int	longfmt;
 int	matchargs;
@@ -132,12 +133,11 @@
 	char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
 	const char *execf, *coref;
 	int debug_opt;
-	int i, ch, bestidx, rv, criteria, pidfromfile;
+	int i, ch, rv, criteria, pidfromfile;
 	size_t jsz;
 	void (*action)(struct kinfo_proc *);
 	struct kinfo_proc *kp;
 	struct list *li;
-	struct timeval best_tval;
 	regex_t reg;
 	regmatch_t regmatch;
=20
@@ -177,7 +177,7 @@
 	pidfromfile =3D -1;
 	execf =3D coref =3D _PATH_DEVNULL;
=20
-	while ((ch =3D getopt(argc, argv, "DF:G:M:N:P:SU:d:fg:ij:lns:t:u:vx")) !=
=3D -1)
+	while ((ch =3D getopt(argc, argv, "DF:G:M:N:P:SU:d:fg:ij:lnos:t:u:vx")) !=
=3D -1)
 		switch (ch) {
 		case 'D':
 			debug_opt++;
@@ -237,6 +237,10 @@
 			newest =3D 1;
 			criteria =3D 1;
 			break;
+		case 'o':
+			oldest =3D 1;
+			criteria =3D 1;
+			break;
 		case 's':
 			makelist(&sidlist, LT_SID, optarg);
 			criteria =3D 1;
@@ -266,6 +270,8 @@
 		criteria =3D 1;
 	if (!criteria)
 		usage();
+	if (newest && oldest)
+		errx(STATUS_ERROR, "-n and -o are mutually exclusive");
=20
 	mypid =3D getpid();
=20
@@ -434,23 +440,29 @@
 			selected[i] =3D 1;
 	}
=20
-	if (newest) {
+	if (newest || oldest) {
+		struct timeval best_tval;
+		int bestidx;
+
 		best_tval.tv_sec =3D 0;
 		best_tval.tv_usec =3D 0;
 		bestidx =3D -1;
=20
+#define	PNEWER(kp)	((kp)->ki_start.tv_sec > best_tval.tv_sec ||	\
+			 ((kp)->ki_start.tv_sec =3D=3D best_tval.tv_sec &&	\
+			  (kp)->ki_start.tv_usec > best_tval.tv_usec))
 		for (i =3D 0, kp =3D plist; i < nproc; i++, kp++) {
 			if (!selected[i])
 				continue;
-
-			if (kp->ki_start.tv_sec > best_tval.tv_sec ||
-			    (kp->ki_start.tv_sec =3D=3D best_tval.tv_sec
-			    && kp->ki_start.tv_usec > best_tval.tv_usec)) {
+			if (bestidx =3D=3D -1 ||
+			    (newest && PNEWER(kp)) ||
+			    (oldest && !PNEWER(kp))) {
 				best_tval.tv_sec =3D kp->ki_start.tv_sec;
 				best_tval.tv_usec =3D kp->ki_start.tv_usec;
 				bestidx =3D i;
 			}
 		}
+#undef	PNEWER
=20
 		memset(selected, 0, nproc);
 		if (bestidx !=3D -1)
@@ -481,9 +493,9 @@
 	const char *ustr;
=20
 	if (pgrep)
-		ustr =3D "[-Sfilnvx] [-d delim]";
+		ustr =3D "[-Sfilnovx] [-d delim]";
 	else
-		ustr =3D "[-signal] [-finvx]";
+		ustr =3D "[-signal] [-finovx]";
=20
 	fprintf(stderr,
 		"usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n"

--LF8A4izpY2Fai3uI--

--LMyZIZ/7CYeQWiVH
Content-Type: application/pgp-signature
Content-Disposition: inline

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

iD8DBQFCMxZ9ForvXbEpPzQRAu1dAJ4oGmjSzEazh1aMRmbVGF6IsSK0TACffIRQ
18XNZKjVu+GFrhO/VW3nxOY=
=krTE
-----END PGP SIGNATURE-----

--LMyZIZ/7CYeQWiVH--



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