summary refs log tree commit diff
path: root/os/etc/s6-rc/src/net-online/shell-up
diff options
context:
space:
mode:
Diffstat (limited to 'os/etc/s6-rc/src/net-online/shell-up')
-rwxr-xr-xos/etc/s6-rc/src/net-online/shell-up62
1 files changed, 62 insertions, 0 deletions
diff --git a/os/etc/s6-rc/src/net-online/shell-up b/os/etc/s6-rc/src/net-online/shell-up
new file mode 100755
index 0000000..b9fa779
--- /dev/null
+++ b/os/etc/s6-rc/src/net-online/shell-up
@@ -0,0 +1,62 @@
+#!/bin/sh
+set -eu
+
+# check if any interface is plugged in and configured
+# then do a ping test to see if we have connectivity
+
+include_ping_test=1
+ping_test_host=gentoo.org
+infinite=false
+timeout=10
+
+get_interfaces()
+{
+	local ifname iftype
+	for ifname in /sys/class/net/*; do
+		[ -h "${ifname}" ] || continue
+		read iftype < ${ifname}/type
+		[ "${iftype}" = "1" ] && printf "%s " ${ifname##*/}
+	done
+	return 0
+}
+
+check_interfaces()
+{
+	local ifcount=0
+	local carriers=0 # plugged in
+	local configured=0
+	for dev in $(get_interfaces); do
+		: $((ifcount += 1))
+		read carrier < /sys/class/net/$dev/carrier 2>/dev/null || carrier=""
+		[ "${carrier}" = 1 ] && : $((carriers += 1))
+		read operstate < /sys/class/net/$dev/operstate 2>/dev/null || operstate=""
+		[ "${operstate}" = up ] && : $((configured += 1))
+	done
+	#[ $configured -eq $ifcount ] && [ $carriers -ge 1 ] && return 0
+	[ $configured -ge 1 ] && [ $carriers -ge 1 ] && return 0
+	return 1
+}
+
+check_online()
+{
+	local timeout=${timeout:-120}
+	while $infinite || [ $timeout -gt 0 ] ; do
+		check_interfaces && break
+		sleep 1
+		: $((timeout -= 1))
+	done
+	#echo 'plugged in'
+
+	if [ "${include_ping_test}" = 1 ] && [ -n "${ping_test_host}" ]; then
+		local timeout=${timeout:-120}
+		while $infinite || [ $timeout -gt 0 ] ; do
+			ping -c 1 $ping_test_host >/dev/null 2>&1
+			[ $? -eq 0 ] && break
+			sleep 1
+			: $((timeout -= 1))
+		done
+	fi
+	#echo 'connected'
+}
+
+check_online