netwait revision 231653
1#!/bin/sh
2
3# $FreeBSD: stable/9/etc/rc.d/netwait 231653 2012-02-14 10:16:56Z dougb $
4#
5# PROVIDE: netwait
6# REQUIRE: NETWORKING
7# KEYWORD: nojail
8#
9# The netwait script is intended to be used by systems which have
10# statically-configured IP addresses in rc.conf(5).  If your system
11# uses DHCP, you should use synchronous_dhclient="YES" in your
12# /etc/rc.conf instead of using netwait.
13
14. /etc/rc.subr
15
16name="netwait"
17rcvar="netwait_enable"
18
19start_cmd="${name}_start"
20stop_cmd=":"
21
22netwait_start()
23{
24	local ip rc count output link
25
26	if [ -z "${netwait_ip}" ]; then
27		err 1 "You must define one or more IP addresses in netwait_ip"
28	fi
29
30	if [ ${netwait_timeout} -lt 1 ]; then
31		err 1 "netwait_timeout must be >= 1"
32	fi
33
34	# Handle SIGINT (Ctrl-C); force abort of while() loop
35	trap break SIGINT
36
37	if [ -n "${netwait_if}" ]; then
38		echo -n "Waiting for $netwait_if to have link"
39
40		count=1
41		while [ ${count} -le ${netwait_if_timeout} ]; do
42			if output=`/sbin/ifconfig ${netwait_if} 2>/dev/null`; then
43				link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
44				if [ -z "${link}" ]; then
45					echo '.'
46					break
47				fi
48			else
49				echo ''
50				err 1 "ifconfig ${netwait_if} failed"
51			fi
52			sleep 1
53			count=$((count+1))
54		done
55		if [ -n "${link}" ]; then
56			# Restore default SIGINT handler
57			trap - SIGINT
58
59			echo ''
60			warn "Interface still has no link.  Continuing with startup, but"
61			warn "be aware you may not have a fully functional networking"
62			warn "layer at this point."
63			return
64		fi
65	fi
66
67	# Handle SIGINT (Ctrl-C); force abort of while() loop
68	trap break SIGINT
69
70	for ip in ${netwait_ip}; do
71		echo -n "Waiting for ${ip} to respond to ICMP"
72
73		count=1
74		while [ ${count} -le ${netwait_timeout} ]; do
75			/sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1
76			rc=$?
77
78			if [ $rc -eq 0 ]; then
79				# Restore default SIGINT handler
80				trap - SIGINT
81
82				echo '.'
83				return
84			fi
85			count=$((count+1))
86		done
87		echo ': No response from host.'
88	done
89
90	# Restore default SIGINT handler
91	trap - SIGINT
92
93	warn "Exhausted IP list.  Continuing with startup, but be aware you may"
94	warn "not have a fully functional networking layer at this point."
95}
96
97load_rc_config $name
98run_rc_command "$1"
99