openrc/init.d/net-online.in

77 lines
2.0 KiB
Plaintext
Raw Normal View History

2015-10-20 04:36:55 +05:30
#!@SBINDIR@/openrc-run
# Copyright (c) 2015 The OpenRC Authors.
# See the Authors file at the top-level directory of this distribution and
# https://github.com/OpenRC/openrc/blob/master/AUTHORS
#
# This file is part of OpenRC. It is subject to the license terms in
# the LICENSE file found in the top-level directory of this
# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
# This file may not be copied, modified, propagated, or distributed
# except according to the terms contained in the LICENSE file.
2015-10-20 04:36:55 +05:30
description="Delays until the network is online or a specific timeout"
depend()
{
after modules
need sysfs
keyword -docker -jail -lxc -openvz -prefix -systemd-nspawn -uml -vserver
2015-10-20 04:36:55 +05:30
}
get_interfaces()
{
local ifname iftype
for ifname in /sys/class/net/*; do
read iftype < ${ifname}/type
[ "$iftype" = "1" ] && printf "%s " ${ifname##*/}
done
}
get_default_gateway()
{
local cmd gateway
if command -v ip > /dev/null 2>&1; then
cmd="ip route show"
else
cmd=route
fi
set -- $($cmd | grep default)
[ "$2" != via ] && gateway="$2" || gateway="$3"
printf "%s" $gateway
}
start ()
{
local carriers configured dev gateway ifcount infinite
local rc state x
2015-10-20 04:36:55 +05:30
ebegin "Checking to see if the network is online"
rc=0
interfaces=${interfaces:-$(get_interfaces)}
timeout=${timeout:-120}
[ $timeout -eq 0 ] && infinite=true || infinite=false
while $infinite || [ $timeout -gt 0 ]; do
carriers=0
configured=0
ifcount=0
for dev in ${interfaces}; do
: $((ifcount += 1))
read x < /sys/class/net/$dev/carrier
[ $x -eq 1 ] && : $((carriers += 1))
read x < /sys/class/net/$dev/operstate
[ "$x" = up ] && : $((configured += 1))
done
[ $configured -eq $ifcount ] && [ $carriers -ge 1 ] && break
sleep 1
: $((timeout -= 1))
done
! $infinite && [ $timeout -eq 0 ] && rc=1
if [ $rc -eq 0 ] && yesno ${ping_default_gateway:-no}; then
gateway="$(get_default_gateway)"
if [ -n "$gateway" ] && ! ping -c 1 $gateway > /dev/null 2>&1; then
rc=1
fi
fi
eend $rc "The network is offline"
}