Skip to content

Commit c40fa83

Browse files
committed
log: send crash output to a sep fd
1 parent e37b312 commit c40fa83

4 files changed

Lines changed: 20 additions & 27 deletions

File tree

intra/backend/protect.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const ( // see protect/protect.go
1515
type Console interface {
1616
Log(int32, *Gostr)
1717
LogFD(readAfterDup int) bool
18+
CrashFD(readUntilEOF int) bool
1819
}
1920

2021
// Controller provides a way to bind and protect socket file descriptors.

intra/log/fconsole.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package log
33
import (
44
"io"
55
"os"
6-
"strings"
76
"syscall"
87
"unsafe"
98
)
@@ -51,32 +50,11 @@ func setNonblock(f *os.File) error {
5150
return syscall.SetNonblock(int(f.Fd()), true)
5251
}
5352

54-
func (f *fconsole) write(msg Logmsg) {
55-
if len(msg) == 0 {
56-
return
57-
}
58-
for msgline := range strings.SplitSeq(msg, "\n") {
59-
if len(msgline) <= 0 {
60-
continue
61-
}
62-
if len(msgline) <= charsPerLine {
63-
f.doWrite(msgline)
64-
}
65-
66-
for len(msgline) > 0 {
67-
m := msgline
68-
if len(m) > charsPerLine {
69-
m = m[:charsPerLine]
70-
}
71-
if err := f.doWrite(m); err != nil {
72-
return
73-
}
74-
msgline = msgline[len(m):]
75-
}
53+
func (f *fconsole) write(m Logmsg) error {
54+
if len(m) == 0 {
55+
return nil
7656
}
77-
}
7857

79-
func (f *fconsole) doWrite(m string) error {
8058
w := f.w
8159
if w == nil {
8260
return io.ErrClosedPipe

intra/tun2socks.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,20 @@ func FatalAtRandom(y bool) {
254254
log.I("tun: fatal at random? %t", y)
255255
}
256256

257+
func pipeCrashOutput(bdg Bridge) (ok bool) {
258+
r, w, err := os.Pipe()
259+
if err != nil {
260+
log.E("tun: err crash output pipe: %v", err)
261+
return false
262+
}
263+
if setCrashFd(w) && bdg.CrashFD(int(r.Fd())) {
264+
return true
265+
}
266+
core.Close(r)
267+
core.Close(w)
268+
return false
269+
}
270+
257271
// setCrashFd sets dup(f) as output file to write go runtime crashes in to.
258272
func setCrashFd(f *os.File) (ok bool) {
259273
// f is dup()ed by debug.SetCrashOutput before use

intra/tunnel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,13 @@ func NewTunnel2(fd, linkmtu, tunmtu int, ifaddrs, fakedns string, dtr DefaultDNS
187187
const dualstack = settings.IP46
188188

189189
logfd := false
190-
crashfd := false
191190
if r, c, err := log.NewFilebased(); err == nil {
192191
closeall := func() {
193192
core.Close(c)
194193
core.Close(r)
195194
}
196195
if logfd = bdg.LogFD(int(r.Fd())); logfd {
197196
log.SetConsole(ctx, c)
198-
crashfd = setCrashFd(c.File())
199197
context.AfterFunc(ctx, closeall)
200198
} else {
201199
closeall()
@@ -205,6 +203,8 @@ func NewTunnel2(fd, linkmtu, tunmtu int, ifaddrs, fakedns string, dtr DefaultDNS
205203
log.SetConsole(ctx, &clogAdapter{bdg})
206204
}
207205

206+
crashfd := pipeCrashOutput(bdg)
207+
208208
natpt := x64.NewNatPt2(ctx)
209209
proxies := ipn.NewProxifier(ctx, dualstack, linkmtu, bdg, bdg)
210210
services := rnet.NewServices(ctx, proxies, bdg, bdg)

0 commit comments

Comments
 (0)