container/params: pass fd instead of file
All checks were successful
Test / Create distribution (push) Successful in 34s
Test / Sandbox (push) Successful in 2m9s
Test / Hakurei (push) Successful in 3m9s
Test / Hpkg (push) Successful in 4m12s
Test / Sandbox (race detector) (push) Successful in 4m29s
Test / Hakurei (race detector) (push) Successful in 5m6s
Test / Flake checks (push) Successful in 1m29s

The file is very difficult to stub. Pass fd instead as it is the value that is actually useful.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-08-23 00:16:46 +09:00
parent 1c692bfb79
commit ea1e3ebae9
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
5 changed files with 19 additions and 22 deletions

View File

@ -45,7 +45,7 @@ type syscallDispatcher interface {
// isatty provides [Isatty]. // isatty provides [Isatty].
isatty(fd int) bool isatty(fd int) bool
// receive provides [Receive]. // receive provides [Receive].
receive(key string, e any, v **os.File) (closeFunc func() error, err error) receive(key string, e any, fdp *uintptr) (closeFunc func() error, err error)
// bindMount provides procPaths.bindMount. // bindMount provides procPaths.bindMount.
bindMount(source, target string, flags uintptr, eq bool) error bindMount(source, target string, flags uintptr, eq bool) error
@ -152,8 +152,8 @@ func (direct) capBoundingSetDrop(cap uintptr) error { return capBound
func (direct) capAmbientClearAll() error { return capAmbientClearAll() } func (direct) capAmbientClearAll() error { return capAmbientClearAll() }
func (direct) capAmbientRaise(cap uintptr) error { return capAmbientRaise(cap) } func (direct) capAmbientRaise(cap uintptr) error { return capAmbientRaise(cap) }
func (direct) isatty(fd int) bool { return Isatty(fd) } func (direct) isatty(fd int) bool { return Isatty(fd) }
func (direct) receive(key string, e any, v **os.File) (func() error, error) { func (direct) receive(key string, e any, fdp *uintptr) (func() error, error) {
return Receive(key, e, v) return Receive(key, e, fdp)
} }
func (direct) bindMount(source, target string, flags uintptr, eq bool) error { func (direct) bindMount(source, target string, flags uintptr, eq bool) error {

View File

@ -356,12 +356,12 @@ func (k *kstub) isatty(fd int) bool {
return expect.ret.(bool) return expect.ret.(bool)
} }
func (k *kstub) receive(key string, e any, v **os.File) (closeFunc func() error, err error) { func (k *kstub) receive(key string, e any, fdp *uintptr) (closeFunc func() error, err error) {
expect := k.expect("receive") expect := k.expect("receive")
return expect.ret.(func() error), expect.error( return expect.ret.(func() error), expect.error(
checkArg(k, "key", key, 0), checkArg(k, "key", key, 0),
checkArgReflect(k, "e", e, 1), checkArgReflect(k, "e", e, 1),
checkArg(k, "v", v, 2)) checkArg(k, "fdp", fdp, 2))
} }
func (k *kstub) bindMount(source, target string, flags uintptr, eq bool) error { func (k *kstub) bindMount(source, target string, flags uintptr, eq bool) error {

View File

@ -99,10 +99,10 @@ func initEntrypoint(k syscallDispatcher, prepareLogger func(prefix string), setV
var ( var (
params initParams params initParams
closeSetup func() error closeSetup func() error
setupFile *os.File setupFd uintptr
offsetSetup int offsetSetup int
) )
if f, err := k.receive(setupEnv, &params, &setupFile); err != nil { if f, err := k.receive(setupEnv, &params, &setupFd); err != nil {
if errors.Is(err, EBADF) { if errors.Is(err, EBADF) {
k.fatal("invalid setup descriptor") k.fatal("invalid setup descriptor")
} }
@ -122,7 +122,7 @@ func initEntrypoint(k syscallDispatcher, prepareLogger func(prefix string), setV
setVerbose(params.Verbose) setVerbose(params.Verbose)
k.verbose("received setup parameters") k.verbose("received setup parameters")
closeSetup = f closeSetup = f
offsetSetup = int(setupFile.Fd() + 1) offsetSetup = int(setupFd + 1)
} }
// write uid/gid map here so parent does not need to set dumpable // write uid/gid map here so parent does not need to set dumpable

View File

@ -25,7 +25,7 @@ func Setup(extraFiles *[]*os.File) (int, *gob.Encoder, error) {
} }
// Receive retrieves setup fd from the environment and receives params. // Receive retrieves setup fd from the environment and receives params.
func Receive(key string, e any, v **os.File) (func() error, error) { func Receive(key string, e any, fdp *uintptr) (func() error, error) {
var setup *os.File var setup *os.File
if s, ok := os.LookupEnv(key); !ok { if s, ok := os.LookupEnv(key); !ok {
@ -38,8 +38,8 @@ func Receive(key string, e any, v **os.File) (func() error, error) {
if setup == nil { if setup == nil {
return nil, syscall.EBADF return nil, syscall.EBADF
} }
if v != nil { if fdp != nil {
*v = setup *fdp = setup.Fd()
} }
} }
} }

View File

@ -53,7 +53,7 @@ func TestSetupReceive(t *testing.T) {
}) })
t.Run("setup receive", func(t *testing.T) { t.Run("setup receive", func(t *testing.T) {
check := func(t *testing.T, useNilFp bool) { check := func(t *testing.T, useNilFdp bool) {
const key = "TEST_SETUP_RECEIVE" const key = "TEST_SETUP_RECEIVE"
payload := []int{syscall.MS_MGC_VAL, syscall.MS_MGC_MSK, syscall.MS_ASYNC, syscall.MS_ACTIVE} payload := []int{syscall.MS_MGC_VAL, syscall.MS_MGC_MSK, syscall.MS_ASYNC, syscall.MS_ACTIVE}
@ -82,13 +82,13 @@ func TestSetupReceive(t *testing.T) {
var ( var (
gotPayload []int gotPayload []int
fp **os.File fdp *uintptr
) )
if !useNilFp { if !useNilFdp {
fp = new(*os.File) fdp = new(uintptr)
} }
var closeFile func() error var closeFile func() error
if f, err := container.Receive(key, &gotPayload, fp); err != nil { if f, err := container.Receive(key, &gotPayload, fdp); err != nil {
t.Fatalf("Receive: error = %v", err) t.Fatalf("Receive: error = %v", err)
} else { } else {
closeFile = f closeFile = f
@ -97,12 +97,9 @@ func TestSetupReceive(t *testing.T) {
t.Errorf("Receive: %#v, want %#v", gotPayload, payload) t.Errorf("Receive: %#v, want %#v", gotPayload, payload)
} }
} }
if !useNilFp { if !useNilFdp {
if name := (*fp).Name(); name != "setup" { if int(*fdp) != dupFd {
t.Errorf("Name: %s, want setup", name) t.Errorf("Fd: %d, want %d", *fdp, dupFd)
}
if fd := int((*fp).Fd()); fd != dupFd {
t.Errorf("Fd: %d, want %d", fd, dupFd)
} }
} }