From 960881fcfe974178c0846a816663a258cdd488cc Mon Sep 17 00:00:00 2001 From: William Hubbs Date: Mon, 19 Oct 2015 18:06:55 -0500 Subject: [PATCH] Add net-online service --- NEWS.md | 12 ++++++++ conf.d/Makefile | 3 +- conf.d/net-online | 15 ++++++++++ init.d/Makefile | 2 +- init.d/net-online.in | 69 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 conf.d/net-online create mode 100644 init.d/net-online.in diff --git a/NEWS.md b/NEWS.md index d67ed601..71a1b7f1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,18 @@ This file will contain a list of notable changes for each release. Note the information in this file is in reverse order. +## OpenRC-0.19 + +This version adds a net-online service. By default, this +service will check all known network interfaces for a configured +interface or a carrier. It will register as started only when all +interfaces are configured and there is at least a carrier on one +interface. The behaviour of this service can be modified in +/etc/conf.d/net-online. + +Currently, this only works on Linux, but if anyone wants to port to +*bsd, that would be welcomed. + ## OpenRC-0.18.3 Modern Linux systems expect /etc/mtab to be a symbolic link to diff --git a/conf.d/Makefile b/conf.d/Makefile index 34a3b070..b8da0e04 100644 --- a/conf.d/Makefile +++ b/conf.d/Makefile @@ -15,7 +15,8 @@ include ${MK}/os.mk CONF-FreeBSD= ipfw moused powerd rarpd savecore syscons -CONF-Linux= consolefont devfs dmesg hwclock keymaps killprocs modules mtab +CONF-Linux= consolefont devfs dmesg hwclock keymaps killprocs modules mtab \ + net-online CONF-NetBSD= moused rarpd savecore diff --git a/conf.d/net-online b/conf.d/net-online new file mode 100644 index 00000000..bf0b09a2 --- /dev/null +++ b/conf.d/net-online @@ -0,0 +1,15 @@ +# The interfaces setting controls which interfaces the net-online +# service considers in deciding whether the network is active. By +# default, it is all ethernet or wireless LAN interfaces. +#interfaces="" + +# This setting controls whether a ping to the default gateway is +# included in the test for network connectivity after all interfaces +# are active. +#ping_default_gateway=no + +# The timeout setting controls how long the net-online service waits +# for the network to be configured. +# The default is 120 seconds. +# if this is set to 0, the wait is infinite. +#timeout=120 diff --git a/init.d/Makefile b/init.d/Makefile index a662f8d7..f2560956 100644 --- a/init.d/Makefile +++ b/init.d/Makefile @@ -23,7 +23,7 @@ SRCS-FreeBSD+= adjkerntz.in devd.in dumpon.in encswap.in ipfw.in \ SRCS-Linux= binfmt.in devfs.in dmesg.in hwclock.in consolefont.in keymaps.in \ killprocs.in modules.in mount-ro.in mtab.in numlock.in \ - procfs.in sysfs.in termencoding.in tmpfiles.dev.in + procfs.in net-online.in sysfs.in termencoding.in tmpfiles.dev.in # Generic BSD scripts SRCS-NetBSD= hostid.in moused.in newsyslog.in pf.in rarpd.in rc-enabled.in \ diff --git a/init.d/net-online.in b/init.d/net-online.in new file mode 100644 index 00000000..ab3da6fb --- /dev/null +++ b/init.d/net-online.in @@ -0,0 +1,69 @@ +#!@SBINDIR@/openrc-run +# Copyright (C) 2015 William Hubbs +# Released under the 2-clause BSD license. + +description="Delays until the network is online or a specific timeout" + +depend() +{ + after modules + need sysfs + keyword -jail -lxc -openvz -prefix -systemd-nspawn -uml -vserver +} + +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 interfaces + local rc state timeout x + + 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" +}