2024-09-24 16:11:08 +09:00
|
|
|
/*
|
|
|
|
Package helper runs external helpers and manages their status and args FDs.
|
|
|
|
*/
|
|
|
|
package helper
|
2024-09-25 01:16:06 +09:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrStatusFault = errors.New("generic status pipe fault")
|
|
|
|
ErrStatusRead = errors.New("unexpected status response")
|
|
|
|
)
|
|
|
|
|
2024-09-29 14:40:01 +09:00
|
|
|
const (
|
2024-10-07 12:48:20 +09:00
|
|
|
// FortifyHelper is set for the process launched by Helper.
|
2024-09-29 14:40:01 +09:00
|
|
|
FortifyHelper = "FORTIFY_HELPER"
|
2024-10-07 12:48:20 +09:00
|
|
|
// FortifyStatus is 1 when sync fd is enabled and 0 otherwise.
|
2024-09-29 14:40:01 +09:00
|
|
|
FortifyStatus = "FORTIFY_STATUS"
|
|
|
|
)
|
|
|
|
|
2024-09-25 01:16:06 +09:00
|
|
|
// Helper wraps *exec.Cmd and manages status and args fd.
|
|
|
|
// Args is always 3 and status if set is always 4.
|
|
|
|
type Helper struct {
|
2024-10-07 12:48:20 +09:00
|
|
|
p *pipes
|
2024-09-25 01:16:06 +09:00
|
|
|
|
2024-10-07 12:48:20 +09:00
|
|
|
argF func(argsFD, statFD int) []string
|
|
|
|
*exec.Cmd
|
2024-09-25 01:16:06 +09:00
|
|
|
|
2024-09-25 14:00:30 +09:00
|
|
|
lock sync.RWMutex
|
2024-09-25 01:16:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) StartNotify(ready chan error) 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 {
|
|
|
|
return errors.New("exec: already started")
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:48:20 +09:00
|
|
|
h.p.ready = ready
|
|
|
|
if argsFD, statFD, err := h.p.prepareCmd(h.Cmd); err != nil {
|
2024-09-25 01:16:06 +09:00
|
|
|
return err
|
|
|
|
} else {
|
2024-10-07 12:48:20 +09:00
|
|
|
h.Cmd.Args = append(h.Cmd.Args, h.argF(argsFD, statFD)...)
|
2024-09-25 01:16:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if ready != nil {
|
2024-10-07 12:48:20 +09:00
|
|
|
h.Cmd.Env = append(h.Cmd.Env, FortifyHelper+"=1", FortifyStatus+"=1")
|
2024-09-25 01:16:06 +09:00
|
|
|
} else {
|
2024-10-07 12:48:20 +09:00
|
|
|
h.Cmd.Env = append(h.Cmd.Env, FortifyHelper+"=1", FortifyStatus+"=0")
|
2024-09-25 01:16:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.Cmd.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-07 12:48:20 +09:00
|
|
|
if err := h.p.readyWriteArgs(); err != nil {
|
2024-09-25 01:16:06 +09:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Wait() error {
|
|
|
|
h.lock.RLock()
|
|
|
|
defer h.lock.RUnlock()
|
|
|
|
|
|
|
|
if h.Cmd.Process == nil {
|
|
|
|
return errors.New("exec: not started")
|
|
|
|
}
|
|
|
|
if h.Cmd.ProcessState != nil {
|
|
|
|
return errors.New("exec: Wait was already called")
|
|
|
|
}
|
|
|
|
|
2024-10-07 12:48:20 +09:00
|
|
|
defer h.p.mustClosePipes()
|
2024-09-25 01:16:06 +09:00
|
|
|
return h.Cmd.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Close() error {
|
2024-10-07 12:48:20 +09:00
|
|
|
return h.p.closeStatus()
|
2024-09-25 01:16:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Helper) Start() error {
|
|
|
|
return h.StartNotify(nil)
|
|
|
|
}
|
|
|
|
|
2024-09-29 14:40:01 +09:00
|
|
|
var execCommand = exec.Command
|
|
|
|
|
2024-10-07 12:48:20 +09:00
|
|
|
// New initialises a new 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 {
|
2024-09-25 01:16:06 +09:00
|
|
|
if wt == nil {
|
2024-09-25 14:00:30 +09:00
|
|
|
panic("attempted to create helper with invalid argument writer")
|
2024-09-25 01:16:06 +09:00
|
|
|
}
|
|
|
|
|
2024-10-07 12:48:20 +09:00
|
|
|
return &Helper{p: &pipes{args: wt}, argF: argF, Cmd: execCommand(name)}
|
2024-09-25 01:16:06 +09:00
|
|
|
}
|