conf/145530: [rc.d] ntpdate(8): ntpdate rc needs wpa_supplicant to go online

Alex Zbyslaw xfb52 at dial.pipex.com
Thu Apr 15 09:10:42 UTC 2010


I don't think the attached script is quite what you want, but I use it 
to solve a similar problem.

On my domestic network I have a gateway FreeBSD box that the entire 
local network sits behind, and I have the machine I use as my desktop.  
Both machines get switched off at least every night.  Come switch on 
time, I don't always remember to boot the gateway first, and often got 
exactly this problem --- ntpdate failing because I had no external 
network connection.

I was able to solve that by creating my own /usr/local/etc/rc.d script 
which runs before ntpdate and waits some amount of time for my gateway 
machine to come up.  If I know I'm booting with no network I can just 
interrupt it with ^C.

I'm sure that something similar would work for you.  Whether such a 
solution is appropriate more generally I don't know but something like 
it should work for you, I think, without any mods being required to the 
base system.

hth,

--Alex

$ egrep gateway_up /etc/rc.conf
gateway_up_enable="YES"                         # Wait for gateway to 
come up
gateway_up_host="192.168.23.3"

In addition you can set e.g.
gateway_up_external="some.domain.name" # External host we can ping
gateway_up_wait=600 # wait 600s for gateway to come up
gateway_up_external_wait=600 # wait 600s for external host to be 
available (e.g. waiting for named to start on gateway)

#!/bin/sh
#
# $Id: gateway_up,v 1.4 2009/05/25 11:23:52 alex Exp $
#

# PROVIDE: gateway_up
# REQUIRE: NETWORKING syslogd named
# BEFORE: ntpdate
# KEYWORD: nojail

. /etc/rc.subr

name="gateway_up"
rcvar=`set_rcvar`
start_cmd="gateway_up_start"
stop_cmd=":"
: ${gateway_up_wait:=180}
: ${gateway_up_external_wait:=180}

[ ${gateway_up_wait} -lt 0 ] && gateway_up_wait=$((-${gateway_up_wait}))
[ ${gateway_up_external_wait} -lt 0 ] && 
gateway_up_external_wait=$((-${gateway_up_external_wait}))

gateway_up_start()
{
    local external_host count echo_done

    if [ -z "$gateway_up_host" ]; then
        warn "gateway_up_host not set"
        return 1
    fi

    if [ -z "$gateway_up_external" ]; then
        if [ -n "$ntpdate_hosts" ]; then
            "$gateway_up_external"="$ntpdate_hosts"
        elif [ -z "$ntpdate_hosts" -a -f ${ntpdate_config} ]; then
            gateway_up_external=`awk '
            /^server[ \t]*127.127/      {next}
            /^(server|peer)/            {print $2}
            ' < ${ntpdate_config}`
        else
            warn "gateway_up_external unset and no ntp hosts found"
        fi
    fi

    if ! ping -W 1 -c 1  $gateway_up_host > /dev/null 2>&1; then
        # Wait for gateway host a maximum of ${gateway_up_wait} seconds
        echo -n "Waiting (roughly) ${gateway_up_wait}s for gateway 
${gateway_up_host}: "
        if ! ping -o -t ${gateway_up_wait} $gateway_up_host > /dev/null 
2>&1; then
            echo "not up...continuing"
            return 0
        fi
        echo "done"
    fi

    # Wait for an external host to ping for a rough maximum of
    # ${gateway_up_external_wait} seconds, trying each in turn
    # for 2 seconds
    if [ -z "${gateway_up_external}" ]; then
        echo "No gateway_up_external hosts to check"
        return 0
    fi
   
    count=0
    echo_done=0
    while [ $count -lt ${gateway_up_external_wait} ]; do
        for external_host in ${gateway_up_external}; do
            count=$((count+1))
            #echo checking $external_host
            # SIGALRM somewhere (prints "Alarm clock") sometimes
            # if we try -o -t 1
            if ping -W 100 -c 1 $external_host > /dev/null 2>&1; then
                if [ $echo_done -gt 0 ]; then
                    echo "done"
                fi
                return 0
            fi

            if [ $count -eq 1 ]; then
                echo -n "Waiting (roughly) ${gateway_up_external_wait}s 
for external connectivity: "
                echo_done=1
            fi

            #echo "sleeping"
            sleep 2
        done
    done
    if [ $echo_done -gt 0 ]; then
        echo "No connectivity...continuing"
    fi
}

load_rc_config $name
run_rc_command "$1"






More information about the freebsd-rc mailing list