MeshCentral/agents/modules_meshcmd_min/process-manager.min.js
Ylian Saint-Hilaire 1bf859f0d1 Updated minified.
2019-01-16 17:59:32 -08:00

1 line
3.0 KiB
JavaScript

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;f<arguments.length;++f){l.args.push(arguments[f])}l.on("exit",function k(){delete this.Parent._psp[this.pid];var r={},q=this.ps.split("\x0D\x0A"),o={},p=0;for(var h in q){var t=q[h].split(" ");var s=[];for(var u in t){if(h==0&&t[u]){o[t[u]]=p++}if(h>0&&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;f<arguments.length;++f){l.args.push(arguments[f])}l.child=this._childProcess.execFile("/bin/ps",["ps","-xa"]);l.child.promise=l;l.child.stdout.ps="";l.child.stdout.on("data",function(h){this.ps+=h.toString()});l.child.on("exit",function(){var p=this.stdout.ps.split("\n");var q=p[0].split("PID")[0].length+3;var h=p[0].split("CMD")[0].length;var r={};for(var o=1;o<p.length;++o){if(p[o].length>0){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();