Skip to content

Commit

Permalink
Merge PR #930 (Supervisor, no snabb gc) into kbara-next
Browse files Browse the repository at this point in the history
  • Loading branch information
Katerina Barone-Adesi committed Jun 27, 2016
2 parents e7a5471 + 127d21f commit 7d76551
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 41 deletions.
58 changes: 54 additions & 4 deletions src/core/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ local STP = require("lib.lua.StackTracePlus")
local ffi = require("ffi")
local zone = require("jit.zone")
local lib = require("core.lib")
local shm = require("core.shm")
local C = ffi.C
-- Load ljsyscall early to help detect conflicts
-- (e.g. FFI type name conflict between Snabb and ljsyscall)
require("syscall")
local S = require("syscall")

require("lib.lua.strict")
require("lib.lua.class")
Expand All @@ -28,8 +29,8 @@ ffi.cdef[[
extern char** argv;
]]

_G.developer_debug = false
debug_on_error = false
_G.developer_debug = lib.getenv("SNABB_DEBUG") and true
debug_on_error = _G.developer_debug

function main ()
zone("startup")
Expand Down Expand Up @@ -130,6 +131,13 @@ function handler (reason)
os.exit(1)
end

-- Cleanup after Snabb process.
function shutdown (pid)
if not _G.developer_debug then
shm.unlink("//"..pid)
end
end

function selftest ()
print("selftest")
assert(programname("/bin/snabb-1.0") == "snabb",
Expand All @@ -154,4 +162,46 @@ function selftest ()
"Incorrect program name selected")
end

xpcall(main, handler)
-- Fork into worker process and supervisor
local worker_pid = S.fork()
if worker_pid == 0 then
-- Worker: Use prctl to ensure we are killed (SIGHUP) when our parent quits
-- and run main.
S.prctl("set_pdeathsig", "hup")
xpcall(main, handler)
else
-- Supervisor: Queue exit_signals using signalfd, prevent them from killing
-- us instantly using sigprocmask.
local exit_signals = "hup, int, quit, term, chld"
local signalfd = S.signalfd(exit_signals)
S.sigprocmask("block", exit_signals)
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))
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))
if worker.WIFEXITED then exit_status = worker.EXITSTATUS
elseif worker.WIFSIGNALED then exit_status = 128 + worker.WTERMSIG
-- WIFSTOPPED and WIFCONTINUED are ignored.
else goto ignore_signal end
else
-- Supervisor received exit signal: kill worker by sending SIGHUP
-- and and set exit status accordingly.
S.kill(worker_pid, "hup")
exit_status = 128 + signals[i].signo
end
-- Run shutdown routine and exit.
shutdown(worker_pid)
os.exit(exit_status)
::ignore_signal::
end
end
end
7 changes: 0 additions & 7 deletions src/program/gc/README

This file was deleted.

1 change: 0 additions & 1 deletion src/program/gc/README.inc

This file was deleted.

29 changes: 0 additions & 29 deletions src/program/gc/gc.lua

This file was deleted.

0 comments on commit 7d76551

Please sign in to comment.