netwait revision 296940
11539Srgrimes#!/bin/sh
214288Spst
31539Srgrimes# $FreeBSD: stable/10/etc/rc.d/netwait 296940 2016-03-16 16:21:30Z ian $
41539Srgrimes#
51539Srgrimes# PROVIDE: netwait
61539Srgrimes# REQUIRE: devd ipfilter ipfw pf routing
71539Srgrimes# KEYWORD: nojail
81539Srgrimes#
91539Srgrimes# The netwait script helps handle two situations:
101539Srgrimes#  - Systems with USB or other late-attaching network hardware which
111539Srgrimes#    is initialized by devd events.  The script waits for all the
121539Srgrimes#    interfaces named in the netwait_if list to appear.
131539Srgrimes#  - Systems with statically-configured IP addresses in rc.conf(5).
141539Srgrimes#    The IP addresses in the netwait_ip list are pinged.  The script
151539Srgrimes#    waits for any single IP in the list to respond to the ping.  If your
161539Srgrimes#    system uses DHCP, you should probably use synchronous_dhclient="YES"
171539Srgrimes#    in your /etc/rc.conf instead of netwait_ip.
181539Srgrimes# Either or both of the wait lists can be used (at least one must be
191539Srgrimes# non-empty if netwait is enabled).
201539Srgrimes
211539Srgrimes. /etc/rc.subr
221539Srgrimes
231539Srgrimesname="netwait"
241539Srgrimesrcvar="netwait_enable"
251539Srgrimes
261539Srgrimesstart_cmd="${name}_start"
271539Srgrimesstop_cmd=":"
281539Srgrimes
291539Srgrimesnetwait_start()
301539Srgrimes{
311539Srgrimes	local ip rc count output link wait_if got_if any_error
321539Srgrimes
3314288Spst	if [ -z "${netwait_if}" ] && [ -z "${netwait_ip}" ]; then
3460833Sjake		err 1 "No interface or IP addresses listed, nothing to wait for"
351539Srgrimes	fi
361539Srgrimes
372162Spaul	if [ ${netwait_timeout} -lt 1 ]; then
382162Spaul		err 1 "netwait_timeout must be >= 1"
392162Spaul	fi
4014288Spst
4114288Spst	if [ -n "${netwait_if}" ]; then
421539Srgrimes		any_error=0
4314288Spst		for wait_if in ${netwait_if}; do
4414288Spst			echo -n "Waiting for ${wait_if}"
4514288Spst			link=""
4614288Spst			got_if=0
4714288Spst			count=1
481539Srgrimes			# Handle SIGINT (Ctrl-C); force abort of while() loop
491539Srgrimes			trap break SIGINT
501539Srgrimes			while [ ${count} -le ${netwait_if_timeout} ]; do
511539Srgrimes				if output=`/sbin/ifconfig ${wait_if} 2>/dev/null`; then
5214288Spst					if [ ${got_if} -eq 0 ]; then
5314288Spst						echo -n ", interface present"
5470492Sphk						got_if=1
5570492Sphk					fi
5614288Spst					link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
5714288Spst					if [ -z "${link}" ]; then
581539Srgrimes						echo ', got link.'
591539Srgrimes						break
601539Srgrimes					fi
6114288Spst				fi
621539Srgrimes				sleep 1
631539Srgrimes				count=$((count+1))
641539Srgrimes			done
6570492Sphk			# Restore default SIGINT handler
6614288Spst			trap - SIGINT
6770492Sphk			if [ ${got_if} -eq 0 ]; then
6814288Spst				echo ", wait failed: interface never appeared."
6914288Spst				any_error=1
7014288Spst			elif [ -n "${link}" ]; then
7114288Spst				echo ", wait failed: interface still has no link."
7214288Spst				any_error=1
7314288Spst			fi
7493032Simp		done
7514288Spst		if [ ${any_error} -eq 1 ]; then
7693032Simp		    warn "Continuing with startup, but be aware you may not have "
7714288Spst		    warn "a fully functional networking layer at this point."
781539Srgrimes		fi
7914288Spst	fi
8014288Spst	
8114288Spst	if [ -n "${netwait_ip}" ]; then
8214288Spst		# Handle SIGINT (Ctrl-C); force abort of for() loop
8314288Spst		trap break SIGINT
8414288Spst
8514288Spst		for ip in ${netwait_ip}; do
8614288Spst			echo -n "Waiting for ${ip} to respond to ICMP ping"
8714288Spst
881539Srgrimes			count=1
891539Srgrimes			while [ ${count} -le ${netwait_timeout} ]; do
901539Srgrimes				/sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1
911539Srgrimes				rc=$?
9293032Simp
9393032Simp				if [ $rc -eq 0 ]; then
9493032Simp					# Restore default SIGINT handler
9593032Simp					trap - SIGINT
9693032Simp
9793032Simp					echo ', got response.'
9893032Simp					return
9993032Simp				fi
1001539Srgrimes				count=$((count+1))
10193032Simp			done
1021539Srgrimes			echo ', failed: No response from host.'
1031539Srgrimes		done
1042162Spaul
1052162Spaul		# Restore default SIGINT handler
106		trap - SIGINT
107
108		warn "Exhausted IP list.  Continuing with startup, but be aware you may"
109		warn "not have a fully functional networking layer at this point."
110	fi
111
112}
113
114load_rc_config $name
115run_rc_command "$1"
116