Skip to content

Commit bc7d5a4

Browse files
authored
Merge pull request #600 from bmeneguele/plat-util
util: fix dup2() usage for Linux and Darwin
2 parents 4613fc6 + b6724cf commit bc7d5a4

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

cmd/util_darwin.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This file contains Darwin (MacOS) specific calls.
2+
3+
// +build darwin
4+
5+
package cmd
6+
7+
// Unfortunatelly MacOS don't have the DUP3() system call, which is forced
8+
// by Linux ARM64 not having the DUP2() anymore. With that, we need to
9+
// repeat the other code and func declarations that are the same.
10+
11+
// FIXME: there MUST be some better way to do that... only dupFD2() should be
12+
// here.
13+
14+
import "syscall"
15+
16+
var (
17+
sysStdout = syscall.Stdout
18+
sysStderr = syscall.Stderr
19+
)
20+
21+
func closeFD(fd int) error {
22+
return syscall.Close(fd)
23+
}
24+
25+
func dupFD(fd int) (int, error) {
26+
return syscall.Dup(fd)
27+
}
28+
29+
// From what I've seen, darwin is the only OS without DUP3() support
30+
func dupFD2(newFD, oldFD int) error {
31+
return syscall.Dup2(newFD, oldFD)
32+
}

cmd/util_unix.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// This file contains Linux specific calls.
22

3-
// +build !windows
3+
// +build !windows,!darwin
44

55
package cmd
66

77
// Since we're using some system calls that are platform-specific, we need
88
// to make sure we have a small layer of compatibility for Unix-like and
99
// Windows operating systems. For now, this file is still valid for BSDs
10-
// (MacOS included).
10+
// (MacOS NOT included)
1111

1212
import "syscall"
1313

@@ -27,6 +27,8 @@ func dupFD(fd int) (int, error) {
2727
return syscall.Dup(fd)
2828
}
2929

30+
// Dup2() is not supported in Linux arm64, so we need to change it.
31+
// Dup3() is available in all Linux arches and BSD* variants, but not darwin.
3032
func dupFD2(newFD, oldFD int) error {
31-
return syscall.Dup2(newFD, oldFD)
33+
return syscall.Dup3(newFD, oldFD, 0)
3234
}

0 commit comments

Comments
 (0)