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 map locking.
11# Note: this is mostly for systems which use fcntl().
12# just invoke it from the obj.*/libsmutil/ directory;
13# otherwise use the -l and -m options to specify the paths.
14# ----------------------------------------
15
16fail()
17{
18  echo "$0: $@"
19  exit 1
20}
21
22err()
23{
24  echo "$0: $@"
25  rc=1
26}
27
28O=`basename $0`.0
29V=vt
30M=../makemap/makemap
31CHKL=./t-lockfile
32
33usage()
34{
35  cat <<EOF
36$0: test basic makemap locking;
37requires `basename ${CHKL}` and `basename ${M}`.
38usage:
39$0 [options]
40options:
41-l locktest   path to `basename ${CHKL}` [default: ${CHKL}]
42-m makemap    path to `basename ${M}` [default: $M]
43EOF
44}
45
46tries=0
47rc=0
48while getopts l:m:t: FLAG
49do
50  case "${FLAG}" in
51    l) CHKL="${OPTARG}";;
52    m) M="${OPTARG}";;
53    t) tries="${OPTARG}";;
54    *) usage
55	exit 69
56	;;
57  esac
58done
59shift `expr ${OPTIND} - 1`
60
61[ -x $M ] || fail "missing $M"
62[ -x ${CHKL} ] || fail "missing ${CHKL}"
63
64MAPTX=`$M -x | egrep 'hash|cdb'`
65
66mm()
67{
68  (echo "l1 l2"; sleep 5; echo "e1 e2") |
69  $M -v $MT $F >> $O 2>&1
70}
71
72chkl()
73{
74  ${CHKL} -Rrc -f $F >> $O 2>&1
75}
76
77for XT in ${MAPTX}
78do
79
80MT=`echo $XT | cut -d: -f1`
81EXT=`echo $XT | cut -d: -f2`
82
83F=$V.${EXT}
84
85rm -f $O
86mm &
87wpid=$!
88sleep 1
89chkl&
90rpid=$!
91
92while [ $tries -gt 0 ]
93do
94  sleep 1; chkl
95  tries=`expr $tries - 1 `
96done
97
98wait $wpid
99wait $rpid
100
101if grep "status=unknown" $O >/dev/null
102then
103  :
104else
105  # get the makemap pid, not the "mm" pid, for checks?
106  grep "status=locked pid=" $O || err "$MT map not locked"
107fi
108
109done
110
111exit $rc
112