Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. function os_set_res(x,y)
  2. _x = x
  3. _y = y
  4. if mainbuf then video.delbuf(mainbuf) end
  5.  
  6. video.setres(x,y)
  7. mainbuf = video.newbuf(x,y)
  8.  
  9. video.box(mainbuf, 0, 0, x, y, 0, 0, 0, 0, 1)
  10. end
  11.  
  12.  
  13. --video.box(100, 100, 150, 150, 255, 255, 255, 1)
  14. proctable = {}
  15. LIP = loadfile("lib/LIP")()
  16. md5 = {
  17. sumhexa = function(str)
  18. return crypto.digest("md5",str)
  19. end
  20. }
  21.  
  22. safeenv = {yield = coroutine.yield,unpack=unpack,pairs = pairs, ipairs = ipairs,pcall=pcall,type=type,assert=assert,error=error,coroutine=coroutine}
  23. safeenv.string = string
  24. safeenv.table = table
  25. safeenv.crypto = crypto
  26. safeenv.math = math
  27. safeenv.tostring = tostring
  28. safeenv.tonumber = tonumber
  29. safeenv.version = "os2017,9,5"
  30. safeenv.time = os.time
  31. safeenv.strftime = os.date
  32. safeenv.selfpid = 0
  33. safeenv.md5 = md5
  34. initcfg = LIP.load("etc/init.ini")
  35. if initcfg.network then
  36. os.execute(initcfg.network.driver)
  37. f = io.open("WATTCP.CFG","wb")
  38. f:write("my_ip=dhcp\r\n")
  39. f:close()
  40. kernel.tcpipinit()
  41. os.remove("WATTCP.CFG")
  42. end
  43. if socket then
  44. socket.connect = function(...)
  45. r = socket.tcp()
  46. r:connect(...)
  47. return r
  48. end
  49. safeenv.socket = socket
  50. safeenv._sck_localhost = socket.dns.tohostname("127.0.0.1")
  51.  
  52. servsock = socket.udp()
  53. assert(servsock:setsockname("*",7))
  54. assert(servsock:settimeout(0))
  55. ip, port = servsock:getsockname()
  56. video.print("_sck_localhost=" .. safeenv._sck_localhost .. "\n")
  57. assert(ip, port)
  58. end
  59. os_set_res(800,600)
  60. bkg = video.newbuf(800,600)
  61. video.box(bkg, 0, 0, 800, 600, 0, 0, 0, 255, 1)
  62. --video.setres(640,480)
  63. img = video.imgload("png","etc/logo.png")
  64. video.put(mainbuf, 400 - 24, 300 - 24, img, -1, -1, -1, -1)
  65. video.render(bkg, mainbuf)
  66. video.print("OpenStar (C) 2016, 2017 Ronsor-OpenStar.\n")
  67. safeenv.environ = {
  68. _pwd = {},
  69. PATH = {"/bin"},
  70. LIBPATH = {"/lib"},
  71. HOME = "/",
  72. VERSION = safeenv.version
  73. }
  74. safeos = {
  75. time = os.time,
  76. date = os.date,
  77. clock = os.clock,
  78. rename = (function(a,b) fsperm(a,b); os.rename(mkfilepath(a),mkfilepath(b)) end),
  79. execute = (function()
  80. end)
  81. }
  82. safeio = {}
  83. filetable = {}
  84. function deepcopy(o, seen)
  85. seen = seen or {}
  86. if o == nil then return nil end
  87. if seen[o] then return seen[o] end
  88.  
  89. local no
  90. if type(o) == 'table' then
  91. no = {}
  92. seen[o] = no
  93.  
  94. for k, v in next, o, nil do
  95. no[deepcopy(k, seen)] = deepcopy(v, seen)
  96. end
  97. setmetatable(no, deepcopy(getmetatable(o), seen))
  98. else -- number, string, boolean, etc
  99. no = o
  100. end
  101. return no
  102. end
  103. boottime = os.time()
  104. function safeenv.getinittime()
  105. return boottime
  106. end
  107. function safeio.open(file, _mode)
  108. if _mode and _mode:find("w") then
  109. fsperm(file)
  110. end
  111. return io.open(mkfilepath(file), _mode)
  112. end
  113. function safeio.close(handle)
  114. return handle:close()
  115. end
  116. function filetable:read(s)
  117. if not s then local s = "*l" end
  118. if type(s) == "number" then
  119. local ret = {}
  120. while true do
  121. local c = self._reader(1,self)
  122. if c == "empty" then
  123. break
  124. elseif c ~= "wait" then
  125. table.insert(ret, c)
  126. end
  127. coroutine.yield()
  128. end
  129. return table.concat(ret)
  130. end
  131. local ret = {}
  132. local c
  133. if self._backspace then self._writer("_\b",self) end
  134. while true do
  135. c = self._reader(1,self)
  136. if c == "\n" or c == "\r" or c == "empty" then
  137. break
  138. elseif c == "\b" and self._backspace == true and #ret > 0 then
  139. table.remove(ret)
  140. self._writer(" \b\b_\b",self)
  141.  
  142. elseif c ~= "wait" then
  143. if self._backspace then self._writer("_\b",self) end
  144. table.insert(ret, c)
  145. end
  146.  
  147. --curenv.io.stdout:write("-")
  148. coroutine.yield()
  149. end
  150. return table.concat(ret)
  151. end
  152. function filetable:write(...)
  153. local tbl = {...}
  154. self._writer(table.concat(tbl),self)
  155. end
  156. function filetable:close()
  157. self._closer()
  158. end
  159. function vstdout(dat)
  160. video.print(dat)
  161. end
  162. allkeys = {}
  163.  
  164. function kbufadd()
  165. newkey = video.inkey()
  166. if newkey == 27 then return false end
  167. table.insert(allkeys, newkey)
  168. return true
  169. end
  170. function vstdin(n,me)
  171. local r;
  172. r = table.remove(allkeys) or 0
  173. if r == 13 then me._writer(" \n",me) end
  174. if r == 0 then
  175. --video.print("_\b")
  176. return "wait"
  177. else
  178. me._writer(string.char(r),me)
  179.  
  180. return string.char(r)
  181. end
  182. end
  183. function mkftable(reader, writer, seeker, closer)
  184. local ret = deepcopy(filetable)
  185. ret._reader = reader
  186. ret._writer = writer
  187. ret._closer = closer
  188. ret._seeker = seeker
  189. return ret
  190. end
  191. function sandbox(func)
  192. setfenv(func, curenv)
  193. return func
  194. end
  195. function safeenv.loadstring(s)
  196. return sandbox(loadstring(s))
  197. end
  198. function safeenv.require(modname)
  199. return sandbox(loadfilepath(modname, curenv.environ.LIBPATH))()
  200. end
  201. function fsperm(...)
  202. myhome = LIP.load("etc/users.ini")[safeenv.getnamefromuid(curuid)].home
  203. if curuid == 1 then return true end
  204. for _,v in pairs({...}) do
  205. if mkfilepath(v):sub(1,#myhome - 1) ~= myhome:sub(2) and mkfilepath(v) ~= myhome:sub(2) then assert(false) end
  206. end
  207. end
  208. function safeenv.kill(pid)
  209. assert(curuid == proctable[pid].uid or curuid == 1)
  210. proctable[pid] = nil
  211. end
  212. function safeenv.getuidfromname(uid)
  213. udb = LIP.load("etc/users.ini")
  214. return udb[uid].uid
  215. end
  216. function safeenv.getnamefromuid(uid)
  217. udb = LIP.load("etc/users.ini")
  218. for k, v in pairs(udb) do
  219. if v.uid == uid then return k end
  220. end
  221. return nil
  222. end
  223. function safeenv.setmypass(newpass)
  224. udbx = LIP.load("etc/users.ini")
  225. udbx[safeenv.getnamefromuid(curuid)].password = md5.sumhexa(newpass)
  226. LIP.save("etc/users.ini",udbx)
  227. end
  228. function safeenv.getuid()
  229. return curuid
  230. end
  231. function safeenv.setuid(newuid)
  232. assert(curuid == 1)
  233. proctable[curpid].uid = newuid
  234. curuid = newuid
  235. end
  236. safeio.stdout = mkftable(nil, vstdout, nil, nil)
  237. safeio.stdin = mkftable(vstdin, vstdout, nil, nil)
  238. safeio.stdin._backspace = true
  239. safeenv.io = safeio
  240. safeenv.os = safeos
  241. safeenv._run_cc = kernel.cc
  242. safeenv.graphics = deepcopy(video)
  243. safeenv.graphics.print = nil
  244. safeenv.graphics.inkey = nil
  245. loadfile("lib/ui")()
  246. function safeenv.proclist()
  247. local ret = {}
  248. for i, s in pairs(proctable) do
  249. ret[i] = {name=s.name,uid=s.uid,time=s.time or -1}
  250. end
  251. return ret
  252. end
  253.  
  254. function safeenv.fork(func)
  255. return spawn(func,curname .. " (child)", curuid, nil, nil, curenv)
  256. end
  257. function safeenv.pidstatus(pid)
  258. if not proctable[pid] then return "dead" end
  259. return "alive"
  260. end
  261. function safeenv.setuserpass(user,pass)
  262. assert(curuid == 1)
  263. udb = LIP.load("etc/users.ini")
  264. udb[user].password = md5.sumhexa(pass)
  265. LIP.save("etc/users.ini", udb)
  266. end
  267. function safeenv.setuseropt(user,opt,val)
  268. assert(curuid == 1)
  269. udb = LIP.load("etc/users.ini")
  270. udb[user][opt] = val
  271. LIP.save("etc/users.ini", udb)
  272. end
  273. function safeenv.getmyopt(opt)
  274. return LIP.load("etc/users.ini")[safeenv.getnamefromuid(curuid)][opt]
  275. end
  276. function safeenv.auth(user,pass)
  277. udb = LIP.load("etc/users.ini")
  278. return udb[user].password == md5.sumhexa(pass)
  279. end
  280. function safeenv.setuidauth(user,pass)
  281. assert(safeenv.auth(user,pass))
  282. curuid = udb[user].uid
  283. proctable[curpid].uid = curuid
  284. end
  285. function loadfilepath(binary,path)
  286. for _,abc in ipairs(path) do
  287. --print(mkfilepath(binary,curenv.environ._PWD))
  288. toret = loadfile(mkfilepath(binary,mkfilepath(abc,curenv.environ._PWD,true) ) )
  289. if toret then return toret end
  290. end
  291. return loadfile(mkfilepath(binary))
  292. end
  293. function safeenv.spawn(path,args,environ)
  294. return spawn(loadfilepath(path,curenv.environ.PATH), path, curuid, args, environ, nil, curpid)
  295. end
  296. function safeenv.chdir(newpath)
  297. curenv.environ._pwd = mkfilepath(newpath,curenv.environ._PWD,true)
  298. end
  299. function safeenv.usedram()
  300. return collectgarbage("count")
  301. end
  302. function mkfilepath(path,npwd,retstr)
  303. local ret = {}
  304. if path:sub(1,1) ~= "/" then
  305. ret = deepcopy(npwd or curenv.environ._pwd)
  306.  
  307. end
  308. for a in path:gmatch("([^%/]+)") do
  309. if a == ".." then
  310. table.remove(ret)
  311. elseif a == "." then
  312. -- nothing
  313. else
  314. table.insert(ret, a)
  315. end
  316. end
  317. -- video.print(table.concat(ret, "/") .. "\n")
  318. if not retstr then
  319. return table.concat(ret, "/")
  320. else
  321. return ret
  322. end
  323. end
  324. function findthread(n)
  325. for k,v in pairs(proctable) do
  326. if v.thread == n then return k end
  327. end
  328. return false
  329. end
  330. function listdir(path)
  331. local extra = {}
  332. if path == "" then
  333. extra = { "dev", "mnt" }
  334. end
  335. if path:sub(1,3) == "dev" then
  336. return {"console"}
  337. end
  338. if path:sub(1,3) == "mnt" then
  339. return {}
  340. end
  341. local ret = {unpack(extra),kernel.listdir(path)}
  342. --video.print(table.concat(ret, " "))
  343. return ret
  344. end
  345. function safeenv.debug(pid)
  346. assert(curuid == proctable[pid].uid or curuid == 1)
  347. return proctable[pid].env
  348. end
  349. function safeenv.listdir(path)
  350. if not path then local path = "/" .. table.concat(curenv.environ._pwd, "/") end
  351. return listdir(mkfilepath(path))
  352. end
  353. function safeenv.mkdir(path)
  354. fsperm(path)
  355. kernel.mkdir(mkfilepath(path))
  356. end
  357. safeenv.mkfilepath = mkfilepath
  358. function safeenv.rmdir(path)
  359. fsperm(path)
  360. kernel.rmdir(mkfilepath(path))
  361. end
  362. function safeenv.rm(path)
  363. fsperm(path)
  364. os.remove(mkfilepath(path))
  365. end
  366. function spawn(func,name,uid,args,environ,newenv,parentid)
  367. local newthread
  368. local tenv = newenv or deepcopy(safeenv)
  369. if not newenv then
  370. tenv._args = args
  371. tenv.arg = args
  372. end
  373. if not newenv then
  374. tenv.environ = parentid and proctable[parentid] and proctable[parentid].env.environ and deepcopy(proctable[parentid].env.environ) or deepcopy(safeenv.environ)
  375. end
  376. pcall(function()
  377. -- proctable[parentid].env.io.stdout:write("Hi world\n")
  378. tenv.io.stdout = proctable[parentid].env.io.stdout
  379. tenv.io.stdin = proctable[parentid].env.io.stdin
  380. -- tenv.io.stdout:write("Hi\n")
  381. end)
  382. setfenv(func, tenv)
  383.  
  384. newthread = coroutine.create(func)
  385.  
  386.  
  387. table.insert(proctable, {name=name,thread=newthread,uid=uid,env=tenv})
  388. return findthread(newthread)
  389. end
  390. function safeenv.os.exit()
  391. error("Exiting")
  392. end
  393. for k,a in pairs(initcfg.apps) do
  394. spawn(function()
  395. --while true do io.stdout:write("-") end
  396. spawn(arg[1],{},environ)
  397. end, "default", 1, {a}, safeenv.environ)
  398. end
  399. -- dofile("lib/stddrv.so")
  400. uith = coroutine.create(function()
  401. while true do
  402. if ui_tick then
  403. video.render(ui_tick())
  404. end
  405. coroutine.yield()
  406. end
  407. end)
  408. while kbufadd() do
  409. coroutine.resume(uith)
  410. if servsock then
  411. dgram, ip, port = servsock:receivefrom()
  412. if dgram then
  413. servsock:sendto(dgram, ip, port)
  414. end
  415. end
  416. for i,s in pairs(proctable) do
  417. curuid = s.uid
  418. curname = s.name
  419. curenv = s.env
  420. s.env.selfpid = i
  421. st = os.clock()
  422. curpid = i
  423. if coroutine.status(s.thread) == "dead" then proctable[i] = nil else
  424. debug.sethook(s.thread, (function()
  425. error("Process taking too long.")
  426. end), "", 16384)
  427. ok, errmsg = coroutine.resume(s.thread)
  428. if not ok and not errmsg:find("Exit") then
  429. s.env.io.stdout._writer(debug.traceback(s.thread) .. "\n" .. errmsg .. "\n",s.env.io.stdout)
  430. if s.env.io.stdout._console then
  431. --s.env.io.stdout._console.pid = false
  432. end
  433. --video.print(debug.traceback(s.thread) .. "\n" .. errmsg .. "\n")
  434. --while kbufadd() do end
  435. proctable[i] = nil end
  436. end
  437. s.time = os.clock() - st
  438.  
  439. end
  440. collectgarbage()
  441. end
  442.  
  443. --[[if not status then
  444. estr = debug.traceback(2)
  445. video.setres(640,480)
  446. video.print("Fatal system error:\n")
  447. video.print(errmsg)
  448. while kbufadd() do end
  449. end]]
  450. video.print("Shutting down.")
  451. while kbufadd() do end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement