aboutsummaryrefslogtreecommitdiffstats
path: root/lib/functions.sh
blob: a5efdde3cb6724e99205e94abbb8fecde8fabde6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#
# Function library: provide common functions
#
# Copyright (C) 2008, Intel Corp.
#   Author: Huang Ying <ying.huang@intel.com>
#
# This file is released under the GPLv2.
#

setup_path()
{
    export PATH=$ROOT/bin:$PATH
}

script_dir()
{
    local rd=$(dirname "$0")
    (cd $rd; pwd)
}

relative_path()
{
    local len1=${#1}
    local len2=${#2}
    if [ $len1 -eq 0 -o $len1 -ge $len2 -o "${2:0:$len1}" != "$1" ]; then
	die "$2 is not the sub-path of $1!"
    fi
    len1=$((len1 + 1))
    echo "${2:$len1}"
}

die()
{
    echo "DIE: $@"
    echo "DIE: $@" 1>&2
    exit -1
}

driver_prepare()
{
    mkdir -p $WDIR/stamps
}

check_kern_warning_bug()
{
    local f="$1"
    [ -n "$f" ] || die "missing parameter for check_kern_warning"
    grep -e '----\[ cut here \]---' $f > /dev/null || \
	grep -e 'BUG:' $f > /dev/null
}

random_sleep()
{
    local s=$((RANDOM / 6553))
    sleep $s
}

start_background()
{
    if [ -n "$BACKGROUND" ]; then
	pid_background=$(bash -i -c "$BACKGROUND &>$WDIR/background_log & echo \$!")
	if ! ps -p $pid_background > /dev/null; then
	    die "Failed to start background testing: $BACKGROUND"
	fi
    fi
}

stop_background()
{
    if [ -n "$pid_background" ]; then
	if ! kill -TERM -$pid_background &> /dev/null; then
	    kill $pid_background || true
	fi
    fi
}

filter_fake_panic()
{
    local orig_klog=$1
    local new_klog=$2
    [ $# -eq 2 ] || die "missing parameter for filter_fake_panic"

    local pn
    pn=$(grep -n "Fake kernel panic" $orig_klog | cut -d ':' -f 1 | head -1)
    if [ -z "$pn" ]; then
	cp $orig_klog $new_klog
    else
	sed -n "1,${pn}p" < $orig_klog > $new_klog
    fi
}