diff options
author | Katze Miau <miaukatzemiau@priveasy.de> | 2011-12-06 15:57:38 (GMT) |
---|---|---|
committer | Katze Miau <miaukatzemiau@priveasy.de> | 2011-12-06 15:57:38 (GMT) |
commit | a4fa439241252bb25559952aeffbffd7f6d9721f (patch) | |
tree | 3dabbf48092244dac75c5f64138b57678fa64651 /files/common/etc/fsm/update/watch | |
parent | a4487afd8a7a2121217de9c1939d783164498a0c (diff) |
add FSM update
FSM to handle firmware update process in a synchronized all-or-nothing
manner. See doc/overview.org for details.
Diffstat (limited to 'files/common/etc/fsm/update/watch')
-rwxr-xr-x | files/common/etc/fsm/update/watch/default | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/files/common/etc/fsm/update/watch/default b/files/common/etc/fsm/update/watch/default new file mode 100755 index 0000000..a6858af --- /dev/null +++ b/files/common/etc/fsm/update/watch/default @@ -0,0 +1,72 @@ +#!/bin/sh -e +set -x + +. ../common.sh + +# announce a transition and terminate +trans () { + echo $1 + exit +} + +# is the firmware file available? +checkFw () { + [ -n "$TargetFw" -a -f "$FwDst" ] || trans idle +} + +checkFwHash () { + [ "$(sha256sum <$FwDst 2>/dev/null | cut -f1 -d' ')" == "$TargetFw" ] || trans idle +} + +# is target time passed but not set +checkTime () { + [ $CurTime -lt "$TargetTime" -o "$AckTime" -eq "$TargetTime" ] 2>/dev/null \ + || trans ready +} + +# check all-or-nothing-invariant for whole update state table +checkPeerState() { + p2ptbl show $Tbl \ + | ( + IFS=$'\t' + while read OId OCurFw OTargetFw OTargetTime OAckTime; do + [ -z "$OTargetFw" -a -z "$OTargetTime" -a -z "$OAckTime" ] && continue + [ -n "$OTargetFw" -a "$OTargetTime" -eq "$OAckTime" ] && continue + exit 1 + done ) || trans ready +} + + +case $CurState in + idle) + checkFw + checkFwHash + # ^^ check hash of firmware file once if the hash is correct; + # do not optimize for the rare case of a wrong hash or + # continuously failing updates + trans ready + ;; + ready) + checkFw + checkTime + [ "$TargetTime" -gt $CurTime ] 2>/dev/null || trans ready + trans scheduled + ;; + scheduled) + checkFw + checkTime + [ $CurTime -gt "$TargetTime" ] || trans scheduled + checkPeerState + trans applying + ;; + *) + # anything else is illegal; especially the state "applying" + # must not be reached: during transition toward it the + # firmware is flashed and the router rebooted + echo "illegal state $CurState" >&2 + exit 1 + ;; +esac + +# default and if any case falls through +trans idle
\ No newline at end of file |