svn commit: r256054 - stable/9/bin/pkill

Edward Tomasz Napierala trasz at FreeBSD.org
Fri Oct 4 17:52:57 UTC 2013


Author: trasz
Date: Fri Oct  4 17:52:56 2013
New Revision: 256054
URL: http://svnweb.freebsd.org/changeset/base/256054

Log:
  MFC r254134:
  
  Add -c flag to pgrep(1) and pkill(1), to match login classes.

Modified:
  stable/9/bin/pkill/pkill.1
  stable/9/bin/pkill/pkill.c
Directory Properties:
  stable/9/bin/pkill/   (props changed)

Modified: stable/9/bin/pkill/pkill.1
==============================================================================
--- stable/9/bin/pkill/pkill.1	Fri Oct  4 17:21:01 2013	(r256053)
+++ stable/9/bin/pkill/pkill.1	Fri Oct  4 17:52:56 2013	(r256054)
@@ -29,7 +29,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd February 11, 2010
+.Dd August 9, 2013
 .Dt PKILL 1
 .Os
 .Sh NAME
@@ -44,6 +44,7 @@
 .Op Fl N Ar system
 .Op Fl P Ar ppid
 .Op Fl U Ar uid
+.Op Fl c Ar class
 .Op Fl d Ar delim
 .Op Fl g Ar pgrp
 .Op Fl j Ar jid
@@ -60,6 +61,7 @@
 .Op Fl N Ar system
 .Op Fl P Ar ppid
 .Op Fl U Ar uid
+.Op Fl c Ar class
 .Op Fl g Ar pgrp
 .Op Fl j Ar jid
 .Op Fl s Ar sid
@@ -130,6 +132,9 @@ or
 process and all of its ancestors are excluded (unless
 .Fl v
 is used).
+.It Fl c Ar class
+Restrict matches to processes running with specified login class
+.Ar class .
 .It Fl f
 Match against full argument lists.
 The default is to match against process names.

Modified: stable/9/bin/pkill/pkill.c
==============================================================================
--- stable/9/bin/pkill/pkill.c	Fri Oct  4 17:21:01 2013	(r256053)
+++ stable/9/bin/pkill/pkill.c	Fri Oct  4 17:52:56 2013	(r256054)
@@ -79,12 +79,14 @@ enum listtype {
 	LT_TTY,
 	LT_PGRP,
 	LT_JID,
-	LT_SID
+	LT_SID,
+	LT_CLASS
 };
 
 struct list {
 	SLIST_ENTRY(list) li_chain;
 	long	li_number;
+	char	*li_name;
 };
 
 SLIST_HEAD(listhead, list);
@@ -116,6 +118,7 @@ static struct listhead ppidlist = SLIST_
 static struct listhead tdevlist = SLIST_HEAD_INITIALIZER(tdevlist);
 static struct listhead sidlist = SLIST_HEAD_INITIALIZER(sidlist);
 static struct listhead jidlist = SLIST_HEAD_INITIALIZER(jidlist);
+static struct listhead classlist = SLIST_HEAD_INITIALIZER(classlist);
 
 static void	usage(void) __attribute__((__noreturn__));
 static int	killact(const struct kinfo_proc *);
@@ -179,7 +182,7 @@ main(int argc, char **argv)
 	execf = NULL;
 	coref = _PATH_DEVNULL;
 
-	while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:ad:fg:ij:lnoqs:t:u:vx")) != -1)
+	while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:ac:d:fg:ij:lnoqs:t:u:vx")) != -1)
 		switch (ch) {
 		case 'D':
 			debug_opt++;
@@ -222,6 +225,10 @@ main(int argc, char **argv)
 		case 'a':
 			ancestors++;
 			break;
+		case 'c':
+			makelist(&classlist, LT_CLASS, optarg);
+			criteria = 1;
+			break;
 		case 'd':
 			if (!pgrep)
 				usage();
@@ -469,6 +476,20 @@ main(int argc, char **argv)
 			continue;
 		}
 
+		SLIST_FOREACH(li, &classlist, li_chain) {
+			/*
+			 * We skip P_SYSTEM processes to match ps(1) output.
+			 */
+			if ((kp->ki_flag & P_SYSTEM) == 0 &&
+			    kp->ki_loginclass != NULL &&
+			    strcmp(kp->ki_loginclass, li->li_name) == 0)
+				break;
+		}
+		if (SLIST_FIRST(&classlist) != NULL && li == NULL) {
+			selected[i] = 0;
+			continue;
+		}
+
 		if (argc == 0)
 			selected[i] = 1;
 	}
@@ -562,9 +583,9 @@ usage(void)
 
 	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] [-c class] [-g pgrp] [-j jid]\n"
+		"             [-s sid] [-t tty] [-u euid] pattern ...\n",
+		getprogname(), ustr);
 
 	exit(STATUS_BADUSAGE);
 }
@@ -664,8 +685,10 @@ makelist(struct listhead *head, enum lis
 		SLIST_INSERT_HEAD(head, li, li_chain);
 		empty = 0;
 
-		li->li_number = (uid_t)strtol(sp, &ep, 0);
-		if (*ep == '\0') {
+		if (type != LT_CLASS)
+			li->li_number = (uid_t)strtol(sp, &ep, 0);
+
+		if (type != LT_CLASS && *ep == '\0') {
 			switch (type) {
 			case LT_PGRP:
 				if (li->li_number == 0)
@@ -750,6 +773,12 @@ foundtty:		if ((st.st_mode & S_IFCHR) ==
 				errx(STATUS_BADUSAGE,
 				     "Invalid jail ID `%s'", sp);
 			break;
+		case LT_CLASS:
+			li->li_number = -1;
+			li->li_name = strdup(sp);
+			if (li->li_name == NULL)
+				err(STATUS_ERROR, "Cannot allocate memory");
+			break;
 		default:
 			usage();
 		}


More information about the svn-src-stable-9 mailing list