diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-02-16 17:26:09 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-02-16 18:51:53 +0900 |
| commit | e599b5583dd821ed26bdfc01f7f67d4a317b9843 (patch) | |
| tree | b444a3dc964814360e6779bc10b30f2e599b537c /cmd | |
| parent | 33a4ab11c226c736c0d8b2644fbbe27a5ca045e9 (diff) | |
fmsg: implement suspend in writer
This removes the requirement to call fmsg.Exit on every exit path, and enables direct use of the "log" package. However, fmsg.BeforeExit is still encouraged when possible to catch exit on suspended output.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/fpkg/bundle.go | 12 | ||||
| -rw-r--r-- | cmd/fpkg/install.go | 38 | ||||
| -rw-r--r-- | cmd/fpkg/main.go | 14 | ||||
| -rw-r--r-- | cmd/fpkg/paths.go | 10 | ||||
| -rw-r--r-- | cmd/fpkg/proc.go | 21 | ||||
| -rw-r--r-- | cmd/fpkg/start.go | 9 | ||||
| -rw-r--r-- | cmd/fpkg/with.go | 4 |
7 files changed, 51 insertions, 57 deletions
diff --git a/cmd/fpkg/bundle.go b/cmd/fpkg/bundle.go index 5c9db725..5692d5cb 100644 --- a/cmd/fpkg/bundle.go +++ b/cmd/fpkg/bundle.go @@ -2,10 +2,10 @@ package main import ( "encoding/json" + "log" "os" "git.gensokyo.uk/security/fortify/dbus" - "git.gensokyo.uk/security/fortify/internal/fmsg" "git.gensokyo.uk/security/fortify/internal/system" ) @@ -63,18 +63,18 @@ func loadBundleInfo(name string, beforeFail func()) *bundleInfo { bundle := new(bundleInfo) if f, err := os.Open(name); err != nil { beforeFail() - fmsg.Fatalf("cannot open bundle: %v", err) + log.Fatalf("cannot open bundle: %v", err) } else if err = json.NewDecoder(f).Decode(&bundle); err != nil { beforeFail() - fmsg.Fatalf("cannot parse bundle metadata: %v", err) + log.Fatalf("cannot parse bundle metadata: %v", err) } else if err = f.Close(); err != nil { - fmsg.Printf("cannot close bundle metadata: %v", err) + log.Printf("cannot close bundle metadata: %v", err) // not fatal } if bundle.ID == "" { beforeFail() - fmsg.Fatal("application identifier must not be empty") + log.Fatal("application identifier must not be empty") } return bundle @@ -82,7 +82,7 @@ func loadBundleInfo(name string, beforeFail func()) *bundleInfo { func formatHostname(name string) string { if h, err := os.Hostname(); err != nil { - fmsg.Printf("cannot get hostname: %v", err) + log.Printf("cannot get hostname: %v", err) return "fortify-" + name } else { return h + "-" + name diff --git a/cmd/fpkg/install.go b/cmd/fpkg/install.go index 7c06461d..d1643b40 100644 --- a/cmd/fpkg/install.go +++ b/cmd/fpkg/install.go @@ -3,10 +3,12 @@ package main import ( "encoding/json" "flag" + "log" "os" "path" "git.gensokyo.uk/security/fortify/fst" + "git.gensokyo.uk/security/fortify/internal" "git.gensokyo.uk/security/fortify/internal/fmsg" ) @@ -25,12 +27,12 @@ func actionInstall(args []string) { args = set.Args() if len(args) != 1 { - fmsg.Fatal("invalid argument") + log.Fatal("invalid argument") } pkgPath := args[0] if !path.IsAbs(pkgPath) { if dir, err := os.Getwd(); err != nil { - fmsg.Fatalf("cannot get current directory: %v", err) + log.Fatalf("cannot get current directory: %v", err) } else { pkgPath = path.Join(dir, pkgPath) } @@ -54,7 +56,7 @@ func actionInstall(args []string) { var workDir string if p, err := os.MkdirTemp("", "fpkg.*"); err != nil { - fmsg.Fatalf("cannot create temporary directory: %v", err) + log.Fatalf("cannot create temporary directory: %v", err) } else { workDir = p } @@ -78,19 +80,17 @@ func actionInstall(args []string) { if s, err := os.Stat(pathSet.metaPath); err != nil { if !os.IsNotExist(err) { cleanup() - fmsg.Fatalf("cannot access %q: %v", pathSet.metaPath, err) - panic("unreachable") + log.Fatalf("cannot access %q: %v", pathSet.metaPath, err) } // did not modify app, clean installation condition met later } else if s.IsDir() { cleanup() - fmsg.Fatalf("metadata path %q is not a file", pathSet.metaPath) - panic("unreachable") + log.Fatalf("metadata path %q is not a file", pathSet.metaPath) } else { app = loadBundleInfo(pathSet.metaPath, cleanup) if app.ID != bundle.ID { cleanup() - fmsg.Fatalf("app %q claims to have identifier %q", bundle.ID, app.ID) + log.Fatalf("app %q claims to have identifier %q", bundle.ID, app.ID) } // sec: should verify credentials } @@ -102,21 +102,20 @@ func actionInstall(args []string) { app.Launcher == bundle.Launcher && app.ActivationPackage == bundle.ActivationPackage { cleanup() - fmsg.Printf("package %q is identical to local application %q", pkgPath, app.ID) - fmsg.Exit(0) + log.Printf("package %q is identical to local application %q", pkgPath, app.ID) + internal.Exit(0) } // AppID determines uid if app.AppID != bundle.AppID { cleanup() - fmsg.Fatalf("package %q app id %d differs from installed %d", pkgPath, bundle.AppID, app.AppID) - panic("unreachable") + log.Fatalf("package %q app id %d differs from installed %d", pkgPath, bundle.AppID, app.AppID) } // sec: should compare version string - fmsg.VPrintf("installing application %q version %q over local %q", bundle.ID, bundle.Version, app.Version) + fmsg.Verbosef("installing application %q version %q over local %q", bundle.ID, bundle.Version, app.Version) } else { - fmsg.VPrintf("application %q clean installation", bundle.ID) + fmsg.Verbosef("application %q clean installation", bundle.ID) // sec: should install credentials } @@ -174,21 +173,18 @@ func actionInstall(args []string) { // serialise metadata to ensure consistency if f, err := os.OpenFile(pathSet.metaPath+"~", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644); err != nil { cleanup() - fmsg.Fatalf("cannot create metadata file: %v", err) - panic("unreachable") + log.Fatalf("cannot create metadata file: %v", err) } else if err = json.NewEncoder(f).Encode(bundle); err != nil { cleanup() - fmsg.Fatalf("cannot write metadata: %v", err) - panic("unreachable") + log.Fatalf("cannot write metadata: %v", err) } else if err = f.Close(); err != nil { - fmsg.Printf("cannot close metadata file: %v", err) + log.Printf("cannot close metadata file: %v", err) // not fatal } if err := os.Rename(pathSet.metaPath+"~", pathSet.metaPath); err != nil { cleanup() - fmsg.Fatalf("cannot rename metadata file: %v", err) - panic("unreachable") + log.Fatalf("cannot rename metadata file: %v", err) } cleanup() diff --git a/cmd/fpkg/main.go b/cmd/fpkg/main.go index f348cb5e..e95a36cb 100644 --- a/cmd/fpkg/main.go +++ b/cmd/fpkg/main.go @@ -2,8 +2,10 @@ package main import ( "flag" + "log" "os" + "git.gensokyo.uk/security/fortify/internal" "git.gensokyo.uk/security/fortify/internal/fmsg" ) @@ -11,7 +13,7 @@ const shell = "/run/current-system/sw/bin/bash" func init() { if err := os.Setenv("SHELL", shell); err != nil { - fmsg.Fatalf("cannot set $SHELL: %v", err) + log.Fatalf("cannot set $SHELL: %v", err) } } @@ -24,14 +26,14 @@ func init() { } func main() { - fmsg.SetPrefix("fpkg") + fmsg.Prepare("fpkg") flag.Parse() - fmsg.SetVerbose(flagVerbose) + fmsg.Store(flagVerbose) args := flag.Args() if len(args) < 1 { - fmsg.Fatal("invalid argument") + log.Fatal("invalid argument") } switch args[0] { @@ -41,8 +43,8 @@ func main() { actionStart(args[1:]) default: - fmsg.Fatal("invalid argument") + log.Fatal("invalid argument") } - fmsg.Exit(0) + internal.Exit(0) } diff --git a/cmd/fpkg/paths.go b/cmd/fpkg/paths.go index cf36c096..231388e5 100644 --- a/cmd/fpkg/paths.go +++ b/cmd/fpkg/paths.go @@ -1,6 +1,7 @@ package main import ( + "log" "os" "os/exec" "path" @@ -25,8 +26,8 @@ func init() { func lookPath(file string) string { if p, err := exec.LookPath(file); err != nil { - fmsg.Fatalf("%s: command not found", file) - panic("unreachable") + log.Fatalf("%s: command not found", file) + return "" } else { return p } @@ -35,15 +36,14 @@ func lookPath(file string) string { var beforeRunFail = new(atomic.Pointer[func()]) func mustRun(name string, arg ...string) { - fmsg.VPrintf("spawning process: %q %q", name, arg) + fmsg.Verbosef("spawning process: %q %q", name, arg) cmd := exec.Command(name, arg...) cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr if err := cmd.Run(); err != nil { if f := beforeRunFail.Swap(nil); f != nil { (*f)() } - fmsg.Fatalf("%s: %v", name, err) - panic("unreachable") + log.Fatalf("%s: %v", name, err) } } diff --git a/cmd/fpkg/proc.go b/cmd/fpkg/proc.go index 7fa6df5a..4413496a 100644 --- a/cmd/fpkg/proc.go +++ b/cmd/fpkg/proc.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "io" + "log" "os" "os/exec" @@ -25,14 +26,12 @@ func fortifyApp(config *fst.Config, beforeFail func()) { ) if p, ok := internal.Path(Fmain); !ok { beforeFail() - fmsg.Fatal("invalid fortify path, this copy of fpkg is not compiled correctly") - panic("unreachable") + log.Fatal("invalid fortify path, this copy of fpkg is not compiled correctly") } else if r, w, err := os.Pipe(); err != nil { beforeFail() - fmsg.Fatalf("cannot pipe: %v", err) - panic("unreachable") + log.Fatalf("cannot pipe: %v", err) } else { - if fmsg.Verbose() { + if fmsg.Load() { cmd = exec.Command(p, "-v", "app", "3") } else { cmd = exec.Command(p, "app", "3") @@ -45,26 +44,22 @@ func fortifyApp(config *fst.Config, beforeFail func()) { go func() { if err := json.NewEncoder(st).Encode(config); err != nil { beforeFail() - fmsg.Fatalf("cannot send configuration: %v", err) - panic("unreachable") + log.Fatalf("cannot send configuration: %v", err) } }() if err := cmd.Start(); err != nil { beforeFail() - fmsg.Fatalf("cannot start fortify: %v", err) - panic("unreachable") + log.Fatalf("cannot start fortify: %v", err) } if err := cmd.Wait(); err != nil { var exitError *exec.ExitError if errors.As(err, &exitError) { beforeFail() - fmsg.Exit(exitError.ExitCode()) - panic("unreachable") + internal.Exit(exitError.ExitCode()) } else { beforeFail() - fmsg.Fatalf("cannot wait: %v", err) - panic("unreachable") + log.Fatalf("cannot wait: %v", err) } } } diff --git a/cmd/fpkg/start.go b/cmd/fpkg/start.go index 4a882110..71e00281 100644 --- a/cmd/fpkg/start.go +++ b/cmd/fpkg/start.go @@ -2,11 +2,12 @@ package main import ( "flag" + "log" "path" "git.gensokyo.uk/security/fortify/fst" "git.gensokyo.uk/security/fortify/helper/bwrap" - "git.gensokyo.uk/security/fortify/internal/fmsg" + "git.gensokyo.uk/security/fortify/internal" ) func actionStart(args []string) { @@ -26,7 +27,7 @@ func actionStart(args []string) { args = set.Args() if len(args) < 1 { - fmsg.Fatal("invalid argument") + log.Fatal("invalid argument") } /* @@ -37,7 +38,7 @@ func actionStart(args []string) { pathSet := pathSetByApp(id) app := loadBundleInfo(pathSet.metaPath, func() {}) if app.ID != id { - fmsg.Fatalf("app %q claims to have identifier %q", id, app.ID) + log.Fatalf("app %q claims to have identifier %q", id, app.ID) } /* @@ -144,7 +145,7 @@ func actionStart(args []string) { */ fortifyApp(config, func() {}) - fmsg.Exit(0) + internal.Exit(0) } func appendGPUFilesystem(config *fst.Config) { diff --git a/cmd/fpkg/with.go b/cmd/fpkg/with.go index 03718dd0..6f5b225e 100644 --- a/cmd/fpkg/with.go +++ b/cmd/fpkg/with.go @@ -6,7 +6,7 @@ import ( "git.gensokyo.uk/security/fortify/fst" "git.gensokyo.uk/security/fortify/helper/bwrap" - "git.gensokyo.uk/security/fortify/internal/fmsg" + "git.gensokyo.uk/security/fortify/internal" ) func withNixDaemon( @@ -95,7 +95,7 @@ func fortifyAppDropShell(config *fst.Config, dropShell bool, beforeFail func()) config.Command = []string{shell, "-l"} fortifyApp(config, beforeFail) beforeFail() - fmsg.Exit(0) + internal.Exit(0) } fortifyApp(config, beforeFail) } |
