summaryrefslogtreecommitdiff
path: root/cleopatre/application/fw_wd/fwwatchd
diff options
context:
space:
mode:
authorNicolas Schodet2011-12-08 17:28:15 +0100
committerNicolas Schodet2011-12-20 15:49:31 +0100
commitb8fe0c67430ec147317fd994d702d2328471b99a (patch)
tree4fbbe84a1e5deddbc5dba46b1c96381d7134d8e4 /cleopatre/application/fw_wd/fwwatchd
parent6aaa2d5c07911b9e04b63cff40049e92ba93abf7 (diff)
cleo/app/fw_wd: refactor fwwatchd code, add tests
Diffstat (limited to 'cleopatre/application/fw_wd/fwwatchd')
-rwxr-xr-xcleopatre/application/fw_wd/fwwatchd85
1 files changed, 48 insertions, 37 deletions
diff --git a/cleopatre/application/fw_wd/fwwatchd b/cleopatre/application/fw_wd/fwwatchd
index 3dcd14428c..11033e6ae4 100755
--- a/cleopatre/application/fw_wd/fwwatchd
+++ b/cleopatre/application/fw_wd/fwwatchd
@@ -16,49 +16,60 @@ TRACE_HEADER_LINE=3
# Load personnalize preferences.
test -f /etc/default/fwwatchd && . /etc/default/fwwatchd
-# Do not continue if disabled.
+# Do not continue if nothing to do.
test $TRACE_ENABLED = true || exit 1
-# Ensure trace folder is created.
-mkdir -p $TRACE_FOLDER || exit 1
-
-# Get list of traces, sort them (by the last number)
-# and handle case where there are no traces.
-last_trace=$(ls -1 $TRACE_FOLDER/$TRACE_BASENAME[0-9]*.gz 2>/dev/null | sort \
--n -k 2 -t _ | tail -n 1)
-# No trace file found.
-if [[ -z $last_trace ]];
-then
- # Start at 1.
- new_trace_digit=1
-else
- # Remove prefix & suffix.
- new_trace_digit=${last_trace##$TRACE_FOLDER/$TRACE_BASENAME}
- new_trace_digit=${new_trace_digit%%.gz}
- # Get trace + 1.
- new_trace_digit=$(expr $new_trace_digit + 1)
-fi
-
-# Sleep for the watchdog, get trace (uncompressed, then compressed), get files
-# and execute commands and reboot if required.
-fw_wd && \
- rm -f ${TRACE_HEADER_PATH} && \
- head -n ${TRACE_HEADER_LINE} /dev/trace > ${TRACE_HEADER_PATH} && \
- echo "Full trace available in ${TRACE_FOLDER}/${TRACE_BASENAME}${new_trace_digit}.gz" \
- >> ${TRACE_HEADER_PATH} && \
- (head -n ${TRACE_HEADER_LINE} ${TRACE_FOLDER}/last_head_trace.txt && \
- cat /dev/trace && \
+# Dump all information to be stored in trace.
+dump_trace () {
+ tail -n +2 ${TRACE_FOLDER}/last_head_trace.txt
+ cat /dev/trace
IFSOLD=$IFS
- IFS=":" && for i in $TRACE_FILES
- do
+ IFS=":" && for i in $TRACE_FILES; do
IFS=$IFSOLD
test -f "$i" && echo "==== $i ====" && cat "$i" && echo ""
done
- IFS=":" && for i in $TRACE_CMDS
- do
+ IFS=":" && for i in $TRACE_CMDS; do
IFS=$IFSOLD
echo "==== $i ====" && $i && echo ""
done
- ) | gzip -c \
- > ${TRACE_FOLDER}/${TRACE_BASENAME}${new_trace_digit}.gz \
- && test $REBOOT = true && reboot
+ IFS=$IFSOLD
+}
+
+# Get all trace information and store it on file system.
+get_trace () {
+ test $TRACE_ENABLED = true || return 0
+
+ # Ensure trace folder is created.
+ mkdir -p $TRACE_FOLDER || return 1
+
+ # Get list of traces, sort them (by the last number)
+ # and handle case where there are no traces.
+ last_trace=$(ls -1 $TRACE_FOLDER/$TRACE_BASENAME[0-9]*.gz 2>/dev/null \
+ | sort -n -k 2 -t _ | tail -n 1)
+ # No trace file found.
+ if [[ -z $last_trace ]]; then
+ # Start at 1.
+ new_trace_digit=1
+ else
+ # Remove prefix & suffix.
+ new_trace_digit=${last_trace##$TRACE_FOLDER/$TRACE_BASENAME}
+ new_trace_digit=${new_trace_digit%%.gz}
+ # Get trace + 1.
+ new_trace_digit=$(expr $new_trace_digit + 1)
+ fi
+
+ # Get trace (uncompressed, then compressed), get files and execute
+ # commands.
+ new_trace=${TRACE_FOLDER}/${TRACE_BASENAME}${new_trace_digit}.gz
+ echo "Full trace available in $new_trace" > ${TRACE_HEADER_PATH} && \
+ head -n ${TRACE_HEADER_LINE} /dev/trace >> ${TRACE_HEADER_PATH} && \
+ dump_trace | gzip -c > $new_trace
+}
+
+# Sleep until watchdog expires, get trace and reboot if requested.
+while true; do
+ fw_wd || exit 1
+ get_trace
+ { test $REBOOT = true && reboot; } || \
+ exit 1
+done