Skip to content

Commit c7d2477

Browse files
committed
case-lib: hijack.sh: fix shell check error
fix shell check error and remove some useless code Signed-off-by: Wu, BinX <binx.wu@intel.com>
1 parent 36da9e3 commit c7d2477

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

case-lib/hijack.sh

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
declare -g SUDO_CMD=$(which sudo)
3+
SUDO_CMD=$(command -v sudo)
44

55
# Overwrite other functions' exit to perform environment cleanup
66
function exit()
@@ -10,16 +10,24 @@ function exit()
1010
# when sof logger collect is open
1111
if [ "X$SOF_LOG_COLLECT" == "X1" ]; then
1212
# when error occurs, exit and catch etrace log
13-
[[ $exit_status -eq 1 ]] && func_lib_start_log_collect 1 && sleep 1s
14-
local loggerBin=$(basename $SOFLOGGER)
15-
sudo pkill -9 $loggerBin 2>/dev/null
13+
[[ $exit_status -eq 1 ]] && {
14+
func_lib_start_log_collect 1
15+
sleep 1s
16+
}
17+
local loggerBin; loggerBin=$(basename "$SOFLOGGER")
18+
sudo pkill -9 "$loggerBin" 2>/dev/null
1619
sleep 1s
1720
fi
1821
# when case ends, store kernel log
22+
# /var/log/kern.log format:
23+
# f1 f2 f3 f4 f5 f6 f7 f8...
24+
# Mouth day Time MachineName kernel: [ time] content
25+
# May 15 21:28:38 MachineName kernel: [ 6.469255] sof-audio-pci 0000:00:0e.0: ipc rx: 0x90020000: GLB_TRACE_MSG
26+
# May 15 21:28:38 MachineName kernel: [ 6.469268] sof-audio-pci 0000:00:0e.0: ipc rx done: 0x90020000: GLB_TRACE_MSG
1927
if [[ -n "$DMESG_LOG_START_LINE" && "$DMESG_LOG_START_LINE" -ne 0 ]]; then
20-
tail -n +"$DMESG_LOG_START_LINE" /var/log/kern.log |cut -f5- -d ' ' > $LOG_ROOT/dmesg.txt
28+
tail -n +"$DMESG_LOG_START_LINE" /var/log/kern.log |cut -f5- -d ' ' > "$LOG_ROOT/dmesg.txt"
2129
else
22-
cat /var/log/kern.log |cut -f5- -d ' ' > $LOG_ROOT/dmesg.txt
30+
cut -f5- -d ' ' /var/log/kern.log > "$LOG_ROOT/dmesg.txt"
2331
fi
2432

2533
# get ps command result as list
@@ -57,7 +65,7 @@ function exit()
5765
fi
5866
# if failed to restore pulseaudio, even test caes passed, set exit status to ret
5967
# to make test case failed. this helps to dectect pulseaudio failures.
60-
if [ $exit_status -eq 0 -a $ret -ne 0 ]; then
68+
if [ "$exit_status" -eq 0 ] && [ $ret -ne 0 ]; then
6169
exit_status=$ret
6270
fi
6371

@@ -79,27 +87,24 @@ function exit()
7987
builtin exit $exit_status
8088
}
8189

90+
SUDO_LEVEL=""
8291
# overwrite the sudo command, sudo in the script can direct using sudo command
8392
sudo()
8493
{
8594
func_hijack_setup_sudo_level
95+
local cmd
8696
case $SUDO_LEVEL in
87-
'0') # as root
88-
eval $(echo "$*")
89-
return $?
97+
'0') cmd="$*" # as root
9098
;;
91-
'1') # sudo without passwd
92-
eval $(echo "$SUDO_CMD env 'PATH=$PATH' $*")
93-
return $?
99+
'1') cmd="$SUDO_CMD env 'PATH=$PATH' $*" # sudo without passwd
94100
;;
95-
'2') # sudo need passwd
96-
eval $(echo "echo '$SUDO_PASSWD' | $SUDO_CMD -S env 'PATH=$PATH' $*")
97-
return $?
101+
'2') cmd="echo '$SUDO_PASSWD' | $SUDO_CMD -S env 'PATH=$PATH' $*" # sudo need passwd
98102
;;
99103
*) # without sudo permission
100104
dlogw "Need root privilege to run $*"
105+
return 2
101106
esac
102-
return 2
107+
eval "$cmd"
103108
}
104109

105110
func_hijack_setup_sudo_level()
@@ -108,7 +113,7 @@ func_hijack_setup_sudo_level()
108113
# root permission, don't need to check
109114
[[ $UID -eq 0 ]] && SUDO_LEVEL=0 && return 0
110115
# now check whether we need sudo passwd using expect
111-
expect >/dev/null <<END
116+
if expect >/dev/null <<END
112117
spawn $SUDO_CMD ls
113118
expect {
114119
"password" {
@@ -117,12 +122,13 @@ expect {
117122
exit 0
118123
}
119124
END
120-
[[ $? -eq 0 ]] && SUDO_LEVEL=1 && return 0
125+
then
126+
SUDO_LEVEL=1 && return 0
127+
fi
121128

122129
# check for sudo passwd
123130
if [[ "$SUDO_PASSWD" ]]; then
124-
local tmp_uid=$(echo "$SUDO_PASSWD"|$SUDO_CMD -S bash -c 'echo $UID')
125-
[[ $tmp_uid -eq 0 ]] && SUDO_LEVEL=2 && return 0
131+
[[ $(echo "$SUDO_PASSWD"|$SUDO_CMD -S id -u) -eq 0 ]] && SUDO_LEVEL=2 && return 0
126132
fi
127133
return 1
128134
}

0 commit comments

Comments
 (0)