From 673b648bd35285a1dbfc0348716dd6bdd8064a51 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Wed, 26 Feb 2025 19:46:43 +0900 Subject: cmd/fpkg: call app in-process Wrapping fortify is slow, painful and error-prone. Start apps in-process instead. Signed-off-by: Ophestra --- cmd/fpkg/proc.go | 57 +++++++++++++------------------------------------------- 1 file changed, 13 insertions(+), 44 deletions(-) (limited to 'cmd/fpkg/proc.go') diff --git a/cmd/fpkg/proc.go b/cmd/fpkg/proc.go index 538c5ed4..ea106adf 100644 --- a/cmd/fpkg/proc.go +++ b/cmd/fpkg/proc.go @@ -1,59 +1,28 @@ package main import ( - "encoding/json" - "errors" - "io" - "log" + "context" "os" - "os/exec" "git.gensokyo.uk/security/fortify/fst" - "git.gensokyo.uk/security/fortify/internal" + "git.gensokyo.uk/security/fortify/internal/app" "git.gensokyo.uk/security/fortify/internal/fmsg" ) -func fortifyApp(config *fst.Config, beforeFail func()) { - var ( - cmd *exec.Cmd - st io.WriteCloser - ) - if p, ok := internal.Path(internal.Fortify); !ok { - beforeFail() - log.Fatal("invalid fortify path, this copy of fpkg is not compiled correctly") - } else if r, w, err := os.Pipe(); err != nil { - beforeFail() - log.Fatalf("cannot pipe: %v", err) +func mustRunApp(ctx context.Context, config *fst.Config, beforeFail func()) { + rs := new(fst.RunState) + a := app.MustNew(std) + + if sa, err := a.Seal(config); err != nil { + fmsg.PrintBaseError(err, "cannot seal app:") + rs.ExitCode = 1 } else { - if fmsg.Load() { - cmd = exec.Command(p, "-v", "app", "3") - } else { - cmd = exec.Command(p, "app", "3") - } - cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr - cmd.ExtraFiles = []*os.File{r} - st = w + // this updates ExitCode + app.PrintRunStateErr(rs, sa.Run(ctx, rs)) } - go func() { - if err := json.NewEncoder(st).Encode(config); err != nil { - beforeFail() - log.Fatalf("cannot send configuration: %v", err) - } - }() - - if err := cmd.Start(); err != nil { + if rs.ExitCode != 0 { beforeFail() - log.Fatalf("cannot start fortify: %v", err) - } - if err := cmd.Wait(); err != nil { - var exitError *exec.ExitError - if errors.As(err, &exitError) { - beforeFail() - internal.Exit(exitError.ExitCode()) - } else { - beforeFail() - log.Fatalf("cannot wait: %v", err) - } + os.Exit(rs.ExitCode) } } -- cgit v1.3.1