home *** CD-ROM | disk | FTP | other *** search
/ chilidog.highland.cc.ks.us / chilidog.highland.cc.ks.us.zip / chilidog.highland.cc.ks.us / backup / bradford.20120521.etc.tar.gz / bradford.20120521.etc.tar / etc / init.d / ntp < prev    next >
Text File  |  2006-06-29  |  6KB  |  252 lines

  1. #! /bin/sh
  2. # Copyright (c) 1995-2003 SuSE Linux AG, Nuernberg, Germany.
  3. # All rights reserved.
  4. #
  5. # Author: Michael Andres
  6. #
  7. # /etc/init.d/ntp
  8. #   and its symbolic link
  9. # /usr/sbin/rcntp
  10. #
  11. ### BEGIN INIT INFO
  12. # Provides:       ntp ntpd xntpd 
  13. # Required-Start: $remote_fs $syslog $named
  14. # Required-Stop:  $remote_fs $syslog
  15. # Default-Start:  2 3 5
  16. # Default-Stop:   0 1 6
  17. # Description:    Start network time protocol daemon (NTPD).
  18. ### END INIT INFO
  19.  
  20. # First reset status of this service
  21. . /etc/rc.status
  22. rc_reset
  23.  
  24. # Return values acc. to LSB for all commands but status:
  25. # 0 - success
  26. # 1 - generic or unspecified error
  27. # 2 - invalid or excess argument(s)
  28. # 3 - unimplemented feature (e.g. "reload")
  29. # 4 - insufficient privilege
  30. # 5 - program is not installed
  31. # 6 - program is not configured
  32. # 7 - program is not running
  33.  
  34. # set default options
  35. NTP_CONF="/etc/ntp.conf"
  36. if [ ! -f ${NTP_CONF} ]; then
  37.     echo -n "Time server configuration file, ${NTP_CONF} does not exist."
  38.     # Tell the user this has skipped
  39.     rc_status -s
  40.     exit 6
  41. fi
  42.  
  43. NTPD_INITIAL_NTPDATE="AUTO-2"
  44. NTPD_OPTIONS="-u ntp"
  45. NTPD_RUN_CHROOTED="yes"
  46.  
  47. NTPD_BIN="/usr/sbin/ntpd"
  48. if [ ! -x ${NTPD_BIN} ]; then
  49.         echo -n "Time server, ${NTPD_BIN} not installed!"
  50.         rc_status -s
  51.         exit 5
  52. fi
  53.  
  54. NTPDATE_BIN="/usr/sbin/ntpdate"
  55.  
  56. # Override defaults, if we have the sysconfig file
  57. test -f /etc/sysconfig/ntp && . /etc/sysconfig/ntp
  58.  
  59. test "${NTPD_RUN_CHROOTED}" = "yes" && \
  60.     CHROOT_PREFIX="/var/lib/ntp" || \
  61.     CHROOT_PREFIX=""
  62.  
  63. NTPD_PID="${CHROOT_PREFIX}/var/run/ntp/ntpd.pid"
  64.  
  65. function ntpd_is_running() {    
  66.     $0 status >/dev/null
  67. }
  68.  
  69. function initial_ntpdate () {
  70.     case "$NTPD_INITIAL_NTPDATE" in
  71.     auto|Auto|AUTO)
  72.         MAX_AUTO=-1;
  73.         ;;
  74.     auto-*|Auto-*|AUTO-*)
  75.         MAX_AUTO=${NTPD_INITIAL_NTPDATE#*-};
  76.         ;;
  77.     *)
  78.         echo $NTPD_INITIAL_NTPDATE
  79.         return 0
  80.         ;;
  81.     esac
  82.     # AUTO: Try to get server from config file
  83.     if [ -r $NTP_CONF ] ; then
  84.         cat $NTP_CONF | awk -v MAX_AUTO=$MAX_AUTO '
  85.         /^[[:space:]]*server[[:space:]]+127.127/ {
  86.             next
  87.         }
  88.         /^[[:space:]]*(server|peer)[[:space:]]/ {
  89.             if ( MAX_AUTO ) {
  90.             printf " %s", $2
  91.             if ( --MAX_AUTO == 0 )
  92.                 exit 0
  93.         }
  94.         }
  95.         '
  96.     fi
  97.  
  98. }
  99.  
  100. function set_cmos_clock () {
  101.     if [ "$NTPD_ADJUST_CMOS_CLOCK" == yes ]; then
  102.         echo -n "Try to set the CMOS clock"
  103.         hwclock --systohc && echo $rc_done || echo $rc_failed
  104.     fi
  105. }
  106.  
  107. function parse_symlink
  108. {
  109.     if [ -c "$NTP_PARSE_DEVICE" ]; then
  110.         if [ -n "$NTP_PARSE_LINK" ]; then
  111.         ln -sf $NTP_PARSE_DEVICE $NTP_PARSE_LINK
  112.         fi
  113.     fi
  114. }
  115.  
  116. function prepare_chroot
  117. {
  118.     for configfile in /etc/{localtime,ntp.conf} $NTPD_CHROOT_FILES; do
  119.         test -d ${CHROOT_PREFIX}/${configfile%/*} || mkdir -p ${CHROOT_PREFIX}/${configfile%/*}
  120.         cp -auL ${configfile} ${CHROOT_PREFIX}/${configfile%/*}
  121.     done
  122.     NTPD_OPTIONS="${NTPD_OPTIONS} -i ${CHROOT_PREFIX}"
  123. }
  124.  
  125. case "$1" in
  126.     start)
  127.     # get the initial date from the timeservers configured in ntp.conf
  128.     NTPDATE_FROM=$(initial_ntpdate)
  129.     if [ -n "$NTPDATE_FROM" -a -x $NTPDATE_BIN ]; then
  130.         ntpd_is_running || {
  131.             echo -n "Try to get initial date and time via NTP from $NTPDATE_FROM"
  132.             # -b: Set time on runlevel change, otherwise let
  133.             #     ntpdate decide whether to slew or step.
  134.             test -z $INIT_VERSION \
  135.                 && FORCE_STEP="" \
  136.                 || FORCE_STEP="-b"
  137.             # -u: Use an unprivileged port for outgoing packets,
  138.             #     may be we have to synchronise with hosts beyond
  139.             #     a firewall.
  140.             $NTPDATE_BIN -s $FORCE_STEP $NTPDATE_FROM \
  141.                 && echo $rc_done \
  142.                 || echo $rc_failed
  143.             # error here is reported but not propagated.
  144.         }
  145.     fi
  146.     # do we want to set the CMOS clock?
  147.         set_cmos_clock
  148.     echo -n "Starting network time protocol daemon (NTPD)"
  149.         # do we need a refclock symlink?
  150.     parse_symlink
  151.     # do we run chrooted?
  152.     test "${NTPD_RUN_CHROOTED}" = "yes" && prepare_chroot
  153.     # do we get the option iburst? 
  154.     if [ -z $2 ]; then
  155.             true
  156.     else
  157.     case $2 in
  158.             iburst)
  159.         awk '{gsub ("^server.*","& iburst"); print $0}' /etc/ntp.conf > /var/lib/ntp/etc/ntp.conf.iburst
  160.         export NTPD_OPTIONS="$NTPD_OPTIONS -c /var/lib/ntp/etc/ntp.conf.iburst"
  161.             ;;
  162.             *)           echo "Unknown option $2"
  163.             echo "options: {iburst}"
  164.         exit 1 ;;
  165.     esac
  166.     fi
  167.     startproc $NTPD_BIN -p ${NTPD_PID} $NTPD_OPTIONS
  168.     rc_status -v
  169.     ;;
  170.     stop)
  171.     echo -n "Shutting down network time protocol daemon (NTPD)"
  172.     killproc -p ${NTPD_PID} -TERM $NTPD_BIN
  173.     rc_status -v
  174.     rm -f ${NTPD_PID}
  175.     ;;
  176.     try-restart)
  177.         $0 status
  178.         if test $? = 0; then
  179.                 $0 restart
  180.         else
  181.                 rc_reset        # Not running is not a failure.
  182.         fi
  183.         # Remember status and be quiet
  184.         rc_status
  185.     ;;
  186.     restart)
  187.     $0 stop
  188.     $0 start
  189.     rc_status
  190.         ;;
  191.     try-restart-iburst)
  192.     $0 status
  193.     if test $? = 0; then
  194.                 $0 stop
  195.         $0 start iburst 
  196.         else
  197.                 rc_reset        # Not running is not a failure.
  198.         fi
  199.     # Remember status and be quiet
  200.     rc_status
  201.         ;;
  202.     force-reload)
  203.     # Does not support signalling to reload
  204.         $0 try-restart
  205.     rc_status
  206.     ;;
  207.     reload)
  208.     echo -n "Reload network time protocol daemon (NTPD)"
  209.     # Does not support signalling to reload
  210.     rc_failed 3
  211.     rc_status -v
  212.     ;;
  213.     status)
  214.     echo -n "Checking for network time protocol daemon (NTPD): "
  215.     checkproc -p ${NTPD_PID} $NTPD_BIN
  216.     rc_status -v
  217.     ;;
  218.     probe)
  219.     # test /etc/ntp.conf -nt /var/run/ntp.pid && echo restart
  220.     rc_failed 3
  221.     ;;
  222.     ntptimeset)
  223.     NTPDATE_FROM=$(initial_ntpdate)
  224.     if [ -n "$NTPDATE_FROM" -a -x $NTPDATE_BIN ]; then
  225.         if ntpd_is_running; then
  226.             echo -n "Can't set time while ntpd is running"
  227.             rc_failed 2
  228.             rc_status -v
  229.         else
  230.             echo -n "Try to get initial date and time via NTP from $NTPDATE_FROM"
  231.             # -b: Set time on runlevel change, otherwise let 
  232.             #     ntpdate decide whether to slew or step.
  233.             test -z $INIT_VERSION \
  234.                 && FORCE_STEP="" \
  235.                 || FORCE_STEP="-b"
  236.             # -u: Use an unprivileged port for outgoing packets,
  237.             #     may be we have to synchronise with hosts beyond 
  238.             #     a firewall.
  239.             $NTPDATE_BIN -s $FORCE_STEP $NTPDATE_FROM \
  240.                 && echo $rc_done \
  241.                 || echo $rc_failed
  242.             # error here is reported but not propagated.
  243.         fi
  244.     fi
  245.     ;;
  246.     *)
  247.     echo "Usage: $0 {start|stop|status|try-restart|restart|try-restart-iburst|force-reload|reload|probe|ntptimeset}"
  248.     exit 1
  249.     ;;
  250. esac
  251. rc_exit
  252.