From 0f1f0e43643d302494c0dac8bbc3eda7813f06bb Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sat, 15 Mar 2025 02:10:22 +0900 Subject: helper: combine helper ipc setup The two-step args call is no longer necessary since stat is passed on initialisation. Signed-off-by: Ophestra --- helper/helper.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'helper/helper.go') 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 +} -- cgit v1.3.1