Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 86 additions & 22 deletions Runner/suites/Multimedia/DSP_AudioPD/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fi

STARTED_BY_TEST=0
PID=""
PIDS=""

check_adsprpcd_wait_state() {
pid="$1"
Expand Down Expand Up @@ -112,9 +113,17 @@ check_adsprpcd_wait_state() {
}

if is_process_running "adsprpcd"; then
log_info "adsprpcd is running"
PID=$(get_one_pid_by_name "adsprpcd" 2>/dev/null || true)
PID=$(sanitize_pid "$PID")
# is_process_running already prints instances/cmdline (for CI debug)
PIDS=$(get_one_pid_by_name "adsprpcd" all 2>/dev/null || true)

# Pick a primary PID for legacy logging (first valid numeric PID)
for p in $PIDS; do
p_clean=$(sanitize_pid "$p")
if [ -n "$p_clean" ]; then
PID="$p_clean"
break
fi
done
else
log_info "adsprpcd is not running"
log_info "Manually starting adsprpcd daemon"
Expand All @@ -130,47 +139,102 @@ else
PID=$(get_one_pid_by_name "adsprpcd" 2>/dev/null || true)
PID=$(sanitize_pid "$PID")
fi

# After start, gather all adsprpcd PIDs (if helper supports it)
PIDS=$(get_one_pid_by_name "adsprpcd" all 2>/dev/null || true)
fi

# Fallback if helper returned nothing
if [ -z "$PIDS" ] && [ -n "$PID" ]; then
PIDS="$PID"
fi

log_info "PID is $PID"

if [ -z "$PID" ] || ! wait_pid_alive "$PID" 10; then
log_fail "Failed to start adsprpcd or PID did not become alive"
# Build an "alive" PID list (avoid false failures if a PID disappears)
PIDS_ALIVE=""
alive_count=0
dead_seen=0
for p in $PIDS; do
p_clean=$(sanitize_pid "$p")
if [ -z "$p_clean" ]; then
continue
fi

if wait_pid_alive "$p_clean" 10; then
alive_count=$((alive_count + 1))
if [ -z "$PIDS_ALIVE" ]; then
PIDS_ALIVE="$p_clean"
else
PIDS_ALIVE="$PIDS_ALIVE $p_clean"
fi
else
dead_seen=1
log_warn "adsprpcd PID $p_clean did not become alive"
fi
done

# Only print alive list if something was dropped (avoids duplicate info in normal case)
if [ "$dead_seen" -eq 1 ]; then
log_info "Alive adsprpcd PIDs: $PIDS_ALIVE"
fi

if [ "$alive_count" -le 0 ]; then
log_fail "Failed to start adsprpcd or no alive PID found"
echo "$TESTNAME FAIL" >"$res_file"

# Kill only if we started it and PID is valid
if [ "$STARTED_BY_TEST" -eq 1 ]; then
PID_CLEAN=$(sanitize_pid "$PID")
if [ -n "$PID_CLEAN" ]; then
kill_process "$PID_CLEAN" || true
fi
for p in $PIDS; do
p_clean=$(sanitize_pid "$p")
if [ -n "$p_clean" ]; then
kill_process "$p_clean" || true
fi
done
fi
exit 0
fi

# Evaluate
check_adsprpcd_wait_state "$PID"
rc=$?
# Evaluate all alive PIDs
fail_seen=0
skip_seen=0

for p in $PIDS_ALIVE; do
check_adsprpcd_wait_state "$p"
rc=$?

if [ "$rc" -eq 0 ]; then
log_pass "$TESTNAME : Test Passed"
echo "$TESTNAME PASS" >"$res_file"
elif [ "$rc" -eq 2 ]; then
if [ "$rc" -eq 2 ]; then
skip_seen=1
break
fi
if [ "$rc" -ne 0 ]; then
fail_seen=1
fi
done

if [ "$skip_seen" -eq 1 ]; then
# SKIP already written by the function
:
else
log_fail "$TESTNAME : Test Failed"
echo "$TESTNAME FAIL" >"$res_file"
if [ "$fail_seen" -eq 0 ]; then
log_pass "$TESTNAME : Test Passed"
echo "$TESTNAME PASS" >"$res_file"
else
log_fail "$TESTNAME : Test Failed"
echo "$TESTNAME FAIL" >"$res_file"
fi
fi

log_info "-------------------Completed $TESTNAME Testcase----------------------------"

# Kill only if we started it
if [ "$STARTED_BY_TEST" -eq 1 ]; then
PID_CLEAN=$(sanitize_pid "$PID")
if [ -n "$PID_CLEAN" ]; then
kill_process "$PID_CLEAN" || true
fi
for p in $PIDS; do
p_clean=$(sanitize_pid "$p")
if [ -n "$p_clean" ]; then
kill_process "$p_clean" || true
fi
done
fi

exit 0
159 changes: 95 additions & 64 deletions Runner/utils/functestlib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4343,34 +4343,68 @@ sanitize_pid() {
| awk '{print $1; exit}'
}

# Get a single PID (first match) for a given process name.
# Prefer pgrep, then get_pid, then a ps fallback.
get_one_pid_by_name() {
get_pids_by_name() {
name="$1"
[ -z "$name" ] && return 1

pid=""

# Prefer pgrep
if command -v pgrep >/dev/null 2>&1; then
pid=$(pgrep -x "$name" 2>/dev/null | awk 'NR==1{print; exit}')
pid=$(sanitize_pid "$pid")
[ -n "$pid" ] && { printf '%s\n' "$pid"; return 0; }

if [ -z "$name" ]; then
return 1
fi

# Fallback to get_pid (should already return first PID after your update)
if command -v get_pid >/dev/null 2>&1; then
pid=$(get_pid "$name" 2>/dev/null)
pid=$(sanitize_pid "$pid")
[ -n "$pid" ] && { printf '%s\n' "$pid"; return 0; }

# Print one PID per line (sanitized), for all processes whose basename matches $name
ps -ef 2>/dev/null | awk -v n="$name" '
NR==1 { next }
{
cmd=$8
sub(".*/", "", cmd)
if (cmd == n) { print $2 }
}' | while IFS= read -r p; do
p_clean=$(sanitize_pid "$p")
if [ -n "$p_clean" ]; then
printf '%s\n' "$p_clean"
fi
done
}

# Backward-compatible wrapper:
# get_one_pid_by_name <name> -> first PID
# get_one_pid_by_name <name> all -> all PIDs (newline-separated)
get_one_pid_by_name() {
name="$1"
mode="${2:-}"

pids=""
first_pid=""

for d in /proc/[0-9]*; do
[ -r "$d/comm" ] || continue
comm=$(tr -d '\r\n' <"$d/comm" 2>/dev/null)
[ "$comm" = "$name" ] || continue

pid=${d#/proc/}
case "$pid" in
''|*[!0-9]*)
continue
;;
esac

if [ -z "$first_pid" ] || [ "$pid" -lt "$first_pid" ]; then
first_pid="$pid"
fi

if [ -z "$pids" ]; then
pids="$pid"
else
pids="$pids $pid"
fi
done

[ -n "$pids" ] || return 1

if [ "$mode" = "all" ]; then
printf '%s\n' "$pids"
else
printf '%s\n' "$first_pid"
fi

# Final fallback: ps
pid=$(ps -e 2>/dev/null | awk -v n="$name" '$NF==n {print $1; exit}')
pid=$(sanitize_pid "$pid")
[ -n "$pid" ] && { printf '%s\n' "$pid"; return 0; }

return 1
}

# Wait until PID is alive (kill -0 succeeds) or timeout seconds elapse.
Expand Down Expand Up @@ -4441,52 +4475,49 @@ kill_process() {
}

is_process_running() {
if [ -z "$1" ]; then
log_info "Usage: is_running <process_name_or_pid>"
return 1
fi
name="$1"

input="$1"
case "$input" in
''|*[!0-9]*)
# Non-numeric input: treat as process name
found=0
pids=$(get_one_pid_by_name "$name" all 2>/dev/null) || {
log_info "Process '$name' is not running."
return 1
}

# Prefer pgrep if available (ShellCheck-friendly, efficient)
if command -v pgrep >/dev/null 2>&1; then
if pgrep -x "$input" >/dev/null 2>&1; then
found=1
fi
else
# POSIX fallback: avoid 'ps | grep' to silence SC2009
# Match as a separate word to mimic 'grep -w'
if ps -e 2>/dev/null | awk -v name="$input" '
$0 ~ ("(^|[[:space:]])" name "([[:space:]]|$)") { exit 0 }
END { exit 1 }
'; then
found=1
fi
fi
log_info "Process '$name' is running."

if [ "$found" -eq 1 ]; then
log_info "Process '$input' is running."
# Only add extra debug if multiple instances exist
case "$pids" in
*" "*)
log_info "Process '$name' instances: $pids"
;;
*)
return 0
else
log_info "Process '$input' is not running."
return 1
;;
esac

for pid in $pids; do
case "$pid" in
''|*[!0-9]*)
continue
;;
esac

cmd=""
if [ -r "/proc/$pid/cmdline" ]; then
cmd=$(tr '\000' ' ' <"/proc/$pid/cmdline" 2>/dev/null)
fi
;;
*)
# Numeric input: treat as PID
if kill -0 "$input" 2>/dev/null; then
log_info "Process with PID $input is running."
return 0

if [ -n "$cmd" ]; then
log_info "Process '$name' PID $pid cmdline: $cmd"
else
log_info "Process with PID $input is not running."
return 1
comm=""
if [ -r "/proc/$pid/comm" ]; then
comm=$(tr -d '\r\n' <"/proc/$pid/comm" 2>/dev/null)
fi
log_info "Process '$name' PID $pid comm: ${comm:-unknown}"
fi
;;
esac
done

return 0
}

get_pid() {
Expand Down
Loading