From 598c5d01fc8ad54383a619f4de711734f5fe4cd3 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Wed, 7 Sep 2016 14:18:49 +0200 Subject: [PATCH 1/4] core.main: check the return value of fork. --- src/core/main.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/main.lua b/src/core/main.lua index 5930142313..9e2348053f 100644 --- a/src/core/main.lua +++ b/src/core/main.lua @@ -164,7 +164,8 @@ function selftest () end -- Fork into worker process and supervisor -local worker_pid = S.fork() +local worker_pid, err = S.fork() +assert(worker_pid, tostring(err)) if worker_pid == 0 then -- Worker: Use prctl to ensure we are killed (SIGHUP) when our parent quits -- and run main. From eabe7bf0e738a806c930afbaa546a265e0403b88 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Wed, 7 Sep 2016 15:04:01 +0200 Subject: [PATCH 2/4] Provide global assert implementation that plays nicely with ljsyscall. --- src/apps/socket/raw.lua | 7 ------- src/apps/socket/unix.lua | 7 ------- src/core/lib.lua | 7 +++++++ src/core/main.lua | 5 +++-- src/program/lisper/lisper.lua | 5 ----- 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/apps/socket/raw.lua b/src/apps/socket/raw.lua index 13ecb8d635..d668248072 100644 --- a/src/apps/socket/raw.lua +++ b/src/apps/socket/raw.lua @@ -12,13 +12,6 @@ local ethernet = require("lib.protocol.ethernet") local ffi = require("ffi") local C = ffi.C ---ljsyscall returns error as a a cdata instead of a string, ---and the standard assert() doesn't use tostring() on it. -local function assert(v, ...) - if not v then error(tostring(... or 'assertion failed'), 2) end - return v, ... -end - local c, t = S.c, S.types.t RawSocket = {} diff --git a/src/apps/socket/unix.lua b/src/apps/socket/unix.lua index 57d29f2b92..00216b9c3a 100644 --- a/src/apps/socket/unix.lua +++ b/src/apps/socket/unix.lua @@ -9,13 +9,6 @@ local link = require("core.link") local packet = require("core.packet") local S = require("syscall") ---ljsyscall returns error as a a cdata instead of a string, ---and the standard assert() doesn't use tostring() on it. -local function assert(v, ...) - if not v then error(tostring(... or 'assertion failed'), 2) end - return v, ... -end - UnixSocket = {} UnixSocket.__index = UnixSocket diff --git a/src/core/lib.lua b/src/core/lib.lua index 6e37a90964..c348ad39b9 100644 --- a/src/core/lib.lua +++ b/src/core/lib.lua @@ -11,6 +11,13 @@ local bit = require("bit") local band, bor, bnot, lshift, rshift, bswap = bit.band, bit.bor, bit.bnot, bit.lshift, bit.rshift, bit.bswap +-- Alternate version of assert that calls tostring on it second argument. Very +-- useful for working with ljsyscall. +function assert (v, ...) + if v then return v, ... end + error(tostring(... or "assertion failed!")) +end + -- Returns true if x and y are structurally similar (isomorphic). function equal (x, y) if type(x) ~= type(y) then return false end diff --git a/src/core/main.lua b/src/core/main.lua index 9e2348053f..d206351072 100644 --- a/src/core/main.lua +++ b/src/core/main.lua @@ -112,10 +112,11 @@ end --- Globally initialize some things. Module can depend on this being done. function initialize () - require("core.lib") + -- Global API + _G.lib = require("core.lib") + _G.assert = _G.lib.assert require("core.clib_h") require("core.lib_h") - -- Global API _G.config = require("core.config") _G.engine = require("core.app") _G.memory = require("core.memory") diff --git a/src/program/lisper/lisper.lua b/src/program/lisper/lisper.lua index 0f43e4c181..258425db7b 100644 --- a/src/program/lisper/lisper.lua +++ b/src/program/lisper/lisper.lua @@ -28,11 +28,6 @@ local ntohl = lib.ntohl local getenv = lib.getenv local hexdump = lib.hexdump -local function assert(v, ...) --assert overload because - if v then return v, ... end - error(tostring((...) or "Assertion failed"), 2) -end - local function parsehex(s) return (s:gsub("[0-9a-fA-F][0-9a-fA-F]", function(cc) return string.char(tonumber(cc, 16)) From 9daa25c57055fb2c639ca14168c493c74be1e828 Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Wed, 7 Sep 2016 15:05:50 +0200 Subject: [PATCH 3/4] Make use of new assert implementation. --- src/core/main.lua | 11 ++++------- src/core/memory.lua | 3 +-- src/lib/hardware/pci.lua | 12 ++++-------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/core/main.lua b/src/core/main.lua index d206351072..af456dca62 100644 --- a/src/core/main.lua +++ b/src/core/main.lua @@ -165,8 +165,7 @@ function selftest () end -- Fork into worker process and supervisor -local worker_pid, err = S.fork() -assert(worker_pid, tostring(err)) +local worker_pid = assert(S.fork()) if worker_pid == 0 then -- Worker: Use prctl to ensure we are killed (SIGHUP) when our parent quits -- and run main. @@ -181,16 +180,14 @@ else while true do -- Read signals from signalfd. Only process the first signal because any -- signal causes shutdown. - local signals, err = S.util.signalfd_read(signalfd) - assert(signals, tostring(err)) + local signals = assert(S.util.signalfd_read(signalfd)) for i = 1, #signals do local exit_status if signals[i].chld then -- SIGCHILD means worker state changed: retrieve its status using -- waitpid and set exit status accordingly. - local status, err, worker = - S.waitpid(worker_pid, "stopped,continued") - assert(status, tostring(err)) + local status, _, worker = + assert(S.waitpid(worker_pid, "stopped,continued")) if worker.WIFEXITED then exit_status = worker.EXITSTATUS elseif worker.WIFSIGNALED then exit_status = 128 + worker.WTERMSIG -- WIFSTOPPED and WIFCONTINUED are ignored. diff --git a/src/core/memory.lua b/src/core/memory.lua index ecfba556e1..30f8cc49e9 100644 --- a/src/core/memory.lua +++ b/src/core/memory.lua @@ -57,8 +57,7 @@ end --- ### HugeTLB: Allocate contiguous memory in bulk from Linux function allocate_hugetlb_chunk () - local fd, err = syscall.open("/proc/sys/vm/nr_hugepages","rdonly") - assert(fd, tostring(err)) + local fd = assert(syscall.open("/proc/sys/vm/nr_hugepages","rdonly")) fd:flock("ex") for i =1, 3 do local page = C.allocate_huge_page(huge_page_size) diff --git a/src/lib/hardware/pci.lua b/src/lib/hardware/pci.lua index de34a4d5d7..3c3fdf0c09 100644 --- a/src/lib/hardware/pci.lua +++ b/src/lib/hardware/pci.lua @@ -142,15 +142,12 @@ function map_pci_memory (device, n, lock) if lock then assert(f:flock("ex, nb"), "failed to lock " .. filepath) end - local st, err = f:stat() - assert(st, tostring(err)) - local mem, err = f:mmap(nil, st.size, "read, write", "shared", 0) - assert(mem, tostring(err)) + local st = assert(f:stat()) + local mem = assert(f:mmap(nil, st.size, "read, write", "shared", 0)) return ffi.cast("uint32_t *", mem), f end function close_pci_resource (fd, base) - local st, err = fd:stat() - assert(st, tostring(err)) + local st = assert(fd:stat()) S.munmap(base, st.size) fd:close() end @@ -159,8 +156,7 @@ end --- mastering is enabled. function set_bus_master (device, enable) root_check() - local f,err = S.open(path(device).."/config", "rdwr") - assert(f, tostring(err)) + local f = assert(S.open(path(device).."/config", "rdwr")) local fd = f:getfd() local value = ffi.new("uint16_t[1]") From 4411ad3498107af1a6e8fab85d69472d0932eeba Mon Sep 17 00:00:00 2001 From: Max Rottenkolber Date: Fri, 9 Sep 2016 14:36:12 +0200 Subject: [PATCH 4/4] Move lib.assert to core.main, fixing global name space breakage. --- src/core/lib.lua | 7 ------- src/core/main.lua | 12 +++++++++--- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/core/lib.lua b/src/core/lib.lua index c348ad39b9..6e37a90964 100644 --- a/src/core/lib.lua +++ b/src/core/lib.lua @@ -11,13 +11,6 @@ local bit = require("bit") local band, bor, bnot, lshift, rshift, bswap = bit.band, bit.bor, bit.bnot, bit.lshift, bit.rshift, bit.bswap --- Alternate version of assert that calls tostring on it second argument. Very --- useful for working with ljsyscall. -function assert (v, ...) - if v then return v, ... end - error(tostring(... or "assertion failed!")) -end - -- Returns true if x and y are structurally similar (isomorphic). function equal (x, y) if type(x) ~= type(y) then return false end diff --git a/src/core/main.lua b/src/core/main.lua index af456dca62..611f167152 100644 --- a/src/core/main.lua +++ b/src/core/main.lua @@ -19,6 +19,13 @@ local S = require("syscall") require("lib.lua.strict") require("lib.lua.class") +-- ljsyscall returns error as a cdata instead of a string, and the standard +-- assert doesn't use tostring on it. +_G.assert = function (v, ...) + if v then return v, ... end + error(tostring(... or "assertion failed!")) +end + -- Reserve names that we want to use for global module. -- (This way we avoid errors from the 'strict' module.) _G.config, _G.engine, _G.memory, _G.link, _G.packet, _G.timer, @@ -112,11 +119,10 @@ end --- Globally initialize some things. Module can depend on this being done. function initialize () - -- Global API - _G.lib = require("core.lib") - _G.assert = _G.lib.assert + require("core.lib") require("core.clib_h") require("core.lib_h") + -- Global API _G.config = require("core.config") _G.engine = require("core.app") _G.memory = require("core.memory")