luci-app-openvpn: various fixes and optimizations

Closes #628 as suggestion was implemented and tested.
Added PID handling function (Fixes https://dev.openwrt.org/ticket/21506).

Signed-off-by: Vladimir Ulrich <admin@evl.su>
This commit is contained in:
Vladimir Ulrich 2016-06-23 19:12:18 +03:00 committed by Jo-Philipp Wich
parent af9d1b02a9
commit 52de960bb5
3 changed files with 12 additions and 7 deletions

View file

@ -235,7 +235,6 @@ for _, option in ipairs(params) do
o.value = option[3] o.value = option[3]
else else
if option[1] == DynamicList then if option[1] == DynamicList then
o.cast = nil
function o.cfgvalue(...) function o.cfgvalue(...)
local val = AbstractValue.cfgvalue(...) local val = AbstractValue.cfgvalue(...)
return ( val and type(val) ~= "table" ) and { val } or val return ( val and type(val) ~= "table" ) and { val } or val

View file

@ -62,7 +62,6 @@ for _, option in ipairs(basicParams) do
o.value = option[3] o.value = option[3]
else else
if option[1] == DynamicList then if option[1] == DynamicList then
o.cast = nil
function o.cfgvalue(...) function o.cfgvalue(...)
local val = AbstractValue.cfgvalue(...) local val = AbstractValue.cfgvalue(...)
return ( val and type(val) ~= "table" ) and { val } or val return ( val and type(val) ~= "table" ) and { val } or val

View file

@ -25,8 +25,13 @@ uci:foreach( "openvpn_recipes", "openvpn_recipe",
end end
) )
function s.getPID(section) function s.getPID(section) -- Universal function which returns valid pid # or nil
return sys.exec("%s | grep -w %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} ) local pid = sys.exec("%s | grep -w %s | grep openvpn | grep -v grep | awk '{print $1}'" % { psstring,section} )
if pid and #pid > 0 and tonumber(pid) ~= nil then
return tonumber(pid)
else
return nil
end
end end
function s.parse(self, section) function s.parse(self, section)
@ -73,7 +78,7 @@ s:option( Flag, "enabled", translate("Enabled") )
local active = s:option( DummyValue, "_active", translate("Started") ) local active = s:option( DummyValue, "_active", translate("Started") )
function active.cfgvalue(self, section) function active.cfgvalue(self, section)
local pid = s.getPID(section) local pid = s.getPID(section)
if pid and #pid > 0 and tonumber(pid) ~= nil then if pid ~= nil then
return (sys.process.signal(pid, 0)) return (sys.process.signal(pid, 0))
and translatef("yes (%i)", pid) and translatef("yes (%i)", pid)
or translate("no") or translate("no")
@ -88,7 +93,7 @@ updown.redirect = luci.dispatcher.build_url(
) )
function updown.cbid(self, section) function updown.cbid(self, section)
local pid = s.getPID(section) local pid = s.getPID(section)
self._state = pid and #pid > 0 and sys.process.signal(pid, 0) self._state = pid ~= nil and sys.process.signal(pid, 0)
self.option = self._state and "stop" or "start" self.option = self._state and "stop" or "start"
return AbstractValue.cbid(self, section) return AbstractValue.cbid(self, section)
end end
@ -99,7 +104,9 @@ end
function updown.write(self, section, value) function updown.write(self, section, value)
if self.option == "stop" then if self.option == "stop" then
local pid = s.getPID(section) local pid = s.getPID(section)
sys.process.signal(pid,15) if pid ~= nil then
sys.process.signal(pid,15)
end
else else
luci.sys.call("/etc/init.d/openvpn start %s" % section) luci.sys.call("/etc/init.d/openvpn start %s" % section)
end end