Gentoo Archives: gentoo-commits

From: William Hubbs <williamh@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/openrc:master commit in: conf.d/, /, init.d/
Date: Thu, 29 Oct 2015 16:36:31
Message-Id: 1445361227.960881fcfe974178c0846a816663a258cdd488cc.williamh@OpenRC
1 commit: 960881fcfe974178c0846a816663a258cdd488cc
2 Author: William Hubbs <w.d.hubbs <AT> gmail <DOT> com>
3 AuthorDate: Mon Oct 19 23:06:55 2015 +0000
4 Commit: William Hubbs <williamh <AT> gentoo <DOT> org>
5 CommitDate: Tue Oct 20 17:13:47 2015 +0000
6 URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=960881fc
7
8 Add net-online service
9
10 NEWS.md | 12 +++++++++
11 conf.d/Makefile | 3 ++-
12 conf.d/net-online | 15 ++++++++++++
13 init.d/Makefile | 2 +-
14 init.d/net-online.in | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++
15 5 files changed, 99 insertions(+), 2 deletions(-)
16
17 diff --git a/NEWS.md b/NEWS.md
18 index d67ed60..71a1b7f 100644
19 --- a/NEWS.md
20 +++ b/NEWS.md
21 @@ -3,6 +3,18 @@
22 This file will contain a list of notable changes for each release. Note
23 the information in this file is in reverse order.
24
25 +## OpenRC-0.19
26 +
27 +This version adds a net-online service. By default, this
28 +service will check all known network interfaces for a configured
29 +interface or a carrier. It will register as started only when all
30 +interfaces are configured and there is at least a carrier on one
31 +interface. The behaviour of this service can be modified in
32 +/etc/conf.d/net-online.
33 +
34 +Currently, this only works on Linux, but if anyone wants to port to
35 +*bsd, that would be welcomed.
36 +
37 ## OpenRC-0.18.3
38
39 Modern Linux systems expect /etc/mtab to be a symbolic link to
40
41 diff --git a/conf.d/Makefile b/conf.d/Makefile
42 index 34a3b07..b8da0e0 100644
43 --- a/conf.d/Makefile
44 +++ b/conf.d/Makefile
45 @@ -15,7 +15,8 @@ include ${MK}/os.mk
46
47 CONF-FreeBSD= ipfw moused powerd rarpd savecore syscons
48
49 -CONF-Linux= consolefont devfs dmesg hwclock keymaps killprocs modules mtab
50 +CONF-Linux= consolefont devfs dmesg hwclock keymaps killprocs modules mtab \
51 + net-online
52
53 CONF-NetBSD= moused rarpd savecore
54
55
56 diff --git a/conf.d/net-online b/conf.d/net-online
57 new file mode 100644
58 index 0000000..bf0b09a
59 --- /dev/null
60 +++ b/conf.d/net-online
61 @@ -0,0 +1,15 @@
62 +# The interfaces setting controls which interfaces the net-online
63 +# service considers in deciding whether the network is active. By
64 +# default, it is all ethernet or wireless LAN interfaces.
65 +#interfaces=""
66 +
67 +# This setting controls whether a ping to the default gateway is
68 +# included in the test for network connectivity after all interfaces
69 +# are active.
70 +#ping_default_gateway=no
71 +
72 +# The timeout setting controls how long the net-online service waits
73 +# for the network to be configured.
74 +# The default is 120 seconds.
75 +# if this is set to 0, the wait is infinite.
76 +#timeout=120
77
78 diff --git a/init.d/Makefile b/init.d/Makefile
79 index a662f8d..f256095 100644
80 --- a/init.d/Makefile
81 +++ b/init.d/Makefile
82 @@ -23,7 +23,7 @@ SRCS-FreeBSD+= adjkerntz.in devd.in dumpon.in encswap.in ipfw.in \
83
84 SRCS-Linux= binfmt.in devfs.in dmesg.in hwclock.in consolefont.in keymaps.in \
85 killprocs.in modules.in mount-ro.in mtab.in numlock.in \
86 - procfs.in sysfs.in termencoding.in tmpfiles.dev.in
87 + procfs.in net-online.in sysfs.in termencoding.in tmpfiles.dev.in
88
89 # Generic BSD scripts
90 SRCS-NetBSD= hostid.in moused.in newsyslog.in pf.in rarpd.in rc-enabled.in \
91
92 diff --git a/init.d/net-online.in b/init.d/net-online.in
93 new file mode 100644
94 index 0000000..ab3da6f
95 --- /dev/null
96 +++ b/init.d/net-online.in
97 @@ -0,0 +1,69 @@
98 +#!@SBINDIR@/openrc-run
99 +# Copyright (C) 2015 William Hubbs <w.d.hubbs@×××××.com>
100 +# Released under the 2-clause BSD license.
101 +
102 +description="Delays until the network is online or a specific timeout"
103 +
104 +depend()
105 +{
106 + after modules
107 + need sysfs
108 + keyword -jail -lxc -openvz -prefix -systemd-nspawn -uml -vserver
109 +}
110 +
111 +get_interfaces()
112 +{
113 + local ifname iftype
114 + for ifname in /sys/class/net/*; do
115 + read iftype < ${ifname}/type
116 + [ "$iftype" = "1" ] && printf "%s " ${ifname##*/}
117 + done
118 +}
119 +
120 +get_default_gateway()
121 +{
122 + local cmd gateway
123 + if command -v ip > /dev/null 2>&1; then
124 + cmd="ip route show"
125 + else
126 + cmd=route
127 + fi
128 + set -- $($cmd | grep default)
129 + [ "$2" != via ] && gateway="$2" || gateway="$3"
130 + printf "%s" $gateway
131 +}
132 +
133 +start ()
134 +{
135 + local carriers configured dev gateway ifcount infinite interfaces
136 + local rc state timeout x
137 +
138 + ebegin "Checking to see if the network is online"
139 + rc=0
140 + interfaces=${interfaces:-$(get_interfaces)}
141 + timeout=${timeout:-120}
142 + [ $timeout -eq 0 ] && infinite=true || infinite=false
143 + while $infinite || [ $timeout -gt 0 ]; do
144 + carriers=0
145 + configured=0
146 + ifcount=0
147 + for dev in ${interfaces}; do
148 + : $((ifcount += 1))
149 + read x < /sys/class/net/$dev/carrier
150 + [ $x -eq 1 ] && : $((carriers += 1))
151 + read x < /sys/class/net/$dev/operstate
152 + [ "$x" = up ] && : $((configured += 1))
153 + done
154 + [ $configured -eq $ifcount ] && [ $carriers -ge 1 ] && break
155 + sleep 1
156 + : $((timeout -= 1))
157 + done
158 + ! $infinite && [ $timeout -eq 0 ] && rc=1
159 + if [ $rc -eq 0 ] && yesno ${ping_default_gateway:-no}; then
160 + gateway="$(get_default_gateway)"
161 + if [ -n "$gateway" ] && ! ping -c 1 $gateway > /dev/null 2>&1; then
162 + rc=1
163 + fi
164 + fi
165 + eend $rc "The network is offline"
166 +}