gluon-web-*: replace nixio with luaposix

This commit is contained in:
Matthias Schiffer 2018-07-13 22:45:23 +02:00
parent 92d90cdfb7
commit 06a9d61523
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
10 changed files with 68 additions and 64 deletions

View File

@ -1,5 +1,4 @@
<%- <%-
local fs = require 'nixio.fs'
local uci = require('simple-uci').cursor() local uci = require('simple-uci').cursor()
local pretty_hostname = require 'pretty_hostname' local pretty_hostname = require 'pretty_hostname'
@ -24,8 +23,8 @@
{ _('Hostname'), pretty_hostname.get(uci) }, { _('Hostname'), pretty_hostname.get(uci) },
{ _('MAC address'), sysconfig.primary_mac }, { _('MAC address'), sysconfig.primary_mac },
{ _('Hardware model'), platform.get_model() }, { _('Hardware model'), platform.get_model() },
{ _('Gluon version'), util.trim(fs.readfile('/lib/gluon/gluon-version')) }, { _('Gluon version'), util.trim(util.readfile('/lib/gluon/gluon-version')) },
{ _('Firmware release'), util.trim(fs.readfile('/lib/gluon/release')) }, { _('Firmware release'), util.trim(util.readfile('/lib/gluon/release')) },
{ _('Site'), site.site_name() }, { _('Site'), site.site_name() },
{ _('Public VPN key'), pubkey }, { _('Public VPN key'), pubkey },
} }

View File

