]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/pkill/tests/pgrep-j_test.sh
sqlite3: Vendor import of sqlite3 3.45.0
[FreeBSD/FreeBSD.git] / bin / pkill / tests / pgrep-j_test.sh
1 #!/bin/sh
2
3 jail_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
9 base=pgrep_j_test
10
11 if [ "$(id -u)" -ne 0 ]; then
12         echo "1..0 # skip Test needs uid 0."
13         exit 0
14 fi
15
16 echo "1..4"
17
18 sleep=$(pwd)/sleep.txt
19 ln -sf /bin/sleep $sleep
20
21 name="pgrep -j <jid>"
22 sleep_amount=15
23 jail -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
26 jail -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 sleep 0.5
29
30 for 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
40 done
41 sleep 0.5
42
43 pid1="$(pgrep -f -x -j "$jid" "$sleep $sleep_amount" | sort)"
44 pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_1_1.pid)" \
45     "$(cat ${PWD}/${base}_1_2.pid)" | sort)
46 if [ "$pid1" = "$pid2" ]; then
47         echo "ok 1 - $name"
48 else
49         echo "not ok 1 - $name # pgrep output: '$pid1', pidfile output: '$pid2'"
50 fi
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)"
53 wait
54
55 name="pgrep -j any"
56 sleep_amount=16
57 jail -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
60 jail -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
63 sleep 2
64 pid1="$(pgrep -f -x -j any "$sleep $sleep_amount" | sort)"
65 pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_2_1.pid)" \
66     "$(cat ${PWD}/${base}_2_2.pid)" | sort)
67 if [ "$pid1" = "$pid2" ]; then
68         echo "ok 2 - $name"
69 else
70         echo "not ok 2 - $name # pgrep output: '$pid1', pidfile output: '$pid2'"
71 fi
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)"
74 wait
75
76 name="pgrep -j none"
77 sleep_amount=17
78 daemon -p ${PWD}/${base}_3_1.pid $sleep $sleep_amount &
79 jail -c path=/ name=${base}_3_2 ip4.addr=127.0.0.1 \
80     command=daemon -p ${PWD}/${base}_3_2.pid $sleep $sleep_amount &
81 sleep 2
82 pid="$(pgrep -f -x -j none "$sleep $sleep_amount")"
83 if [ "$pid" = "$(cat ${PWD}/${base}_3_1.pid)" ]; then
84         echo "ok 3 - $name"
85 else
86         echo "not ok 3 - $name # pgrep output: '$pid1', pidfile output: '$pid2'"
87 fi
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)"
90 wait
91
92 # test 4 is like test 1 except with jname instead of jid.
93 name="pgrep -j <jname>"
94 sleep_amount=18
95 jail -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
98 jail -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
101 sleep 0.5
102
103 jname="${base}_4_1,${base}_4_2"
104 pid1="$(pgrep -f -x -j "$jname" "$sleep $sleep_amount" | sort)"
105 pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_4_1.pid)" \
106     "$(cat ${PWD}/${base}_4_2.pid)" | sort)
107 if [ "$pid1" = "$pid2" ]; then
108         echo "ok 4 - $name"
109 else
110         echo "not ok 4 - $name # pgrep output: '$(echo $pid1)', pidfile output: '$(echo $pid2)'"
111 fi
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)"
114 wait
115
116 rm -f $sleep