From 1e348ddd456a66876e75e0d67804e891f4ffc47c Mon Sep 17 00:00:00 2001
From: Nils Schneider <nils@nilsschneider.net>
Date: Wed, 22 Jan 2014 23:25:34 +0100
Subject: [PATCH] gluon-config-mode: delay reboot a little

The reboot page should be delayed a little to give the browser time for
fetching assets (like the stylesheet). This adds a two second delay.
Unfortunately, I couldn't think of a sane way to do this within a luci
controller.

This patch forks the process. The parent will continue rendering the
page for the browser while the child will sleep and then reboot the
device. For this to work reliable the child needs to close stdout before
sleeping so the webserver closes the connection to the browser. This,
again, required a hack as lua does not allow closing std filehandles,
when prevented using luci.reboot() which was calling os.execute() and
that function will not work with stdout closed.
---
 .../controller/gluon-config-mode/index.lua     | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/package/gluon-config-mode/files/usr/lib/lua/luci/controller/gluon-config-mode/index.lua b/package/gluon-config-mode/files/usr/lib/lua/luci/controller/gluon-config-mode/index.lua
index b05205ea6..1842caafd 100644
--- a/package/gluon-config-mode/files/usr/lib/lua/luci/controller/gluon-config-mode/index.lua
+++ b/package/gluon-config-mode/files/usr/lib/lua/luci/controller/gluon-config-mode/index.lua
@@ -51,7 +51,6 @@ function action_reboot()
   if meshvpn_enabled == "1" then
     pubkey = configmode.get_fastd_pubkey(meshvpn_name)
   end
-  luci.template.render("gluon-config-mode/reboot", {pubkey=pubkey})
 
   uci:foreach("gluon-config-mode", "wizard", function(s)
       uci:set("gluon-config-mode", s[".name"], "configured", "1")
@@ -59,5 +58,20 @@ function action_reboot()
   uci:save("gluon-config-mode")
   uci:commit("gluon-config-mode")
 
-  luci.sys.reboot()
+  if nixio.fork() ~= 0 then
+    luci.template.render("gluon-config-mode/reboot", {pubkey=pubkey})
+  else
+    debug.setfenv(io.stdout, debug.getfenv(io.open '/dev/null'))
+    io.stdout:close()
+
+    -- Sleep a little so the browser can fetch everything required to
+    -- display the reboot page, then reboot the device.
+    nixio.nanosleep(2)
+
+    -- Run reboot with popen so it gets its own std filehandles.
+    io.popen("reboot")
+
+    -- Prevent any further execution in this child.
+    os.exit()
+  end
 end
-- 
GitLab