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=pgrep_j_test 21 22echo "1..3" 23 24name="pgrep -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 jid1=$(jail_name_to_jid ${base}_1_1) 35 jid2=$(jail_name_to_jid ${base}_1_2) 36 jid="${jid1},${jid2}" 37 pid1="$(pgrep -f -x -j $jid "$sleep 5" | sort)" 38 pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_1_1.pid)" \ 39 $(cat ${PWD}/${base}_1_2.pid) | sort) 40 if [ "$pid1" = "$pid2" ]; then 41 echo "ok 1 - $name" 42 else 43 echo "not ok 1 - $name" 44 fi 45 [ -f ${PWD}/${base}_1_1.pid ] && kill $(cat ${PWD}/${base}_1_1.pid) 46 [ -f ${PWD}/${base}_1_2.pid ] && kill $(cat ${PWD}/${base}_1_2.pid) 47 rm -f $sleep 48else 49 echo "ok 1 - $name # skip Test needs uid 0." 50fi 51 52name="pgrep -j any" 53if [ `id -u` -eq 0 ]; then 54 sleep=$(pwd)/sleep.txt 55 ln -sf /bin/sleep $sleep 56 jail -c path=/ name=${base}_2_1 ip4.addr=127.0.0.1 \ 57 command=daemon -p ${PWD}/${base}_2_1.pid $sleep 5 & 58 59 jail -c path=/ name=${base}_2_2 ip4.addr=127.0.0.1 \ 60 command=daemon -p ${PWD}/${base}_2_2.pid $sleep 5 & 61 62 sleep 2 63 pid1="$(pgrep -f -x -j any "$sleep 5" | sort)" 64 pid2=$(printf "%s\n%s" "$(cat ${PWD}/${base}_2_1.pid)" \ 65 $(cat ${PWD}/${base}_2_2.pid) | sort) 66 if [ "$pid1" = "$pid2" ]; then 67 echo "ok 2 - $name" 68 else 69 echo "not ok 2 - $name" 70 fi 71 [ -f ${PWD}/${base}_2_1.pid ] && kill $(cat ${PWD}/${base}_2_1.pid) 72 [ -f ${PWD}/${base}_2_2.pid ] && kill $(cat ${PWD}/${base}_2_2.pid) 73 rm -f $sleep 74else 75 echo "ok 2 - $name # skip Test needs uid 0." 76fi 77 78name="pgrep -j none" 79if [ `id -u` -eq 0 ]; then 80 sleep=$(pwd)/sleep.txt 81 ln -sf /bin/sleep $sleep 82 daemon -p ${PWD}/${base}_3_1.pid $sleep 5 & 83 jail -c path=/ name=${base}_3_2 ip4.addr=127.0.0.1 \ 84 command=daemon -p ${PWD}/${base}_3_2.pid $sleep 5 & 85 sleep 2 86 pid="$(pgrep -f -x -j none "$sleep 5")" 87 if [ "$pid" = "$(cat ${PWD}/${base}_3_1.pid)" ]; then 88 echo "ok 3 - $name" 89 else 90 echo "not ok 3 - $name" 91 fi 92 rm -f $sleep 93 [ -f ${PWD}/${base}_3_1.pid ] && kill $(cat $PWD/${base}_3_1.pid) 94 [ -f ${PWD}/${base}_3_2.pid ] && kill $(cat $PWD/${base}_3_2.pid) 95else 96 echo "ok 3 - $name # skip Test needs uid 0." 97fi 98