Here are (currently untested) rewrites of init.d and logrotate.d using
start-stop-daemon.  I'm "posting early, posting often" in case I
forget to follow through or I get an attack of guilt and start doing
paid work tomorrow.
-- 
Trent W. Buck
#! /bin/sh
### BEGIN INIT INFO
# Provides:          thttpd
# Required-Start:    networking
# Required-Stop:     networking
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start the thttpd web server.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/thttpd
NAME=thttpd
DESC='web server'

PIDFILE=/var/run/$NAME.pid
CONFFILE=/etc/$NAME/$NAME.conf
DAEMON_OPTS="-C $CONFFILE -i $PIDFILE"

test -x $DAEMON || exit 0
test -f $CONFFILE || exit 0

set -e

case "$1" in
    start)
        echo -n "Starting $DESC: "
        start-stop-daemon -S -q -p $PIDFILE -x $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
    stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon -K -q -p $PIDFILE -x $DAEMON --signal 10
        echo "$NAME."
        ;;
    force-stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon -K -q -p $PIDFILE -x $DAEMON
        echo "$NAME."
        ;;
    force-reload)
        if start-stop-daemon -K -q -p $PIDFILE -x $DAEMON --test
        then $0 restart
        fi
        ;;
    restart)
        ## This will allow thttpd up to one minute (60 seconds) to
        ## wrap up any open connections before killing it forcefully.
        ## 
        ## This should work OK for text and pictures, but clients
        ## downloading large files like an .iso or .mov will probably
        ## get unceremoniously disconnected.
        echo -n "Restarting $DESC: "
        start-stop-daemon -K -q -p $PIDFILE -x $DAEMON --signal 10 -R 60
        sleep 1
        start-stop-daemon -S -q -p $PIDFILE -x $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
    postrotate)
        ## After logrotate(8) does its thing, a running non-syslogging
        ## thttpd needs to acquire a new filehandle for the new log
        ## file, which we tell it to do via SIGHUP.  If thttpd is
        ## chrooted, this won't work, and we need to restart thttpd
        ## entirely.  Putting this logic in the init.d script instead
        ## of the logrotate.d script avoids a lot of code duplication.
        if  start-stop-daemon -K -q -p $PIDFILE -x $DAEMON --test &&
            /bin/grep -q '^[[:space:]]*logfile' /etc/thttpd/thttpd.conf
        then
            if /bin/grep -q '^[[:space:]]*chroot' /etc/thttpd/thttpd.conf
            then $0 restart
            else start-stop-daemon -K -q -p $PIDFILE -x $DAEMON --signal 1
            fi
        fi
        ;;
    *)
        N=/etc/init.d/$NAME
        echo "Usage: $N 
{start|stop|force-stop|restart|force-reload|postrotate}" >&2
        exit 1
        ;;
esac

exit 0
/var/log/thttpd.log {
    rotate 14
    daily
    compress
    missingok
    delaycompress
    postrotate
        if test -x /usr/sbin/invoke-rc.d
        then invoke-rc.d thttpd postrotate
        else /etc/init.d/thttpd postrotate
        fi >/dev/null 2>&1
    endscript
}

Attachment: signature.asc
Description: Digital signature

Reply via email to