netwait revision 223311
1#!/bin/sh
2
3# $FreeBSD: head/etc/rc.d/netwait 223311 2011-06-19 22:59:54Z 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"
17rc_var=`set_rcvar`
18start_cmd="${name}_start"
19stop_cmd=":"
20
21netwait_start()
22{
23	local ip rc count output link
24
25	if [ -z "${netwait_ip}" ]; then
26		err 1 "You must define one or more IP addresses in netwait_ip"
27	fi
28
29	if [ ${netwait_timeout} -lt 1 ]; then
30		err 1 "netwait_timeout must be >= 1"
31	fi
32
33	# Handle SIGINT (Ctrl-C); force abort of while() loop
34	trap break SIGINT
35
36	if [ -n "${netwait_if}" ]; then
37		echo -n "Waiting for $netwait_if to have link"
38
39		count=1
40		while [ ${count} -le ${netwait_if_timeout} ]; do
41			if output=`/sbin/ifconfig ${netwait_if} 2>/dev/null`; then
42				link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
43				if [ -z "${link}" ]; then
44					echo '.'
45					break
46				fi
47			else
48				echo ''
49				err 1 "ifconfig ${netwait_if} failed"
50			fi
51			sleep 1
52			count=$((count+1))
53		done
54		if [ -n "${link}" ]; then
55			# Restore default SIGINT handler
56			trap - SIGINT
57
58			echo ''
59			warn "Interface still has no link.  Continuing with startup, but"
60			warn "be aware you may not have a fully functional networking"
61			warn "layer at this point."
62			return
63		fi
64	fi
65
66	# Handle SIGINT (Ctrl-C); force abort of while() loop
67	trap break SIGINT
68
69	for ip in ${netwait_ip}; do
70		echo -n "Waiting for ${ip} to respond to ICMP"
71
72		count=1
73		while [ ${count} -le ${netwait_timeout} ]; do
74			/sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1
75			rc=$?
76
77			if [ $rc -eq 0 ]; then
78				# Restore default SIGINT handler
79				trap - SIGINT
80
81				echo '.'
82				return
83			fi
84			count=$((count+1))
85		done
86		echo ': No response from host.'
87	done
88
89	# Restore default SIGINT handler
90	trap - SIGINT
91
92	warn "Exhausted IP list.  Continuing with startup, but be aware you may"
93	warn "not have a fully functional networking layer at this point."
94}
95
96load_rc_config $name
97run_rc_command "$1"
98