xref: /freebsd/bin/pkill/tests/pkill-j_test.sh (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
165c3cfc1SJulio Merino#!/bin/sh
265c3cfc1SJulio Merino
36677932eSCraig Rodriguesjail_name_to_jid()
46677932eSCraig Rodrigues{
56677932eSCraig Rodrigues	local check_name="$1"
67a2065efSEnji Cooper	jls -j "$check_name" -s | tr ' ' '\n' | grep jid= | sed -e 's/.*=//g'
76677932eSCraig Rodrigues}
86677932eSCraig Rodrigues
96677932eSCraig Rodriguesbase=pkill_j_test
1065c3cfc1SJulio Merino
11*5ff2e55eSAlex Richardsonif [ "$(id -u)" -ne 0 ]; then
12bacc28ceSEnji Cooper	echo "1..0 # skip Test needs uid 0."
13bacc28ceSEnji Cooper	exit 0
14bacc28ceSEnji Cooperfi
15bacc28ceSEnji Cooper
16c4f0631fSJamie Grittonecho "1..4"
1765c3cfc1SJulio Merino
1865c3cfc1SJulio Merinosleep=$(pwd)/sleep.txt
1965c3cfc1SJulio Merinoln -sf /bin/sleep $sleep
20bacc28ceSEnji Cooper
21bacc28ceSEnji Coopername="pkill -j <jid>"
2251acc171SRuslan Bukinsleep_amount=15
236677932eSCraig Rodriguesjail -c path=/ name=${base}_1_1 ip4.addr=127.0.0.1 \
24bacc28ceSEnji Cooper    command=daemon -p ${PWD}/${base}_1_1.pid $sleep $sleep_amount &
256677932eSCraig Rodrigues
266677932eSCraig Rodriguesjail -c path=/ name=${base}_1_2 ip4.addr=127.0.0.1 \
27bacc28ceSEnji Cooper    command=daemon -p ${PWD}/${base}_1_2.pid $sleep $sleep_amount &
286677932eSCraig Rodrigues
29bacc28ceSEnji Cooper$sleep $sleep_amount &
30bacc28ceSEnji Cooper
31*5ff2e55eSAlex Richardsonfor i in $(seq 1 10); do
326677932eSCraig Rodrigues	jid1=$(jail_name_to_jid ${base}_1_1)
336677932eSCraig Rodrigues	jid2=$(jail_name_to_jid ${base}_1_2)
346677932eSCraig Rodrigues	jid="${jid1},${jid2}"
35bacc28ceSEnji Cooper	case "$jid" in
36*5ff2e55eSAlex Richardson	[0-9]*,[0-9]*)
37bacc28ceSEnji Cooper		break
38bacc28ceSEnji Cooper		;;
39*5ff2e55eSAlex Richardson	*)
40*5ff2e55eSAlex Richardson		echo "Did not match: '${jid}'" >&2
41*5ff2e55eSAlex Richardson		;;
42bacc28ceSEnji Cooper	esac
43bacc28ceSEnji Cooper	sleep 0.1
44bacc28ceSEnji Cooperdone
457a2065efSEnji Coopersleep 0.5
46bacc28ceSEnji Cooper
476677932eSCraig Rodriguesif pkill -f -j "$jid" $sleep && sleep 0.5 &&
48*5ff2e55eSAlex Richardson    ! test -f "${PWD}/${base}_1_1.pid" &&
49*5ff2e55eSAlex Richardson    ! test -f "${PWD}/${base}_1_2.pid" ; then
5065c3cfc1SJulio Merino	echo "ok 1 - $name"
5165c3cfc1SJulio Merinoelse
5265c3cfc1SJulio Merino	echo "not ok 1 - $name"
5365c3cfc1SJulio Merinofi 2>/dev/null
54*5ff2e55eSAlex Richardson[ -f ${PWD}/${base}_1_1.pid ] && kill "$(cat ${PWD}/${base}_1_1.pid)"
55*5ff2e55eSAlex Richardson[ -f ${PWD}/${base}_1_2.pid ] && kill "$(cat ${PWD}/${base}_1_2.pid)"
566677932eSCraig Rodrigueswait
5765c3cfc1SJulio Merino
5865c3cfc1SJulio Merinoname="pkill -j any"
5951acc171SRuslan Bukinsleep_amount=16
606677932eSCraig Rodriguesjail -c path=/ name=${base}_2_1 ip4.addr=127.0.0.1 \
61bacc28ceSEnji Cooper    command=daemon -p ${PWD}/${base}_2_1.pid $sleep $sleep_amount &
626677932eSCraig Rodrigues
636677932eSCraig Rodriguesjail -c path=/ name=${base}_2_2 ip4.addr=127.0.0.1 \
64bacc28ceSEnji Cooper    command=daemon -p ${PWD}/${base}_2_2.pid $sleep $sleep_amount &
656677932eSCraig Rodrigues
66bacc28ceSEnji Cooper$sleep $sleep_amount &
676677932eSCraig Rodrigueschpid3=$!
68bacc28ceSEnji Coopersleep 0.5
6965c3cfc1SJulio Merinoif pkill -f -j any $sleep && sleep 0.5 &&
70*5ff2e55eSAlex Richardson    ! test -f ${PWD}/${base}_2_1.pid &&
71*5ff2e55eSAlex Richardson    ! test -f ${PWD}/${base}_2_2.pid && kill $chpid3; then
7265c3cfc1SJulio Merino	echo "ok 2 - $name"
7365c3cfc1SJulio Merinoelse
7465c3cfc1SJulio Merino	echo "not ok 2 - $name"
7565c3cfc1SJulio Merinofi 2>/dev/null
76*5ff2e55eSAlex Richardson[ -f ${PWD}/${base}_2_1.pid ] && kill "$(cat ${PWD}/${base}_2_1.pid)"
77*5ff2e55eSAlex Richardson[ -f ${PWD}/${base}_2_2.pid ] && kill "$(cat ${PWD}/${base}_2_2.pid)"
786677932eSCraig Rodrigueswait
7965c3cfc1SJulio Merino
8065c3cfc1SJulio Merinoname="pkill -j none"
8151acc171SRuslan Bukinsleep_amount=17
82bacc28ceSEnji Cooperdaemon -p ${PWD}/${base}_3_1.pid $sleep $sleep_amount
836677932eSCraig Rodriguesjail -c path=/ name=${base}_3_2 ip4.addr=127.0.0.1 \
84bacc28ceSEnji Cooper    command=daemon -p ${PWD}/${base}_3_2.pid $sleep $sleep_amount &
856677932eSCraig Rodriguessleep 1
86bacc28ceSEnji Cooperif pkill -f -j none "$sleep $sleep_amount" && sleep 1 &&
876677932eSCraig Rodrigues    [ ! -f ${PWD}/${base}_3_1.pid -a -f ${PWD}/${base}_3_2.pid ] ; then
8865c3cfc1SJulio Merino	echo "ok 3 - $name"
8965c3cfc1SJulio Merinoelse
906677932eSCraig Rodrigues	ls ${PWD}/*.pid
9165c3cfc1SJulio Merino	echo "not ok 3 - $name"
9265c3cfc1SJulio Merinofi 2>/dev/null
93*5ff2e55eSAlex Richardson[ -f ${PWD}/${base}_3_1.pid ] && kill "$(cat ${base}_3_1.pid)"
94*5ff2e55eSAlex Richardson[ -f ${PWD}/${base}_3_2.pid ] && kill "$(cat ${base}_3_2.pid)"
95c4f0631fSJamie Grittonwait
96c4f0631fSJamie Gritton
97c4f0631fSJamie Gritton# test 4 is like test 1 except with jname instead of jid.
98c4f0631fSJamie Grittonname="pkill -j <jname>"
9951acc171SRuslan Bukinsleep_amount=18
100c4f0631fSJamie Grittonjail -c path=/ name=${base}_4_1 ip4.addr=127.0.0.1 \
101c4f0631fSJamie Gritton    command=daemon -p ${PWD}/${base}_4_1.pid $sleep $sleep_amount &
102c4f0631fSJamie Gritton
103c4f0631fSJamie Grittonjail -c path=/ name=${base}_4_2 ip4.addr=127.0.0.1 \
104c4f0631fSJamie Gritton    command=daemon -p ${PWD}/${base}_4_2.pid $sleep $sleep_amount &
105c4f0631fSJamie Gritton
106c4f0631fSJamie Gritton$sleep $sleep_amount &
107c4f0631fSJamie Gritton
108c4f0631fSJamie Grittonsleep 0.5
109c4f0631fSJamie Gritton
110c4f0631fSJamie Grittonjname="${base}_4_1,${base}_4_2"
111c4f0631fSJamie Grittonif pkill -f -j "$jname" $sleep && sleep 0.5 &&
112*5ff2e55eSAlex Richardson    ! test -f ${PWD}/${base}_4_1.pid &&
113*5ff2e55eSAlex Richardson    ! test -f ${PWD}/${base}_4_2.pid ; then
114c4f0631fSJamie Gritton	echo "ok 4 - $name"
115c4f0631fSJamie Grittonelse
116c4f0631fSJamie Gritton	echo "not ok 4 - $name"
117c4f0631fSJamie Grittonfi 2>/dev/null
118*5ff2e55eSAlex Richardson[ -f ${PWD}/${base}_4_1.pid ] && kill "$(cat ${PWD}/${base}_4_1.pid)"
119*5ff2e55eSAlex Richardson[ -f ${PWD}/${base}_4_2.pid ] && kill "$(cat ${PWD}/${base}_4_2.pid)"
120c4f0631fSJamie Grittonwait
121bacc28ceSEnji Cooper
122bacc28ceSEnji Cooperrm -f $sleep
123