mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-12-27 17:55:37 +08:00
41 lines
720 B
Go
41 lines
720 B
Go
//go:build darwin || linux
|
|
|
|
package libbox
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
var stderrFile *os.File
|
|
|
|
func RedirectStderr(path string) error {
|
|
if stats, err := os.Stat(path); err == nil && stats.Size() > 0 {
|
|
_ = os.Rename(path, path+".old")
|
|
}
|
|
outputFile, err := os.Create(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if runtime.GOOS != "android" {
|
|
if sUserID > 0 {
|
|
err = outputFile.Chown(sUserID, sGroupID)
|
|
if err != nil {
|
|
outputFile.Close()
|
|
os.Remove(outputFile.Name())
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
err = unix.Dup2(int(outputFile.Fd()), int(os.Stderr.Fd()))
|
|
if err != nil {
|
|
outputFile.Close()
|
|
os.Remove(outputFile.Name())
|
|
return err
|
|
}
|
|
stderrFile = outputFile
|
|
return nil
|
|
}
|