devout: improve socket error handling
This commit is contained in:
parent
324465bc18
commit
e49aba127c
1 changed files with 19 additions and 10 deletions
|
@ -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,8 +87,8 @@
|
||||||
|
|
||||||
(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
|
||||||
|
(ll.accept s)
|
||||||
(client addr)
|
(client addr)
|
||||||
(do
|
(do
|
||||||
(loop:register client (partial handle-client db))
|
(loop:register client (partial handle-client db))
|
||||||
true)))
|
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))
|
||||||
|
|
Loading…
Reference in a new issue