Make pkill/pgrep -j ARG take jname, not just jid.

PR:		201588
Submitted by:	Daniel Shahaf <danielsh at apache.org>
MFC after:	3 days
This commit is contained in:
Jamie Gritton 2015-08-22 05:04:36 +00:00
parent fa7a1ca7ae
commit c4f0631f19
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=287012
6 changed files with 78 additions and 20 deletions

View File

@ -5,7 +5,7 @@
PROG= pkill
LIBADD= kvm
LIBADD= kvm jail
LINKS= ${BINDIR}/pkill ${BINDIR}/pgrep
MLINKS= pkill.1 pgrep.1

View File

@ -9,6 +9,7 @@ DIRDEPS = \
lib/${CSU_DIR} \
lib/libc \
lib/libcompiler_rt \
lib/libjail \
lib/libkvm \

View File

@ -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

View File

@ -59,6 +59,7 @@ __FBSDID("$FreeBSD$");
#include <grp.h>
#include <errno.h>
#include <locale.h>
#include <jail.h>
#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);

View File

@ -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 <jname>"
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

View File

@ -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 <jname>"
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