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