summary refs log tree commit diff
path: root/os/etc/s6-rc/src/net-online/shell-up
blob: b9fa77956acc45cc3f99d07abd58867d878d6d1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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