]> Cypherpunks repositories - gostls13.git/commitdiff
os: handle file creation with close-on-exec flag correctly on darwin, freebsd
authorMikio Hara <mikioh.mikioh@gmail.com>
Tue, 4 Mar 2014 00:27:29 +0000 (09:27 +0900)
committerMikio Hara <mikioh.mikioh@gmail.com>
Tue, 4 Mar 2014 00:27:29 +0000 (09:27 +0900)
Fixes #7187.
Update #7193

LGTM=bradfitz
R=golang-codereviews, dave, rsc, minux.ma, bradfitz
CC=golang-codereviews
https://golang.org/cl/64510043

src/pkg/os/file_unix.go
src/pkg/os/sys_darwin.go [new file with mode: 0644]
src/pkg/os/sys_freebsd.go [new file with mode: 0644]
src/pkg/os/sys_nacl.go [new file with mode: 0644]
src/pkg/os/sys_unix.go [new file with mode: 0644]

index f6d76f289df4608972e22b4298d80839a2f10ea9..699e4409eafadc76f1f296d56c5ab3f7d469d395 100644 (file)
@@ -81,12 +81,7 @@ func OpenFile(name string, flag int, perm FileMode) (file *File, err error) {
 
        // There's a race here with fork/exec, which we are
        // content to live with.  See ../syscall/exec_unix.go.
-       // On OS X 10.6, the O_CLOEXEC flag is not respected.
-       // On OS X 10.7, the O_CLOEXEC flag works.
-       // Without a cheap & reliable way to detect 10.6 vs 10.7 at
-       // runtime, we just always call syscall.CloseOnExec on Darwin.
-       // Once >=10.7 is prevalent, this extra call can removed.
-       if syscall.O_CLOEXEC == 0 || runtime.GOOS == "darwin" { // O_CLOEXEC not supported
+       if !supportsCloseOnExec {
                syscall.CloseOnExec(r)
        }
 
diff --git a/src/pkg/os/sys_darwin.go b/src/pkg/os/sys_darwin.go
new file mode 100644 (file)
index 0000000..7a8330a
--- /dev/null
@@ -0,0 +1,31 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package os
+
+import "syscall"
+
+// supportsCloseOnExec reports whether the platform supports the
+// O_CLOEXEC flag.
+var supportsCloseOnExec bool
+
+func init() {
+       // Seems like kern.osreldate is veiled on latest OS X. We use
+       // kern.osrelease instead.
+       osver, err := syscall.Sysctl("kern.osrelease")
+       if err != nil {
+               return
+       }
+       var i int
+       for i = range osver {
+               if osver[i] != '.' {
+                       continue
+               }
+       }
+       // The O_CLOEXEC flag was introduced in OS X 10.7 (Darwin
+       // 11.0.0). See http://support.apple.com/kb/HT1633.
+       if i > 2 || i == 2 && osver[0] >= '1' && osver[1] >= '1' {
+               supportsCloseOnExec = true
+       }
+}
diff --git a/src/pkg/os/sys_freebsd.go b/src/pkg/os/sys_freebsd.go
new file mode 100644 (file)
index 0000000..273c2df
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package os
+
+import "syscall"
+
+// supportsCloseOnExec reports whether the platform supports the
+// O_CLOEXEC flag.
+var supportsCloseOnExec bool
+
+func init() {
+       osrel, err := syscall.SysctlUint32("kern.osreldate")
+       if err != nil {
+               return
+       }
+       // The O_CLOEXEC flag was introduced in FreeBSD 8.3.
+       // See http://www.freebsd.org/doc/en/books/porters-handbook/freebsd-versions.html.
+       if osrel >= 803000 {
+               supportsCloseOnExec = true
+       }
+}
diff --git a/src/pkg/os/sys_nacl.go b/src/pkg/os/sys_nacl.go
new file mode 100644 (file)
index 0000000..07907c8
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package os
+
+// supportsCloseOnExec reports whether the platform supports the
+// O_CLOEXEC flag.
+const supportsCloseOnExec = false
diff --git a/src/pkg/os/sys_unix.go b/src/pkg/os/sys_unix.go
new file mode 100644 (file)
index 0000000..39c20dc
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2014 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build dragonfly linux netbsd openbsd solaris
+
+package os
+
+// supportsCloseOnExec reports whether the platform supports the
+// O_CLOEXEC flag.
+const supportsCloseOnExec = true