@ -13,13 +13,13 @@ package 'gluon-web-admin'
local util = require 'gluon.util' local util = require 'gluon.util'
local fs = require 'nixio.fs' local unistd = require 'posix.unistd'
local tmpfile = "/tmp/firmware.img" local tmpfile = "/tmp/firmware.img"
local function filehandler(meta, chunk, eof) local function filehandler(meta, chunk, eof)
if not fs.access(tmpfile) and not file and chunk and #chunk > 0 then if not unistd.access(tmpfile) and not file and chunk and #chunk > 0 then
file = io.open(tmpfile, "w") file = io.open(tmpfile, "w")
end end
if file and chunk then if file and chunk then
@ -31,33 +31,35 @@ local function filehandler(meta, chunk, eof)
end end
local function action_upgrade(http, renderer) local function action_upgrade(http, renderer)
local nixio = require 'nixio' local fcntl = require 'posix.fcntl'
local stat = require 'posix.sys.stat'
local wait = require 'posix.sys.wait'
local function fork_exec(...) local function fork_exec(argv)
local pid = nixio.fork() local pid = unistd.fork()
if pid > 0 then if pid > 0 then
return return
elseif pid == 0 then elseif pid == 0 then
-- change to root dir -- change to root dir
nixio.chdir("/") unistd.chdir('/')
-- patch stdin, out, err to /dev/null -- patch stdin, out, err to /dev/null
local null = nixio.open("/dev/null", "w+") local null = fcntl.open('/dev/null', fcntl.O_RDWR)
if null then if null then
nixio.dup(null, nixio.stderr) unistd.dup2(null, unistd.STDIN_FILENO)
nixio.dup(null, nixio.stdout) unistd.dup2(null, unistd.STDOUT_FILENO)
nixio.dup(null, nixio.stdin) unistd.dup2(null, unistd.STDERR_FILENO)
if null:fileno() > 2 then if null > 2 then
null:close() unistd.close(null)
end end
end end
-- Sleep a little so the browser can fetch everything required to -- Sleep a little so the browser can fetch everything required to
-- display the reboot page, then reboot the device. -- display the reboot page, then reboot the device.
nixio.nanosleep(1) unistd.sleep(1)
-- replace with target command -- replace with target command
nixio.exec(...) unistd.exec(argv[0], argv)
end end
end end
@ -67,7 +69,7 @@ local function action_upgrade(http, renderer)
local function storage_size() local function storage_size()
local size = 0 local size = 0
if fs.access("/proc/mtd") then if unistd.access("/proc/mtd") then
for l in io.lines("/proc/mtd") do for l in io.lines("/proc/mtd") do
local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"') local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
if n == "linux" then if n == "linux" then
@ -75,7 +77,7 @@ local function action_upgrade(http, renderer)
break break
end end
end end
elseif fs.access("/proc/partitions") then elseif unistd.access("/proc/partitions") then
for l in io.lines("/proc/partitions") do for l in io.lines("/proc/partitions") do
local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)') local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
if b and n and not n:match('[0-9]') then if b and n and not n:match('[0-9]') then
@ -95,7 +97,7 @@ local function action_upgrade(http, renderer)
-- Determine state -- Determine state
local step = tonumber(http:getenv("REQUEST_METHOD") == "POST" and http:formvalue("step")) or 1 local step = tonumber(http:getenv("REQUEST_METHOD") == "POST" and http:formvalue("step")) or 1
local has_image = fs.access(tmpfile) local has_image = unistd.access(tmpfile)
local has_support = has_image and image_supported(tmpfile) local has_support = has_image and image_supported(tmpfile)
-- Step 1: file upload, error on unsupported image format -- Step 1: file upload, error on unsupported image format
@ -103,7 +105,7 @@ local function action_upgrade(http, renderer)
-- If there is an image but user has requested step 1 -- If there is an image but user has requested step 1
-- or type is not supported, then remove it. -- or type is not supported, then remove it.
if has_image then if has_image then
fs.unlink(tmpfile) unistd.unlink(tmpfile)
end end
renderer.render_layout('admin/upgrade', { renderer.render_layout('admin/upgrade', {
@ -114,17 +116,17 @@ local function action_upgrade(http, renderer)
elseif step == 2 then elseif step == 2 then
renderer.render_layout('admin/upgrade_confirm', { renderer.render_layout('admin/upgrade_confirm', {
checksum = image_checksum(tmpfile), checksum = image_checksum(tmpfile),
filesize = fs.stat(tmpfile).size, filesize = stat.stat(tmpfile).st_size,
flashsize = storage_size(), flashsize = storage_size(),
keepconfig = (http:formvalue("keepcfg") == "1"), keepconfig = (http:formvalue("keepcfg") == "1"),
}, 'gluon-web-admin') }, 'gluon-web-admin')
elseif step == 3 then elseif step == 3 then
if http:formvalue("keepcfg") == "1" then local cmd = {[0] = '/sbin/sysupgrade', tmpfile}
fork_exec("/sbin/sysupgrade", tmpfile) if http:formvalue('keepcfg') ~= '1' then
else table.insert(cmd, 1, '-n')
fork_exec("/sbin/sysupgrade", "-n", tmpfile)
end end
fork_exec(cmd)
renderer.render_layout('admin/upgrade_reboot', nil, 'gluon-web-admin', { renderer.render_layout('admin/upgrade_reboot', nil, 'gluon-web-admin', {
hidenav = true, hidenav = true,
}) })
@ -132,7 +134,7 @@ local function action_upgrade(http, renderer)
end end
local has_platform = fs.access("/lib/upgrade/platform.sh") local has_platform = unistd.access("/lib/upgrade/platform.sh")
if has_platform then if has_platform then
local upgrade = entry({"admin", "upgrade"}, call(action_upgrade), _("Upgrade firmware"), 90) local upgrade = entry({"admin", "upgrade"}, call(action_upgrade), _("Upgrade firmware"), 90)
upgrade.filehandler = filehandler upgrade.filehandler = filehandler

View File

@ -10,24 +10,28 @@ You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
]]-- ]]--
local nixio = require "nixio" local util = require 'gluon.util'
local fs = require "nixio.fs" local site = require 'gluon.site'
local util = require "gluon.util"
local site = require "gluon.site" local fcntl = require 'posix.fcntl'
local unistd = require 'posix.unistd'
local wait = require 'posix.sys.wait'
local f_keys = Form(translate("SSH keys"), translate("You can provide your SSH keys here (one per line):"), 'keys') local f_keys = Form(translate("SSH keys"), translate("You can provide your SSH keys here (one per line):"), 'keys')
local s = f_keys:section(Section) local s = f_keys:section(Section)
local keys = s:option(TextValue, "keys") local keys = s:option(TextValue, "keys")
keys.wrap = "off" keys.wrap = "off"
keys.rows = 5 keys.rows = 5
keys.default = fs.readfile("/etc/dropbear/authorized_keys") or "" keys.default = util.readfile("/etc/dropbear/authorized_keys") or ""
function keys:write(value) function keys:write(value)
value = util.trim(value:gsub("\r", "")) value = util.trim(value:gsub("\r", ""))
if value ~= "" then if value ~= "" then
fs.writefile("/etc/dropbear/authorized_keys", value .. "\n") local f = io.open("/etc/dropbear/authorized_keys", "w")
f:write(value, "\n")
f:close()
else else
fs.remove("/etc/dropbear/authorized_keys") unistd.unlink("/etc/dropbear/authorized_keys")
end end
end end
@ -72,34 +76,34 @@ function pw2.cfgvalue()
end end
local function set_password(password) local function set_password(password)
local inr, inw = nixio.pipe() local inr, inw = unistd.pipe()
local pid = nixio.fork() local pid = unistd.fork()
if pid < 0 then if pid < 0 then
return false return false
elseif pid == 0 then elseif pid == 0 then
inw:close() unistd.close(inw)
local null = nixio.open('/dev/null', 'w') local null = fcntl.open('/dev/null', fcntl.O_WRONLY)
nixio.dup(null, nixio.stderr) unistd.dup2(null, unistd.STDOUT_FILENO)
nixio.dup(null, nixio.stdout) unistd.dup2(null, unistd.STDERR_FILENO)
if null:fileno() > 2 then if null > 2 then
null:close() unistd.close(null)
end end
nixio.dup(inr, nixio.stdin) unistd.dup2(inr, unistd.STDIN_FILENO)
inr:close() unistd.close(inr)
nixio.execp('passwd') unistd.execp('passwd', {[0] = 'passwd'})
os.exit(127) os.exit(127)
end end
inr:close() unistd.close(inr)
inw:write(string.format('%s\n%s\n', password, password)) unistd.write(inw, string.format('%s\n%s\n', password, password))
inw:close() unistd.close(inw)
local wpid, status, code = nixio.waitpid(pid) local wpid, status, code = wait.wait(pid)
return wpid and status == 'exited' and code == 0 return wpid and status == 'exited' and code == 0
end end

