commit bad1ea36c4290e95ffc647ee36b85df914fb9380
parent 9be91c42651017a27465185a7fb959d3027c4e26
Author: Christoph Lohmann <20h@r-36.net>
Date: Wed, 23 Feb 2011 17:49:31 +0100
Adding state and suspend support to conn.
Diffstat:
5 files changed, 250 insertions(+), 38 deletions(-)
diff --git a/Makefile b/Makefile
@@ -12,6 +12,12 @@ dist:
@gzip conn-${VERSION}.tar
@rm -rf conn-${VERSION}
+pm:
+ @echo installing pm file to ${DESTDIR}/etc/pm/sleep.d
+ @mkdir -p ${DESTDIR}/etc/pm/sleep.d
+ @cp etc/pm/sleep.d/10-conn.sh ${DESTDIR}/etc/pm/sleep.d
+ @chmod 755 ${DESTDIR}/etc/pm/sleep.d/10-conn.sh
+
install:
@echo installing conn script to ${DESTDIR}${PREFIX}/bin
@cp bin/conn ${DESTDIR}${PREFIX}/bin
diff --git a/README.md b/README.md
@@ -34,6 +34,9 @@ many different connections in a Unix like environment.
/etc/conn/$conn/$files These files depend solely on the connection, which
is served. Look at them for details.
+/etc/pm/sleep.d/10-conn.sh The script for providing suspend and hib-
+ ernate support.
+
### Calltree
* conn
* conn/run.sh
@@ -47,11 +50,20 @@ many different connections in a Unix like environment.
eth
wifi
wwan
+ # Connecto to the wifi
% conn -s wifi
+ # HUP wpa_supplicant
% conn -u wifi
+ # Kill the wifi
% conn -k wifi
+ # Start ethernet
% conn -s eth
+ # Kill the ethernet
% conn -k eth
+ # Start the wwan
% conn -s wwan
-
+ # Suspend all connections
+ % conn -n
+ # Wakeup all previous connections
+ % conn -w
diff --git a/etc/conn/common.sh b/etc/conn/common.sh
@@ -16,15 +16,82 @@ ETCDIR="/etc/conn"
WIFIDIR="${ETCDIR}/wifi"
WWANDIR="${ETCDIR}/wwan"
ETHDIR="${ETCDIR}/eth"
+STATEDIR="/var/state/conn"
mkpaths() {
- for i in $RUNDIR $ETCDIR $WIFIDIR $WWANDIR \
- $ETHDIR;
+ for i in $RUNDIR $STATEDIR;
do
[ ! -e $i ] && mkdir -p $i
done
}
-mkpaths
+
+## Locking in bash
+lock() {
+ LOCKF="$1.lock"
+ lock=0
+
+ [ -e $LOCKF ] && lock=1
+
+ while [ $lock -eq 1 ]
+ do
+ [ ! -e $LOCKF ] && lock=0
+ sleep 0.5;
+ done
+
+ touch $LOCKF
+}
+
+unlock() {
+ LOCKF="$1.lock"
+ rm $LOCKF
+}
+
+## Global conn lock, when there is database access.
+CONNLOCKF="$STATEDIR/conn"
+lockconn() {
+ lock $CONNLOCKF
+}
+
+unlockconn() {
+ unlock $CONNLOCKF
+}
+
+## State handling
+OPENCONNFILE="$STATEDIR/open"
+getopenconnections() {
+ [ ! -e $OPENCONNFILE ] && return 1
+ [ `stat -c%s $OPENCONNFILE` -eq 0 ] && return 1
+ cat $OPENCONNFILE
+ return 0
+}
+
+isopen() {
+ isin=`getopenconnections | grep "$1 $2"`
+ [ "$isin" == "" ] && return 1
+ return 0
+}
+
+addconnection() {
+ isopen $1 $2 && return 1
+ echo "$1 $2" >> $OPENCONNFILE
+}
+
+delconnection() {
+ [ ! -e $OPENCONNFILE ] && return 1
+
+ if ! isopen $1 $2;
+ then
+ return 1
+ else
+ cat $OPENCONNFILE | grep -v "$1 $2" \
+ > $OPENCONNFILE.bak
+ mv $OPENCONNFILE.bak $OPENCONNFILE
+ fi
+}
+
+getdefaultconnection() {
+ echo "$DEFAULTCONNECTION $DEFAULTPROFILE"
+}
## WPA handling
WPAPID="${RUNDIR}/wpa_supplicant.pid"
diff --git a/etc/conn/run.sh b/etc/conn/run.sh
@@ -2,40 +2,154 @@
. ./common.sh
-profile=$3
-connection=$2
-[ "$2" == "" ] && [ "$DEFAULTCONNECTION" != "" ] \
- && connection="$DEFAULTCONNECTION" \
- && profile="$DEFAULTPROFILE"
-
-case "$1" in
- -s|-k|-u)
- arg=$1
- if [ $# -lt 3 ];
- then
- shift $#
- else
- shift 3
- fi
-
- runconnection $connection $arg $profile $*
- exit $?
- ;;
- -l)
- echo "Available network types:"
- for i in `ls -d */ | sed 's~/~~g'`;
+mkpaths
+
+usage() {
+ echo "usage: $1 [-hfnwlo] [-[s|k|u|r] [connection [args ...]]"
+}
+
+dostart=0
+dokill=0
+dohup=0
+dorestart=0
+dolist=0
+dolistopen=0
+dosuspend=0
+dowakeup=0
+doforce=0
+
+cmdarg=""
+while getopts "fwnskurlo" opt;
+do
+ case $opt in
+ s)
+ dostart=1
+ cmdarg="-s"
+ ;;
+ k)
+ dokill=1
+ cmdarg="-k"
+ ;;
+ u)
+ dohup=1
+ cmdarg="-u"
+ ;;
+ r)
+ dorestart=1
+ ;;
+ l)
+ dolist=1
+ ;;
+ o)
+ dolistopen=1
+ ;;
+ n)
+ dosuspend=1
+ ;;
+ w)
+ dowakeup=1
+ ;;
+ f)
+ doforce=1
+ ;;
+ *)
+ usage
+ exit 1
+ ;;
+ esac
+done
+shift $(($OPTIND - 1))
+
+if [ $dolist -eq 1 ];
+then
+ echo "Available network types:"
+ for i in `ls -d */ | sed 's~/~~g'`;
+ do
+ echo $i
+ done
+
+ exit 0
+fi
+
+if [ $dolistopen -eq 1 ];
+then
+ conns=`getopenconnections`
+ if [ "$conns" == "" ];
+ then
+ echo "There are no connections open."
+ exit 1
+ else
+ echo "Open connections:"
+ for conn in $conns;
do
- echo $i
+ echo $conn
done
- ;;
- -r)
- shift 1
- $0 -k $*
- $0 -s $*
- ;;
- *)
- echo "usage: $0 [-s|-k|-u|-r|-l] connection [args ...]"
- exit 1
- ;;
-esac
+ fi
+
+ exit 0
+fi
+
+[ $dowakeup -eq 1 ] && cmdarg="-s"
+
+if [ $dosuspend -eq 1 ];
+then
+ $0 -f -k $*
+ exit 0
+fi
+
+if [ $dorestart -eq 1 ];
+then
+ $0 -k $*
+ $0 -s $*
+fi
+
+if [ "$cmdarg" == "" ];
+then
+ usage
+ exit 1
+fi
+
+connection=$1
+profile=$2
+if [ "$1" == "" ];
+then
+ if [ $dostart -eq 1 ] && [ ! $dowakeup -eq 1 ];
+ then
+ conns=`getdefaultconnection`
+ else
+ conns=`getopenconnections`
+ fi
+
+ [ "$conns" == "" ] && exit 0
+ [ $doforce -eq 1 ] && cmdarg="-f $cmdarg"
+
+ for conn in $conns;
+ do
+ args=(`echo $conn`)
+ [ ${#args[@]} -eq 0 ] && continue
+ [ ${#args[@]} -eq 1 ] && args[1]=""
+
+ $0 $cmdarg ${args[0]} ${args[1]}
+ exit 0
+ done
+
+ exit $?
+fi
+
+if [ ! $doforce -eq 1 ];
+then
+ [ $dostart -eq 1 ] && addconnection $connection $profile
+ [ $dokill -eq 1 ] && delconnection $connection $profile
+fi
+
+if [ $# -lt 2 ];
+then
+ shift $#
+else
+ shift 2
+fi
+
+runconnection $connection $cmdarg $profile $*
+
+exit $?
diff --git a/etc/pm/sleep.d/10-conn.sh b/etc/pm/sleep.d/10-conn.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+case $1 in
+ hibernate|suspend)
+ conn -n;
+ ;;
+ thaw|resume)
+ conn -w;
+ ;;
+ *)
+ ;;
+esac
+