lualoader: config: improve readConfFiles, rename to readConf

The previous interface was pretty bad, and required the caller to get some
implementation details correct that it really shouldn't need to (e.g.
loader_conf_files handling) and pass in an empty table for it to use.

The new and much improved interface, readConf, is much less of a hack;
hiding these implementation details and just doing the right thing.
config.lua will now use it to process /boot/defaults/loader.conf and the
subsequent loader_conf_files from there, and read-conf will also use it.

This improvement submitted by Olivier (cited below), loader_conf_files
handling from the original patch was changed to just clobber it before
processing and not bother restoring it after the fact following r360505
where it's now guaranteed to evade the loader environment.

PR:		244640
Submitted by:	Olivier Certner (olivier freebsd free fr>
This commit is contained in:
Kyle Evans 2020-04-30 21:04:39 +00:00
parent bf832717cf
commit 3fe0ac6aa5
3 changed files with 47 additions and 46 deletions

View File

@ -127,10 +127,7 @@ end
cli['read-conf'] = function(...)
local _, argv = cli.arguments(...)
-- Don't trigger a reload of previously loaded loader_conf_files, in
-- case this config file doesn't set it.
loader.setenv("loader_conf_files", "")
config.readConfFiles(assert(core.popFrontTable(argv)), {})
config.readConf(assert(core.popFrontTable(argv)))
end
cli['reload-conf'] = function(...)

View File

@ -488,36 +488,36 @@ function config.parse(text)
return status
end
function config.readConfFiles(files, loaded_files)
if files ~= nil then
-- The caller may not have passed in loader_conf_files; we could
-- have instead gotten some other string of files. We don't
-- want to trigger any redundant re-read/loads based on this.
local prefiles = getEnv("loader_conf_files")
for name in files:gmatch("([%w%p]+)%s*") do
if loaded_files[name] ~= nil then
goto continue
end
function config.readConf(file, loaded_files)
if loaded_files == nil then
loaded_files = {}
end
print("Loading " .. name)
-- These may or may not exist, and that's ok. Do a
-- silent parse so that we complain on parse errors but
-- not for them simply not existing.
if not config.processFile(name, true) then
print(MSG_FAILPARSECFG:format(name))
end
if loaded_files[file] ~= nil then
return
end
loaded_files[name] = true
local newfiles = getEnv("loader_conf_files")
if prefiles ~= newfiles then
-- Recurse; process the new files immediately.
-- If we come back and it turns out we've
-- already loaded the rest of what was in the
-- original loader_conf_files, no big deal.
config.readConfFiles(newfiles, loaded_files)
prefiles = newfiles
end
::continue::
print("Loading " .. file)
-- The final value of loader_conf_files is not important, so just
-- clobber it here. We'll later check if it's no longer nil and process
-- the new value for files to read.
setEnv("loader_conf_files", nil)
-- These may or may not exist, and that's ok. Do a
-- silent parse so that we complain on parse errors but
-- not for them simply not existing.
if not config.processFile(file, true) then
print(MSG_FAILPARSECFG:format(file))
end
loaded_files[file] = true
-- Going to process "loader_conf_files" extra-files
local loader_conf_files = getEnv("loader_conf_files")
if loader_conf_files ~= nil then
for name in loader_conf_files:gmatch("[%w%p]+") do
config.readConf(name, loaded_files)
end
end
end
@ -630,12 +630,7 @@ function config.load(file, reloading)
file = "/boot/defaults/loader.conf"
end
if not config.processFile(file) then
print(MSG_FAILPARSECFG:format(file))
end
local loaded_files = {file = true}
config.readConfFiles(getEnv("loader_conf_files"), loaded_files)
config.readConf(file)
checkNextboot()

View File

@ -26,7 +26,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd April 27, 2020
.Dd April 30, 2020
.Dt CONFIG.LUA 8
.Os
.Sh NAME
@ -59,15 +59,24 @@ to
A lookup will be done as needed to determine what value
.Ev idx
actually corresponds to.
.It Fn config.readConfFiles files loaded_files
.It Fn config.readConf file loaded_files
Process
.Ev files
as if it were
.Ev loader_conf_files .
The caller should pass in a table as the
.Pa file
as a configuration file
.Po e.g., as
.Pa loader.conf
.Pc
and then processing files listed in
.Ev loader_conf_files
variable
.Po see
.Xr loader.conf 5
.Pc .
The caller may optionally pass in a table as the
.Ev loaded_files
argument, which uses filenames as keys and any non-nil value to indicate that
the file named by the key has been loaded.
argument, which uses filenames as keys and any non-nil value to
indicate that the file named by the key has already been loaded and
should not be loaded again.
.It Fn config.processFile name silent
Process and parse
.Ev name