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() {}