// Daemon discovery, so that `dipstick refresh` can reach the running daemon. // // A pidfile rather than a systemd unit name: the daemon must also be // reachable when it was started by hand, and asking systemd would tie a // developer's session to the packaged unit. It is verified against // /proc before signalling, because a pidfile left behind by an unclean // exit points at whatever process later took that pid. package daemon import ( "errors" "fmt" "os" "path/filepath" "strconv" "strings" "syscall" ) // ErrNotRunning is returned when no daemon can be found. It is a normal // outcome, not a failure: the user may simply not have started the service. var ErrNotRunning = errors.New("the dipstick daemon is not running") // PIDPath is where the running daemon records itself. // // The runtime directory is the right home for it - the kernel empties it on // logout, so a pidfile cannot survive the session that produced it - but it // is not guaranteed to exist, so the configuration directory is the fallback. func PIDPath() (string, error) { if dir := os.Getenv("XDG_RUNTIME_DIR"); dir != "" { return filepath.Join(dir, "dipstick", "daemon.pid"), nil } dir, err := os.UserConfigDir() if err != nil { return "", err } return filepath.Join(dir, "dipstick", "daemon.pid"), nil } // WritePID records this process as the daemon and returns the function that // removes the record again. // // A failure here is not fatal: it costs the refresh button, not the service. func WritePID() (remove func(), err error) { path, err := PIDPath() if err != nil { return func() {}, err } if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { return func() {}, err } if err := os.WriteFile(path, []byte(strconv.Itoa(os.Getpid())+"\n"), 0o644); err != nil { return func() {}, err } return func() { os.Remove(path) }, nil } // Refresh asks the running daemon for an immediate round. func Refresh() error { pid, err := readPID() if err != nil { return err } if err := syscall.Kill(pid, syscall.SIGUSR1); err != nil { if errors.Is(err, syscall.ESRCH) { return ErrNotRunning } return fmt.Errorf("signalling the daemon (pid %d): %w", pid, err) } return nil } func readPID() (int, error) { path, err := PIDPath() if err != nil { return 0, err } raw, err := os.ReadFile(path) if err != nil { return 0, ErrNotRunning } pid, err := strconv.Atoi(strings.TrimSpace(string(raw))) if err != nil || pid <= 0 { return 0, ErrNotRunning } if !isDipstick(pid) { return 0, ErrNotRunning } return pid, nil } // daemonComm is the process name a recorded pid must have. It is a variable // only so the tests can point it at the test binary and exercise the real // check instead of a stub. var daemonComm = "dipstick" // isDipstick guards against pid reuse: without it a stale pidfile turns the // refresh button into a signal aimed at an unrelated process. func isDipstick(pid int) bool { comm, err := os.ReadFile(filepath.Join("/proc", strconv.Itoa(pid), "comm")) if err != nil { return false } return strings.TrimSpace(string(comm)) == daemonComm }