aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--conf.d/Makefile5
-rw-r--r--conf.d/Makefile.FreeBSD3
-rw-r--r--conf.d/Makefile.Linux4
-rw-r--r--conf.d/Makefile.NetBSD3
-rw-r--r--conf.d/network.in13
-rw-r--r--init.d/network.in80
6 files changed, 73 insertions, 35 deletions
diff --git a/conf.d/Makefile b/conf.d/Makefile
index 9d0fc0d0..a884b46b 100644
--- a/conf.d/Makefile
+++ b/conf.d/Makefile
@@ -1,9 +1,14 @@
DIR= ${CONFDIR}
CONF= bootmisc fsck hostname local localmount network urandom
+TARGETS+= network
CLEANFILES+= network
MK= ../mk
include ${MK}/os.mk
include Makefile.${OS}
include ${MK}/scripts.mk
+
+network: network.in network.${OS}
+ cp network.in network
+ [ -e network.${OS} ] && cat network.${OS} >> network
diff --git a/conf.d/Makefile.FreeBSD b/conf.d/Makefile.FreeBSD
index fb531e2f..c43a1d4f 100644
--- a/conf.d/Makefile.FreeBSD
+++ b/conf.d/Makefile.FreeBSD
@@ -1,4 +1 @@
CONF+= ipfw moused powerd rarpd savecore syscons
-
-network:
- cp network.in network
diff --git a/conf.d/Makefile.Linux b/conf.d/Makefile.Linux
index 9e1ce9d8..74679400 100644
--- a/conf.d/Makefile.Linux
+++ b/conf.d/Makefile.Linux
@@ -1,5 +1 @@
CONF+= consolefont dmesg hwclock keymaps modules
-
-network:
- cp network.in network
- cat network.Linux >> network
diff --git a/conf.d/Makefile.NetBSD b/conf.d/Makefile.NetBSD
index fd0be1ec..18a52cce 100644
--- a/conf.d/Makefile.NetBSD
+++ b/conf.d/Makefile.NetBSD
@@ -1,4 +1 @@
CONF+= moused rarpd savecore
-
-network:
- cp network.in network
diff --git a/conf.d/network.in b/conf.d/network.in
index 2bb2b071..469494d6 100644
--- a/conf.d/network.in
+++ b/conf.d/network.in
@@ -1,10 +1,17 @@
-# Assign static IP addresses and run custom scripts per interface
+# Assign static IP addresses and run custom scripts per interface.
# Seperate commands with ;
-# Prefix with ! to run a shell script
+# Prefix with ! to run a shell script.
# ifconfig_eth0="up; 192.168.0.10 netmask 255.255.255.0; ! echo up"
# You also have ifup_eth0 and ifdown_eth0 to run other commands when
# eth0 is started and stopped.
# Lastly, the interfaces variable pulls in virtual interfaces that cannot
-# be automatically detected.
+# be automatically detected.
+
+# You can also use files instead of variables here if you like:
+# /etc/ifconfig.eth0 is equivalent to ifconfig_eth0
+# /etc/ip.eth0 is equivalent to ifconfig_eth0
+# /etc/ifup.eth0 is equivalent to ifup_eth0
+# /etc/ifdown.eth0 is equivalent to ifdown_eth0
+# Any files found will automatically be put into the interfaces variable.
diff --git a/init.d/network.in b/init.d/network.in
index 9b3fc285..ba458186 100644
--- a/init.d/network.in
+++ b/init.d/network.in
@@ -2,6 +2,8 @@
# Copyright 2009 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
+# This script was inspired by the equivalent rc.d network from NetBSD.
+
description="Configures network interfaces."
__nl="
"
@@ -14,7 +16,28 @@ depend()
keyword nojail noprefix novserver
}
-interfaces()
+uniqify()
+{
+ local result= i=
+ for i; do
+ case " $result " in
+ *" $i "*);;
+ *) result="$result $i";;
+ esac
+ done
+ echo "${result# *}"
+}
+
+reverse()
+{
+ local result= i=
+ for i; do
+ result="$i $result"
+ done
+ echo "${result# *}"
+}
+
+sys_interfaces()
{
case "${RC_UNAME}" in
Linux)
@@ -34,17 +57,33 @@ interfaces()
esac
}
-uniqify()
+auto_interfaces()
{
- local result=
- while [ -n "$1" ]; do
- case " ${result} " in
- *" $1 "*);;
- *) result="${result} $1";;
- esac
- shift
- done
- echo "${result# *}"
+ local ifs= c= f=
+
+ case "${RC_UNAME}" in
+ NetBSD)
+ for c in $(ifconfig -C 2>/dev/null); do
+ for f in /etc/ifconfig.${c}[0-9]*; do
+ [ -f "$f" ] && printf "%s" "$f{##*.} "
+ done
+ done
+ ;;
+ *)
+ for f in /etc/ifconfig.*; do
+ [ -f "$f" ] && printf "%s" "${f##*.} "
+ done
+ for f in /etc/ip.*; do
+ [ -f "$f" ] && printf "%s" "${f##*.} "
+ done
+ ;;
+ esac
+ echo
+}
+
+interfaces()
+{
+ uniqify $(sys_interfaces "$@") $interfaces $(auto_interfaces)
}
dumpargs()
@@ -53,7 +92,7 @@ dumpargs()
shift
case "$@" in
- '') cat "$f";;
+ '') [ -f "$f" ] && cat "$f";;
*"$__nl"*) echo "$@";;
*)
(
@@ -125,7 +164,7 @@ runargs()
start()
{
- local cr=0 r= iface= cnf= cmd= args= upcmd=
+ local cr=0 r= iface= cmd= args= upcmd=
einfo "Starting network"
routeflush
if [ "${RC_UNAME}" = "Linux" ]; then
@@ -140,7 +179,7 @@ start()
127.0.0.1 -reject || cr=1
fi
eindent
- for iface in $(uniqify $(interfaces) $interfaces); do
+ for iface in $(interfaces); do
local func= cf=
eval upcmd=\$ifup_$iface
for func in ip ifconfig; do
@@ -157,9 +196,7 @@ start()
ip) func=runip;;
esac
eindent
- if [ -n "$upcmd" -o -f /etc/ifup."$iface" ]; then
- runargs /etc/ifup."$iface" "$upcmd"
- fi
+ runargs /etc/ifup."$iface" "$upcmd"
r=0
dumpargs "$cf" "$cmd" | while read -r args; do
case "$args" in
@@ -201,11 +238,11 @@ start()
stop()
{
- local cr=0 r= iface= cnf= cmd= args= downcmd=
+ local iface= cmd= downcmd=
einfo "Stopping network"
routeflush
eindent
- for iface in $(uniqify $(interfaces u) $interfaces); do
+ for iface in $(reverse $(interfaces u)); do
eval downcmd=\$ifdown_$iface
eval cmd=\$ip_$iface
[ -z "$cmd" ] && eval cmd=\$ifconfig_$iface
@@ -214,10 +251,9 @@ stop()
-n "$downcmd" -o -f /etc/ifdown."$iface" ];
then
vebegin "$iface"
- if [ -n "$downcmd" -o -f /etc/ifdown."$iface" ]; then
- runargs /etc/ifdown."$iface" "$downcmd"
- fi
+ runargs /etc/ifdown."$iface" "$downcmd"
ifconfig "$iface" down 2>/dev/null
+ ifconfig "$iface" destroy 2>/dev/null
veend $?
fi
done