xref: /freebsd/libexec/rc/rc.d/netwait (revision 51da4b19be15e6c1862e40565d7a3b310c566679)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb
30696600cSBjoern A. Zeeb# $FreeBSD$
40696600cSBjoern A. Zeeb#
50696600cSBjoern A. Zeeb# PROVIDE: netwait
6*51da4b19SCy Schubert# REQUIRE: devd ipfw pf routing
70696600cSBjoern A. Zeeb# KEYWORD: nojail
80696600cSBjoern A. Zeeb#
90696600cSBjoern A. Zeeb# The netwait script helps handle two situations:
100696600cSBjoern A. Zeeb#  - Systems with USB or other late-attaching network hardware which
110696600cSBjoern A. Zeeb#    is initialized by devd events.  The script waits for all the
120696600cSBjoern A. Zeeb#    interfaces named in the netwait_if list to appear.
130696600cSBjoern A. Zeeb#  - Systems with statically-configured IP addresses in rc.conf(5).
140696600cSBjoern A. Zeeb#    The IP addresses in the netwait_ip list are pinged.  The script
150696600cSBjoern A. Zeeb#    waits for any single IP in the list to respond to the ping.  If your
160696600cSBjoern A. Zeeb#    system uses DHCP, you should probably use synchronous_dhclient="YES"
170696600cSBjoern A. Zeeb#    in your /etc/rc.conf instead of netwait_ip.
180696600cSBjoern A. Zeeb# Either or both of the wait lists can be used (at least one must be
190696600cSBjoern A. Zeeb# non-empty if netwait is enabled).
200696600cSBjoern A. Zeeb
210696600cSBjoern A. Zeeb. /etc/rc.subr
220696600cSBjoern A. Zeeb
230696600cSBjoern A. Zeebname="netwait"
240696600cSBjoern A. Zeebdesc="Wait for network devices or the network being up"
250696600cSBjoern A. Zeebrcvar="netwait_enable"
260696600cSBjoern A. Zeeb
270696600cSBjoern A. Zeebstart_cmd="${name}_start"
280696600cSBjoern A. Zeebstop_cmd=":"
290696600cSBjoern A. Zeeb
300696600cSBjoern A. Zeebnetwait_start()
310696600cSBjoern A. Zeeb{
320696600cSBjoern A. Zeeb	local ip rc count output link wait_if got_if any_error
330696600cSBjoern A. Zeeb
340696600cSBjoern A. Zeeb	if [ -z "${netwait_if}" ] && [ -z "${netwait_ip}" ]; then
350696600cSBjoern A. Zeeb		err 1 "No interface or IP addresses listed, nothing to wait for"
360696600cSBjoern A. Zeeb	fi
370696600cSBjoern A. Zeeb
380696600cSBjoern A. Zeeb	if [ ${netwait_timeout} -lt 1 ]; then
390696600cSBjoern A. Zeeb		err 1 "netwait_timeout must be >= 1"
400696600cSBjoern A. Zeeb	fi
410696600cSBjoern A. Zeeb
420696600cSBjoern A. Zeeb	if [ -n "${netwait_if}" ]; then
430696600cSBjoern A. Zeeb		any_error=0
440696600cSBjoern A. Zeeb		for wait_if in ${netwait_if}; do
450696600cSBjoern A. Zeeb			echo -n "Waiting for ${wait_if}"
460696600cSBjoern A. Zeeb			link=""
470696600cSBjoern A. Zeeb			got_if=0
480696600cSBjoern A. Zeeb			count=1
490696600cSBjoern A. Zeeb			# Handle SIGINT (Ctrl-C); force abort of while() loop
500696600cSBjoern A. Zeeb			trap break SIGINT
510696600cSBjoern A. Zeeb			while [ ${count} -le ${netwait_if_timeout} ]; do
520696600cSBjoern A. Zeeb				if output=`/sbin/ifconfig ${wait_if} 2>/dev/null`; then
530696600cSBjoern A. Zeeb					if [ ${got_if} -eq 0 ]; then
540696600cSBjoern A. Zeeb						echo -n ", interface present"
550696600cSBjoern A. Zeeb						got_if=1
560696600cSBjoern A. Zeeb					fi
570696600cSBjoern A. Zeeb					link=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
580696600cSBjoern A. Zeeb					if [ -z "${link}" ]; then
590696600cSBjoern A. Zeeb						echo ', got link.'
600696600cSBjoern A. Zeeb						break
610696600cSBjoern A. Zeeb					fi
620696600cSBjoern A. Zeeb				fi
630696600cSBjoern A. Zeeb				sleep 1
640696600cSBjoern A. Zeeb				count=$((count+1))
650696600cSBjoern A. Zeeb			done
660696600cSBjoern A. Zeeb			# Restore default SIGINT handler
670696600cSBjoern A. Zeeb			trap - SIGINT
680696600cSBjoern A. Zeeb			if [ ${got_if} -eq 0 ]; then
690696600cSBjoern A. Zeeb				echo ", wait failed: interface never appeared."
700696600cSBjoern A. Zeeb				any_error=1
710696600cSBjoern A. Zeeb			elif [ -n "${link}" ]; then
720696600cSBjoern A. Zeeb				echo ", wait failed: interface still has no link."
730696600cSBjoern A. Zeeb				any_error=1
740696600cSBjoern A. Zeeb			fi
750696600cSBjoern A. Zeeb		done
760696600cSBjoern A. Zeeb		if [ ${any_error} -eq 1 ]; then
770696600cSBjoern A. Zeeb		    warn "Continuing with startup, but be aware you may not have "
780696600cSBjoern A. Zeeb		    warn "a fully functional networking layer at this point."
790696600cSBjoern A. Zeeb		fi
800696600cSBjoern A. Zeeb	fi
810696600cSBjoern A. Zeeb
820696600cSBjoern A. Zeeb	if [ -n "${netwait_ip}" ]; then
830696600cSBjoern A. Zeeb		# Handle SIGINT (Ctrl-C); force abort of for() loop
840696600cSBjoern A. Zeeb		trap break SIGINT
850696600cSBjoern A. Zeeb
860696600cSBjoern A. Zeeb		for ip in ${netwait_ip}; do
870696600cSBjoern A. Zeeb			echo -n "Waiting for ${ip} to respond to ICMP ping"
880696600cSBjoern A. Zeeb
890696600cSBjoern A. Zeeb			count=1
900696600cSBjoern A. Zeeb			while [ ${count} -le ${netwait_timeout} ]; do
910696600cSBjoern A. Zeeb				/sbin/ping -t 1 -c 1 -o ${ip} >/dev/null 2>&1
920696600cSBjoern A. Zeeb				rc=$?
930696600cSBjoern A. Zeeb
940696600cSBjoern A. Zeeb				if [ $rc -eq 0 ]; then
950696600cSBjoern A. Zeeb					# Restore default SIGINT handler
960696600cSBjoern A. Zeeb					trap - SIGINT
970696600cSBjoern A. Zeeb
980696600cSBjoern A. Zeeb					echo ', got response.'
990696600cSBjoern A. Zeeb					return
1000696600cSBjoern A. Zeeb				fi
1010696600cSBjoern A. Zeeb				count=$((count+1))
1020696600cSBjoern A. Zeeb			done
1030696600cSBjoern A. Zeeb			echo ', failed: No response from host.'
1040696600cSBjoern A. Zeeb		done
1050696600cSBjoern A. Zeeb
1060696600cSBjoern A. Zeeb		# Restore default SIGINT handler
1070696600cSBjoern A. Zeeb		trap - SIGINT
1080696600cSBjoern A. Zeeb
1090696600cSBjoern A. Zeeb		warn "Exhausted IP list.  Continuing with startup, but be aware you may"
1100696600cSBjoern A. Zeeb		warn "not have a fully functional networking layer at this point."
1110696600cSBjoern A. Zeeb	fi
1120696600cSBjoern A. Zeeb
1130696600cSBjoern A. Zeeb}
1140696600cSBjoern A. Zeeb
1150696600cSBjoern A. Zeebload_rc_config $name
1160696600cSBjoern A. Zeebrun_rc_command "$1"
117