Skip to content

Commit 10fa4bd

Browse files
committed
functestlib: expand ensure_reasonable_clock for no-network systems
- Log timedatectl status/show-timesync/timesync-status outputs when epoch detected - Log systemctl status for systemd-timesyncd for debug - Try RTC restore (hwclock / rtc0 since_epoch) before falling back - Seed time from kernel build timestamp (uname -v / /proc/version) if available - Continue gracefully if clock remains invalid Signed-off-by: Srikanth Muppandam <smuppand@qti.qualcomm.com>
1 parent d5f5ec3 commit 10fa4bd

File tree

1 file changed

+177
-14
lines changed

1 file changed

+177
-14
lines changed

Runner/utils/functestlib.sh

Lines changed: 177 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -293,28 +293,191 @@ ensure_reasonable_clock() {
293293
now="$(date +%s 2>/dev/null || echo 0)"
294294
cutoff="$(date -d '2020-01-01 UTC' +%s 2>/dev/null || echo 1577836800)"
295295
[ -z "$cutoff" ] && cutoff=1577836800
296+
296297
[ "$now" -ge "$cutoff" ] 2>/dev/null && return 0
297298

298-
log_warn "System clock looks invalid (epoch=$now). Attempting quick time sync..."
299+
log_warn "System clock looks invalid (epoch=$now). Trying local time sources (no network)..."
300+
301+
# Optional diagnostics file (caller may set this, e.g. CLOCK_DIAG_FILE="$OUT_DIR/clock_diag.txt")
302+
diag_file="${CLOCK_DIAG_FILE:-}"
303+
304+
# ---- Diagnostics (only when invalid clock) ----
305+
if [ -n "$diag_file" ]; then
306+
{
307+
echo "==== CLOCK DIAGNOSTICS ===="
308+
echo "timestamp=$(date '+%Y-%m-%d %H:%M:%S' 2>/dev/null || true)"
309+
echo "epoch_now=$now cutoff=$cutoff"
310+
echo
311+
} >>"$diag_file" 2>/dev/null || true
312+
fi
313+
314+
# Log minimal summaries to stdout; write full outputs to diag file.
315+
# date summary
316+
date_out="$(date 2>&1 || true)"
317+
if [ -n "$diag_file" ]; then
318+
{
319+
echo "---- date ----"
320+
echo "$date_out"
321+
echo
322+
} >>"$diag_file" 2>/dev/null || true
323+
fi
324+
log_info "date: $(printf '%s' "$date_out" | head -n 1)"
325+
326+
# timedatectl summaries + full dump
299327
if command -v timedatectl >/dev/null 2>&1; then
300-
timedatectl set-ntp true 2>/dev/null || true
328+
td_status="$(timedatectl status 2>&1 || true)"
329+
if [ -n "$diag_file" ]; then
330+
{
331+
echo "---- timedatectl status ----"
332+
echo "$td_status"
333+
echo
334+
} >>"$diag_file" 2>/dev/null || true
335+
fi
336+
337+
# minimal, stable-ish summaries
338+
td_local="$(printf '%s\n' "$td_status" | sed -n 's/^[[:space:]]*Local time:[[:space:]]*//p' | head -n 1)"
339+
td_sync="$(printf '%s\n' "$td_status" | sed -n 's/^[[:space:]]*System clock synchronized:[[:space:]]*//p' | head -n 1)"
340+
td_ntp="$(printf '%s\n' "$td_status" | sed -n 's/^[[:space:]]*NTP service:[[:space:]]*//p' | head -n 1)"
341+
td_rtc="$(printf '%s\n' "$td_status" | sed -n 's/^[[:space:]]*RTC time:[[:space:]]*//p' | head -n 1)"
342+
343+
[ -n "$td_local" ] && log_info "timedatectl: Local time: $td_local"
344+
[ -n "$td_rtc" ] && log_info "timedatectl: RTC time: $td_rtc"
345+
[ -n "$td_sync" ] && log_info "timedatectl: System clock synchronized: $td_sync"
346+
[ -n "$td_ntp" ] && log_info "timedatectl: NTP service: $td_ntp"
347+
348+
td_show="$(timedatectl show-timesync --all 2>&1 || true)"
349+
if [ -n "$diag_file" ]; then
350+
{
351+
echo "---- timedatectl show-timesync --all ----"
352+
echo "$td_show"
353+
echo
354+
} >>"$diag_file" 2>/dev/null || true
355+
fi
356+
357+
td_server="$(printf '%s\n' "$td_show" | sed -n 's/^ServerName=//p' | head -n 1)"
358+
td_addr="$(printf '%s\n' "$td_show" | sed -n 's/^ServerAddress=//p' | head -n 1)"
359+
td_sysntp="$(printf '%s\n' "$td_show" | sed -n 's/^SystemNTPServers=//p' | head -n 1)"
360+
td_fallback="$(printf '%s\n' "$td_show" | sed -n 's/^FallbackNTPServers=//p' | head -n 1)"
361+
362+
if [ -n "$td_server" ] || [ -n "$td_addr" ]; then
363+
log_info "timesync: server=${td_server:-NA} addr=${td_addr:-NA}"
364+
fi
365+
if [ -n "$td_sysntp" ]; then
366+
# Keep it short
367+
short_sysntp="$(printf '%s' "$td_sysntp" | awk '{print $1" "$2" "$3" "$4}')"
368+
log_info "timesync: SystemNTPServers: ${short_sysntp:-NA}"
369+
elif [ -n "$td_fallback" ]; then
370+
short_fb="$(printf '%s' "$td_fallback" | awk '{print $1" "$2" "$3" "$4}')"
371+
log_info "timesync: FallbackNTPServers: ${short_fb:-NA}"
372+
fi
373+
374+
td_ts="$(timedatectl timesync-status 2>&1 || true)"
375+
if [ -n "$diag_file" ]; then
376+
{
377+
echo "---- timedatectl timesync-status ----"
378+
echo "$td_ts"
379+
echo
380+
} >>"$diag_file" 2>/dev/null || true
381+
fi
382+
383+
td_pkt="$(printf '%s\n' "$td_ts" | sed -n 's/^[[:space:]]*Packet count:[[:space:]]*//p' | head -n 1)"
384+
td_srvline="$(printf '%s\n' "$td_ts" | sed -n 's/^[[:space:]]*Server:[[:space:]]*//p' | head -n 1)"
385+
[ -n "$td_srvline" ] && log_info "timesync-status: Server: $td_srvline"
386+
[ -n "$td_pkt" ] && log_info "timesync-status: Packet count: $td_pkt"
387+
else
388+
log_info "timedatectl: not available"
389+
if [ -n "$diag_file" ]; then
390+
echo "timedatectl: not available" >>"$diag_file" 2>/dev/null || true
391+
fi
301392
fi
302-
grace=25
303-
start="$(date +%s 2>/dev/null || echo 0)"
304-
end=$((start + grace))
305-
while :; do
306-
cur="$(date +%s 2>/dev/null || echo 0)"
307-
if [ "$cur" -ge "$cutoff" ] 2>/dev/null; then
308-
log_pass "Clock synchronized."
393+
394+
# systemctl status summaries + full dump
395+
if command -v systemctl >/dev/null 2>&1; then
396+
sdts="$(systemctl status systemd-timesyncd --no-pager --full 2>&1 || true)"
397+
if [ -n "$diag_file" ]; then
398+
{
399+
echo "---- systemctl status systemd-timesyncd ----"
400+
echo "$sdts"
401+
echo
402+
} >>"$diag_file" 2>/dev/null || true
403+
fi
404+
405+
sd_active="$(printf '%s\n' "$sdts" | sed -n 's/^[[:space:]]*Active:[[:space:]]*//p' | head -n 1)"
406+
sd_pid="$(printf '%s\n' "$sdts" | sed -n 's/^[[:space:]]*Main PID:[[:space:]]*//p' | head -n 1)"
407+
[ -n "$sd_active" ] && log_info "systemd-timesyncd: Active: $sd_active"
408+
[ -n "$sd_pid" ] && log_info "systemd-timesyncd: Main PID: $sd_pid"
409+
else
410+
log_info "systemctl: not available"
411+
if [ -n "$diag_file" ]; then
412+
echo "systemctl: not available" >>"$diag_file" 2>/dev/null || true
413+
fi
414+
fi
415+
416+
# 1) Try RTC (if it is sane)
417+
if command -v hwclock >/dev/null 2>&1 && [ -e /dev/rtc0 ]; then
418+
hwclock -s 2>/dev/null || true
419+
now="$(date +%s 2>/dev/null || echo 0)"
420+
if [ "$now" -ge "$cutoff" ] 2>/dev/null; then
421+
log_pass "Clock restored from RTC."
309422
return 0
310423
fi
311-
if [ "$cur" -ge "$end" ] 2>/dev/null; then
312-
break
424+
425+
if [ -r /sys/class/rtc/rtc0/since_epoch ]; then
426+
rtc_epoch="$(tr -cd 0-9 < /sys/class/rtc/rtc0/since_epoch 2>/dev/null)"
427+
if [ -n "$rtc_epoch" ]; then
428+
log_info "rtc0: since_epoch=$rtc_epoch"
429+
if [ -n "$diag_file" ]; then
430+
echo "rtc0: since_epoch=$rtc_epoch" >>"$diag_file" 2>/dev/null || true
431+
fi
432+
fi
433+
if [ -n "$rtc_epoch" ] && [ "$rtc_epoch" -ge "$cutoff" ] 2>/dev/null; then
434+
if date -d "@$rtc_epoch" >/dev/null 2>&1; then
435+
date -s "@$rtc_epoch" >/dev/null 2>&1 || true
436+
fi
437+
now="$(date +%s 2>/dev/null || echo 0)"
438+
if [ "$now" -ge "$cutoff" ] 2>/dev/null; then
439+
log_pass "Clock restored from rtc0 since_epoch."
440+
return 0
441+
fi
442+
fi
313443
fi
314-
sleep 1
315-
done
444+
else
445+
log_info "RTC: hwclock or /dev/rtc0 not available"
446+
if [ -n "$diag_file" ]; then
447+
echo "RTC: hwclock or /dev/rtc0 not available" >>"$diag_file" 2>/dev/null || true
448+
fi
449+
fi
450+
451+
# 2) Try kernel build timestamp from /proc/version or uname -v
452+
kb="$(uname -v 2>/dev/null || cat /proc/version 2>/dev/null || true)"
453+
if [ -n "$diag_file" ]; then
454+
{
455+
echo "---- kernel version string ----"
456+
echo "$kb"
457+
echo
458+
} >>"$diag_file" 2>/dev/null || true
459+
fi
460+
461+
kb_date="$(printf '%s\n' "$kb" | sed -n \
462+
's/.*\([A-Z][a-z][a-z] [A-Z][a-z][a-z] [ 0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [A-Z][A-Za-z0-9+:-]* [0-9][0-9][0-9][0-9]\).*/\1/p' \
463+
| head -n 1)"
464+
465+
if [ -n "$kb_date" ]; then
466+
log_info "kernel-build-time: parsed='$kb_date'"
467+
kb_epoch="$(date -d "$kb_date" +%s 2>/dev/null || echo 0)"
468+
if [ "$kb_epoch" -ge "$cutoff" ] 2>/dev/null; then
469+
date -s "$kb_date" >/dev/null 2>&1 || true
470+
now="$(date +%s 2>/dev/null || echo 0)"
471+
if [ "$now" -ge "$cutoff" ] 2>/dev/null; then
472+
log_pass "Clock seeded from kernel build time: $kb_date"
473+
return 0
474+
fi
475+
fi
476+
else
477+
log_info "kernel-build-time: could not parse from uname -v / /proc/version"
478+
fi
316479

317-
log_warn "Clock still invalid; TLS downloads may fail. Treating as limited network."
480+
log_warn "Clock still invalid; continuing (timestamps may be epoch)."
318481
return 1
319482
}
320483

0 commit comments

Comments
 (0)