From 02d66a141e41e174302a1d8339b34ed05f814d92 Mon Sep 17 00:00:00 2001 From: m3ta-chiron Date: Sat, 14 Nov 2026 20:38:37 +0100 Subject: [PATCH] fix(linux): fall back to /run/user/ when XDG_RUNTIME_DIR is unset A daemon autostarted from an environment without XDG_RUNTIME_DIR (e.g. an agent running under a systemd system service) could not reach the PipeWire/PulseAudio socket: oto dialed the relative path "pulse/native" and ALSA reported "Host is down". `talk say` then showed the desktop notification (godbus has its own /run/user fallback) but played no audio. talk now sets XDG_RUNTIME_DIR to /run/user/ at startup when it is empty and that directory exists and belongs to the current user; a spawned daemon inherits it. Co-Authored-By: Claude Opus 5.5 --- cmd/talk/main.go | 1 + cmd/talk/runtimedir_linux.go | 35 +++++++++++++++++++++++++++++++++++ cmd/talk/runtimedir_other.go | 6 ++++++ 3 files changed, 42 insertions(+) create mode 100644 cmd/talk/runtimedir_linux.go create mode 100644 cmd/talk/runtimedir_other.go diff --git a/cmd/talk/main.go b/cmd/talk/main.go index 7990604..8fce545 100644 --- a/cmd/talk/main.go +++ b/cmd/talk/main.go @@ -36,5 +36,6 @@ func buildVersion() string { } func main() { + ensureRuntimeDir() os.Exit(run(os.Args[1:], os.Stdin, os.Stdout, os.Stderr)) } diff --git a/cmd/talk/runtimedir_linux.go b/cmd/talk/runtimedir_linux.go new file mode 100644 index 0000000..a6ef5a5 --- /dev/null +++ b/cmd/talk/runtimedir_linux.go @@ -0,0 +1,35 @@ +//go:build linux + +package main + +import ( + "fmt" + "os" + "syscall" +) + +// ensureRuntimeDir fills in XDG_RUNTIME_DIR when it is missing. +// +// Processes started from a systemd system service, cron or a sudo/su shell +// often lack it, while the user's session (PipeWire/PulseAudio socket, +// D-Bus) still lives in /run/user/. Without it oto dials the relative +// path "pulse/native" and the pipewire ALSA plugin reports "Host is down", +// so the daemon shows the desktop notification (godbus has its own +// /run/user fallback) but plays nothing. The directory is only used when it +// exists and belongs to the current user. The spawned daemon inherits the +// value via os.Environ(). +func ensureRuntimeDir() { + if os.Getenv("XDG_RUNTIME_DIR") != "" { + return + } + uid := os.Getuid() + dir := fmt.Sprintf("/run/user/%d", uid) + fi, err := os.Stat(dir) + if err != nil || !fi.IsDir() { + return + } + if st, ok := fi.Sys().(*syscall.Stat_t); !ok || int(st.Uid) != uid { + return + } + _ = os.Setenv("XDG_RUNTIME_DIR", dir) +} diff --git a/cmd/talk/runtimedir_other.go b/cmd/talk/runtimedir_other.go new file mode 100644 index 0000000..85c2869 --- /dev/null +++ b/cmd/talk/runtimedir_other.go @@ -0,0 +1,6 @@ +//go:build !linux + +package main + +// ensureRuntimeDir is a no-op outside Linux (see runtimedir_linux.go). +func ensureRuntimeDir() {}