package runnersetup import ( "context" "os" "os/exec" "path/filepath" "strconv" "strings" ) // command runs a system program from /, never the folder the installer // was started from (which this user may not read). func command(ctx context.Context, name string, args ...string) *exec.Cmd { cmd := exec.CommandContext(ctx, name, args...) cmd.Dir = "/" return cmd } func systemdQuote(s string) string { return strconv.Quote(strings.ReplaceAll(s, "%", "%%")) } func serviceUnit(binary, env string) string { return "[Unit]\nDescription=equipo-runner\n[Service]\nType=simple\nEnvironmentFile=" + systemdQuote(env) + "\nExecStart=" + systemdQuote(strings.ReplaceAll(binary, "$", "$$")) + " run\nRestart=on-failure\nRestartSec=5\nTimeoutStopSec=45\nKillMode=control-group\nUMask=0077\nNoNewPrivileges=yes\n[Install]\nWantedBy=default.target\n" } func userServiceAvailable(ctx context.Context) bool { return command(ctx, "systemctl", "--user", "show-environment").Run() == nil } func readLingering(ctx context.Context) (bool, error) { b, e := command(ctx, "loginctl", "show-user", strconv.Itoa(os.Getuid()), "--property=Linger", "--value").Output() if e != nil { return false, e } switch strings.TrimSpace(string(b)) { case "yes": return true, nil case "no": return false, nil default: return false, errUnavailable } } func enableService(ctx context.Context, path string) error { for _, args := range [][]string{{"--user", "link", path}, {"--user", "daemon-reload"}, {"--user", "enable", "equipo-runner.service"}, {"--user", "restart", "equipo-runner.service"}} { if e := command(ctx, "systemctl", args...).Run(); e != nil { return e } } return nil } // CheckServiceEnvironment refuses to enable a unit without its private local // configuration. It reads only the explicit installation root. func CheckServiceEnvironment(root, origin, state string) error { values, e := ReadServiceEnvironment(root) if e != nil { return e } if values["EQUIPO_ORIGIN"] != origin || values["EQUIPO_RUNNER_STATE_DIR"] != state { return errUnavailable } return nil } func SaveServiceEnvironment(root string, data []byte) error { return writeOwned(root+"/runner.env", data, 0600) } func ReadServiceEnvironment(root string) (map[string]string, error) { return ReadEnvFile(root + "/runner.env") } // ReadEnvFile reads a private runner environment file: EQUIPO_ keys // only, each once, values optionally quoted; the file the runner's own, // 0600, no symlink (a Mac's launchd agent passes it with --env-file). func ReadEnvFile(path string) (map[string]string, error) { if !filepath.IsAbs(path) || filepath.Clean(path) != path { return nil, errUnavailable } real, e := filepath.EvalSymlinks(path) if e != nil || real != path { return nil, errUnavailable } i, e := os.Lstat(path) if e != nil || !i.Mode().IsRegular() || i.Mode().Perm() != 0600 || i.Size() > 65536 { return nil, errUnavailable } b, e := os.ReadFile(path) if e != nil { return nil, e } values := map[string]string{} for _, line := range strings.Split(string(b), "\n") { if strings.HasPrefix(line, "#") || line == "" { continue } k, v, ok := strings.Cut(line, "=") _, duplicate := values[k] if !ok || !strings.HasPrefix(k, "EQUIPO_") || duplicate { return nil, errUnavailable } if strings.HasPrefix(v, "\"") { var err error v, err = strconv.Unquote(v) if err != nil { return nil, errUnavailable } } values[k] = v } return values, nil }