forked from Hyperion/HyperionOS
super dupper system update (it runs)
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
local args={...}
|
||||
local kernel=args[1]
|
||||
|
||||
function string.hasSuffix(str, suffix)
|
||||
return string.sub(str, #suffix+1) == suffix
|
||||
end
|
||||
@@ -117,7 +116,7 @@ function table.proxy(tbl)
|
||||
end
|
||||
end,
|
||||
__newindex = function()
|
||||
error("Attempt to modify read-only table", 2)
|
||||
error("Attempt to modify table proxy", 2)
|
||||
end,
|
||||
__pairs = function()
|
||||
return function(_, k)
|
||||
@@ -158,6 +157,8 @@ local function serialize(table)
|
||||
local coma=true
|
||||
if type(i) == "string" then
|
||||
output=output.."[\""..i.."\"]="
|
||||
elseif type(i) == "number" then
|
||||
output=output.."["..tostring(i).."]="
|
||||
end
|
||||
if type(v) == "table" then
|
||||
if v == table then
|
||||
@@ -224,6 +225,46 @@ function getmetatable(object)
|
||||
end
|
||||
end
|
||||
|
||||
function isEqual(a, ...)
|
||||
local args={...}
|
||||
for i=0, #args do
|
||||
if a==args[i] then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
table.serialize=serialize
|
||||
kernel.log("Loaded stdlib")
|
||||
function table.keys(t)
|
||||
local a = {}
|
||||
for n in pairs(t) do table.insert(a, n) end
|
||||
return a
|
||||
end
|
||||
|
||||
function table.values(t)
|
||||
local a = {}
|
||||
for _, n in pairs(t) do table.insert(a, n) end
|
||||
return a
|
||||
end
|
||||
|
||||
function table.indexOf(t, value)
|
||||
for i,v in ipairs(t) do
|
||||
if v==value then
|
||||
return i
|
||||
end
|
||||
end
|
||||
return -1
|
||||
end
|
||||
|
||||
syscall = setmetatable({}, {__index = function(self, name)
|
||||
return function(...)
|
||||
local res = table.pack(coroutine.yield("syscall", name, ...))
|
||||
if res[1] then
|
||||
return table.unpack(res, 2, res.n)
|
||||
else
|
||||
error(res[2], 2)
|
||||
end
|
||||
end
|
||||
end})
|
||||
|
||||
table.serialize=serialize
|
||||
@@ -1,3 +0,0 @@
|
||||
local args={...}
|
||||
local kernel=args[1]
|
||||
kernel._U=table.proxy(_G)
|
||||
@@ -1,351 +1,277 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
local fs = {}
|
||||
|
||||
-- =====================================================================
|
||||
-- INTERNAL STATE
|
||||
-- =====================================================================
|
||||
|
||||
local disks = {} -- address → disk object
|
||||
local mounts = {["/"] = "$"} -- mountpoint → disk address (root = boot disk)
|
||||
local cwd = "/"
|
||||
|
||||
local SYMLINK_PREFIX = "#!@SYMLINK["
|
||||
local SYMLINK_SUFFIX = "]"
|
||||
local SYMLINK_MAX_DEPTH = 64
|
||||
|
||||
|
||||
-- =====================================================================
|
||||
-- PATH NORMALIZATION
|
||||
-- =====================================================================
|
||||
|
||||
local function splitPath(p)
|
||||
local t = {}
|
||||
for part in p:gmatch("[^/]+") do t[#t+1] = part end
|
||||
return t
|
||||
end
|
||||
local kernel = ...
|
||||
local vfs = {}
|
||||
vfs.mounts = { ["$"] = "/" }
|
||||
local disks = kernel.disks
|
||||
|
||||
-- Path handling
|
||||
local function normalizePath(path)
|
||||
if not path or path == "" then return "/" end
|
||||
|
||||
-- cwd
|
||||
if path:sub(1,1) ~= "/" then
|
||||
path=cwd..path
|
||||
end
|
||||
|
||||
|
||||
local parts = splitPath(path)
|
||||
local out = {}
|
||||
|
||||
for _,part in ipairs(parts) do
|
||||
local parts = {}
|
||||
for part in path:gmatch("[^/]+") do
|
||||
if part == ".." then
|
||||
if #out > 0 then table.remove(out) end
|
||||
if #parts > 0 then table.remove(parts) end
|
||||
elseif part ~= "." and part ~= "" then
|
||||
out[#out+1] = part
|
||||
table.insert(parts, part)
|
||||
end
|
||||
end
|
||||
|
||||
return "/" .. table.concat(out, "/")
|
||||
return "/" .. table.concat(parts, "/") .. (path:sub(-1) == "/" and "/" or "")
|
||||
end
|
||||
|
||||
local function resolvePath(path)
|
||||
local task = kernel.currentTask
|
||||
local cwd = task.cwd or "/"
|
||||
|
||||
-- =====================================================================
|
||||
-- DISK & MOUNT RESOLUTION
|
||||
-- =====================================================================
|
||||
if path:sub(1,1) ~= "/" then
|
||||
path = cwd .. path
|
||||
end
|
||||
|
||||
-- Finds which disk owns an absolute path
|
||||
local function resolveMount(abs)
|
||||
local best = "/"
|
||||
path = normalizePath(path)
|
||||
|
||||
for mount, addr in pairs(mounts) do
|
||||
if abs:sub(1, #mount) == mount then
|
||||
if #mount > #best then
|
||||
best = mount
|
||||
end
|
||||
local mountPoint = "/"
|
||||
local mountId = "$"
|
||||
for k,v in pairs(vfs.mounts) do
|
||||
if path:sub(1,#v) == v and #v > #mountPoint then
|
||||
mountPoint = v
|
||||
mountId = k
|
||||
end
|
||||
end
|
||||
|
||||
local disk = disks[mounts[best]]
|
||||
local diskPath = path:sub(#mountPoint + 1)
|
||||
return disks[mountId], diskPath
|
||||
end
|
||||
|
||||
-- File object creation
|
||||
local function newFileObject(disk, handle, mode, path)
|
||||
return {
|
||||
disk = disk,
|
||||
handle = handle,
|
||||
mode = mode,
|
||||
path = path,
|
||||
refcount = 1
|
||||
}
|
||||
end
|
||||
|
||||
local function allocFD(task)
|
||||
local fd = 0
|
||||
while task.fd[fd] do fd = fd + 1 end
|
||||
return fd
|
||||
end
|
||||
|
||||
local function allocFD(task)
|
||||
-- enforce per-task limit
|
||||
local count = 0
|
||||
for _ in pairs(task.fd) do count = count + 1 end
|
||||
if count >= kernel.config.maxFilesPerTask then
|
||||
error("EMFILE") -- Too many open files in this task
|
||||
end
|
||||
|
||||
-- find first free FD
|
||||
local fd = 0
|
||||
while task.fd[fd] do fd = fd + 1 end
|
||||
return fd
|
||||
end
|
||||
|
||||
local function checkSystemLimit()
|
||||
-- enforce system-wide limit
|
||||
local total = 0
|
||||
for _, task in pairs(kernel.tasks or {}) do
|
||||
for _ in pairs(task.fd) do total = total + 1 end
|
||||
end
|
||||
if total >= kernel.config.maxOpenFiles then
|
||||
error("ENFILE") -- Too many open files in the system
|
||||
end
|
||||
end
|
||||
|
||||
-- VFS syscalls
|
||||
function vfs.open(path, mode)
|
||||
local task = kernel.currentTask
|
||||
|
||||
-- check limits
|
||||
checkSystemLimit()
|
||||
|
||||
local disk, diskPath = resolvePath(path)
|
||||
if not disk then
|
||||
error("No disk registered for mount: " .. best)
|
||||
error("No disk mounted for path '"..path.."'")
|
||||
end
|
||||
|
||||
local sub = abs:sub(#best + 1)
|
||||
if sub == "" then sub = "/" end
|
||||
return disk, sub
|
||||
local handle = disk:open(diskPath, mode)
|
||||
if not handle then return nil end
|
||||
|
||||
local file = newFileObject(disk.address, handle, mode, path)
|
||||
local fd = allocFD(task)
|
||||
task.fd[fd] = file
|
||||
|
||||
return fd
|
||||
end
|
||||
|
||||
function vfs.close(fd)
|
||||
local task = kernel.currentTask
|
||||
local file = task.fd[fd]
|
||||
if not file then error("EBADF") end
|
||||
|
||||
|
||||
-- =====================================================================
|
||||
-- SYMLINK HANDLING
|
||||
-- =====================================================================
|
||||
|
||||
local function isSymlinkRaw(disk, p)
|
||||
if not disk:fileExists(p) then return false end
|
||||
local text = disk:readAllText(p)
|
||||
return text and text:sub(1, #SYMLINK_PREFIX) == SYMLINK_PREFIX
|
||||
end
|
||||
|
||||
local function readSymlinkRaw(disk, p)
|
||||
local text = disk:readAllText(p)
|
||||
if not text then return nil end
|
||||
if text:sub(1, #SYMLINK_PREFIX) ~= SYMLINK_PREFIX then return nil end
|
||||
|
||||
local t = text:sub(#SYMLINK_PREFIX + 1)
|
||||
if t:sub(-1) == SYMLINK_SUFFIX then
|
||||
t = t:sub(1, -2)
|
||||
task.fd[fd] = nil
|
||||
file.refcount = file.refcount - 1
|
||||
if file.refcount == 0 then
|
||||
file.handle.close()
|
||||
end
|
||||
return t
|
||||
return true
|
||||
end
|
||||
|
||||
function vfs.read(fd, count)
|
||||
local file = kernel.currentTask.fd[fd]
|
||||
if not file then error("EBADF") end
|
||||
if not file.mode:find("r") then error("File not open for reading") end
|
||||
return file.handle.read(count)
|
||||
end
|
||||
|
||||
-- =====================================================================
|
||||
-- FULL PATH RESOLUTION (FOLLOWS SYMLINKS)
|
||||
-- =====================================================================
|
||||
function vfs.write(fd, data)
|
||||
local file = kernel.currentTask.fd[fd]
|
||||
if not file then error("EBADF") end
|
||||
if not file.mode:find("w") then error("File not open for writing") end
|
||||
return file.handle.write(data)
|
||||
end
|
||||
|
||||
local function resolveSymlink(path)
|
||||
local abs = normalizePath(path)
|
||||
local parts = splitPath(abs)
|
||||
local out = {}
|
||||
function vfs.whereis(fd)
|
||||
local file = kernel.currentTask.fd[fd]
|
||||
if not file then error("EBADF") end
|
||||
return file.path
|
||||
end
|
||||
|
||||
local depth = 0
|
||||
local idx = 1
|
||||
-- Filesystem operations
|
||||
function vfs.mkdir(path)
|
||||
local disk, diskPath = resolvePath(path)
|
||||
if not disk then error("No disk mounted") end
|
||||
return disk:makeDirectory(diskPath)
|
||||
end
|
||||
|
||||
while idx <= #parts do
|
||||
local comp = parts[idx]
|
||||
function vfs.remove(path)
|
||||
local disk, diskPath = resolvePath(path)
|
||||
if not disk then error("No disk mounted") end
|
||||
return disk:remove(diskPath)
|
||||
end
|
||||
|
||||
if comp == "." then
|
||||
-- nothing
|
||||
elseif comp == ".." then
|
||||
if #out > 0 then table.remove(out) end
|
||||
else
|
||||
local curAbs = "/" .. table.concat(out, "/")
|
||||
if curAbs ~= "/" then curAbs = curAbs .. "/" end
|
||||
curAbs = curAbs .. comp
|
||||
|
||||
local disk, dpath = resolveMount(curAbs)
|
||||
|
||||
if isSymlinkRaw(disk, dpath) then
|
||||
depth = depth + 1
|
||||
if depth > SYMLINK_MAX_DEPTH then
|
||||
error("Too many symlink levels: " .. path)
|
||||
end
|
||||
|
||||
local target = readSymlinkRaw(disk, dpath)
|
||||
if target then
|
||||
local newAbs
|
||||
|
||||
if target:sub(1,1) == "/" then
|
||||
-- absolute target
|
||||
newAbs = normalizePath(target)
|
||||
else
|
||||
-- relative to current out[]
|
||||
local tmp = "/" .. table.concat(out, "/")
|
||||
if tmp ~= "/" then tmp = tmp .. "/" end
|
||||
tmp = tmp .. target
|
||||
newAbs = normalizePath(tmp)
|
||||
end
|
||||
|
||||
-- rebuild remaining parts
|
||||
local remaining = {}
|
||||
for j = idx + 1, #parts do
|
||||
remaining[#remaining+1] = parts[j]
|
||||
end
|
||||
|
||||
-- restart symlink resolution with new path
|
||||
abs = newAbs
|
||||
parts = splitPath(abs)
|
||||
|
||||
-- append remaining
|
||||
for _,x in ipairs(remaining) do parts[#parts+1] = x end
|
||||
|
||||
out = {}
|
||||
idx = 0
|
||||
else
|
||||
out[#out+1] = comp
|
||||
end
|
||||
else
|
||||
out[#out+1] = comp
|
||||
end
|
||||
end
|
||||
|
||||
idx = idx + 1
|
||||
function vfs.attributes(path)
|
||||
if type(path) == "number" then
|
||||
local file = kernel.currentTask.fd[path]
|
||||
if not file then error("EBADF") end
|
||||
return disks[file.disk]:attributes(file.path)
|
||||
end
|
||||
|
||||
local finalAbs = "/" .. table.concat(out, "/")
|
||||
return resolveMount(finalAbs)
|
||||
local disk, diskPath = resolvePath(path)
|
||||
if not disk then error("No disk mounted") end
|
||||
return disk:attributes(diskPath)
|
||||
end
|
||||
|
||||
function vfs.list(path)
|
||||
local disk, diskPath = resolvePath(path)
|
||||
if not disk then error("No disk mounted") end
|
||||
return disk:list(diskPath)
|
||||
end
|
||||
|
||||
function vfs.exists(path)
|
||||
local disk, diskPath = resolvePath(path)
|
||||
if not disk then return false end
|
||||
return disk:directoryExists(diskPath) or disk:fileExists(diskPath)
|
||||
end
|
||||
|
||||
-- =====================================================================
|
||||
-- PUBLIC API
|
||||
-- =====================================================================
|
||||
|
||||
-- MOUNT OPERATIONS -----------------------------------------------------
|
||||
|
||||
function fs.virtDisk(diskObj)
|
||||
if kernel.uid ~= 0 then error("Permission Denied") end
|
||||
if disks[diskObj.address] then
|
||||
error("Disk exists: " .. diskObj.address)
|
||||
function vfs.type(path)
|
||||
if type(path) == "number" then
|
||||
local file = kernel.currentTask.fd[path]
|
||||
if not file then error("EBADF") end
|
||||
return disks[file.disk]:type(file.path)
|
||||
end
|
||||
disks[diskObj.address] = diskObj
|
||||
local disk, diskPath = resolvePath(path)
|
||||
if not disk then error("No disk mounted") end
|
||||
return disk:type(diskPath)
|
||||
end
|
||||
|
||||
function fs.mount(disk, mountPoint)
|
||||
if kernel.uid ~= 0 then error("Permission Denied") end
|
||||
mountPoint = normalizePath(mountPoint)
|
||||
|
||||
local drive, path = resolveMount(normalizePath(mountPoint))
|
||||
if not drive:directoryExists(path) then error("Must mount on folder") end
|
||||
-- CWD
|
||||
function vfs.getcwd()
|
||||
return kernel.currentTask.cwd
|
||||
end
|
||||
|
||||
if mountPoint ~= "/" and mounts[mountPoint] then
|
||||
error("Already mounted: " .. mountPoint)
|
||||
function vfs.setcwd(path)
|
||||
if path:sub(-1) ~= "/" then path = path .. "/" end
|
||||
if path:sub(1,1) ~= "/" then path = "/" .. path end
|
||||
kernel.currentTask.cwd = normalizePath(path)
|
||||
end
|
||||
|
||||
-- Mounting
|
||||
function vfs.mount(diskId, path)
|
||||
if kernel.uid ~= 0 then error("Permission denied") end
|
||||
if not disks[diskId] then error("Unknown disk '"..diskId.."'") end
|
||||
if path:sub(-1) ~= "/" then path = path .. "/" end
|
||||
|
||||
for _,v in pairs(vfs.mounts) do
|
||||
if v == path then error("Mount point already used") end
|
||||
end
|
||||
|
||||
mounts[mountPoint] = disk
|
||||
vfs.mounts[diskId] = path
|
||||
end
|
||||
|
||||
function fs.unmount(mountPoint)
|
||||
if kernel.uid ~= 0 then error("Permission Denied") end
|
||||
mountPoint = normalizePath(mountPoint)
|
||||
|
||||
if mountPoint == "/" then error("Cannot unmount root") end
|
||||
mounts[mountPoint] = nil
|
||||
end
|
||||
|
||||
function fs.eject(addr)
|
||||
if kernel.uid ~= 0 then error("Permission Denied") end
|
||||
disks[addr] = nil
|
||||
end
|
||||
|
||||
function fs.isMount(path)
|
||||
if mounts[normalizePath(path)] then return true, mounts[normalizePath(path)] end
|
||||
end
|
||||
|
||||
-- SYMLINK API ----------------------------------------------------------
|
||||
|
||||
function fs.symlink(target, linkPath)
|
||||
kernel.log("WARNING: Symlinks are a untested feature if you find any bugs please report them to https://git.astronand.dev/Hyperion/HyperionOS","WARN")
|
||||
local disk, p = resolveMount(normalizePath(linkPath))
|
||||
return disk:writeAllText(p, SYMLINK_PREFIX .. target .. SYMLINK_SUFFIX)
|
||||
end
|
||||
|
||||
function fs.isLink(path)
|
||||
local disk, p = resolveMount(normalizePath(path))
|
||||
return isSymlinkRaw(disk, p)
|
||||
end
|
||||
|
||||
function fs.readLink(path)
|
||||
local disk, p = resolveMount(normalizePath(path))
|
||||
return readSymlinkRaw(disk, p)
|
||||
end
|
||||
|
||||
|
||||
-- FILE OPERATIONS ------------------------------------------------------
|
||||
|
||||
function fs.exists(path, ...)
|
||||
local disk, p = resolveSymlink(path)
|
||||
return disk:fileExists(p, ...) or disk:directoryExists(p, ...)
|
||||
end
|
||||
|
||||
function fs.isFile(path, ...)
|
||||
local disk, p = resolveSymlink(path)
|
||||
return disk:fileExists(p, ...)
|
||||
end
|
||||
|
||||
function fs.isDir(path, ...)
|
||||
local disk, p = resolveSymlink(path)
|
||||
return disk:directoryExists(p, ...)
|
||||
end
|
||||
|
||||
function fs.list(path, ...)
|
||||
local disk, p = resolveSymlink(path)
|
||||
return disk:list(p, ...)
|
||||
end
|
||||
|
||||
function fs.makeDir(path, ...)
|
||||
local disk, p = resolveSymlink(path)
|
||||
return disk:makeDirectory(p, ...)
|
||||
end
|
||||
|
||||
-- remove does NOT follow symlinks (UNIX semantics)
|
||||
function fs.remove(path, ...)
|
||||
if fs.isMount(path) then return "Cannot delete mounted folder" end
|
||||
local abs = normalizePath(path)
|
||||
local disk, p = resolveMount(abs)
|
||||
return disk:remove(p, ...)
|
||||
end
|
||||
|
||||
function fs.readAllText(path, ...)
|
||||
local disk, p = resolveSymlink(path)
|
||||
return disk:readAllText(p, ...)
|
||||
end
|
||||
|
||||
function fs.writeAllText(path, text, ...)
|
||||
local disk, p = resolveSymlink(path)
|
||||
return disk:writeAllText(p, text, ...)
|
||||
end
|
||||
|
||||
function fs.appendAllText(path, text, ...)
|
||||
local disk, p = resolveSymlink(path)
|
||||
return disk:appendAllText(p, text, ...)
|
||||
end
|
||||
|
||||
function fs.load(path, name, ...)
|
||||
return load(fs.readAllText(path, ...), name or path, nil, kernel._U)
|
||||
end
|
||||
|
||||
function fs.getSize(path, ...)
|
||||
local disk, p = resolveSymlink(path)
|
||||
return disk:getSize(p, ...)
|
||||
end
|
||||
|
||||
-- =====================================================================
|
||||
-- WD STUFF
|
||||
-- =====================================================================
|
||||
|
||||
function fs.setCwd(path)
|
||||
if not path or path == "" then return "/" end
|
||||
|
||||
-- ensure absolute
|
||||
if path:sub(1,1) ~= "/" then
|
||||
path = "/" .. path
|
||||
end
|
||||
|
||||
local parts = splitPath(path)
|
||||
local out = {}
|
||||
|
||||
for _,part in ipairs(parts) do
|
||||
if part == ".." then
|
||||
if #out > 0 then table.remove(out) end
|
||||
elseif part ~= "." and part ~= "" then
|
||||
out[#out+1] = part
|
||||
function vfs.unmount(path)
|
||||
if kernel.uid ~= 0 then error("Permission denied") end
|
||||
for k,v in pairs(vfs.mounts) do
|
||||
if v == path then
|
||||
vfs.mounts[k] = nil
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
cwd="/" .. table.concat(out, "/")
|
||||
return false
|
||||
end
|
||||
|
||||
function fs.getCwd(path)
|
||||
return cwd
|
||||
function vfs.getMounts()
|
||||
return vfs.mounts
|
||||
end
|
||||
|
||||
-- =====================================================================
|
||||
-- INIT
|
||||
-- =====================================================================
|
||||
function vfs.virtdisk(obj)
|
||||
kernel.disks[obj.address] = obj
|
||||
kernel.log("Registered virtual disk at "..obj.address)
|
||||
end
|
||||
|
||||
kernel.log("Loading disks for vfs")
|
||||
local ok,err = xpcall(function()
|
||||
for _,v in kernel.initdisks.list() do
|
||||
fs.virtDisk(v)
|
||||
-- Redirect file operations to VFS
|
||||
function vfs.dup(oldfd)
|
||||
local task = kernel.currentTask
|
||||
local file = task.fd[oldfd]
|
||||
if not file then error("EBADF") end
|
||||
|
||||
local newfd = allocFD(task)
|
||||
task.fd[newfd] = file
|
||||
file.refcount = file.refcount + 1
|
||||
return newfd
|
||||
end
|
||||
|
||||
function vfs.dup2(oldfd, newfd)
|
||||
local task = kernel.currentTask
|
||||
local file = task.fd[oldfd]
|
||||
if not file then error("EBADF") end
|
||||
|
||||
if oldfd == newfd then return newfd end
|
||||
|
||||
if task.fd[newfd] then
|
||||
vfs.close(newfd)
|
||||
end
|
||||
end, debug.traceback)
|
||||
if not ok then kernel.panic(err) end
|
||||
|
||||
kernel.disks=disks
|
||||
kernel.mounts=mounts
|
||||
kernel.fs = fs
|
||||
kernel.cache.preload.fs = fs
|
||||
kernel.cache.preload.filesystem = kernel.cache.preload.fs
|
||||
task.fd[newfd] = file
|
||||
file.refcount = file.refcount + 1
|
||||
return newfd
|
||||
end
|
||||
|
||||
-- Syscall registration
|
||||
kernel.vfs = vfs
|
||||
kernel.syscalls["VFS_open"] = vfs.open
|
||||
kernel.syscalls["VFS_read"] = vfs.read
|
||||
kernel.syscalls["VFS_write"] = vfs.write
|
||||
kernel.syscalls["VFS_close"] = vfs.close
|
||||
kernel.syscalls["VFS_list"] = vfs.list
|
||||
kernel.syscalls["VFS_type"] = vfs.type
|
||||
kernel.syscalls["VFS_attributes"] = vfs.attributes
|
||||
kernel.syscalls["VFS_mkdir"] = vfs.mkdir
|
||||
kernel.syscalls["VFS_remove"] = vfs.remove
|
||||
kernel.syscalls["VFS_exists"] = vfs.exists
|
||||
kernel.syscalls["VFS_mount"] = vfs.mount
|
||||
kernel.syscalls["VFS_unmount"] = vfs.unmount
|
||||
kernel.syscalls["VFS_getcwd"] = vfs.getcwd
|
||||
kernel.syscalls["VFS_setcwd"] = vfs.setcwd
|
||||
kernel.syscalls["VFS_whereis"] = vfs.whereis
|
||||
kernel.syscalls["VFS_dup"] = vfs.dup
|
||||
kernel.syscalls["VFS_dup2"] = vfs.dup2
|
||||
|
||||
kernel.log("VFS module loaded")
|
||||
|
||||
@@ -1,179 +0,0 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
function kernel.fs.mkvirtfs(address, readOnly, label, autoRegister)
|
||||
if kernel.uid ~= 0 then error("Permission Denied") end
|
||||
local disk = {}
|
||||
disk.address = address
|
||||
local isRO = readOnly or false
|
||||
|
||||
local backend = {
|
||||
["/"] = { __dir = true }
|
||||
}
|
||||
|
||||
-----------------------------------------------------------------
|
||||
-- Helpers
|
||||
-----------------------------------------------------------------
|
||||
|
||||
local function norm(path)
|
||||
if not path or path == "" then return "/" end
|
||||
if path:sub(1,1) ~= "/" then path = "/" .. path end
|
||||
path = path:gsub("/+", "/")
|
||||
return path
|
||||
end
|
||||
|
||||
local function split(path)
|
||||
local parts = {}
|
||||
for p in path:gmatch("[^/]+") do table.insert(parts, p) end
|
||||
return parts
|
||||
end
|
||||
|
||||
local function getNode(path)
|
||||
path = norm(path)
|
||||
if path == "/" then return backend["/"] end
|
||||
local node = backend["/"]
|
||||
for _, part in ipairs(split(path)) do
|
||||
node = node[part]
|
||||
if not node then return nil end
|
||||
end
|
||||
return node
|
||||
end
|
||||
|
||||
local function getParent(path)
|
||||
local parts = split(norm(path))
|
||||
local name = table.remove(parts)
|
||||
local parentPath = "/" .. table.concat(parts, "/")
|
||||
return getNode(parentPath), name
|
||||
end
|
||||
|
||||
local function ensureWrite()
|
||||
if isRO then return false, "read-only" end
|
||||
return true
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------
|
||||
-- File IO (supports string or file-object with read/write funcs)
|
||||
-----------------------------------------------------------------
|
||||
|
||||
local function fileRead(node, ...)
|
||||
if type(node) == "string" then
|
||||
return node
|
||||
elseif type(node) == "table" and node.__file and node.read then
|
||||
return node.read(...)
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function fileWrite(parent, name, text, ...)
|
||||
local node = parent[name]
|
||||
if type(node) == "string" then
|
||||
local ok, err = ensureWrite(); if not ok then return false, err end
|
||||
parent[name] = text
|
||||
return true
|
||||
elseif type(node) == "table" and node.__file and node.write then
|
||||
return node.write(text, ...)
|
||||
end
|
||||
parent[name] = text
|
||||
return true
|
||||
end
|
||||
|
||||
local function fileAppend(parent, name, text, ...)
|
||||
local node = parent[name]
|
||||
if type(node) == "string" then
|
||||
local ok, err = ensureWrite(); if not ok then return false, err end
|
||||
parent[name] = node .. text
|
||||
return true
|
||||
elseif type(node) == "table" and node.__file and node.write then
|
||||
return node.write((node.read() or "") .. text, ...)
|
||||
end
|
||||
parent[name] = text
|
||||
return true
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------
|
||||
-- API
|
||||
-----------------------------------------------------------------
|
||||
|
||||
function disk:isReadOnly() return isRO end
|
||||
function disk:spaceUsed() return 0 end
|
||||
function disk:spaceTotal() return 0 end
|
||||
|
||||
function disk:readAllText(path, ...)
|
||||
local node = getNode(path)
|
||||
if not node then return nil, "file not found" end
|
||||
return fileRead(node, ...)
|
||||
end
|
||||
|
||||
function disk:writeAllText(path, text, ...)
|
||||
local parent, name = getParent(path)
|
||||
if not parent or not parent.__dir then return false, "invalid directory" end
|
||||
return fileWrite(parent, name, tostring(text), ...)
|
||||
end
|
||||
|
||||
function disk:appendAllText(path, text, ...)
|
||||
local parent, name = getParent(path)
|
||||
if not parent or not parent.__dir then return false, "invalid directory" end
|
||||
return fileAppend(parent, name, tostring(text), ...)
|
||||
end
|
||||
|
||||
function disk:list(path)
|
||||
local node = getNode(path)
|
||||
if not node or not node.__dir then return nil, "not a directory" end
|
||||
local out = {}
|
||||
for k, _ in pairs(node) do if k ~= "__dir" then table.insert(out, k) end end
|
||||
return out
|
||||
end
|
||||
|
||||
function disk:fileExists(path)
|
||||
local node = getNode(path)
|
||||
return type(node) == "string" or (type(node) == "table" and node.__file)
|
||||
end
|
||||
|
||||
function disk:directoryExists(path)
|
||||
local node = getNode(path)
|
||||
return node and type(node) == "table" and node.__dir or false
|
||||
end
|
||||
|
||||
function disk:makeDirectory(path)
|
||||
local ok, err = ensureWrite(); if not ok then return false, err end
|
||||
local parent, name = getParent(path)
|
||||
if not parent or not parent.__dir then return false, "invalid parent directory" end
|
||||
parent[name] = { __dir = true }
|
||||
return true
|
||||
end
|
||||
|
||||
function disk:remove(path)
|
||||
local ok, err = ensureWrite(); if not ok then return false, err end
|
||||
local parent, name = getParent(path)
|
||||
if not parent or not parent[name] then return false, "not found" end
|
||||
parent[name] = nil
|
||||
return true
|
||||
end
|
||||
|
||||
function disk:setLabel(new)
|
||||
local ok, err = ensureWrite(); if not ok then return false, err end
|
||||
label = tostring(new)
|
||||
end
|
||||
|
||||
function disk:getLabel()
|
||||
return label
|
||||
end
|
||||
|
||||
function disk:size(path, ...)
|
||||
local node = getNode(path)
|
||||
if type(node) == "string" then return #node end
|
||||
if type(node) == "table" and node.__file and node.read then
|
||||
local v = node.read(...)
|
||||
return v and #v or 0
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-----------------------------------------------------------------
|
||||
-- Auto-register with kernel and return backend
|
||||
-----------------------------------------------------------------
|
||||
if autoRegister then
|
||||
kernel.fs.virtDisk(disk)
|
||||
end
|
||||
|
||||
return backend, disk
|
||||
end
|
||||
@@ -1,56 +1,42 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
-- List of search paths
|
||||
local paths = {
|
||||
local kernel = ...
|
||||
local cache = {}
|
||||
local searchpaths = {
|
||||
"/lib/?.lua",
|
||||
"/lib/?",
|
||||
"/usr/lib/?.lua",
|
||||
"/usr/lib/?",
|
||||
"/usr/local/lib/?"
|
||||
"/usr/local/lib/?.lua",
|
||||
"/usr/local/lib/?",
|
||||
"?.lua",
|
||||
"?"
|
||||
}
|
||||
|
||||
-- Custom require implementation
|
||||
function require(module)
|
||||
-- Return cached module if it already exists
|
||||
if kernel.cache.preload[module]~=nil then
|
||||
return kernel.cache.preload[module]
|
||||
function require(module,...)
|
||||
if cache[module] then
|
||||
return cache[module]
|
||||
end
|
||||
|
||||
local err = {}
|
||||
|
||||
for _, path in ipairs(paths) do
|
||||
-- Replace "?" with module name
|
||||
local filePath = string.replace(path, "?", module)
|
||||
|
||||
-- Try to open file
|
||||
local file = kernel.fs.isFile(filePath)
|
||||
if file then
|
||||
local content = kernel.fs.readAllText(filePath)
|
||||
|
||||
-- Load the module as Lua code
|
||||
local chunk, loadErr = load(content, filePath)
|
||||
if not chunk then
|
||||
table.insert(err, "Error loading: " .. filePath .. ": " .. loadErr)
|
||||
local modpath = module:gsub("%.", "/")
|
||||
local failed = {}
|
||||
for _, path in ipairs(searchpaths) do
|
||||
local full_path = string.gsub(path, "%?", modpath)
|
||||
if full_path:sub(1,1)~="/" then
|
||||
full_path=kernel.currentTask.cwd..full_path
|
||||
end
|
||||
if kernel.vfs.exists(full_path) then
|
||||
if kernel.vfs.type(full_path)=="directory" then
|
||||
full_path = full_path .. "/init"
|
||||
end
|
||||
if kernel.vfs.exists(full_path) then
|
||||
local handle = kernel.vfs.open(full_path, "r")
|
||||
local file_content = kernel.vfs.read(handle, 1024 * 1024 * 4)
|
||||
kernel.vfs.close(handle)
|
||||
return assert(load(file_content, full_path, "t", kernel._U))(...)
|
||||
else
|
||||
-- Execute the module. If the module returns a value, cache it.
|
||||
local ok, result = xpcall(chunk, debug.traceback)
|
||||
if not ok then
|
||||
table.insert(err, "Error executing: "..filePath..": "..tostring(result))
|
||||
else
|
||||
if result ~= nil then
|
||||
kernel.cache.preload[module] = result
|
||||
return result
|
||||
end
|
||||
|
||||
-- If module doesn't return anything, cache `true` like Lua does
|
||||
kernel.cache.preload[module] = true
|
||||
return true
|
||||
end
|
||||
table.insert(failed, full_path)
|
||||
end
|
||||
else
|
||||
table.insert(err, "Module not found: " .. filePath)
|
||||
table.insert(failed, full_path)
|
||||
end
|
||||
end
|
||||
|
||||
-- If nothing worked, raise an error with all reasons
|
||||
error("Unable to require module '" .. module .. "':\n" .. table.concat(err, "\n"))
|
||||
end
|
||||
kernel.log("Created require")
|
||||
error("Module not found: " .. module .. " (searched paths: " .. table.concat(failed, ", ") .. ")")
|
||||
end
|
||||
@@ -1,57 +1,196 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
local data = kernel.fs.mkvirtfs("devfs0000", true, "devfs", true)
|
||||
if not kernel.fs.isDir("/dev") then kernel.fs.makeDir("/dev") end
|
||||
kernel.devfs={}
|
||||
kernel.devfs.data=data
|
||||
local kernel = ...
|
||||
|
||||
data["/"]["OSVERSION"]="Hyperion 1.0.0"
|
||||
data["/"]["eeprom"]={
|
||||
__file=true,
|
||||
read=function()
|
||||
return kernel.computer:getEEPROM()
|
||||
local proxy = {}
|
||||
local data = {}
|
||||
|
||||
proxy.address = "devfs0000"
|
||||
proxy.isReadOnly = false
|
||||
proxy.spaceUsed = function() return 0 end
|
||||
proxy.spaceTotal = function() return 0 end
|
||||
proxy.makeDirectory = function() error("Permission denied") end
|
||||
proxy.remove = function() error("Permission denied") end
|
||||
proxy.setLabel = function() error("Permission denied") end
|
||||
proxy.getLabel = function() return "devfs" end
|
||||
proxy.attributes = function(path) return {
|
||||
type = proxy.type(path),
|
||||
isReadOnly = false,
|
||||
size = 0,
|
||||
lastModified = 0,
|
||||
created = 0,
|
||||
Permissions = "666",
|
||||
owner = "root",
|
||||
group = "root"
|
||||
} end
|
||||
|
||||
local function getNode(path)
|
||||
local parts = string.split(path, "/")
|
||||
if parts[1] == "" then
|
||||
table.remove(parts, 1)
|
||||
end
|
||||
|
||||
local node = data
|
||||
for _, part in ipairs(parts) do
|
||||
if node[part] then
|
||||
node = node[part]
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
return node
|
||||
end
|
||||
|
||||
proxy.type = function(path)
|
||||
local node = getNode(path)
|
||||
if node then
|
||||
return node.type
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
proxy.list = function(path)
|
||||
local node = getNode(path)
|
||||
if node and node.type == "directory" then
|
||||
local content = table.keys(node)
|
||||
table.remove(content, table.indexOf(content, "type"))
|
||||
return content
|
||||
else
|
||||
error("Not a directory")
|
||||
end
|
||||
end
|
||||
|
||||
proxy.open = function(path, mode)
|
||||
local node = getNode(path)
|
||||
if node and (node.type == "file" or node.type == "character device") then
|
||||
if mode == "r" then
|
||||
return {
|
||||
read = node.read,
|
||||
close = function() end
|
||||
}
|
||||
elseif mode == "w" then
|
||||
return {
|
||||
write = node.write,
|
||||
close = function() end
|
||||
}
|
||||
else
|
||||
error("Invalid mode")
|
||||
end
|
||||
else
|
||||
error("Not a file"..type(node))
|
||||
end
|
||||
end
|
||||
|
||||
local function newStringFile(content)
|
||||
return {
|
||||
type = "file",
|
||||
read = function() return content end,
|
||||
write = function(newContent) content = newContent end
|
||||
}
|
||||
end
|
||||
|
||||
local function newDirectory()
|
||||
return {
|
||||
type = "directory"
|
||||
}
|
||||
end
|
||||
|
||||
data["random"] = {
|
||||
type = "character device",
|
||||
read = function(amount)
|
||||
local result = ""
|
||||
for _ = 1, amount do
|
||||
result = result .. string.char(math.random(0, 255))
|
||||
end
|
||||
return result
|
||||
end,
|
||||
write=function(text)
|
||||
if kernel.uid ~= 0 then error("Permission Denied") end
|
||||
kernel.computer:setEEPROM(text)
|
||||
write = function() error("Permission denied") end
|
||||
}
|
||||
|
||||
data["null"] = {
|
||||
type = "character device",
|
||||
read = function() return "" end,
|
||||
write = function() end
|
||||
}
|
||||
|
||||
data["zero"] = {
|
||||
type = "character device",
|
||||
read = function(amount)
|
||||
return string.rep("\0", amount)
|
||||
end,
|
||||
write = function() error("Permission denied") end
|
||||
}
|
||||
|
||||
data["rtc"] = {
|
||||
type = "character device",
|
||||
read = function() return kernel.computer:time() end,
|
||||
write = function() error("Permission denied") end
|
||||
}
|
||||
|
||||
data["rtc0"] = {
|
||||
type = "character device",
|
||||
read = function() return kernel.computer:time() end,
|
||||
write = function() error("Permission denied") end
|
||||
}
|
||||
|
||||
data["eeprom"] = {
|
||||
type = "character device",
|
||||
read = function() return kernel.computer:getEEPROM() end,
|
||||
write = function(data)
|
||||
if kernel.uid ~= 0 then
|
||||
error("Permission denied")
|
||||
end
|
||||
kernel.computer:setEEPROM(data)
|
||||
end
|
||||
}
|
||||
data["/"]["null"]={
|
||||
__file=true,
|
||||
read=function() end,
|
||||
write=function() end
|
||||
}
|
||||
data["/"]["random"]={
|
||||
__file=true,
|
||||
read=function(amount)
|
||||
local s = ""
|
||||
for i = 1, amount do
|
||||
s = s .. string.char(math.random(0, 255))
|
||||
end
|
||||
return s
|
||||
|
||||
data["stdin"] = {
|
||||
type = "link",
|
||||
read = function(amount)
|
||||
return kernel.currentTask.fd[0].read(amount)
|
||||
end,
|
||||
write=function() end
|
||||
}
|
||||
data["/"]["zero"]={
|
||||
__file=true,
|
||||
read=function(amount)
|
||||
return ("\0"):rep(amount)
|
||||
end,
|
||||
write=function() end
|
||||
}
|
||||
data["/"]["rtc0"]={
|
||||
__file=true,
|
||||
read=function()
|
||||
return kernel.computer:time()
|
||||
end,
|
||||
write=function() end
|
||||
}
|
||||
data["/"]["rtc"]={
|
||||
__file=true,
|
||||
read=function()
|
||||
return kernel.computer:time()
|
||||
end,
|
||||
write=function() end
|
||||
write = function() error("Permission denied") end
|
||||
}
|
||||
|
||||
kernel.log("Created devfs")
|
||||
data["stdout"] = {
|
||||
type = "link",
|
||||
read = function() error("Permission denied") end,
|
||||
write = function(data)
|
||||
kernel.currentTask.fd[1].write(data)
|
||||
end
|
||||
}
|
||||
|
||||
data["stderr"] = {
|
||||
type = "link",
|
||||
read = function() error("Permission denied") end,
|
||||
write = function(data)
|
||||
kernel.currentTask.fd[2] = data
|
||||
end
|
||||
}
|
||||
|
||||
local keyboard = kernel.newFifo()
|
||||
local mouse = kernel.newFifo()
|
||||
data["input"] = newDirectory()
|
||||
data["input"]["keyboard"] = {
|
||||
type = "pipe",
|
||||
read = function(amount)
|
||||
return keyboard.pop()
|
||||
end,
|
||||
write = function() error("Permission denied") end
|
||||
}
|
||||
data["input"]["mouse"] = {
|
||||
type = "pipe",
|
||||
read = function(amount)
|
||||
return mouse.pop()
|
||||
end,
|
||||
write = function() error("Permission denied") end
|
||||
}
|
||||
|
||||
data["pts"] = newDirectory()
|
||||
|
||||
kernel.devfs = {}
|
||||
kernel.devfs.keyboard = keyboard
|
||||
kernel.devfs.mouse = mouse
|
||||
kernel.devfs.proxy = proxy
|
||||
kernel.devfs.data = data
|
||||
kernel.vfs.virtdisk(proxy)
|
||||
@@ -1,18 +0,0 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
local data = kernel.fs.mkvirtfs("sysfs0000", true, "sysfs", true)
|
||||
if not kernel.fs.isDir("/sys") then kernel.fs.makeDir("/sys") end
|
||||
|
||||
data["/"]["OSVERSION"]="Hyperion 1.0.0"
|
||||
data["/"]["eeprom"]={
|
||||
__file=true,
|
||||
read=function()
|
||||
return kernel.computer:getEEPROM()
|
||||
end,
|
||||
write=function(text)
|
||||
if kernel.uid ~= 0 then error("Permission Denied") end
|
||||
kernel.computer:setEEPROM(text)
|
||||
end
|
||||
}
|
||||
|
||||
kernel.log("Created sysfs")
|
||||
12
Build/lib/modules/Hyperion/13_keventd.kmod
Normal file
12
Build/lib/modules/Hyperion/13_keventd.kmod
Normal file
@@ -0,0 +1,12 @@
|
||||
local kernel = ...
|
||||
|
||||
kernel.processes.keventd = function()
|
||||
while true do
|
||||
local event = {kernel.computer:getMachineEvent()}
|
||||
if event[1] then
|
||||
if event[1] == "key" or event[1] == "keyPressed" or event[1] == "keyReleased" then
|
||||
kernel.devfs.keyboard.push(event)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,26 +0,0 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
local ifs=kernel.ifs
|
||||
|
||||
kernel.log("Mounting fstab")
|
||||
local fstab=ifs.readAllText("/etc/fstab")
|
||||
local entrys = string.split(fstab,"\n")
|
||||
for i,v in ipairs(entrys) do
|
||||
if v:sub(1,1)=="U" then
|
||||
local id=""
|
||||
for i=3,#v do
|
||||
if v:sub(i,i)==";" then
|
||||
if i==3 then kernel.log("Invalid fstab line... Skipping.","WARN") goto endline end
|
||||
id=v:sub(3,i-1)
|
||||
end
|
||||
end
|
||||
local path=v:sub(#id+4,#v)
|
||||
if i~=#entrys then
|
||||
path=path:sub(1,#path-1)
|
||||
end
|
||||
kernel.log("Mounted "..id.." to "..path)
|
||||
kernel.fs.mount(id,path)
|
||||
::endline::
|
||||
end
|
||||
end
|
||||
kernel.log("Mounted all disks")
|
||||
17
Build/lib/modules/Hyperion/19_fstab.kmod
Normal file
17
Build/lib/modules/Hyperion/19_fstab.kmod
Normal file
@@ -0,0 +1,17 @@
|
||||
local kernel=...
|
||||
for i,v in ipairs(string.split(kernel.fstab,"\n")) do
|
||||
if v:sub(1,1)=="U" then
|
||||
local id=""
|
||||
for i=3,#v do
|
||||
if v:sub(i,i)==";" then
|
||||
if i==3 then kernel.log("Invalid fstab line... Skipping.","WARN") goto endline end
|
||||
id=v:sub(3,i-1)
|
||||
end
|
||||
end
|
||||
local path=v:sub(#id+4)
|
||||
if id~="$" then
|
||||
kernel.vfs.mount(id,path)
|
||||
end
|
||||
::endline::
|
||||
end
|
||||
end
|
||||
1
Build/lib/modules/Hyperion/20_firmware.kmod
Normal file
1
Build/lib/modules/Hyperion/20_firmware.kmod
Normal file
@@ -0,0 +1 @@
|
||||
local kernel = ...
|
||||
75
Build/lib/modules/Hyperion/20_ipc.kmod
Normal file
75
Build/lib/modules/Hyperion/20_ipc.kmod
Normal file
@@ -0,0 +1,75 @@
|
||||
local kernel = ...
|
||||
local ports = {}
|
||||
local signals = {}
|
||||
local ipc = {}
|
||||
|
||||
function ipc.open(port)
|
||||
if not ports[port] then
|
||||
local handle = kernel.newUUID()
|
||||
ports[port] = {owner = kernel.currentProcess.pid, handle = handle, messages = {}}
|
||||
return ports[port].handle
|
||||
end
|
||||
error("Port already opened")
|
||||
end
|
||||
|
||||
function ipc.close(port)
|
||||
if ports[port] then
|
||||
if ports[port].owner == kernel.currentProcess.pid then
|
||||
ports[port] = nil
|
||||
return true
|
||||
else
|
||||
error("Cannot close port you do not own")
|
||||
end
|
||||
end
|
||||
error("Port not opened")
|
||||
end
|
||||
|
||||
function ipc.send(port, message)
|
||||
if ports[port] then
|
||||
table.insert(ports[port].messages, {from = kernel.currentProcess.pid, message = message})
|
||||
if signals[ports[port].owner] then
|
||||
signals[ports[port].owner](port, message)
|
||||
end
|
||||
return true
|
||||
end
|
||||
error("Port not opened")
|
||||
end
|
||||
|
||||
function ipc.receive(port)
|
||||
if ports[port] then
|
||||
if #ports[port].messages > 0 then
|
||||
return table.remove(ports[port].messages, 1)
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
error("Port not opened")
|
||||
end
|
||||
|
||||
function ipc.setSignalHandler(pid, handler)
|
||||
signals[pid] = handler
|
||||
end
|
||||
|
||||
function ipc.clearSignalHandler(pid)
|
||||
signals[pid] = nil
|
||||
end
|
||||
|
||||
function ipc.sendSignal(pid, ...)
|
||||
coroutine.resumeWithTimeout(coroutine.create(function(...)
|
||||
if signals[pid] then
|
||||
signals[pid](...)
|
||||
return true
|
||||
end
|
||||
end), 100)
|
||||
return false
|
||||
end
|
||||
|
||||
kernel.ipc = ipc
|
||||
kernel.syscalls["ipc_open"] = ipc.open
|
||||
kernel.syscalls["ipc_close"] = ipc.close
|
||||
kernel.syscalls["ipc_send"] = ipc.send
|
||||
kernel.syscalls["ipc_receive"] = ipc.receive
|
||||
kernel.syscalls["ipc_setSignalHandler"] = ipc.setSignalHandler
|
||||
kernel.syscalls["ipc_clearSignalHandler"] = ipc.clearSignalHandler
|
||||
kernel.syscalls["ipc_sendSignal"] = ipc.sendSignal
|
||||
kernel.log("Loaded IPC module")
|
||||
@@ -1,13 +0,0 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
kernel.drivers.processes["keventd"]=function()
|
||||
while true do
|
||||
local event={kernel.computer:getMachineEvent()}
|
||||
while event[1]~=nil do
|
||||
kernel.hpv.sendSignal("all", table.unpack(event))
|
||||
event={kernel.computer:getMachineEvent()}
|
||||
end
|
||||
coroutine.yield()
|
||||
end
|
||||
end
|
||||
kernel.log("Created keventd daemon")
|
||||
@@ -1,15 +0,0 @@
|
||||
local args={...}
|
||||
local kernel=args[1]
|
||||
local io={}
|
||||
local iolib={}
|
||||
|
||||
io.stdout={}
|
||||
io.stdin={}
|
||||
io.stderr={}
|
||||
|
||||
iolib.stdout=io.stdout
|
||||
iolib.stdin =io.stdin
|
||||
iolib.stderr=io.stderr
|
||||
|
||||
function io.register(ttyObj)
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
kernel.log("Loading third party drivers")
|
||||
for _,subf in ipairs(kernel.fs.list("/lib/modules/")) do
|
||||
if kernel.fs.isDir("/lib/modules/"..subf) then
|
||||
if subf~="Hyperion" then
|
||||
for _,driver in ipairs(kernel.fs.list("/lib/modules/"..subf)) do
|
||||
kernel.log("Compiling driver \""..subf..":"..driver.."\"")
|
||||
local code = kernel.fs.readAllText("/lib/modules/"..subf.."/"..driver)
|
||||
local func, err = load(code, "@"..driver)
|
||||
if not func then
|
||||
kernel.log("DriverCompileErr: "..tostring(err), "ERROR")
|
||||
else
|
||||
local ok, err = xpcall(func, debug.traceback, table.unpack(args))
|
||||
if not ok then
|
||||
kernel.log("DriverExecErr: "..tostring(err), "ERROR")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
17
Build/lib/modules/Hyperion/30_userspace.kmod
Normal file
17
Build/lib/modules/Hyperion/30_userspace.kmod
Normal file
@@ -0,0 +1,17 @@
|
||||
local args={...}
|
||||
local kernel=args[1]
|
||||
kernel._G=_G
|
||||
kernel._U=setmetatable({},{
|
||||
__index = kernel._G,
|
||||
__newindex = function(t,k,v)
|
||||
if kernel.config.allowGlobalOverwrites or kernel.allowGlobalOverwrites then
|
||||
rawset(t,k,v)
|
||||
return
|
||||
end
|
||||
error("Attempt to modify global variable '"..k.."'",2)
|
||||
end,
|
||||
__metatable = false
|
||||
})
|
||||
kernel.allowGlobalOverwrites=true
|
||||
kernel._U._G=kernel._U
|
||||
kernel.allowGlobalOverwrites=false
|
||||
@@ -1,14 +0,0 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
kernel.log("initializing third party drivers")
|
||||
for _,v in ipairs(kernel.drivers.raw) do
|
||||
if v.arch==kernel.arch then
|
||||
if v.load then
|
||||
kernel.log("Loading "..v.name)
|
||||
local ok,err = xpcall(v.load, debug.traceback)
|
||||
if not ok then
|
||||
kernel.log("DriverLoadErr: "..tostring(err))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
65
Build/lib/modules/Hyperion/40_pam.kmod
Normal file
65
Build/lib/modules/Hyperion/40_pam.kmod
Normal file
@@ -0,0 +1,65 @@
|
||||
local kernel = ...
|
||||
local pam = {}
|
||||
kernel.pam = pam
|
||||
local loggedIn = {}
|
||||
|
||||
local function getFile(path)
|
||||
local file = kernel.vfs.open(path, "r")
|
||||
if not file then error("Failed to open file: "..path) end
|
||||
local content = kernel.vfs.read(file, 1024000)
|
||||
kernel.vfs.close(file)
|
||||
return content
|
||||
end
|
||||
|
||||
local blake2s = require("crypto.blake2s")
|
||||
if not blake2s then error("Failed to load blake2s") end
|
||||
|
||||
if not kernel.vfs.exists("/etc/pam.d/secret") then
|
||||
local key = ""
|
||||
for i=1, 256 do
|
||||
key=key..string.char(math.random(1,255))
|
||||
end
|
||||
local handle = kernel.vfs.open("/etc/pam.d/secret", "w")
|
||||
kernel.vfs.write(handle, key)
|
||||
kernel.vfs.close(handle)
|
||||
end
|
||||
|
||||
local pepper = getFile("/etc/pam.d/secret")
|
||||
|
||||
function pam.authenticate(username, password)
|
||||
local fpasswd = getFile("/etc/passwd")
|
||||
local fshadow = getFile("/etc/shadow")
|
||||
|
||||
local passwdLines = string.split(fpasswd, "\n")
|
||||
local shadowLines = string.split(fshadow, "\n")
|
||||
|
||||
local passwd = {}
|
||||
local shadow = {}
|
||||
for _, line in ipairs(passwdLines) do
|
||||
local fields = string.split(line, ":")
|
||||
passwd[fields[1]] = fields
|
||||
end
|
||||
for _, line in ipairs(shadowLines) do
|
||||
local fields = string.split(line, ":")
|
||||
shadow[fields[1]] = fields
|
||||
end
|
||||
|
||||
for user, fields in pairs(passwd) do
|
||||
if user == username then
|
||||
local shadowPasswd = string.split(shadow[user][2], "$")
|
||||
local salt = shadowPasswd[2]
|
||||
local hashedPassword = blake2s(password .. salt, pepper)
|
||||
if hashedPassword == shadowPasswd[3] then
|
||||
loggedIn[username] = kernel.newUUID()
|
||||
return loggedIn[username]
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function pam.authToken(username, token)
|
||||
return loggedIn[username] == token
|
||||
end
|
||||
|
||||
211
Build/lib/modules/Hyperion/45_hypervisor.kmod
Normal file
211
Build/lib/modules/Hyperion/45_hypervisor.kmod
Normal file
@@ -0,0 +1,211 @@
|
||||
local kernel = ...
|
||||
local tasks = {}
|
||||
local sys = {}
|
||||
local nextpid = 2
|
||||
kernel.exitMain=false
|
||||
|
||||
function sys.spawn(func, name, envars, args, tgid, fshandles)
|
||||
local id = nextpid
|
||||
nextpid = nextpid + 1
|
||||
tasks[tostring(id)] = {
|
||||
coro=coroutine.create(function()
|
||||
local ok, err = xpcall(func, debug.traceback, table.unpack(args or {}))
|
||||
if not ok then
|
||||
tasks[tostring(id)].status="Z"
|
||||
tasks[tostring(id)].exit=tostring(err)
|
||||
else
|
||||
tasks[tostring(id)].status="Z"
|
||||
tasks[tostring(id)].exit=err
|
||||
end
|
||||
end),
|
||||
name=name or "task"..tostring(id),
|
||||
envars=envars or {},
|
||||
args=args or {},
|
||||
status="R",
|
||||
pid=id,
|
||||
tgid=tgid or kernel.currentTask.tgid,
|
||||
user=kernel.user,
|
||||
uid=kernel.uid,
|
||||
fd=fshandles or {},
|
||||
exit="",
|
||||
sleep=0,
|
||||
ivs=0,
|
||||
vs=0,
|
||||
children={},
|
||||
parent=kernel.currentTask,
|
||||
siblings=kernel.currentTask.children,
|
||||
syscallReturn={},
|
||||
cwd=kernel.currentTask.cwd,
|
||||
term=kernel.currentTask.term,
|
||||
timeSlice=0,
|
||||
lastTime=0,
|
||||
totalTime=0,
|
||||
numRuns=0
|
||||
}
|
||||
table.insert(kernel.currentTask.children, tasks[tostring(id)])
|
||||
end
|
||||
|
||||
function sys.sleep(ms)
|
||||
kernel.currentTask.status="S"
|
||||
kernel.currentTask.sleep=kernel.computer:time()+ms
|
||||
coroutine.yield()
|
||||
end
|
||||
|
||||
kernel.syscalls["HPV_spawn"]=sys.spawn
|
||||
kernel.syscalls["HPV_sleep"]=sys.sleep
|
||||
kernel._G.sleep=function(...)coroutine.yield("HPV_sleep",...)end
|
||||
|
||||
local function reapDeadTasks()
|
||||
for pid, task in pairs(tasks) do
|
||||
if task.status == "Z" and not task.reapTime then
|
||||
task.coro = nil
|
||||
task.ivs = nil
|
||||
task.vs = nil
|
||||
task.args = nil
|
||||
task.envars = nil
|
||||
task.cwd = nil
|
||||
task.term = nil
|
||||
task.numRuns = nil
|
||||
task.totalTime = nil
|
||||
task.lastTime = nil
|
||||
task.timeSlice = nil
|
||||
task.syscallReturn = nil
|
||||
task.sleep = nil
|
||||
for k,v in pairs(task.fd) do
|
||||
kernel.vfs.close(v)
|
||||
end
|
||||
task.fd = nil
|
||||
task.reapTime = kernel.computer:time() + 30000
|
||||
elseif task.reapTime and kernel.computer:time() > task.reapTime then
|
||||
for _,child in ipairs(task.children) do
|
||||
child.parent = tasks["1"]
|
||||
child.siblings = tasks["1"].children
|
||||
table.insert(tasks["1"].children, child)
|
||||
end
|
||||
for i, sibling in ipairs(task.siblings) do
|
||||
if sibling.pid == task.pid then
|
||||
table.remove(task.siblings, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
tasks[pid] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local alpha = 0.85
|
||||
local C_target = 0.01
|
||||
local Tmin = 0.0005
|
||||
local Tmax = 0.5
|
||||
local lambda_budget = 0.08
|
||||
local lambda_clamp = 0.03
|
||||
local lambda_var = 0.02
|
||||
local k_min = 0.5
|
||||
local k_max = 0.5
|
||||
local B = 0.01
|
||||
|
||||
function kernel.main()
|
||||
while not kernel.exitMain do
|
||||
local N = 0
|
||||
local Tmin_hit = 0
|
||||
local Tmax_hit = 0
|
||||
local totalTaskTime = 0
|
||||
local taskTimes = {}
|
||||
|
||||
for pid, task in pairs(tasks) do
|
||||
if task.status == "S" then
|
||||
if kernel.computer:time() >= task.sleep then
|
||||
task.status="R"
|
||||
task.sleep=0
|
||||
end
|
||||
end
|
||||
if task.status == "R" then
|
||||
kernel.currentTask = task
|
||||
kernel.vfs.cwd = task.cwd
|
||||
kernel.user = task.user
|
||||
kernel.uid = task.uid
|
||||
N = N + 1
|
||||
|
||||
-- assign adaptive time slice
|
||||
task.timeSlice = math.min(Tmax, math.max(Tmin, B / (N ^ alpha)))
|
||||
|
||||
-- measure execution time
|
||||
local startTime = kernel.computer:time()
|
||||
local ret
|
||||
if kernel.config.preempt then
|
||||
ret = {coroutine.resumeWithTimeout(task.coro, task.timeSlice, table.unpack(task.syscallReturn))}
|
||||
else
|
||||
ret = {coroutine.resume(task.coro, table.unpack(task.syscallReturn))}
|
||||
end
|
||||
local elapsed = kernel.computer:time() - startTime
|
||||
task.lastTime = elapsed
|
||||
task.totalTime = (task.totalTime or 0) + elapsed
|
||||
task.numRuns = (task.numRuns or 0) + 1
|
||||
|
||||
taskTimes[#taskTimes + 1] = elapsed
|
||||
totalTaskTime = totalTaskTime + elapsed
|
||||
|
||||
if elapsed <= Tmin then Tmin_hit = Tmin_hit + 1 end
|
||||
if elapsed >= Tmax then Tmax_hit = Tmax_hit + 1 end
|
||||
|
||||
-- handle task results
|
||||
if ret[1] == "error" then
|
||||
kernel.log("processHandlerException: "..ret[2])
|
||||
task.status = "Z"
|
||||
task.exit = "processHandlerException: "..ret[2]
|
||||
elseif ret[1] == "timeout" then
|
||||
task.ivs=task.ivs+1
|
||||
task.syscallReturn = {}
|
||||
elseif ret[1] == "success" then
|
||||
task.vs=task.vs+1
|
||||
if ret[2]=="syscall" then
|
||||
if kernel.syscalls[ret[3]] then
|
||||
if kernel.config.debugSyscalls then
|
||||
kernel.log("Task "..task.pid.." invoking syscall: "..ret[3])
|
||||
end
|
||||
local sysret = {xpcall(kernel.syscalls[ret[3]], debug.traceback, table.unpack(ret, 4))}
|
||||
if kernel.config.debugSyscalls then
|
||||
if not sysret[1] then
|
||||
kernel.log("Task "..task.pid.." syscall "..ret[3].." failed: "..tostring(sysret[2]))
|
||||
else
|
||||
kernel.log("Task "..task.pid.." syscall "..ret[3].." completed returning "..tostring(#sysret-1).." values")
|
||||
for i=2,#sysret do
|
||||
kernel.log(" retval["..tostring(i-1).."] = "..tostring(sysret[i]))
|
||||
end
|
||||
end
|
||||
end
|
||||
if not sysret[1] then
|
||||
task.syscallReturn={false, sysret[2]}
|
||||
else
|
||||
task.syscallReturn={true, table.unpack(sysret,2)}
|
||||
end
|
||||
else
|
||||
task.syscallReturn={false, "Unknown syscall: "..tostring(ret[3])}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local T_prev_avg = (N > 0) and (totalTaskTime / N) or 0
|
||||
local T_prev_var = 0
|
||||
for _, t in ipairs(taskTimes) do
|
||||
T_prev_var = T_prev_var + (t - T_prev_avg)^2
|
||||
end
|
||||
if N > 0 then T_prev_var = T_prev_var / N end
|
||||
|
||||
if N > 0 then
|
||||
local f_clamp = k_min*(Tmin_hit/N) - k_max*(Tmax_hit/N)
|
||||
local B_budget = (C_target * (N^(alpha-1))) / math.max(T_prev_avg, 1e-8)
|
||||
B = B + lambda_budget * (B_budget - B)
|
||||
+ lambda_clamp * f_clamp
|
||||
- lambda_var * T_prev_var
|
||||
end
|
||||
|
||||
-- clean up dead tasks
|
||||
reapDeadTasks()
|
||||
end
|
||||
end
|
||||
|
||||
kernel.tasks=tasks
|
||||
kernel.hpv=sys
|
||||
6
Build/lib/modules/Hyperion/47_dbg.kmod
Normal file
6
Build/lib/modules/Hyperion/47_dbg.kmod
Normal file
@@ -0,0 +1,6 @@
|
||||
local kernel=...
|
||||
local debug=debug
|
||||
kernel._G.debug={
|
||||
getinfo=debug.getinfo,
|
||||
traceback=debug.traceback
|
||||
}
|
||||
125
Build/lib/modules/Hyperion/50_tty.kmod
Normal file
125
Build/lib/modules/Hyperion/50_tty.kmod
Normal file
@@ -0,0 +1,125 @@
|
||||
local kernel=...
|
||||
kernel.tty={}
|
||||
|
||||
function kernel.tty.register(tty, ttyo)
|
||||
kernel.tty[tty]=ttyo
|
||||
end
|
||||
|
||||
function kernel.tty.print(text)
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] then
|
||||
kernel.tty[term].print(text)
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.printInline(text)
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] then
|
||||
kernel.tty[term].printInline(text)
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.size()
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] then
|
||||
return kernel.tty[term].size()
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.setCursorPos(x,y)
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] then
|
||||
return kernel.tty[term].setCursorPos(x,y)
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.getCursorPos()
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] then
|
||||
return kernel.tty[term].getCursorPos()
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.clear()
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] then
|
||||
return kernel.tty[term].clear()
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.setTextColor(color)
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] then
|
||||
return kernel.tty[term].setTextColor(color)
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.setBackgroundColor(color)
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] then
|
||||
return kernel.tty[term].setBackgroundColor(color)
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.scroll(n)
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] then
|
||||
return kernel.tty[term].scroll(n)
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.getTextColor()
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] and kernel.tty[term].getTextColor then
|
||||
return kernel.tty[term].getTextColor()
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.getBackgroundColor()
|
||||
local term=kernel.currentTask.term
|
||||
if term and kernel.tty[term] and kernel.tty[term].getBackgroundColor then
|
||||
return kernel.tty[term].getBackgroundColor()
|
||||
end
|
||||
end
|
||||
|
||||
function kernel.tty.bind(ttyid)
|
||||
if not ttyid then
|
||||
return false, "No TTY ID specified"
|
||||
end
|
||||
if not kernel.tty[ttyid] then
|
||||
return false, "TTY "..tostring(ttyid).." not registered"
|
||||
end
|
||||
kernel.currentTask.term=ttyid
|
||||
return true
|
||||
end
|
||||
|
||||
function kernel.tty.unbind()
|
||||
kernel.currentTask.term=false
|
||||
end
|
||||
|
||||
function kernel.tty.isBound()
|
||||
return kernel.currentTask.term ~= nil
|
||||
end
|
||||
|
||||
function kernel.tty.getBoundTTY()
|
||||
return kernel.currentTask.term
|
||||
end
|
||||
|
||||
kernel.syscalls["TTY_print"]=kernel.tty.print
|
||||
kernel.syscalls["TTY_printInline"]=kernel.tty.printInline
|
||||
kernel.syscalls["TTY_size"]=kernel.tty.size
|
||||
kernel.syscalls["TTY_setCursorPos"]=kernel.tty.setCursorPos
|
||||
kernel.syscalls["TTY_getCursorPos"]=kernel.tty.getCursorPos
|
||||
kernel.syscalls["TTY_clear"]=kernel.tty.clear
|
||||
kernel.syscalls["TTY_setTextColor"]=kernel.tty.setTextColor
|
||||
kernel.syscalls["TTY_setBackgroundColor"]=kernel.tty.setBackgroundColor
|
||||
kernel.syscalls["TTY_scroll"]=kernel.tty.scroll
|
||||
kernel.syscalls["TTY_getTextColor"]=kernel.tty.getTextColor
|
||||
kernel.syscalls["TTY_getBackgroundColor"]=kernel.tty.getBackgroundColor
|
||||
kernel.syscalls["TTY_bind"]=kernel.tty.bind
|
||||
kernel.syscalls["TTY_unbind"]=kernel.tty.unbind
|
||||
kernel.syscalls["TTY_isBound"]=kernel.tty.isBound
|
||||
kernel.syscalls["TTY_getBoundTTY"]=kernel.tty.getBoundTTY
|
||||
|
||||
kernel.log("TTY module loaded attempting to register console tty")
|
||||
kernel.status="init"
|
||||
2
Build/lib/modules/Hyperion/55_tty_rebind.kmod
Normal file
2
Build/lib/modules/Hyperion/55_tty_rebind.kmod
Normal file
@@ -0,0 +1,2 @@
|
||||
local kernel=...
|
||||
kernel.tty.bind("tty0")
|
||||
28
Build/lib/modules/Hyperion/70_stdlibadv.kmod
Normal file
28
Build/lib/modules/Hyperion/70_stdlibadv.kmod
Normal file
@@ -0,0 +1,28 @@
|
||||
local kernel=...
|
||||
function print(...)
|
||||
coroutine.yield()
|
||||
local args={...}
|
||||
local output=""
|
||||
for i=1,#args do
|
||||
output=output..tostring(args[i]).."\t"
|
||||
end
|
||||
output=output:sub(1,-2)
|
||||
kernel.tty.print(output)
|
||||
end
|
||||
|
||||
function printf(fmt, ...)
|
||||
coroutine.yield()
|
||||
local output=string.format(fmt,...)
|
||||
kernel.tty.print(output)
|
||||
end
|
||||
|
||||
function printInline(...)
|
||||
coroutine.yield()
|
||||
local args={...}
|
||||
local output=""
|
||||
for i=1,#args do
|
||||
output=output..tostring(args[i]).."\t"
|
||||
end
|
||||
output=output:sub(1,-2)
|
||||
kernel.tty.printInline(output)
|
||||
end
|
||||
@@ -1,251 +1,44 @@
|
||||
local args = {...}
|
||||
local kernel = args[1]
|
||||
local tasks={}
|
||||
local currentTask={}
|
||||
local signals={}
|
||||
local tid=2
|
||||
local gid=2
|
||||
local hookuuid=0
|
||||
local sys={}
|
||||
local kernel = ...
|
||||
kernel.log("Loading init system...")
|
||||
kernel.log("InitPath: "..kernel.config.initPath)
|
||||
local handle = kernel.vfs.open(kernel.config.initPath, "r")
|
||||
local data = kernel.vfs.read(handle, 1024 * 1024 * 4)
|
||||
kernel.vfs.close(handle)
|
||||
|
||||
function sys.hookSignal(sig, func)
|
||||
if not currentTask.signal[sig] then
|
||||
currentTask.signal[sig]={}
|
||||
end
|
||||
hookuuid=hookuuid+1
|
||||
currentTask.signal[sig][tostring(hookuuid)]=func
|
||||
callbackid=tostring(hookuuid)
|
||||
return {
|
||||
remove=function()
|
||||
currentTask.signal[sig][callbackid]=nil
|
||||
end
|
||||
}
|
||||
local initFunc, err = load(data, "@sysinit")
|
||||
if not initFunc then
|
||||
error("Failed to load init system: "..err)
|
||||
end
|
||||
|
||||
function sys.clearSignalHooks(typ)
|
||||
if not typ or typ == "all" then
|
||||
signals[tostring(currentTask.pid)]={}
|
||||
currentTask.signal=signals[tostring(currentTask.pid)]
|
||||
else
|
||||
if currentTask.signal[typ] then
|
||||
currentTask.signal[typ]={}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function sys.sendSignal(pid, signal, ...)
|
||||
if pid=="all" then
|
||||
for i,v in pairs(tasks) do
|
||||
v.sigQ[#v.sigQ+1]={signal, ...}
|
||||
end
|
||||
return
|
||||
end
|
||||
if not tasks[tostring(pid)] then return false end
|
||||
tasks[tostring(pid)].sigQ[#tasks[tostring(pid)].sigQ+1]={signal, ...}
|
||||
return true
|
||||
end
|
||||
|
||||
function sys.flushSigs()
|
||||
local sigs = {}
|
||||
for i,v in ipairs(currentTask.sigQ) do
|
||||
sigs[i]=v
|
||||
end
|
||||
for i=1, #sigs do
|
||||
local sig = sigs[i]
|
||||
if currentTask.signal[sig[1]] then
|
||||
for k,v in pairs(currentTask.signal[sig[1]]) do
|
||||
coroutine.resumeWithTimeout(coroutine.create(function()
|
||||
local ok,err = xpcall(v,debug.traceback,table.unpack(sig))
|
||||
if not ok and sig[1]~="callbackErr" then
|
||||
sys.sendSignal(currentTask.pid, "callbackErr", sig[1], k, err)
|
||||
end
|
||||
end),10)
|
||||
end
|
||||
else
|
||||
for k,v in pairs(currentTask.signal["unhandledSignal"]) do
|
||||
coroutine.resumeWithTimeout(coroutine.create(function()
|
||||
local ok,err = xpcall(v,debug.traceback,table.unpack(sig))
|
||||
if not ok and sig[1]~="callbackErr" then
|
||||
sys.sendSignal(currentTask.pid, "callbackErr", sig[1], k, err)
|
||||
end
|
||||
end),10)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function sys.spawn(func, name, evars, args)
|
||||
local id=tid
|
||||
tid=tid+1
|
||||
name=name or tostring(id)
|
||||
|
||||
signals[tostring(id)]={}
|
||||
tasks[tostring(id)]={
|
||||
coro=coroutine.create(function()
|
||||
local ret = {xpcall(func, debug.traceback, table.unpack(args))}
|
||||
if not ret[1] then
|
||||
sys.sendSignal(currentTask.ppid, "ChildTaskError", id, err)
|
||||
else
|
||||
sys.sendSignal(currentTask.ppid, "ChildTaskExit", id, table.unpack(ret, 2))
|
||||
end
|
||||
currentTask.status="Z"
|
||||
end),
|
||||
name=name,
|
||||
pid=id,
|
||||
cwd=currentTask.cwd,
|
||||
ppid=currentTask.pid,
|
||||
tgid=currentTask.tgid,
|
||||
user=kernel.user,
|
||||
uid=kernel.uid,
|
||||
evars=evars,
|
||||
args=args,
|
||||
vy=0,
|
||||
ivy=0,
|
||||
status="R",
|
||||
sleep=0,
|
||||
signal=signals[tostring(id)],
|
||||
parent=currentTask,
|
||||
children={},
|
||||
sibling=currentTask.children,
|
||||
sigQ={}
|
||||
}
|
||||
return id
|
||||
end
|
||||
|
||||
function sys.spawnFromFile(path, name, envars, args)
|
||||
sys.spawn(kernel.fs.load(path), name, envars, args)
|
||||
end
|
||||
|
||||
function sys.spawnAndWait(func, name, envars, args)
|
||||
local id=sys.spawn(func, name, envars, args)
|
||||
local exit = false
|
||||
local errored = false
|
||||
local rets = {}
|
||||
|
||||
local hext=sys.hookSignal("ChildTaskExit",function(pid, ...)
|
||||
if pid==id then
|
||||
rets={...}
|
||||
exit=true
|
||||
end
|
||||
end)
|
||||
|
||||
local herr=sys.hookSignal("ChildTaskError",function(pid, ...)
|
||||
if pid==id then
|
||||
rets={...}
|
||||
exit=true
|
||||
errored=true
|
||||
end
|
||||
end)
|
||||
|
||||
while not exit do
|
||||
coroutine.yield()
|
||||
end
|
||||
return not errored, table.unpack(rets)
|
||||
end
|
||||
|
||||
function sys.spawnFromFileAndWait(path, name, envars, args)
|
||||
return sys.spawnAndWait(kernel.fs.load(path), name, envars, args)
|
||||
end
|
||||
|
||||
function sys.exit(...)
|
||||
sys.sendSignal(currentTask.ppid, "ChildTaskExit", currentTask.pid, ...)
|
||||
currentTask.status="Z"
|
||||
coroutine.yield()
|
||||
end
|
||||
|
||||
local function collectZombieProc()
|
||||
local ret = {}
|
||||
for _,v in pairs(tasks) do
|
||||
if v.status=="Z" then
|
||||
local pid = v.pid
|
||||
for _,c in ipairs(v.children) do
|
||||
c.parent=tasks["1"]
|
||||
c.sibling=tasks["1"].children
|
||||
c.ppid=1
|
||||
c.tgid=1
|
||||
end
|
||||
signals[tostring(pid)]=nil
|
||||
tasks[tostring(pid)]=nil
|
||||
table.insert(ret, pid)
|
||||
end
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
kernel.log("initPath is: " .. tostring(kernel.initPath))
|
||||
signals["1"]={}
|
||||
tasks["1"]={
|
||||
kernel.tasks["1"] = {
|
||||
coro=coroutine.create(function()
|
||||
local ok, code_or_err = xpcall(function() return kernel.fs.readAllText(kernel.initPath) end, debug.traceback)
|
||||
if not ok then currentTask.status="Z"; kernel.panic(code_or_err) end
|
||||
local code = code_or_err
|
||||
|
||||
local func, err = load(code, "@SysInit", nil, kernel._U)
|
||||
if not func then currentTask.status="Z"; kernel.panic(err) end
|
||||
|
||||
local ok, err = xpcall(func, debug.traceback, kernel)
|
||||
local ok, err = xpcall(initFunc, debug.traceback, kernel)
|
||||
if not ok then
|
||||
currentTask.status="Z"
|
||||
kernel.panic(err)
|
||||
kernel.panic("Init system crashed: "..tostring(err))
|
||||
else
|
||||
currentTask.status="Z"
|
||||
kernel.panic("Attempted to kill init!")
|
||||
kernel.panic("Init system exited: "..tostring(err))
|
||||
end
|
||||
end),
|
||||
name="SysInit",
|
||||
name="sysinit",
|
||||
status="R",
|
||||
pid=1,
|
||||
cwd="",
|
||||
ppid=0,
|
||||
tgid=1,
|
||||
user="root",
|
||||
uid=0,
|
||||
evars={},
|
||||
args={kernel},
|
||||
vy=0,
|
||||
ivy=0,
|
||||
status="R",
|
||||
fd={},
|
||||
exit="",
|
||||
sleep=0,
|
||||
signal=signals["1"],
|
||||
parent={name="Hyprkrnl",pid=0},
|
||||
ivs=0,
|
||||
vs=0,
|
||||
parent=kernel.kernelTask,
|
||||
siblings=kernel.kernelTask.children,
|
||||
children={},
|
||||
sibling={},
|
||||
sigQ={}
|
||||
syscallReturn={},
|
||||
cwd="/",
|
||||
timeSlice=0,
|
||||
lastTime=0,
|
||||
totalTime=0,
|
||||
numRuns=0
|
||||
}
|
||||
tasks["1"].sibling={tasks["1"]}
|
||||
|
||||
kernel.log("Created pid 1")
|
||||
kernel.cache.preload.sys=sys
|
||||
kernel.cache.preload.system=kernel.cache.preload.sys
|
||||
kernel.cache.preload.os=kernel.cache.preload.sys
|
||||
kernel.hpv=sys
|
||||
kernel.tasks=tasks
|
||||
kernel.signals=signals
|
||||
kernel.currentTask=currentTask
|
||||
|
||||
kernel.saveLog()
|
||||
kernel.status="running"
|
||||
while kernel.status~="Panic" do
|
||||
for k,v in pairs(tasks) do
|
||||
currentTask=v
|
||||
kernel.computer:print(currentTask.name)
|
||||
kernel.currentTask=v
|
||||
kernel.process=currentTask.name
|
||||
kernel.user=currentTask.user
|
||||
kernel.uid=currentTask.uid
|
||||
kernel.fs.setCwd(currentTask.cwd)
|
||||
sys.flushSigs()
|
||||
local status = coroutine.resumeWithTimeout(currentTask.coro, 50)
|
||||
if status then
|
||||
currentTask.vy=currentTask.vy+1
|
||||
else
|
||||
currentTask.ivy=currentTask.ivy+1
|
||||
end
|
||||
currentTask.cwd=kernel.fs.getCwd()
|
||||
end
|
||||
collectZombieProc()
|
||||
end
|
||||
|
||||
kernel.process="Kernel"
|
||||
kernel.user="root"
|
||||
kernel.uid=0
|
||||
kernel.panic(kernel.reason or "Exited pid 0")
|
||||
kernel.log("created init task with PID 1")
|
||||
kernel.log("Initializing init system...")
|
||||
Reference in New Issue
Block a user