github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/stderrutils/dup_stderr_nix.go (about)

     1  // Copyright 2017 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  //go:build linux || android || darwin || freebsd || openbsd
     5  // +build linux android darwin freebsd openbsd
     6  
     7  package stderrutils
     8  
     9  import (
    10  	"os"
    11  	"syscall"
    12  )
    13  
    14  // DupStderr duplicates stderr and return it as an *os.File. Use this to
    15  // preserve stderr before any redirection (e.g. from keybase/client/go/logger)
    16  // if needed.
    17  func DupStderr() (*os.File, error) {
    18  	dupStderrFd, err := syscall.Dup(2)
    19  	if err != nil {
    20  		return nil, err
    21  	}
    22  	stderrFile := os.NewFile(uintptr(dupStderrFd), "stderr")
    23  	return stderrFile, nil
    24  }