1223311Sdougb#!/bin/sh 2223311Sdougb 3223311Sdougb# $FreeBSD$ 4223311Sdougb# 5223311Sdougb# PROVIDE: netwait 6296807Sian# REQUIRE: devd ipfilter ipfw pf routing 7223311Sdougb# KEYWORD: nojail 8223311Sdougb# 9292752Sian# The netwait script helps handle two situations: 10292752Sian# - Systems with USB or other late-attaching network hardware which 11292752Sian# is initialized by devd events. The script waits for all the 12292752Sian# interfaces named in the netwait_if list to appear. 13292752Sian# - Systems with statically-configured IP addresses in rc.conf(5). 14292752Sian# The IP addresses in the netwait_ip list are pinged. The script 15292752Sian# waits for any single IP in the list to respond to the ping. If your 16292752Sian# system uses DHCP, you should probably use synchronous_dhclient="YES" 17292752Sian# in your /etc/rc.conf instead of netwait_ip. 18292752Sian# Either or both of the wait lists can be used (at least one must be 19292752Sian# non-empty if netwait is enabled). 20223311Sdougb 21223311Sdougb. /etc/rc.subr 22223311Sdougb 23223311Sdougbname="netwait" 24298514Slmedesc="Wait for network devices or the network being up" 25230099Sdougbrcvar="netwait_enable" 26223408Sdougb 27223311Sdougbstart_cmd="${name}_start" 28223311Sdougbstop_cmd=":" 29223311Sdougb 30223311Sdougbnetwait_start() 31223311Sdougb{ 32292752Sian local ip rc count output link wait_if got_if any_error 33223311Sdougb 34292752Sian if [ -z "${netwait_if}" ] && [ -z "${netwait_ip}" ]; then 35292752Sian err 1 "No interface or IP addresses listed, nothing to wait for" 36223311Sdougb fi 37223311Sdougb 38223311Sdougb if [ ${netwait_timeout} -lt 1 ]; then 39223311Sdougb err 1 "netwait_timeout must be >= 1" 40223311Sdougb fi 41223311Sdougb 42223311Sdougb if [ -n "${netwait_if}" ]; then 43292752Sian any_error=0 44292752Sian for wait_if in ${netwait_if}; do 45292752Sian echo -n "Waiting for ${wait_if}" 46292752Sian link="" 47292752Sian got_if=0 48292752Sian count=1 49292752Sian # Handle SIGINT (Ctrl-C); force abort of while() loop 50292752Sian trap break SIGINT 51292752Sian while [ ${count} -le ${netwait_if_timeout} ]; do 52292752Sian if output=`/sbin/ifconfig ${wait_if} 2>/dev/null`; then 53292752Sian if [ ${got_if} -eq 0 ]; then 54292752Sian echo -n ", interface present" 55292752Sian got_if=1 56292752Sian fi 57292752Sian link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'` 58292752Sian if [ -z "${link}" ]; then 59292752Sian echo ', got link.' 60292752Sian break 61292752Sian fi 62223311Sdougb fi 63292752Sian sleep 1 64292752Sian count=$((count+1)) 65292752Sian done 66292752Sian # Restore default SIGINT handler 67292752Sian trap - SIGINT 68292752Sian if [ ${got_if} -eq 0 ]; then 69292752Sian echo ", wait failed: interface never appeared." 70292752Sian any_error=1 71292752Sian elif [ -n "${link}" ]; then 72292752Sian echo ", wait failed: interface still has no link." 73292752Sian any_error=1 74223311Sdougb fi 75223311Sdougb done 76292752Sian if [ ${any_error} -eq 1 ]; then 77292752Sian warn "Continuing with startup, but be aware you may not have " 78292752Sian warn "a fully functional networking layer at this point." 79223311Sdougb fi 80223311Sdougb fi 81292752Sian 82292752Sian if [ -n "${netwait_ip}" ]; then 83292752Sian # Handle SIGINT (Ctrl-C); force abort of for() loop 84292752Sian trap break SIGINT 85223311Sdougb 86292752Sian for ip in ${netwait_ip}; do 87292752Sian echo -n "Waiting for ${ip} to respond to ICMP ping" 88223311Sdougb 89292752Sian count=1 90292752Sian while [ ${count} -le ${netwait_timeout} ]; do 91292752Sian /sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1 92292752Sian rc=$? 93223311Sdougb 94292752Sian if [ $rc -eq 0 ]; then 95292752Sian # Restore default SIGINT handler 96292752Sian trap - SIGINT 97223311Sdougb 98292752Sian echo ', got response.' 99292752Sian return 100292752Sian fi 101292752Sian count=$((count+1)) 102292752Sian done 103292752Sian echo ', failed: No response from host.' 104223311Sdougb done 105223311Sdougb 106292752Sian # Restore default SIGINT handler 107292752Sian trap - SIGINT 108223311Sdougb 109292752Sian warn "Exhausted IP list. Continuing with startup, but be aware you may" 110292752Sian warn "not have a fully functional networking layer at this point." 111292752Sian fi 112292752Sian 113223311Sdougb} 114223311Sdougb 115223311Sdougbload_rc_config $name 116223311Sdougbrun_rc_command "$1" 117