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