Blob Blame History Raw
#!/bin/bash
#
# ntpd		This shell script takes care of starting and stopping
#		ntpd (NTPv4 daemon).
#
# chkconfig: - 58 74
# description: ntpd is the NTPv4 daemon. \
# The Network Time Protocol (NTP) is used to synchronize the time of \
# a computer client or server to another server or reference time source, \
# such as a radio or satellite receiver or modem.

### BEGIN INIT INFO
# Provides: ntpd
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop ntpd
# Description: ntpd is the NTPv4 daemon. The Network Time Protocol (NTP)
#              is used to synchronize the time of a computer client or
#              server to another server or reference time source, such
#              as a radio or satellite receiver or modem.
### END INIT INFO

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

if [ -f /etc/sysconfig/ntpd ];then
        . /etc/sysconfig/ntpd
fi

prog=ntpd
lockfile=/var/lock/subsys/$prog
ntpconf=/etc/ntp.conf
ntpstep=/etc/ntp/step-tickers

sync_hwclock() {
	ARC=0
	SRM=0
	UTC=0

	if [ -f /etc/sysconfig/clock ]; then
	   . /etc/sysconfig/clock

	   # convert old style clock config to new values
	   if [ "${CLOCKMODE}" = "GMT" ]; then
	      UTC=true
	   elif [ "${CLOCKMODE}" = "ARC" ]; then
	      ARC=true
	   fi
	fi

	CLOCKFLAGS="$CLOCKFLAGS --systohc"

	case "$UTC" in
	    yes|true)	CLOCKFLAGS="$CLOCKFLAGS --utc";;
	    no|false)	CLOCKFLAGS="$CLOCKFLAGS --localtime";;
	esac
	case "$ARC" in
	    yes|true)	CLOCKFLAGS="$CLOCKFLAGS --arc";;
	esac
	case "$SRM" in
	    yes|true)	CLOCKFLAGS="$CLOCKFLAGS --srm";;
	esac

	action $"Syncing hardware clock to system time" /sbin/hwclock $CLOCKFLAGS
}

readconf() {
	dostep=''
	dropstr=''
	hasg=''
	OPTIND=1
	while getopts ":46aAbc:dD:f:gi:I:k:l:LmnN:p:P:qr:s:t:u:U:v:V:x" args $OPTIONS;
	do 
	  case "$args" in
	    x) dostep=yes;;
	    c) ntpconf="$OPTARG";;
	    u) dropstr="-U $(echo $OPTARG | sed 's/:.*//')";;
	    g) hasg=yes;;
	  esac
	done

	tickers=''
	if [ -s "$ntpstep" ]; then
	    tickers=$(sed 's/#.*//' $ntpstep)
	    echo "$tickers" | grep -qi '[a-z0-9]' && dostep=yes || tickers=''
	fi
	if [ -n "$dostep" -a -z "$tickers" ]; then
	    # -x option is used, but step-tickers doesn't exist or contain
	    # anything useful, use servers from ntp.conf instead
	    tickers=$(awk '$1=="peer"||$1=="server"{print $2}' $ntpconf | \
	        egrep -v '127\.127\.[0-9]+\.[0-9]+')
	fi
}

start() {
	# Check that networking is up.
	[ "$NETWORKING" = "no" ] && exit 1

	[ -x /usr/sbin/ntpd ] || exit 5

	readconf;

	if [ -n "$dostep" ]; then
	    echo -n $"$prog: Synchronizing with time server: "
	    /usr/sbin/ntpdate $dropstr -s -b $NTPDATE_OPTIONS $tickers &>/dev/null
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && success || failure
	    echo
	    if [ $RETVAL -eq 0 ]; then
	        [ "$SYNC_HWCLOCK" = "yes" ] && sync_hwclock
	    elif [ -z "$hasg" ]; then
	        OPTIONS="$OPTIONS -g"
	    fi
	elif [ -z "$hasg" ]; then
	    # -g can replace the grep for time servers
	    # as it permits ntpd to violate its 1000s limit once.
	    OPTIONS="$OPTIONS -g"
	fi
        # Start daemons.
        echo -n $"Starting $prog: "
        daemon $prog $OPTIONS
	RETVAL=$?
        echo
	[ $RETVAL -eq 0 ] && touch $lockfile
	return $RETVAL
}

stop() {
        echo -n $"Shutting down $prog: "
	killproc $prog
	RETVAL=$?
        echo
	[ $RETVAL -eq 0 ] && rm -f $lockfile
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status $prog
	;;
  restart|force-reload)
	stop
	start
	;;
  try-restart|condrestart)
	if status $prog > /dev/null; then
	    stop
	    start
	fi
	;;
  reload)
	exit 3
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
	exit 2
esac