how do I clear the close-on-exec flag in go?

huangapple go评论85阅读模式
英文:

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)
    }

huangapple
  • 本文由 发表于 2014年10月20日 23:03:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/26468841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定