delete punchd socket on SIGINT
This commit is contained in:
parent
5d08473d86
commit
e8d06b5982
16
punchd.nim
16
punchd.nim
|
@ -8,6 +8,8 @@ import tcp_syni
|
||||||
from strutils import format, join
|
from strutils import format, join
|
||||||
from nativesockets import setSockOptInt
|
from nativesockets import setSockOptInt
|
||||||
|
|
||||||
|
const PunchdSocket = "/tmp/punchd.socket"
|
||||||
|
|
||||||
type
|
type
|
||||||
# Requests
|
# Requests
|
||||||
TcpSyniConnect = object
|
TcpSyniConnect = object
|
||||||
|
@ -23,6 +25,11 @@ type
|
||||||
srcPorts: array[3, Port]
|
srcPorts: array[3, Port]
|
||||||
seqNums: seq[uint32]
|
seqNums: seq[uint32]
|
||||||
|
|
||||||
|
Sigint = object of CatchableError
|
||||||
|
|
||||||
|
proc handleSigint() {.noconv.} =
|
||||||
|
raise newException(Sigint, "received SIGINT")
|
||||||
|
|
||||||
proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} =
|
proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} =
|
||||||
var id: string
|
var id: string
|
||||||
var sock: AsyncSocket
|
var sock: AsyncSocket
|
||||||
|
@ -77,12 +84,17 @@ proc handleUsers(sock: AsyncSocket) {.async.} =
|
||||||
asyncCheck handleRequests(user)
|
asyncCheck handleRequests(user)
|
||||||
|
|
||||||
proc main() =
|
proc main() =
|
||||||
removeFile("/tmp/punchd.socket")
|
setControlCHook(handleSigint)
|
||||||
|
removeFile(PunchdSocket)
|
||||||
let unixSocket = newAsyncSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP)
|
let unixSocket = newAsyncSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP)
|
||||||
unixSocket.bindUnix("/tmp/punchd.socket")
|
unixSocket.bindUnix(PunchdSocket)
|
||||||
unixSocket.listen()
|
unixSocket.listen()
|
||||||
asyncCheck handleUsers(unixSocket)
|
asyncCheck handleUsers(unixSocket)
|
||||||
|
try:
|
||||||
runForever()
|
runForever()
|
||||||
|
|
||||||
|
except Sigint:
|
||||||
|
removeFile(PunchdSocket)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue