Skip to content

util: fix dup2() usage for Linux and Darwin #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}