xref: /freebsd/bin/pkill/tests/pgrep-j_test.sh (revision b2d48be1bc7df45ddd13b143a160d0acb5a383c5)
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=5
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
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: '$(echo $pid1)', pidfile output: '$(echo $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=6
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: '$(echo $pid1)', pidfile output: '$(echo $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=7
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: '$(echo $pid1)', pidfile output: '$(echo $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=8
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