57 lines
1.8 KiB
Lua
57 lines
1.8 KiB
Lua
local kargs = ...
|
|
local kio = kernel.kmode_run("return kio")
|
|
local tty = kernel.kmode_run("return tty")
|
|
local exec = kernel.kmode_run("return exec")
|
|
local thd = require("thread")
|
|
local fs = require("filesystem")
|
|
local security = require("security")
|
|
|
|
kernel.dmesg(kernel.loglevel.INFO, "ksysinit started")
|
|
local function run_hooks(hook, args)
|
|
local hooks = fs.list("/etc/ksysinit/"..hook)
|
|
for i=1, #hooks do
|
|
local path = fs.resolve("/etc/ksysinit/"..hook.."/"..hooks[i])
|
|
local stat = fs.stat(path)
|
|
if (stat.mode == "file" and stat.uid == 0 and stat.gid == 0 and mode & 2 == 0) then
|
|
local f, err = loadfile(path)
|
|
if not f then
|
|
kernel.dmesg(kernel.loglevel.WARNING, "Failed to load startup hook `"..hooks[i].."': "..err)
|
|
goto continue
|
|
end
|
|
xpcall(function()
|
|
f(table.unpack(args))
|
|
end, function(err)
|
|
kernel.dmesg(kernel.loglevel.ERROR, "Error in startup hook `"..hooks[i].."': "..debug.traceback(err))
|
|
end)
|
|
end
|
|
::continue::
|
|
end
|
|
end
|
|
|
|
kernel.dmesg(kernel.loglevel.INFO, "running startup hooks")
|
|
run_hooks("startup", {})
|
|
|
|
kernel.dmesg(kernel.loglevel.INFO, "starting ttys")
|
|
for i=1, tonumber(kargs["tty.count"]) or 1 do
|
|
local pty = tty.get(i)
|
|
exec.loadfile("/sbin/login.velx", true, {
|
|
uid = 0,
|
|
gid = 0,
|
|
tty = pty
|
|
})
|
|
end
|
|
|
|
-- Now we wait for shutdown or other things
|
|
while true do
|
|
local siginfo = kernel.sig_pull()
|
|
if siginfo.type == "shutdown" then
|
|
run_hooks("shutdown", {})
|
|
kernel.kmode_run([[computer.shutdown()]])
|
|
elseif siginfo.type == "reboot" then
|
|
run_hooks("shutdown", {})
|
|
kernel.kmode_run([[computer.shutdown(true)]])
|
|
elseif siginfo.type == "ipc" and siginfo.message[1] == "svcrun" and security.hasperm("svc", siginfo.uid, siginfo.gid) then
|
|
table.pack(table.unpack(siginfo.message, 4))
|
|
run_hooks("services/"..siginfo.message[2].."/"..siginfo.message[3], table.pack(table.unpack(siginfo.message, 4)))
|
|
end
|
|
end |