From a3ede9c5ded93d3195b1d1faeafca50340bd9d5c Mon Sep 17 00:00:00 2001 From: Andrew Ruthven Date: Thu, 22 Mar 2012 23:19:20 +1300 Subject: [PATCH] Write out to our own tempfile, have more guards to make sure the files are what we expect. This resolves an annoying race condition with update-motd which sometimes caused duplicated data in the MOTD. --- debian/init.d | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/debian/init.d b/debian/init.d index e077a69..f7a083e 100644 --- a/debian/init.d +++ b/debian/init.d @@ -34,6 +34,7 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/bin/mythtv-status # Introduce the server's location here NAME=mythtv-status # Introduce the short server's name here DESC="MythTV Status" # Introduce a short description here +TEMPFILE=/var/run/motd.mythtv-status test -x $DAEMON || exit 0 @@ -62,12 +63,27 @@ case "$1" in [ -f /var/run/motd ] || touch /var/run/motd [ -f /var/run/motd.orig ] || cp /var/run/motd /var/run/motd.orig - cp /var/run/motd.orig /var/run/motd.new + if [ -f $TEMPFILE ]; then + echo "Hey, $TEMPFILE already exists" 1>&2 + ls -l /var/run/motd* 1>&2 + ps -ef | grep mythtv-status 1>&2 + cat /var/run/motd.new 1>&2 + fi + + cp /var/run/motd.orig $TEMPFILE ret=0 - $DAEMON $ARGS -h $HOST >> /var/run/motd.new 2> /dev/null || ret=$? + start=$(date) + $DAEMON $ARGS -h $HOST >> $TEMPFILE || ret=$? if [ $ret -eq 0 -o $ret -eq 1 ]; then - mv /var/run/motd.new /var/run/motd + if [ ! -f $TEMPFILE ]; then + # My file has gone away. WTF?! Spit out some debugging. + echo "Odd, $TEMPFILE has gone away. Start: $date, End: $(date)" 1>&2 + ls -l /var/run/motd* 1>&2 + ps -ef | grep mythtv-status 1>&2 + else + mv $TEMPFILE /var/run/motd + fi log_end_msg 0 else log_failure_msg -- 2.30.2