This repository has been archived by the owner on Aug 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpslook.js~
executable file
·179 lines (174 loc) · 4.81 KB
/
pslook.js~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var exec = require('child_process').exec,
fs = require('fs'),
child;
// Default options
var defaultOptions={
datas:[],
match:'*',
};
// List running processes
function ls(callback, options) {
if('function' !== typeof callback) {
throw Error('No callback given, or given callback is not a function.')
}
if(!options) {
options=defaultOptions;
}
fs.readdir('/proc', function(err, files) {
err&&callback(err)||files.forEach(function(file){
// Test if the file is a pid
if(/^[0-9]+$/.test(file)) {
// if the user just want the pid and do no grep
if(0===options.datas.length) {
callback(null,{'pid':parseInt(file,10)});
// else reading other datas
} else {
read(file,callback,options);
}
}
});
});
}
// Read details on a particular process
function read(pid, callback, options) {
var process={'pid':pid}, foundDatas=0;
if(!options) {
options=defaultOptions;
}
if(!(options.datas&&options.datas instanceof Array&&options.datas.length)) {
callback(Error('No datas asked.'));
}
// discount data retrieves
function dataFound() {
if(--foundDatas===0)
callback(null,process);
}
// Reading the command line
if(-1!==options.datas.indexOf('cmdline')) {
foundDatas++;
fs.readFile('/proc/'+pid+'/cmdline', 'utf8', function (err, data) {
if(err) {
err&&callback(err);
} else {
process.cmdline=data.split('\u0000');
if(''===process.cmdline[process.cmdline.length-1]) {
process.cmdline.pop();
}
dataFound();
}
});
}
// Reading the env vars
if(-1!==options.datas.indexOf('environ')) {
foundDatas++;
fs.readFile('/proc/'+pid+'/environ', 'utf8', function (err, data) {
if(err) {
err&&callback(err);
} else {
process.env={};
data.split('\u0000').forEach(function(env){
var vals;
if(env) {
vals=env.split('=');
process.env[vals[0]]=vals[1];
}
});
dataFound();
}
});
}
// Reading the process cwd
if(-1!==options.datas.indexOf('cwd')) {
foundDatas++;
fs.readlink('/proc/'+pid+'/cwd', function (err, data) {
if(err) {
err&&callback(err);
} else {
process.cwd=data;
dataFound();
}
});
}
// Reading the process stat
if(-1!==options.datas.indexOf('stat')) {
foundDatas++;
fs.readFile('/proc/'+pid+'/stat', 'utf8', function (err, data) {
if (err) throw err;
var fields=data.split(/[\r\n\s]+/);
process.stat={
'command':fields[1],
'state':fields[2],
'ppid': parseInt(fields[3],10),
'pgrp': parseInt(fields[4],10),
'session': parseInt(fields[5],10),
'tty_nr': parseInt(fields[6],10),
'tpgid': parseInt(fields[7],10),
'flags': parseInt(fields[8],10),
'minflt': parseInt(fields[9],10),
'cminflt': parseInt(fields[10],10),
'majflt': parseInt(fields[11],10),
'cmajflt': parseInt(fields[12],10),
'utime': parseInt(fields[13],10),
'stime': parseInt(fields[14],10),
'cutime': parseInt(fields[15],10),
'cstime': parseInt(fields[16],10),
'priority': parseInt(fields[17],10),
'nice': parseInt(fields[18],10),
'numThreads': parseInt(fields[19],10),
'itrealvalue': parseInt(fields[20],10),
'startTime': parseInt(fields[21],10),
'vsize': parseInt(fields[22],10),
'rss': parseInt(fields[23],10),
'rsslim': parseInt(fields[24],10),
'startCode': parseInt(fields[25],10),
'endCode': parseInt(fields[26],10),
'startStack': parseInt(fields[27],10),
'kstkesp': parseInt(fields[28],10),
'kstkeip': parseInt(fields[29],10),
'signal': parseInt(fields[30],10),
'blocked': parseInt(fields[31],10),
'sigignore': parseInt(fields[32],10),
'sigcatch': parseInt(fields[33],10),
'wchan': parseInt(fields[34],10),
'nswap': parseInt(fields[35],10),
'cnswap': parseInt(fields[36],10),
'exitSignal': parseInt(fields[37],10),
'processor': parseInt(fields[38],10),
'rtPriority': parseInt(fields[39],10),
'policy': parseInt(fields[40],10),
'delaycctBlkioTicks': parseInt(fields[41],10),
'guestTime': parseInt(fields[42],10),
'cguestTime': parseInt(fields[43],10),
}
dataFound();
});
}
// Reading the process statm
if(-1!==options.datas.indexOf('statm')) {
foundDatas++;
fs.readFile('/proc/'+pid+'/statm', 'utf8', function (err, data) {
if (err) throw err;
var fields=data.split(/[\r\n\s]+/);
process.statm={
'size': fields[1],
'resident': fields[2],
'share': parseInt(fields[3],10),
'text': parseInt(fields[4],10),
'lib': parseInt(fields[5],10),
'data': parseInt(fields[5],10),
'dt': parseInt(fields[5],10)
}
dataFound();
});
}
// Checking datas integrity
if(options.datas.length!==foundDatas) {
callback(Error('Asked unknow datas.'))
}
}
/*ls(function(err, process) {
console.log(process);
}, {datas: ['cmdline']});*/
read(4535,function(err, process) {
console.log(process);
}, {datas: ['cmdline','statm','stat','cwd']});