From 708de50887afd2fc4889b98f0d860608ed07933e Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 29 Jul 2024 00:14:01 +0800 Subject: [PATCH] syscall.forkAndExecInChild fix: os.Fcntl/Dup2 retval --- internal/lib/syscall/exec_libc2.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/internal/lib/syscall/exec_libc2.go b/internal/lib/syscall/exec_libc2.go index 12bcfcbc..3a5dede4 100644 --- a/internal/lib/syscall/exec_libc2.go +++ b/internal/lib/syscall/exec_libc2.go @@ -263,15 +263,15 @@ func forkAndExecInChild(argv0 *c.Char, argv, envv **c.Char, chroot, dir *c.Char, if fd[i] == i { // dup2(i, i) won't clear close-on-exec flag on Linux, // probably not elsewhere either. - if ret := os.Fcntl(c.Int(fd[i]), syscall.F_SETFD, 0); ret != 0 { - err1 = Errno(ret) + if ret := os.Fcntl(c.Int(fd[i]), syscall.F_SETFD, 0); ret < 0 { + err1 = Errno(os.Errno) goto childerror } continue } // The new fd is created NOT close-on-exec, - if ret := os.Dup2(c.Int(fd[i]), c.Int(i)); ret != 0 { - err1 = Errno(ret) + if ret := os.Dup2(c.Int(fd[i]), c.Int(i)); ret < 0 { + err1 = Errno(os.Errno) goto childerror } } @@ -281,10 +281,7 @@ func forkAndExecInChild(argv0 *c.Char, argv, envv **c.Char, chroot, dir *c.Char, // Programs that know they inherit fds >= 3 will need // to set them close-on-exec. for i = len(fd); i < 3; i++ { - /* TODO(xsw): - rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0) - */ - panic("todo: syscall.forkAndExecInChild - for i") + os.Close(c.Int(i)) } // Detach fd 0 from tty