summaryrefslogtreecommitdiff
path: root/cleopatre
diff options
context:
space:
mode:
authorNicolas Schodet2012-08-03 17:31:59 +0200
committerNicolas Schodet2012-09-07 11:20:10 +0200
commitd5b342ee1fd935266b08dde62dc9c5db6cc7fa16 (patch)
tree2e417d9ee0ff8547b260a558a8281fa26784190b /cleopatre
parentc0040714fd70eb07d6ed82c07e3678a9303c7d6b (diff)
cleo/app/fw_wd: clean up trace files at startup, refs #3208
Diffstat (limited to 'cleopatre')
-rwxr-xr-xcleopatre/application/fw_wd/S99fwwatchd2
-rwxr-xr-xcleopatre/application/fw_wd/fwwatchd-clean35
-rw-r--r--cleopatre/application/fw_wd/fwwatchd.default6
-rw-r--r--cleopatre/buildroot/package/fw_wd/fw_wd.mk6
-rw-r--r--cleopatre/devkit/tests/common/Makefile1
-rwxr-xr-xcleopatre/devkit/tests/fw_wd/run-test67
6 files changed, 115 insertions, 2 deletions
diff --git a/cleopatre/application/fw_wd/S99fwwatchd b/cleopatre/application/fw_wd/S99fwwatchd
index cb0000edfd..0cdcd552a9 100755
--- a/cleopatre/application/fw_wd/S99fwwatchd
+++ b/cleopatre/application/fw_wd/S99fwwatchd
@@ -4,12 +4,14 @@
#
DAEMON=/usr/bin/fwwatchd
+CLEAN=${DAEMON}-clean
PIDFILE=/var/run/fwwatchd.pid
test -f $DAEMON || exit 0
start() {
echo "Starting fwwatchd..."
+ $CLEAN
$DAEMON &
echo $! > $PIDFILE
}
diff --git a/cleopatre/application/fw_wd/fwwatchd-clean b/cleopatre/application/fw_wd/fwwatchd-clean
new file mode 100755
index 0000000000..a28c87e458
--- /dev/null
+++ b/cleopatre/application/fw_wd/fwwatchd-clean
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+# Trace folder.
+TRACE_FOLDER=/usr/local/trace/
+# Trace basename.
+TRACE_BASENAME=trace_
+
+# Load personnalize preferences.
+test -f /etc/default/fwwatchd && . /etc/default/fwwatchd
+
+# Return true if traces use too much space.
+should_clean () {
+ if [[ -n "$TRACE_SIZE_LIMIT_KB" ]]; then
+ set -- $(du -ks "$TRACE_FOLDER")
+ [[ $1 -gt "$TRACE_SIZE_LIMIT_KB" ]] && return 0
+ fi
+ if [[ -n "$TRACE_FREE_LIMIT_KB" ]]; then
+ set -- $(df -k "$TRACE_FOLDER" | tail -n 1)
+ [[ $4 -lt "$TRACE_FREE_LIMIT_KB" ]] && return 0
+ fi
+ return 1
+}
+
+# Remove oldest trace, or return an error if nothing to remove.
+clean_oldest_trace () {
+ old_trace=$(ls -1 $TRACE_FOLDER/$TRACE_BASENAME[0-9]*.gz 2>/dev/null \
+ | sort -n -k 2 -t _ | head -n 1)
+ [[ -z "$old_trace" ]] && return 1
+ rm "$old_trace"
+}
+
+# Clean up old traces if needed.
+while should_clean; do
+ clean_oldest_trace || break
+done
diff --git a/cleopatre/application/fw_wd/fwwatchd.default b/cleopatre/application/fw_wd/fwwatchd.default
index f91e2b14c0..a3a8479783 100644
--- a/cleopatre/application/fw_wd/fwwatchd.default
+++ b/cleopatre/application/fw_wd/fwwatchd.default
@@ -22,3 +22,9 @@ TRACE_FILES="/proc/net/plc/version:/etc/hpav.info:/proc/uptime"
# List of commands to get output with traces (separator is ':').
TRACE_CMDS=""
+
+# Size limit for traces, in kilobytes.
+TRACE_SIZE_LIMIT_KB=200
+
+# Filesystem free space limit for traces, in kilobytes.
+TRACE_FREE_LIMIT_KB=100
diff --git a/cleopatre/buildroot/package/fw_wd/fw_wd.mk b/cleopatre/buildroot/package/fw_wd/fw_wd.mk
index caa19fe149..96611ee50d 100644
--- a/cleopatre/buildroot/package/fw_wd/fw_wd.mk
+++ b/cleopatre/buildroot/package/fw_wd/fw_wd.mk
@@ -46,6 +46,10 @@ $(TARGET_DIR)/usr/bin/fwwatchd: $(FW_WD_DIR)/fwwatchd
mkdir -p $(TARGET_DIR)/usr/bin
cp $< $@
+$(TARGET_DIR)/usr/bin/fwwatchd-clean: $(FW_WD_DIR)/fwwatchd-clean
+ mkdir -p $(TARGET_DIR)/usr/bin
+ cp $< $@
+
$(TARGET_DIR)/etc/init.d/S99fwwatchd: $(FW_WD_DIR)/S99fwwatchd
mkdir -p $(TARGET_DIR)/etc/init.d
cp $< $@
@@ -56,12 +60,14 @@ $(TARGET_DIR)/etc/default/fwwatchd: $(FW_WD_DIR)/fwwatchd.default
fw_wd: uclibc $(TARGET_DIR)/usr/bin/fw_wd \
$(TARGET_DIR)/usr/bin/fwwatchd \
+ $(TARGET_DIR)/usr/bin/fwwatchd-clean \
$(TARGET_DIR)/etc/init.d/S99fwwatchd \
$(TARGET_DIR)/etc/default/fwwatchd
fw_wd-clean:
rm -f $(TARGET_DIR)/usr/bin/fw_wd
rm -f $(TARGET_DIR)/usr/bin/fwwatchd
+ rm -f $(TARGET_DIR)/usr/bin/fwwatchd-clean
rm -f $(TARGET_DIR)/etc/init.d/S99fwwatchd
rm -f $(TARGET_DIR)/etc/default/fwwatchd
-$(MAKE) -C $(FW_WD_DIR) clean
diff --git a/cleopatre/devkit/tests/common/Makefile b/cleopatre/devkit/tests/common/Makefile
index 3874303b8b..cf8755532d 100644
--- a/cleopatre/devkit/tests/common/Makefile
+++ b/cleopatre/devkit/tests/common/Makefile
@@ -37,6 +37,7 @@ busybox/.config: $(BUILDROOT_BUSYBOX_CONFIG) buildroot-config $(BUILDROOT_BUSYBO
mkdir -p busybox
cp $< $@
sed -i 's/CONFIG_HALT=y/# CONFIG_HALT is not set/' $@
+ sed -i 's/CONFIG_DF=y/# CONFIG_DF is not set/' $@
$(MAKE) -C $(BUILDROOT_BUSYBOX_DIR) O=$(CURDIR)/busybox oldconfig
busybox-unpacked: buildroot-config
diff --git a/cleopatre/devkit/tests/fw_wd/run-test b/cleopatre/devkit/tests/fw_wd/run-test
index ce1d77218e..1116bfe391 100755
--- a/cleopatre/devkit/tests/fw_wd/run-test
+++ b/cleopatre/devkit/tests/fw_wd/run-test
@@ -1,7 +1,7 @@
#!/usr/bin/python
"""Test FW watch daemon."""
from shutil import copy, rmtree
-from os import symlink, mkdir, chmod
+from os import symlink, mkdir, chmod, listdir
import sys
import subprocess
import gzip
@@ -52,12 +52,28 @@ echo "reboot run"
exit %(retcode)s
"""
+df_stub = """\
+#!/bin/sh
+fs=%(fs_size_kb)d
+if test -d /usr/local/trace; then
+ set -- $(du -sk /usr/local/trace)
+ trace=$1
+else
+ trace=0
+fi
+free=$(expr $fs - $trace)
+echo "Filesystem 1k-blocks Used Available Use%% Mounted on"
+echo "/dev/mtdblock2 xxxx $trace $free yy%% /usr/local"
+"""
+
default_content = """\
REBOOT=%(reboot)s
TRACE_ENABLED=%(trace_enabled)s
TRACE_FOLDER="/usr/local/trace/"
TRACE_FILES="%(trace_files)s"
TRACE_CMDS="%(trace_cmds)s"
+TRACE_SIZE_LIMIT_KB=%(size_limit_kb)s
+TRACE_FREE_LIMIT_KB=%(free_limit_kb)s
"""
chroot_env = {
@@ -81,6 +97,11 @@ class TestParams:
self.trace_cmds_test = False
self.trace_dir = True
self.old_traces = [ ]
+ self.big_traces = [ ]
+ self.size_limit_kb = None
+ self.free_limit_kb = None
+ self.fs_size_kb = 1024
+ self.after_clean = None
self.kill = False
self.__dict__.update(kw)
@@ -112,6 +133,7 @@ def create_rootfs(root_path, test_params):
symlink('/bin/busybox', root_path + '/proc/self/exe')
chroot_call(root_path, [ '/bin/busybox', '--install', '-s' ])
copy(fw_wd_path + '/fwwatchd', root_path + '/usr/bin')
+ copy(fw_wd_path + '/fwwatchd-clean', root_path + '/usr/bin')
create_file(root_path, '/dev/null', '')
# Create test stubs.
if not test_params.kill:
@@ -124,6 +146,9 @@ def create_rootfs(root_path, test_params):
create_file(root_path, '/sbin/reboot',
reboot_stub % { 'retcode': test_params.reboot_retcode },
executable=True)
+ create_file(root_path, '/bin/df',
+ df_stub % { 'fs_size_kb': test_params.fs_size_kb },
+ executable=True)
if test_params.trace_present:
create_file(root_path, '/dev/trace', trace_content)
for f in test_params.trace_files:
@@ -133,9 +158,13 @@ def create_rootfs(root_path, test_params):
for i in test_params.old_traces:
create_file(root_path, '/usr/local/trace/trace_%d.gz' % i,
'Old trace %d' % i)
+ for i, size in test_params.big_traces:
+ create_file(root_path, '/usr/local/trace/trace_%d.gz' % i, 'B' * size)
# Create default file.
def bool_value(b):
return 'true' if b else 'false'
+ def int_value(i):
+ return str(i) if i is not None else ''
def list_value(l):
return ':'.join(l)
create_file(root_path, '/etc/default/fwwatchd', default_content % {
@@ -143,6 +172,8 @@ def create_rootfs(root_path, test_params):
'trace_enabled': bool_value(test_params.trace_enabled),
'trace_files': list_value(test_params.trace_files),
'trace_cmds': list_value(trace_cmds[test_params.trace_cmds_test]),
+ 'size_limit_kb': int_value(test_params.size_limit_kb),
+ 'free_limit_kb': int_value(test_params.free_limit_kb),
})
def check(root_path, test_params, stdout):
@@ -179,7 +210,10 @@ def check(root_path, test_params, stdout):
expected_trace += "==== %s ====\n" % cmd
expected_trace += result
expected_trace += "\n"
- expected_trace_nb = max(test_params.old_traces + [ 0 ] ) + 1
+ max_big_trace = (max(test_params.big_traces, key=lambda x: x[0])[0]
+ if test_params.big_traces else 0)
+ expected_trace_nb = max(test_params.old_traces
+ + [ max_big_trace, 0 ]) + 1
expected_file_name = '/usr/local/trace/trace_%d.gz' % expected_trace_nb
try:
f = gzip.open(root_path + expected_file_name)
@@ -200,12 +234,26 @@ def check(root_path, test_params, stdout):
print test_params.test_name, \
"failed (can not open %s)" % expected_file_name
return False
+ if test_params.after_clean is not None:
+ traces = set(listdir(root_path + '/usr/local/trace'))
+ expected_traces = set('trace_%d.gz' % i
+ for i in test_params.after_clean)
+ if traces < expected_traces:
+ print test_params.test_name, \
+ "failed (should not be cleaned: %s)" % ' '.join(
+ expected_traces - traces)
+ elif traces > expected_traces:
+ print test_params.test_name, \
+ "failed (should be cleaned: %s)" % ' '.join(
+ traces - expected_traces)
return True
def run_test(root_path, test_params):
"""Run FW watch daemon in a chrooted environment and check result. Return
a number of failed test."""
create_rootfs('root', test_params)
+ subprocess.check_call([ 'fakechroot', '/usr/sbin/chroot', root_path,
+ '/usr/bin/fwwatchd-clean' ], env=chroot_env)
proc = subprocess.Popen([ 'fakechroot', '/usr/sbin/chroot', root_path,
'/usr/bin/fwwatchd' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=chroot_env)
@@ -238,6 +286,21 @@ if __name__ == '__main__':
failed += run_test('root', tp)
tp = TestParams("old traces", old_traces=[ 1, 2, 8 ])
failed += run_test('root', tp)
+ # Clean tests.
+ big_traces = [ (3, 2 * 1024), (4, 40 * 1024), (5, 50 * 1024),
+ (6, 2 * 1024) ]
+ tp = TestParams("clean: no limit", big_traces=big_traces, kill=True,
+ after_clean=[ 3, 4, 5, 6 ])
+ failed += run_test('root', tp)
+ tp = TestParams("clean: size limit", big_traces=big_traces, kill=True,
+ size_limit_kb=80, after_clean=[ 5, 6 ])
+ failed += run_test('root', tp)
+ tp = TestParams("clean: free limit", big_traces=big_traces, kill=True,
+ free_limit_kb=1024 - 80, after_clean=[ 5, 6 ])
+ failed += run_test('root', tp)
+ tp = TestParams("clean: can not help", big_traces=big_traces, kill=True,
+ free_limit_kb=2048, after_clean=[ ])
+ failed += run_test('root', tp)
# Reboot tests.
tp = TestParams("no reboot", reboot=False)
failed += run_test('root', tp)