-
Notifications
You must be signed in to change notification settings - Fork 7
/
loop
executable file
·130 lines (99 loc) · 3.3 KB
/
loop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh -e
# This script ensures at most one running instance for a <msgid>
if [ $# -lt 2 -o \( queue != "$1" -a rqueue != "$1" \) ]; then
echo "Format: $0 queue|rqueue <msgid>[.del]"
exit 1
fi
# Helpers
validate=${CABLE_HOME}/validate
fetch=${CABLE_HOME}/fetch
crypto=${CABLE_HOME}/crypto
comm=${CABLE_HOME}/comm
# Parameters
qtype=$1
msgid="${2%.del}"
dirid="${2}"
lockmagick="$3"
locktmout=2
# Directories
msgdir=${CABLE_QUEUES}/${qtype}/"${dirid}"
trap '[ $? = 0 ] || error failed' 0
error() {
logger -t loop -p mail.err "$@ (${msgid})"
trap - 0
exit 1
}
# Sanity checks
[ ${#msgid} = 40 ] || error "bad msgid"
[ -r "${msgdir}" ] || error "cannot access"
# .del actions: blocking lock (to let the renaming action finish)
if [ "${dirid}" != "${msgid}" ]; then
if [ ${qtype} = queue ]; then
if [ -e "${msgdir}" ]; then
exec flock -w ${locktmout} "${msgdir}"/ "${comm}" ack "${msgid}"
else
error ".del directory not found"
fi
else
if [ -e "${msgdir}" ]; then
exec flock -w ${locktmout} "${msgdir}"/ "${comm}" fin "${msgid}"
else
error ".del directory not found"
fi
fi
else
if [ ${qtype} = queue ]; then
# lock combined operations
if [ lockmagick != "${lockmagick}" ]; then
exec flock -w ${locktmout} -n "${msgdir}"/ "$0" ${qtype} "${msgid}" lockmagick
fi
"${validate}" ${qtype} "${msgid}"
set +e
# handle ack first, to retry send if failed
if [ -e "${msgdir}"/ack.ok ]; then
"${comm}" ack "${msgid}"
elif [ -e "${msgdir}"/ack.req ]; then
"${crypto}" ack "${msgid}" && \
"${comm}" ack "${msgid}"
fi
# if ack succeeds, ${msgdir} is renamed
if [ -e "${msgdir}" ]; then
if [ -e "${msgdir}"/send.req ]; then
"${fetch}" send "${msgid}" && \
"${crypto}" send "${msgid}" && \
exec "${comm}" send "${msgid}"
elif [ -e "${msgdir}"/send.rdy ]; then
"${crypto}" send "${msgid}" && \
exec "${comm}" send "${msgid}"
elif [ -e "${msgdir}"/send.ok ]; then
if [ ! -e "${msgdir}"/ack.ok ]; then
exec "${comm}" send "${msgid}"
fi
else
error "send.req/rdy/ok not found"
fi
fi
else
# lock combined operations
if [ lockmagick != "${lockmagick}" ]; then
exec flock -w ${locktmout} -n "${msgdir}"/ "$0" ${qtype} "${msgid}" lockmagick
fi
"${validate}" ${qtype} "${msgid}"
set +e
if [ -e "${msgdir}"/recv.rdy ]; then
"${crypto}" recv "${msgid}" && \
"${comm}" recv "${msgid}"
elif [ -e "${msgdir}"/recv.ok ]; then
"${comm}" recv "${msgid}"
elif [ -e "${msgdir}"/recv.req ]; then
"${fetch}" recv "${msgid}" && \
"${crypto}" recv "${msgid}" && \
"${comm}" recv "${msgid}"
fi
if [ -e "${msgdir}"/peer.req ]; then
exec "${crypto}" peer "${msgid}"
elif [ ! -e "${msgdir}"/peer.ok ]; then
error "peer.req/ok not found"
fi
fi
fi