netwait revision 296807
1223311Sdougb#!/bin/sh 2223311Sdougb 3223311Sdougb# $FreeBSD: head/etc/rc.d/netwait 296807 2016-03-13 19:42:59Z ian $ 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" 24230099Sdougbrcvar="netwait_enable" 25223408Sdougb 26223311Sdougbstart_cmd="${name}_start" 27223311Sdougbstop_cmd=":" 28223311Sdougb 29223311Sdougbnetwait_start() 30223311Sdougb{ 31292752Sian local ip rc count output link wait_if got_if any_error 32223311Sdougb 33292752Sian if [ -z "${netwait_if}" ] && [ -z "${netwait_ip}" ]; then 34292752Sian err 1 "No interface or IP addresses listed, nothing to wait for" 35223311Sdougb fi 36223311Sdougb 37223311Sdougb if [ ${netwait_timeout} -lt 1 ]; then 38223311Sdougb err 1 "netwait_timeout must be >= 1" 39223311Sdougb fi 40223311Sdougb 41223311Sdougb if [ -n "${netwait_if}" ]; then 42292752Sian any_error=0 43292752Sian for wait_if in ${netwait_if}; do 44292752Sian echo -n "Waiting for ${wait_if}" 45292752Sian link="" 46292752Sian got_if=0 47292752Sian count=1 48292752Sian # Handle SIGINT (Ctrl-C); force abort of while() loop 49292752Sian trap break SIGINT 50292752Sian while [ ${count} -le ${netwait_if_timeout} ]; do 51292752Sian if output=`/sbin/ifconfig ${wait_if} 2>/dev/null`; then 52292752Sian if [ ${got_if} -eq 0 ]; then 53292752Sian echo -n ", interface present" 54292752Sian got_if=1 55292752Sian fi 56292752Sian link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'` 57292752Sian if [ -z "${link}" ]; then 58292752Sian echo ', got link.' 59292752Sian break 60292752Sian fi 61223311Sdougb fi 62292752Sian sleep 1 63292752Sian count=$((count+1)) 64292752Sian done 65292752Sian # Restore default SIGINT handler 66292752Sian trap - SIGINT 67292752Sian if [ ${got_if} -eq 0 ]; then 68292752Sian echo ", wait failed: interface never appeared." 69292752Sian any_error=1 70292752Sian elif [ -n "${link}" ]; then 71292752Sian echo ", wait failed: interface still has no link." 72292752Sian any_error=1 73223311Sdougb fi 74223311Sdougb done 75292752Sian if [ ${any_error} -eq 1 ]; then 76292752Sian warn "Continuing with startup, but be aware you may not have " 77292752Sian warn "a fully functional networking layer at this point." 78223311Sdougb fi 79223311Sdougb fi 80292752Sian 81292752Sian if [ -n "${netwait_ip}" ]; then 82292752Sian # Handle SIGINT (Ctrl-C); force abort of for() loop 83292752Sian trap break SIGINT 84223311Sdougb 85292752Sian for ip in ${netwait_ip}; do 86292752Sian echo -n "Waiting for ${ip} to respond to ICMP ping" 87223311Sdougb 88292752Sian count=1 89292752Sian while [ ${count} -le ${netwait_timeout} ]; do 90292752Sian /sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1 91292752Sian rc=$? 92223311Sdougb 93292752Sian if [ $rc -eq 0 ]; then 94292752Sian # Restore default SIGINT handler 95292752Sian trap - SIGINT 96223311Sdougb 97292752Sian echo ', got response.' 98292752Sian return 99292752Sian fi 100292752Sian count=$((count+1)) 101292752Sian done 102292752Sian echo ', failed: No response from host.' 103223311Sdougb done 104223311Sdougb 105292752Sian # Restore default SIGINT handler 106292752Sian trap - SIGINT 107223311Sdougb 108292752Sian warn "Exhausted IP list. Continuing with startup, but be aware you may" 109292752Sian warn "not have a fully functional networking layer at this point." 110292752Sian fi 111292752Sian 112223311Sdougb} 113223311Sdougb 114223311Sdougbload_rc_config $name 115223311Sdougbrun_rc_command "$1" 116