aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Łyszczek <michal.lyszczek@bofc.pl>2019-04-14 16:13:40 +0200
committerMichał Łyszczek <michal.lyszczek@bofc.pl>2019-04-14 16:45:43 +0200
commit9d59447138c79e88e65e7548194b3731e4ab33d5 (patch)
treef6d04545dbaee239899826adcb223516c44c29a8
parent1bc4bd01a7c0c5a7ec8045e0be1e717313e6a7be (diff)
downloadntpd-setwait-9d59447138c79e88e65e7548194b3731e4ab33d5.tar.gz
ntpd-setwait-9d59447138c79e88e65e7548194b3731e4ab33d5.tar.bz2
ntpd-setwait-9d59447138c79e88e65e7548194b3731e4ab33d5.zip
init.d: add init and config scripts
* Makefile.am add rules to install init scripts * init.d/ntpd-setwait sysv init script * init.d/ntpd-setwait.openrc openrc init script * init.d/ntpd-setwait.conf config file for init scripts Signed-off-by: Michał Łyszczek <michal.lyszczek@bofc.pl>
-rw-r--r--Makefile.am6
-rwxr-xr-xinit.d/ntpd-setwait165
-rw-r--r--init.d/ntpd-setwait.conf31
-rwxr-xr-xinit.d/ntpd-setwait.openrc29
4 files changed, 230 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index ce2b0ad..3984764 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,10 @@
ACLOCAL_AMFLAGS=-I m4
-EXTRA_DIST = readme.md
+EXTRA_DIST = readme.md init.d/ntpd-setwait.conf init.d/ntpd-setwait.openrc
+
+sysconf_DATA = init.d/ntpd-setwait.conf
+init_ddir = $(sysconfdir)/init.d
+dist_init_d_SCRIPTS = init.d/ntpd-setwait
bin_PROGRAMS = ntpd-setwait
ntpd_setwait_SOURCES = main.c daemonize.c daemonize.h
diff --git a/init.d/ntpd-setwait b/init.d/ntpd-setwait
new file mode 100755
index 0000000..323cf0b
--- /dev/null
+++ b/init.d/ntpd-setwait
@@ -0,0 +1,165 @@
+#!/bin/sh
+## ==========================================================================
+# Licensed under BSD 2clause license See LICENSE file for more information
+# Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
+## ==========================================================================
+
+###
+# some systems like solaris has alias on stop, remove those aliases so our
+# init script can work as expected
+#
+
+unalias stop > /dev/null 2>&1
+unalias start > /dev/null 2>&1
+
+## ==========================================================================
+# ____ _ __
+# _____ ____ __ __ _____ _____ ___ / __/(_)/ /___ _____
+# / ___// __ \ / / / // ___// ___// _ \ / /_ / // // _ \ / ___/
+# (__ )/ /_/ // /_/ // / / /__ / __/ / __// // // __/(__ )
+# /____/ \____/ \__,_//_/ \___/ \___/ /_/ /_//_/ \___//____/
+#
+## ==========================================================================
+
+
+. /usr/local/etc/kurload.conf
+
+
+## ==========================================================================
+# _ __
+# ____ _____ (_)_ __ ____ _ / /_ ___ _ __ ____ _ _____ _____
+# / __ \ / ___// /| | / // __ `// __// _ \ | | / // __ `// ___// ___/
+# / /_/ // / / / | |/ // /_/ // /_ / __/ | |/ // /_/ // / (__ )
+# / .___//_/ /_/ |___/ \__,_/ \__/ \___/ |___/ \__,_//_/ /____/
+# /_/
+## ==========================================================================
+
+
+MAX_DEVIATION=${MAX_DEVIATION:="300"}
+NTPD_BIN=${NTPD_BIN:="/usr/sbin/ntpd"}
+NTPD_OPTS=${NTPD_OPTS:=""}
+PID_FILE=${PID_FILE:="/var/run/ntpd.pid"}
+PROGRAM_LOG=${PROGRAM_LOG:="/var/log/ntpd-setwait.log"}
+
+command=/usr/local/bin/ntpd-setwait
+
+
+## ==========================================================================
+# __ __ _ ____
+# ____ __ __ / /_ / /(_)_____ / __/__ __ ____ _____
+# / __ \ / / / // __ \ / // // ___/ / /_ / / / // __ \ / ___/
+# / /_/ // /_/ // /_/ // // // /__ / __// /_/ // / / /(__ )
+# / .___/ \__,_//_.___//_//_/ \___/ /_/ \__,_//_/ /_//____/
+# /_/
+## ==========================================================================
+
+
+## ==========================================================================
+# starts server as daemon
+## ==========================================================================
+
+
+start() {
+ echo -n "Starting ntpd-setwait with ntpd: ${NTPD_BIN}... "
+
+ /sbin/start-stop-daemon --make-pidfile --pidfile "${PID_FILE}" \
+ --start --background --name ntpd-setwait --stderr ${PROGRAM_LOG} \
+ --exec ${command} -- -f ${MAX_DEVIATION} ${NTPD_BIN} ${NTPD_OPTS}
+
+ if [ "$?" -ne "0" ] ; then
+ echo "error"
+ exit 1
+ fi
+
+ echo "ok"
+}
+
+
+## ==========================================================================
+# stops the server but lets it finish any job it currently performs. If
+# stop gets called twice, server will abandon any job and will exit asap
+## ==========================================================================
+
+
+stop() {
+ echo -n "Stopping ntpd... "
+ if /bin/kill -15 $(cat "${PID_FILE}") > /dev/null 2>&1; then
+ echo "ok"
+ return 0
+ fi
+
+ echo "already stopped"
+}
+
+
+## ==========================================================================
+# __ __
+# _____ / /_ ____ _ _____ / /_
+# / ___// __// __ `// ___// __/
+# (__ )/ /_ / /_/ // / / /_
+# /____/ \__/ \__,_//_/ \__/
+#
+## ==========================================================================
+
+
+case "$1" in
+ "start")
+ start
+ ;;
+
+ "stop")
+ stop
+ ;;
+
+ "restart")
+ stop
+ start
+ ;;
+
+ "status")
+ if [ ! -f "${PID_FILE}" ] ; then
+ # file doesn't exist, server wasn't started yet, or it was closed
+ # and server removed pid file
+ echo "ntpd is not running"
+ exit 1
+ fi
+
+ if [ $(/usr/bin/stat -c %s "${PID_FILE}") -eq 0 ] ; then
+ # file exists but its size is 0, meaning server started and ended
+ # but couldn't remove pid, so it truncated it to 0 bytes size
+ echo "ntpd is not running"
+ exit 1
+ fi
+
+ if /bin/ps -p $(cat "${PID_FILE}") > /dev/null 2>&1 ; then
+ # process exists and is alive
+ echo "ntpd is running"
+ exit 0
+ fi
+
+ # information about pid exists in pid file, but process is not running
+ # that means server exited in non-clean way and didn't clean pid file
+ echo "ntpd crashed"
+ exit 2
+ ;;
+ *)
+ echo -e "usage: $0 {start|stop|status|restart}"
+ echo -e ""
+ echo -e "exit codes"
+ echo -e "\tstart"
+ echo -e "\t\t0\tstarted with success"
+ echo -e "\t\t1\terror starting ntpd"
+ echo -e ""
+ echo -e "\tstop"
+ echo -e "\t\t0\tntpd stopped with success"
+ echo -e "\t\t1\tcouldn't stop ntpd (already stopped or crashed)"
+ echo -e ""
+ echo -e "\tstatus"
+ echo -e "\t\t0\tntpd is running"
+ echo -e "\t\t1\tntpd is not running"
+ echo -e "\t\t2\tntpd crashed"
+ echo -e ""
+ echo -e "\trestart"
+ echo -e "\t\t0\tntpd restarted"
+ echo -e "\t\t1\terror restarting ntpd"
+esac
diff --git a/init.d/ntpd-setwait.conf b/init.d/ntpd-setwait.conf
new file mode 100644
index 0000000..3823814
--- /dev/null
+++ b/init.d/ntpd-setwait.conf
@@ -0,0 +1,31 @@
+###
+# when local and ntp time differs by MAX_DEVIATION ammount of seconds,
+# ntpd-setwait will set system time to ntp time before launching ntpd.
+#
+
+MAX_DEVIATION=300
+
+###
+# ntpd binary to use, should be full absolute path
+#
+
+NTPD_BIN="/usr/sbin/ntpd"
+
+###
+# options to pass to NTPD_BIN
+#
+
+NTPD_OPTS="-d"
+
+###
+# where should pid file be stored
+#
+
+PID_FILE="/var/run/ntpd.pid"
+
+###
+# location where logs from ntpd-setwait (before executing ntpd)
+# are to be stored
+#
+
+PROGRAM_LOG="/var/log/ntpd-setwait.log"
diff --git a/init.d/ntpd-setwait.openrc b/init.d/ntpd-setwait.openrc
new file mode 100755
index 0000000..9e813e1
--- /dev/null
+++ b/init.d/ntpd-setwait.openrc
@@ -0,0 +1,29 @@
+#!/sbin/openrc-run
+
+MAX_DEVIATION=${MAX_DEVIATION:="300"}
+NTPD_BIN=${NTPD_BIN:="/usr/sbin/ntpd"}
+NTPD_OPTS=${NTPD_OPTS:=""}
+PID_FILE=${PID_FILE:="/var/run/ntpd.pid"}
+PROGRAM_LOG=${PROGRAM_LOG:="/var/log/ntpd-setwait.log"}
+
+command=/usr/bin/ntpd-setwait
+
+depend() {
+ need net
+}
+
+start() {
+ ebegin "Starting ntpd-setwait with ntpd: ${NTPD_BIN}"
+
+ /sbin/start-stop-daemon --make-pidfile --pidfile "${PID_FILE}" \
+ --start --background --name ntpd-setwait --stderr ${PROGRAM_LOG} \
+ --exec ${command} -- -f ${MAX_DEVIATION} ${NTPD_BIN} ${NTPD_OPTS}
+
+ eend $?
+}
+
+stop() {
+ ebegin "Stopping ntpd"
+ /sbin/start-stop-daemon --stop --signal 15 --pidfile "${PID_FILE}"
+ eend $?
+}