From e8d06b59825a6b11663eb63618e65e97a51871f7 Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Tue, 28 Jul 2020 00:17:45 +0200 Subject: [PATCH] delete punchd socket on SIGINT --- punchd.nim | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/punchd.nim b/punchd.nim index 8c35839..032026f 100644 --- a/punchd.nim +++ b/punchd.nim @@ -8,6 +8,8 @@ import tcp_syni from strutils import format, join from nativesockets import setSockOptInt +const PunchdSocket = "/tmp/punchd.socket" + type # Requests TcpSyniConnect = object @@ -23,6 +25,11 @@ type srcPorts: array[3, Port] seqNums: seq[uint32] + Sigint = object of CatchableError + +proc handleSigint() {.noconv.} = + raise newException(Sigint, "received SIGINT") + proc handleRequest(line: string, unixSock: AsyncSocket) {.async.} = var id: string var sock: AsyncSocket @@ -77,12 +84,17 @@ proc handleUsers(sock: AsyncSocket) {.async.} = asyncCheck handleRequests(user) proc main() = - removeFile("/tmp/punchd.socket") + setControlCHook(handleSigint) + removeFile(PunchdSocket) let unixSocket = newAsyncSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP) - unixSocket.bindUnix("/tmp/punchd.socket") + unixSocket.bindUnix(PunchdSocket) unixSocket.listen() asyncCheck handleUsers(unixSocket) - runForever() + try: + runForever() + + except Sigint: + removeFile(PunchdSocket) when isMainModule: main()