netwait revision 292752
1230832Sgnn#!/bin/sh
2230832Sgnn
3230832Sgnn# $FreeBSD: head/etc/rc.d/netwait 292752 2015-12-26 18:21:32Z ian $
4230832Sgnn#
5230832Sgnn# PROVIDE: netwait
6230832Sgnn# REQUIRE: devd routing
7230832Sgnn# KEYWORD: nojail
8230832Sgnn#
9230832Sgnn# The netwait script helps handle two situations:
10230832Sgnn#  - Systems with USB or other late-attaching network hardware which
11230832Sgnn#    is initialized by devd events.  The script waits for all the
12230832Sgnn#    interfaces named in the netwait_if list to appear.
13230832Sgnn#  - Systems with statically-configured IP addresses in rc.conf(5).
14230832Sgnn#    The IP addresses in the netwait_ip list are pinged.  The script
15230832Sgnn#    waits for any single IP in the list to respond to the ping.  If your
16230832Sgnn#    system uses DHCP, you should probably use synchronous_dhclient="YES"
17230832Sgnn#    in your /etc/rc.conf instead of netwait_ip.
18230832Sgnn# Either or both of the wait lists can be used (at least one must be
19230832Sgnn# non-empty if netwait is enabled).
20230832Sgnn
21230832Sgnn. /etc/rc.subr
22230832Sgnn
23230832Sgnnname="netwait"
24230832Sgnnrcvar="netwait_enable"
25230832Sgnn
26230832Sgnnstart_cmd="${name}_start"
27230832Sgnnstop_cmd=":"
28230832Sgnn
29230832Sgnnnetwait_start()
30230832Sgnn{
31230832Sgnn	local ip rc count output link wait_if got_if any_error
32230832Sgnn
33230832Sgnn	if [ -z "${netwait_if}" ] && [ -z "${netwait_ip}" ]; then
34230832Sgnn		err 1 "No interface or IP addresses listed, nothing to wait for"
35230832Sgnn	fi
36230832Sgnn
37230832Sgnn	if [ ${netwait_timeout} -lt 1 ]; then
38230832Sgnn		err 1 "netwait_timeout must be >= 1"
39230832Sgnn	fi
40230832Sgnn
41230832Sgnn	if [ -n "${netwait_if}" ]; then
42230832Sgnn		any_error=0
43230832Sgnn		for wait_if in ${netwait_if}; do
44230832Sgnn			echo -n "Waiting for ${wait_if}"
45230832Sgnn			link=""
46230832Sgnn			got_if=0
47230832Sgnn			count=1
48230832Sgnn			# Handle SIGINT (Ctrl-C); force abort of while() loop
49230832Sgnn			trap break SIGINT
50230832Sgnn			while [ ${count} -le ${netwait_if_timeout} ]; do
51230832Sgnn				if output=`/sbin/ifconfig ${wait_if} 2>/dev/null`; then
52230832Sgnn					if [ ${got_if} -eq 0 ]; then
53230832Sgnn						echo -n ", interface present"
54230832Sgnn						got_if=1
55230832Sgnn					fi
56230832Sgnn					link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
57230832Sgnn					if [ -z "${link}" ]; then
58230832Sgnn						echo ', got link.'
59230832Sgnn						break
60230832Sgnn					fi
61230832Sgnn				fi
62230832Sgnn				sleep 1
63230832Sgnn				count=$((count+1))
64230832Sgnn			done
65230832Sgnn			# Restore default SIGINT handler
66			trap - SIGINT
67			if [ ${got_if} -eq 0 ]; then
68				echo ", wait failed: interface never appeared."
69				any_error=1
70			elif [ -n "${link}" ]; then
71				echo ", wait failed: interface still has no link."
72				any_error=1
73			fi
74		done
75		if [ ${any_error} -eq 1 ]; then
76		    warn "Continuing with startup, but be aware you may not have "
77		    warn "a fully functional networking layer at this point."
78		fi
79	fi
80	
81	if [ -n "${netwait_ip}" ]; then
82		# Handle SIGINT (Ctrl-C); force abort of for() loop
83		trap break SIGINT
84
85		for ip in ${netwait_ip}; do
86			echo -n "Waiting for ${ip} to respond to ICMP ping"
87
88			count=1
89			while [ ${count} -le ${netwait_timeout} ]; do
90				/sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1
91				rc=$?
92
93				if [ $rc -eq 0 ]; then
94					# Restore default SIGINT handler
95					trap - SIGINT
96
97					echo ', got response.'
98					return
99				fi
100				count=$((count+1))
101			done
102			echo ', failed: No response from host.'
103		done
104
105		# 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