View File

@ -4,7 +4,7 @@
module('gluon.web.model', package.seeall) module('gluon.web.model', package.seeall)
local fs = require 'nixio.fs' local unistd = require 'posix.unistd'
local classes = require 'gluon.web.model.classes' local classes = require 'gluon.web.model.classes'
local util = require 'gluon.web.util' local util = require 'gluon.web.util'
@ -38,7 +38,7 @@ return function(config, http, renderer, name, pkg)
local modeldir = config.base_path .. '/model/' local modeldir = config.base_path .. '/model/'
local filename = modeldir..name..'.lua' local filename = modeldir..name..'.lua'
if not fs.access(filename) then if not unistd.access(filename) then
error("Model '" .. name .. "' not found!") error("Model '" .. name .. "' not found!")
end end

View File

@ -1,4 +1,3 @@
local fs = require 'nixio.fs'
local iwinfo = require 'iwinfo' local iwinfo = require 'iwinfo'
local uci = require("simple-uci").cursor() local uci = require("simple-uci").cursor()
local util = require 'gluon.util' local util = require 'gluon.util'

View File

@ -9,7 +9,7 @@ include ../gluon.mk
define Package/gluon-web define Package/gluon-web
TITLE:=Minimal Lua web framework derived from LuCI TITLE:=Minimal Lua web framework derived from LuCI
DEPENDS:=+lua-jsonc +luci-lib-nixio DEPENDS:=+lua-jsonc +luaposix
endef endef
define lang-config define lang-config

