Rewrite the core parts in C. We now provide librc so other programs can

query runlevels, services and state without using bash. We also provide
libeinfo so other programs can easily use our informational functions.

As such, we have dropped the requirement of using bash as the init script
shell. We now use /bin/sh and have strived to make the scripts as portable
as possible. Shells that work are bash and dash. busybox works provided
you disable s-s-d. If you have WIPE_TMP set to yes in conf.d/bootmisc you
should disable find too.
zsh and ksh do not work at this time.

Networking support is currently being re-vamped also as it was heavily bash
array based. As such, a new config format is available like so
config_eth0="1.2.3.4/24 5.6.7.8/16"
or like so
config_eth0="'1.2.3.4 netmask 255.255.255.0' '5.6.7.8 netmask 255.255.0.0'"

We will still support the old bash array format provided that /bin/sh IS
a link it bash.

ChangeLog for baselayout-1 can be found in our SVN repo.
This commit is contained in:
Roy Marples
2007-04-05 11:18:42 +00:00
commit 5af58b4514
169 changed files with 20917 additions and 0 deletions

6
net/Makefile Normal file
View File

@@ -0,0 +1,6 @@
DIR = /$(LIB)/rcscripts/net
FILES = dhclient.sh dhcpcd.sh macchanger.sh macnet.sh ssidnet.sh system.sh \
wpa_supplicant.sh
TOPDIR = ..
include $(TOPDIR)/default.mk

75
net/dhclient.sh Normal file
View File

@@ -0,0 +1,75 @@
# Copyright 2004-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
dhclient_depend() {
after interface
program start /sbin/dhclient
provide dhcp
}
_config_vars="$_config_vars dhcp dhcpcd"
dhclient_start() {
local args= opt= opts= pidfile="/var/run/dhclient-${IFACE}.pid"
local sendhost=true dconf=
_wait_for_carrier || return 1
# Get our options
eval opts=\$dhcp_${IFVAR}
[ -z "${opts}" ] && opts=${dhcp}
# Map some generic options to dhcpcd
for opt in ${opts} ; do
case "${opt}" in
nodns) args="${args} -e PEER_DNS=no" ;;
nontp) args="${args} -e PEER_NTP=no" ;;
nogateway) args="${args} -e PEER_ROUTERS=no" ;;
nosendhost) sendhost=false ;;
esac
done
# Add our route metric
[ "${metric:-0}" != "0" ] && args="${args} -e IF_METRIC=${metric}"
if ${sendhost} ; then
local hname="$(hostname)"
if [ "${hname}" != "(none)" -a "${hname}" != "localhost" ]; then
dhconf="${dhconf} interface \"${iface}\" {"
dhconf="${dhconf} send host-name \"${hname}\";"
dhconf="${dhconf}}"
fi
fi
# Bring up DHCP for this interface
ebegin "Running dhclient"
echo "${dhconf}" | start-stop-daemon --start --exec /sbin/dhclient \
--pidfile "${pidfile}" -- ${opts} -q -1 -pf "${pidfile}" "${IFACE}"
eend $? || return 1
_show_address
return 0
}
dhclient_stop() {
local pidfile="/var/run/dhclient-${IFACE}.pid" opts=
[ ! -f "${pidfile}" ] && return 0
# Get our options
if [ -x /sbin/dhclient ] ; then
eval opts=\$dhcp_${IFVAR}
[ -z "${opts}" ] && opts=${dhcp}
fi
ebegin "Stopping dhclient on ${IFACE}"
case " ${opts} " in
*" release "*) dhclient -q -r -pf "${pidfile}" "${IFACE}" ;;
*)
start-stop-daemon --stop --quiet \
--exec /sbin/dhclient --pidfile "${pidfile}"
;;
esac
eend $?
}
# vim: set ts=4 :

71
net/dhcpcd.sh Normal file
View File

