syscall.forkAndExecInChild fix: os.Fcntl/Dup2 retval

This commit is contained in:
xushiwei
2024-07-29 00:14:01 +08:00
parent 4df478316c
commit 708de50887

View File

@@ -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