From 5184ff63f7beebd545fc6817f10d256d47e31c4b Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Mon, 4 Mar 2024 23:01:19 +0000 Subject: [PATCH] add anoia.nl, a convenience wrapper on netlink --- pkgs/anoia/nl.fnl | 15 +++++++++++++++ pkgs/anoia/test-nl.fnl | 7 +++++++ pkgs/ifwait/ifwait.fnl | 40 ++++++++++++++++++---------------------- 3 files changed, 40 insertions(+), 22 deletions(-) create mode 100644 pkgs/anoia/nl.fnl create mode 100644 pkgs/anoia/test-nl.fnl diff --git a/pkgs/anoia/nl.fnl b/pkgs/anoia/nl.fnl new file mode 100644 index 0000000..8a65587 --- /dev/null +++ b/pkgs/anoia/nl.fnl @@ -0,0 +1,15 @@ +(local netlink (require :netlink)) + +(local { : view } (require :fennel)) + +(fn events [groups] + (let [sock (netlink.socket)] + (coroutine.wrap + (fn [] + (each [_ e (ipairs (sock:query groups))] + (coroutine.yield e)) + (while (sock:poll) + (each [_ e (ipairs (sock:event))] + (coroutine.yield e))))))) + +{ : events } diff --git a/pkgs/anoia/test-nl.fnl b/pkgs/anoia/test-nl.fnl new file mode 100644 index 0000000..07bf7b9 --- /dev/null +++ b/pkgs/anoia/test-nl.fnl @@ -0,0 +1,7 @@ +(local nl (require :anoia.nl)) +(local { : view } (require :fennel)) + +(let [events (nl.events {:link true})] + (each [ev events] + (print "got one ") + (print (view ev)))) diff --git a/pkgs/ifwait/ifwait.fnl b/pkgs/ifwait/ifwait.fnl index 36dd3e7..41e58bf 100644 --- a/pkgs/ifwait/ifwait.fnl +++ b/pkgs/ifwait/ifwait.fnl @@ -1,5 +1,4 @@ -(local netlink (require :netlink)) -(local sock (netlink.socket)) +(local nl (require :anoia.nl)) ; (local { : view} (require :fennel)) @@ -22,31 +21,28 @@ (parse-args arg) (assert false (.. "Usage: " (. arg 0) " [-v] ifname [present|up|running]")))) -(fn run-events [evs] - (each [_ v (ipairs evs)] - (let [got - (match v - ;; - up: Reflects the administrative state of the interface (IFF_UP) - ;; - running: Reflects the operational state (IFF_RUNNING). - {:event "newlink" :name parameters.link :up :yes :running :yes} - {:present true :up true :running true} +(fn run-event [v] + (let [got + (match v + ;; - up: Reflects the administrative state of the interface (IFF_UP) + ;; - running: Reflects the operational state (IFF_RUNNING). + {:event "newlink" :name parameters.link :up :yes :running :yes} + {:present true :up true :running true} - {:event "newlink" :name parameters.link :up :yes} - {:present :true :up true} + {:event "newlink" :name parameters.link :up :yes} + {:present :true :up true} - {:event "newlink" :name parameters.link} - {:present true } - - _ - {})] - (when (. got parameters.expecting) - (os.exit 0))))) + {:event "newlink" :name parameters.link} + {:present true } + _ + {})] + (when (. got parameters.expecting) + (os.exit 0)))) (when parameters.verbose (print (.. (. arg 0) ": waiting for " parameters.link " to be " parameters.expecting))) -(run-events (sock:query {:link true})) - -(while (sock:poll) (run-events (sock:event))) +(each [event (nl.events {:link true})] + (run-event event))