View File

@ -2,7 +2,7 @@
-- Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net> -- Copyright 2017 Matthias Schiffer <mschiffer@universe-factory.net>
-- Licensed to the public under the Apache License 2.0. -- Licensed to the public under the Apache License 2.0.
local nixio = require 'nixio' local stdlib = require 'posix.stdlib'
local http = require 'gluon.web.http' local http = require 'gluon.web.http'
local dispatcher = require 'gluon.web.dispatcher' local dispatcher = require 'gluon.web.dispatcher'
@ -27,9 +27,10 @@ local function limitsource(handle, limit)
end end
return function(config) return function(config)
local env = stdlib.getenv()
dispatcher(config, http.Http( dispatcher(config, http.Http(
nixio.getenv(), env,
limitsource(io.stdin, tonumber(nixio.getenv("CONTENT_LENGTH"))), limitsource(io.stdin, tonumber(env.CONTENT_LENGTH)),
io.stdout io.stdout
)) ))
end end

View File

@ -3,7 +3,7 @@
-- Copyright 2017-2018 Matthias Schiffer <mschiffer@universe-factory.net> -- Copyright 2017-2018 Matthias Schiffer <mschiffer@universe-factory.net>
-- Licensed to the public under the Apache License 2.0. -- Licensed to the public under the Apache License 2.0.
local fs = require "nixio.fs" local glob = require 'posix.glob'
local json = require "jsonc" local json = require "jsonc"
local tpl = require "gluon.web.template" local tpl = require "gluon.web.template"
local util = require "gluon.web.util" local util = require "gluon.web.util"
@ -158,10 +158,10 @@ local function dispatch(config, http, request)
ctl() ctl()
end end
for path in (fs.glob(base .. "*.lua") or function() end) do for _, path in ipairs(glob.glob(base .. "*.lua") or {}) do
load_ctl(path) load_ctl(path)
end end
for path in (fs.glob(base .. "*/*.lua") or function() end) do for _, path in ipairs(glob.glob(base .. "*/*.lua") or {}) do
load_ctl(path) load_ctl(path)
end end
end end

View File

@ -4,7 +4,6 @@
local string = string local string = string
local table = table local table = table
local nixio = require "nixio"
local protocol = require "gluon.web.http.protocol" local protocol = require "gluon.web.http.protocol"
local util = require "gluon.web.util" local util = require "gluon.web.util"

View File

@ -1,8 +1,8 @@
-- Copyright 2018 Matthias Schiffer <mschiffer@universe-factory.net> -- Copyright 2018 Matthias Schiffer <mschiffer@universe-factory.net>
-- Licensed to the public under the Apache License 2.0. -- Licensed to the public under the Apache License 2.0.
local tparser = require "gluon.web.template.parser" local tparser = require 'gluon.web.template.parser'
local fs = require "nixio.fs" local unistd = require 'posix.unistd'
return function(config) return function(config)
@ -20,7 +20,7 @@ return function(config)
local function load_catalog(lang, pkg) local function load_catalog(lang, pkg)
if pkg then if pkg then
local file = i18n_file(lang, pkg) local file = i18n_file(lang, pkg)
local cat = fs.access(file) and tparser.load_catalog(file) local cat = unistd.access(file) and tparser.load_catalog(file)
if cat then return cat end if cat then return cat end
end end
@ -32,7 +32,7 @@ return function(config)
local i18n = {} local i18n = {}
function i18n.supported(lang) function i18n.supported(lang)
return lang == 'en' or fs.access(i18n_file(lang, 'gluon-web')) return lang == 'en' or unistd.access(i18n_file(lang, 'gluon-web'))
end end
function i18n.load(lang, pkg) function i18n.load(lang, pkg)