mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
[plugin] SSH: rejig how the Dropbear process is stopped so it will be stopped when the plugin is disabled
This commit is contained in:
committed by
Frans de Jonge
parent
cc9ef163fb
commit
d71d134844
@@ -99,15 +99,46 @@ function SSH:isRunning()
|
||||
return util.pathExists("/tmp/dropbear_koreader.pid")
|
||||
end
|
||||
|
||||
function SSH:stop()
|
||||
os.execute("cat /tmp/dropbear_koreader.pid | xargs kill")
|
||||
UIManager:show(InfoMessage:new {
|
||||
text = T(_("SSH server stopped.")),
|
||||
timeout = 2,
|
||||
})
|
||||
--- Stops the Dropbear process started by this plugin.
|
||||
--- @param force boolean If true, forces the process to stop if it doesn't exit gracefully.
|
||||
--- @return boolean Success, string|nil Error
|
||||
function SSH:stopPlugin(force)
|
||||
if not self:isRunning() then
|
||||
return true
|
||||
end
|
||||
|
||||
if self:isRunning() then
|
||||
os.remove("/tmp/dropbear_koreader.pid")
|
||||
local pid_path = "/tmp/dropbear_koreader.pid"
|
||||
|
||||
local function readPID()
|
||||
local f = io.open(pid_path, "r")
|
||||
if not f then return nil end
|
||||
local s = f:read("*l")
|
||||
f:close()
|
||||
return s and tonumber(s) or nil
|
||||
end
|
||||
|
||||
local pid = readPID()
|
||||
|
||||
local function isProcAlive(p)
|
||||
return p and util.pathExists("/proc/" .. p)
|
||||
end
|
||||
|
||||
local function send(sig, p)
|
||||
return os.execute(string.format("kill -%s %d", sig, p)) == 0
|
||||
end
|
||||
|
||||
send("TERM", pid)
|
||||
for _ = 1, 20 do
|
||||
if not isProcAlive(pid) then break end
|
||||
ffiutil.sleep(0.1)
|
||||
end
|
||||
|
||||
if isProcAlive(pid) and force then
|
||||
send("KILL", pid)
|
||||
for _ = 1, 10 do
|
||||
if not isProcAlive(pid) then break end
|
||||
ffiutil.sleep(0.1)
|
||||
end
|
||||
end
|
||||
|
||||
-- Plug the hole in the Kindle's firewall
|
||||
@@ -119,6 +150,33 @@ function SSH:stop()
|
||||
"iptables -D OUTPUT -p tcp --sport", self.SSH_port,
|
||||
"-m conntrack --ctstate ESTABLISHED -j ACCEPT"))
|
||||
end
|
||||
|
||||
if not isProcAlive(pid) then
|
||||
os.remove(pid_path)
|
||||
return true
|
||||
end
|
||||
return false, "dropbear process did not exit"
|
||||
end
|
||||
|
||||
function SSH:stop()
|
||||
local ok, err = self:stopPlugin(false)
|
||||
if not ok then
|
||||
logger.warn("SSH: graceful stop failed:", err)
|
||||
ok, err = self:stopPlugin(true)
|
||||
if not ok then
|
||||
logger.err("SSH: force-stop failed:", err)
|
||||
UIManager:show(InfoMessage:new{
|
||||
icon = "notice-warning",
|
||||
text = _("Failed to stop SSH server."),
|
||||
})
|
||||
end
|
||||
end
|
||||
if ok then
|
||||
UIManager:Show(InfoMessage:new{
|
||||
text = _("SSH server stopped."),
|
||||
timeout = 2,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
function SSH:onToggleSSHServer()
|
||||
|
||||
Reference in New Issue
Block a user