From 54499c336ab52f8d78c5458ee392b0cfb5431bd0 Mon Sep 17 00:00:00 2001 From: Chris Turner Date: Sat, 10 Mar 2012 11:37:48 +0000 Subject: [PATCH] pkill(1): add '-j jid' flag to restrict matches to jailed processes. Add a '-j' option to pgrep(1)/pkill(1). Update manual page to reflect usage. As DragonFlyBSD did not have this option previously, the 'COMPATIBILITY' notes outlined in FreeBSD's '-j' options do not apply - namely, the '-j 0' option defaults to non-jailed processes, and '-1' is used to indicate all jailed processes should be matched. Also, the 'any'/'none' string options were not ported as they require more invasive changes. Inspired-by: FreeBSD --- usr.bin/pkill/pkill.1 | 12 ++++++++++++ usr.bin/pkill/pkill.c | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/usr.bin/pkill/pkill.1 b/usr.bin/pkill/pkill.1 index ed1498087f..00a8d5696f 100644 --- a/usr.bin/pkill/pkill.1 +++ b/usr.bin/pkill/pkill.1 @@ -53,6 +53,7 @@ .Op Fl s Ar sid .Op Fl t Ar tty .Op Fl u Ar euid +.Op Fl j Ar jid .Op Ar pattern Op ... .Nm pkill .Op Fl signal @@ -64,6 +65,7 @@ .Op Fl s Ar sid .Op Fl t Ar tty .Op Fl u Ar euid +.Op Fl j Ar jid .Op Ar pattern Op ... .Sh DESCRIPTION The @@ -109,6 +111,16 @@ The value zero is taken to mean the process group ID of the running or .Nm pkill command. +.It Fl j Ar jid +Restrict matches to processes inside jails with a jail ID in the comma-separated +list +.Ar jid . +The value +.Dq Li -1 +matches processes in any jail. +The value +.Dq Li 0 +matches processes not in a jail. .It Fl l Long output. Print the process name in addition to the process ID for each matching diff --git a/usr.bin/pkill/pkill.c b/usr.bin/pkill/pkill.c index 3766bfa3b4..4ef27e7021 100644 --- a/usr.bin/pkill/pkill.c +++ b/usr.bin/pkill/pkill.c @@ -71,7 +71,8 @@ enum listtype { LT_TTY, /* tty: dev_t */ LT_PPID, /* parent pid: pid_t */ LT_PGRP, /* process group: pid_t */ - LT_SID /* session id: pid_t */ + LT_SID, /* session id: pid_t */ + LT_JID /* jail id: pid_t */ }; struct list { @@ -107,6 +108,7 @@ struct listhead pgrplist = SLIST_HEAD_INITIALIZER(list); struct listhead ppidlist = SLIST_HEAD_INITIALIZER(list); struct listhead tdevlist = SLIST_HEAD_INITIALIZER(list); struct listhead sidlist = SLIST_HEAD_INITIALIZER(list); +struct listhead jidlist = SLIST_HEAD_INITIALIZER(list); void usage(void); void killact(struct kinfo_proc *, int); @@ -166,7 +168,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:j:lns:t:u:vx")) != -1) { switch (ch) { case 'G': makelist(&rgidlist, LT_GROUP, optarg); @@ -192,6 +194,10 @@ main(int argc, char **argv) makelist(&pgrplist, LT_PGRP, optarg); criteria = 1; break; + case 'j': + makelist(&jidlist, LT_JID, optarg); + criteria = 1; + break; case 'l': if (!pgrep) usage(); @@ -373,6 +379,19 @@ main(int argc, char **argv) continue; } + SLIST_FOREACH(li, &jidlist, li_chain) { + /* A particular jail ID, including 0 (not in jail) */ + if (kp->kp_jailid == li->li_datum.ld_pid) + break; + /* Any jail */ + if (kp->kp_jailid > 0 && li->li_datum.ld_pid < 0) + break; + } + if (SLIST_FIRST(&jidlist) != NULL && li == NULL) { + selected[i] = 0; + continue; + } + if (argc == 0) selected[i] = 1; } @@ -437,8 +456,8 @@ usage(void) fprintf(stderr, "usage: %s %s [-G gid] [-P ppid] [-U uid] [-g pgrp] [-s sid]\n" - " [-t tty] [-u euid] pattern ...\n", getprogname(), - ustr); + " [-t tty] [-u euid] [-j jid] pattern ...\n", + getprogname(), ustr); exit(STATUS_ERROR); } @@ -533,6 +552,15 @@ makelist(struct listhead *head, enum listtype type, char *src) if (!parse_pid(sp, &p, li, getsid(mypid))) usage(); break; + case LT_JID: + /* default to no jail */ + if (!parse_pid(sp, &p, li, 0)) + usage(); + if (li->li_datum.ld_pid < -1) { + errx(STATUS_BADUSAGE, + "Negative jail ID `%s'", sp); + } + break; case LT_USER: li->li_datum.ld_uid = (uid_t)strtol(sp, &p, 0); if (*p != '\0') { -- 2.41.0