2024-09-04 01:20:12 +09:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-09-16 20:31:15 +09:00
|
|
|
"git.ophivana.moe/cat/fortify/internal/final"
|
2024-09-04 01:20:12 +09:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
systemdCheckPath = "/run/systemd/system"
|
|
|
|
|
|
|
|
home = "HOME"
|
|
|
|
xdgConfigHome = "XDG_CONFIG_HOME"
|
|
|
|
|
|
|
|
PulseServer = "PULSE_SERVER"
|
|
|
|
PulseCookie = "PULSE_COOKIE"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SdBooted implements https://www.freedesktop.org/software/systemd/man/sd_booted.html
|
2024-09-12 20:53:33 +09:00
|
|
|
func SdBooted() (bool, error) {
|
2024-09-04 01:20:12 +09:00
|
|
|
_, err := os.Stat(systemdCheckPath)
|
|
|
|
if err != nil {
|
2024-09-12 20:53:33 +09:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
err = nil
|
2024-09-04 01:20:12 +09:00
|
|
|
}
|
2024-09-12 20:53:33 +09:00
|
|
|
return false, err
|
2024-09-04 01:20:12 +09:00
|
|
|
}
|
2024-09-12 20:53:33 +09:00
|
|
|
|
|
|
|
return true, nil
|
2024-09-04 01:20:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiscoverPulseCookie try various standard methods to discover the current user's PulseAudio authentication cookie
|
|
|
|
func DiscoverPulseCookie() string {
|
|
|
|
if p, ok := os.LookupEnv(PulseCookie); ok {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
if p, ok := os.LookupEnv(home); ok {
|
|
|
|
p = path.Join(p, ".pulse-cookie")
|
|
|
|
if s, err := os.Stat(p); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
2024-09-16 20:31:15 +09:00
|
|
|
final.Fatal("Error accessing PulseAudio cookie:", err)
|
2024-09-04 01:20:12 +09:00
|
|
|
// unreachable
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
} else if !s.IsDir() {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p, ok := os.LookupEnv(xdgConfigHome); ok {
|
|
|
|
p = path.Join(p, "pulse", "cookie")
|
|
|
|
if s, err := os.Stat(p); err != nil {
|
|
|
|
if !errors.Is(err, fs.ErrNotExist) {
|
2024-09-16 20:31:15 +09:00
|
|
|
final.Fatal("Error accessing PulseAudio cookie:", err)
|
2024-09-04 01:20:12 +09:00
|
|
|
// unreachable
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
} else if !s.IsDir() {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-16 20:31:15 +09:00
|
|
|
final.Fatal(fmt.Sprintf("Cannot locate PulseAudio cookie (tried $%s, $%s/pulse/cookie, $%s/.pulse-cookie)",
|
2024-09-04 01:20:12 +09:00
|
|
|
PulseCookie, xdgConfigHome, home))
|
|
|
|
return ""
|
|
|
|
}
|