aboutsummaryrefslogtreecommitdiffhomepage
path: root/helper/direct.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-02-13 23:15:34 +0900
committerOphestra <cat@gensokyo.uk>2025-02-13 23:34:15 +0900
commitfe7d208cf76fa6f24bb9d12ba29b5ed61d837ce3 (patch)
treecac02af50a13b2078739a8f5a74d219f3b60833d /helper/direct.go
parent60c287375048b21eab2bd82f1e7d43e36dcfb3a2 (diff)
helper: use generic extra files interface
This replaces the pipes object and integrates context into helper process lifecycle. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'helper/direct.go')
-rw-r--r--helper/direct.go79
1 files changed, 13 insertions, 66 deletions
diff --git a/helper/direct.go b/helper/direct.go
index 91e40451..9198952d 100644
--- a/helper/direct.go
+++ b/helper/direct.go
@@ -1,93 +1,40 @@
package helper
import (
+ "context"
"errors"
"io"
- "os/exec"
"sync"
+
+ "git.gensokyo.uk/security/fortify/helper/proc"
)
// direct wraps *exec.Cmd and manages status and args fd.
// Args is always 3 and status if set is always 4.
type direct struct {
- // helper pipes
- // cannot be nil
- p *pipes
-
- // returns an array of arguments passed directly
- // to the helper process
- argF func(argsFD, statFD int) []string
-
lock sync.RWMutex
- *exec.Cmd
+ *helperCmd
}
-func (h *direct) StartNotify(ready chan error) error {
+func (h *direct) Start(ctx context.Context, stat bool) error {
h.lock.Lock()
defer h.lock.Unlock()
// Check for doubled Start calls before we defer failure cleanup. If the prior
// call to Start succeeded, we don't want to spuriously close its pipes.
- if h.Cmd.Process != nil {
+ if h.Cmd != nil && h.Cmd.Process != nil {
return errors.New("exec: already started")
}
- h.p.ready = ready
- if argsFD, statFD, err := h.p.prepareCmd(h.Cmd); err != nil {
- return err
- } else {
- h.Cmd.Args = append(h.Cmd.Args, h.argF(argsFD, statFD)...)
- }
-
- if ready != nil {
- h.Cmd.Env = append(h.Cmd.Env, FortifyHelper+"=1", FortifyStatus+"=1")
- } else {
- h.Cmd.Env = append(h.Cmd.Env, FortifyHelper+"=1", FortifyStatus+"=0")
- }
-
- if err := h.Cmd.Start(); err != nil {
- return err
- }
- if err := h.p.readyWriteArgs(); err != nil {
- return err
- }
-
- return nil
-}
-
-func (h *direct) Wait() error {
- h.lock.RLock()
- defer h.lock.RUnlock()
-
- if h.Cmd.Process == nil {
- return errors.New("exec: not started")
- }
- defer h.p.mustClosePipes()
- if h.Cmd.ProcessState != nil {
- return errors.New("exec: Wait was already called")
- }
-
- return h.Cmd.Wait()
-}
-
-func (h *direct) Close() error {
- return h.p.closeStatus()
-}
-
-func (h *direct) Start() error {
- return h.StartNotify(nil)
-}
-
-func (h *direct) Unwrap() *exec.Cmd {
- return h.Cmd
+ args := h.finalise(ctx, stat)
+ h.Cmd.Args = append(h.Cmd.Args, args...)
+ return proc.Fulfill(ctx, h.Cmd, h.files, h.extraFiles)
}
// New initialises a new direct Helper instance with wt as the null-terminated argument writer.
// Function argF returns an array of arguments passed directly to the child process.
-func New(wt io.WriterTo, name string, argF func(argsFD, statFD int) []string) Helper {
- if wt == nil {
- panic("attempted to create helper with invalid argument writer")
- }
-
- return &direct{p: &pipes{args: wt}, argF: argF, Cmd: execCommand(name)}
+func New(wt io.WriterTo, name string, argF func(argsFd, statFd int) []string) Helper {
+ d := new(direct)
+ d.helperCmd = newHelperCmd(d, name, wt, argF, nil)
+ return d
}