github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/net/listener_unix.go (about) 1 // +build linux darwin netbsd freebsd openbsd dragonfly 2 3 package net 4 5 import ( 6 nt "net" 7 "os" 8 "sync" 9 10 "golang.org/x/sys/unix" 11 ) 12 13 type listener struct { 14 f *os.File 15 fd int 16 ln nt.Listener 17 once sync.Once 18 pconn nt.PacketConn 19 lnaddr nt.Addr 20 addr, network string 21 } 22 23 // renormalize takes the net listener and detaches it from it's parent 24 // event loop, grabs the file descriptor, and makes it non-blocking. 25 func (ln *listener) renormalize() error { 26 var err error 27 switch netln := ln.ln.(type) { 28 case nil: 29 switch pconn := ln.pconn.(type) { 30 case *nt.UDPConn: 31 ln.f, err = pconn.File() 32 } 33 case *nt.TCPListener: 34 ln.f, err = netln.File() 35 case *nt.UnixListener: 36 ln.f, err = netln.File() 37 } 38 if err != nil { 39 ln.close() 40 return err 41 } 42 ln.fd = int(ln.f.Fd()) 43 return unix.SetNonblock(ln.fd, true) 44 } 45 46 func (ln *listener) close() { 47 ln.once.Do( 48 func() { 49 if ln.f != nil { 50 sniffErrorAndLog(ln.f.Close()) 51 } 52 if ln.ln != nil { 53 sniffErrorAndLog(ln.ln.Close()) 54 } 55 if ln.pconn != nil { 56 sniffErrorAndLog(ln.pconn.Close()) 57 } 58 if ln.network == "unix" { 59 sniffErrorAndLog(os.RemoveAll(ln.addr)) 60 } 61 }) 62 }