What changed: a new internal/daemon package and `magpie daemon`, which run
`magpie serve` as the user's own service, with no window and no desktop
session.
- Linux: a systemd user unit at ~/.config/systemd/user/magpie.service
(Restart=on-failure), driven by systemctl --user. status reads
`systemctl --user show`, and says `sudo loginctl enable-linger $USER`
when the user doesn't linger. su/sudo without the user's bus, no
systemd, and WSL without systemd each get an error that says what to
do.
- macOS: a launch agent, com.yetone.magpie.serve (RunAtLoad, KeepAlive on
failure), loaded with launchctl bootstrap gui/$UID. Its log is
~/Library/Logs/magpie/serve.log.
- Windows and Android: refused, pointing to `magpie autostart on` (or to
WSL).
- The service carries PATH, the proxy variables, MAGPIE_*, the XDG folders
and the agents' folder variables from the install shell. The file is
0600 because a proxy can hold a password. It runs magpie by its PATH
name when that is the same file, so Homebrew's link survives updates.
It refuses a go-build binary and an AppTranslocation copy.
- Install is refused while `magpie autostart` is on: the tray app serves
the gateway too, and the two would crash-loop on the port.
- Docs: a reference.md section, and one sentence on the site's start page
(en, zh, ja).
Why: users on headless Linux boxes asked for a supported way to keep
`magpie serve` up across reboots instead of hand-written units. The
existing `magpie autostart` needs a desktop session (an XDG autostart
entry).
What was run:
- gofmt -l; go vet on darwin, GOOS=linux -tags nogui, and GOOS=windows.
- go test ./internal/daemon ./internal/cli on macOS, at origin/main
5b9530b56.
- The daemon tests cross-built for linux/arm64 and run in a Debian
container.
- The tests use fake systemctl, launchctl and loginctl on PATH in a test
home.
- Each guarantee was broken by hand and its test went red: the % escape,
the autostart refusal, the 0600 chmod, the bootout before re-bootstrap,
the go-build refusal, the bus advice, the last-exit filter and the
PATH link.
- Real use on Linux: a Debian bookworm container running systemd as PID 1,
with the linux/arm64 build, as an unprivileged user.
- install: the gateway answered on :3425, and the unit was 0600.
- kill -9: systemd restarted it 5s later.
- Container restart with linger on: it came back by itself.
- restart, stop, start and uninstall worked, and the unit and its wants
link were gone afterwards.
- Under sudo without XDG_RUNTIME_DIR it printed the bus advice.
- macOS: the darwin build ran `magpie daemon install` under a sandbox HOME
with a fake launchctl. plutil -lint passed on the plist, and the calls
were print and then bootstrap.
What wasn't: no real launch agent was loaded on a Mac (deliberately: never
on this machine), so launchd's own acceptance of the plist is checked
only by plutil. The linger advice was checked by the unit test, not seen
from a real SSH login. Windows was not run beyond vet.
188 lines
5.7 KiB
Go
188 lines
5.7 KiB
Go
package daemon
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"os/user"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// systemd runs the service as a user unit: systemctl --user, no root.
|
|
type systemd struct{}
|
|
|
|
const unit = Name + ".service"
|
|
|
|
func (systemd) file() string {
|
|
dir := os.Getenv("XDG_CONFIG_HOME")
|
|
if dir == "" {
|
|
home, _ := os.UserHomeDir()
|
|
dir = filepath.Join(home, ".config")
|
|
}
|
|
return filepath.Join(dir, "systemd", "user", unit)
|
|
}
|
|
|
|
func (systemd) logs() string { return "journalctl --user -u " + Name + " -f" }
|
|
|
|
// unitText is the unit for exe, with env set in it.
|
|
func unitText(exe string, env []string) string {
|
|
var b strings.Builder
|
|
b.WriteString("# Written by `magpie daemon install`; `magpie daemon uninstall` removes it.\n")
|
|
b.WriteString("[Unit]\nDescription=magpie gateway\nWants=network-online.target\nAfter=network-online.target\n\n")
|
|
b.WriteString("[Service]\nType=simple\n")
|
|
b.WriteString("ExecStart=" + unitQuote(exe) + " serve\n")
|
|
b.WriteString("Restart=on-failure\nRestartSec=5\n")
|
|
for _, kv := range env {
|
|
b.WriteString("Environment=" + unitQuote(kv) + "\n")
|
|
}
|
|
b.WriteString("\n[Install]\nWantedBy=default.target\n")
|
|
return b.String()
|
|
}
|
|
|
|
// unitQuote quotes s as one word of a unit file: systemd reads \ and "
|
|
// escaped inside quotes, and % as the start of a specifier.
|
|
func unitQuote(s string) string {
|
|
s = strings.NewReplacer(`\`, `\\`, `"`, `\"`, "%", "%%", "\n", " ").Replace(s)
|
|
return `"` + s + `"`
|
|
}
|
|
|
|
func (s systemd) install(exe string, env []string) error {
|
|
if err := s.reachable(); err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(s.file()), 0o755); err != nil {
|
|
return err
|
|
}
|
|
if err := private(s.file(), []byte(unitText(exe, env))); err != nil {
|
|
return err
|
|
}
|
|
if _, err := systemctl("daemon-reload"); err != nil {
|
|
return err
|
|
}
|
|
if _, err := systemctl("enable", unit); err != nil {
|
|
return err
|
|
}
|
|
// restart, not start: an install over one running takes the new unit
|
|
_, err := systemctl("restart", unit)
|
|
return err
|
|
}
|
|
|
|
func (s systemd) uninstall() error {
|
|
if _, err := os.Stat(s.file()); err != nil {
|
|
return nil
|
|
}
|
|
if err := s.reachable(); err != nil {
|
|
return err
|
|
}
|
|
if _, err := systemctl("disable", "--now", unit); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Remove(s.file()); err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
_, err := systemctl("daemon-reload")
|
|
return err
|
|
}
|
|
|
|
func (systemd) start() error { _, err := systemctl("start", unit); return err }
|
|
func (systemd) stop() error { _, err := systemctl("stop", unit); return err }
|
|
func (systemd) restart() error { _, err := systemctl("restart", unit); return err }
|
|
|
|
func (s systemd) status() (Status, error) {
|
|
if err := s.reachable(); err != nil {
|
|
return Status{}, err
|
|
}
|
|
out, err := systemctl("show", unit, "--property=ActiveState,SubState,MainPID,UnitFileState,ExecMainStatus")
|
|
if err != nil {
|
|
return Status{}, err
|
|
}
|
|
v := map[string]string{}
|
|
for _, line := range strings.Split(out, "\n") {
|
|
if k, val, ok := strings.Cut(strings.TrimSpace(line), "="); ok {
|
|
v[k] = val
|
|
}
|
|
}
|
|
st := Status{
|
|
Enabled: v["UnitFileState"] == "enabled",
|
|
Running: v["ActiveState"] == "active",
|
|
State: v["ActiveState"],
|
|
}
|
|
if sub := v["SubState"]; sub != "" && sub != st.State {
|
|
st.State += " (" + sub + ")"
|
|
}
|
|
if pid, err := strconv.Atoi(v["MainPID"]); err == nil && pid > 0 {
|
|
st.PID = pid
|
|
}
|
|
// a stop ends it with SIGTERM's 15: only a run that failed is news
|
|
failing := st.State != "" && (v["ActiveState"] == "failed" || v["ActiveState"] == "activating")
|
|
if code := v["ExecMainStatus"]; failing && code != "" && code != "0" {
|
|
st.LastExit = code
|
|
}
|
|
if a := lingerAdvice(); a != "" {
|
|
st.Advice = append(st.Advice, a)
|
|
}
|
|
return st, nil
|
|
}
|
|
|
|
// reachable says what to do when systemctl --user can't be used: no
|
|
// systemd, or a session without the user's bus (su, sudo, some SSH).
|
|
func (systemd) reachable() error {
|
|
_, err := systemctl("is-system-running")
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if errors.Is(err, exec.ErrNotFound) {
|
|
advice := "this system has no systemd (systemctl isn't on PATH): run `magpie serve` under the service manager it has"
|
|
if wsl() {
|
|
advice += "; on WSL, turn systemd on with [boot] systemd=true in /etc/wsl.conf, then `wsl --shutdown`"
|
|
}
|
|
return errors.New(advice)
|
|
}
|
|
msg := err.Error()
|
|
if strings.Contains(msg, "bus") || strings.Contains(msg, "XDG_RUNTIME_DIR") {
|
|
return fmt.Errorf("systemctl --user can't reach your session's service manager: log in as this user directly (not through su or sudo), or set XDG_RUNTIME_DIR=/run/user/%d (%s)", os.Getuid(), msg)
|
|
}
|
|
// is-system-running exits non-zero for "degraded" too, which still
|
|
// runs user units
|
|
return nil
|
|
}
|
|
|
|
func wsl() bool {
|
|
b, _ := os.ReadFile("/proc/version")
|
|
return strings.Contains(strings.ToLower(string(b)), "microsoft")
|
|
}
|
|
|
|
// lingerAdvice is what to run so the service starts at boot and keeps
|
|
// running after logging out, when it doesn't: systemd starts a user's
|
|
// units with their first login and stops them with their last, unless the
|
|
// user lingers. Empty when it does, or when loginctl can't tell.
|
|
func lingerAdvice() string {
|
|
name := os.Getenv("USER")
|
|
if u, err := user.Current(); err == nil && u.Username != "" {
|
|
name = u.Username
|
|
}
|
|
if name == "" {
|
|
return ""
|
|
}
|
|
out, err := run("loginctl", "show-user", name, "--property=Linger", "--value")
|
|
if err != nil || strings.TrimSpace(out) != "no" {
|
|
return ""
|
|
}
|
|
return "it starts when you log in and stops when you log out; to run it from boot, with no one logged in: sudo loginctl enable-linger " + name
|
|
}
|
|
|
|
// Linger is lingerAdvice, for after an install.
|
|
func Linger() string {
|
|
if goos != "linux" {
|
|
return ""
|
|
}
|
|
return lingerAdvice()
|
|
}
|
|
|
|
func systemctl(args ...string) (string, error) {
|
|
return run("systemctl", append([]string{"--user"}, args...)...)
|
|
}
|