跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

control_lock.go

80 行 · 2.2 KB历史下载
1package workbench
2
3import (
4 "errors"
5 "os"
6 "path/filepath"
7 "time"
8
9 "github.com/sylvinus/agent-vm/internal/host"
10)
11
12// The lock contains no credentials. Old Windows versions created it with the
13// Administrators group as owner. Test the OS lock before repairing its ACL;
14// credential-file ownership must not be used as a process-liveness test.
15func openControllerLock(profile string, flags int) (*os.File, error) {
16 path := filepath.Join(profile, "control.lock")
17 if info, err := os.Lstat(path); err == nil {
18 if !info.Mode().IsRegular() {
19 return nil, errors.New("控制锁必须是普通文件")
20 }
21 } else if !os.IsNotExist(err) {
22 return nil, err
23 }
24 f, err := os.OpenFile(path, flags, 0600)
25 if err != nil {
26 return nil, err
27 }
28 opened, statErr := f.Stat()
29 current, pathErr := os.Lstat(path)
30 if statErr != nil || pathErr != nil || !current.Mode().IsRegular() || !os.SameFile(opened, current) {
31 f.Close()
32 return nil, errors.New("控制锁路径已变化,请重试")
33 }
34 return f, nil
35}
36
37// A status probe can briefly own the lock while it checks or repairs its ACL.
38// Let that probe finish before deciding that another controller owns the profile.
39func lockControllerForStart(f *os.File) error {
40 deadline := time.Now().Add(2 * time.Second)
41 for {
42 err := lockController(f)
43 if err == nil || !controllerLockContended(err) || !time.Now().Before(deadline) {
44 return err
45 }
46 time.Sleep(10 * time.Millisecond)
47 }
48}
49
50func controllerLockStatus(profile string) (free bool, err error) {
51 f, err := openControllerLock(profile, os.O_RDWR)
52 if err != nil {
53 if os.IsNotExist(err) {
54 return true, nil
55 }
56 return false, err
57 }
58 defer f.Close()
59 if err := lockController(f); err != nil {
60 if controllerLockContended(err) {
61 return false, nil
62 }
63 return false, err
64 }
65 defer unlockController(f)
66 // Repair legacy ownership only while holding the same lock that protects
67 // record publication. Never unlink a live lock or relax its permissions.
68 if err := host.ProtectFile(f.Name()); err != nil {
69 return false, err
70 }
71 if err := host.CheckPrivateFile(f.Name()); err != nil {
72 return false, err
73 }
74 return true, nil
75}
76
77func controllerLockFree(profile string) bool {
78 free, err := controllerLockStatus(profile)
79 return free && err == nil
80}