| 1 | |
| 2 | |
| 3 | |
| 4 | package launchd |
| 5 | |
| 6 | import ( |
| 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 | |
| 20 | var Template string |
| 21 | |
| 22 | |
| 23 | var DaemonTemplate string |
| 24 | |
| 25 | |
| 26 | func GetPlistPath(instName string) string { |
| 27 | return fmt.Sprintf("%s/Library/LaunchAgents/%s.plist", os.Getenv("HOME"), ServiceNameFrom(instName)) |
| 28 | } |
| 29 | |
| 30 | |
| 31 | func ServiceNameFrom(instName string) string { |
| 32 | return fmt.Sprintf("io.lima-vm.autostart.%s", instName) |
| 33 | } |
| 34 | |
| 35 | |
| 36 | func 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 | |
| 44 | func 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 | |
| 52 | func 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 | |
| 59 | func AutoStartedServiceName() string { |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | if xpcServiceName := os.Getenv("XPC_SERVICE_NAME"); xpcServiceName != "0" { |
| 65 | return xpcServiceName |
| 66 | } |
| 67 | return "" |
| 68 | } |
| 69 | |
| 70 | var domainTarget = sync.OnceValue(func() string { |
| 71 | return fmt.Sprintf("gui/%d", os.Getuid()) |
| 72 | }) |
| 73 | |
| 74 | func serviceTarget(instName string) string { |
| 75 | return fmt.Sprintf("%s/%s", domainTarget(), ServiceNameFrom(instName)) |
| 76 | } |
| 77 | |
| 78 | func RequestStart(ctx context.Context, inst *limatype.Instance) error { |
| 79 | |
| 80 | |
| 81 | _ = launchctlWithoutOutput(ctx, "bootout", serviceTarget(inst.Name)) |
| 82 | |
| 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 | |
| 90 | func 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 | |
| 103 | func GetDaemonPlistPath(instName string) string { |
| 104 | return fmt.Sprintf("/Library/LaunchDaemons/%s.plist", DaemonServiceNameFrom(instName)) |
| 105 | } |
| 106 | |
| 107 | |
| 108 | func DaemonServiceNameFrom(instName string) string { |
| 109 | return fmt.Sprintf("io.lima-vm.daemon.%s", instName) |
| 110 | } |