Skip to content

Commit 9440420

Browse files
committed
intra/tunnel: set crash output with file path
1 parent fff52c1 commit 9440420

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

intra/tun2socks.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ package intra
2626
import (
2727
"context"
2828
"os"
29+
"path/filepath"
2930
"runtime"
3031
"runtime/debug"
3132
"sync/atomic"
@@ -321,7 +322,7 @@ var crashpiped atomic.Bool
321322

322323
func pipeCrashOutput(c Console) (ok bool) {
323324
if crashRWErr != nil {
324-
log.E("tun: err crash output pipe: %v", crashRWErr)
325+
log.E("tun: crashout: err pipe: %v", crashRWErr)
325326
return false
326327
}
327328
pipeBuffer256k(crashWriter)
@@ -339,19 +340,24 @@ func pipeCrashOutput(c Console) (ok bool) {
339340
func setCrashFd(f *os.File) (ok bool) {
340341
// f is dup()ed by debug.SetCrashOutput before use
341342
err := debug.SetCrashOutput(f, debug.CrashOptions{})
342-
logei(err)("tun: crash output file %s, err? %v", fname(f), err)
343+
logei(err)("tun: crashout: set %s, err? %v", fname(f), err)
343344
return err == nil
344345
}
345346

346-
// SetCrashOutput will set the crash output file to dup(fd), and return true if successful.
347-
// Disables crash output if fd is less than 3.
348-
func SetCrashOutput(fd int) bool {
347+
// SetCrashOutput set crash output to file at fp; returns true if so.
348+
// Disables crash output if fp cannot be opened; and returns false.
349+
func SetCrashOutput(fp string) bool {
349350
p := crashpiped.Swap(false)
350351
ok := setCrashFd(nil)
351-
// defer core.Close(crashReader) if fd not owned by the client
352-
log.I("tun: closing crash out... ok? %t; was piped? %t; new fd: %d", ok, p, fd)
353-
if fd >= 2 {
354-
return setCrashFd(os.NewFile(uintptr(fd), "ktcfd"))
352+
// if fd not owned by client code
353+
// if p { defer core.Close(crashReader) }
354+
355+
fout, err := os.OpenFile(filepath.Clean(fp), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
356+
357+
logei(err)("tun: crashout: closed? %t; was piped? %t; f: %s; err? %v", ok, p, fp, err)
358+
359+
if err == nil {
360+
return setCrashFd(fout)
355361
}
356362
return false
357363
}
@@ -366,18 +372,18 @@ func pipeBuffer256k(f *os.File) bool {
366372
// kernel may round this up to the nearest page size multiple?
367373
x, err := unix.FcntlInt(fd, unix.F_SETPIPE_SZ, b256k)
368374
if err != nil {
369-
log.W("tun: pipe: (%s %d) err set size %d: %v", nom, fd, x, err)
375+
log.W("tun: crashout: pipe (%s %d) err set size %d: %v", nom, fd, x, err)
370376
return false
371377
}
372378

373379
x, err = unix.FcntlInt(fd, unix.F_GETPIPE_SZ, 0)
374380
if err != nil {
375-
log.W("tun: pipe: (%s %d) err get size: %v", nom, fd, err)
381+
log.W("tun: crashout: pipe (%s %d) err get size: %v", nom, fd, err)
376382
return false
377383
}
378384

379385
runtime.KeepAlive(f)
380-
log.W("tun: pipe: (%s %d) buffer %s", nom, fd, core.FmtBytes(uint64(x)))
386+
log.W("tun: crashout: pipe (%s %d) buffer %s", nom, fd, core.FmtBytes(uint64(x)))
381387
return true
382388
}
383389

0 commit comments

Comments
 (0)