Commit 6687410a authored by Kyle Evans's avatar Kyle Evans
Browse files

makesyscalls: sprinkle some assert() on standard function calls

Improves our error reporting, ensuring that we aren't just ignoring
errors in the common case.

Note specifically the boundary where we have to change up our error
handling approach.  It's fine to error() out up until we create the
tempdir, then the rest should try to handle it gracefully and abort().
A future change will clean this up further by pcall'ing all of the bits
that cannot currently error() without cleaning up.
parent e12a0ffe
...@@ -95,28 +95,30 @@ local files = {} ...@@ -95,28 +95,30 @@ local files = {}
local function cleanup() local function cleanup()
for _, v in pairs(files) do for _, v in pairs(files) do
v:close() assert(v:close())
end end
if cleantmp then if cleantmp then
if lfs.dir(tmpspace) then if lfs.dir(tmpspace) then
for fname in lfs.dir(tmpspace) do for fname in lfs.dir(tmpspace) do
if fname ~= "." and fname ~= ".." then if fname ~= "." and fname ~= ".." then
os.remove(tmpspace .. "/" .. fname)) assert(os.remove(tmpspace .. "/" ..
fname))
end end
end end
end end
if lfs.attributes(tmpspace) and not lfs.rmdir(tmpspace) then if lfs.attributes(tmpspace) and not lfs.rmdir(tmpspace) then
io.stderr:write("Failed to clean up tmpdir: " .. assert(io.stderr:write("Failed to clean up tmpdir: " ..
tmpspace .. "\n") tmpspace .. "\n"))
end end
else else
io.stderr:write("Temp files left in " .. tmpspace .. "\n") assert(io.stderr:write("Temp files left in " .. tmpspace ..
"\n"))
end end
end end
local function abort(status, msg) local function abort(status, msg)
io.stderr:write(msg .. "\n") assert(io.stderr:write(msg .. "\n"))
cleanup() cleanup()
os.exit(status) os.exit(status)
end end
...@@ -208,14 +210,11 @@ local function process_config(file) ...@@ -208,14 +210,11 @@ local function process_config(file)
-- would need to sanitize the line for potentially special characters. -- would need to sanitize the line for potentially special characters.
local line_expr = "^([%w%p]+%s*)=(%s*[`\"]?[^\"`]+[`\"]?)" local line_expr = "^([%w%p]+%s*)=(%s*[`\"]?[^\"`]+[`\"]?)"
if file == nil then if not file then
return nil, "No file given" return nil, "No file given"
end end
local fh = io.open(file) local fh = assert(io.open(file))
if fh == nil then
return nil, "Could not open file"
end
for nextline in fh:lines() do for nextline in fh:lines() do
-- Strip any whole-line comments -- Strip any whole-line comments
...@@ -262,7 +261,7 @@ local function process_config(file) ...@@ -262,7 +261,7 @@ local function process_config(file)
end end
end end
io.close(fh) assert(io.close(fh))
return cfg return cfg
end end
...@@ -291,7 +290,7 @@ local function grab_capenabled(file, open_fail_ok) ...@@ -291,7 +290,7 @@ local function grab_capenabled(file, open_fail_ok)
end end
end end
io.close(fh) assert(io.close(fh))
return capentries return capentries
end end
...@@ -369,8 +368,8 @@ local function read_file(tmpfile) ...@@ -369,8 +368,8 @@ local function read_file(tmpfile)
end end
local fh = files[tmpfile] local fh = files[tmpfile]
fh:seek("set") assert(fh:seek("set"))
return fh:read("a") return assert(fh:read("a"))
end end
local function write_line(tmpfile, line) local function write_line(tmpfile, line)
...@@ -378,13 +377,13 @@ local function write_line(tmpfile, line) ...@@ -378,13 +377,13 @@ local function write_line(tmpfile, line)
print("Not found: " .. tmpfile) print("Not found: " .. tmpfile)
return return
end end
files[tmpfile]:write(line) assert(files[tmpfile]:write(line))
end end
local function write_line_pfile(tmppat, line) local function write_line_pfile(tmppat, line)
for k in pairs(files) do for k in pairs(files) do
if k:match(tmppat) ~= nil then if k:match(tmppat) ~= nil then
files[k]:write(line) assert(files[k]:write(line))
end end
end end
end end
...@@ -505,7 +504,7 @@ local function process_sysfile(file) ...@@ -505,7 +504,7 @@ local function process_sysfile(file)
process_syscall_def(prevline) process_syscall_def(prevline)
end end
io.close(fh) assert(io.close(fh))
return capentries return capentries
end end
...@@ -1108,7 +1107,7 @@ end ...@@ -1108,7 +1107,7 @@ end
-- Entry point -- Entry point
if #arg < 1 or #arg > 2 then if #arg < 1 or #arg > 2 then
abort(1, "usage: " .. arg[0] .. " input-file <config-file>") error("usage: " .. arg[0] .. " input-file <config-file>")
end end
local sysfile, configfile = arg[1], arg[2] local sysfile, configfile = arg[1], arg[2]
...@@ -1116,13 +1115,7 @@ local sysfile, configfile = arg[1], arg[2] ...@@ -1116,13 +1115,7 @@ local sysfile, configfile = arg[1], arg[2]
-- process_config either returns nil and a message, or a -- process_config either returns nil and a message, or a
-- table that we should merge into the global config -- table that we should merge into the global config
if configfile ~= nil then if configfile ~= nil then
local res, msg = process_config(configfile) local res = assert(process_config(configfile))
if res == nil then
-- Error... handle?
print(msg)
os.exit(1)
end
for k, v in pairs(res) do for k, v in pairs(res) do
if v ~= config[k] then if v ~= config[k] then
...@@ -1150,17 +1143,28 @@ process_compat() ...@@ -1150,17 +1143,28 @@ process_compat()
process_abi_flags() process_abi_flags()
if not lfs.mkdir(tmpspace) then if not lfs.mkdir(tmpspace) then
abort(1, "Failed to create tempdir " .. tmpspace) error("Failed to create tempdir " .. tmpspace)
end end
-- XXX Revisit the error handling here, we should probably move the rest of this
-- into a function that we pcall() so we can catch the errors and clean up
-- gracefully.
for _, v in ipairs(temp_files) do for _, v in ipairs(temp_files) do
local tmpname = tmpspace .. v local tmpname = tmpspace .. v
files[v] = io.open(tmpname, "w+") files[v] = io.open(tmpname, "w+")
-- XXX Revisit these with a pcall() + error handler
if not files[v] then
abort(1, "Failed to open temp file: " .. tmpname)
end
end end
for _, v in ipairs(output_files) do for _, v in ipairs(output_files) do
local tmpname = tmpspace .. v local tmpname = tmpspace .. v
files[v] = io.open(tmpname, "w+") files[v] = io.open(tmpname, "w+")
-- XXX Revisit these with a pcall() + error handler
if not files[v] then
abort(1, "Failed to open temp output file: " .. tmpname)
end
end end
-- Write out all of the preamble bits -- Write out all of the preamble bits
...@@ -1358,12 +1362,12 @@ write_line("systrace", read_file("systraceret")) ...@@ -1358,12 +1362,12 @@ write_line("systrace", read_file("systraceret"))
for _, v in ipairs(output_files) do for _, v in ipairs(output_files) do
local target = config[v] local target = config[v]
if target ~= "/dev/null" then if target ~= "/dev/null" then
local fh = io.open(target, "w+") local fh = assert(io.open(target, "w+"))
if fh == nil then if fh == nil then
abort(1, "Failed to open '" .. target .. "'") abort(1, "Failed to open '" .. target .. "'")
end end
fh:write(read_file(v)) assert(fh:write(read_file(v)))
fh:close() assert(fh:close())
end end
end end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment