Skip to content

Commit bbd25d2

Browse files
committed
internal/poll: use correct fcntl implementations
Use the libc fcntl (via syscall.fcntl) on aix and solaris like it is already done for darwin. For the syscall-based fcntl implementation use FcntlSyscall from internal/syscall/unix in order to get fcntl64 on 32-bit Linux systems. On aix, fcntl with F_DUPFD_CLOEXEC is not supported. Thus, defined F_DUPFD_CLOEXEC = 0 in the syscall package and check its value before calling fcntl(fd, syscall.F_DUPFD_CLOEXEC, 0). On js/wasm, fcntl is not supported thus let its implementation return ENOSYS directly. Updates #36211 Change-Id: I96a2ea79e5c4eed2fefd94d0aefd72c940825682 Reviewed-on: https://go-review.googlesource.com/c/go/+/212278 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent aa175a1 commit bbd25d2

File tree

7 files changed

+50
-18
lines changed

7 files changed

+50
-18
lines changed

src/internal/poll/fcntl_js.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build js,wasm
6+
7+
package poll
8+
9+
import "syscall"
10+
11+
// fcntl not supported on js/wasm
12+
func fcntl(fd int, cmd int, arg int) (int, error) {
13+
return 0, syscall.ENOSYS
14+
}

src/internal/poll/fcntl_libc.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build aix darwin solaris
6+
7+
package poll
8+
9+
import _ "unsafe" // for go:linkname
10+
11+
// Implemented in the syscall package.
12+
//go:linkname fcntl syscall.fcntl
13+
func fcntl(fd int, cmd int, arg int) (int, error)

src/internal/poll/fcntl_syscall.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2019 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build dragonfly freebsd linux netbsd openbsd
6+
7+
package poll
8+
9+
import (
10+
"internal/syscall/unix"
11+
"syscall"
12+
)
13+
14+
func fcntl(fd int, cmd int, arg int) (int, error) {
15+
r, _, e := syscall.Syscall(unix.FcntlSyscall, uintptr(fd), uintptr(cmd), uintptr(arg))
16+
if e != 0 {
17+
return int(r), syscall.Errno(e)
18+
}
19+
return int(r), nil
20+
}

src/internal/poll/fd_fsync_darwin.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
package poll
66

7-
import (
8-
"syscall"
9-
_ "unsafe" // for go:linkname
10-
)
7+
import "syscall"
118

129
// Fsync invokes SYS_FCNTL with SYS_FULLFSYNC because
1310
// on OS X, SYS_FSYNC doesn't fully flush contents to disk.
@@ -21,7 +18,3 @@ func (fd *FD) Fsync() error {
2118
_, e1 := fcntl(fd.Sysfd, syscall.F_FULLFSYNC, 0)
2219
return e1
2320
}
24-
25-
// Implemented in syscall/syscall_darwin.go.
26-
//go:linkname fcntl syscall.fcntl
27-
func fcntl(fd int, cmd int, arg int) (int, error)

src/internal/poll/fd_fsync_posix.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,3 @@ func (fd *FD) Fsync() error {
1616
defer fd.decref()
1717
return syscall.Fsync(fd.Sysfd)
1818
}
19-
20-
func fcntl(fd int, cmd int, arg int) (int, error) {
21-
r, _, e := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), uintptr(cmd), uintptr(arg))
22-
if e != 0 {
23-
return int(r), syscall.Errno(e)
24-
}
25-
return int(r), nil
26-
}

src/internal/poll/fd_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ var tryDupCloexec = int32(1)
451451

452452
// DupCloseOnExec dups fd and marks it close-on-exec.
453453
func DupCloseOnExec(fd int) (int, string, error) {
454-
if atomic.LoadInt32(&tryDupCloexec) == 1 {
454+
if syscall.F_DUPFD_CLOEXEC != 0 && atomic.LoadInt32(&tryDupCloexec) == 1 {
455455
r0, e1 := fcntl(fd, syscall.F_DUPFD_CLOEXEC, 0)
456456
if e1 == nil {
457457
return r0, "", nil

src/syscall/syscall_aix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ func syscall6(trap, nargs, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2 uintptr, err
2323
const (
2424
_ = iota
2525
TIOCSCTTY
26-
F_DUPFD_CLOEXEC
2726
SYS_EXECVE
2827
SYS_FCNTL
2928
)
3029

3130
const (
31+
F_DUPFD_CLOEXEC = 0
3232
// AF_LOCAL doesn't exist on AIX
3333
AF_LOCAL = AF_UNIX
3434
)

0 commit comments

Comments
 (0)