var GM=require("_GenericMarshal");function processManager(){this._ObjectID="process-manager";switch(process.platform){case"win32":this._kernel32=GM.CreateNativeProxy("kernel32.dll");this._kernel32.CreateMethod("GetLastError");this._kernel32.CreateMethod("CreateToolhelp32Snapshot");this._kernel32.CreateMethod("Process32First");this._kernel32.CreateMethod("Process32Next");break;case"linux":case"darwin":this._childProcess=require("child_process");break;default:throw (process.platform+" not supported");break}this.enumerateProcesses=function a(){var d=require("promise");var e=new d(function(g,f){this._res=g;this._rej=f});this.getProcesses(function(g,f){f._res(g)},e);return(e)};this.getProcesses=function b(d){switch(process.platform){default:throw ("Enumerating processes on "+process.platform+" not supported");break;case"win32":var n={};var e=this._kernel32.CreateToolhelp32Snapshot(2,0);var g=GM.CreateVariable(304);g.toBuffer().writeUInt32LE(304,0);var j=this._kernel32.Process32First(e,g);while(j.Val){n[g.Deref(8,4).toBuffer().readUInt32LE(0)]={pid:g.Deref(8,4).toBuffer().readUInt32LE(0),cmd:g.Deref(GM.PointerSize==4?36:44,260).String};j=this._kernel32.Process32Next(e,g)}if(d){d.apply(this,[n])}break;case"linux":if(!this._psp){this._psp={}}var l=this._childProcess.execFile("/bin/ps",["ps","-uxa"],{type:this._childProcess.SpawnTypes.TERM});this._psp[l.pid]=l;l.Parent=this;l.ps="";l.callback=d;l.args=[];for(var f=1;f0&&t[u]){s.push(t[u])}}if(h>0){if(s[o.PID]){r[s[o.PID]]={pid:o.PID,user:s[o.USER],cmd:s[o.COMMAND]}}}}if(this.callback){this.args.unshift(r);this.callback.apply(this.parent,this.args)}});l.stdout.on("data",function(h){this.parent.ps+=h.toString()});break;case"darwin":var m=require("promise");var l=new m(function(i,h){this._res=i;this._rej=h});l.pm=this;l.callback=d;l.args=[];for(var f=1;f0){r[p[o].substring(0,q).trim()]={pid:p[o].substring(0,q).trim(),cmd:p[o].substring(h)}}}this.promise._res(r)});l.then(function(h){this.args.unshift(h);this.callback.apply(this.pm,this.args)});break}};this.getProcessInfo=function c(g){switch(process.platform){default:throw ("getProcessInfo() not supported for "+process.platform);break;case"linux":var h=require("fs").readFileSync("/proc/"+g+"/status");var e={};var f=h.toString().split("\n");for(var d in f){var j=f[d].split(":");if(j.length>1){j[1]=j[1].trim()}e[j[0]]=j[1]}return(e);break}}}module.exports=new processManager();