fix(pocketbook): Networking and DNS resolution during early boot (#15244)
This commit is contained in:
1 parent
fa924651a6
commit
3566d5f8cb
1 file changed
+56
-7
@@ -11,6 +11,11 @@ local _ = require("gettext")
|
||||
|
||||
require("ffi/linux_input_h")
|
||||
|
||||
ffi.cdef[[
|
||||
int res_init(void);
|
||||
int __res_init(void);
|
||||
]]
|
||||
|
||||
local function yes() return true end
|
||||
local function no() return false end
|
||||
|
||||
@@ -392,17 +397,61 @@ function PocketBook:initNetworkManager(NetworkMgr)
|
||||
end
|
||||
end
|
||||
|
||||
function NetworkMgr:isConnected()
|
||||
function NetworkMgr:isWifiOn()
|
||||
return band(inkview.QueryNetwork(), C.NET_CONNECTED) ~= 0
|
||||
end
|
||||
NetworkMgr.isWifiOn = NetworkMgr.isConnected
|
||||
|
||||
local res_init_done = false
|
||||
function NetworkMgr:isConnected()
|
||||
local is_connected = self:isWifiOn() and self:hasDefaultRoute()
|
||||
|
||||
if is_connected then
|
||||
if not res_init_done then
|
||||
-- Re-init resolver once connected
|
||||
-- Workaround for glibc bug where /etc/resolv.conf is parsed
|
||||
-- only once. We force a reload when the route first appears.
|
||||
-- See https://sourceware.org/bugzilla/show_bug.cgi?id=984
|
||||
local ok = pcall(function() ffi.C.res_init() end)
|
||||
if not ok then pcall(function() ffi.C.__res_init() end) end
|
||||
res_init_done = true
|
||||
end
|
||||
else
|
||||
-- Connection lost; reset flag to trigger re-init on next success
|
||||
res_init_done = false
|
||||
end
|
||||
|
||||
return is_connected
|
||||
end
|
||||
|
||||
function NetworkMgr:isOnline()
|
||||
-- Fail early if we don't even have a default route, otherwise we're
|
||||
-- unlikely to be online and canResolveHostnames would never succeed
|
||||
-- again because PocketBook's glibc parses /etc/resolv.conf on first
|
||||
-- use only. See https://sourceware.org/bugzilla/show_bug.cgi?id=984
|
||||
return NetworkMgr:hasDefaultRoute() and NetworkMgr:canResolveHostnames()
|
||||
-- Override to call isConnected before canResolveHostnames
|
||||
-- to work around glibc bug. See https://sourceware.org/bugzilla/show_bug.cgi?id=984
|
||||
return self:isConnected() and self:canResolveHostnames()
|
||||
end
|
||||
|
||||
-- Ensure NetworkConnected is eventually broadcasted if KOReader boots
|
||||
-- while the system network stack is still coming up.
|
||||
local orig_init = NetworkMgr.init
|
||||
function NetworkMgr:init()
|
||||
local is_link_up = self:isWifiOn()
|
||||
local res = orig_init(self)
|
||||
|
||||
if is_link_up and not self.is_connected then
|
||||
local function waitForRoute(iter)
|
||||
iter = iter or 0
|
||||
if not self:isWifiOn() then return end
|
||||
if self:isConnected() then
|
||||
self.is_connected = true
|
||||
UIManager:broadcastEvent(require("ui/event"):new("NetworkConnected"))
|
||||
elseif iter < 90 then
|
||||
UIManager:scheduleIn(0.5, function() waitForRoute(iter + 1) end)
|
||||
else
|
||||
logger.warn("NetworkMgr: Network route not established after 60s, giving up.")
|
||||
end
|
||||
end
|
||||
UIManager:scheduleIn(0.5, function() waitForRoute(0) end)
|
||||
end
|
||||
return res
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in new issue
Block a user