Skip to content

Commit b6254c1

Browse files
committed
fix(weston): harden start/stop and wayland socket discovery for base/prop
Keep weston_is_running() process-based; sockets are used only for diagnostics. Improve find_wayland_sockets() to support base (/run/user/$uid/wayland-*) and prop (/dev/socket/weston*), plus /run/wayland-*. Make weston_stop() and weston_start() more robust for CI (bounded retries, systemd + direct spawn fallback). Removed duplicate entry of weston_start() Signed-off-by: Srikanth Muppandam <smuppand@qti.qualcomm.com>
1 parent 8322bc9 commit b6254c1

1 file changed

Lines changed: 119 additions & 191 deletions

File tree

Runner/utils/functestlib.sh

Lines changed: 119 additions & 191 deletions
Original file line numberDiff line numberDiff line change
@@ -862,94 +862,136 @@ check_tar_file() {
862862

863863
# Return space-separated PIDs for 'weston' (BusyBox friendly).
864864
weston_pids() {
865-
pids=""
865+
# Print weston PIDs (space-separated). Empty if none.
866866
if command -v pgrep >/dev/null 2>&1; then
867-
pids="$(pgrep -x weston 2>/dev/null || true)"
868-
fi
869-
if [ -z "$pids" ]; then
870-
pids="$(ps -eo pid,comm 2>/dev/null | awk '$2=="weston"{print $1}')"
867+
pgrep -x weston 2>/dev/null | tr '\n' ' ' | sed 's/[[:space:]]*$//'
868+
return 0
871869
fi
872-
echo "$pids"
870+
871+
# Fallback: ps (avoid matching grep itself)
872+
# shellcheck disable=SC2009
873+
ps 2>/dev/null | awk '
874+
$0 ~ /[[:space:]]weston([[:space:]]|$)/ { print $1 }
875+
' | tr '\n' ' ' | sed 's/[[:space:]]*$//'
876+
return 0
873877
}
874878

875879
# Is Weston running?
876880
weston_is_running() {
877-
[ -n "$(weston_pids)" ]
881+
# Process-based ONLY (do not use socket existence)
882+
pids="$(weston_pids)"
883+
[ -n "$pids" ]
878884
}
879885

880886
# Stop all Weston processes
881887
weston_stop() {
882-
if weston_is_running; then
883-
log_info "Stopping Weston..."
884-
pkill -x weston
885-
for i in $(seq 1 10); do
886-
log_info "Waiting for Weston to stop with $i attempt "
887-
if ! weston_is_running; then
888-
log_info "Weston stopped successfully"
889-
return 0
890-
fi
891-
sleep 1
888+
if ! weston_is_running; then
889+
log_info "Weston is not running."
890+
# Still cleanup stale sockets (prevents false positives elsewhere)
891+
weston_cleanup_stale_sockets
892+
return 0
893+
fi
894+
895+
log_info "Stopping Weston..."
896+
# First try graceful
897+
if command -v pkill >/dev/null 2>&1; then
898+
pkill -TERM -x weston >/dev/null 2>&1 || true
899+
elif command -v killall >/dev/null 2>&1; then
900+
killall weston >/dev/null 2>&1 || true
901+
else
902+
# Last resort: kill by PID list
903+
for p in $(weston_pids); do
904+
kill "$p" >/dev/null 2>&1 || true
892905
done
893-
log_error "Failed to stop Weston after waiting."
894-
return 1
906+
fi
907+
908+
i=1
909+
while [ "$i" -le 10 ]; do
910+
log_info "Waiting for Weston to stop with $i attempt"
911+
if ! weston_is_running; then
912+
# Clean up stale socket files that may remain after exit
913+
weston_cleanup_stale_sockets
914+
log_info "Weston stopped successfully"
915+
return 0
916+
fi
917+
sleep 1
918+
i=$((i + 1))
919+
done
920+
921+
log_warn "Weston still running after grace period; forcing kill..."
922+
923+
if command -v pkill >/dev/null 2>&1; then
924+
pkill -KILL -x weston >/dev/null 2>&1 || true
895925
else
896-
log_info "Weston is not running."
926+
for p in $(weston_pids); do
927+
kill -9 "$p" >/dev/null 2>&1 || true
928+
done
929+
fi
930+
931+
sleep 1
932+
933+
if weston_is_running; then
934+
log_error "Failed to stop Weston (still running: $(weston_pids))"
935+
return 1
897936
fi
937+
938+
weston_cleanup_stale_sockets
939+
log_info "Weston stopped successfully (forced)"
898940
return 0
899941
}
900-
901942
# Start weston with correct env if not running
902943
weston_start() {
903944
if weston_is_running; then
904-
log_info "Weston already running."
945+
log_info "Weston already running (PID(s): $(weston_pids))."
905946
return 0
906947
fi
907-
948+
949+
# If stale sockets exist from previous crash/kill, remove them first
950+
weston_cleanup_stale_sockets
951+
908952
if command -v systemctl >/dev/null 2>&1; then
909953
log_info "Attempting to start via systemd: weston.service"
910954
systemctl start weston.service >/dev/null 2>&1 || true
911955
sleep 1
912956
if weston_is_running; then
913-
log_info "Weston started via systemd (weston.service)."
957+
log_info "Weston started via systemd (weston.service) (PID(s): $(weston_pids))."
914958
return 0
915959
fi
916-
960+
917961
log_info "Attempting to start via systemd: weston@.service"
918962
systemctl start weston@.service >/dev/null 2>&1 || true
919963
sleep 1
920964
if weston_is_running; then
921-
log_info "Weston started via systemd (weston@.service)."
965+
log_info "Weston started via systemd (weston@.service) (PID(s): $(weston_pids))."
922966
return 0
923967
fi
924-
968+
925969
log_warn "systemd start did not bring Weston up; will try direct spawn."
926970
fi
927-
928-
# Minimal-friendly direct spawn (no headless module guesses here).
971+
929972
ensure_xdg_runtime_dir
930-
973+
931974
if ! command -v weston >/dev/null 2>&1; then
932975
log_fail "weston binary not found in PATH."
933976
return 1
934977
fi
935-
978+
936979
log_info "Attempting to spawn Weston (no backend override). Log: /tmp/weston.self.log"
937980
( nohup weston --log=/tmp/weston.self.log >/dev/null 2>&1 & ) || true
938-
981+
939982
tries=0
940-
while [ $tries -lt 5 ]; do
983+
while [ "$tries" -lt 10 ]; do
941984
if weston_is_running; then
942985
log_info "Weston is now running (PID(s): $(weston_pids))."
943-
return 0
944-
fi
945-
if [ -n "$(find_wayland_sockets | head -n1)" ]; then
946-
log_info "A Wayland socket appeared after spawn."
986+
# Optional: show socket for debug (base/prop both)
987+
sock="$(find_wayland_sockets | head -n 1)"
988+
[ -n "$sock" ] && log_info "Wayland socket: $sock"
947989
return 0
948990
fi
949991
sleep 1
950-
tries=$((tries+1))
992+
tries=$((tries + 1))
951993
done
952-
994+
953995
if [ -f /tmp/weston.self.log ]; then
954996
log_warn "Weston spawn failed; last log lines:"
955997
tail -n 20 /tmp/weston.self.log 2>/dev/null | sed 's/^/[weston.log] /' || true
@@ -1081,128 +1123,6 @@ wayland_pick_socket() {
10811123
return 1
10821124
}
10831125

1084-
# ---- Wayland/Weston helpers -----------------------
1085-
# Ensure a private XDG runtime directory exists and is usable (0700).
1086-
weston_start() {
1087-
# Already up?
1088-
if weston_is_running; then
1089-
log_info "Weston already running."
1090-
return 0
1091-
fi
1092-
1093-
# 1) Try systemd user/system units if present
1094-
if command -v systemctl >/dev/null 2>&1; then
1095-
for unit in weston.service weston@.service; do
1096-
log_info "Attempting to start via systemd: $unit"
1097-
systemctl start "$unit" >/dev/null 2>&1 || true
1098-
sleep 1
1099-
if weston_is_running; then
1100-
log_info "Weston started via $unit."
1101-
return 0
1102-
fi
1103-
done
1104-
log_warn "systemd start did not bring Weston up; will try direct spawn."
1105-
fi
1106-
1107-
# Helper: attempt spawn for a given uid (empty => current user)
1108-
# Tries multiple backend names (to cover distro/plugin differences)
1109-
# Returns 0 if a weston process + socket appears, else non-zero.
1110-
spawn_weston_try() {
1111-
target_uid="$1" # "" or numeric uid
1112-
backends="${WESTON_BACKENDS:-headless headless-backend.so}"
1113-
1114-
# Prepare runtime dir
1115-
if [ -n "$target_uid" ]; then
1116-
run_dir="/run/user/$target_uid"
1117-
mkdir -p "$run_dir" 2>/dev/null || true
1118-
chown "$target_uid:$target_uid" "$run_dir" 2>/dev/null || true
1119-
else
1120-
ensure_xdg_runtime_dir
1121-
run_dir="$XDG_RUNTIME_DIR"
1122-
fi
1123-
chmod 700 "$run_dir" 2>/dev/null || true
1124-
1125-
# Where to log
1126-
log_file="/tmp/weston.${target_uid:-self}.log"
1127-
rm -f "$log_file" 2>/dev/null || true
1128-
1129-
for be in $backends; do
1130-
log_info "Spawning weston (uid=${target_uid:-$(id -u)}) with backend='$be' …"
1131-
if ! command -v weston >/dev/null 2>&1; then
1132-
log_fail "weston binary not found in PATH."
1133-
return 1
1134-
fi
1135-
1136-
# Build the command: avoid optional modules that may not exist on minimal builds
1137-
cmd="XDG_RUNTIME_DIR='$run_dir' weston --backend='$be' --log='$log_file'"
1138-
1139-
if [ -n "$target_uid" ]; then
1140-
# Run as that uid if we can
1141-
if command -v su >/dev/null 2>&1; then
1142-
su -s /bin/sh -c "$cmd >/dev/null 2>&1 &" "#$target_uid" || true
1143-
elif command -v runuser >/dev/null 2>&1; then
1144-
runuser -u "#$target_uid" -- sh -c "$cmd >/dev/null 2>&1 &" || true
1145-
else
1146-
log_warn "No su/runuser available to switch uid=$target_uid; skipping this mode."
1147-
continue
1148-
fi
1149-
else
1150-
# Current user
1151-
( nohup sh -c "$cmd" >/dev/null 2>&1 & ) || true
1152-
fi
1153-
1154-
# Wait up to ~5s for process + a socket to appear
1155-
tries=0
1156-
while [ $tries -lt 5 ]; do
1157-
if weston_is_running; then
1158-
# See if a fresh socket is visible
1159-
sock="$(wayland_pick_socket)"
1160-
if [ -n "$sock" ]; then
1161-
log_info "Weston up (backend=$be). Socket: $sock"
1162-
return 0
1163-
fi
1164-
fi
1165-
sleep 1
1166-
tries=$((tries+1))
1167-
done
1168-
1169-
# Show weston log tail to aid debugging
1170-
if [ -r "$log_file" ]; then
1171-
log_warn "Weston did not come up with backend '$be'. Last log lines:"
1172-
tail -n 20 "$log_file" | sed 's/^/[weston.log] /'
1173-
else
1174-
log_warn "Weston did not come up with backend '$be' and no log file present ($log_file)."
1175-
fi
1176-
done
1177-
1178-
return 1
1179-
}
1180-
1181-
# 2) Try as current user
1182-
if spawn_weston_try ""; then
1183-
return 0
1184-
fi
1185-
1186-
# 3) Try as 'weston' user (common on embedded images)
1187-
weston_uid=""
1188-
if command -v getent >/dev/null 2>&1; then
1189-
weston_uid="$(getent passwd weston 2>/dev/null | awk -F: '{print $3}')"
1190-
fi
1191-
[ -z "$weston_uid" ] && weston_uid="$(id -u weston 2>/dev/null || true)"
1192-
1193-
if [ -n "$weston_uid" ]; then
1194-
log_info "Attempting to spawn Weston as uid=$weston_uid (user 'weston')."
1195-
if spawn_weston_try "$weston_uid"; then
1196-
return 0
1197-
fi
1198-
else
1199-
log_info "No 'weston' user found; skipping user-switch spawn."
1200-
fi
1201-
1202-
log_warn "All weston spawn attempts failed."
1203-
return 1
1204-
}
1205-
12061126
# Return first Wayland socket under a base dir (prints path or fails).
12071127
find_wayland_socket_in() {
12081128
base="$1"
@@ -1412,38 +1332,46 @@ weston_pick_env_or_start() {
14121332

14131333
# Find candidate Wayland sockets in common locations.
14141334
# Prints absolute socket paths, one per line, most-preferred first.
1415-
find_wayland_sockets() {
1416-
uid="$(id -u 2>/dev/null || echo 0)"
1417-
1418-
if [ -n "${XDG_RUNTIME_DIR:-}" ] && [ -n "${WAYLAND_DISPLAY:-}" ] &&
1419-
[ -S "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" ]; then
1420-
echo "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY"
1421-
fi
1335+
weston_is_running() {
1336+
# Process-based ONLY (do not use socket existence)
1337+
pids="$(weston_pids)"
1338+
[ -n "$pids" ]
1339+
}
14221340

1423-
# Current uid
1424-
for f in "/run/user/$uid/wayland-0" "/run/user/$uid/wayland-1" "/run/user/$uid/wayland-2"; do
1425-
[ -S "$f" ] && echo "$f"
1341+
find_wayland_sockets() {
1342+
# Keep your base/prop behavior:
1343+
# - base: /run/user/$uid/wayland-*
1344+
# - prop: /dev/socket/weston (or /dev/socket/weston/wayland-*)
1345+
# - also allow /run/wayland-*
1346+
# NOTE: socket presence != process presence; use only for diagnostics.
1347+
for s in \
1348+
/dev/socket/weston \
1349+
/dev/socket/weston/wayland-* \
1350+
/run/wayland-* \
1351+
/run/user/*/wayland-* \
1352+
"${XDG_RUNTIME_DIR:-}"/wayland-* \
1353+
; do
1354+
[ -S "$s" ] && printf '%s\n' "$s"
14261355
done
1427-
for f in /run/user/"$uid"/wayland-*; do
1428-
[ -S "$f" ] && echo "$f"
1429-
done 2>/dev/null
1430-
1431-
# Any other user under /run/user (covers weston as uid 100, 1000, etc.)
1432-
for d in /run/user/*; do
1433-
[ -d "$d" ] || continue
1434-
[ "$d" = "/run/user/$uid" ] && continue # skip current uid, already handled above
1435-
for f in "$d"/wayland-*; do
1436-
[ -S "$f" ] && echo "$f"
1437-
done
1438-
done 2>/dev/null
1439-
1440-
for f in /dev/socket/weston/wayland-*; do
1441-
[ -S "$f" ] && echo "$f"
1442-
done 2>/dev/null
1443-
1444-
for f in /tmp/wayland-*; do
1445-
[ -S "$f" ] && echo "$f"
1446-
done 2>/dev/null
1356+
}
1357+
1358+
weston_cleanup_stale_sockets() {
1359+
# Remove stale sockets only if weston is NOT running.
1360+
weston_is_running && return 0
1361+
1362+
# Collect candidates (base + prop + common)
1363+
for s in \
1364+
/dev/socket/weston \
1365+
/dev/socket/weston/wayland-* \
1366+
/run/wayland-* \
1367+
/run/user/*/wayland-* \
1368+
"${XDG_RUNTIME_DIR:-}"/wayland-* \
1369+
; do
1370+
[ -S "$s" ] || continue
1371+
# Best-effort cleanup; ignore errors (permissions)
1372+
rm -f "$s" 2>/dev/null || true
1373+
done
1374+
return 0
14471375
}
14481376

14491377
# Ensure XDG_RUNTIME_DIR has owner=current-user and mode 0700.

0 commit comments

Comments
 (0)