-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathfunctestlib.sh
More file actions
executable file
·4615 lines (4059 loc) · 151 KB
/
functestlib.sh
File metadata and controls
executable file
·4615 lines (4059 loc) · 151 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause
# --- Logging helpers ---
log() {
level=$1
shift
echo "[$level] $(date '+%Y-%m-%d %H:%M:%S') - $*"
}
log_info() { log "INFO" "$@"; }
log_pass() { log "PASS" "$@"; }
log_fail() { log "FAIL" "$@"; }
log_error() { log "ERROR" "$@"; }
log_skip() { log "SKIP" "$@"; }
log_warn() { log "WARN" "$@"; }
# --- Kernel Log Collection ---
get_kernel_log() {
if command -v journalctl >/dev/null 2>&1; then
journalctl -k -b
elif command -v dmesg >/dev/null 2>&1; then
dmesg
elif [ -f /var/log/kern.log ]; then
cat /var/log/kern.log
else
log_warn "No kernel log source found"
return 1
fi
}
# Locate a kernel module (.ko) file by name
# Tries to find it under current kernel version first, then all module trees
find_kernel_module() {
module_name="$1"
kver=$(uname -r)
# Attempt to find module under the currently running kernel
module_path=$(find "/lib/modules/$kver" -name "${module_name}.ko" 2>/dev/null | head -n 1)
# If not found, search all available module directories
if [ -z "$module_path" ]; then
log_warn "Module not found under /lib/modules/$kver, falling back to full search in /lib/modules/"
module_path=$(find /lib/modules/ -name "${module_name}.ko" 2>/dev/null | head -n 1)
# Warn if found outside current kernel version
if [ -n "$module_path" ]; then
found_version=$(echo "$module_path" | cut -d'/' -f4)
if [ "$found_version" != "$kver" ]; then
log_warn "Found ${module_name}.ko under $found_version, not under current kernel ($kver)"
fi
fi
fi
echo "$module_path"
}
# Check if a kernel module is currently loaded
is_module_loaded() {
module_name="$1"
/sbin/lsmod | awk '{print $1}' | grep -q "^${module_name}$"
}
# load_kernel_module <path-to-ko> [params...]
# 1) If already loaded, no-op
# 2) Try insmod <ko> [params]
# 3) If that fails, try modprobe <modname> [params]
load_kernel_module() {
module_path="$1"; shift
params="$*"
module_name=$(basename "$module_path" .ko)
if is_module_loaded "$module_name"; then
log_info "Module $module_name is already loaded"
return 0
fi
if [ ! -f "$module_path" ]; then
log_error "Module file not found: $module_path"
# still try modprobe if it exists in modules directory
else
log_info "Loading module via insmod: $module_path $params"
if /sbin/insmod "$module_path" "$params" 2>insmod_err.log; then
log_info "Module $module_name loaded successfully via insmod"
return 0
else
log_warn "insmod failed: $(cat insmod_err.log)"
fi
fi
# fallback to modprobe
log_info "Falling back to modprobe $module_name $params"
if /sbin/modprobe "$module_name" "$params" 2>modprobe_err.log; then
log_info "Module $module_name loaded successfully via modprobe"
return 0
else
log_error "modprobe failed: $(cat modprobe_err.log)"
return 1
fi
}
# Remove a kernel module by name with optional forced removal
unload_kernel_module() {
module_name="$1"
force="$2"
if ! is_module_loaded "$module_name"; then
log_info "Module $module_name is not loaded, skipping unload"
return 0
fi
log_info "Attempting to remove module: $module_name"
if /sbin/rmmod "$module_name" 2>rmmod_err.log; then
log_info "Module $module_name removed via rmmod"
return 0
fi
log_warn "rmmod failed: $(cat rmmod_err.log)"
log_info "Trying modprobe -r as fallback"
if /sbin/modprobe -r "$module_name" 2>modprobe_err.log; then
log_info "Module $module_name removed via modprobe"
return 0
fi
log_warn "modprobe -r failed: $(cat modprobe_err.log)"
if [ "$force" = "true" ]; then
log_warn "Trying forced rmmod: $module_name"
if /sbin/rmmod -f "$module_name" 2>>rmmod_err.log; then
log_info "Module $module_name force removed"
return 0
else
log_error "Forced rmmod failed: $(cat rmmod_err.log)"
return 1
fi
fi
log_error "Unable to unload module: $module_name"
return 1
}
# --- Dependency check ---
check_dependencies() {
# Support both:
# check_dependencies date awk sed
# check_dependencies "$deps" where deps="date awk sed"
if [ "$#" -eq 1 ]; then
# Split the single string into args
# shellcheck disable=SC2086
set -- $1
fi
missing=0
missing_cmds=""
for cmd in "$@"; do
[ -n "$cmd" ] || continue
if ! command -v "$cmd" >/dev/null 2>&1; then
log_warn "Required command '$cmd' not found in PATH."
missing=1
missing_cmds="$missing_cmds $cmd"
fi
done
if [ "$missing" -ne 0 ]; then
testname="${TESTNAME:-UnknownTest}"
log_skip "$testname SKIP missing dependencies$missing_cmds"
if [ -n "${TESTNAME:-}" ]; then
echo "$TESTNAME SKIP" > "./$TESTNAME.res" 2>/dev/null || true
fi
# Default: exit like today. Allow opt-out for callers that want a return code.
if [ "${CHECK_DEPS_NO_EXIT:-0}" = "1" ]; then
return 1
fi
exit 0
fi
return 0
}
# --- Test case directory lookup ---
find_test_case_by_name() {
test_name=$1
base_dir="${__RUNNER_SUITES_DIR:-$ROOT_DIR/suites}"
# Only search under the SUITES directory!
testpath=$(find "$base_dir" -type d -iname "$test_name" -print -quit 2>/dev/null)
echo "$testpath"
}
find_test_case_bin_by_name() {
test_name=$1
base_dir="${__RUNNER_UTILS_BIN_DIR:-$ROOT_DIR/common}"
find "$base_dir" -type f -iname "$test_name" -print -quit 2>/dev/null
}
find_test_case_script_by_name() {
test_name=$1
base_dir="${__RUNNER_UTILS_BIN_DIR:-$ROOT_DIR/common}"
find "$base_dir" -type d -iname "$test_name" -print -quit 2>/dev/null
}
# Check each given kernel config is set to y/m in /proc/config.gz, logs result, returns 0/1.
check_kernel_config() {
cfgs=$1
for config_key in $cfgs; do
if command -v zgrep >/dev/null 2>&1; then
if zgrep -qE "^${config_key}=(y|m)" /proc/config.gz 2>/dev/null; then
log_pass "Kernel config $config_key is enabled"
else
log_fail "Kernel config $config_key is missing or not enabled"
return 1
fi
else
# Fallback if zgrep is unavailable
if gzip -dc /proc/config.gz 2>/dev/null | grep -qE "^${config_key}=(y|m)"; then
log_pass "Kernel config $config_key is enabled"
else
log_fail "Kernel config $config_key is missing or not enabled"
return 1
fi
fi
done
return 0
}
check_dt_nodes() {
node_paths="$1"
log_info "$node_paths"
found=false
for node in $node_paths; do
log_info "$node"
if [ -d "$node" ] || [ -f "$node" ]; then
log_pass "Device tree node exists: $node"
found=true
fi
done
if [ "$found" = true ]; then
return 0
else
log_fail "Device tree node(s) missing: $node_paths"
return 1
fi
}
check_driver_loaded() {
drivers="$1"
for driver in $drivers; do
if [ -z "$driver" ]; then
log_fail "No driver/module name provided to check_driver_loaded"
return 1
fi
if grep -qw "$driver" /proc/modules || lsmod | awk '{print $1}' | grep -qw "$driver"; then
log_pass "Driver/module '$driver' is loaded"
return 0
else
log_fail "Driver/module '$driver' is not loaded"
return 1
fi
done
}
# --- Optional: POSIX-safe repo root detector ---
detect_runner_root() {
path=$1
while [ "$path" != "/" ]; do
if [ -d "$path/suites" ]; then
echo "$path"
return
fi
path=$(dirname "$path")
done
echo ""
}
# ----------------------------
# Additional Utility Functions
# ----------------------------
# Function is to check for network connectivity status
check_network_status() {
echo "[INFO] Checking network connectivity..."
# Prefer the egress/source IP chosen by the routing table (most accurate).
ip_addr=$(ip -4 route get 1.1.1.1 2>/dev/null \
| awk 'NR==1{for(i=1;i<=NF;i++) if($i=="src"){print $(i+1); exit}}')
# Fallback: first global IPv4 on any UP interface (works even without a default route).
if [ -z "$ip_addr" ]; then
ip_addr=$(ip -o -4 addr show scope global up 2>/dev/null \
| awk 'NR==1{split($4,a,"/"); print a[1]}')
fi
if [ -n "$ip_addr" ]; then
echo "[PASS] Network is active. IP address: $ip_addr"
# Quick reachability probe (single ICMP). BusyBox-compatible flags.
if ping -c 1 -W 2 8.8.8.8 >/dev/null 2>&1; then
echo "[PASS] Internet is reachable."
return 0
else
echo "[WARN] Network active but no internet access."
return 2
fi
else
echo "[FAIL] No active network interface found."
return 1
fi
}
# --- Make sure system time is sane (TLS needs a sane clock) ---
ensure_reasonable_clock() {
now="$(date +%s 2>/dev/null || echo 0)"
cutoff="$(date -d '2020-01-01 UTC' +%s 2>/dev/null || echo 1577836800)"
[ -z "$cutoff" ] && cutoff=1577836800
[ "$now" -ge "$cutoff" ] 2>/dev/null && return 0
log_warn "System clock looks invalid (epoch=$now). Trying local time sources (no network)..."
# Optional diagnostics file (caller may set this, e.g. CLOCK_DIAG_FILE="$OUT_DIR/clock_diag.txt")
diag_file="${CLOCK_DIAG_FILE:-}"
# ---- Diagnostics (only when invalid clock) ----
if [ -n "$diag_file" ]; then
{
echo "==== CLOCK DIAGNOSTICS ===="
echo "timestamp=$(date '+%Y-%m-%d %H:%M:%S' 2>/dev/null || true)"
echo "epoch_now=$now cutoff=$cutoff"
echo
} >>"$diag_file" 2>/dev/null || true
fi
# Log minimal summaries to stdout; write full outputs to diag file.
# date summary
date_out="$(date 2>&1 || true)"
if [ -n "$diag_file" ]; then
{
echo "---- date ----"
echo "$date_out"
echo
} >>"$diag_file" 2>/dev/null || true
fi
log_info "date: $(printf '%s' "$date_out" | head -n 1)"
# timedatectl summaries + full dump
if command -v timedatectl >/dev/null 2>&1; then
td_status="$(timedatectl status 2>&1 || true)"
if [ -n "$diag_file" ]; then
{
echo "---- timedatectl status ----"
echo "$td_status"
echo
} >>"$diag_file" 2>/dev/null || true
fi
# minimal, stable-ish summaries
td_local="$(printf '%s\n' "$td_status" | sed -n 's/^[[:space:]]*Local time:[[:space:]]*//p' | head -n 1)"
td_sync="$(printf '%s\n' "$td_status" | sed -n 's/^[[:space:]]*System clock synchronized:[[:space:]]*//p' | head -n 1)"
td_ntp="$(printf '%s\n' "$td_status" | sed -n 's/^[[:space:]]*NTP service:[[:space:]]*//p' | head -n 1)"
td_rtc="$(printf '%s\n' "$td_status" | sed -n 's/^[[:space:]]*RTC time:[[:space:]]*//p' | head -n 1)"
[ -n "$td_local" ] && log_info "timedatectl: Local time: $td_local"
[ -n "$td_rtc" ] && log_info "timedatectl: RTC time: $td_rtc"
[ -n "$td_sync" ] && log_info "timedatectl: System clock synchronized: $td_sync"
[ -n "$td_ntp" ] && log_info "timedatectl: NTP service: $td_ntp"
td_show="$(timedatectl show-timesync --all 2>&1 || true)"
if [ -n "$diag_file" ]; then
{
echo "---- timedatectl show-timesync --all ----"
echo "$td_show"
echo
} >>"$diag_file" 2>/dev/null || true
fi
td_server="$(printf '%s\n' "$td_show" | sed -n 's/^ServerName=//p' | head -n 1)"
td_addr="$(printf '%s\n' "$td_show" | sed -n 's/^ServerAddress=//p' | head -n 1)"
td_sysntp="$(printf '%s\n' "$td_show" | sed -n 's/^SystemNTPServers=//p' | head -n 1)"
td_fallback="$(printf '%s\n' "$td_show" | sed -n 's/^FallbackNTPServers=//p' | head -n 1)"
if [ -n "$td_server" ] || [ -n "$td_addr" ]; then
log_info "timesync: server=${td_server:-NA} addr=${td_addr:-NA}"
fi
if [ -n "$td_sysntp" ]; then
# Keep it short
short_sysntp="$(printf '%s' "$td_sysntp" | awk '{print $1" "$2" "$3" "$4}')"
log_info "timesync: SystemNTPServers: ${short_sysntp:-NA}"
elif [ -n "$td_fallback" ]; then
short_fb="$(printf '%s' "$td_fallback" | awk '{print $1" "$2" "$3" "$4}')"
log_info "timesync: FallbackNTPServers: ${short_fb:-NA}"
fi
td_ts="$(timedatectl timesync-status 2>&1 || true)"
if [ -n "$diag_file" ]; then
{
echo "---- timedatectl timesync-status ----"
echo "$td_ts"
echo
} >>"$diag_file" 2>/dev/null || true
fi
td_pkt="$(printf '%s\n' "$td_ts" | sed -n 's/^[[:space:]]*Packet count:[[:space:]]*//p' | head -n 1)"
td_srvline="$(printf '%s\n' "$td_ts" | sed -n 's/^[[:space:]]*Server:[[:space:]]*//p' | head -n 1)"
[ -n "$td_srvline" ] && log_info "timesync-status: Server: $td_srvline"
[ -n "$td_pkt" ] && log_info "timesync-status: Packet count: $td_pkt"
else
log_info "timedatectl: not available"
if [ -n "$diag_file" ]; then
echo "timedatectl: not available" >>"$diag_file" 2>/dev/null || true
fi
fi
# systemctl status summaries + full dump
if command -v systemctl >/dev/null 2>&1; then
sdts="$(systemctl status systemd-timesyncd --no-pager --full 2>&1 || true)"
if [ -n "$diag_file" ]; then
{
echo "---- systemctl status systemd-timesyncd ----"
echo "$sdts"
echo
} >>"$diag_file" 2>/dev/null || true
fi
sd_active="$(printf '%s\n' "$sdts" | sed -n 's/^[[:space:]]*Active:[[:space:]]*//p' | head -n 1)"
sd_pid="$(printf '%s\n' "$sdts" | sed -n 's/^[[:space:]]*Main PID:[[:space:]]*//p' | head -n 1)"
[ -n "$sd_active" ] && log_info "systemd-timesyncd: Active: $sd_active"
[ -n "$sd_pid" ] && log_info "systemd-timesyncd: Main PID: $sd_pid"
else
log_info "systemctl: not available"
if [ -n "$diag_file" ]; then
echo "systemctl: not available" >>"$diag_file" 2>/dev/null || true
fi
fi
# 1) Try RTC (if it is sane)
if command -v hwclock >/dev/null 2>&1 && [ -e /dev/rtc0 ]; then
hwclock -s 2>/dev/null || true
now="$(date +%s 2>/dev/null || echo 0)"
if [ "$now" -ge "$cutoff" ] 2>/dev/null; then
log_pass "Clock restored from RTC."
return 0
fi
if [ -r /sys/class/rtc/rtc0/since_epoch ]; then
rtc_epoch="$(tr -cd 0-9 < /sys/class/rtc/rtc0/since_epoch 2>/dev/null)"
if [ -n "$rtc_epoch" ]; then
log_info "rtc0: since_epoch=$rtc_epoch"
if [ -n "$diag_file" ]; then
echo "rtc0: since_epoch=$rtc_epoch" >>"$diag_file" 2>/dev/null || true
fi
fi
if [ -n "$rtc_epoch" ] && [ "$rtc_epoch" -ge "$cutoff" ] 2>/dev/null; then
if date -d "@$rtc_epoch" >/dev/null 2>&1; then
date -s "@$rtc_epoch" >/dev/null 2>&1 || true
fi
now="$(date +%s 2>/dev/null || echo 0)"
if [ "$now" -ge "$cutoff" ] 2>/dev/null; then
log_pass "Clock restored from rtc0 since_epoch."
return 0
fi
fi
fi
else
log_info "RTC: hwclock or /dev/rtc0 not available"
if [ -n "$diag_file" ]; then
echo "RTC: hwclock or /dev/rtc0 not available" >>"$diag_file" 2>/dev/null || true
fi
fi
# 2) Try kernel build timestamp from /proc/version or uname -v
kb="$(uname -v 2>/dev/null || cat /proc/version 2>/dev/null || true)"
if [ -n "$diag_file" ]; then
{
echo "---- kernel version string ----"
echo "$kb"
echo
} >>"$diag_file" 2>/dev/null || true
fi
kb_date="$(printf '%s\n' "$kb" | sed -n \
'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' \
| head -n 1)"
if [ -n "$kb_date" ]; then
log_info "kernel-build-time: parsed='$kb_date'"
kb_epoch="$(date -d "$kb_date" +%s 2>/dev/null || echo 0)"
if [ "$kb_epoch" -ge "$cutoff" ] 2>/dev/null; then
date -s "$kb_date" >/dev/null 2>&1 || true
now="$(date +%s 2>/dev/null || echo 0)"
if [ "$now" -ge "$cutoff" ] 2>/dev/null; then
log_pass "Clock seeded from kernel build time: $kb_date"
return 0
fi
fi
else
log_info "kernel-build-time: could not parse from uname -v / /proc/version"
fi
log_warn "Clock still invalid; continuing (timestamps may be epoch)."
return 1
}
# If the tar file already exists,then function exit. Otherwise function to check the network connectivity and it will download tar from internet.
extract_tar_from_url() {
url="$1"
outdir="${LOG_DIR:-.}"
mkdir -p "$outdir" 2>/dev/null || true
case "$url" in
/*)
tarfile="$url"
;;
file://*)
tarfile="${url#file://}"
;;
*)
tarfile="$outdir/$(basename "$url")"
;;
esac
markfile="${tarfile}.extracted"
skip_sentinel="${outdir}/.asset_fetch_skipped"
# If a previous run already marked "assets unavailable", honor it and SKIP.
if [ -f "$skip_sentinel" ]; then
log_info "Previous run marked assets unavailable on this system (${skip_sentinel}); skipping download."
return 2
fi
tar_already_extracted() {
tf="$1"
od="$2"
if [ -f "${tf}.extracted" ]; then
return 0
fi
tmp_list="${od}/.tar_ls.$$"
if tar -tf "$tf" 2>/dev/null | head -n 20 > "$tmp_list"; then
total=0
present=0
while IFS= read -r ent; do
[ -z "$ent" ] && continue
total=$((total + 1))
ent="${ent%/}"
if [ -e "$od/$ent" ] || [ -e "$od/$(basename "$ent")" ]; then
present=$((present + 1))
fi
done < "$tmp_list"
rm -f "$tmp_list" 2>/dev/null || true
if [ "$present" -ge 3 ]; then
return 0
fi
if [ "$total" -gt 0 ] && [ $((present * 100 / total)) -ge 50 ]; then
return 0
fi
fi
return 1
}
if command -v check_tar_file >/dev/null 2>&1; then
check_tar_file "$url"
status=$?
else
if [ -f "$tarfile" ]; then
if tar_already_extracted "$tarfile" "$outdir"; then
status=0
else
status=2
fi
else
status=1
fi
fi
ensure_reasonable_clock || {
log_warn "Proceeding in limited-network mode."
limited_net=1
}
is_busybox_wget() {
if command -v wget >/dev/null 2>&1; then
if wget --help 2>&1 | head -n 1 | grep -qi busybox; then
return 0
fi
fi
return 1
}
tls_capable_fetcher_available() {
scheme_https=0
case "$url" in
https://*)
scheme_https=1
;;
esac
if [ "$scheme_https" -eq 0 ]; then
return 0
fi
if command -v curl >/dev/null 2>&1; then
if curl -V 2>/dev/null | grep -qiE 'ssl|tls'; then
return 0
fi
fi
if command -v aria2c >/dev/null 2>&1; then
return 0
fi
if command -v wget >/dev/null 2>&1; then
if ! is_busybox_wget; then
return 0
fi
if command -v openssl >/dev/null 2>&1; then
return 0
fi
fi
return 1
}
try_download() {
src="$1"
dst="$2"
part="${dst}.part.$$"
ca=""
for cand in \
/etc/ssl/certs/ca-certificates.crt \
/etc/ssl/cert.pem \
/system/etc/security/cacerts/ca-certificates.crt
do
if [ -r "$cand" ]; then
ca="$cand"
break
fi
done
if command -v curl >/dev/null 2>&1; then
if [ -n "$ca" ]; then
curl -4 -L --fail --retry 3 --retry-delay 2 --connect-timeout 10 \
-o "$part" --cacert "$ca" "$src"
else
curl -4 -L --fail --retry 3 --retry-delay 2 --connect-timeout 10 \
-o "$part" "$src"
fi
rc=$?
if [ $rc -eq 0 ]; then
mv -f "$part" "$dst" 2>/dev/null || true
return 0
fi
rm -f "$part" 2>/dev/null || true
case "$rc" in
60|35|22)
return 60
;;
esac
fi
if command -v aria2c >/dev/null 2>&1; then
aria2c -x4 -s4 -m3 --connect-timeout=10 \
-o "$(basename "$part")" --dir="$(dirname "$part")" "$src"
rc=$?
if [ $rc -eq 0 ]; then
mv -f "$part" "$dst" 2>/dev/null || true
return 0
fi
rm -f "$part" 2>/dev/null || true
fi
if command -v wget >/dev/null 2>&1; then
if is_busybox_wget; then
wget -O "$part" -T 15 "$src"
rc=$?
if [ $rc -ne 0 ]; then
log_warn "BusyBox wget failed (rc=$rc); final attempt with --no-check-certificate."
wget -O "$part" -T 15 --no-check-certificate "$src"
rc=$?
fi
if [ $rc -eq 0 ]; then
mv -f "$part" "$dst" 2>/dev/null || true
return 0
fi
rm -f "$part" 2>/dev/null || true
return 60
else
if [ -n "$ca" ]; then
wget -4 --timeout=15 --tries=3 --ca-certificate="$ca" -O "$part" "$src"
rc=$?
else
wget -4 --timeout=15 --tries=3 -O "$part" "$src"
rc=$?
fi
if [ $rc -ne 0 ]; then
log_warn "wget failed (rc=$rc); final attempt with --no-check-certificate."
wget -4 --timeout=15 --tries=1 --no-check-certificate -O "$part" "$src"
rc=$?
fi
if [ $rc -eq 0 ]; then
mv -f "$part" "$dst" 2>/dev/null || true
return 0
fi
rm -f "$part" 2>/dev/null || true
if [ $rc -eq 5 ]; then
return 60
fi
return $rc
fi
fi
return 127
}
if [ "$status" -eq 0 ]; then
log_info "Already extracted. Skipping download."
return 0
fi
if [ "$status" -eq 2 ]; then
log_info "File exists and is valid, but not yet extracted. Proceeding to extract."
else
case "$url" in
/*|file://*)
if [ ! -f "$tarfile" ]; then
log_fail "Local tar file not found: $tarfile"
return 1
fi
;;
*)
if [ ! -f "$tarfile" ] || [ ! -s "$tarfile" ]; then
prestage_dirs=""
if [ -n "${ASSET_DIR:-}" ]; then prestage_dirs="$prestage_dirs $ASSET_DIR"; fi
if [ -n "${VIDEO_ASSET_DIR:-}" ]; then prestage_dirs="$prestage_dirs $VIDEO_ASSET_DIR"; fi
if [ -n "${AUDIO_ASSET_DIR:-}" ]; then prestage_dirs="$prestage_dirs $AUDIO_ASSET_DIR"; fi
prestage_dirs="$prestage_dirs . $outdir ${ROOT_DIR:-} ${ROOT_DIR:-}/cache /var/Runner /var/Runner/cache"
for d in $prestage_dirs; do
if [ -d "$d" ] && [ -f "$d/$(basename "$tarfile")" ]; then
log_info "Using pre-staged tarball: $d/$(basename "$tarfile")"
cp -f "$d/$(basename "$tarfile")" "$tarfile" 2>/dev/null || true
break
fi
done
if [ ! -s "$tarfile" ]; then
for top in /mnt /media; do
if [ -d "$top" ]; then
for d in "$top"/*; do
if [ -d "$d" ] && [ -f "$d/$(basename "$tarfile")" ]; then
log_info "Using pre-staged tarball: $d/$(basename "$tarfile")"
cp -f "$d/$(basename "$tarfile")" "$tarfile" 2>/dev/null || true
break 2
fi
done
fi
done
fi
fi
if [ ! -s "$tarfile" ]; then
if [ -n "$limited_net" ]; then
log_warn "Limited network, cannot fetch media bundle. Marking SKIP for callers."
: > "$skip_sentinel" 2>/dev/null || true
return 2
fi
if ! tls_capable_fetcher_available; then
log_warn "No TLS-capable downloader available on this minimal build, cannot fetch: $url"
log_warn "Pre-stage $(basename "$url") locally or use a file:// URL."
: > "$skip_sentinel" 2>/dev/null || true
return 2
fi
log_info "Downloading $url -> $tarfile"
if ! try_download "$url" "$tarfile"; then
rc=$?
if [ $rc -eq 60 ]; then
log_warn "TLS/handshake problem while downloading (cert/clock/firewall or minimal wget). Marking SKIP."
: > "$skip_sentinel" 2>/dev/null || true
return 2
fi
log_fail "Failed to download $(basename "$url")"
return 1
fi
fi
;;
esac
fi
log_info "Extracting $(basename "$tarfile")..."
if tar -xvf "$tarfile"; then
: > "$markfile" 2>/dev/null || true
# Clear the minimal/offline sentinel only if it exists (SC2015-safe)
if [ -f "$skip_sentinel" ]; then
rm -f "$skip_sentinel" 2>/dev/null || true
fi
first_entry="$(tar -tf "$tarfile" 2>/dev/null | head -n 1 | sed 's#/$##')"
if [ -n "$first_entry" ]; then
if [ -e "$first_entry" ] || [ -e "$outdir/$first_entry" ]; then
log_pass "Files extracted successfully ($(basename "$first_entry") present)."
return 0
fi
fi
log_warn "Extraction finished but couldn't verify entries. Assuming success."
return 0
fi
log_fail "Failed to extract $(basename "$tarfile")"
return 1
}
# Function to check if a tar file exists
check_tar_file() {
url="$1"
outdir="${LOG_DIR:-.}"
mkdir -p "$outdir" 2>/dev/null || true
case "$url" in
/*) tarfile="$url" ;;
file://*) tarfile="${url#file://}" ;;
*) tarfile="$outdir/$(basename "$url")" ;;
esac
markfile="${tarfile}.extracted"
# 1) Existence & basic validity
if [ ! -f "$tarfile" ]; then
log_info "File $(basename "$tarfile") does not exist in $outdir."
return 1
fi
if [ ! -s "$tarfile" ]; then
log_warn "File $(basename "$tarfile") exists but is empty."
return 1
fi
if ! tar -tf "$tarfile" >/dev/null 2>&1; then
log_warn "File $(basename "$tarfile") is not a valid tar archive."
return 1
fi
# 2) Already extracted? (marker first)
if [ -f "$markfile" ]; then
log_pass "$(basename "$tarfile") has already been extracted (marker present)."
return 0
fi
# 3) Heuristic: check multiple entries from the tar exist on disk
tmp_list="${outdir}/.tar_ls.$$"
if tar -tf "$tarfile" 2>/dev/null | head -n 20 >"$tmp_list"; then
total=0; present=0
while IFS= read -r ent; do
[ -z "$ent" ] && continue
total=$((total + 1))
ent="${ent%/}"
# check exact relative path and also basename (covers archives with a top-level dir)
if [ -e "$outdir/$ent" ] || [ -e "$outdir/$(basename "$ent")" ]; then
present=$((present + 1))
fi
done < "$tmp_list"
rm -f "$tmp_list" 2>/dev/null || true
# If we find a reasonable portion of entries, assume it's extracted
if [ "$present" -ge 3 ] || { [ "$total" -gt 0 ] && [ $((present * 100 / total)) -ge 50 ]; }; then
log_pass "$(basename "$tarfile") already extracted ($present/$total entries found)."
return 0
fi
fi
# 4) Exists and valid, but not yet extracted
log_info "$(basename "$tarfile") exists and is valid, but not yet extracted."
return 2
}
# Return space-separated PIDs for 'weston' (BusyBox friendly).
weston_pids() {
pids=""
if command -v pgrep >/dev/null 2>&1; then
pids="$(pgrep -x weston 2>/dev/null || true)"
fi
if [ -z "$pids" ]; then
pids="$(ps -eo pid,comm 2>/dev/null | awk '$2=="weston"{print $1}')"
fi
echo "$pids"
}
# Is Weston running
weston_is_running() {
[ -n "$(weston_pids)" ]
}
# Stop all Weston processes, also stop socket activation and instances
weston_stop() {
if command -v systemctl >/dev/null 2>&1; then
log_info "Stopping Weston, stopping weston.socket to prevent auto restart"
systemctl stop weston.socket >/dev/null 2>&1 || true
log_info "Stopping Weston, stopping weston.service"
systemctl stop weston.service >/dev/null 2>&1 || true
log_info "Stopping Weston, stopping weston@root.service if present"
systemctl stop weston@root.service >/dev/null 2>&1 || true
# Stop any active weston@*.service instances
units="$(systemctl list-units 'weston@*.service' --no-legend --no-pager 2>/dev/null | awk '{print $1}')"
if [ -n "$units" ]; then
for u in $units; do
log_info "Stopping Weston, stopping instance unit $u"
systemctl stop "$u" >/dev/null 2>&1 || true
done
fi
fi
if ! weston_is_running; then
log_info "Weston is not running."
# Still cleanup stale sockets (prevents false positives elsewhere)
weston_cleanup_stale_sockets
return 0
fi
log_info "Stopping Weston..."
# First try graceful
if command -v pkill >/dev/null 2>&1; then
log_info "Stopping Weston processes"
pkill -TERM -x weston >/dev/null 2>&1 || true
elif command -v killall >/dev/null 2>&1; then
killall weston >/dev/null 2>&1 || true
else
# Last resort: kill by PID list
for p in $(weston_pids); do
kill "$p" >/dev/null 2>&1 || true
done
fi
i=1
while [ "$i" -le 10 ]; do
log_info "Waiting for Weston to stop, attempt $i of 10"
if ! weston_is_running; then
weston_cleanup_stale_sockets
log_info "Weston stopped successfully"
return 0
fi
sleep 1
i=$((i + 1))
done
log_warn "Weston still running after grace period, sending SIGKILL"
if command -v pkill >/dev/null 2>&1; then
pkill -KILL -x weston >/dev/null 2>&1 || true
else
for p in $(weston_pids); do
kill -9 "$p" >/dev/null 2>&1 || true
done
fi
i=1
while [ "$i" -le 5 ]; do
log_info "Waiting for Weston to stop after SIGKILL, attempt $i of 5"
if ! weston_is_running; then
weston_cleanup_stale_sockets
log_info "Weston stopped successfully"
return 0
fi
sleep 1
i=$((i + 1))
done
log_error "Failed to stop Weston completely"
# Still attempt cleanup to avoid future false positives
weston_cleanup_stale_sockets
return 1
}
# Start weston with correct env if not running
weston_start() {
if weston_is_running; then
log_info "Weston already running"
return 0
fi
# If stale sockets exist from previous crash/kill, remove them first
weston_cleanup_stale_sockets
if command -v systemctl >/dev/null 2>&1; then
log_info "Attempting to start Weston, enabling weston.socket if available"
systemctl start weston.socket >/dev/null 2>&1 || true
sleep 1
log_info "Attempting to start Weston via weston.service"
systemctl start weston.service >/dev/null 2>&1 || true
sleep 1
if weston_is_running; then
log_info "Weston started via weston.service"
return 0
fi
log_info "Attempting to start Weston via weston@root.service"
systemctl start weston@root.service >/dev/null 2>&1 || true
sleep 1
if weston_is_running; then
log_info "Weston started via weston@root.service"
return 0
fi
# Try one enabled weston@ instance if any exists
enabled_unit="$(systemctl list-unit-files 'weston@*.service' --no-legend --no-pager 2>/dev/null | awk '$2=="enabled"{print $1; exit}')"