diff --git a/config.go b/config.go index 2ba0b6e..261b59c 100644 --- a/config.go +++ b/config.go @@ -6,7 +6,6 @@ import ( "fmt" "git.ophivana.moe/security/fortify/dbus" - "git.ophivana.moe/security/fortify/internal" "git.ophivana.moe/security/fortify/internal/app" "git.ophivana.moe/security/fortify/internal/fmsg" "git.ophivana.moe/security/fortify/internal/system" @@ -50,7 +49,7 @@ func init() { func init() { methodHelpString := "Method of launching the child process, can be one of \"sudo\"" - if internal.SdBootedV { + if os.SdBooted() { methodHelpString += ", \"systemd\"" } diff --git a/internal/app/app_nixos_test.go b/internal/app/app_nixos_test.go index e5f57e0..1107921 100644 --- a/internal/app/app_nixos_test.go +++ b/internal/app/app_nixos_test.go @@ -586,3 +586,7 @@ func (s *stubNixOS) Paths() internal.Paths { RunDirPath: "/run/user/1971/fortify", } } + +func (s *stubNixOS) SdBooted() bool { + return true +} diff --git a/internal/app/seal.go b/internal/app/seal.go index b433b82..80c3ec2 100644 --- a/internal/app/seal.go +++ b/internal/app/seal.go @@ -108,7 +108,7 @@ func (a *app) Seal(config *Config) error { } case method[LaunchMethodMachineCtl]: seal.launchOption = LaunchMethodMachineCtl - if !internal.SdBootedV { + if !a.os.SdBooted() { return fmsg.WrapError(ErrSystemd, "system has not been booted with systemd as init system") } diff --git a/internal/early.go b/internal/early.go deleted file mode 100644 index 81ca49c..0000000 --- a/internal/early.go +++ /dev/null @@ -1,35 +0,0 @@ -package internal - -import ( - "errors" - "io/fs" - "os" - - "git.ophivana.moe/security/fortify/internal/fmsg" -) - -const ( - systemdCheckPath = "/run/systemd/system" -) - -var SdBootedV = func() bool { - if v, err := SdBooted(); err != nil { - fmsg.Println("cannot read systemd marker:", err) - return false - } else { - return v - } -}() - -// SdBooted implements https://www.freedesktop.org/software/systemd/man/sd_booted.html -func SdBooted() (bool, error) { - _, err := os.Stat(systemdCheckPath) - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - err = nil - } - return false, err - } - - return true, nil -} diff --git a/internal/system.go b/internal/system.go index e98bb67..3e973b5 100644 --- a/internal/system.go +++ b/internal/system.go @@ -1,6 +1,7 @@ package internal import ( + "errors" "io/fs" "os" "os/exec" @@ -37,6 +38,8 @@ type System interface { // Paths returns a populated [Paths] struct. Paths() Paths + // SdBooted implements https://www.freedesktop.org/software/systemd/man/sd_booted.html + SdBooted() bool } // Paths contains environment dependent paths used by fortify. @@ -71,46 +74,21 @@ func CopyPaths(os System, v *Paths) { type Std struct { paths Paths pathsOnce sync.Once + + sdBooted bool + sdBootedOnce sync.Once } -func (s *Std) Geteuid() int { - return os.Geteuid() -} - -func (s *Std) LookupEnv(key string) (string, bool) { - return os.LookupEnv(key) -} - -func (s *Std) TempDir() string { - return os.TempDir() -} - -func (s *Std) LookPath(file string) (string, error) { - return exec.LookPath(file) -} - -func (s *Std) Executable() (string, error) { - return os.Executable() -} - -func (s *Std) Lookup(username string) (*user.User, error) { - return user.Lookup(username) -} - -func (s *Std) ReadDir(name string) ([]os.DirEntry, error) { - return os.ReadDir(name) -} - -func (s *Std) Stat(name string) (fs.FileInfo, error) { - return os.Stat(name) -} - -func (s *Std) Open(name string) (fs.File, error) { - return os.Open(name) -} -func (s *Std) Exit(code int) { - fmsg.Exit(code) -} +func (s *Std) Geteuid() int { return os.Geteuid() } +func (s *Std) LookupEnv(key string) (string, bool) { return os.LookupEnv(key) } +func (s *Std) TempDir() string { return os.TempDir() } +func (s *Std) LookPath(file string) (string, error) { return exec.LookPath(file) } +func (s *Std) Executable() (string, error) { return os.Executable() } +func (s *Std) Lookup(username string) (*user.User, error) { return user.Lookup(username) } +func (s *Std) ReadDir(name string) ([]os.DirEntry, error) { return os.ReadDir(name) } +func (s *Std) Stat(name string) (fs.FileInfo, error) { return os.Stat(name) } +func (s *Std) Open(name string) (fs.File, error) { return os.Open(name) } +func (s *Std) Exit(code int) { fmsg.Exit(code) } const xdgRuntimeDir = "XDG_RUNTIME_DIR" @@ -118,3 +96,31 @@ func (s *Std) Paths() Paths { s.pathsOnce.Do(func() { CopyPaths(s, &s.paths) }) return s.paths } + +func (s *Std) SdBooted() bool { + s.sdBootedOnce.Do(func() { s.sdBooted = copySdBooted() }) + return s.sdBooted +} + +const systemdCheckPath = "/run/systemd/system" + +func copySdBooted() bool { + if v, err := sdBooted(); err != nil { + fmsg.Println("cannot read systemd marker:", err) + return false + } else { + return v + } +} + +func sdBooted() (bool, error) { + _, err := os.Stat(systemdCheckPath) + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + err = nil + } + return false, err + } + + return true, nil +} diff --git a/main.go b/main.go index 7bb7ffe..85fb868 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,7 @@ func main() { flag.Parse() fmsg.SetVerbose(flagVerbose) - if internal.SdBootedV { + if os.SdBooted() { fmsg.VPrintln("system booted with systemd as init system") }