From 626a365f790f628dc8deff27b2296545f239d3fe Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Wed, 1 Mar 2023 17:55:52 +0000 Subject: [PATCH] replace waitup with more generally useful ifwait * wait for $interface present before ip link set up dev * wait for wlan0 running before adding to bridge --- overlay.nix | 2 +- pkgs/ifwait/.#README | 1 + pkgs/{waitup => ifwait}/default.nix | 13 +++--- pkgs/ifwait/ifwait.fnl | 52 +++++++++++++++++++++++ pkgs/{waitup => ifwait}/waitup.fnl | 8 ++-- pkgs/liminix-tools/networking/default.nix | 2 + rotuer.nix | 18 +++----- 7 files changed, 75 insertions(+), 21 deletions(-) create mode 120000 pkgs/ifwait/.#README rename pkgs/{waitup => ifwait}/default.nix (60%) create mode 100644 pkgs/ifwait/ifwait.fnl rename pkgs/{waitup => ifwait}/waitup.fnl (90%) diff --git a/overlay.nix b/overlay.nix index a77dcbf..7ec77e4 100644 --- a/overlay.nix +++ b/overlay.nix @@ -87,7 +87,7 @@ final: prev: { }; netlink-lua = final.callPackage ./pkgs/netlink-lua {}; - waitup = final.callPackage ./pkgs/waitup {}; + ifwait = final.callPackage ./pkgs/ifwait {}; serviceFns = final.writeText "service-fns.sh" '' output() { cat $1/.outputs/$2; } diff --git a/pkgs/ifwait/.#README b/pkgs/ifwait/.#README new file mode 120000 index 0000000..c1c90e4 --- /dev/null +++ b/pkgs/ifwait/.#README @@ -0,0 +1 @@ +dan@noetbook.911352349782478447 \ No newline at end of file diff --git a/pkgs/waitup/default.nix b/pkgs/ifwait/default.nix similarity index 60% rename from pkgs/waitup/default.nix rename to pkgs/ifwait/default.nix index 0639c7c..ce9ba85 100644 --- a/pkgs/waitup/default.nix +++ b/pkgs/ifwait/default.nix @@ -9,21 +9,22 @@ let netlink = netlink-lua.override {inherit lua;}; fennel = lua.pkgs.fennel; in stdenv.mkDerivation rec { - pname = "waitup"; + pname = "ifwait"; version = "1"; + phases = [ "installPhase" ]; - buildInputs = [ lua netlink-lua ]; - nativeBuildInputs = [ makeWrapper fennel ]; + buildInputs = [ lua netlink ]; + nativeBuildInputs = [ makeWrapper fennel ]; - src = ./.; + LUA_CPATH = "${netlink}/lib/lua/${lua.luaversion}/\?.so"; # for nix-shell installPhase = '' mkdir -p $out/bin $out/lib - fennel --compile ${./waitup.fnl} > $out/lib/waitup.lua + fennel --compile ${./ifwait.fnl} > $out/lib/${pname}.lua makeWrapper ${lua}/bin/lua $out/bin/${pname} \ --prefix LUA_CPATH ";" ${netlink}/lib/lua/${lua.luaversion}/\?.so \ - --add-flags $out/lib/waitup.lua + --add-flags $out/lib/${pname}.lua ''; } diff --git a/pkgs/ifwait/ifwait.fnl b/pkgs/ifwait/ifwait.fnl new file mode 100644 index 0000000..36dd3e7 --- /dev/null +++ b/pkgs/ifwait/ifwait.fnl @@ -0,0 +1,52 @@ +(local netlink (require :netlink)) +(local sock (netlink.socket)) + +; (local { : view} (require :fennel)) + +(fn assoc [tbl k v] + (tset tbl k v) + tbl) + +(fn parse-args [args] + (match args + ["-v" & rest] (assoc (parse-args rest) :verbose true) + ["-t" timeout & rest] (assoc (parse-args rest) :timeout (tonumber timeout)) + [linkname "up"] {:link linkname :expecting "up"} + [linkname "running"] {:link linkname :expecting "running"} + [linkname "present"] {:link linkname :expecting "present"} + [linkname nil] {:link linkname :expecting "present"} + _ nil)) + +(local parameters + (or + (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} + + {: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))))) + + +(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))) diff --git a/pkgs/waitup/waitup.fnl b/pkgs/ifwait/waitup.fnl similarity index 90% rename from pkgs/waitup/waitup.fnl rename to pkgs/ifwait/waitup.fnl index e5675a8..1addf1c 100644 --- a/pkgs/waitup/waitup.fnl +++ b/pkgs/ifwait/waitup.fnl @@ -10,9 +10,11 @@ (local stream (io.open (.. "/proc/self/fd/" fd) "w")) (fn notify-ready [] - (stream:write "\n") - (print (.. (. arg 0) ": received netlink operstate up for " ifname)) - (stream:close)) + (when (= "file" (io.type stream)) + (stream:write "\n") + (stream:close)) + (print (.. (. arg 0) ": received netlink operstate up for " ifname))) + (fn run-events [evs] (each [_ v (ipairs evs)] diff --git a/pkgs/liminix-tools/networking/default.nix b/pkgs/liminix-tools/networking/default.nix index b85b866..d0444ca 100644 --- a/pkgs/liminix-tools/networking/default.nix +++ b/pkgs/liminix-tools/networking/default.nix @@ -1,6 +1,7 @@ { callPackage , liminix +, ifwait , lib }: let @@ -12,6 +13,7 @@ in { [] ++ optional (type == "bridge") "ip link add name ${device} type bridge" + ++ ["${ifwait}/bin/ifwait -v ${device} present"] ++ ["ip link set up dev ${device}"] ++ optional (primary != null) "ip link set dev ${device} master ${primary.device}"; diff --git a/rotuer.nix b/rotuer.nix index 9c2fef8..bb85897 100644 --- a/rotuer.nix +++ b/rotuer.nix @@ -19,7 +19,7 @@ let route; inherit (pkgs.liminix.services) oneshot longrun bundle target; inherit (pkgs) - waitup + ifwait serviceFns iptables; in rec { @@ -122,17 +122,13 @@ in rec { }; services.bridgewlan = - let waitup-wlan = longrun { - name = "waitup-wlan0"; - run = "${waitup}/bin/waitup wlan0 10"; - notification-fd = 10; - dependencies = [ services.wireless services.hostap ]; - }; + let dev = services.wireless.device; in oneshot { - name = "add-wlan-to-bridge"; - up = "ip link set dev ${services.wireless.device} master ${services.lan.device}"; - down = "ip link set dev ${services.wireless.device} nomaster"; - dependencies = [ waitup-wlan ]; + name = "add-wlan2-to-bridge"; + up = "${ifwait}/bin/ifwait -v ${dev} running && ip link set dev ${dev} master ${services.lan.device}"; + down = "ip link set dev ${dev} nomaster"; + dependencies = [ services.wireless ]; + }; }; users.dnsmasq = {