2011-06-19 22:59:54 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# $FreeBSD$
|
|
|
|
#
|
|
|
|
# PROVIDE: netwait
|
2020-08-11 23:36:38 +00:00
|
|
|
# REQUIRE: devd ipfw pf routing
|
2011-06-19 22:59:54 +00:00
|
|
|
# KEYWORD: nojail
|
|
|
|
#
|
2015-12-26 18:21:32 +00:00
|
|
|
# The netwait script helps handle two situations:
|
|
|
|
# - Systems with USB or other late-attaching network hardware which
|
|
|
|
# is initialized by devd events. The script waits for all the
|
|
|
|
# interfaces named in the netwait_if list to appear.
|
|
|
|
# - Systems with statically-configured IP addresses in rc.conf(5).
|
|
|
|
# The IP addresses in the netwait_ip list are pinged. The script
|
|
|
|
# waits for any single IP in the list to respond to the ping. If your
|
|
|
|
# system uses DHCP, you should probably use synchronous_dhclient="YES"
|
|
|
|
# in your /etc/rc.conf instead of netwait_ip.
|
|
|
|
# Either or both of the wait lists can be used (at least one must be
|
|
|
|
# non-empty if netwait is enabled).
|
2011-06-19 22:59:54 +00:00
|
|
|
|
|
|
|
. /etc/rc.subr
|
|
|
|
|
|
|
|
name="netwait"
|
2016-04-23 16:10:54 +00:00
|
|
|
desc="Wait for network devices or the network being up"
|
2012-01-14 02:18:41 +00:00
|
|
|
rcvar="netwait_enable"
|
2011-06-22 06:27:32 +00:00
|
|
|
|
2011-06-19 22:59:54 +00:00
|
|
|
start_cmd="${name}_start"
|
|
|
|
stop_cmd=":"
|
|
|
|
|
|
|
|
netwait_start()
|
|
|
|
{
|
2015-12-26 18:21:32 +00:00
|
|
|
local ip rc count output link wait_if got_if any_error
|
2011-06-19 22:59:54 +00:00
|
|
|
|
2015-12-26 18:21:32 +00:00
|
|
|
if [ -z "${netwait_if}" ] && [ -z "${netwait_ip}" ]; then
|
|
|
|
err 1 "No interface or IP addresses listed, nothing to wait for"
|
2011-06-19 22:59:54 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ${netwait_timeout} -lt 1 ]; then
|
|
|
|
err 1 "netwait_timeout must be >= 1"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "${netwait_if}" ]; then
|
2015-12-26 18:21:32 +00:00
|
|
|
any_error=0
|
|
|
|
for wait_if in ${netwait_if}; do
|
|
|
|
echo -n "Waiting for ${wait_if}"
|
|
|
|
link=""
|
|
|
|
got_if=0
|
|
|
|
count=1
|
|
|
|
# Handle SIGINT (Ctrl-C); force abort of while() loop
|
|
|
|
trap break SIGINT
|
|
|
|
while [ ${count} -le ${netwait_if_timeout} ]; do
|
|
|
|
if output=`/sbin/ifconfig ${wait_if} 2>/dev/null`; then
|
|
|
|
if [ ${got_if} -eq 0 ]; then
|
|
|
|
echo -n ", interface present"
|
|
|
|
got_if=1
|
|
|
|
fi
|
|
|
|
link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
|
|
|
|
if [ -z "${link}" ]; then
|
|
|
|
echo ', got link.'
|
|
|
|
break
|
|
|
|
fi
|
2011-06-19 22:59:54 +00:00
|
|
|
fi
|
2015-12-26 18:21:32 +00:00
|
|
|
sleep 1
|
|
|
|
count=$((count+1))
|
|
|
|
done
|
2011-06-19 22:59:54 +00:00
|
|
|
# Restore default SIGINT handler
|
|
|
|
trap - SIGINT
|
2015-12-26 18:21:32 +00:00
|
|
|
if [ ${got_if} -eq 0 ]; then
|
|
|
|
echo ", wait failed: interface never appeared."
|
|
|
|
any_error=1
|
|
|
|
elif [ -n "${link}" ]; then
|
|
|
|
echo ", wait failed: interface still has no link."
|
|
|
|
any_error=1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ ${any_error} -eq 1 ]; then
|
|
|
|
warn "Continuing with startup, but be aware you may not have "
|
|
|
|
warn "a fully functional networking layer at this point."
|
2011-06-19 22:59:54 +00:00
|
|
|
fi
|
|
|
|
fi
|
2015-12-26 18:21:32 +00:00
|
|
|
|
|
|
|
if [ -n "${netwait_ip}" ]; then
|
|
|
|
# Handle SIGINT (Ctrl-C); force abort of for() loop
|
|
|
|
trap break SIGINT
|
2011-06-19 22:59:54 +00:00
|
|
|
|
2015-12-26 18:21:32 +00:00
|
|
|
for ip in ${netwait_ip}; do
|
|
|
|
echo -n "Waiting for ${ip} to respond to ICMP ping"
|
2011-06-19 22:59:54 +00:00
|
|
|
|
2015-12-26 18:21:32 +00:00
|
|
|
count=1
|
|
|
|
while [ ${count} -le ${netwait_timeout} ]; do
|
|
|
|
/sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1
|
|
|
|
rc=$?
|
2011-06-19 22:59:54 +00:00
|
|
|
|
2015-12-26 18:21:32 +00:00
|
|
|
if [ $rc -eq 0 ]; then
|
|
|
|
# Restore default SIGINT handler
|
|
|
|
trap - SIGINT
|
2011-06-19 22:59:54 +00:00
|
|
|
|
2015-12-26 18:21:32 +00:00
|
|
|
echo ', got response.'
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
count=$((count+1))
|
|
|
|
done
|
|
|
|
echo ', failed: No response from host.'
|
2011-06-19 22:59:54 +00:00
|
|
|
done
|
|
|
|
|
2015-12-26 18:21:32 +00:00
|
|
|
# Restore default SIGINT handler
|
|
|
|
trap - SIGINT
|
|
|
|
|
|
|
|
warn "Exhausted IP list. Continuing with startup, but be aware you may"
|
|
|
|
warn "not have a fully functional networking layer at this point."
|
|
|
|
fi
|
2011-06-19 22:59:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
load_rc_config $name
|
|
|
|
run_rc_command "$1"
|