From c4b111a2732b06d29af6310d22e942c6bbb7e888 Mon Sep 17 00:00:00 2001 From: jamie Date: Sat, 22 Aug 2015 05:04:36 +0000 Subject: [PATCH] Make pkill/pgrep -j ARG take jname, not just jid. PR: 201588 Submitted by: Daniel Shahaf MFC after: 3 days --- bin/pkill/Makefile | 2 +- bin/pkill/Makefile.depend | 1 + bin/pkill/pkill.1 | 22 +++++++++++----------- bin/pkill/pkill.c | 18 ++++++++++++------ bin/pkill/tests/pgrep-j_test.sh | 27 ++++++++++++++++++++++++++- bin/pkill/tests/pkill-j_test.sh | 28 +++++++++++++++++++++++++++- 6 files changed, 78 insertions(+), 20 deletions(-) diff --git a/bin/pkill/Makefile b/bin/pkill/Makefile index 84a5af71ddf3..2985b37dbff1 100644 --- a/bin/pkill/Makefile +++ b/bin/pkill/Makefile @@ -5,7 +5,7 @@ PROG= pkill -LIBADD= kvm +LIBADD= kvm jail LINKS= ${BINDIR}/pkill ${BINDIR}/pgrep MLINKS= pkill.1 pgrep.1 diff --git a/bin/pkill/Makefile.depend b/bin/pkill/Makefile.depend index a1ac545aff09..2940edd9368d 100644 --- a/bin/pkill/Makefile.depend +++ b/bin/pkill/Makefile.depend @@ -9,6 +9,7 @@ DIRDEPS = \ lib/${CSU_DIR} \ lib/libc \ lib/libcompiler_rt \ + lib/libjail \ lib/libkvm \ diff --git a/bin/pkill/pkill.1 b/bin/pkill/pkill.1 index 3f219b69a0f3..70b791213895 100644 --- a/bin/pkill/pkill.1 +++ b/bin/pkill/pkill.1 @@ -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 August 9, 2013 +.Dd August 21, 2015 .Dt PKILL 1 .Os .Sh NAME @@ -47,7 +47,7 @@ .Op Fl c Ar class .Op Fl d Ar delim .Op Fl g Ar pgrp -.Op Fl j Ar jid +.Op Fl j Ar jail .Op Fl s Ar sid .Op Fl t Ar tty .Op Fl u Ar euid @@ -63,7 +63,7 @@ .Op Fl U Ar uid .Op Fl c Ar class .Op Fl g Ar pgrp -.Op Fl j Ar jid +.Op Fl j Ar jail .Op Fl s Ar sid .Op Fl t Ar tty .Op Fl u Ar euid @@ -149,16 +149,16 @@ or command. .It Fl i Ignore case distinctions in both the process table and the supplied pattern. -.It Fl j Ar jid -Restrict matches to processes inside jails with a jail ID in the comma-separated -list -.Ar jid . -The value +.It Fl j Ar jail +Restrict matches to processes inside the specified jails. +The argument +.Ar jail +may be .Dq Li any -matches processes in any jail. -The value +to match processes in any jail, .Dq Li none -matches processes not in jail. +to match processes not in jail, +or a comma-separated list of jail IDs or names. .It Fl l Long output. For diff --git a/bin/pkill/pkill.c b/bin/pkill/pkill.c index e73f5f258e3d..8c814ce4b0f7 100644 --- a/bin/pkill/pkill.c +++ b/bin/pkill/pkill.c @@ -59,6 +59,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #define STATUS_MATCH 0 #define STATUS_NOMATCH 1 @@ -78,7 +79,7 @@ enum listtype { LT_GROUP, LT_TTY, LT_PGRP, - LT_JID, + LT_JAIL, LT_SID, LT_CLASS }; @@ -245,7 +246,7 @@ main(int argc, char **argv) cflags |= REG_ICASE; break; case 'j': - makelist(&jidlist, LT_JID, optarg); + makelist(&jidlist, LT_JAIL, optarg); criteria = 1; break; case 'l': @@ -585,7 +586,7 @@ usage(void) fprintf(stderr, "usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n" - " [-P ppid] [-U uid] [-c class] [-g pgrp] [-j jid]\n" + " [-P ppid] [-U uid] [-c class] [-g pgrp] [-j jail]\n" " [-s sid] [-t tty] [-u euid] pattern ...\n", getprogname(), ustr); @@ -700,7 +701,7 @@ makelist(struct listhead *head, enum listtype type, char *src) if (li->li_number == 0) li->li_number = getsid(mypid); break; - case LT_JID: + case LT_JAIL: if (li->li_number < 0) errx(STATUS_BADUSAGE, "Negative jail ID `%s'", sp); @@ -766,15 +767,20 @@ foundtty: if ((st.st_mode & S_IFCHR) == 0) li->li_number = st.st_rdev; break; - case LT_JID: + case LT_JAIL: { + int jid; + if (strcmp(sp, "none") == 0) li->li_number = 0; else if (strcmp(sp, "any") == 0) li->li_number = -1; + else if ((jid = jail_getid(sp)) != -1) + li->li_number = jid; else if (*ep != '\0') errx(STATUS_BADUSAGE, - "Invalid jail ID `%s'", sp); + "Invalid jail ID or name `%s'", sp); break; + } case LT_CLASS: li->li_number = -1; li->li_name = strdup(sp); diff --git a/bin/pkill/tests/pgrep-j_test.sh b/bin/pkill/tests/pgrep-j_test.sh index e24e5c2ff5fe..1d6281368075 100644 --- a/bin/pkill/tests/pgrep-j_test.sh +++ b/bin/pkill/tests/pgrep-j_test.sh @@ -14,7 +14,7 @@ if [ `id -u` -ne 0 ]; then exit 0 fi -echo "1..3" +echo "1..4" sleep=$(pwd)/sleep.txt ln -sf /bin/sleep $sleep @@ -87,5 +87,30 @@ else fi [ -f ${PWD}/${base}_3_1.pid ] && kill $(cat $PWD/${base}_3_1.pid) [ -f ${PWD}/${base}_3_2.pid ] && kill $(cat $PWD/${base}_3_2.pid) +wait + +# test 4 is like test 1 except with jname instead of jid. +name="pgrep -j " +sleep_amount=8 +jail -c path=/ name=${base}_4_1 ip4.addr=127.0.0.1 \ + command=daemon -p ${PWD}/${base}_4_1.pid $sleep $sleep_amount & + +jail -c path=/ name=${base}_4_2 ip4.addr=127.0.0.1 \ + command=daemon -p ${PWD}/${base}_4_2.pid $sleep $sleep_amount & + +sleep 0.5 + +jname="${base}_4_1,${base}_4_2" +pid1="$(pgrep -f -x -j "$jname" "$sleep $sleep_amount" | sort)" +pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_4_1.pid)" \ + $(cat ${PWD}/${base}_4_2.pid) | sort) +if [ "$pid1" = "$pid2" ]; then + echo "ok 4 - $name" +else + echo "not ok 4 - $name # pgrep output: '$(echo $pid1)', pidfile output: '$(echo $pid2)'" +fi +[ -f ${PWD}/${base}_4_1.pid ] && kill $(cat ${PWD}/${base}_4_1.pid) +[ -f ${PWD}/${base}_4_2.pid ] && kill $(cat ${PWD}/${base}_4_2.pid) +wait rm -f $sleep diff --git a/bin/pkill/tests/pkill-j_test.sh b/bin/pkill/tests/pkill-j_test.sh index 26e185fc8988..0af24cfc86fa 100644 --- a/bin/pkill/tests/pkill-j_test.sh +++ b/bin/pkill/tests/pkill-j_test.sh @@ -14,7 +14,7 @@ if [ `id -u` -ne 0 ]; then exit 0 fi -echo "1..3" +echo "1..4" sleep=$(pwd)/sleep.txt ln -sf /bin/sleep $sleep @@ -90,5 +90,31 @@ else fi 2>/dev/null [ -f ${PWD}/${base}_3_1.pid ] && kill $(cat ${base}_3_1.pid) [ -f ${PWD}/${base}_3_2.pid ] && kill $(cat ${base}_3_2.pid) +wait + +# test 4 is like test 1 except with jname instead of jid. +name="pkill -j " +sleep_amount=8 +jail -c path=/ name=${base}_4_1 ip4.addr=127.0.0.1 \ + command=daemon -p ${PWD}/${base}_4_1.pid $sleep $sleep_amount & + +jail -c path=/ name=${base}_4_2 ip4.addr=127.0.0.1 \ + command=daemon -p ${PWD}/${base}_4_2.pid $sleep $sleep_amount & + +$sleep $sleep_amount & + +sleep 0.5 + +jname="${base}_4_1,${base}_4_2" +if pkill -f -j "$jname" $sleep && sleep 0.5 && + ! -f ${PWD}/${base}_4_1.pid && + ! -f ${PWD}/${base}_4_2.pid ; then + echo "ok 4 - $name" +else + echo "not ok 4 - $name" +fi 2>/dev/null +[ -f ${PWD}/${base}_4_1.pid ] && kill $(cat ${PWD}/${base}_4_1.pid) +[ -f ${PWD}/${base}_4_2.pid ] && kill $(cat ${PWD}/${base}_4_2.pid) +wait rm -f $sleep