local a=...local b={}local c={}local d=2;a.exitMain=false;local e=coroutine.resumeWithTimeout;local function f(g,h)return math.floor(g/2^h)%2==1 end;local function i(j)a.vfs.access(j,"rx")local k=a.vfs.open(j,"r")local l=a.vfs.read(k,1024*1024*4)a.vfs.close(k)local m=a.freshUserEnv()local n,o=load(l,"@"..j,"t",m)if not n then error("ENOEXEC: "..tostring(o))end;local p=a.vfs.lstat(j)local q=f(p.perms,6)local r=a.currentTask and a.currentTask.uid or a.uid;local s=q and p.owner or r;return n,s,q end;local function t(n,u,v,w,x,y,z)local A=d;d=d+1;b[tostring(A)]={coro=coroutine.create(function()local B,o=xpcall(n,debug.traceback,table.unpack(w or{}))if a.config.logTaskExit then if not B then a.log("Task "..tostring(A).." exited with err: "..tostring(o),"ERROR",0xFF0000)elseif o then a.log("Task "..tostring(A).." exited with code: "..tostring(o),"DBUG",0x00FFFF)else a.log("Task "..tostring(A).." exited without code","DBUG",0x00FFFF)end end;if type(o)=="number"then b[tostring(A)].exit=o end;if b[tostring(A)].fd then for k,C in pairs(b[tostring(A)].fd)do pcall(a.vfs.close,k)end end;b[tostring(A)].status="Z"end),name=u or"task"..tostring(A),envars=v or(a.currentTask and a.currentTask.envars or{}),args=w or{},status="R",pid=A,tgid=x or(a.currentTask and a.currentTask.tgid or A),uid=y,euid=z,gid=a.currentTask and a.currentTask.gid or 0,groups=a.currentTask and a.currentTask.groups or{},fd={},sleep=0,ivs=0,vs=0,children={},parent=a.currentTask or a.kernelTask,siblings=a.currentTask and a.currentTask.children or a.kernelTask.children,syscallReturn={},cwd=a.currentTask and a.currentTask.cwd or"/",timeSlice=0,lastTime=0,totalTime=0,numRuns=0}table.insert(a.currentTask and a.currentTask.children or a.kernelTask.children,b[tostring(A)])return A end;function c.spawn(n,u,v,w,x)local D=a.currentTask;local y=D and D.uid or a.uid;local z=D and D.euid or y;return t(n,u,v,w,x,y,z)end;function c.execspawn(j,u,v,w,x)local n,s,E=i(j,a._U)local D=a.currentTask;local y=D and D.uid or a.uid;if E then a.log("execspawn: suid exec '"..j.."' caller_uid="..tostring(y).." -> euid="..tostring(s))end;return t(n,u or j,v,w,x,y,s)end;function c.exec(j,w,v)local F=a.currentTask;local n,s,C=i(j,a._U)if F.fd then for k,C in pairs(F.fd)do if k>2 then pcall(a.vfs.close,k)end end end;F.euid=s;F.args=w or{}F.envars=v or F.envars;F.name=j;F.coro=coroutine.create(function()local B,o=xpcall(n,debug.traceback,table.unpack(F.args))if a.config.logTaskExit then if not B then a.log("Task "..tostring(F.pid).." exec '"..j.."' err: "..tostring(o),"ERROR",0xFF0000)else a.log("Task "..tostring(F.pid).." exec '"..j.."' exited: "..tostring(o),"DBUG",0x00FFFF)end end;if type(o)=="number"then b[tostring(F.pid)].exit=o end;if b[tostring(F.pid)].fd then for k,C in pairs(b[tostring(F.pid)].fd)do pcall(a.vfs.close,k)end end;b[tostring(F.pid)].status="Z"end)F.syscallReturn={}coroutine.yield()end;function c.sleep(G)a.currentTask.status="S"a.currentTask.sleep=a.EFI:getEpochMs()+G*1000 end;function c.getTask(H)local F=b[tostring(H)]if not F then return nil end;local I,J={},{}for K,L in ipairs(F.children)do I[K]=L.pid end;for K,L in ipairs(F.siblings)do J[K]=L.pid end;return{name=F.name,status=F.status,pid=F.pid,tgid=F.tgid,username=a.users[F.uid],uid=F.uid,euid=F.euid,exit=F.exit,sleep=F.sleep,ivs=F.ivs,vs=F.vs,children=I,siblings=J,parent=F.parent.pid,cwd=F.cwd,term=F.term}end;function c.collect(H)local I={}for C,L in ipairs(a.currentTask.children)do I[#I+1]=L.pid end;local F=b[tostring(H)]if not F then return false,"Task does not exist"elseif not isEqualToAny(F.pid,table.unpack(I))then return false,"You do not own this task"elseif F.status~="Z"then return false,"Task must exit to collect status"else F.reapTime=0;return true,F.exit end end;function c.kill(H)local F=b[tostring(H)]if not F then return false,"Task does not exist"elseif F.status=="Z"then return false,"Task is already dead"end;local D=a.currentTask;local M=D and(D.euid or D.uid)or a.uid;if M~=0 and F.uid~=(D and D.uid or a.uid)then return false,"EPERM"end;F.status="Z"return true end;function c.stop(H)local F=b[tostring(H)]if not F then return false,"Task does not exist"elseif F.status~="R"then return false,"Cannot stop non-running task"else F.status="T"return true end end;function c.continue(H)local F=b[tostring(H)]if not F then return false,"Task does not exist"elseif F.status~="T"then return false,"Task is not stopped"else F.status="R"return true end end;function c.getpid()return a.currentTask.pid end;function c.getppid()return a.currentTask.parent.pid end;function c.getTasks()local N={}for C,L in pairs(b)do N[#N+1]=L.pid end;return N end;function c.getEnviron(O)return a.currentTask.envars[O]end;function c.setEnviron(O,P)a.currentTask.envars[O]=P end;function c.exit(Q)local F=a.currentTask;if a.config.logTaskExit then if Q then a.log("Task "..tostring(F.pid).." exited with code: "..tostring(Q),"DBUG",0x00FFFF)else a.log("Task "..tostring(F.pid).." exited without code","DBUG",0x00FFFF)end end;b[tostring(F.pid)].status="Z"if type(Q)=="number"then b[tostring(F.pid)].exit=Q end end;function c.setuid(R)local F=a.currentTask;if F.euid~=0 and F.uid~=R then error("EPERM")end;F.uid=R;F.euid=R;a.uid=R end;function c.geteuid()return a.currentTask.euid end;function c.getuid()return a.currentTask.uid end;local function S()for H,F in pairs(b)do if F.status=="Z"and not F.reapTime then F.coro=nil;F.ivs=nil;F.vs=nil;F.args=nil;F.envars=nil;F.cwd=nil;F.numRuns=nil;F.totalTime=nil;F.lastTime=nil;F.timeSlice=nil;F.syscallReturn=nil;F.sleep=nil;F.fd=nil;F.reapTime=a.EFI:getEpochMs()+30000 elseif F.reapTime and a.EFI:getEpochMs()>F.reapTime and F.status=="Z"then for C,T in ipairs(F.children)do T.parent=b["1"]T.siblings=b["1"].children;table.insert(b["1"].children,T)end;for K,U in ipairs(F.siblings)do if U.pid==F.pid then table.remove(F.siblings,K)break end end;b[H]=nil end end end;local V=0.85;local W=0.01;local X=0.0005;local Y=0.5;local Z=0.08;local _=0.03;local a0=0.02;local a1=0.5;local a2=0.5;local a3=0.01;function a.main()while not a.exitMain do local a4=0;local a5=0;local a6=0;local a7=0;local a8={}for H,F in pairs(b)do a.currentTask=F;a.uid=F.euid or F.uid;a.process=F.name;if F.status=="S"and a.EFI:getEpochMs()>=F.sleep then F.status="R"F.sleep=0 end;if F.status=="D"then if F.ksh then coroutine.resume(F.ksh)end end;if F.status=="R"then a4=a4+1;F.timeSlice=math.min(Y,math.max(X,a3/a4^V))if F.sigq and#F.sigq~=0 and F.sigh then local a9=coroutine.create(F.sigh)local aa,o=coroutine.resumeWithTimeout(a9,100,table.remove(F.sigq,1))if aa=="error"then F.sigd.error=o;F.sigd.active=false;F.sigh=nil;F.sigq=nil;F.sigd=nil elseif aa=="success"then if o=="syscall"then F.sigd.error="Cannot execute syscalls from signals"F.sigd.active=false;F.sigh=nil;F.sigq=nil;F.sigd=nil end end end;if F.status=="R"then local ab=a.EFI:getEpochMs()local N;if a.config.preempt then if not F.debugger then N={e(F.coro,F.timeSlice,table.unpack(F.syscallReturn))}else N={coroutine.resume(F.coro,table.unpack(F.syscallReturn))}end else N={coroutine.resume(F.coro,table.unpack(F.syscallReturn))}end;local ac=a.EFI:getEpochMs()-ab;F.lastTime=ac;F.totalTime=(F.totalTime or 0)+ac;F.numRuns=(F.numRuns or 0)+1;a8[#a8+1]=ac;a7=a7+ac;if ac<=X then a5=a5+1 end;if ac>=Y then a6=a6+1 end;if N[1]=="error"or N[1]==false then a.log("processHandlerException: "..tostring(N[2]),"ERROR",0xFF0000)F.status="Z"F.exit="processHandlerException: "..tostring(N[2])elseif N[1]=="timeout"then F.ivs=F.ivs+1;F.syscallReturn={}elseif N[1]=="success"or N[1]==true then F.vs=F.vs+1;if N[2]=="syscall"then local ad=N[3]if a.syscalls[ad]then if a.config.debugSyscalls then a.log("Task "..F.pid.." syscall: "..ad,"DBUG",0x00FFFF)for K=4,#N do a.log(" inval["..K-3 .."] = "..tostring(N[K]),"DBUG",0x00FFFF)end end;local ae={xpcall(a.syscalls[ad],debug.traceback,table.unpack(N,4))}if a.config.debugSyscalls then if not ae[1]then a.log("Task "..F.pid.." syscall "..ad.." failed: "..tostring(ae[2]),"ERROR",0xFF0000)else a.log("Task "..F.pid.." syscall "..ad.." ok, "..#ae-1 .." retvals","DBUG",0x00FFFF)for K=2,#ae do local L=type(ae[K])=="table"and table.serialize(ae[K])or tostring(ae[K])a.log(" retval["..K-1 .."] = "..L,"DBUG",0x00FFFF)end end end;if not ae[1]then F.syscallReturn={false,ae[2]}else F.syscallReturn={true,table.unpack(ae,2)}end else F.syscallReturn={false,"Unknown syscall: "..tostring(ad)}end end end end end end;local af=a4>0 and a7/a4 or 0;local ag=0;for C,ah in ipairs(a8)do ag=ag+(ah-af)^2 end;if a4>0 then ag=ag/a4 end;if a4>0 then local ai=a1*a5/a4-a2*a6/a4;local aj=W*a4^(V-1)/math.max(af,1e-8)a3=a3+Z*(aj-a3)+_*ai-a0*ag end;S()end end;local ak=a.syscalls;ak["spawn"]=c.spawn;ak["execspawn"]=c.execspawn;ak["exec"]=c.exec;ak["sleep"]=c.sleep;ak["getTask"]=c.getTask;ak["collect"]=c.collect;ak["kill"]=c.kill;ak["stop"]=c.stop;ak["continue"]=c.continue;ak["getpid"]=c.getpid;ak["getppid"]=c.getppid;ak["getTasks"]=c.getTasks;ak["setEnviron"]=c.setEnviron;ak["getEnviron"]=c.getEnviron;ak["exit"]=c.exit;ak["setuid"]=c.setuid;ak["getuid"]=c.getuid;ak["geteuid"]=c.geteuid;a._G.sleep=function(...)coroutine.yield("syscall","sleep",...)end;a.tasks=b;a.hpv=c