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