Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function os_set_res(x,y)
- _x = x
- _y = y
- if mainbuf then video.delbuf(mainbuf) end
- video.setres(x,y)
- mainbuf = video.newbuf(x,y)
- video.box(mainbuf, 0, 0, x, y, 0, 0, 0, 0, 1)
- end
- --video.box(100, 100, 150, 150, 255, 255, 255, 1)
- proctable = {}
- LIP = loadfile("lib/LIP")()
- md5 = {
- sumhexa = function(str)
- return crypto.digest("md5",str)
- end
- }
- safeenv = {yield = coroutine.yield,unpack=unpack,pairs = pairs, ipairs = ipairs,pcall=pcall,type=type,assert=assert,error=error,coroutine=coroutine}
- safeenv.string = string
- safeenv.table = table
- safeenv.crypto = crypto
- safeenv.math = math
- safeenv.tostring = tostring
- safeenv.tonumber = tonumber
- safeenv.version = "os2017,9,5"
- safeenv.time = os.time
- safeenv.strftime = os.date
- safeenv.selfpid = 0
- safeenv.md5 = md5
- initcfg = LIP.load("etc/init.ini")
- if initcfg.network then
- os.execute(initcfg.network.driver)
- f = io.open("WATTCP.CFG","wb")
- f:write("my_ip=dhcp\r\n")
- f:close()
- kernel.tcpipinit()
- os.remove("WATTCP.CFG")
- end
- if socket then
- socket.connect = function(...)
- r = socket.tcp()
- r:connect(...)
- return r
- end
- safeenv.socket = socket
- safeenv._sck_localhost = socket.dns.tohostname("127.0.0.1")
- servsock = socket.udp()
- assert(servsock:setsockname("*",7))
- assert(servsock:settimeout(0))
- ip, port = servsock:getsockname()
- video.print("_sck_localhost=" .. safeenv._sck_localhost .. "\n")
- assert(ip, port)
- end
- os_set_res(800,600)
- bkg = video.newbuf(800,600)
- video.box(bkg, 0, 0, 800, 600, 0, 0, 0, 255, 1)
- --video.setres(640,480)
- img = video.imgload("png","etc/logo.png")
- video.put(mainbuf, 400 - 24, 300 - 24, img, -1, -1, -1, -1)
- video.render(bkg, mainbuf)
- video.print("OpenStar (C) 2016, 2017 Ronsor-OpenStar.\n")
- safeenv.environ = {
- _pwd = {},
- PATH = {"/bin"},
- LIBPATH = {"/lib"},
- HOME = "/",
- VERSION = safeenv.version
- }
- safeos = {
- time = os.time,
- date = os.date,
- clock = os.clock,
- rename = (function(a,b) fsperm(a,b); os.rename(mkfilepath(a),mkfilepath(b)) end),
- execute = (function()
- end)
- }
- safeio = {}
- filetable = {}
- function deepcopy(o, seen)
- seen = seen or {}
- if o == nil then return nil end
- if seen[o] then return seen[o] end
- local no
- if type(o) == 'table' then
- no = {}
- seen[o] = no
- for k, v in next, o, nil do
- no[deepcopy(k, seen)] = deepcopy(v, seen)
- end
- setmetatable(no, deepcopy(getmetatable(o), seen))
- else -- number, string, boolean, etc
- no = o
- end
- return no
- end
- boottime = os.time()
- function safeenv.getinittime()
- return boottime
- end
- function safeio.open(file, _mode)
- if _mode and _mode:find("w") then
- fsperm(file)
- end
- return io.open(mkfilepath(file), _mode)
- end
- function safeio.close(handle)
- return handle:close()
- end
- function filetable:read(s)
- if not s then local s = "*l" end
- if type(s) == "number" then
- local ret = {}
- while true do
- local c = self._reader(1,self)
- if c == "empty" then
- break
- elseif c ~= "wait" then
- table.insert(ret, c)
- end
- coroutine.yield()
- end
- return table.concat(ret)
- end
- local ret = {}
- local c
- if self._backspace then self._writer("_\b",self) end
- while true do
- c = self._reader(1,self)
- if c == "\n" or c == "\r" or c == "empty" then
- break
- elseif c == "\b" and self._backspace == true and #ret > 0 then
- table.remove(ret)
- self._writer(" \b\b_\b",self)
- elseif c ~= "wait" then
- if self._backspace then self._writer("_\b",self) end
- table.insert(ret, c)
- end
- --curenv.io.stdout:write("-")
- coroutine.yield()
- end
- return table.concat(ret)
- end
- function filetable:write(...)
- local tbl = {...}
- self._writer(table.concat(tbl),self)
- end
- function filetable:close()
- self._closer()
- end
- function vstdout(dat)
- video.print(dat)
- end
- allkeys = {}
- function kbufadd()
- newkey = video.inkey()
- if newkey == 27 then return false end
- table.insert(allkeys, newkey)
- return true
- end
- function vstdin(n,me)
- local r;
- r = table.remove(allkeys) or 0
- if r == 13 then me._writer(" \n",me) end
- if r == 0 then
- --video.print("_\b")
- return "wait"
- else
- me._writer(string.char(r),me)
- return string.char(r)
- end
- end
- function mkftable(reader, writer, seeker, closer)
- local ret = deepcopy(filetable)
- ret._reader = reader
- ret._writer = writer
- ret._closer = closer
- ret._seeker = seeker
- return ret
- end
- function sandbox(func)
- setfenv(func, curenv)
- return func
- end
- function safeenv.loadstring(s)
- return sandbox(loadstring(s))
- end
- function safeenv.require(modname)
- return sandbox(loadfilepath(modname, curenv.environ.LIBPATH))()
- end
- function fsperm(...)
- myhome = LIP.load("etc/users.ini")[safeenv.getnamefromuid(curuid)].home
- if curuid == 1 then return true end
- for _,v in pairs({...}) do
- if mkfilepath(v):sub(1,#myhome - 1) ~= myhome:sub(2) and mkfilepath(v) ~= myhome:sub(2) then assert(false) end
- end
- end
- function safeenv.kill(pid)
- assert(curuid == proctable[pid].uid or curuid == 1)
- proctable[pid] = nil
- end
- function safeenv.getuidfromname(uid)
- udb = LIP.load("etc/users.ini")
- return udb[uid].uid
- end
- function safeenv.getnamefromuid(uid)
- udb = LIP.load("etc/users.ini")
- for k, v in pairs(udb) do
- if v.uid == uid then return k end
- end
- return nil
- end
- function safeenv.setmypass(newpass)
- udbx = LIP.load("etc/users.ini")
- udbx[safeenv.getnamefromuid(curuid)].password = md5.sumhexa(newpass)
- LIP.save("etc/users.ini",udbx)
- end
- function safeenv.getuid()
- return curuid
- end
- function safeenv.setuid(newuid)
- assert(curuid == 1)
- proctable[curpid].uid = newuid
- curuid = newuid
- end
- safeio.stdout = mkftable(nil, vstdout, nil, nil)
- safeio.stdin = mkftable(vstdin, vstdout, nil, nil)
- safeio.stdin._backspace = true
- safeenv.io = safeio
- safeenv.os = safeos
- safeenv._run_cc = kernel.cc
- safeenv.graphics = deepcopy(video)
- safeenv.graphics.print = nil
- safeenv.graphics.inkey = nil
- loadfile("lib/ui")()
- function safeenv.proclist()
- local ret = {}
- for i, s in pairs(proctable) do
- ret[i] = {name=s.name,uid=s.uid,time=s.time or -1}
- end
- return ret
- end
- function safeenv.fork(func)
- return spawn(func,curname .. " (child)", curuid, nil, nil, curenv)
- end
- function safeenv.pidstatus(pid)
- if not proctable[pid] then return "dead" end
- return "alive"
- end
- function safeenv.setuserpass(user,pass)
- assert(curuid == 1)
- udb = LIP.load("etc/users.ini")
- udb[user].password = md5.sumhexa(pass)
- LIP.save("etc/users.ini", udb)
- end
- function safeenv.setuseropt(user,opt,val)
- assert(curuid == 1)
- udb = LIP.load("etc/users.ini")
- udb[user][opt] = val
- LIP.save("etc/users.ini", udb)
- end
- function safeenv.getmyopt(opt)
- return LIP.load("etc/users.ini")[safeenv.getnamefromuid(curuid)][opt]
- end
- function safeenv.auth(user,pass)
- udb = LIP.load("etc/users.ini")
- return udb[user].password == md5.sumhexa(pass)
- end
- function safeenv.setuidauth(user,pass)
- assert(safeenv.auth(user,pass))
- curuid = udb[user].uid
- proctable[curpid].uid = curuid
- end
- function loadfilepath(binary,path)
- for _,abc in ipairs(path) do
- --print(mkfilepath(binary,curenv.environ._PWD))
- toret = loadfile(mkfilepath(binary,mkfilepath(abc,curenv.environ._PWD,true) ) )
- if toret then return toret end
- end
- return loadfile(mkfilepath(binary))
- end
- function safeenv.spawn(path,args,environ)
- return spawn(loadfilepath(path,curenv.environ.PATH), path, curuid, args, environ, nil, curpid)
- end
- function safeenv.chdir(newpath)
- curenv.environ._pwd = mkfilepath(newpath,curenv.environ._PWD,true)
- end
- function safeenv.usedram()
- return collectgarbage("count")
- end
- function mkfilepath(path,npwd,retstr)
- local ret = {}
- if path:sub(1,1) ~= "/" then
- ret = deepcopy(npwd or curenv.environ._pwd)
- end
- for a in path:gmatch("([^%/]+)") do
- if a == ".." then
- table.remove(ret)
- elseif a == "." then
- -- nothing
- else
- table.insert(ret, a)
- end
- end
- -- video.print(table.concat(ret, "/") .. "\n")
- if not retstr then
- return table.concat(ret, "/")
- else
- return ret
- end
- end
- function findthread(n)
- for k,v in pairs(proctable) do
- if v.thread == n then return k end
- end
- return false
- end
- function listdir(path)
- local extra = {}
- if path == "" then
- extra = { "dev", "mnt" }
- end
- if path:sub(1,3) == "dev" then
- return {"console"}
- end
- if path:sub(1,3) == "mnt" then
- return {}
- end
- local ret = {unpack(extra),kernel.listdir(path)}
- --video.print(table.concat(ret, " "))
- return ret
- end
- function safeenv.debug(pid)
- assert(curuid == proctable[pid].uid or curuid == 1)
- return proctable[pid].env
- end
- function safeenv.listdir(path)
- if not path then local path = "/" .. table.concat(curenv.environ._pwd, "/") end
- return listdir(mkfilepath(path))
- end
- function safeenv.mkdir(path)
- fsperm(path)
- kernel.mkdir(mkfilepath(path))
- end
- safeenv.mkfilepath = mkfilepath
- function safeenv.rmdir(path)
- fsperm(path)
- kernel.rmdir(mkfilepath(path))
- end
- function safeenv.rm(path)
- fsperm(path)
- os.remove(mkfilepath(path))
- end
- function spawn(func,name,uid,args,environ,newenv,parentid)
- local newthread
- local tenv = newenv or deepcopy(safeenv)
- if not newenv then
- tenv._args = args
- tenv.arg = args
- end
- if not newenv then
- tenv.environ = parentid and proctable[parentid] and proctable[parentid].env.environ and deepcopy(proctable[parentid].env.environ) or deepcopy(safeenv.environ)
- end
- pcall(function()
- -- proctable[parentid].env.io.stdout:write("Hi world\n")
- tenv.io.stdout = proctable[parentid].env.io.stdout
- tenv.io.stdin = proctable[parentid].env.io.stdin
- -- tenv.io.stdout:write("Hi\n")
- end)
- setfenv(func, tenv)
- newthread = coroutine.create(func)
- table.insert(proctable, {name=name,thread=newthread,uid=uid,env=tenv})
- return findthread(newthread)
- end
- function safeenv.os.exit()
- error("Exiting")
- end
- for k,a in pairs(initcfg.apps) do
- spawn(function()
- --while true do io.stdout:write("-") end
- spawn(arg[1],{},environ)
- end, "default", 1, {a}, safeenv.environ)
- end
- -- dofile("lib/stddrv.so")
- uith = coroutine.create(function()
- while true do
- if ui_tick then
- video.render(ui_tick())
- end
- coroutine.yield()
- end
- end)
- while kbufadd() do
- coroutine.resume(uith)
- if servsock then
- dgram, ip, port = servsock:receivefrom()
- if dgram then
- servsock:sendto(dgram, ip, port)
- end
- end
- for i,s in pairs(proctable) do
- curuid = s.uid
- curname = s.name
- curenv = s.env
- s.env.selfpid = i
- st = os.clock()
- curpid = i
- if coroutine.status(s.thread) == "dead" then proctable[i] = nil else
- debug.sethook(s.thread, (function()
- error("Process taking too long.")
- end), "", 16384)
- ok, errmsg = coroutine.resume(s.thread)
- if not ok and not errmsg:find("Exit") then
- s.env.io.stdout._writer(debug.traceback(s.thread) .. "\n" .. errmsg .. "\n",s.env.io.stdout)
- if s.env.io.stdout._console then
- --s.env.io.stdout._console.pid = false
- end
- --video.print(debug.traceback(s.thread) .. "\n" .. errmsg .. "\n")
- --while kbufadd() do end
- proctable[i] = nil end
- end
- s.time = os.clock() - st
- end
- collectgarbage()
- end
- --[[if not status then
- estr = debug.traceback(2)
- video.setres(640,480)
- video.print("Fatal system error:\n")
- video.print(errmsg)
- while kbufadd() do end
- end]]
- video.print("Shutting down.")
- while kbufadd() do end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement