1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00

(Sync Commit)

This commit is contained in:
Shelikhoo 2018-03-16 12:35:12 +08:00
parent 43abfc9463
commit 8e5063dedf
No known key found for this signature in database
GPG Key ID: 7791BDB0709ABD21
2 changed files with 66 additions and 12 deletions

View File

@ -23,7 +23,10 @@ type UnixInboundHandler struct {
func (uih *UnixInboundHandler) Start() {
var err error
uih.listenerHolder, err = domainsocket.ListenDS(uih.ctx, uih.path)
newError(err).AtError().WriteToLog()
if err != nil {
newError(err).AtError().WriteToLog()
}
}
func (uih *UnixInboundHandler) Close() {
if uih.listenerHolder != nil {

View File

@ -3,24 +3,75 @@ package domainsocket
import (
"context"
"net"
"os"
"syscall"
)
type Listener struct {
ln net.Listener
ln net.Listener
listenerChan <-chan net.Conn
ctx context.Context
path string
lockfile os.File
}
func ListenDS(ctx context.Context, path string) (*Listener, error) {
addr := new(net.UnixAddr)
addr.Name = path
addr.Net = "unix"
li, err := net.ListenUnix("unix", addr)
if err != nil {
return nil, err
}
vln := &Listener{ln: li}
vln := &Listener{path: path}
return vln, nil
}
func (ls *Listener) Down() {
ls.ln.Close()
func (ls *Listener) Down() error {
err := ls.ln.Close()
if err != nil {
newError(err).AtDebug().WriteToLog()
}
return err
}
//Setup systen level Listener
func (ls *Listener) LowerUP() error {
if isUnixDomainSocketFileSystemBased(ls.path) && !___DEBUG_IGNORE_FLOCK {
}
addr := new(net.UnixAddr)
addr.Name = ls.path
addr.Net = "unix"
li, err := net.ListenUnix("unix", addr)
if err != nil {
return err
}
}
func isUnixDomainSocketFileSystemBased(path string) bool {
//No Branching
return path[0] != 0
}
func AcquireLock(lockfilepath string) (*os.File, error) {
f, err := os.Create(lockfilepath)
if err != nil {
newError(err).AtDebug().WriteToLog()
return f, err
}
err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX)
if err != nil {
newError(err).AtDebug().WriteToLog()
err = f.Close()
if err != nil {
if ___DEBUG_PANIC_WHEN_ENCOUNTED_IMPOSSIBLE_ERROR {
panic(err)
}
newError(err).AtDebug().WriteToLog()
}
return nil, err
}
}
//DEBUG CONSTS
const ___DEBUG_IGNORE_FLOCK = false
const ___DEBUG_PANIC_WHEN_ERROR_UNPROPAGATEABLE = false
const ___DEBUG_PANIC_WHEN_ENCOUNTED_IMPOSSIBLE_ERROR = false