From e20c9546eafa8a0da2b5b3451f223301b671910d Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Fri, 20 Mar 2020 13:15:02 +1100 Subject: [PATCH] replacing the init system with something more OpenOS rc inspired --- lib/rc.lua | 76 +++++++++++++++++++++++++++++++++++++++++++++++ module/init.lua | 14 ++++----- service/getty.lua | 10 +++++++ 3 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 lib/rc.lua diff --git a/lib/rc.lua b/lib/rc.lua new file mode 100644 index 0000000..1ef1cd4 --- /dev/null +++ b/lib/rc.lua @@ -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 diff --git a/module/init.lua b/module/init.lua index a2f858e..11985dd 100644 --- a/module/init.lua +++ b/module/init.lua @@ -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() diff --git a/service/getty.lua b/service/getty.lua index df2467f..fcdb926 100644 --- a/service/getty.lua +++ b/service/getty.lua @@ -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