@@ -0,0 +1,71 @@
# Copyright 2004-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
dhcpcd_depend() {
after interface
program start /sbin/dhcpcd
provide dhcp
# We prefer dhcpcd over the others
after dhclient pump udhcpc
}
_config_vars="$_config_vars dhcp dhcpcd"
dhcpcd_start() {
local args= opt= opts= pidfile="/var/run/dhcpcd-${IFACE}.pid"
_wait_for_carrier || return 1
eval args=\$dhcpcd_${IFVAR}
# Get our options
eval opts=\$dhcp_${IFVAR}
[ -z "${opts}" ] && opts=${dhcp}
# Map some generic options to dhcpcd
for opt in ${opts} ; do
case "${opt}" in
nodns) args="${args} -R" ;;
nontp) args="${args} -N" ;;
nonis) args="${args} -Y" ;;
nogateway) args="${args} -G" ;;
nosendhost) args="${args} -h ''";
esac
done
# Add our route metric
[ "${metric:-0}" != "0" ] && args="${args} -m ${metric}"
# Bring up DHCP for this interface
ebegin "Running dhcpcd"
eval /sbin/dhcpcd "${args}" "${IFACE}"
eend $? || return 1
_show_address
return 0
}
dhcpcd_stop() {
local pidfile="/var/run/dhcpcd-${IFACE}.pid" opts=
[ ! -f "${pidfile}" ] && return 0
# Get our options
if [ -x /sbin/dhcpcd ] ; then
eval opts=\$dhcp_${IFVAR}
[ -z "${opts}" ] && opts=${dhcp}
fi
ebegin "Stopping dhcpcd on ${IFACE}"
case " ${opts} " in
*" release "*) dhcpcd -k "${IFACE}" ;;
*)
start-stop-daemon --stop --quiet \
--exec /sbin/dhcpcd --pidfile "${pidfile}"
;;
esac
eend $?
}
# vim: set ts=4 :

88
net/macchanger.sh Normal file
View File

@@ -0,0 +1,88 @@
# Copyright 2004-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
macchanger_depend() {
before macnet
}
_config_vars="$_config_vars mac"
macchanger_pre_start() {
# We don't change MAC addresses from background
${IN_BACKGROUND} && return 0
local mac= opts=
eval mac=\$mac_${IFVAR}
[ -z "${mac}" ] && return 0
_exists true || return 1
ebegin "Changing MAC address of ${IFACE}"
# The interface needs to be up for macchanger to work most of the time
_down
mac=$(echo "${mac}" | tr '[:upper:]' '[:lower:]')
case "${mac}" in
# specific mac-addr, i wish there were a shorter way to specify this
[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f])
# We don't need macchanger to change to a specific mac address
_set_mac_address "${mac}"
if eend "$?" ; then
mac=$(_get_mac_address)
eindent
einfo "changed to ${mac}"
eoutdent
return 0
fi
;;
# increment MAC address, default macchanger behavior
increment) opts="${opts}" ;;
# randomize just the ending bytes
random-ending) opts="${opts} -e" ;;
# keep the same kind of physical layer (eg fibre, copper)
random-samekind) opts="${opts} -a" ;;
# randomize to any known vendor of any physical layer type
random-anykind) opts="${opts} -A" ;;
# fully random bytes
random-full|random) opts="${opts} -r" ;;
# default case is just to pass on all the options
*) opts="${opts} ${mac}" ;;
esac
if [ ! -x /sbin/macchanger ] ; then
eerror "For changing MAC addresses, emerge net-analyzer/macchanger"
return 1
fi
mac=$(/sbin/macchanger ${opts} "${IFACE}" \
| sed -n -e 's/^Faked MAC:.*\<\(..:..:..:..:..:..\)\>.*/\U\1/p' )
_up
# Sometimes the interface needs to be up ....
if [ -z "${mac}" ] ; then
mac=$(/sbin/macchanger ${opts} "${IFACE}" \
| sed -n -e 's/^Faked MAC:.*\<\(..:..:..:..:..:..\)\>.*/\U\1/p' )
fi
if [ -z "${mac}" ] ; then
eend 1 "Failed to set MAC address"
return 1
fi
eend 0
eindent
einfo "changed to" "${mac}"
eoutdent
return 0
}
# vim: set ts=4 :

19
net/macnet.sh Normal file
View File

@@ -0,0 +1,19 @@
# Copyright 2005-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
macnet_depend() {
before rename interface wireless
after macchanger
}
macnet_pre_start() {
local mac=$(_get_mac_address 2>/dev/null)
[ -z "${mac}" ] && return 0
vebegin "Configuring ${IFACE} for MAC address ${mac}"
mac=$(echo "${mac}" | sed -e 's/://g')
_configure_variables "${mac}"
veend 0
}
# vim: set ts=4 :

24
net/ssidnet.sh Normal file
View File

@@ -0,0 +1,24 @@
# Copyright 2004-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
ssidnet_depend() {
before interface system
after wireless
}
ssidnet_pre_start() {
[ -z "${SSID}" -a -z "${SSIDVAR}" ] && return 0
local mac=$(_get_ap_mac_address | sed -e 's/://g') x=
vebegin "Configuring ${IFACE} for SSID ${SSID}"
_configure_variables "${mac}" "${SSIDVAR}"
# Backwards compat for old gateway var
eval x=\$gateway_${SSIDVAR}
[ -n "${x}" ] && gateway=${x}
veend 0
}
# vim: set ts=4 :

