From 4fc82e0067db4b54ee05d56feaa4fb981d380e5f Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 13 Jan 2010 22:07:34 +0100 Subject: [PATCH] examples: add example ntpd service Signed-off-by: Denys Vlasenko --- examples/var_service/ntpd/log/run | 21 ++++++++++ examples/var_service/ntpd/ntp.script | 29 ++++++++++++++ examples/var_service/ntpd/p_log | 4 ++ examples/var_service/ntpd/run | 60 ++++++++++++++++++++++++++++ examples/var_service/ntpd/w_log | 4 ++ 5 files changed, 118 insertions(+) create mode 100755 examples/var_service/ntpd/log/run create mode 100755 examples/var_service/ntpd/ntp.script create mode 100755 examples/var_service/ntpd/p_log create mode 100755 examples/var_service/ntpd/run create mode 100755 examples/var_service/ntpd/w_log diff --git a/examples/var_service/ntpd/log/run b/examples/var_service/ntpd/log/run new file mode 100755 index 000000000..560d1b19f --- /dev/null +++ b/examples/var_service/ntpd/log/run @@ -0,0 +1,21 @@ +#!/bin/sh + +user=logger + +logdir="/var/log/service/`(cd ..;basename $PWD)`" +mkdir -p "$logdir" 2>/dev/null +chown -R "$user": "$logdir" +chmod -R go-rwxst,u+rwX "$logdir" +rm logdir +ln -s "$logdir" logdir + +# make this dir accessible to logger +chmod a+rX . + +exec >/dev/null +exec 2>&1 +exec \ +env - PATH="$PATH" \ +softlimit \ +setuidgid "$user" \ +svlogd -tt "$logdir" diff --git a/examples/var_service/ntpd/ntp.script b/examples/var_service/ntpd/ntp.script new file mode 100755 index 000000000..90299ae8e --- /dev/null +++ b/examples/var_service/ntpd/ntp.script @@ -0,0 +1,29 @@ +#!/bin/sh + +exec 2>/dev/null +echo "`tail -n 99 "$0.log"`" >"$0.log" + +exec >>"$0.log" +exec 2>&1 + +dt=`date '+%Y-%m-%d %H:%M:%S'` + +if test x"$stratum" != x"" \ +&& test x"$poll_interval" != x"" \ +&& test 4 -ge "$stratum" \ +&& test 128 -le "$poll_interval" \ +; then + echo "$dt: $1"\ + "freq_drift_ppm=$freq_drift_ppm"\ + "offset=$offset"\ + "stratum=$stratum"\ + "poll_interval=$poll_interval,"\ + "setting hardware clock" + exec hwclock --systohc +fi + +echo "$dt: $1"\ + "freq_drift_ppm=$freq_drift_ppm"\ + "offset=$offset"\ + "stratum=$stratum"\ + "poll_interval=$poll_interval"\ diff --git a/examples/var_service/ntpd/p_log b/examples/var_service/ntpd/p_log new file mode 100755 index 000000000..a2521be05 --- /dev/null +++ b/examples/var_service/ntpd/p_log @@ -0,0 +1,4 @@ +#!/bin/sh + +cd log/logdir || exit 1 +cat @* current | $PAGER diff --git a/examples/var_service/ntpd/run b/examples/var_service/ntpd/run new file mode 100755 index 000000000..581d231a3 --- /dev/null +++ b/examples/var_service/ntpd/run @@ -0,0 +1,60 @@ +#!/bin/bash +# (using bashism (arrays) in dhcp config) + +#exec >/dev/null +exec 2>&1 +exec /dev/null +chown -R "$user:" "$rundir" +chmod -R a=rX "$rundir" +rm -rf rundir 2>/dev/null +ln -s "$rundir" rundir + + +echo "* Checking network" +test -f /var/run/service/fw/up || exec sleep 7 +sleep 5 # to let it settle + +# Grab config from dhcp +cfg=-1 +for f in rundir/*.ntpconf; do + test -f "$f" || continue + . "$f" +done + +# Select peers +p_opt="" +cfg=0 +while test x"${ntpip[$cfg]}" != x""; do + p_opt="$p_opt -p ${ntpip[$cfg]}" + let cfg=cfg+1 +done +test x"$p_opt" == x"" && p_opt="$default_p_opt" + +if test x"$p_opt" == x""; then + echo "* No NTP peers configured, stopping" + sv o . + exec sleep 1 +fi + + +# Let others know that we are up +date '+%Y-%m-%d %H:%M:%S %Z' >rundir/up + +# Go go go +echo "* Starting ntpd[$$]" +exec \ +env - PATH="$PATH" \ +softlimit \ +setuidgid "$user" \ +ntpd -ddnNl -S ./ntp.script $p_opt diff --git a/examples/var_service/ntpd/w_log b/examples/var_service/ntpd/w_log new file mode 100755 index 000000000..34b19b373 --- /dev/null +++ b/examples/var_service/ntpd/w_log @@ -0,0 +1,4 @@ +#!/bin/sh + +cd log/logdir || exit 1 +watch -n2 'w=`ttysize w`; h=`ttysize h`; tail -$((h-3)) current 2>&1 | cut -b0-$((w-2))' -- 2.25.1