From nobody@FreeBSD.org  Thu Nov 22 19:17:12 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 70CBE16A417
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 22 Nov 2007 19:17:12 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 6C5EF13C468
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 22 Nov 2007 19:17:12 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.1/8.14.1) with ESMTP id lAMJH9AI031216
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 22 Nov 2007 19:17:09 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.1/8.14.1/Submit) id lAMJH9uV031214;
	Thu, 22 Nov 2007 19:17:09 GMT
	(envelope-from nobody)
Message-Id: <200711221917.lAMJH9uV031214@www.freebsd.org>
Date: Thu, 22 Nov 2007 19:17:09 GMT
From: Jan Schaumann <jschauma@netmeister.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: new options -r to pkill(1) a pid after timeout
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         118205
>Category:       bin
>Synopsis:       [patch] [request] new options -r to pkill(1) a pid after timeout
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Nov 22 19:20:00 UTC 2007
>Closed-Date:    
>Last-Modified:  Sat Jan 26 08:34:50 UTC 2008
>Originator:     Jan Schaumann
>Release:        
>Organization:
>Environment:
>Description:
I occasionally have the need to kill a process iff it has been running
for longer than N seconds.  Instead of parsing the output of ps(1) and
then calling kill(1) myself, I have attached a patch to add the "-r runtime"
flag to pkill(1).

See http://mail-index.netbsd.org/tech-userlevel/2007/07/26/0003.html for
a discussion about this feature in NetBSD.
>How-To-Repeat:

>Fix:


Patch attached with submission follows:

diff -bur pkill.orig/pkill.1 pkill/pkill.1
--- pkill.orig/pkill.1	2006-12-20 03:57:22.000000000 -0800
+++ pkill/pkill.1	2007-11-22 11:10:15.000000000 -0800
@@ -50,6 +50,7 @@
 .Op Fl M Ar core
 .Op Fl N Ar system
 .Op Fl P Ar ppid
+.Op Fl r Ar runtime
 .Op Fl U Ar uid
 .Op Fl d Ar delim
 .Op Fl g Ar pgrp
@@ -66,6 +67,7 @@
 .Op Fl M Ar core
 .Op Fl N Ar system
 .Op Fl P Ar ppid
+.Op Fl r Ar runtime
 .Op Fl U Ar uid
 .Op Fl g Ar pgrp
 .Op Fl j Ar jid
@@ -116,6 +118,9 @@
 Restrict matches to processes with a parent process ID in the
 comma-separated list
 .Ar ppid .
+.It Fl r Ar runtime
+Restrict matches to processes with a running time (ie wall clock) larger than
+.Ar runtime .
 .It Fl S
 Search also in system processes (kernel threads).
 .It Fl U Ar uid
diff -bur pkill.orig/pkill.c pkill/pkill.c
--- pkill.orig/pkill.c	2006-11-23 03:55:17.000000000 -0800
+++ pkill/pkill.c	2007-11-22 11:13:03.000000000 -0800
@@ -106,6 +106,7 @@
 static int	interactive;
 static int	inverse;
 static int	longfmt;
+static int	runtime;
 static int	matchargs;
 static int	fullmatch;
 static int	kthreads;
@@ -180,7 +181,7 @@
 	pidfilelock = 0;
 	execf = coref = _PATH_DEVNULL;
 
-	while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:d:fg:ij:lnos:t:u:vx")) != -1)
+	while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:d:fg:ij:lnor:s:t:u:vx")) != -1)
 		switch (ch) {
 		case 'D':
 			debug_opt++;
@@ -252,6 +253,14 @@
 			oldest = 1;
 			criteria = 1;
 			break;
+		case 'r':
+			runtime = strtol(optarg, &q, 10);
+			if (*q) {
+				errx(EXIT_FAILURE, "illegal runtime: %s", optarg);
+				/* NOTREACHED */
+			}
+			criteria = 1;
+			break;
 		case 's':
 			makelist(&sidlist, LT_SID, optarg);
 			criteria = 1;
@@ -364,6 +373,14 @@
 						selected[i] = 1;
 				} else
 					selected[i] = 1;
+
+				if (runtime) {
+					if (kp->p_ustart_sec < (time(NULL) - runtime)) {
+						selected[i] = 1;
+					} else {
+						selected[i] = 0;
+					}
+				}
 			} else if (rv != REG_NOMATCH) {
 				regerror(rv, &reg, buf, sizeof(buf));
 				errx(STATUS_ERROR,
@@ -464,9 +481,15 @@
 			continue;
 		}
 
-		if (argc == 0)
+		if (argc == 0) {
+			if (runtime) {
+				if (kp->p_ustart_sec < (time(NULL) - runtime)) {
 			selected[i] = 1;
 	}
+			} else
+				selected[i] = 1;
+		}
+	}
 
 	if (newest || oldest) {
 		best_tval.tv_sec = 0;
@@ -528,9 +551,9 @@
 
 	fprintf(stderr,
 		"usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n"
-		"             [-P ppid] [-U uid] [-g pgrp] [-j jid] [-s sid]\n"
-		"             [-t tty] [-u euid] pattern ...\n", getprogname(),
-		ustr);
+		"             [-P ppid] [-U uid] [-g pgrp] [-j jid] [-r runtime]\n"
+		"             [-s sid] [-t tty] [-u euid] pattern ...\n",
+		getprogname(), ustr);
 
 	exit(STATUS_BADUSAGE);
 }


>Release-Note:
>Audit-Trail:
>Unformatted:
