bootstrap/rc.d/rc.tftpd

105 lines
2.6 KiB
Bash
Executable file

#!/bin/bash
# Version: 0.2.9
# Copyright (c) 2005-2017:
# Darren 'Tadgy' Austin <darren (at) afterdark.org.uk>
# Licensed under the terms of the GNU General Public License version 3.
EXEC="/usr/sbin/in.tftpd"
ARGS=(--listen --address=FIXME --user tftp --secure /data/tftpboot)
PIDFILE=""
checkconfigured() {
# This function can be used to perform any pre-start tests; hopfully to insure the daemon
# can start correctly, before actually trying to start it. A return value of 0 means the
# tests were passed and the daemon should be started. Any other value prevents the
# daemon from being started and an error message will be emitted.
return 0
}
checkstatus() {
local RUNPIDS="$(pgrep -f "$EXEC")"
if [ ! -z "$RUNPIDS" ]; then
echo -n "${BASH_SOURCE##*/}: ${EXEC##*/}: running"
if [ ! -z "$PIDFILE" ]; then
if [ ! -e "$PIDFILE" ]; then
echo -n ", but .pid file does not exist"
elif ! echo "$RUNPIDS" | grep "\<$(cat "$PIDFILE")\>" >/dev/null 2>&1; then
echo -n ", but .pid file is stale"
fi
fi
echo
else
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: stopped"
return 1
fi
return 0
}
startdaemon() {
if ! checkconfigured; then
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: not started - pre-start checks failed" >&2
return 1
elif [ ! -e "$EXEC" ]; then
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: not found" >&2
return 1
elif [ ! -x "$EXEC" ]; then
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: not executable" >&2
return 1
fi
"$EXEC" "${ARGS[@]}"
return $?
}
stopdaemon() {
if ! kill -TERM "$(cat "$PIDFILE" 2>/dev/null)" >/dev/null 2>&1; then
kill -TERM "$(pgrep -f "$EXEC")" >/dev/null 2>&1
fi
sleep 2
if checkstatus >/dev/null; then
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: failed to stop gracefully - slaying" >&2
kill -KILL "$(pgrep -f "$EXEC")" >/dev/null 2>&1
fi
return 0
}
case "$1" in
'start')
if checkstatus >/dev/null; then
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: already running" >&2
echo " Try: $BASH_SOURCE status" >&2
ERR=1
else
startdaemon
ERR=$?
fi
;;
'stop')
if ! checkstatus >/dev/null; then
echo "${BASH_SOURCE##*/}: ${EXEC##*/}: not running" >&2
echo " Try: $BASH_SOURCE status" >&2
ERR=1
else
stopdaemon
ERR=$?
fi
;;
'restart')
if checkstatus >/dev/null; then
stopdaemon && sleep 2 && startdaemon
ERR=$?
else
startdaemon
ERR=$?
fi
;;
'status')
checkstatus
ERR=$?
;;
*)
echo "Usage: $BASH_SOURCE <start|stop|restart|status>" >&2
ERR=1
;;
esac
return $ERR 2>/dev/null || exit $ERR