xref: /freebsd/bin/pkill/tests/pkill-j_test.sh (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1#!/bin/sh
2
3jail_name_to_jid()
4{
5	local check_name="$1"
6	jls -j "$check_name" -s | tr ' ' '\n' | grep jid= | sed -e 's/.*=//g'
7}
8
9base=pkill_j_test
10
11if [ "$(id -u)" -ne 0 ]; then
12	echo "1..0 # skip Test needs uid 0."
13	exit 0
14fi
15
16echo "1..4"
17
18sleep=$(pwd)/sleep.txt
19ln -sf /bin/sleep $sleep
20
21name="pkill -j <jid>"
22sleep_amount=15
23jail -c path=/ name=${base}_1_1 ip4.addr=127.0.0.1 \
24    command=daemon -p ${PWD}/${base}_1_1.pid $sleep $sleep_amount &
25
26jail -c path=/ name=${base}_1_2 ip4.addr=127.0.0.1 \
27    command=daemon -p ${PWD}/${base}_1_2.pid $sleep $sleep_amount &
28
29$sleep $sleep_amount &
30
31for i in $(seq 1 10); do
32	jid1=$(jail_name_to_jid ${base}_1_1)
33	jid2=$(jail_name_to_jid ${base}_1_2)
34	jid="${jid1},${jid2}"
35	case "$jid" in
36	[0-9]*,[0-9]*)
37		break
38		;;
39	*)
40		echo "Did not match: '${jid}'" >&2
41		;;
42	esac
43	sleep 0.1
44done
45sleep 0.5
46
47if pkill -f -j "$jid" $sleep && sleep 0.5 &&
48    ! test -f "${PWD}/${base}_1_1.pid" &&
49    ! test -f "${PWD}/${base}_1_2.pid" ; then
50	echo "ok 1 - $name"
51else
52	echo "not ok 1 - $name"
53fi 2>/dev/null
54[ -f ${PWD}/${base}_1_1.pid ] && kill "$(cat ${PWD}/${base}_1_1.pid)"
55[ -f ${PWD}/${base}_1_2.pid ] && kill "$(cat ${PWD}/${base}_1_2.pid)"
56wait
57
58name="pkill -j any"
59sleep_amount=16
60jail -c path=/ name=${base}_2_1 ip4.addr=127.0.0.1 \
61    command=daemon -p ${PWD}/${base}_2_1.pid $sleep $sleep_amount &
62
63jail -c path=/ name=${base}_2_2 ip4.addr=127.0.0.1 \
64    command=daemon -p ${PWD}/${base}_2_2.pid $sleep $sleep_amount &
65
66$sleep $sleep_amount &
67chpid3=$!
68sleep 0.5
69if pkill -f -j any $sleep && sleep 0.5 &&
70    ! test -f ${PWD}/${base}_2_1.pid &&
71    ! test -f ${PWD}/${base}_2_2.pid && kill $chpid3; then
72	echo "ok 2 - $name"
73else
74	echo "not ok 2 - $name"
75fi 2>/dev/null
76[ -f ${PWD}/${base}_2_1.pid ] && kill "$(cat ${PWD}/${base}_2_1.pid)"
77[ -f ${PWD}/${base}_2_2.pid ] && kill "$(cat ${PWD}/${base}_2_2.pid)"
78wait
79
80name="pkill -j none"
81sleep_amount=17
82daemon -p ${PWD}/${base}_3_1.pid $sleep $sleep_amount
83jail -c path=/ name=${base}_3_2 ip4.addr=127.0.0.1 \
84    command=daemon -p ${PWD}/${base}_3_2.pid $sleep $sleep_amount &
85sleep 1
86if pkill -f -j none "$sleep $sleep_amount" && sleep 1 &&
87    [ ! -f ${PWD}/${base}_3_1.pid -a -f ${PWD}/${base}_3_2.pid ] ; then
88	echo "ok 3 - $name"
89else
90	ls ${PWD}/*.pid
91	echo "not ok 3 - $name"
92fi 2>/dev/null
93[ -f ${PWD}/${base}_3_1.pid ] && kill "$(cat ${base}_3_1.pid)"
94[ -f ${PWD}/${base}_3_2.pid ] && kill "$(cat ${base}_3_2.pid)"
95wait
96
97# test 4 is like test 1 except with jname instead of jid.
98name="pkill -j <jname>"
99sleep_amount=18
100jail -c path=/ name=${base}_4_1 ip4.addr=127.0.0.1 \
101    command=daemon -p ${PWD}/${base}_4_1.pid $sleep $sleep_amount &
102
103jail -c path=/ name=${base}_4_2 ip4.addr=127.0.0.1 \
104    command=daemon -p ${PWD}/${base}_4_2.pid $sleep $sleep_amount &
105
106$sleep $sleep_amount &
107
108sleep 0.5
109
110jname="${base}_4_1,${base}_4_2"
111if pkill -f -j "$jname" $sleep && sleep 0.5 &&
112    ! test -f ${PWD}/${base}_4_1.pid &&
113    ! test -f ${PWD}/${base}_4_2.pid ; then
114	echo "ok 4 - $name"
115else
116	echo "not ok 4 - $name"
117fi 2>/dev/null
118[ -f ${PWD}/${base}_4_1.pid ] && kill "$(cat ${PWD}/${base}_4_1.pid)"
119[ -f ${PWD}/${base}_4_2.pid ] && kill "$(cat ${PWD}/${base}_4_2.pid)"
120wait
121
122rm -f $sleep
123