Changeset View
Changeset View
Standalone View
Standalone View
etc/rc.d/netwait
#!/bin/sh | #!/bin/sh | ||||
# $FreeBSD$ | # $FreeBSD$ | ||||
# | # | ||||
# PROVIDE: netwait | # PROVIDE: netwait | ||||
# REQUIRE: NETWORKING | # REQUIRE: devd routing | ||||
# KEYWORD: nojail | # KEYWORD: nojail | ||||
# | # | ||||
# The netwait script is intended to be used by systems which have | # The netwait script helps handle two situations: | ||||
# statically-configured IP addresses in rc.conf(5). If your system | # - Systems with USB or other late-attaching network hardware which | ||||
# uses DHCP, you should use synchronous_dhclient="YES" in your | # is initialized by devd events. The script waits for all the | ||||
# /etc/rc.conf instead of using netwait. | # 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). | |||||
. /etc/rc.subr | . /etc/rc.subr | ||||
name="netwait" | name="netwait" | ||||
rcvar="netwait_enable" | rcvar="netwait_enable" | ||||
start_cmd="${name}_start" | start_cmd="${name}_start" | ||||
stop_cmd=":" | stop_cmd=":" | ||||
netwait_start() | netwait_start() | ||||
{ | { | ||||
local ip rc count output link | local ip rc count output link wait_if got_if any_error | ||||
if [ -z "${netwait_ip}" ]; then | if [ -z "${netwait_if}" ] && [ -z "${netwait_ip}" ]; then | ||||
err 1 "You must define one or more IP addresses in netwait_ip" | err 1 "No interface or IP addresses listed, nothing to wait for" | ||||
fi | fi | ||||
if [ ${netwait_timeout} -lt 1 ]; then | if [ ${netwait_timeout} -lt 1 ]; then | ||||
err 1 "netwait_timeout must be >= 1" | err 1 "netwait_timeout must be >= 1" | ||||
fi | fi | ||||
# Handle SIGINT (Ctrl-C); force abort of while() loop | |||||
trap break SIGINT | |||||
if [ -n "${netwait_if}" ]; then | if [ -n "${netwait_if}" ]; then | ||||
echo -n "Waiting for $netwait_if to have link" | any_error=0 | ||||
for wait_if in ${netwait_if}; do | |||||
echo -n "Waiting for ${wait_if}" | |||||
link="" | |||||
got_if=0 | |||||
count=1 | count=1 | ||||
# Handle SIGINT (Ctrl-C); force abort of while() loop | |||||
trap break SIGINT | |||||
while [ ${count} -le ${netwait_if_timeout} ]; do | while [ ${count} -le ${netwait_if_timeout} ]; do | ||||
if output=`/sbin/ifconfig ${netwait_if} 2>/dev/null`; then | 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\)'` | link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'` | ||||
if [ -z "${link}" ]; then | if [ -z "${link}" ]; then | ||||
echo '.' | echo ', got link.' | ||||
break | break | ||||
fi | fi | ||||
else | |||||
echo '' | |||||
err 1 "ifconfig ${netwait_if} failed" | |||||
fi | fi | ||||
sleep 1 | sleep 1 | ||||
count=$((count+1)) | count=$((count+1)) | ||||
done | done | ||||
if [ -n "${link}" ]; then | |||||
# Restore default SIGINT handler | # Restore default SIGINT handler | ||||
trap - SIGINT | trap - SIGINT | ||||
if [ ${got_if} -eq 0 ]; then | |||||
echo '' | echo ", wait failed: interface never appeared." | ||||
warn "Interface still has no link. Continuing with startup, but" | any_error=1 | ||||
warn "be aware you may not have a fully functional networking" | elif [ -n "${link}" ]; then | ||||
warn "layer at this point." | echo ", wait failed: interface still has no link." | ||||
return | any_error=1 | ||||
fi | 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." | |||||
fi | fi | ||||
fi | |||||
# Handle SIGINT (Ctrl-C); force abort of while() loop | if [ -n "${netwait_ip}" ]; then | ||||
# Handle SIGINT (Ctrl-C); force abort of for() loop | |||||
trap break SIGINT | trap break SIGINT | ||||
for ip in ${netwait_ip}; do | for ip in ${netwait_ip}; do | ||||
echo -n "Waiting for ${ip} to respond to ICMP" | echo -n "Waiting for ${ip} to respond to ICMP ping" | ||||
count=1 | count=1 | ||||
while [ ${count} -le ${netwait_timeout} ]; do | while [ ${count} -le ${netwait_timeout} ]; do | ||||
/sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1 | /sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1 | ||||
rc=$? | rc=$? | ||||
if [ $rc -eq 0 ]; then | if [ $rc -eq 0 ]; then | ||||
# Restore default SIGINT handler | # Restore default SIGINT handler | ||||
trap - SIGINT | trap - SIGINT | ||||
echo '.' | echo ', got response.' | ||||
return | return | ||||
fi | fi | ||||
count=$((count+1)) | count=$((count+1)) | ||||
done | done | ||||
echo ': No response from host.' | echo ', failed: No response from host.' | ||||
done | done | ||||
# Restore default SIGINT handler | # Restore default SIGINT handler | ||||
trap - SIGINT | trap - SIGINT | ||||
warn "Exhausted IP list. Continuing with startup, but be aware you may" | warn "Exhausted IP list. Continuing with startup, but be aware you may" | ||||
warn "not have a fully functional networking layer at this point." | warn "not have a fully functional networking layer at this point." | ||||
fi | |||||
} | } | ||||
load_rc_config $name | load_rc_config $name | ||||
run_rc_command "$1" | run_rc_command "$1" |