From e49aba127c63f4d4fbd09fba2019707899445477 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Fri, 26 Apr 2024 20:49:23 +0100 Subject: [PATCH] devout: improve socket error handling --- pkgs/devout/devout.fnl | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/pkgs/devout/devout.fnl b/pkgs/devout/devout.fnl index 3e63318..6750277 100644 --- a/pkgs/devout/devout.fnl +++ b/pkgs/devout/devout.fnl @@ -11,6 +11,12 @@ (fn trace [expr] (do (print :TRACE (view expr)) expr)) +(macro check-errno [expr] + (let [{ :view v } (require :fennel)] + `(case ,expr + val# val# + (nil err#) (error (string.format "%s failed: errno=%d" ,(v expr) err#))))) + (fn parse-event [s] (let [at (string.find s "@" 1 true) (nl nxt) (string.find s "\0" 1 true)] @@ -81,9 +87,9 @@ (fn unix-socket [name] (let [addr (string.pack "=Hz" AF_LOCAL name)] - (match (ll.socket AF_LOCAL SOCK_STREAM 0) - fd (match (ll.bind fd addr) - 0 (doto fd (ll.listen 32)) + (case (ll.socket AF_LOCAL SOCK_STREAM 0) + fd (case (ll.bind fd addr) + 0 (doto fd (ll.listen 32)) (nil err) (values nil err)) (nil err) (values nil err)))) @@ -138,17 +144,20 @@ (fn run [] (let [[sockname nl-groups] arg - s (unix-socket sockname) + s (check-errno (unix-socket sockname)) db (database) - nl (open-netlink nl-groups) + nl (check-errno (open-netlink nl-groups)) loop (event-loop)] (loop:register s - #(match (ll.accept s) - (client addr) - (do - (loop:register client (partial handle-client db)) - true))) + #(case + (ll.accept s) + (client addr) + (do + (loop:register client (partial handle-client db)) + true) + (nil err) + (print (string.format "error accepting connection, errno=%d" err)))) (loop:register nl #(do (db:add (ll.read nl)) true))