2024-10-07 15:37:52 +09:00
|
|
|
package helper
|
|
|
|
|
|
|
|
import (
|
2025-02-13 23:15:34 +09:00
|
|
|
"context"
|
2024-10-07 15:37:52 +09:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"sync"
|
2025-02-13 23:15:34 +09:00
|
|
|
|
|
|
|
"git.gensokyo.uk/security/fortify/helper/proc"
|
2024-10-07 15:37:52 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
// direct wraps *exec.Cmd and manages status and args fd.
|
|
|
|
// Args is always 3 and status if set is always 4.
|
|
|
|
type direct struct {
|
|
|
|
lock sync.RWMutex
|
2025-02-13 23:15:34 +09:00
|
|
|
*helperCmd
|
2024-10-07 15:37:52 +09:00
|
|
|
}
|
|
|
|
|
2025-02-13 23:15:34 +09:00
|
|
|
func (h *direct) Start(ctx context.Context, stat bool) error {
|
2024-10-07 15:37:52 +09:00
|
|
|
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.
|
2025-02-13 23:15:34 +09:00
|
|
|
if h.Cmd != nil && h.Cmd.Process != nil {
|
2024-10-07 15:37:52 +09:00
|
|
|
return errors.New("exec: already started")
|
|
|
|
}
|
|
|
|
|
2025-02-13 23:15:34 +09:00
|
|
|
args := h.finalise(ctx, stat)
|
|
|
|
h.Cmd.Args = append(h.Cmd.Args, args...)
|
|
|
|
return proc.Fulfill(ctx, h.Cmd, h.files, h.extraFiles)
|
2024-10-07 15:37:52 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2025-02-13 23:15:34 +09:00
|
|
|
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
|
2024-10-07 15:37:52 +09:00
|
|
|
}
|