跳到主要内容

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

浏览文件

launchd.go

110 行 · 3.8 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package launchd
5
6import (
7 "context"
8 _ "embed"
9 "fmt"
10 "os"
11 "os/exec"
12 "sync"
13
14 "github.com/sirupsen/logrus"
15
16 "github.com/lima-vm/lima/v2/pkg/limatype"
17)
18
19//go:embed io.lima-vm.autostart.INSTANCE.plist
20var Template string
21
22//go:embed io.lima-vm.daemon.INSTANCE.plist
23var DaemonTemplate string
24
25// GetPlistPath returns the path to the launchd plist file for the given instance name.
26func GetPlistPath(instName string) string {
27 return fmt.Sprintf("%s/Library/LaunchAgents/%s.plist", os.Getenv("HOME"), ServiceNameFrom(instName))
28}
29
30// ServiceNameFrom returns the launchd service name for the given instance name.
31func ServiceNameFrom(instName string) string {
32 return fmt.Sprintf("io.lima-vm.autostart.%s", instName)
33}
34
35// EnableDisableService enables or disables the launchd service for the given instance name.
36func EnableDisableService(ctx context.Context, enable bool, instName string) error {
37 action := "enable"
38 if !enable {
39 action = "disable"
40 }
41 return launchctl(ctx, action, serviceTarget(instName))
42}
43
44func launchctl(ctx context.Context, args ...string) error {
45 cmd := exec.CommandContext(ctx, "launchctl", args...)
46 cmd.Stdout = os.Stdout
47 cmd.Stderr = os.Stderr
48 logrus.Debugf("running command: %v", cmd.Args)
49 return cmd.Run()
50}
51
52func launchctlWithoutOutput(ctx context.Context, args ...string) error {
53 cmd := exec.CommandContext(ctx, "launchctl", args...)
54 logrus.Debugf("running command without output: %v", cmd.Args)
55 return cmd.Run()
56}
57
58// AutoStartedServiceName returns the launchd service name if the instance is started by launchd.
59func AutoStartedServiceName() string {
60 // Assume the instance is started by launchd if XPC_SERVICE_NAME is set and not "0".
61 // To confirm it is actually started by launchd, it needs to use `launch_activate_socket`.
62 // But that requires actual socket activation setup in the plist file.
63 // So we just check XPC_SERVICE_NAME here.
64 if xpcServiceName := os.Getenv("XPC_SERVICE_NAME"); xpcServiceName != "0" {
65 return xpcServiceName
66 }
67 return ""
68}
69
70var domainTarget = sync.OnceValue(func() string {
71 return fmt.Sprintf("gui/%d", os.Getuid())
72})
73
74func serviceTarget(instName string) string {
75 return fmt.Sprintf("%s/%s", domainTarget(), ServiceNameFrom(instName))
76}
77
78func RequestStart(ctx context.Context, inst *limatype.Instance) error {
79 // Call `launchctl bootout` first, because instance may be stopped without unloading the plist file.
80 // If the plist file is not unloaded, `launchctl bootstrap` will fail.
81 _ = launchctlWithoutOutput(ctx, "bootout", serviceTarget(inst.Name))
82 // If disabled, `launchctl bootstrap` will fail.
83 _ = EnableDisableService(ctx, true, inst.Name)
84 if err := launchctl(ctx, "bootstrap", domainTarget(), GetPlistPath(inst.Name)); err != nil {
85 return fmt.Errorf("failed to start the instance %#q via launchctl: %w", inst.Name, err)
86 }
87 return nil
88}
89
90func RequestStop(ctx context.Context, inst *limatype.Instance) (bool, error) {
91 logrus.Debugf("AutoStartedIdentifier=%#q, ServiceNameFrom=%#q", inst.AutoStartedIdentifier, ServiceNameFrom(inst.Name))
92 if inst.AutoStartedIdentifier == ServiceNameFrom(inst.Name) {
93 logrus.Infof("Stopping the instance %#q started by launchd", inst.Name)
94 if err := launchctl(ctx, "bootout", serviceTarget(inst.Name)); err != nil {
95 return false, fmt.Errorf("failed to stop the instance %#q via launchctl: %w", inst.Name, err)
96 }
97 return true, nil
98 }
99 return false, nil
100}
101
102// GetDaemonPlistPath returns the path to the system LaunchDaemon plist for the given instance.
103func GetDaemonPlistPath(instName string) string {
104 return fmt.Sprintf("/Library/LaunchDaemons/%s.plist", DaemonServiceNameFrom(instName))
105}
106
107// DaemonServiceNameFrom returns the launchd daemon service name for the given instance name.
108func DaemonServiceNameFrom(instName string) string {
109 return fmt.Sprintf("io.lima-vm.daemon.%s", instName)
110}