chiark / gitweb /
add kvm-img wrapper script
[bin.git] / bsmtp-pull
1 #! /bin/bash
2 # Depends: lockfile-progs, ssh
3 set -e
4
5 if [ -z "$1" ]; then
6     echo "Usage: $0 hostname" 2>&1
7     exit 1
8 fi
9
10 DIR="$HOME/tmp/.bsmtp"
11 mkdir -p "$DIR"
12 cd "$DIR"
13
14 HOST="$1"
15
16 # TODO: Note that this scheme may currently lose mail if the local disk
17 # fills up! This is obviously very bad. Fix this.
18
19 # By default, lockfile-create gives up after three minutes, so don't cron
20 # this any more frequently than that without supplying a --retry argument.
21 lockfile-create "$HOST"
22 # Race condition pointed out by pjb: this doesn't guarantee that the lock is
23 # held before the critical section starts.
24 lockfile-touch "$HOST" &
25 TOUCH="$!"
26 trap 'kill "$TOUCH"; lockfile-remove "$HOST"' EXIT ERR HUP INT QUIT TERM
27 disown %1
28
29 ssh -2 -i "$HOME/.ssh/id-bsmtp-$HOST" -C "$HOST" bsmtp-pull-server > "$HOST"
30 [ -s "$HOST" ] || exit 0
31 /usr/sbin/sendmail -bS -odq < "$HOST"
32 rm -f "$HOST"
33
34 exit 0