xref: /freebsd/bin/pkill/tests/pkill-j_test.sh (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1#!/bin/sh
2# $FreeBSD$
3
4jail_name_to_jid()
5{
6	local check_name="$1"
7	(
8		line="$(jls -n 2> /dev/null | grep  name=$check_name  )"
9		for nv in $line; do
10			local name="${nv%=*}"
11			if [ "${name}" = "jid" ]; then
12				eval $nv
13				echo $jid
14				break
15			fi
16		done
17	)
18}
19
20base=pkill_j_test
21
22echo "1..3"
23
24name="pkill -j <jid>"
25if [ `id -u` -eq 0 ]; then
26	sleep=$(pwd)/sleep.txt
27	ln -sf /bin/sleep $sleep
28        jail -c path=/ name=${base}_1_1 ip4.addr=127.0.0.1 \
29            command=daemon -p ${PWD}/${base}_1_1.pid $sleep 5 &
30
31        jail -c path=/ name=${base}_1_2 ip4.addr=127.0.0.1 \
32            command=daemon -p ${PWD}/${base}_1_2.pid $sleep 5 &
33
34	$sleep 5 &
35	sleep 0.5
36	jid1=$(jail_name_to_jid ${base}_1_1)
37	jid2=$(jail_name_to_jid ${base}_1_2)
38	jid="${jid1},${jid2}"
39	if pkill -f -j "$jid" $sleep && sleep 0.5 &&
40	    ! -f ${PWD}/${base}_1_1.pid &&
41	    ! -f ${PWD}/${base}_1_2.pid ; then
42		echo "ok 1 - $name"
43	else
44		echo "not ok 1 - $name"
45	fi 2>/dev/null
46	rm -f $sleep
47	[ -f ${PWD}/${base}_1_1.pid ] && kill $(cat ${PWD}/${base}_1_1.pid)
48	[ -f ${PWD}/${base}_1_2.pid ] && kill $(cat ${PWD}/${base}_1_2.pid)
49	wait
50else
51	echo "ok 1 - $name # skip Test needs uid 0."
52fi
53
54name="pkill -j any"
55if [ `id -u` -eq 0 ]; then
56	sleep=$(pwd)/sleep.txt
57	ln -sf /bin/sleep $sleep
58        jail -c path=/ name=${base}_2_1 ip4.addr=127.0.0.1 \
59            command=daemon -p ${PWD}/${base}_2_1.pid $sleep 5 &
60
61        jail -c path=/ name=${base}_2_2 ip4.addr=127.0.0.1 \
62            command=daemon -p ${PWD}/${base}_2_2.pid $sleep 5 &
63
64	$sleep 5 &
65	sleep 0.5
66	chpid3=$!
67	if pkill -f -j any $sleep && sleep 0.5 &&
68	    [ ! -f ${PWD}/${base}_2_1.pid -a
69	      ! -f ${PWD}/${base}_2_2.pid ] && kill $chpid3; then
70		echo "ok 2 - $name"
71	else
72		echo "not ok 2 - $name"
73	fi 2>/dev/null
74	rm -f $sleep
75	[ -f ${PWD}/${base}_2_1.pid ] && kill $(cat ${PWD}/${base}_2_1.pid)
76	[ -f ${PWD}/${base}_2_2.pid ] && kill $(cat ${PWD}/${base}_2_2.pid)
77	wait
78else
79	echo "ok 2 - $name # skip Test needs uid 0."
80fi
81
82name="pkill -j none"
83if [ `id -u` -eq 0 ]; then
84	sleep=$(pwd)/sleep.txt
85	ln -sf /bin/sleep $sleep
86	daemon -p ${PWD}/${base}_3_1.pid $sleep 5
87	jail -c path=/ name=${base}_3_2 ip4.addr=127.0.0.1 \
88            command=daemon -p ${PWD}/${base}_3_2.pid $sleep 5 &
89	sleep 1
90	if pkill -f -j none "$sleep 5" && sleep 1 &&
91	    [ ! -f ${PWD}/${base}_3_1.pid -a -f ${PWD}/${base}_3_2.pid ] ; then
92		echo "ok 3 - $name"
93	else
94		ls ${PWD}/*.pid
95		echo "not ok 3 - $name"
96	fi 2>/dev/null
97	rm -f $sleep
98	[ -f ${PWD}/${base}_3_1.pid ] && kill $(cat ${base}_3_1.pid)
99	[ -f ${PWD}/${base}_3_2.pid ] && kill $(cat ${base}_3_2.pid)
100else
101	echo "ok 3 - $name # skip Test needs uid 0."
102fi
103