devout: improve socket error handling

This commit is contained in:
Daniel Barlow 2024-04-26 20:49:23 +01:00
parent 324465bc18
commit e49aba127c

View file

@ -11,6 +11,12 @@
(fn trace [expr] (fn trace [expr]
(do (print :TRACE (view expr)) 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] (fn parse-event [s]
(let [at (string.find s "@" 1 true) (let [at (string.find s "@" 1 true)
(nl nxt) (string.find s "\0" 1 true)] (nl nxt) (string.find s "\0" 1 true)]
@ -81,9 +87,9 @@
(fn unix-socket [name] (fn unix-socket [name]
(let [addr (string.pack "=Hz" AF_LOCAL name)] (let [addr (string.pack "=Hz" AF_LOCAL name)]
(match (ll.socket AF_LOCAL SOCK_STREAM 0) (case (ll.socket AF_LOCAL SOCK_STREAM 0)
fd (match (ll.bind fd addr) fd (case (ll.bind fd addr)
0 (doto fd (ll.listen 32)) 0 (doto fd (ll.listen 32))
(nil err) (values nil err)) (nil err) (values nil err))
(nil err) (values nil err)))) (nil err) (values nil err))))
@ -138,17 +144,20 @@
(fn run [] (fn run []
(let [[sockname nl-groups] arg (let [[sockname nl-groups] arg
s (unix-socket sockname) s (check-errno (unix-socket sockname))
db (database) db (database)
nl (open-netlink nl-groups) nl (check-errno (open-netlink nl-groups))
loop (event-loop)] loop (event-loop)]
(loop:register (loop:register
s s
#(match (ll.accept s) #(case
(client addr) (ll.accept s)
(do (client addr)
(loop:register client (partial handle-client db)) (do
true))) (loop:register client (partial handle-client db))
true)
(nil err)
(print (string.format "error accepting connection, errno=%d" err))))
(loop:register (loop:register
nl nl
#(do (db:add (ll.read nl)) true)) #(do (db:add (ll.read nl)) true))