aboutsummaryrefslogtreecommitdiffhomepage
path: root/helper/helper.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-03-15 02:10:22 +0900
committerOphestra <cat@gensokyo.uk>2025-03-15 02:10:22 +0900
commit0f1f0e43643d302494c0dac8bbc3eda7813f06bb (patch)
tree4730fd645c2b958a48495ee4a5644e538bf0ba8b /helper/helper.go
parentf9bf20a3c75d01cf597477231f9bbb61f15cd4fd (diff)
helper: combine helper ipc setup
The two-step args call is no longer necessary since stat is passed on initialisation. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'helper/helper.go')
-rw-r--r--helper/helper.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/helper/helper.go b/helper/helper.go
index ab276442..ac45ac79 100644
--- a/helper/helper.go
+++ b/helper/helper.go
@@ -2,9 +2,14 @@
package helper
import (
+ "context"
"fmt"
+ "io"
+ "os"
"os/exec"
"time"
+
+ "git.gensokyo.uk/security/fortify/helper/proc"
)
var (
@@ -28,3 +33,56 @@ type Helper interface {
fmt.Stringer
}
+
+func newHelperFiles(
+ ctx context.Context,
+ wt io.WriterTo,
+ stat bool,
+ argF func(argsFd, statFd int) []string,
+ extraFiles []*os.File,
+) (hl *helperFiles, args []string) {
+ hl = new(helperFiles)
+ hl.ctx = ctx
+ hl.useArgsFd = wt != nil
+ hl.useStatFd = stat
+
+ hl.extraFiles = new(proc.ExtraFilesPre)
+ for _, f := range extraFiles {
+ _, v := hl.extraFiles.Append()
+ *v = f
+ }
+
+ argsFd := -1
+ if hl.useArgsFd {
+ f := proc.NewWriterTo(wt)
+ argsFd = int(proc.InitFile(f, hl.extraFiles))
+ hl.files = append(hl.files, f)
+ }
+
+ statFd := -1
+ if hl.useStatFd {
+ f := proc.NewStat(&hl.stat)
+ statFd = int(proc.InitFile(f, hl.extraFiles))
+ hl.files = append(hl.files, f)
+ }
+
+ args = argF(argsFd, statFd)
+ return
+}
+
+// helperFiles provides a generic wrapper around helper ipc.
+type helperFiles struct {
+ // whether argsFd is present
+ useArgsFd bool
+ // whether statFd is present
+ useStatFd bool
+
+ // closes statFd
+ stat io.Closer
+ // deferred extraFiles fulfillment
+ files []proc.File
+ // passed through to [proc.Fulfill] and [proc.InitFile]
+ extraFiles *proc.ExtraFilesPre
+
+ ctx context.Context
+}