英文:
how do I clear the close-on-exec flag in go?
问题
当在Centos6上调用os.OpenFile时,文件句柄上会设置O_CLOEXEC标志。我认为无法避免设置该标志。例如,以下调用:
f, err := os.OpenFile("lockfile", os.O_CREATE|os.O_RDWR, 0666)
在strace中的显示如下:
[pid 2928] open("lockfile", O_RDWR|O_CREAT|O_CLOEXEC, 0666) = 3
syscall.CloseOnExec用于为给定的文件句柄设置close-on-exec标志,但我找不到相应的函数来清除close-on-exec标志。
如何清除文件的close-on-exec标志?
英文:
When os.OpenFile is called on Centos6, the O_CLOEXEC flag is set on the file handle. I don't think it is possible to avoid the flag being set. For example, the following call:
f, err := os.OpenFile( "lockfile", os.O_CREATE|os.O_RDWR, 0666 )
looks like this in strace:
[pid 2928] open("lockfile", O_RDWR|O_CREAT|O_CLOEXEC, 0666) = 3
syscall.CloseOnExec is provided to set the close-on-exec flag for a given file handle, but I can't find a corresponding function for clearing the close-on-exec flag.
How can I clear the close-on-exec flag for a file ?
答案1
得分: 4
我在https://golang.org/src/pkg/syscall/exec_linux.go中找到了一个提示:
_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), F_SETFD, 0)
if err1 != 0 {
goto childerror
}
在其他地方,我读到应该使用Syscall而不是RawSyscall,所以我将其改写为:
_, _, err = syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), syscall.F_SETFD, 0)
if err != syscall.Errno(0x0) {
log.Fatal(err)
}
英文:
I found a hint in https://golang.org/src/pkg/syscall/exec_linux.go:
_, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), F_SETFD, 0)
if err1 != 0 {
goto childerror
}
Elsewhere, I read I should use Syscall rather than RawSyscall, and so I rewrote this as:
_, _, err = syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), syscall.F_SETFD, 0)
if err != syscall.Errno(0x0) {
log.Fatal(err)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论