-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatchdog.sh
More file actions
executable file
·1487 lines (1422 loc) · 59.6 KB
/
Copy pathwatchdog.sh
File metadata and controls
executable file
·1487 lines (1422 loc) · 59.6 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
#!/usr/bin/env bash
# macprovider-watchdog: local provider liveness monitor plus
# auto-update rollback observer.
#
# Health verdict: the exact launchd service PID must own the configured local
# listener and its /v1/health endpoint must answer. Other macprovider-cli
# diagnostics are structurally irrelevant. Coordinator TCP
# reachability is advisory logging only; a missing ESTABLISHED coordinator
# connection no longer causes a kick by itself.
set -euo pipefail
LABEL="${MACPROVIDER_WATCHDOG_LABEL:-live.streamvc.macprovider}"
CONFIG_PATH="${MACPROVIDER_CONFIG_PATH:-$HOME/.config/macprovider/config.yaml}"
BINARY_PATH="${MACPROVIDER_BINARY_PATH:-$HOME/macprovider/macprovider-cli}"
COORDINATOR_HOST="${MACPROVIDER_COORDINATOR_HOST:-coordinator.streamvc.live}"
COORDINATOR_PORT="${MACPROVIDER_COORDINATOR_PORT:-443}"
LOG_DIR="${MACPROVIDER_LOG_DIR:-$HOME/Library/Logs/macprovider}"
LOG_PATH="$LOG_DIR/watchdog.log"
# Issue #191 R1 architect HIGH: arming + grace state. Without
# these, a first-time install can spin in a restart loop — the
# Swift CLI loads the model BEFORE connecting to the coordinator
# (cold-cache model load is 10-20 minutes), and a watchdog that
# kicks on "no ESTABLISHED connection" would Darwin.exit the
# process every 60s before it ever opens its socket.
#
# Arming rule: the watchdog stays disarmed (no kicks) until it
# observes at least ONE successful ESTABLISHED connection IN THE
# CURRENT BOOT. The armed marker stores the boot id (kern.boottime
# sec) so a reboot — which restarts the provider into a fresh
# cold-cache model load — re-disarms the watchdog and prevents the
# stale-arming restart loop the R1 fix did not cover (R2 ARCH HIGH).
#
# Grace rule: after we observe a restart-worthy failure, we wait at least KICK_GRACE_SECONDS
# before logging another restart request. This covers the post-restart model-reload
# window without re-triggering on the gap between launchd respawn
# and re-establishing the coordinator socket.
STATE_DIR="${MACPROVIDER_WATCHDOG_STATE_DIR:-$HOME/.local/share/macprovider-watchdog/state}"
ARMED_FILE="$STATE_DIR/armed"
LAST_KICK_FILE="$STATE_DIR/last_kick"
KICK_GRACE_SECONDS="${MACPROVIDER_WATCHDOG_KICK_GRACE_SECONDS:-300}"
mkdir -p "$LOG_DIR" "$STATE_DIR"
# Boot id: per-boot identifier sourced from kern.bootsessionuuid.
# Apple-provided UUID is immutable for the lifetime of a single
# boot (verified against XNU sysctl: read-only). Unlike
# kern.boottime, this value is NOT affected by NTP / manual
# wall-clock time correction (R3 architect MEDIUM #1), so a
# clock-set event during a wedge cannot silently re-disarm the
# watchdog and let the wedge persist.
current_boot_id() {
sysctl -n kern.bootsessionuuid 2>/dev/null
}
# Acceptable formats in config.yaml are: `provider_id: ID` (yaml
# key) or `provider-id: ID` (alternate hyphenated form some operator
# tools have written historically). Either matches and surfaces the
# value with surrounding whitespace stripped.
read_provider_id() {
if [ ! -f "$CONFIG_PATH" ]; then
return 1
fi
awk '
/^[[:space:]]*provider[_-]id[[:space:]]*:/ {
sub(/^[^:]*:[[:space:]]*/, "")
sub(/[[:space:]]*#.*$/, "")
sub(/[[:space:]]+$/, "")
gsub(/^["'\'']|["'\'']$/, "")
print
exit
}
' "$CONFIG_PATH"
}
read_config_port() {
if [ ! -f "$CONFIG_PATH" ]; then
return 1
fi
awk '
/^[[:space:]]*port[[:space:]]*:/ {
sub(/^[^:]*:[[:space:]]*/, "")
sub(/[[:space:]]*#.*$/, "")
sub(/[[:space:]]+$/, "")
print
exit
}
' "$CONFIG_PATH"
}
ts() { date -u +"%Y-%m-%dT%H:%M:%SZ"; }
log() { printf "[%s] %s\n" "$(ts)" "$*" >> "$LOG_PATH"; }
resolve_coordinator_ip() {
# First try dscacheutil (no network call if already cached);
# fall back to host(1) which most macs have via bind-utils.
ip="$(dscacheutil -q host -a name "$COORDINATOR_HOST" 2>/dev/null \
| awk '/^ip_address:/ { print $2; exit }')"
if [ -z "$ip" ] && command -v host >/dev/null 2>&1; then
ip="$(host -t A "$COORDINATOR_HOST" 2>/dev/null \
| awk '/has address/ { print $4; exit }')"
fi
printf "%s" "${ip:-}"
}
has_established_conn() {
ip="$1"
if [ -z "$ip" ]; then
return 1
fi
# BSD netstat on macOS: print ESTABLISHED TCP rows; awk matches
# the foreign-address column against our coordinator IP:port.
# Format: Proto Recv-Q Send-Q Local-Address Foreign-Address (state)
netstat -an -p tcp 2>/dev/null \
| awk -v target="${ip}.${COORDINATOR_PORT}" '
$0 ~ /ESTABLISHED/ && $5 == target { found = 1; exit }
END { exit found ? 0 : 1 }
'
}
provider_process_pid() {
launchctl_bin="${MACPROVIDER_LAUNCHCTL:-launchctl}"
service_target="gui/$(id -u)/$LABEL"
if ! service_output="$("$launchctl_bin" print "$service_target" 2>/dev/null)"; then
return 1
fi
candidates="$(printf "%s\n" "$service_output" | awk 'NF == 3 && $1 == "pid" && $2 == "=" && $3 ~ /^[0-9]+$/ { print $3 }')"
[ "$(printf "%s\n" "$candidates" | awk 'NF { count++ } END { print count + 0 }')" -eq 1 ] || return 1
candidate="$candidates"
expected="$BINARY_PATH"
if command -v realpath >/dev/null 2>&1 && [ -e "$expected" ]; then
expected="$(realpath "$expected" 2>/dev/null || printf "%s" "$expected")"
fi
command -v lsof >/dev/null 2>&1 || return 1
executable_output="$(lsof -a -p "$candidate" -d txt -Fn 2>/dev/null)" || return 1
command_paths="$(printf "%s\n" "$executable_output" | awk 'substr($0, 1, 1) == "n" && length($0) > 1 { print substr($0, 2) }')"
found_expected=""
while IFS= read -r command_path; do
[ -n "$command_path" ] || continue
if command -v realpath >/dev/null 2>&1 && [ -e "$command_path" ]; then
command_path="$(realpath "$command_path" 2>/dev/null || printf "%s" "$command_path")"
fi
if [ "$command_path" = "$expected" ]; then
found_expected=1
break
fi
done <<EOF
$command_paths
EOF
[ "$found_expected" = 1 ] || return 1
printf "%s" "$candidate"
}
local_health_listener_owned_by_provider() {
provider_pid="$1"
port="$2"
if ! command -v lsof >/dev/null 2>&1; then
return 1
fi
lsof -nP -iTCP:"$port" -sTCP:LISTEN -t 2>/dev/null | awk -v pid="$provider_pid" '$1 == pid { found = 1 } END { exit found ? 0 : 1 }'
}
local_provider_health_ok() {
provider_pid="$1"
port="$(read_config_port || true)"
case "$port" in
''|*[!0-9]*) return 1 ;;
esac
local_health_listener_owned_by_provider "$provider_pid" "$port" || return 1
curl_bin="${MACPROVIDER_CURL:-/usr/bin/curl}"
"$curl_bin" -fsS --max-time 2 "http://127.0.0.1:${port}/v1/health" >/dev/null 2>&1
}
valid_lifecycle_lease() {
provider_pid="$1"
[ -x "$BINARY_PATH" ] || return 1
if "$BINARY_PATH" lifecycle-lease status --expected-kind startup --expected-pid "$provider_pid" >/dev/null 2>&1; then
return 0
fi
"$BINARY_PATH" lifecycle-lease status --expected-kind maintenance >/dev/null 2>&1
}
note_provider_restart_request() {
log "provider restart requested for $LABEL but skipped: launchd KeepAlive is the sole runtime manager"
}
now_epoch() { date -u +%s; }
autoupdate_recovery_tick() {
AUTUPDATE_STATE_ROOT="${MACPROVIDER_AUTOUPDATE_STATE_ROOT:-$HOME/.local/share/macprovider/autoupdate}" \
MACPROVIDER_BINARY_PATH="$BINARY_PATH" \
MACPROVIDER_LABEL="$LABEL" \
LOG_PATH="$LOG_PATH" \
python3 <<'PY'
import datetime
import fcntl
import hashlib
import json
import os
import pwd
import re
import shutil
import stat
import subprocess
import sys
import time
import uuid
root = os.environ["AUTUPDATE_STATE_ROOT"]
binary_path = os.environ["MACPROVIDER_BINARY_PATH"]
label = os.environ["MACPROVIDER_LABEL"]
log_path = os.environ["LOG_PATH"]
pending = os.path.join(root, "pending.json")
lock_path = os.path.join(root, "update.lock")
install_lock_path = os.path.expanduser("~/.config/macprovider/install.lock")
lifecycle_root = os.path.expanduser("~/Library/Application Support/macprovider/lifecycle")
lifecycle_lock_path = os.path.join(lifecycle_root, ".lease.json.lock")
uid = os.getuid()
provider_user = pwd.getpwuid(uid).pw_name
reload_helper_label = "live.streamvc.macprovider-compatibility-reload"
legacy_reload_helper_label = re.compile(
rf"^{re.escape(reload_helper_label)}\."
r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
)
reload_helper_removal_max_checks = 100
class ReloadHelperFenceError(RuntimeError):
pass
def ts():
return datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
def log(message):
with open(log_path, "a", encoding="utf-8") as fh:
fh.write(f"[{ts()}] autoupdate {message}\n")
def event(outcome, phase, failure_class, reason, marker=None):
payload = {
"event": "provider_autoupdate_watchdog",
"source": "coordinator",
"outcome": outcome,
"phase": phase,
"reason": reason,
"timestamp": ts(),
}
if failure_class:
payload["failure_class"] = failure_class
if marker:
payload["update_id"] = marker.get("update_id", "")
payload["target_version"] = marker.get("target_version", "")
log(json.dumps(payload, sort_keys=True, separators=(",", ":")))
def fence_reload_helpers():
try:
listed = subprocess.run(
["launchctl", "list"],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
timeout=5,
)
except Exception as exc:
raise ReloadHelperFenceError(f"reload_helper_list_failed:{type(exc).__name__}") from exc
if listed.returncode != 0:
raise ReloadHelperFenceError(f"reload_helper_list_failed:{listed.returncode}")
labels = set()
for line in listed.stdout.splitlines():
fields = line.split(None, 2)
if len(fields) != 3:
continue
candidate = fields[2]
if candidate == reload_helper_label or legacy_reload_helper_label.fullmatch(candidate):
labels.add(candidate)
labels.add(reload_helper_label)
ordered_labels = [reload_helper_label] + sorted(labels - {reload_helper_label})
domain = f"gui/{uid}"
for helper_label in ordered_labels:
try:
subprocess.run(
["launchctl", "bootout", f"{domain}/{helper_label}"],
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=5,
)
except Exception as exc:
raise ReloadHelperFenceError(
f"reload_helper_bootout_failed:{helper_label}:{type(exc).__name__}"
) from exc
absent = False
for attempt in range(reload_helper_removal_max_checks):
try:
inspected = subprocess.run(
["launchctl", "print", f"{domain}/{helper_label}"],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
timeout=5,
)
except Exception as exc:
raise ReloadHelperFenceError(
f"reload_helper_inspection_failed:{helper_label}:{type(exc).__name__}"
) from exc
if (
inspected.returncode == 113
and "Could not find service" in inspected.stdout
):
absent = True
break
if inspected.returncode != 0:
raise ReloadHelperFenceError(
f"reload_helper_inspection_failed:{helper_label}:{inspected.returncode}"
)
if attempt + 1 < reload_helper_removal_max_checks:
time.sleep(0.1)
if not absent:
raise ReloadHelperFenceError(f"reload_helper_removal_timeout:{helper_label}")
launch_agents = os.path.expanduser("~/Library/LaunchAgents")
for helper_label in ordered_labels:
helper_plist = os.path.join(launch_agents, f"{helper_label}.plist")
if not os.path.lexists(helper_plist):
continue
if os.path.isdir(helper_plist) and not os.path.islink(helper_plist):
raise ReloadHelperFenceError(f"reload_helper_plist_not_file:{helper_label}")
try:
os.unlink(helper_plist)
except Exception as exc:
raise ReloadHelperFenceError(
f"reload_helper_plist_remove_failed:{helper_label}:{type(exc).__name__}"
) from exc
def record_watchdog_recovery(marker, failure_class):
target = marker["target_path"]
reason_code = f"watchdog_rollback_{failure_class}"
operation_id = f"watchdog-recovery:{marker['update_id']}"
command = [
target,
"lifecycle-state",
"transition",
"--state",
"watchdog_recovery",
"--reason-code",
reason_code,
"--writer",
"watchdog",
"--operation-id",
operation_id,
]
compatibility_id = marker.get("previous_compatibility_set_id") or marker.get("target_compatibility_set_id")
if compatibility_id:
command.extend(["--compatibility-set-id", compatibility_id])
try:
result = subprocess.run(
command,
check=False,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=10,
)
if result.returncode == 0:
log(f"lifecycle_transition=watchdog_recovery reason_code={reason_code} operation_id={operation_id}")
else:
log(f"lifecycle_transition_failed=watchdog_recovery exit_status={result.returncode}")
except Exception as exc:
log(f"lifecycle_transition_failed=watchdog_recovery error={type(exc).__name__}")
def reject_path(path, must_exist=True):
try:
st = os.lstat(path)
except FileNotFoundError:
if must_exist:
raise
return None
if stat.S_ISLNK(st.st_mode):
raise RuntimeError(f"symlink_rejected:{path}")
if st.st_uid != uid:
raise RuntimeError(f"owner_rejected:{path}")
if st.st_nlink != 1 and not stat.S_ISDIR(st.st_mode):
raise RuntimeError(f"hardlink_rejected:{path}")
if st.st_mode & (stat.S_IWGRP | stat.S_IWOTH):
raise RuntimeError(f"writable_rejected:{path}")
try:
acl = subprocess.run(["/bin/ls", "-le", path], check=False, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
for line in acl.stdout.splitlines():
stripped = line.strip().lower()
if not re.match(r"^[0-9]+:", stripped):
continue
if ("write" in stripped or "append" in stripped or "add_file" in stripped) and f"user:{provider_user.lower()}" not in stripped:
raise RuntimeError(f"acl_write_rejected:{path}")
except FileNotFoundError:
pass
return st
def verify_root():
current = root
parts = []
while True:
parts.append(current)
parent = os.path.dirname(current)
if parent == current or current == os.path.expanduser("~"):
break
current = parent
for path in reversed(parts):
if os.path.exists(path):
st = reject_path(path)
if not stat.S_ISDIR(st.st_mode):
raise RuntimeError(f"not_directory:{path}")
def read_marker():
fd = os.open(pending, os.O_RDONLY | getattr(os, "O_NONBLOCK", 0) | getattr(os, "O_NOFOLLOW", 0))
try:
raw = os.read(fd, 65536)
finally:
os.close(fd)
marker = json.loads(raw.decode("utf-8"))
validate_marker_strict(marker)
return marker
def validate_marker_strict(marker):
required = {"update_id", "target_version", "target_path", "backup_path", "size", "mode", "sha256", "marker_deadline"}
if not required.issubset(marker.keys()):
raise RuntimeError("marker_missing_required_fields")
if not re.match(r"^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", str(marker["update_id"])):
raise RuntimeError("marker_update_id_invalid")
if not re.match(r"^[0-9]+\.[0-9]+\.[0-9]+$", str(marker["target_version"])):
raise RuntimeError("marker_target_version_invalid")
for key in ("target_path", "backup_path"):
value = str(marker[key])
if not os.path.isabs(value) or value.endswith("/") or "/../" in value or "/./" in value:
raise RuntimeError(f"marker_{key}_invalid")
size = int(marker["size"])
mode = int(marker["mode"])
if size < 0 or size > 1024 * 1024 * 1024:
raise RuntimeError("marker_size_invalid")
if mode < 0 or mode > 0o7777:
raise RuntimeError("marker_mode_invalid")
if not re.match(r"^[0-9a-f]{64}$", str(marker["sha256"])):
raise RuntimeError("marker_sha256_invalid")
release_backup = marker.get("release_backup_path")
release_sha = marker.get("release_backup_sha256")
if (release_backup is None) != (release_sha is None):
raise RuntimeError("marker_release_backup_incomplete")
if release_backup is not None:
value = str(release_backup)
if not os.path.isabs(value) or value.endswith("/") or "/../" in value or "/./" in value:
raise RuntimeError("marker_release_backup_path_invalid")
if not re.match(r"^[0-9a-f]{64}$", str(release_sha)):
raise RuntimeError("marker_release_backup_sha256_invalid")
compatibility_id = marker.get("target_compatibility_set_id")
compatibility_sha = marker.get("target_compatibility_set_sha256")
if (compatibility_id is None) != (compatibility_sha is None):
raise RuntimeError("marker_compatibility_set_incomplete")
if compatibility_id is not None:
if not isinstance(compatibility_id, str) or not compatibility_id or compatibility_id.strip() != compatibility_id or len(compatibility_id.encode("utf-8")) > 512:
raise RuntimeError("marker_compatibility_set_id_invalid")
if not re.match(r"^[0-9a-f]{64}$", str(compatibility_sha)):
raise RuntimeError("marker_compatibility_set_sha256_invalid")
previous_fields = (
marker.get("previous_version"),
marker.get("previous_compatibility_set_id"),
marker.get("previous_compatibility_set_sha256"),
marker.get("transaction_state"),
)
if any(value is not None for value in previous_fields):
if any(value is None for value in previous_fields):
raise RuntimeError("marker_previous_compatibility_set_incomplete")
previous_version, previous_id, previous_sha, transaction_state = previous_fields
if compatibility_id is None or release_backup is None:
raise RuntimeError("marker_previous_compatibility_set_unbound")
if not re.match(r"^[0-9]+\.[0-9]+\.[0-9]+$", str(previous_version)):
raise RuntimeError("marker_previous_version_invalid")
if not re.match(
r"^[A-Za-z0-9_.-]{1,64}/[A-Za-z0-9_.-]{1,100}:v[0-9]+\.[0-9]+\.[0-9]+@[0-9a-f]{40}$",
str(previous_id),
):
raise RuntimeError("marker_previous_compatibility_set_id_invalid")
if not re.match(r"^[0-9a-f]{64}$", str(previous_sha)):
raise RuntimeError("marker_previous_compatibility_set_sha256_invalid")
if transaction_state not in {
"activating_target",
"restoring_previous",
"awaiting_previous_readiness",
}:
raise RuntimeError("marker_transaction_state_invalid")
raw_deadline = str(marker["marker_deadline"])
if not raw_deadline.endswith("Z"):
raise RuntimeError("marker_deadline_invalid")
try:
deadline = datetime.datetime.strptime(raw_deadline, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=datetime.timezone.utc)
except ValueError:
raise RuntimeError("marker_deadline_invalid")
now = datetime.datetime.now(datetime.timezone.utc)
post_start_window = 60
future_tolerance = post_start_window + 30 * 60
if deadline > now + datetime.timedelta(seconds=future_tolerance):
raise RuntimeError("marker_deadline_out_of_bounds")
def current_binary_version(path):
try:
result = subprocess.run([path, "--version"], check=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=5)
except Exception:
return ""
output = f"{result.stdout}\n{result.stderr}"
match = re.search(r"([0-9]+(?:\.[0-9]+){2}(?:[-+][0-9A-Za-z.-]+)?)", output)
return match.group(1) if match else ""
def read_success_sentinel(path):
reject_path(path)
fd = os.open(path, os.O_RDONLY | getattr(os, "O_NONBLOCK", 0) | getattr(os, "O_NOFOLLOW", 0))
try:
payload = json.loads(os.read(fd, 65536).decode("utf-8"))
finally:
os.close(fd)
update_id = str(payload.get("update_id", ""))
if not re.match(r"^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$", update_id):
raise RuntimeError("sentinel_update_id_invalid")
return {
"update_id": update_id,
"binary_version": str(payload.get("binary_version", "")),
}
def process_success_sentinel(marker):
if marker.get("transaction_state") in {
"restoring_previous",
"awaiting_previous_readiness",
}:
return False
binary_dir = os.path.dirname(marker["target_path"])
for name in os.listdir(binary_dir):
if not name.startswith(".macprovider-cli.success-"):
continue
sentinel = os.path.join(binary_dir, name)
try:
validate_restore_inputs(marker)
payload = read_success_sentinel(sentinel)
sentinel_version = payload["binary_version"]
current_version = current_binary_version(marker["target_path"])
if not sentinel_version or sentinel_version != current_version:
event("failure", "post_start", "orphaned_success_sentinel", "binary_version_mismatch", {"update_id": payload["update_id"], "target_version": sentinel_version})
os.unlink(sentinel)
continue
if payload["update_id"] != str(marker["update_id"]):
event("failure", "post_start", "orphaned_success_sentinel", "update_id_mismatch", {"update_id": payload["update_id"], "target_version": sentinel_version})
os.unlink(sentinel)
continue
try:
os.unlink(pending)
except FileNotFoundError:
pass
try:
os.unlink(marker["backup_path"])
except FileNotFoundError:
pass
release_backup = marker.get("release_backup_path")
if release_backup:
shutil.rmtree(release_backup, ignore_errors=True)
os.unlink(sentinel)
event("success", "post_start", None, "success_sentinel_cleanup_completed", marker)
return True
except Exception as exc:
event("failure", "post_start", "orphaned_success_sentinel", str(exc), marker)
try:
os.unlink(sentinel)
except FileNotFoundError:
pass
return False
def sha256(path):
h = hashlib.sha256()
fd = os.open(path, os.O_RDONLY | getattr(os, "O_NONBLOCK", 0) | getattr(os, "O_NOFOLLOW", 0))
try:
while True:
chunk = os.read(fd, 1024 * 1024)
if not chunk:
break
h.update(chunk)
finally:
os.close(fd)
return h.hexdigest()
def binary_path_without_pending():
candidate = os.environ.get("MACPROVIDER_BINARY_PATH", "")
if candidate:
return candidate
return shutil.which("macprovider-cli") or ""
def known_binary_dir():
configured = os.environ.get("MACPROVIDER_BINARY_DIR", "")
if configured:
return os.path.realpath(configured)
plist_path = os.path.expanduser("~/Library/LaunchAgents/live.streamvc.macprovider.plist")
try:
result = subprocess.run(
["/usr/libexec/PlistBuddy", "-c", "Print ProgramArguments:0", plist_path],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
timeout=5,
)
if result.returncode == 0 and result.stdout.strip():
return os.path.realpath(os.path.dirname(result.stdout.strip()))
except Exception:
pass
binary = binary_path_without_pending()
if binary:
return os.path.realpath(os.path.dirname(binary))
return ""
def scan_without_pending():
binary = binary_path_without_pending()
if not binary:
return
binary_dir = os.path.dirname(binary)
for name in os.listdir(binary_dir):
path = os.path.join(binary_dir, name)
if name.startswith(".macprovider-cli.success-"):
try:
payload = read_success_sentinel(path)
sentinel_version = payload["binary_version"]
current_version = current_binary_version(binary)
if sentinel_version and sentinel_version == current_version:
os.unlink(path)
event("failure", "post_start", "orphaned_success_sentinel", "no_matching_pending", {"update_id": payload["update_id"], "target_version": current_version})
else:
os.unlink(path)
event("failure", "post_start", "orphaned_success_sentinel", "binary_version_mismatch", {"update_id": payload["update_id"], "target_version": sentinel_version})
except Exception as exc:
log(f"success_sentinel_scan_error={exc}")
elif name.startswith(".macprovider-cli.rollback-"):
try:
os.unlink(path)
log(f"deleted_stale_backup={path}")
except FileNotFoundError:
pass
elif name.startswith(".macprovider-cli.release-rollback-"):
shutil.rmtree(path, ignore_errors=True)
def quarantine(reason, marker=None):
stamp = datetime.datetime.now(datetime.timezone.utc).strftime("%Y%m%dT%H%M%SZ")
dest = os.path.join(root, f"pending-quarantined-{stamp}.json")
try:
os.replace(pending, dest)
log(f"pending_marker_quarantined={dest} reason={reason}")
except FileNotFoundError:
pass
def marker_deadline_expired(marker):
raw_deadline = str(marker["marker_deadline"])
deadline = datetime.datetime.strptime(raw_deadline, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=datetime.timezone.utc)
return datetime.datetime.now(datetime.timezone.utc) >= deadline
def write_marker(marker):
validate_marker_strict(marker)
payload = json.dumps(marker, sort_keys=True, separators=(",", ":")).encode("utf-8")
temporary = os.path.join(root, f".pending-{uuid.uuid4()}.json")
fd = os.open(
temporary,
os.O_CREAT | os.O_EXCL | os.O_WRONLY | getattr(os, "O_NOFOLLOW", 0),
0o600,
)
try:
offset = 0
while offset < len(payload):
written = os.write(fd, payload[offset:])
if written <= 0:
raise RuntimeError("marker_write_failed")
offset += written
os.fchmod(fd, 0o600)
os.fsync(fd)
finally:
os.close(fd)
try:
os.replace(temporary, pending)
directory_fd = os.open(root, os.O_RDONLY)
try:
os.fsync(directory_fd)
finally:
os.close(directory_fd)
except Exception:
try:
os.unlink(temporary)
except FileNotFoundError:
pass
raise
def transition_marker(marker, state, readiness_seconds=None):
updated = dict(marker)
updated["transaction_state"] = state
if readiness_seconds is not None:
updated["marker_deadline"] = (
datetime.datetime.now(datetime.timezone.utc)
+ datetime.timedelta(seconds=readiness_seconds)
).strftime("%Y-%m-%dT%H:%M:%SZ")
write_marker(updated)
return updated
def process_start(pid):
if not isinstance(pid, int) or isinstance(pid, bool) or pid <= 0:
return ""
result = subprocess.run(
["ps", "-p", str(pid), "-o", "lstart="],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
)
return result.stdout.strip() if result.returncode == 0 else ""
def boot_session():
try:
result = subprocess.run(
["/usr/sbin/sysctl", "-n", "kern.bootsessionuuid"],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
)
value = result.stdout.strip()
if value:
return value
except FileNotFoundError:
pass
try:
with open("/proc/sys/kernel/random/boot_id", encoding="ascii") as handle:
return handle.read().strip()
except OSError:
return ""
def normalize_lock_fd(fd, path):
info = os.fstat(fd)
if (
not stat.S_ISREG(info.st_mode)
or info.st_uid != uid
or info.st_nlink != 1
or stat.S_IMODE(info.st_mode) & 0o077
):
raise RuntimeError(f"mutation_lock_invalid:{path}")
os.fchmod(fd, 0o600)
if stat.S_IMODE(os.fstat(fd).st_mode) != 0o600:
raise RuntimeError(f"mutation_lock_mode_invalid:{path}")
def acquire_lifecycle_lock():
os.makedirs(lifecycle_root, mode=0o700, exist_ok=True)
directory_st = reject_path(lifecycle_root)
if not stat.S_ISDIR(directory_st.st_mode) or stat.S_IMODE(directory_st.st_mode) != 0o700:
raise RuntimeError("lifecycle_lease_directory_invalid")
fd = os.open(
lifecycle_lock_path,
os.O_CREAT | os.O_RDWR | getattr(os, "O_NOFOLLOW", 0),
0o600,
)
try:
path_st = reject_path(lifecycle_lock_path)
descriptor_st = os.fstat(fd)
if (
not stat.S_ISREG(descriptor_st.st_mode)
or descriptor_st.st_uid != uid
or descriptor_st.st_nlink != 1
or stat.S_IMODE(descriptor_st.st_mode) != 0o600
or (descriptor_st.st_dev, descriptor_st.st_ino) != (path_st.st_dev, path_st.st_ino)
):
raise RuntimeError("lifecycle_lease_lock_invalid")
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
os.close(fd)
return None
return fd
except Exception:
os.close(fd)
raise
def inspect_lifecycle_lease():
if not os.path.isfile(binary_path) or not os.access(binary_path, os.X_OK):
return None
try:
result = subprocess.run(
[binary_path, "lifecycle-lease", "status"],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
timeout=5,
)
except (OSError, subprocess.SubprocessError):
return None
if result.returncode != 0:
return None
try:
payload = json.loads(result.stdout)
except json.JSONDecodeError:
return None
kind = payload.get("kind")
owner_pid = payload.get("owner_pid")
if (
payload.get("state") != "valid"
or kind not in {"startup", "maintenance"}
or not isinstance(owner_pid, int)
or isinstance(owner_pid, bool)
or owner_pid <= 0
):
return None
return {"kind": kind, "owner_pid": owner_pid}
def launchd_provider_pid():
launchctl = os.environ.get("MACPROVIDER_LAUNCHCTL", "launchctl")
try:
result = subprocess.run(
[launchctl, "print", f"gui/{uid}/{label}"],
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
timeout=5,
)
except Exception:
return None
if result.returncode != 0:
return None
candidates = re.findall(r"^\s*pid\s*=\s*([0-9]+)\s*$", result.stdout, re.MULTILINE)
if len(candidates) != 1:
return None
return int(candidates[0])
def installer_owner_is_live(lock_fd):
os.lseek(lock_fd, 0, os.SEEK_SET)
payload = os.read(lock_fd, 4097)
if len(payload) > 4096:
raise RuntimeError("installer_owner_record_oversized")
if not payload.strip():
return False
try:
record = json.loads(payload.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
raise RuntimeError("installer_owner_record_invalid") from exc
if not isinstance(record, dict):
raise RuntimeError("installer_owner_record_invalid")
owner_pid = record.get("pid")
owner_start = record.get("process_start")
owner_boot = record.get("boot_session")
if (
not isinstance(owner_pid, int)
or isinstance(owner_pid, bool)
or owner_pid <= 0
or not isinstance(owner_start, str)
or not owner_start
or not isinstance(owner_boot, str)
or not owner_boot
):
raise RuntimeError("installer_owner_record_invalid")
current_boot = boot_session()
if not current_boot:
raise RuntimeError("installer_owner_boot_identity_unavailable")
return owner_boot == current_boot and process_start(owner_pid) == owner_start
def release_transaction_locks(descriptors):
for descriptor in reversed(descriptors):
fcntl.flock(descriptor, fcntl.LOCK_UN)
os.close(descriptor)
def acquire_transaction_locks():
os.makedirs(root, mode=0o700, exist_ok=True)
os.makedirs(os.path.dirname(install_lock_path), mode=0o700, exist_ok=True)
descriptors = []
for path in (install_lock_path, lock_path):
fd = os.open(path, os.O_CREAT | os.O_RDWR | getattr(os, "O_NOFOLLOW", 0), 0o600)
try:
normalize_lock_fd(fd, path)
except Exception:
os.close(fd)
release_transaction_locks(descriptors)
raise
try:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
os.close(fd)
release_transaction_locks(descriptors)
return None
descriptors.append(fd)
try:
owner_live = installer_owner_is_live(descriptors[0])
except Exception:
release_transaction_locks(descriptors)
raise
if owner_live:
release_transaction_locks(descriptors)
return None
return descriptors
def validate_restore_inputs(marker):
backup = marker["backup_path"]
target = marker["target_path"]
update_id = marker["update_id"]
expected_backup = os.path.join(os.path.dirname(target), f".macprovider-cli.rollback-{update_id}")
if backup != expected_backup:
raise RuntimeError("backup_path_derivation_mismatch")
trusted_dir = known_binary_dir()
if not trusted_dir:
raise RuntimeError("unsupported_install_topology:binary_dir_unknown")
target_parent = os.path.realpath(os.path.dirname(target))
backup_parent = os.path.realpath(os.path.dirname(backup))
if target_parent != trusted_dir or backup_parent != trusted_dir:
raise RuntimeError("unsupported_install_topology:path_outside_binary_dir")
for checked in (target, backup):
cursor = checked
while os.path.realpath(os.path.dirname(cursor)) == trusted_dir and cursor != trusted_dir:
reject_path(cursor, must_exist=os.path.exists(cursor))
break
backup_st = reject_path(backup)
reject_path(os.path.dirname(target))
if not os.path.isabs(target) or target.endswith("/"):
raise RuntimeError("target_path_invalid")
if backup_st.st_size != int(marker["size"]):
raise RuntimeError("backup_size_mismatch")
if sha256(backup) != str(marker["sha256"]):
raise RuntimeError("backup_sha256_mismatch")
release_backup = marker.get("release_backup_path")
if release_backup:
expected_release_backup = os.path.join(os.path.dirname(target), f".macprovider-cli.release-rollback-{update_id}")
if release_backup != expected_release_backup:
raise RuntimeError("release_backup_path_derivation_mismatch")
if os.path.realpath(os.path.dirname(release_backup)) != trusted_dir:
raise RuntimeError("unsupported_install_topology:release_backup_outside_binary_dir")
release_st = reject_path(release_backup)
if not stat.S_ISDIR(release_st.st_mode):
raise RuntimeError("release_backup_not_directory")
allowed = lambda name: name in {"mlx.metallib", "THIRD-PARTY-NOTICES.txt", "compatibility-set.json", "compatibility-set-local", "catalog-release", "external-local-members", "Malibu.app.zip", "malibu-app-state.json"} or name.endswith(".bundle")
if any(not allowed(name) for name in os.listdir(release_backup)):
raise RuntimeError("release_backup_unexpected_entry")
if release_tree_sha256(release_backup) != str(marker["release_backup_sha256"]):
raise RuntimeError("release_backup_sha256_mismatch")
external_backup = os.path.join(release_backup, "external-local-members")
if os.path.exists(external_backup):
validate_external_local_backup(external_backup)
validate_malibu_app_backup(release_backup)
return backup, target, release_backup
def release_tree_sha256(root_path):
records = []
for current, directory_names, file_names in os.walk(root_path, topdown=True, followlinks=False):
directory_names.sort()
file_names.sort()
for name in directory_names + file_names:
path = os.path.join(current, name)
item_st = reject_path(path)
relative = os.path.relpath(path, root_path)
if "\x00" in relative or "\n" in relative or relative == ".." or relative.startswith("../"):
raise RuntimeError("release_tree_path_invalid")
mode = stat.S_IMODE(item_st.st_mode)
if stat.S_ISDIR(item_st.st_mode):
record = f"d\0{relative}\0{mode}\0"
elif stat.S_ISREG(item_st.st_mode):
record = f"f\0{relative}\0{mode}\0{item_st.st_size}\0{sha256(path)}\0"
else:
raise RuntimeError("release_tree_entry_invalid")
records.append((relative, record.encode("utf-8")))
digest = hashlib.sha256()
for _, record in sorted(records, key=lambda item: item[0]):
digest.update(record)
return digest.hexdigest()
def owned_release_resource(name):
return name in {"mlx.metallib", "THIRD-PARTY-NOTICES.txt", "compatibility-set.json", "compatibility-set-local", "catalog-release"} or name.endswith(".bundle")
def external_local_members():
home = os.path.expanduser("~")
return [
("launchd", os.path.join(home, "Library/LaunchAgents/live.streamvc.macprovider.plist"), "provider.plist"),
("watchdog_script", os.path.join(home, ".local/share/macprovider-watchdog/macprovider-health-monitor"), "watchdog.sh"),
("watchdog_plist", os.path.join(home, "Library/LaunchAgents/live.streamvc.macprovider-watchdog.plist"), "watchdog.plist"),
]
def validate_external_local_backup(backup_directory):
reject_path(backup_directory)
state_path = os.path.join(backup_directory, "state.json")
reject_path(state_path)
with open(state_path, "r", encoding="utf-8") as handle:
state = json.load(handle)
if set(state) != {"schema_version", "members"} or state["schema_version"] != 1 or not isinstance(state["members"], list):
raise RuntimeError("external_backup_state_invalid")
expected = external_local_members()
if [record.get("member") for record in state["members"]] != [member[0] for member in expected]:
raise RuntimeError("external_backup_members_invalid")
expected_names = {"state.json"}
for record, (_, _, backup_name) in zip(state["members"], expected):
present = record.get("was_present")
if not isinstance(present, bool):
raise RuntimeError("external_backup_presence_invalid")
backup_path = os.path.join(backup_directory, backup_name)
if present:
if set(record) != {"member", "mode", "sha256", "was_present"}:
raise RuntimeError("external_backup_record_invalid")
mode = record.get("mode")
digest = record.get("sha256")
if not isinstance(mode, int) or isinstance(mode, bool) or mode < 0 or mode > 0o7777:
raise RuntimeError("external_backup_mode_invalid")
if not isinstance(digest, str) or not re.match(r"^[0-9a-f]{64}$", digest):