106
net/system.sh Normal file
View File

@@ -0,0 +1,106 @@
# Copyright 2005-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
_config_vars="$_config_vars dns_servers dns_domain dns_search"
_config_vars="$_config_vars ntp_servers nis_servers nis_domain"
system_depend() {
after interface
before dhcp
}
_system_dns() {
local servers= domain= search= x=
eval servers=\$dns_servers_${IFVAR}
[ -z "${servers}" ] && servers=${dns_servers}
eval domain=\$dns_domain_${IFVAR}
[ -z "${domain}" ] && domain=${dns_domain}
eval search=\$dns_search_${IFVAR}
[ -z "${search}" ] && search=${dns_search}
[ -z "${servers}" -a -z "${domain}" -a -z "${search}" ] && return 0
local buffer="# Generated by net-scripts for interface ${IFACE}\n"
[ -n "${domain}" ] && buffer="${buffer}domain ${domain}\n"
[ -n "${search}" ] && buffer="${buffer}search ${search}\n"
for x in ${servers} ; do
buffer="${buffer}nameserver ${x}\n"
done
# Support resolvconf if we have it.
if [ -x /sbin/resolvconf ] ; then
printf "${buffer}" | resolvconf -a "${IFACE}"
else
printf "${buffer}" > /etc/resolv.conf
chmod 644 /etc/resolv.conf
fi
}
_system_ntp() {
local servers= buffer= x=
eval servers=\$ntp_servers_${IFVAR}
[ -z ${servers} ] && servers=${ntp_servers}
[ -z ${servers} ] && return 0
buffer="# Generated by net-scripts for interface ${IFACE}\n"
buffer="${buffer}restrict default noquery notrust nomodify\n"
buffer="${buffer}restrict 127.0.0.1\n"
for x in ${servers} ; do
buffer="${buffer}restrict ${x} nomodify notrap noquery\n"
buffer="${buffer}server ${x}\n"
done
buffer="${buffer}driftfile /var/lib/ntp/ntp.drift\n"
buffer="${buffer}logfile /var/log/ntp.log\n"
printf "${buffer}" > /etc/ntp.conf
chmod 644 /etc/ntp.conf
}
_system_nis() {
local servers= domain= x= buffer=
eval servers=\$nis_servers_${IFVAR}
[ -z "${servers}" ] && servers=${nis_servers}
eval domain=\$nis_domain_${IFVAR}
[ -z "${domain}" ] && domain=${nis_domain}
[ -z "${servers}" -a -z "${domain}" ] && return 0
buffer="# Generated by net-scripts for interface ${iface}\n"
if [ -n "${domain}" ] ; then
hostname -y "${domain}"
if [ -n "${servers}" ] ; then
for x in ${servers} ; do
buffer="${buffer}domain ${domain} server ${x}\n"
done
else
buffer="${buffer}domain ${domain} broadcast\n"
fi
else
for x in ${servers} ; do
buffer="${buffer}ypserver ${x}\n"
done
fi
printf "${buffer}" > /etc/yp.conf
chmod 644 /etc/yp.conf
}
system_pre_start() {
_system_dns
_system_ntp
_system_nis
return 0
}
# vim: set ts=4 :

164
net/wpa_supplicant.sh Normal file
View File

