xref: /freebsd/contrib/sendmail/libsmutil/t-lockfile-0.sh (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1#!/bin/sh
2# Copyright (c) 2021 Proofpoint, Inc. and its suppliers.
3#	All rights reserved.
4#
5# By using this file, you agree to the terms and conditions set
6# forth in the LICENSE file which can be found at the top level of
7# the sendmail distribution.
8#
9# ----------------------------------------
10# test t-lockfile, analyze result
11# ----------------------------------------
12
13fail()
14{
15  echo "$0: $@"
16  exit 1
17}
18
19PRG=./t-lockfile
20O=l.log
21
22analyze()
23{
24 # the "owner" unlock operation must be before
25 # the "client" lock operation can succeed
26 U=`grep -n 'owner=1, unlock.*done' $O | cut -d: -f1 | head -n1`
27 [ x"$U" = "x" ] && U=`grep -n '_close' $O | cut -d: -f1 | head -n1`
28 L=`grep -n 'owner=0, lock.* ok' $O | cut -d: -f1`
29 [ x"$U" = "x" ] && return 1
30 [ x"$L" = "x" ] && return 1
31 [ $U -lt $L ]
32}
33
34all=true
35while getopts 2a: FLAG
36do
37  case "${FLAG}" in
38    2) all=false;;
39    a) O=${OPTARG}
40       analyze || fail "$opts: unlock1=$U, lock2=$L"
41       exit;;
42  esac
43done
44shift `expr ${OPTIND} - 1`
45
46[ -x ${PRG} ] || fail "missing ${PRG}"
47
48if $all
49then
50for opts in "" "-r" "-n" "-nr"
51do
52  ${PRG} $opts > $O 2>&1 || fail "$opts: $?"
53  analyze || fail "$opts: unlock1=$U, lock2=$L"
54done
55fi
56
57# try with two processes
58for opts in "" "-r"
59do
60rm -f $O
61${PRG} -W >> $O 2>&1 || fail "-W: $?"
62wpid=$!
63${PRG} -R $opts >> $O 2>&1 || fail "-R $opts: $?"
64rpid=$!
65analyze || fail "$opts: unlock1=$U, lock2=$L"
66wait $wpid
67wait $rpid
68done
69
70exit 0
71