Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion tailscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,37 @@ func TsnetSetLogFD(sd, fd C.int) C.int {
s.s.Logf = logger.Discard
return 0
}
f := os.NewFile(uintptr(fd), "logfd")
// Write to fd directly rather than through os.NewFile: an *os.File
// closes its fd when garbage collected, and fd belongs to the caller.
f := fdWriter(fd)
s.s.Logf = func(format string, args ...any) {
fmt.Fprintf(f, format, args...)
fmt.Fprintf(f, "\n")
}
return 0
}

// fdWriter is an io.Writer for a file descriptor it does not own.
type fdWriter int

func (w fdWriter) Write(b []byte) (int, error) {
var n int
for n < len(b) {
m, err := syscall.Write(int(w), b[n:])
if err == syscall.EINTR {
continue
}
if err != nil {
return n, err
}
if m == 0 {
return n, io.ErrShortWrite
}
n += m
}
return n, nil
}

//export TsnetLoopback
func TsnetLoopback(sd C.int, addrOut *C.char, addrLen C.size_t, proxyOut *C.char, localOut *C.char) C.int {
// Panic here to ensure we always leave the out values NUL-terminated.
Expand Down
4 changes: 4 additions & 0 deletions tailscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func TestGetRemoteAddr(t *testing.T) {
tsnetctest.RunTestGetRemoteAddr(t)
}

func TestSetLogFD(t *testing.T) {
tsnetctest.RunTestSetLogFD(t)
}

func TestExtractIP(t *testing.T) {
ipv4 := "1.23.33.4:12343"
ipv6 := "[1::2234::34fc::44]:56576"
Expand Down
37 changes: 37 additions & 0 deletions tsnetctest/tsnetctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,12 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"syscall"
"testing"
"time"

"golang.org/x/sys/unix"
"tailscale.com/net/netns"
"tailscale.com/tstest/integration"
"tailscale.com/tstest/integration/testcontrol"
Expand Down Expand Up @@ -404,3 +407,37 @@ func RunTestGetRemoteAddr(t *testing.T) {
t.Fatal(C.GoString(C.err))
}
}

// RunTestSetLogFD checks that libtailscale leaves the fd passed to
// tailscale_set_logfd open after the logger is replaced and the
// server is closed: the fd belongs to the caller.
func RunTestSetLogFD(t *testing.T) {
var p [2]int
if err := syscall.Pipe(p[:]); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
syscall.Close(p[0])
syscall.Close(p[1])
})

sd := C.tailscale_new()
if C.tailscale_set_logfd(sd, C.int(p[1])) != 0 {
t.Fatal("tailscale_set_logfd failed")
}
if C.tailscale_set_logfd(sd, -1) != 0 {
t.Fatal("tailscale_set_logfd(-1) failed")
}
if C.tailscale_close(sd) != 0 {
t.Fatal("tailscale_close failed")
}

// Give any finalizer holding the fd a chance to run.
for i := 0; i < 10; i++ {
runtime.GC()
time.Sleep(10 * time.Millisecond)
}
if _, err := unix.FcntlInt(uintptr(p[1]), unix.F_GETFD, 0); err != nil {
t.Fatalf("log fd %d was closed by libtailscale: %v", p[1], err)
}
}