xref: /freebsd/bin/pkill/tests/pgrep-j_test.sh (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
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=pgrep_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="pgrep -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 &
29sleep 0.5
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	esac
40	sleep 0.1
41done
42sleep 0.5
43
44pid1="$(pgrep -f -x -j "$jid" "$sleep $sleep_amount" | sort)"
45pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_1_1.pid)" \
46    "$(cat ${PWD}/${base}_1_2.pid)" | sort)
47if [ "$pid1" = "$pid2" ]; then
48	echo "ok 1 - $name"
49else
50	echo "not ok 1 - $name # pgrep output: '$pid1', pidfile output: '$pid2'"
51fi
52[ -f ${PWD}/${base}_1_1.pid ] && kill "$(cat ${PWD}/${base}_1_1.pid)"
53[ -f ${PWD}/${base}_1_2.pid ] && kill "$(cat ${PWD}/${base}_1_2.pid)"
54wait
55
56name="pgrep -j any"
57sleep_amount=16
58jail -c path=/ name=${base}_2_1 ip4.addr=127.0.0.1 \
59    command=daemon -p ${PWD}/${base}_2_1.pid $sleep $sleep_amount &
60
61jail -c path=/ name=${base}_2_2 ip4.addr=127.0.0.1 \
62    command=daemon -p ${PWD}/${base}_2_2.pid $sleep $sleep_amount &
63
64sleep 2
65pid1="$(pgrep -f -x -j any "$sleep $sleep_amount" | sort)"
66pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_2_1.pid)" \
67    "$(cat ${PWD}/${base}_2_2.pid)" | sort)
68if [ "$pid1" = "$pid2" ]; then
69	echo "ok 2 - $name"
70else
71	echo "not ok 2 - $name # pgrep output: '$pid1', pidfile output: '$pid2'"
72fi
73[ -f ${PWD}/${base}_2_1.pid ] && kill "$(cat ${PWD}/${base}_2_1.pid)"
74[ -f ${PWD}/${base}_2_2.pid ] && kill "$(cat ${PWD}/${base}_2_2.pid)"
75wait
76
77name="pgrep -j none"
78sleep_amount=17
79daemon -p ${PWD}/${base}_3_1.pid $sleep $sleep_amount &
80jail -c path=/ name=${base}_3_2 ip4.addr=127.0.0.1 \
81    command=daemon -p ${PWD}/${base}_3_2.pid $sleep $sleep_amount &
82sleep 2
83pid="$(pgrep -f -x -j none "$sleep $sleep_amount")"
84if [ "$pid" = "$(cat ${PWD}/${base}_3_1.pid)" ]; then
85	echo "ok 3 - $name"
86else
87	echo "not ok 3 - $name # pgrep output: '$pid1', pidfile output: '$pid2'"
88fi
89[ -f ${PWD}/${base}_3_1.pid ] && kill "$(cat $PWD/${base}_3_1.pid)"
90[ -f ${PWD}/${base}_3_2.pid ] && kill "$(cat $PWD/${base}_3_2.pid)"
91wait
92
93# test 4 is like test 1 except with jname instead of jid.
94name="pgrep -j <jname>"
95sleep_amount=18
96jail -c path=/ name=${base}_4_1 ip4.addr=127.0.0.1 \
97    command=daemon -p ${PWD}/${base}_4_1.pid $sleep $sleep_amount &
98
99jail -c path=/ name=${base}_4_2 ip4.addr=127.0.0.1 \
100    command=daemon -p ${PWD}/${base}_4_2.pid $sleep $sleep_amount &
101
102sleep 0.5
103
104jname="${base}_4_1,${base}_4_2"
105pid1="$(pgrep -f -x -j "$jname" "$sleep $sleep_amount" | sort)"
106pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_4_1.pid)" \
107    "$(cat ${PWD}/${base}_4_2.pid)" | sort)
108if [ "$pid1" = "$pid2" ]; then
109	echo "ok 4 - $name"
110else
111	echo "not ok 4 - $name # pgrep output: '$(echo $pid1)', pidfile output: '$(echo $pid2)'"
112fi
113[ -f ${PWD}/${base}_4_1.pid ] && kill "$(cat ${PWD}/${base}_4_1.pid)"
114[ -f ${PWD}/${base}_4_2.pid ] && kill "$(cat ${PWD}/${base}_4_2.pid)"
115wait
116
117rm -f $sleep
118