openrc/net/wpa_supplicant.sh

198 lines
4.8 KiB
Bash
Raw Normal View History

# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
wpa_supplicant_depend()
{
2007-09-25 17:50:00 +05:30
if [ -x /usr/sbin/wpa_supplicant ]; then
program start /usr/sbin/wpa_supplicant
else
program start /sbin/wpa_supplicant
fi
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
2007-11-28 21:15:03 +05:30
if ! type _get_ssid >/dev/null 2>&1; then
_get_ssid()
{
local timeout=5 ssid=
2007-11-28 21:15:03 +05:30
while [ ${timeout} -gt 0 ]; do
ssid=$(wpa_cli -i"${IFACE}" status | sed -n -e 's/^ssid=//p')
2007-11-28 21:15:03 +05:30
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= wireless=true
2007-09-25 17:50:00 +05:30
local wpas=/usr/sbin/wpa_supplicant wpac=/usr/bin/wpa_cli
if [ ! -x "${wpas}" ]; then
wpas=/sbin/wpa_supplicant
wpac=/bin/wpa_cli
fi
[ "${RC_UNAME}" = "Linux" ] || unset wpac
eval opts=\$wpa_supplicant_${IFVAR}
case " ${opts} " in
*" -Dwired "*) wireless=false;;
*) _is_wireless || return 0;;
esac
# We don't configure wireless if we're being called from
# the background unless we're not currently running
2007-11-22 18:58:14 +05:30
if yesno ${IN_BACKGROUND}; then
if ${wireless} && \
service_started_daemon "${SVCNAME}" "${wpas}"; then
SSID=$(_get_ssid "${IFACE}")
SSIDVAR=$(_shell_var "${SSID}")
service_set_value "SSID" "${SSID}"
metric=2000
fi
return 0
fi
service_set_value "SSID" ""
2007-11-28 21:15:03 +05:30
ebegin "Starting wpa_supplicant on ${IFVAR}"
2007-11-28 21:15:03 +05:30
if [ -x /sbin/iwconfig ]; then
local x=
2007-11-28 21:15:03 +05:30
for x in txpower rate rts frag; do
iwconfig "${IFACE}" "${x}" auto 2>/dev/null
done
fi
cfgfile=${opts##* -c}
2007-11-28 21:15:03 +05:30
if [ -n "${cfgfile}" -a "${cfgfile}" != "${opts}" ]; then
case "${cfgfile}" in
2007-11-28 21:15:03 +05:30
" "*) 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
2007-11-28 21:15:03 +05:30
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
2007-04-07 01:08:23 +05:30
local ctrl_dir=$(sed -n -e 's/[[:space:]]*#.*//g;s/[[:space:]]*$//g;s/^ctrl_interface=//p' "${cfgfile}")
2007-11-28 21:15:03 +05:30
if [ -z "${ctrl_dir}" ]; then
ctrl_dir=${opts##* -C}
2007-11-28 21:15:03 +05:30
if [ -n "${ctrl_dir}" -a "${ctrl_dir}" != "${opts}" ]; then
case "${ctrl_dir}" in
2007-11-28 21:15:03 +05:30
" "*) ctrl_dir=${ctrl_dir# *};;
esac
ctrl_dir=${ctrl_dir%% *}
else
ctrl_dir="/var/run/wpa_supplicant"
opts="${opts} -C ${ctrl_dir}"
fi
fi
service_set_value ctrl_dir "${ctrl_dir}"
actfile="/etc/wpa_supplicant/wpa_cli.sh"
if [ -n "${wpac}" ]; then
opts="${opts} -W"
else
sleep 2 # FBSD 7.0 beta2 bug
2007-12-18 23:29:29 +05:30
mark_service_inactive
fi
2007-09-25 17:50:00 +05:30
start-stop-daemon --start --exec "${wpas}" \
--pidfile "/var/run/wpa_supplicant-${IFACE}.pid" \
-- ${opts} -B -i "${IFACE}" \
-P "/var/run/wpa_supplicant-${IFACE}.pid"
eend $? || return 1
if [ -z "${wpac}" ]; then
ebegin "Backgrounding ..."
exit 1
fi
# Starting wpa_supplication-0.4.0, we can get wpa_cli to
# start/stop our scripts from wpa_supplicant messages
local inact=false
2007-12-18 23:29:29 +05:30
service_inactive && inact=true
mark_service_inactive
ebegin "Starting wpa_cli on" "${IFACE}"
2007-09-25 17:50:00 +05:30
start-stop-daemon --start --exec "${wpac}" \
--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
2007-11-28 21:15:03 +05:30
if eend $?; then
ebegin "Backgrounding ..."
exit 1
fi
# wpa_cli failed to start? OK, error here
2007-09-25 17:50:00 +05:30
start-stop-daemon --quiet --stop --exec "${wpas}" \
--pidfile "/var/run/wpa_supplicant-${IFACE}.pid"
2007-12-18 23:29:29 +05:30
${inact} || mark_service_stopped
return 1
}
wpa_supplicant_post_stop()
{
2007-09-25 17:50:00 +05:30
local wpas=/usr/sbin/wpa_supplicant wpac=/usr/bin/wpa_cli
if [ ! -x "${wpas}" ]; then
wpas=/sbin/wpa_supplicant
wpac=/bin/wpa_cli
fi
2007-11-22 18:58:14 +05:30
if yesno "${IN_BACKGROUND}"; then
# Only stop wpa_supplicant if it's not the controlling daemon
2007-09-25 17:50:00 +05:30
! service_started_daemon "${SVCNAME}" "${wpas}" 1
fi
[ $? != 0 ] && return 0
local pidfile="/var/run/wpa_cli-${IFACE}.pid"
2007-11-28 21:15:03 +05:30
if [ -f ${pidfile} ]; then
ebegin "Stopping wpa_cli on ${IFACE}"
2007-09-25 17:50:00 +05:30
start-stop-daemon --stop --exec "${wpac}" --pidfile "${pidfile}"
eend $?
fi
pidfile="/var/run/wpa_supplicant-${IFACE}.pid"
2007-11-28 21:15:03 +05:30
if [ -f ${pidfile} ]; then
ebegin "Stopping wpa_supplicant on ${IFACE}"
2007-09-25 17:50:00 +05:30
start-stop-daemon --stop --exec "${wpas}" --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}"
}