2024-09-22 00:29:36 +09:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2024-10-21 20:47:02 +09:00
|
|
|
"git.ophivana.moe/security/fortify/internal/fmsg"
|
2024-09-22 00:29:36 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// state that remain constant for the lifetime of the process
|
|
|
|
// fetched and cached here
|
|
|
|
|
|
|
|
const (
|
|
|
|
xdgRuntimeDir = "XDG_RUNTIME_DIR"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SystemConstants contains state from the operating system
|
|
|
|
type SystemConstants struct {
|
|
|
|
// path to shared directory e.g. /tmp/fortify.%d
|
|
|
|
SharePath string `json:"share_path"`
|
|
|
|
// XDG_RUNTIME_DIR value e.g. /run/user/%d
|
|
|
|
RuntimePath string `json:"runtime_path"`
|
|
|
|
// application runtime directory e.g. /run/user/%d/fortify
|
|
|
|
RunDirPath string `json:"run_dir_path"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
scVal SystemConstants
|
|
|
|
scOnce sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
func copySC() {
|
|
|
|
sc := SystemConstants{
|
|
|
|
SharePath: path.Join(os.TempDir(), "fortify."+strconv.Itoa(os.Geteuid())),
|
|
|
|
}
|
|
|
|
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintf("process share directory at %q", sc.SharePath)
|
2024-09-22 00:29:36 +09:00
|
|
|
|
|
|
|
// runtimePath, runDirPath
|
|
|
|
if r, ok := os.LookupEnv(xdgRuntimeDir); !ok {
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.Println("variable", xdgRuntimeDir, "unset")
|
2024-09-22 00:29:36 +09:00
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
|
|
|
sc.RuntimePath = r
|
|
|
|
sc.RunDirPath = path.Join(sc.RuntimePath, "fortify")
|
2024-10-21 20:47:02 +09:00
|
|
|
fmsg.VPrintf("XDG runtime directory at %q", sc.RunDirPath)
|
2024-09-22 00:29:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
scVal = sc
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSC returns a populated SystemConstants value
|
|
|
|
func GetSC() SystemConstants {
|
|
|
|
scOnce.Do(copySC)
|
|
|
|
return scVal
|
|
|
|
}
|