1223311Sdougb#!/bin/sh
2223311Sdougb
3223311Sdougb# $FreeBSD$
4223311Sdougb#
5223311Sdougb# PROVIDE: netwait
6223311Sdougb# REQUIRE: NETWORKING
7223311Sdougb# KEYWORD: nojail
8223311Sdougb#
9223311Sdougb# The netwait script is intended to be used by systems which have
10223311Sdougb# statically-configured IP addresses in rc.conf(5).  If your system
11223311Sdougb# uses DHCP, you should use synchronous_dhclient="YES" in your
12223311Sdougb# /etc/rc.conf instead of using netwait.
13223311Sdougb
14223311Sdougb. /etc/rc.subr
15223311Sdougb
16223311Sdougbname="netwait"
17231653Sdougbrcvar="netwait_enable"
18223408Sdougb
19223311Sdougbstart_cmd="${name}_start"
20223311Sdougbstop_cmd=":"
21223311Sdougb
22223311Sdougbnetwait_start()
23223311Sdougb{
24223311Sdougb	local ip rc count output link
25223311Sdougb
26223311Sdougb	if [ -z "${netwait_ip}" ]; then
27223311Sdougb		err 1 "You must define one or more IP addresses in netwait_ip"
28223311Sdougb	fi
29223311Sdougb
30223311Sdougb	if [ ${netwait_timeout} -lt 1 ]; then
31223311Sdougb		err 1 "netwait_timeout must be >= 1"
32223311Sdougb	fi
33223311Sdougb
34223311Sdougb	# Handle SIGINT (Ctrl-C); force abort of while() loop
35223311Sdougb	trap break SIGINT
36223311Sdougb
37223311Sdougb	if [ -n "${netwait_if}" ]; then
38223311Sdougb		echo -n "Waiting for $netwait_if to have link"
39223311Sdougb
40223311Sdougb		count=1
41223311Sdougb		while [ ${count} -le ${netwait_if_timeout} ]; do
42223311Sdougb			if output=`/sbin/ifconfig ${netwait_if} 2>/dev/null`; then
43223311Sdougb				link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
44223311Sdougb				if [ -z "${link}" ]; then
45223311Sdougb					echo '.'
46223311Sdougb					break
47223311Sdougb				fi
48223311Sdougb			else
49223311Sdougb				echo ''
50223311Sdougb				err 1 "ifconfig ${netwait_if} failed"
51223311Sdougb			fi
52223311Sdougb			sleep 1
53223311Sdougb			count=$((count+1))
54223311Sdougb		done
55223311Sdougb		if [ -n "${link}" ]; then
56223311Sdougb			# Restore default SIGINT handler
57223311Sdougb			trap - SIGINT
58223311Sdougb
59223311Sdougb			echo ''
60223311Sdougb			warn "Interface still has no link.  Continuing with startup, but"
61223311Sdougb			warn "be aware you may not have a fully functional networking"
62223311Sdougb			warn "layer at this point."
63223311Sdougb			return
64223311Sdougb		fi
65223311Sdougb	fi
66223311Sdougb
67223311Sdougb	# Handle SIGINT (Ctrl-C); force abort of while() loop
68223311Sdougb	trap break SIGINT
69223311Sdougb
70223311Sdougb	for ip in ${netwait_ip}; do
71223311Sdougb		echo -n "Waiting for ${ip} to respond to ICMP"
72223311Sdougb
73223311Sdougb		count=1
74223311Sdougb		while [ ${count} -le ${netwait_timeout} ]; do
75223311Sdougb			/sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1
76223311Sdougb			rc=$?
77223311Sdougb
78223311Sdougb			if [ $rc -eq 0 ]; then
79223311Sdougb				# Restore default SIGINT handler
80223311Sdougb				trap - SIGINT
81223311Sdougb
82223311Sdougb				echo '.'
83223311Sdougb				return
84223311Sdougb			fi
85223311Sdougb			count=$((count+1))
86223311Sdougb		done
87223311Sdougb		echo ': No response from host.'
88223311Sdougb	done
89223311Sdougb
90223311Sdougb	# Restore default SIGINT handler
91223311Sdougb	trap - SIGINT
92223311Sdougb
93223311Sdougb	warn "Exhausted IP list.  Continuing with startup, but be aware you may"
94223311Sdougb	warn "not have a fully functional networking layer at this point."
95223311Sdougb}
96223311Sdougb
97223311Sdougbload_rc_config $name
98223311Sdougbrun_rc_command "$1"
99