Skip to content

Instantly share code, notes, and snippets.

@noktoborus
Created September 20, 2011 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noktoborus/1228830 to your computer and use it in GitHub Desktop.
Save noktoborus/1228830 to your computer and use it in GitHub Desktop.
/etc/init.d/rfkill-event for gentoo, script execute files from /etc/runlevels/soft-${rftype (hci|phy|uwb|...} when rfkill event set to disable or enable radio
#!/sbin/runscript
# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $
opts="run"
script="/etc/init.d/rfkill-event"
pidfile="/var/run/rfkill-event.pid"
# op=2 - status change
# op=0 - init device
# op=1 - destroy device
# (soft|hard)=0 - flag not setted (device enable)
# (soft|hard)=1 - flag setted (device disabled)
# stop at (op=1 || hwst=1)
# start at ((op=0 && hwst=0 ) || (op=2 && hwst=0)) # || (op!=1 && hwst=0)
run() {
LIST=""
rfkill event\
| sed -u -e 's/.*idx \([0-9]*\) type [0-9]* op \([0-2]\) soft [0-1] hard \([0-1]\)\|.*/\1 \2 \3/' -e '/^$/d'\
| while read idx op hwst;
do
# update rf indexes
[ -z "$LIST" -o x"$op" != x"1" ] && LIST=$(rfkill list | grep -v '^[[:space:]].*')
sys=$(echo "$LIST" | sed -e "s/${idx}: \([a-z]*\).*\|.*/\1/" -e '/^$/d' | head -n1)
[ -z "$sys" ] && continue
if [ x"$op" = x"1" -o x"$hwst" = x"1" ];
then
# rf set off
einfo "RF $sys got down"
find -L "/etc/runlevels/rf-${sys}" -type f -exec sh -c "[ -x '{}' ] && '{}' stop" \; 2>/dev/null
elif [ x"$op" != x"1" -a x"$hwst" = x"0" ];
then
# rf set on
einfo "RF $sys got up"
find -L "/etc/runlevels/rf-${sys}" -type f -exec sh -c "[ -x '{}' ] && '{}' start" \; 2>/dev/null
fi
done
}
start() {
ebegin "Start rfkill event catcher"
start-stop-daemon -S -x ${script} --pidfile "$pidfile" -mb -- run
R=$?
if [ $R -eq 0 ];
then
(
sleep 3
P=$( (ps --ppid $(cat "$pidfile") -o pid | tail -n1 | xargs -I {} ps --ppid {} -o pid | tail -n+2) 2>/dev/null)
[ ! -z "$P" ] && echo "$P" >"$pidfile"
) &
fi
eend $R
}
stop() {
ebegin "Stop rfkill event catcher"
start-stop-daemon -K -x ${script} --pidfile "$pidfile"
eend $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment