doudao7511 2014-10-20 15:03
浏览 64

如何清除go中的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条回答 默认 最新

  • duanlvji4780 2014-10-20 15:53
    关注

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?