replacing the init system with something more OpenOS rc inspired

This commit is contained in:
Izaya 2020-03-20 13:15:02 +11:00
parent 66ea129b7a
commit e20c9546ea
3 changed files with 91 additions and 9 deletions

76
lib/rc.lua Normal file
View File

@ -0,0 +1,76 @@
local serial = require "serialization"
local rc = {}
local service = {}
local cfg = {}
cfg.enabled = {}
local function loadConfig()
local f = io.open("/boot/cfg/rc.cfg","rb")
if not f then return false end
cfg = serial.unserialize(f:read("*a")) or {}
f:close()
cfg.enabled = cfg.enabled or {}
return true
end
local function saveConfig()
local f = io.open("/boot/cfg/rc.cfg","wb")
if not f then return false end
f:write(serial.serialize(cfg))
f:close()
return true
end
function rc.load(name,force)
if force then
rc.stop(name)
service[name] = nil
end
if service[name] then return true end
service[name] = setmetatable({},{__index=_G})
local f = io.open("/boot/service/"..name..".lua","rb")
local res = load(f:read("*a"),name,"t",service[name])()
f:close()
return res
end
function rc.stop(name,...)
if not service[name] then return false, "service not found" end
service[name].stop(...)
end
function rc.start(name,...)
rc.load(name)
if not service[name] then return false, "service not found" end
return service[name].start(...)
end
function rc.restart(name)
rc.stop(name)
rc.start(name)
end
function rc.enable(name)
for k,v in pairs(cfg.enabled) do
if v == name then return false end
end
saveConfig()
end
function rc.disable(name)
local disabled = false
for k,v in pairs(cfg.enabled) do
if v == name then table.remove(cfg.enabled,k) disabled = true break end
end
saveConfig()
return disabled
end
loadConfig()
for k,v in pairs(cfg.enabled) do
rc.start(v)
end
_G.service = service
return rc

View File

@ -22,20 +22,16 @@ os.spawn(function()
os.setenv("HOSTNAME",hostname)
syslog(string.format("Hostname set to %s",hostname))
local pids = {}
local function loadlist()
local f = io.open("/boot/cfg/init.txt","rb")
if not f then return false end
for line in f:read("*a"):gmatch("[^\r\n]+") do
pids[line] = -1
end
f:close()
local rc = require "rc"
for k,v in pairs(rc.cfg.enabled) do
pids[v] = -1
dprint(v)
end
loadlist()
while true do
for k,v in pairs(pids) do
if not os.taskInfo(v) then
syslog("Starting service "..k)
pids[k] = os.spawnfile("/boot/service/"..k)
pids[k] = rc.start(k)
end
end
coroutine.yield()

View File

@ -1,4 +1,5 @@
local gpus,screens,ttyn,pids = {}, {}, 0, {}
local basepid = nil
local shell = require "shell"
local function scan()
local w,di = pcall(computer.getDeviceInfo)
@ -53,9 +54,11 @@ local function allocate()
end
end
function start()
scan()
allocate()
dprint("screens ready")
basepid = os.spawn(function()
while true do
coroutine.yield()
for k,v in pairs(pids) do
@ -66,3 +69,10 @@ while true do
end
end
end
end,"getty")
return basepid
end
function stop()
os.kill(basepid)
basepid = nil
end