netwait revision 298514
1#!/bin/sh
2
3# $FreeBSD: head/etc/rc.d/netwait 298514 2016-04-23 16:10:54Z lme $
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"
24desc="Wait for network devices or the network being up"
25rcvar="netwait_enable"
26
27start_cmd="${name}_start"
28stop_cmd=":"
29
30netwait_start()
31{
32	local ip rc count output link wait_if got_if any_error
33
34	if [ -z "${netwait_if}" ] && [ -z "${netwait_ip}" ]; then
35		err 1 "No interface or IP addresses listed, nothing to wait for"
36	fi
37
38	if [ ${netwait_timeout} -lt 1 ]; then
39		err 1 "netwait_timeout must be >= 1"
40	fi
41
42	if [ -n "${netwait_if}" ]; then
43		any_error=0
44		for wait_if in ${netwait_if}; do
45			echo -n "Waiting for ${wait_if}"
46			link=""
47			got_if=0
48			count=1
49			# Handle SIGINT (Ctrl-C); force abort of while() loop
50			trap break SIGINT
51			while [ ${count} -le ${netwait_if_timeout} ]; do
52				if output=`/sbin/ifconfig ${wait_if} 2>/dev/null`; then
53					if [ ${got_if} -eq 0 ]; then
54						echo -n ", interface present"
55						got_if=1
56					fi
57					link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
58					if [ -z "${link}" ]; then
59						echo ', got link.'
60						break
61					fi
62				fi
63				sleep 1
64				count=$((count+1))
65			done
66			# Restore default SIGINT handler
67			trap - SIGINT
68			if [ ${got_if} -eq 0 ]; then
69				echo ", wait failed: interface never appeared."
70				any_error=1
71			elif [ -n "${link}" ]; then
72				echo ", wait failed: interface still has no link."
73				any_error=1
74			fi
75		done
76		if [ ${any_error} -eq 1 ]; then
77		    warn "Continuing with startup, but be aware you may not have "
78		    warn "a fully functional networking layer at this point."
79		fi
80	fi
81	
82	if [ -n "${netwait_ip}" ]; then
83		# Handle SIGINT (Ctrl-C); force abort of for() loop
84		trap break SIGINT
85
86		for ip in ${netwait_ip}; do
87			echo -n "Waiting for ${ip} to respond to ICMP ping"
88
89			count=1
90			while [ ${count} -le ${netwait_timeout} ]; do
91				/sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1
92				rc=$?
93
94				if [ $rc -eq 0 ]; then
95					# Restore default SIGINT handler
96					trap - SIGINT
97
98					echo ', got response.'
99					return
100				fi
101				count=$((count+1))
102			done
103			echo ', failed: No response from host.'
104		done
105
106		# Restore default SIGINT handler
107		trap - SIGINT
108
109		warn "Exhausted IP list.  Continuing with startup, but be aware you may"
110		warn "not have a fully functional networking layer at this point."
111	fi
112
113}
114
115load_rc_config $name
116run_rc_command "$1"
117