netwait revision 296946
1#!/bin/sh
2
3# $FreeBSD: releng/10.3/etc/rc.d/netwait 296946 2016-03-16 17:35:55Z ian $
4#
5# PROVIDE: netwait
6# REQUIRE: devd ipfilter ipfw pf routing
7# KEYWORD: nojail
8#
9# The netwait script helps handle two situations:
10#  - Systems with USB or other late-attaching network hardware which
11#    is initialized by devd events.  The script waits for all the
12#    interfaces named in the netwait_if list to appear.
13#  - Systems with statically-configured IP addresses in rc.conf(5).
14#    The IP addresses in the netwait_ip list are pinged.  The script
15#    waits for any single IP in the list to respond to the ping.  If your
16#    system uses DHCP, you should probably use synchronous_dhclient="YES"
17#    in your /etc/rc.conf instead of netwait_ip.
18# Either or both of the wait lists can be used (at least one must be
19# non-empty if netwait is enabled).
20
21. /etc/rc.subr
22
23name="netwait"
24rcvar="netwait_enable"
25
26start_cmd="${name}_start"
27stop_cmd=":"
28
29netwait_start()
30{
31	local ip rc count output link wait_if got_if any_error
32
33	if [ -z "${netwait_if}" ] && [ -z "${netwait_ip}" ]; then
34		err 1 "No interface or IP addresses listed, nothing to wait for"
35	fi
36
37	if [ ${netwait_timeout} -lt 1 ]; then
38		err 1 "netwait_timeout must be >= 1"
39	fi
40
41	if [ -n "${netwait_if}" ]; then
42		any_error=0
43		for wait_if in ${netwait_if}; do
44			echo -n "Waiting for ${wait_if}"
45			link=""
46			got_if=0
47			count=1
48			# Handle SIGINT (Ctrl-C); force abort of while() loop
49			trap break SIGINT
50			while [ ${count} -le ${netwait_if_timeout} ]; do
51				if output=`/sbin/ifconfig ${wait_if} 2>/dev/null`; then
52					if [ ${got_if} -eq 0 ]; then
53						echo -n ", interface present"
54						got_if=1
55					fi
56					link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
57					if [ -z "${link}" ]; then
58						echo ', got link.'
59						break
60					fi
61				fi
62				sleep 1
63				count=$((count+1))
64			done
65			# 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