From ea1e3ebae993a3d89302940dc1649b627c420aa1 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sat, 23 Aug 2025 00:16:46 +0900 Subject: [PATCH] container/params: pass fd instead of file The file is very difficult to stub. Pass fd instead as it is the value that is actually useful. Signed-off-by: Ophestra --- container/dispatcher.go | 6 +++--- container/dispatcher_test.go | 4 ++-- container/init.go | 6 +++--- container/params.go | 6 +++--- container/params_test.go | 19 ++++++++----------- 5 files changed, 19 insertions(+), 22 deletions(-) diff --git a/container/dispatcher.go b/container/dispatcher.go index 86837b9..78648a7 100644 --- a/container/dispatcher.go +++ b/container/dispatcher.go @@ -45,7 +45,7 @@ type syscallDispatcher interface { // isatty provides [Isatty]. isatty(fd int) bool // 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(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) capAmbientRaise(cap uintptr) error { return capAmbientRaise(cap) } func (direct) isatty(fd int) bool { return Isatty(fd) } -func (direct) receive(key string, e any, v **os.File) (func() error, error) { - return Receive(key, e, v) +func (direct) receive(key string, e any, fdp *uintptr) (func() error, error) { + return Receive(key, e, fdp) } func (direct) bindMount(source, target string, flags uintptr, eq bool) error { diff --git a/container/dispatcher_test.go b/container/dispatcher_test.go index 1152e16..012673a 100644 --- a/container/dispatcher_test.go +++ b/container/dispatcher_test.go @@ -356,12 +356,12 @@ func (k *kstub) isatty(fd int) 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") return expect.ret.(func() error), expect.error( checkArg(k, "key", key, 0), 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 { diff --git a/container/init.go b/container/init.go index 2139ccc..813b4ac 100644 --- a/container/init.go +++ b/container/init.go @@ -99,10 +99,10 @@ func initEntrypoint(k syscallDispatcher, prepareLogger func(prefix string), setV var ( params initParams closeSetup func() error - setupFile *os.File + setupFd uintptr offsetSetup int ) - if f, err := k.receive(setupEnv, ¶ms, &setupFile); err != nil { + if f, err := k.receive(setupEnv, ¶ms, &setupFd); err != nil { if errors.Is(err, EBADF) { k.fatal("invalid setup descriptor") } @@ -122,7 +122,7 @@ func initEntrypoint(k syscallDispatcher, prepareLogger func(prefix string), setV setVerbose(params.Verbose) k.verbose("received setup parameters") closeSetup = f - offsetSetup = int(setupFile.Fd() + 1) + offsetSetup = int(setupFd + 1) } // write uid/gid map here so parent does not need to set dumpable diff --git a/container/params.go b/container/params.go index 09be370..00c98cf 100644 --- a/container/params.go +++ b/container/params.go @@ -25,7 +25,7 @@ func Setup(extraFiles *[]*os.File) (int, *gob.Encoder, error) { } // 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 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 { return nil, syscall.EBADF } - if v != nil { - *v = setup + if fdp != nil { + *fdp = setup.Fd() } } } diff --git a/container/params_test.go b/container/params_test.go index 537d686..720f0bf 100644 --- a/container/params_test.go +++ b/container/params_test.go @@ -53,7 +53,7 @@ func TestSetupReceive(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" 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 ( gotPayload []int - fp **os.File + fdp *uintptr ) - if !useNilFp { - fp = new(*os.File) + if !useNilFdp { + fdp = new(uintptr) } 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) } else { closeFile = f @@ -97,12 +97,9 @@ func TestSetupReceive(t *testing.T) { t.Errorf("Receive: %#v, want %#v", gotPayload, payload) } } - if !useNilFp { - if name := (*fp).Name(); name != "setup" { - t.Errorf("Name: %s, want setup", name) - } - if fd := int((*fp).Fd()); fd != dupFd { - t.Errorf("Fd: %d, want %d", fd, dupFd) + if !useNilFdp { + if int(*fdp) != dupFd { + t.Errorf("Fd: %d, want %d", *fdp, dupFd) } }