Skip to content

Commit

Permalink
Merge pull request #600 from bmeneguele/plat-util
Browse files Browse the repository at this point in the history
util: fix dup2() usage for Linux and Darwin
  • Loading branch information
bmeneg committed Feb 23, 2021
2 parents 4613fc6 + b6724cf commit bc7d5a4
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 bc7d5a4

Please sign in to comment.