diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2008-02-07 15:38:08 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2008-02-07 15:38:08 +0000 |
commit | 12a53d32aad70e494f9f2d33c835e2d021913450 (patch) | |
tree | 779e29e7fa314ad1b04849830c90d7943e940c62 /usr.bin/pkill | |
parent | 6b3f36a8d54742b75994faffc0399f5a367d8d63 (diff) |
Add add -o flag to pkill/pgrep like on Solaris and Linux.
OK jmc@ henning@ oga@ mikeb@
Diffstat (limited to 'usr.bin/pkill')
-rw-r--r-- | usr.bin/pkill/pkill.1 | 16 | ||||
-rw-r--r-- | usr.bin/pkill/pkill.c | 35 |
2 files changed, 35 insertions, 16 deletions
diff --git a/usr.bin/pkill/pkill.1 b/usr.bin/pkill/pkill.1 index 74b31470244..b50213d6109 100644 --- a/usr.bin/pkill/pkill.1 +++ b/usr.bin/pkill/pkill.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: pkill.1,v 1.9 2007/10/31 17:14:34 jmc Exp $ +.\" $OpenBSD: pkill.1,v 1.10 2008/02/07 15:38:07 millert Exp $ .\" $NetBSD: pkill.1,v 1.8 2003/02/14 15:59:18 grant Exp $ .\" .\" Copyright (c) 2002 The NetBSD Foundation, Inc. @@ -35,7 +35,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd $Mdocdate: October 31 2007 $ +.Dd $Mdocdate: February 7 2008 $ .Dt PKILL 1 .Os .Sh NAME @@ -43,7 +43,7 @@ .Nd find or signal processes by name .Sh SYNOPSIS .Nm pgrep -.Op Fl flnvx +.Op Fl flnovx .Op Fl d Ar delim .Op Fl G Ar gid .Op Fl g Ar pgrp @@ -55,7 +55,7 @@ .Op Ar pattern ... .Nm pkill .Op Fl Ar signal -.Op Fl fnvx +.Op Fl fnovx .Op Fl G Ar gid .Op Fl g Ar pgrp .Op Fl P Ar ppid @@ -111,7 +111,13 @@ This option can only be used with the .Nm pgrep command. .It Fl n -Match only the most recently created process, if any. +Match only the most recently created (newest) process, if any. +Cannot be used in conjuction with +.Fl o . +.It Fl o +Match only the least recently created (oldest) process, if any. +Cannot be used in conjuction with +.Fl n . .It Fl P Ar ppid Restrict matches to processes with a parent process ID in the comma-separated list diff --git a/usr.bin/pkill/pkill.c b/usr.bin/pkill/pkill.c index 02848d7b64f..3a611f304f5 100644 --- a/usr.bin/pkill/pkill.c +++ b/usr.bin/pkill/pkill.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pkill.c,v 1.15 2006/09/19 05:52:23 otto Exp $ */ +/* $OpenBSD: pkill.c,v 1.16 2008/02/07 15:38:07 millert Exp $ */ /* $NetBSD: pkill.c,v 1.5 2002/10/27 11:49:34 kleink Exp $ */ /*- @@ -38,7 +38,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: pkill.c,v 1.15 2006/09/19 05:52:23 otto Exp $"; +static const char rcsid[] = "$OpenBSD: pkill.c,v 1.16 2008/02/07 15:38:07 millert Exp $"; #endif /* !lint */ #include <sys/types.h> @@ -50,6 +50,7 @@ static const char rcsid[] = "$OpenBSD: pkill.c,v 1.15 2006/09/19 05:52:23 otto E #include <stdio.h> #include <stdlib.h> +#include <stdint.h> #include <limits.h> #include <string.h> #include <unistd.h> @@ -90,6 +91,7 @@ int nproc; int pgrep; int signum = SIGTERM; int newest; +int oldest; int inverse; int longfmt; int matchargs; @@ -158,7 +160,7 @@ main(int argc, char **argv) criteria = 0; - while ((ch = getopt(argc, argv, "G:P:U:d:fg:lns:t:u:vx")) != -1) + while ((ch = getopt(argc, argv, "G:P:U:d:fg:lnos:t:u:vx")) != -1) switch (ch) { case 'G': makelist(&rgidlist, LT_GROUP, optarg); @@ -193,6 +195,10 @@ main(int argc, char **argv) newest = 1; criteria = 1; break; + case 'o': + oldest = 1; + criteria = 1; + break; case 's': makelist(&sidlist, LT_SID, optarg); criteria = 1; @@ -220,7 +226,7 @@ main(int argc, char **argv) argv += optind; if (argc != 0) criteria = 1; - if (!criteria) + if (!criteria || (newest && oldest)) usage(); mypid = getpid(); @@ -364,18 +370,25 @@ main(int argc, char **argv) selected[i] = 1; } - if (newest) { - bestsec = 0; - bestusec = 0; + if (newest || oldest) { bestidx = -1; + if (newest) + bestsec = bestusec = 0; + else + bestsec = bestusec = UINT32_MAX; + for (i = 0, kp = plist; i < nproc; i++, kp++) { if (!selected[i]) continue; - if (kp->p_ustart_sec > bestsec || + if ((newest && (kp->p_ustart_sec > bestsec || (kp->p_ustart_sec == bestsec - && kp->p_ustart_usec > bestusec)) { + && kp->p_ustart_usec > bestusec))) + || (oldest && (kp->p_ustart_sec < bestsec || + (kp->p_ustart_sec == bestsec + && kp->p_ustart_usec < bestusec)))) { + bestsec = kp->p_ustart_sec; bestusec = kp->p_ustart_usec; bestidx = i; @@ -417,9 +430,9 @@ usage(void) const char *ustr; if (pgrep) - ustr = "[-flnvx] [-d delim]"; + ustr = "[-flnovx] [-d delim]"; else - ustr = "[-signal] [-fnvx]"; + ustr = "[-signal] [-fnovx]"; fprintf(stderr, "usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid] " "[-t tty]\n\t[-U uid] [-u euid] [pattern ...]\n", __progname, ustr); |