diff --git a/package/gluon-announce/files/lib/gluon/announce/collect.lua b/package/gluon-announce/files/lib/gluon/announce/collect.lua index 43392075..3510fbf0 100755 --- a/package/gluon-announce/files/lib/gluon/announce/collect.lua +++ b/package/gluon-announce/files/lib/gluon/announce/collect.lua @@ -2,7 +2,6 @@ local announce = require 'gluon.announce' local json = require 'luci.jsonc' -local ltn12 = require 'luci.ltn12' local announce_dir = '/lib/gluon/announce/' .. arg[1] .. '.d' diff --git a/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/hardware/nproc b/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/hardware/nproc index 3072f8f8..fc94bd5b 100644 --- a/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/hardware/nproc +++ b/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/hardware/nproc @@ -1,8 +1,8 @@ local n = 0 -local cpus = util.trim(fs.readfile('/sys/devices/system/cpu/online')) +local cpus = util.readline(io.open('/sys/devices/system/cpu/online')) -for _, entry in ipairs(cpus:split(',')) do +for entry in cpus:gmatch('([^,]+)') do local x, y = entry:match('(%d+)-(%d+)') if x then n = n + tonumber(y) - tonumber(x) + 1 diff --git a/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/software/firmware b/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/software/firmware index cf50f79f..53f6fbaf 100644 --- a/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/software/firmware +++ b/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/software/firmware @@ -1,4 +1,4 @@ return { - base = 'gluon-' .. util.trim(fs.readfile('/lib/gluon/gluon-version')), - release = util.trim(fs.readfile('/lib/gluon/release')), + base = 'gluon-' .. util.readline(io.open('/lib/gluon/gluon-version')), + release = util.readline(io.open('/lib/gluon/release')), } diff --git a/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/system/site_code b/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/system/site_code index 876fb6d8..fa7d9e80 100644 --- a/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/system/site_code +++ b/package/gluon-announce/files/lib/gluon/announce/nodeinfo.d/system/site_code @@ -1,3 +1 @@ -local site = require 'gluon.site_config' - -return site.site_code +return require('gluon.site_config').site_code diff --git a/package/gluon-announce/files/lib/gluon/announce/statistics.d/idletime b/package/gluon-announce/files/lib/gluon/announce/statistics.d/idletime index 845de268..61258820 100644 --- a/package/gluon-announce/files/lib/gluon/announce/statistics.d/idletime +++ b/package/gluon-announce/files/lib/gluon/announce/statistics.d/idletime @@ -1 +1 @@ -return tonumber(fs.readfile('/proc/uptime'):match('^[^ ]+ ([^ ]+)')) +return tonumber(util.readline(io.open('/proc/uptime')):match('^[^ ]+ ([^ ]+)')) diff --git a/package/gluon-announce/files/lib/gluon/announce/statistics.d/loadavg b/package/gluon-announce/files/lib/gluon/announce/statistics.d/loadavg index d79973aa..3548ac7a 100644 --- a/package/gluon-announce/files/lib/gluon/announce/statistics.d/loadavg +++ b/package/gluon-announce/files/lib/gluon/announce/statistics.d/loadavg @@ -1 +1 @@ -return tonumber(fs.readfile('/proc/loadavg'):match('^([^ ]+) ')) +return tonumber(util.readline(io.open('/proc/loadavg')):match('^([^ ]+) ')) diff --git a/package/gluon-announce/files/lib/gluon/announce/statistics.d/memory b/package/gluon-announce/files/lib/gluon/announce/statistics.d/memory index 7b07a107..2e880346 100644 --- a/package/gluon-announce/files/lib/gluon/announce/statistics.d/memory +++ b/package/gluon-announce/files/lib/gluon/announce/statistics.d/memory @@ -1,4 +1,4 @@ -local data = fs.readfile('/proc/meminfo') +local data = io.open('/proc/meminfo'):read('*a') local fields = {} for k, v in data:gmatch('([^\n:]+):%s*(%d+) kB') do diff --git a/package/gluon-announce/files/lib/gluon/announce/statistics.d/processes b/package/gluon-announce/files/lib/gluon/announce/statistics.d/processes index 33ecff66..30f09790 100644 --- a/package/gluon-announce/files/lib/gluon/announce/statistics.d/processes +++ b/package/gluon-announce/files/lib/gluon/announce/statistics.d/processes @@ -1,3 +1,3 @@ -local running, total = fs.readfile('/proc/loadavg'):match('^[^ ]+ [^ ]+ [^ ]+ (%d+)/(%d+)') +local running, total = util.readline(io.open('/proc/loadavg')):match('^[^ ]+ [^ ]+ [^ ]+ (%d+)/(%d+)') return { running = tonumber(running), total = tonumber(total) } diff --git a/package/gluon-announce/files/lib/gluon/announce/statistics.d/uptime b/package/gluon-announce/files/lib/gluon/announce/statistics.d/uptime index 0bc45bea..509d1470 100644 --- a/package/gluon-announce/files/lib/gluon/announce/statistics.d/uptime +++ b/package/gluon-announce/files/lib/gluon/announce/statistics.d/uptime @@ -1 +1 @@ -return tonumber(fs.readfile('/proc/uptime'):match('^([^ ]+) ')) +return tonumber(util.readline(io.open('/proc/uptime')):match('^([^ ]+) ')) diff --git a/package/gluon-announce/files/usr/lib/lua/gluon/announce.lua b/package/gluon-announce/files/usr/lib/lua/gluon/announce.lua index fdd2479e..444dbd33 100644 --- a/package/gluon-announce/files/usr/lib/lua/gluon/announce.lua +++ b/package/gluon-announce/files/usr/lib/lua/gluon/announce.lua @@ -4,7 +4,7 @@ module('gluon.announce', package.seeall) fs = require 'nixio.fs' uci = require('luci.model.uci').cursor() -util = require 'luci.util' +util = require 'gluon.util' local function collect_entry(entry) if fs.stat(entry, 'type') == 'dir' then diff --git a/package/gluon-core/files/usr/lib/lua/gluon/util.lua b/package/gluon-core/files/usr/lib/lua/gluon/util.lua index 7d7809b1..86b86805 100644 --- a/package/gluon-core/files/usr/lib/lua/gluon/util.lua +++ b/package/gluon-core/files/usr/lib/lua/gluon/util.lua @@ -38,7 +38,7 @@ local uci = require('luci.model.uci').cursor() module 'gluon.util' function exec(...) - return os.execute(escape_args('', ...)) + return os.execute(escape_args('', 'exec', ...)) end -- Removes all lines starting with a prefix from a file, optionally adding a new one @@ -52,6 +52,12 @@ function replace_prefix(file, prefix, add) os.rename(tmp, file) end +function readline(fd) + local line = fd:read('*l') + fd:close() + return line +end + function lock(file) exec('lock', file) end diff --git a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/neighbours.d/batadv b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/neighbours.d/batadv index e83f188f..69b7981d 100644 --- a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/neighbours.d/batadv +++ b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/neighbours.d/batadv @@ -5,7 +5,7 @@ function ifname2address(ifname) if ifname_address_cache[ifname] ~= nil then ifaddress = ifname_address_cache[ifname] else - ifaddress = util.trim(fs.readfile("/sys/class/net/" .. ifname .. "/address")) + ifaddress = util.readline(io.open("/sys/class/net/" .. ifname .. "/address")) ifname_address_cache[ifname] = ifaddress end diff --git a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/neighbours.d/wifi b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/neighbours.d/wifi index 26168003..bbe5fc96 100644 --- a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/neighbours.d/wifi +++ b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/neighbours.d/wifi @@ -16,18 +16,17 @@ end function interfaces() local interfaces = {} - for _, line in ipairs(util.split(util.exec('batctl if'))) do - ifname = line:match('^(.-): active') - if ifname ~= nil then - pcall(function() - local address = util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address')) - local wifitype = iwinfo.type(ifname) - if wifitype ~= nil then - interfaces[address] = { ifname = ifname, iw = iwinfo[wifitype] } - end - end) - end + local popen = io.popen('exec batctl if') + for ifname in popen:read('*a'):gmatch('([^%s]+): active') do + pcall(function() + local address = util.readline(io.open('/sys/class/net/' .. ifname .. '/address')) + local wifitype = iwinfo.type(ifname) + if wifitype ~= nil then + interfaces[address] = { ifname = ifname, iw = iwinfo[wifitype] } + end + end) end + popen:close() return interfaces end diff --git a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/network/mesh/bat0/interfaces b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/network/mesh/bat0/interfaces index 1ac22d30..b1d30efe 100644 --- a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/network/mesh/bat0/interfaces +++ b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/network/mesh/bat0/interfaces @@ -1,5 +1,3 @@ -local list = util.exec('batctl if') - local wireless = {} local tunnel = {} local other = {} @@ -7,21 +5,27 @@ local other = {} local function get_address(t, ifname) pcall( function() - table.insert(t, util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address'))) + table.insert(t, util.readline(io.open('/sys/class/net/' .. ifname .. '/address'))) end ) end -local function is_wireless(ifname) - local type = fs.stat('/sys/class/net/' .. ifname .. '/wireless', 'type') +local function file_exists(filename) + local f = io.open(filename) + if f == nil then + return false + else + f:close() + return true + end +end - return type == 'directory' +local function is_wireless(ifname) + return file_exists('/sys/class/net/' .. ifname .. '/wireless') end local function is_tuntap(ifname) - local type = fs.stat('/sys/class/net/' .. ifname .. '/tun_flags', 'type') - - return type == 'regular' + return file_exists('/sys/class/net/' .. ifname .. '/tun_flags') end local function nil_table(t) @@ -32,18 +36,17 @@ local function nil_table(t) end end -for _, line in ipairs(util.split(list)) do - local ifname = line:match('^(.-):') - if ifname ~= nil then - if is_wireless(ifname) then - get_address(wireless, ifname) - elseif is_tuntap(ifname) then - get_address(tunnel, ifname) - else - get_address(other, ifname) - end +local popen = io.popen('exec batctl if') +for ifname in popen:read('*a'):gmatch('([^%s]+): active') do + if is_wireless(ifname) then + get_address(wireless, ifname) + elseif is_tuntap(ifname) then + get_address(tunnel, ifname) + else + get_address(other, ifname) end end +popen:close() return { wireless = nil_table(wireless), diff --git a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/network/mesh_interfaces b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/network/mesh_interfaces index 1fef5e10..c69ca4ef 100644 --- a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/network/mesh_interfaces +++ b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/network/mesh_interfaces @@ -1,15 +1,13 @@ -local list = util.exec('batctl if') - local interfaces = {} -for _, line in ipairs(util.split(list)) do - local ifname = line:match('^(.-):') - if ifname ~= nil then - pcall( - function() - table.insert(interfaces, util.trim(fs.readfile('/sys/class/net/' .. ifname .. '/address'))) - end - ) - end + +local popen = io.popen('exec batctl if') +for ifname in popen:read('*a'):gmatch('([^%s]+): active') do + pcall( + function() + table.insert(interfaces, util.readline(io.open('/sys/class/net/' .. ifname .. '/address'))) + end + ) end +popen:close() return interfaces diff --git a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/software/batman-adv/version b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/software/batman-adv/version index abb317e9..e0f556cf 100644 --- a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/software/batman-adv/version +++ b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/nodeinfo.d/software/batman-adv/version @@ -1 +1 @@ -return util.trim(fs.readfile('/sys/module/batman_adv/version')) +return util.readline(io.open('/sys/module/batman_adv/version')) diff --git a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/statistics.d/gateway b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/statistics.d/gateway index a1be9ac0..67623b0c 100644 --- a/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/statistics.d/gateway +++ b/package/gluon-mesh-batman-adv-core/files/lib/gluon/announce/statistics.d/gateway @@ -1,5 +1,14 @@ -local gateway = util.trim(util.exec("batctl -m bat0 gateways | awk '/^=>/ { print $2 }'")) +local gateway = '' + +local popen = io.popen("exec batctl -m bat0 gateways") +for line in popen:lines() do + if line:sub(1, 3) == '=> ' then + gateway = line:sub(4, 20) + break + end +end +popen:close() if gateway ~= '' then - return gateway + return gateway end diff --git a/package/gluon-mesh-vpn-fastd/files/lib/gluon/announce/nodeinfo.d/software/fastd b/package/gluon-mesh-vpn-fastd/files/lib/gluon/announce/nodeinfo.d/software/fastd index bbfbda08..7b1ff4bc 100644 --- a/package/gluon-mesh-vpn-fastd/files/lib/gluon/announce/nodeinfo.d/software/fastd +++ b/package/gluon-mesh-vpn-fastd/files/lib/gluon/announce/nodeinfo.d/software/fastd @@ -1,4 +1,5 @@ -return { - enabled = uci:get_bool('fastd', 'mesh_vpn', 'enabled'), - version = util.exec('fastd -v'):match('^[^%s]+%s+([^\n]+)'), +local ret = { + enabled = uci:get('fastd', 'mesh_vpn', 'enabled') ~= 0, + version = util.readline(io.popen('exec fastd -v')):match('^[^%s]+%s+(.+)'), } +return ret diff --git a/package/gluon-mesh-vpn-fastd/files/lib/gluon/announce/statistics.d/mesh_vpn b/package/gluon-mesh-vpn-fastd/files/lib/gluon/announce/statistics.d/mesh_vpn index 0d6d66f0..42efe827 100644 --- a/package/gluon-mesh-vpn-fastd/files/lib/gluon/announce/statistics.d/mesh_vpn +++ b/package/gluon-mesh-vpn-fastd/files/lib/gluon/announce/statistics.d/mesh_vpn @@ -2,7 +2,6 @@ local json = require 'luci.jsonc' local ltn12 = require 'luci.ltn12' local nixio = require 'nixio' local site = require 'gluon.site_config' -local uci = require('luci.model.uci').cursor() local fastd_sock = nixio.socket('unix', 'stream') local socket_path = uci:get('fastd', 'mesh_vpn', 'status_socket')