hakurei/container/params.go
Ophestra 8b9652fc72
All checks were successful
Test / Create distribution (push) Successful in 34s
Test / Sandbox (push) Successful in 2m21s
Test / Hakurei (push) Successful in 3m12s
Test / Sandbox (race detector) (push) Successful in 4m5s
Test / Hpkg (push) Successful in 4m8s
Test / Hakurei (race detector) (push) Successful in 4m54s
Test / Flake checks (push) Successful in 1m20s
container/params: expose pipe deadline
This allows more graceful handling of unresponsive process.

Signed-off-by: Ophestra <cat@gensokyo.uk>
2025-10-31 21:50:51 +09:00

53 lines
1.1 KiB
Go

package container
import (
"encoding/gob"
"errors"
"os"
"strconv"
"syscall"
"time"
)
// Setup appends the read end of a pipe for setup params transmission and returns its fd.
func Setup(deadline time.Time, extraFiles *[]*os.File) (int, *gob.Encoder, error) {
if r, w, err := os.Pipe(); err != nil {
return -1, nil, err
} else {
if err = w.SetDeadline(deadline); err != nil {
return -1, nil, err
}
fd := 3 + len(*extraFiles)
*extraFiles = append(*extraFiles, r)
return fd, gob.NewEncoder(w), nil
}
}
var (
ErrReceiveEnv = errors.New("environment variable not set")
)
// Receive retrieves setup fd from the environment and receives params.
func Receive(key string, e any, fdp *uintptr) (func() error, error) {
var setup *os.File
if s, ok := os.LookupEnv(key); !ok {
return nil, ErrReceiveEnv
} else {
if fd, err := strconv.Atoi(s); err != nil {
return nil, optionalErrorUnwrap(err)
} else {
setup = os.NewFile(uintptr(fd), "setup")
if setup == nil {
return nil, syscall.EDOM
}
if fdp != nil {
*fdp = setup.Fd()
}
}
}
return setup.Close, gob.NewDecoder(setup).Decode(e)
}