#!/usr/bin/sh #------------------------------------------------------------------------------ # /usr/sbin/setdate - set date to current time # # Copyright (c) 2001-2024 The Eisfair Team, team(at)eisfair(dot)org # # Creation: 2003-11-14 jed # Last Update: $Id$ # # Usage: setdate [--quiet] # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #------------------------------------------------------------------------------ # include eislib . /var/install/include/eislib #exec 2> /tmp/netdate-trace-$$.log #set -x #------------------------------------------------------------------------------ # check_backgroundprocess # ======================= # Parameters: $1 waittime until background process, if not # already terminated, will be killed # $2 PID of background process # # Returns: 0 background process terminated # 1 background process was killed # # Thank you Ansgar for your help solving this annoying udp timeout/connection # problem and the hang-at-boot-time problem :-) #------------------------------------------------------------------------------ check_backgroundprocess() { # sleep 1 second SLEEPTIME=1 # wait at most $1 seconds WAITTIME=$1 RTIME=$1 PID=$2 # loop while [ ${RTIME} -gt 0 ] do sleep ${SLEEPTIME} # check process $2 kill -0 ${PID} >/dev/null 2>/dev/null RC=$? if [ ${RC} = 1 ] then # process terminated return 0 else RTIME=`expr ${RTIME} - ${SLEEPTIME}` fi done # process still lives kill -9 ${PID} >/dev/null 2>&1 return 1 } #============================================================================== # main #============================================================================== . /etc/config.d/netdate if [ "${NETDATE_DO_DEBUG}" = "yes" ] then set -x fi # file names netdate_tmpfile=/tmp/netdate.$$ netdate_e_tmpfile=/tmp/netdate_e.$$ netdate_s_tmpfile=/tmp/netdate_s.$$ netdate_r_tmpfile=/tmp/netdate_return.$$ # command line parameter if [ "$1" = '-quiet' -o "$1" = '--quiet' ] then qflag='-quiet' shift fi if [ "${START_NETDATE}" = 'yes' ] then # get timeserver checked_servers='' server_port=37 # port 37 - time protocol server_protocol='' for SVRNAME in ${NETDATE_TIMESERVER} do if [ "${SVRNAME}" = "tcp" -o "${SVRNAME}" = "udp" ] then # save protocol server_protocol=${SVRNAME} else # check if timeserver(s) can be reached # nc command line switches: # -w - timeout in seconds # -v - be verbose # -z - scan for listening daemons, without sending any data to them for count in 1 2 3 4 do /usr/bin/nc -w 2 -z ${SVRNAME} ${server_port} 2>/dev/null ret=${?} if [ ${ret} -eq 0 ] then break fi sleep 2 done if [ ${ret} -eq 0 ] then if [ -z "${checked_servers}" ] then checked_servers="${server_protocol} ${SVRNAME}" else checked_servers="${checked_servers} ${server_protocol} ${SVRNAME}" fi server_protocol='' else if [ "${qflag}" != "-quiet" ] then echo "- skipping timeserver '${SVRNAME}', because it cannot be reached or the port is not accessible!" fi fi fi done # remove optional protocols from display string display_checked_servers=`echo "${checked_servers}" | sed 's/udp//g;s/tcp//g;s/^ *//' | tr -s ' ' ' '` if [ -n "${checked_servers}" ] then # display checked_servers only if not empty if [ "${qflag}" != "-quiet" ] then mecho mecho "Syncing time using timeserver(s) '${display_checked_servers}':" fi # save stderr exec 3>&2 # redirect stderr to /dev/null # this will also disable set -x output! exec 2>/dev/null # sync time ( /usr/sbin/netdate -l 10 ${checked_servers} > ${netdate_s_tmpfile} 2> ${netdate_e_tmpfile}; echo $? > ${netdate_r_tmpfile} ) >/dev/null 2>&1 & BPID=$! # assume no error ret=0 # check background process # wait at most 30 seconds if ! check_backgroundprocess 30 ${BPID} >/dev/null 2>&1 then # only the sourrounding shell was killed # get PID of netdate command and kill it too BPID_ND=`ps -eo "%p %c %a" | grep "/usr/sbin/netdate -l 10" | sed 's/^ *//' | cut -f1 -d' '` BPID_ND=`echo $BPID_ND` [ -z "$BPID_ND" ] && kill -9 $BPID_ND >/dev/null 2>&1 echo "Error: Aborting time synchronisation due to a timeout condition." >> ${netdate_e_tmpfile} ret=1 fi # check and remove netdate_return file if [ -f ${netdate_r_tmpfile} ] then ret=`cat ${netdate_r_tmpfile}` rm ${netdate_r_tmpfile} fi cat ${netdate_s_tmpfile} ${netdate_e_tmpfile} > ${netdate_tmpfile} rm -f ${netdate_s_tmpfile} ${netdate_e_tmpfile} if [ ${ret} -eq 0 ] then # adjust time drift /usr/sbin/hwclock --adjust | grep -v 'Needed adjustment is less than one second' # set hw clock /usr/sbin/hwclock --systohc --utc fi # restore stderr, close &3 exec 2>&3 3>&- #test echo "Check stderr" >&2 #test echo "Check stdout" >&1 # strip CR /usr/bin/dtou ${netdate_tmpfile} if [ "${qflag}" != "-quiet" ] then # show result cat ${netdate_tmpfile} fi if [ "$NETDATE_LOGGING" = "yes" ] then # write to syslog /usr/bin/logger -t "netdate" -p "local0.info" "Syncing time using timeserver ${display_checked_servers}" while read line do /usr/bin/logger -t "netdate" -p "local0.info" "${line}" done < ${netdate_tmpfile} fi # delete tmp file rm -f ${netdate_tmpfile} else if [ "${qflag}" != "-quiet" ] then mecho --error "No timeservers are reachable, syncing aborted!" fi if [ "${NETDATE_LOGGING}" = "yes" ] then /usr/bin/logger -t "netdate" -p "local0.error" "No timeservers are reachable, syncing aborted!" fi fi else if [ "${qflag}" != "-quiet" ] then mecho --info "Time syncing has been disabled" fi fi if [ "${NETDATE_DO_DEBUG}" = "yes" ] then set +x fi #============================================================================== # end #==============================================================================