1
1
mirror of https://git.shadowkat.net/izaya/OC-PsychOS2.git synced 2024-11-22 03:54:20 -05:00

replacing the init system with something more OpenOS rc inspired

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

View File

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