-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_redirect_linux.go
57 lines (47 loc) · 1.41 KB
/
output_redirect_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"log"
"os"
"strconv"
"github.com/oraoto/go-pidfd"
)
func implementOutputFdRedirect() {
outFdPidStr := os.Getenv(OutFdPidEnvVar)
if outFdPidStr == "" {
return
}
outFdPid, err := strconv.Atoi(outFdPidStr)
if err != nil {
log.Fatalf("Invalid %s (must be an integer): '%s'", OutFdPidEnvVar, err)
}
outFdProc, err := pidfd.Open(outFdPid, 0)
if err != nil {
log.Fatalf("pidfd.Open(%d) failed: %s", outFdPid, err)
}
outFdStdOutStr := os.Getenv(OutFdStdoutEnvVar)
if outFdStdOutStr != "" {
outFdStdOut, err := strconv.Atoi(outFdStdOutStr)
if err != nil {
log.Fatalf("Invalid %s (must be an integer): '%s'", OutFdStdoutEnvVar, err)
}
myStdoutFd, err := outFdProc.GetFd(outFdStdOut, 0)
if err != nil {
log.Fatalf("pidfd.GetFd(%d) failed: %s", outFdStdOut, err)
}
os.Stdout = os.NewFile(uintptr(myStdoutFd), fmt.Sprintf("/proc/%d/fd/%d", outFdPid, outFdStdOut))
}
outFdStdErrStr := os.Getenv(OutFdStderrEnvVar)
if outFdStdErrStr != "" {
outFdStdErr, err := strconv.Atoi(outFdStdErrStr)
if err != nil {
log.Fatalf("Invalid %s (must be an integer): '%s'", OutFdStderrEnvVar, err)
}
myStderrFd, err := outFdProc.GetFd(outFdStdErr, 0)
if err != nil {
log.Fatalf("pidfd.GetFd(%d) failed: %s", outFdStdErr, err)
}
os.Stderr = os.NewFile(uintptr(myStderrFd), fmt.Sprintf("/proc/%d/fd/%d", outFdPid, outFdStdErr))
log.Default().SetOutput(os.Stderr)
}
}