Skip to content

Commit

Permalink
util: fix dup2() usage for Linux and Darwin
Browse files Browse the repository at this point in the history
We must create a new Darwin specific file to handle the lack of DUP2()
in Linux ARM64 systems and also the lack of DUP3() in Darwin. In other
words: Linux ARM64 must use DUP3() while Darwin must use DUP2().

Other BSDs have DUP3(), allowing us to use the same Linux file for them.

Signed-off-by: Bruno Meneguele <[email protected]>
  • Loading branch information
bmeneg committed Feb 23, 2021
1 parent 090ff30 commit b6724cf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
32 changes: 32 additions & 0 deletions cmd/util_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file contains Darwin (MacOS) specific calls.

// +build darwin

package cmd

// Unfortunatelly MacOS don't have the DUP3() system call, which is forced
// by Linux ARM64 not having the DUP2() anymore. With that, we need to
// repeat the other code and func declarations that are the same.

// FIXME: there MUST be some better way to do that... only dupFD2() should be
// here.

import "syscall"

var (
sysStdout = syscall.Stdout
sysStderr = syscall.Stderr
)

func closeFD(fd int) error {
return syscall.Close(fd)
}

func dupFD(fd int) (int, error) {
return syscall.Dup(fd)
}

// From what I've seen, darwin is the only OS without DUP3() support
func dupFD2(newFD, oldFD int) error {
return syscall.Dup2(newFD, oldFD)
}
8 changes: 5 additions & 3 deletions cmd/util_unix.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// This file contains Linux specific calls.

// +build !windows
// +build !windows,!darwin

package cmd

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

import "syscall"

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

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

0 comments on commit b6724cf

Please sign in to comment.