Skip to content

Commit

Permalink
better crash resilience
Browse files Browse the repository at this point in the history
  • Loading branch information
Topheee committed Jan 29, 2023
1 parent 491a70f commit e999007
Showing 1 changed file with 53 additions and 19 deletions.
72 changes: 53 additions & 19 deletions entry.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
#!/bin/sh
#!/bin/bash

ilog() {
echo "[INF] " $@ 1>&2
}
wlog() {
echo "[WRN] " $@ 1>&2
}
elog() {
echo "[ERR] " $@ 1>&2
}

# signal handler for graceful shutdown of the container
nice_exit() {
Expand All @@ -14,10 +24,10 @@ trap nice_exit INT TERM
# we require an environment variable MAIL_DOMAIN to be present
if [ -z "$MAIL_DOMAIN" ]; then
if [ ! -z "$PF_MYDOMAIN" ]; then
echo "INFO: MAIL_DOMAIN is not set, using PF_MYDOMAIN ('$PF_MYDOMAIN')"
ilog "MAIL_DOMAIN is not set, using PF_MYDOMAIN ('$PF_MYDOMAIN')"
export MAIL_DOMAIN="$PF_MYDOMAIN"
else
echo "WARN: please set the MAIL_DOMAIN environment variable to your FQDN. using 'localhost'"
wlog "please set the MAIL_DOMAIN environment variable to your FQDN. using 'localhost'"
export MAIL_DOMAIN=localhost
fi
fi
Expand All @@ -30,15 +40,15 @@ echo "$MAIL_DOMAIN" > /etc/postfix/vhosts
/usr/local/bin/add_mailbox.sh postmaster
echo "changeme" | /usr/local/bin/add_user.sh postmaster

echo "INFO: configuring courier imap"
ilog "configuring courier imap"
for VAR in $(env | grep '^CO_' | tr '[:upper:]' '[:lower:]'); do
CONFVAR=$(echo "$VAR" | cut -d= -f1 | cut -c 4-)
CONFVAL=$(echo "$VAR" | cut -d= -f2)
echo "configuring $CONFVAR = $CONFVAL"
sed -i "s/^$CONFVAR=.*$/$CONFVAR=$CONFVAL/" /etc/courier/imapd-ssl
done

echo "INFO: configuring postfix smtp"
ilog "configuring postfix smtp"
for VAR in $(env | grep '^PF_' | tr '[:upper:]' '[:lower:]'); do
CONFVAR=$(echo "$VAR" | cut -d= -f1 | cut -c 4- | tr '[:upper:]' '[:lower:]')
CONFVAL=$(echo "$VAR" | cut -d= -f2)
Expand All @@ -56,7 +66,7 @@ KEYFILE="$(postconf smtp_tls_key_file | cut -d ' ' -f3)"
DHFILE='/etc/postfix/dh512.pem'
IMAPCERTFILE='/etc/courier/imapd.pem' # default from /etc/courier/imapd-ssl
if [ ! -z "$CERTFILE" ]; then
echo "INFO: building courier certificate file based on $CERTFILE, $KEYFILE and $DHFILE"
ilog "building courier certificate file based on $CERTFILE, $KEYFILE and $DHFILE"
cat "$CERTFILE" "$KEYFILE" "$DHFILE" > $IMAPCERTFILE
fi

Expand All @@ -71,20 +81,44 @@ sleep 1
/etc/init.d/courier-imap start
/etc/init.d/courier-imap-ssl start

SASL_PID=$(cat /var/spool/postfix/var/run/saslauthd/saslauthd.pid)
#while [ ! -e "/var/spool/postfix/pid/master.pid" ]; do sleep 1; done
#POSTFIX_PID=$(cat /var/spool/postfix/pid/master.pid | tr -d ' ')
wait_and_get_socket() {
sock_file="$1"
timeout="$2"

#tail -f /var/log/syslog
#while [ ! -e "/var/log/mail.log" ]; do sleep 1; done
#tail -f /var/log/auth.log /var/log/mail.log /var/log/mail.err &
if [ -z "$timeout" ]; then timeout=5; fi

echo "INFO: running and waiting for pid $SASL_PID to exit..."
# we cannot simply `wait` here, as it is not our child process...
#wait "$SASL_PID"
tail --pid=$SASL_PID -f /var/log/syslog &
#tail --pid=$SASL_PID -f /dev/null &
wait $!
i=0
while [ ! -e "${sock_file}" -a "$i" -lt "$timeout" ]; do
i="$(expr "$i" '+' 1)"
sleep 1
done

echo "INFO: pid $SASL_PID exited. Bye-bye!"
if [ "$i" -eq "$timeout" ]; then
elog "waiting for socket file ${sock_file} timed out."
exit 1
fi

cat "${sock_file}"
}

SASL_PID=$(wait_and_get_socket /var/spool/postfix/var/run/saslauthd/saslauthd.pid)
IMAPD_PID=$(wait_and_get_socket /var/run/courier/imapd.pid)
IMAPD_SSL_PID=$(wait_and_get_socket /var/run/courier/imapd-ssl.pid)
POSTFIX_PID=$(wait_and_get_socket /var/spool/postfix/pid/master.pid | tr -d ' ')

ilog "running and waiting for pids saslauthd: $SASL_PID, imapd: $IMAPD_PID, imapd_ssl: $IMAPD_SSL_PID, postfix: $POSTFIX_PID to exit..."

# we cannot simply `wait` here, as it is not our child process...
tail --pid=$SASL_PID -f /var/log/auth.log &
SASL_TAIL_PID="$!"
# no idea what /var/log/btmp is, but there is no explicit logfile for imapd and imapd_ssl
tail --pid=$IMAPD_PID -f /var/log/btmp &
IMAPD_TAIL_PID="$!"
tail --pid=$IMAPD_SSL_PID -f /var/log/syslog &
IMAPD_SSL_TAIL_PID="$!"
tail --pid=$POSTFIX_PID -f /var/log/mail.log &
POSTFIX_TAIL_PID="$!"

wait -n -p EXITED_PID "$SASL_TAIL_PID" "$IMAPD_TAIL_PID" "$IMAPD_SSL_TAIL_PID" "$POSTFIX_TAIL_PID"

ilog "pid $EXITED_PID exited. Bye-bye!"

0 comments on commit e999007

Please sign in to comment.