@@ -0,0 +1,164 @@
# Copyright 2004-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
wpa_supplicant_depend() {
program start /sbin/wpa_supplicant
after macnet plug
before interface
provide wireless
# Prefer us over iwconfig
after iwconfig
}
# Only set these functions if not set already
# IE, prefer to use iwconfig
if ! type _get_ssid >/dev/null 2>/dev/null ; then
_get_ssid() {
local timeout=5 ssid=
while [ ${timeout} -gt 0 ] ;do
ssid=$(wpa_cli -i"${IFACE}" status | sed -n -e 's/^ssid=//p')
if [ -n "${ssid}" ] ; then
echo "${ssid}"
return 0
fi
sleep 1
timeout=$((timeout - 1))
done
return 1
}
_get_ap_mac_address() {
wpa_cli -i"${IFACE}" status | sed -n -e 's/^bssid=\(.*\)$/\1/p' \
| tr '[:lower:]' '[:upper:]'
}
fi
wpa_supplicant_pre_start() {
local opts= cfgfile= ctrl_dir=
_is_wireless || return 0
# We don't configure wireless if we're being called from
# the background unless we're not currently running
if [ "${IN_BACKGROUND}" = "true" ] ; then
if service_started_daemon "${SVCNAME}" /sbin/wpa_supplicant ; then
SSID=$(_get_ssid "${IFACE}")
SSIDVAR=$(_shell_var "${SSID}")
save_options "SSID" "${SSID}"
metric=2000
fi
return 0
fi
save_options "SSID" ""
eval opts=\$wpa_supplicant_${IFVAR}
ebegin "Starting wpa_supplicant on" "${IFVAR}"
if [ -x /sbin/iwconfig ] ; then
local x=
for x in txpower rate rts frag ; do
iwconfig "${IFACE}" "${x}" auto 2>/dev/null
done
fi
cfgfile=${opts##* -c}
if [ -n "${cfgfile}" -a "${cfgfile}" != "${opts}" ] ; then
case "${cfgfile}" in
" "*) cfgfile=${cfgfile# *} ;;
esac
cfgfile=${cfgfile%% *}
else
# Support new and old style locations
cfgfile="/etc/wpa_supplicant/wpa_supplicant-${IFACE}.conf"
[ ! -e "${cfgfile}" ] \
&& cfgfile="/etc/wpa_supplicant/wpa_supplicant.conf"
[ ! -e ${cfgfile} ] \
&& cfgfile="/etc/wpa_supplicant.conf"
opts="${opts} -c ${cfgfile}"
fi
if [ ! -f ${cfgfile} ] ; then
eend 1 "/etc/wpa_supplicant/wpa_supplicant.conf not found"
return 1
fi
# Work out where the ctrl_interface dir is if it's not specified
local ctrl_dir=$(sed -n -e 's/[ \t]*#.*//g;s/[ \t]*$//g;s/^ctrl_interface=//p' "${cfgfile}")
if [ -z "${ctrl_dir}" ] ; then
ctrl_dir=${opts##* -C}
if [ -n "${ctrl_dir}" -a "${ctrl_dir}" != "${opts}" ] ; then
case "${ctrl_dir}" in
" "*) ctrl_dir=${ctrl_dir# *} ;;
esac
ctrl_dir=${ctrl_dir%% *}
else
ctrl_dir="/var/run/wpa_supplicant"
opts="${opts} -C ${ctrl_dir}"
fi
fi
save_options ctrl_dir "${ctrl_dir}"
actfile="/etc/wpa_supplicant/wpa_cli.sh"
start-stop-daemon --start --exec /sbin/wpa_supplicant \
--pidfile "/var/run/wpa_supplicant-${IFACE}.pid" \
-- ${opts} -W -B -i "${IFACE}" \
-P "/var/run/wpa_supplicant-${IFACE}.pid"
eend $? || return 1
# Starting wpa_supplication-0.4.0, we can get wpa_cli to
# start/stop our scripts from wpa_supplicant messages
local inact=false
service_inactive "${SVCNAME}" && inact=true
mark_service_inactive "${SVCNAME}"
ebegin "Starting wpa_cli on" "${IFACE}"
start-stop-daemon --start --exec /bin/wpa_cli \
--pidfile "/var/run/wpa_cli-${IFACE}.pid" \
-- -a /etc/wpa_supplicant/wpa_cli.sh -p "${ctrl_dir}" -i "${IFACE}" \
-P "/var/run/wpa_cli-${IFACE}.pid" -B
if eend $? ; then
ebegin "Backgrounding ..."
exit 1
fi
# wpa_cli failed to start? OK, error here
start-stop-daemon --quiet --stop --exec /sbin/wpa_supplicant \
--pidfile "/var/run/wpa_supplicant-${IFACE}.pid"
${inact} || mark_service_stopped "${SVCNAME}"
return 1
}
wpa_supplicant_post_stop() {
if [ "${IN_BACKGROUND}" = "true" ] ; then
# Only stop wpa_supplicant if it's not the controlling daemon
! service_started_daemon "${SVCNAME}" /sbin/wpa_supplicant 1
fi
[ $? != 0 ] && return 0
local pidfile="/var/run/wpa_cli-${IFACE}.pid"
if [ -f ${pidfile} ] ; then
ebegin "Stopping wpa_cli on ${IFACE}"
start-stop-daemon --stop --exec /bin/wpa_cli \
--pidfile "${pidfile}"
eend $?
fi
pidfile="/var/run/wpa_supplicant-${IFACE}.pid"
if [ -f ${pidfile} ] ; then
ebegin "Stopping wpa_supplicant on ${IFACE}"
start-stop-daemon --stop --exec /sbin/wpa_supplicant \
--pidfile "${pidfile}"
eend $?
fi
# If wpa_supplicant exits uncleanly, we need to remove the stale dir
[ -S "/var/run/wpa_supplicant/${IFACE}" ] \
&& rm -f "/var/run/wpa_supplicant/${IFACE}"
}
# vim: set ts=4 :