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>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user