跳到主要内容

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

浏览文件

pidfile.go

128 行 · 3.7 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package store
5
6import (
7 "errors"
8 "fmt"
9 "os"
10 "path/filepath"
11 "strings"
12
13 "github.com/sirupsen/logrus"
14
15 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
16 "github.com/lima-vm/lima/v2/pkg/osutil"
17)
18
19// pidFileSuffix is the suffix of the PID files of an instance.
20const pidFileSuffix = ".pid"
21
22// bootSessionID is a variable so that it can be replaced in the tests.
23var bootSessionID = osutil.BootSessionID
24
25// currentBootSessionID returns the ID of the current boot of the host, or an empty string
26// on the platforms that do not provide one. Any other failure is returned as an error:
27// guessing would either void a PID file that is current, or trust one that is not.
28func currentBootSessionID() (string, error) {
29 id, err := bootSessionID()
30 if errors.Is(err, osutil.ErrBootSessionNotSupported) {
31 // PID files are handled the way they were before the marker was introduced.
32 return "", nil
33 }
34 if err != nil {
35 return "", fmt.Errorf("failed to determine the boot session ID: %w", err)
36 }
37 return id, nil
38}
39
40// WritePIDFile writes pid to path.
41//
42// The directory of path holds a marker with the boot session ID of the host, so that
43// ReadPIDFile can tell whether a PID file was written during the current boot. When the
44// marker is from a previous boot, every PID file of the directory is removed before the
45// marker is refreshed: those PIDs are meaningless, and refreshing the marker would
46// otherwise make them look current.
47func WritePIDFile(path string, pid int) error {
48 dir := filepath.Dir(path)
49 current, err := currentBootSessionID()
50 if err != nil {
51 return err
52 }
53 if current != "" {
54 stale, err := staleBootSession(dir, current)
55 if err != nil {
56 return err
57 }
58 if stale {
59 if err := removePIDFiles(dir); err != nil {
60 return err
61 }
62 }
63 if err := writeBootSessionMarker(dir, current); err != nil {
64 return err
65 }
66 }
67 return writePIDFileAtomically(path, pid)
68}
69
70// writePIDFileAtomically renames the PID file into place, so that a reader never sees a partially
71// written one.
72func writePIDFileAtomically(path string, pid int) error {
73 tmpPath := path + ".tmp"
74 if err := os.WriteFile(tmpPath, fmt.Appendf(nil, "%d\n", pid), 0o644); err != nil {
75 return err
76 }
77 if err := os.Rename(tmpPath, path); err != nil {
78 _ = os.Remove(tmpPath)
79 return err
80 }
81 return nil
82}
83
84// pidFileFromPreviousBoot returns true when the PID file at path was written during a
85// previous boot of the host.
86func pidFileFromPreviousBoot(path string) (bool, error) {
87 current, err := currentBootSessionID()
88 if err != nil || current == "" {
89 return false, err
90 }
91 return staleBootSession(filepath.Dir(path), current)
92}
93
94// staleBootSession returns true when dir holds a boot session marker of a boot other than
95// the current one.
96func staleBootSession(dir, current string) (bool, error) {
97 b, err := os.ReadFile(filepath.Join(dir, filenames.BootSession))
98 if err != nil {
99 if errors.Is(err, os.ErrNotExist) {
100 // No marker: the PID files were written by an older version of Lima.
101 return false, nil
102 }
103 return false, err
104 }
105 return strings.TrimSpace(string(b)) != current, nil
106}
107
108func writeBootSessionMarker(dir, id string) error {
109 return os.WriteFile(filepath.Join(dir, filenames.BootSession), []byte(id+"\n"), 0o644)
110}
111
112func removePIDFiles(dir string) error {
113 entries, err := os.ReadDir(dir)
114 if err != nil {
115 return err
116 }
117 for _, entry := range entries {
118 if entry.IsDir() || !strings.HasSuffix(entry.Name(), pidFileSuffix) {
119 continue
120 }
121 path := filepath.Join(dir, entry.Name())
122 logrus.Infof("Removing PID file %#q left behind by a previous boot", path)
123 if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
124 return err
125 }
126 }
127 return nil
128}