rc(8) script -- waiting for the network to become usable

Doug Barton dougb at FreeBSD.org
Thu Jun 3 06:30:44 UTC 2010


On 06/02/10 21:45, Jeremy Chadwick wrote:
>
> There have been quite a few changes to the script over the past few
> months, mainly due to a user with a complex setup (multiple NICs, use of
> vlan(4), and some other stuff).

Sounds like you've been making good progress. :)

> At present the script is intended to be dropped into /usr/local/etc/rc.d
> and used.  It's also been renamed, and many of the variables adjusted or
> changed in functionality.  If you could give this a try and let me know
> if it works for you, I'd appreciate it.  I tested it on a series of
> systems we have here as well and it works OK.
>
> http://jdc.parodius.com/freebsd/netwait

Overall looks good, I've attached a patch with a few tweaks, mostly 
style. See 
http://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/rc-scripts.html 
for more information.

The default empty variable assignments are not desirable.

If the script is enabled (which is the only way start_cmd will run) then 
netwait_ip being empty is a fatal error.

I think the 'trap break' trick will work, give it a try. :)


hth,

Doug

-- 

	... and that's just a little bit of history repeating.
			-- Propellerheads

	Improve the effectiveness of your Internet presence with
	a domain name makeover!    http://SupersetSolutions.com/

-------------- next part --------------
--- netwait.orig	2010-06-02 22:42:39.000000000 -0700
+++ netwait	2010-06-02 23:16:00.000000000 -0700
@@ -17,31 +17,33 @@
 
 # Add the following lines to /etc/rc.conf to enable netwait:
 #
-# netwait_enable:        Set to "NO" by default.
+# netwait_enable (bool): Set to "NO" by default.
 #                        Set it to "YES" to enable netwait.
-# netwait_ip:            Set to "" by default; MUST BE SET BY USER.
+# netwait_ip (str):      Set to "" by default; MUST BE SET BY USER.
 #                        IP address/destination to be ping'd.
-# netwait_timeout:       Set to "60" by default.
+# netwait_timeout (int): Set to "60" by default.
 #                        Total number of seconds to perform pings,
 #                        at a rate of one ping per second.  If any
 #                        of these are successful (exit code 0), the
 #                        network is considered usable.  The 1 second
 #                        delay between pings is indirectly controlled
 #                        using the ping -t flag.
-# netwait_iface:         Set to "" by default.
+# netwait_iface (str):   Set to "" by default.
 #                        The interface name to watch link state on
 #                        before attempting pings.  Uses ifconfig(8)
 #                        to monitor link status.
-# netwait_iface_timeout: Set to "30" by default.
+# netwait_iface_timeout (int): Set to "30" by default.
 #                        Total number of seconds to wait for link
 #                        state to change from "status: no carrier"
 #                        to something else (presumably "active").
 #                        There is a 1 second delay between attempts.
 
+# Suggestion: s/iface/if/ since that's a pretty common abbreviation
+
 : ${netwait_enable:="NO"}
-: ${netwait_ip:=""}
+#: ${netwait_ip:=""}
 : ${netwait_timeout:="60"}
-: ${netwait_iface:=""}
+#: ${netwait_iface:=""}
 : ${netwait_iface_timeout:="30"}
 
 netwait_start()
@@ -49,20 +51,18 @@
 	local ip rc count output link
 
 	if [ -z "${netwait_ip}" ]; then
-		warn "You must define one or more IP addresses in netwait_ip"
-		return
+		err 1 "You must define one or more IP addresses in netwait_ip"
 	fi
 
-	if [ ${netwait_timeout} -le 0 ]; then
-		warn "netwait_timeout must be >= 1"
-		return
+	if [ ${netwait_timeout} -lt 1 ]; then
+		err 1 "netwait_timeout must be >= 1"
 	fi
 
 	# Handle SIGINT (Ctrl-C); force abort of while() loop
-	trap count=${netwait_iface_timeout} SIGINT
+	trap break SIGINT
 
-	if [ ! -z "${netwait_iface}" ]; then
-		echo "Waiting for interface ${netwait_iface} to have link..."
+	if [ -n "${netwait_iface}" ]; then
+		echo "Waiting for interface $netwait_iface to have link..."
 
 		count=1
 		while [ ${count} -le ${netwait_iface_timeout} ]; do
@@ -76,19 +76,19 @@
 			sleep 1
 			count=$((count+1))
 		done
-		if [ ! -z "${link}" ]; then
+		if [ -n "${link}" ]; then
 			# Restore default SIGINT handler
 			trap - SIGINT
 
-			echo "Interface still has no link.  Continuing with startup, but"
-			echo "be aware you may not have a fully functional networking"
-			echo "layer at this point."
+			warn "Interface still has no link.  Continuing with startup, but"
+			warn "be aware you may not have a fully functional networking"
+			warn "layer at this point."
 			return
 		fi
 	fi
 
 	# Handle SIGINT (Ctrl-C); force abort of while() loop
-	trap count=${netwait_timeout} SIGINT
+	trap break SIGINT
 
 	for ip in ${netwait_ip}; do
 		echo "Waiting for ${ip} to respond to ICMP..."
@@ -114,8 +114,8 @@
 	# Restore default SIGINT handler
 	trap - SIGINT
 
-	echo "Exhausted IP list.  Continuing with startup, but be aware you may"
-	echo "not have a fully functional networking layer at this point."
+	warn "Exhausted IP list.  Continuing with startup, but be aware you may"
+	warn "not have a fully functional networking layer at this point."
 }
 
 load_rc_config $name


More information about the freebsd-rc mailing list