1
1
#include " worker.h"
2
2
#include < libproc.h>
3
3
4
- using namespace Nan ;
5
-
6
- Worker::Worker (Callback* callback) : AsyncWorker(callback) {
4
+ Worker::Worker (Napi::Function &callback) : AsyncWorker(callback)
5
+ {
7
6
this ->processes = new kinfo_proc[1024 ];
8
7
processCount = new uint32_t ;
9
8
}
10
9
11
- Worker::~Worker () {
12
- delete[] this ->processes ;
13
- delete processCount;
10
+ Worker::~Worker ()
11
+ {
12
+ delete[] this ->processes ;
13
+ delete processCount;
14
14
}
15
15
16
- void Worker::Execute () {
16
+ void Worker::Execute ()
17
+ {
17
18
bool done;
18
19
int err;
19
20
size_t length = 0 ;
20
- static const int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
21
+ static const int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 };
21
22
22
23
*this ->processCount = 0 ;
23
24
@@ -33,69 +34,77 @@ void Worker::Execute() {
33
34
34
35
this ->processes = NULL ;
35
36
done = false ;
36
- do {
37
+ do
38
+ {
37
39
assert (this ->processes == NULL );
38
40
39
41
// Call sysctl with a NULL buffer.
40
42
41
43
length = 0 ;
42
- err = sysctl ((int *)name, sizeof (name) / sizeof (*name) - 1 , NULL , &length, NULL , 0 );
43
- if (err == -1 ) {
44
+ err = sysctl ((int *)name, sizeof (name) / sizeof (*name) - 1 , NULL , &length, NULL , 0 );
45
+ if (err == -1 )
46
+ {
44
47
err = errno;
45
48
}
46
49
47
50
// Allocate an appropriately sized buffer based on the results
48
51
// from the previous call.
49
52
50
- if (err == 0 ) {
51
- this ->processes = (kinfo_proc*)malloc (length);
53
+ if (err == 0 )
54
+ {
55
+ this ->processes = (kinfo_proc *)malloc (length);
52
56
}
53
57
54
58
// Call sysctl again with the new buffer. If we get an ENOMEM
55
59
// error, toss away our buffer and start again.
56
60
57
- if (err == 0 ) {
58
- err = sysctl ((int *)name, sizeof (name) / sizeof (*name) - 1 , this ->processes , &length, NULL , 0 );
59
- if (err == -1 ) {
61
+ if (err == 0 )
62
+ {
63
+ err = sysctl ((int *)name, sizeof (name) / sizeof (*name) - 1 , this ->processes , &length, NULL , 0 );
64
+ if (err == -1 )
65
+ {
60
66
err = errno;
61
67
}
62
- if (err == 0 ) {
68
+ if (err == 0 )
69
+ {
63
70
done = true ;
64
- } else if (err == ENOMEM) {
71
+ }
72
+ else if (err == ENOMEM)
73
+ {
65
74
assert (this ->processes != NULL );
66
75
free (this ->processes );
67
76
this ->processes = NULL ;
68
77
err = 0 ;
69
78
}
70
79
}
71
- } while (err == 0 && ! done);
80
+ } while (err == 0 && !done);
72
81
73
- if (err == 0 ) {
82
+ if (err == 0 )
83
+ {
74
84
*this ->processCount = length / sizeof (kinfo_proc);
75
85
}
76
86
}
77
87
78
88
#define NAME_BUFFER_SIZE 4096
79
89
80
- void Worker::HandleOKCallback () {
81
- HandleScope scope;
82
-
83
- v8::Local<v8::Array> result = New<v8::Array>(*processCount);
84
- for (uint32_t i = 0 ; i < *processCount; i++) {
85
- v8::Local<v8::Object> object = New<v8::Object>();
86
-
87
- char buffer[NAME_BUFFER_SIZE];
88
- proc_name (this ->processes [i].kp_proc .p_pid , buffer, NAME_BUFFER_SIZE);
89
- Set (object, New<v8::String>(" name" ).ToLocalChecked (), New<v8::String>(buffer).ToLocalChecked ());
90
- proc_pidpath (this ->processes [i].kp_proc .p_pid , buffer, NAME_BUFFER_SIZE);
91
- Set (object, New<v8::String>(" path" ).ToLocalChecked (), New<v8::String>(buffer).ToLocalChecked ());
92
- Set (object, New<v8::String>(" pid" ).ToLocalChecked (), New<v8::Number>(this ->processes [i].kp_proc .p_pid ));
93
- Set (object, New<v8::String>(" ppid" ).ToLocalChecked (), New<v8::Number>(this ->processes [i].kp_eproc .e_ppid ));
94
- Set (object, New<v8::String>(" pgid" ).ToLocalChecked (), New<v8::Number>(this ->processes [i].kp_eproc .e_pgid ));
95
- Set (result, i, New<v8::Value>(object));
96
- }
97
-
98
- v8::Local<v8::Value> argv[] = { result };
99
- AsyncResource resource (" macos-native-processlist:addon.HandleOKCallback" );
100
- callback->Call (1 , argv, &resource);
90
+ void Worker::OnOK ()
91
+ {
92
+ auto env = this ->Env ();
93
+ auto result = Napi::Array::New (env);
94
+ for (uint32_t i = 0 ; i < *processCount; i++)
95
+ {
96
+ auto object = Napi::Object::New (env);
97
+
98
+ char buffer[NAME_BUFFER_SIZE];
99
+ proc_name (this ->processes [i].kp_proc .p_pid , buffer, NAME_BUFFER_SIZE);
100
+ object.Set (" name" , Napi::String::New (env, buffer));
101
+ proc_pidpath (this ->processes [i].kp_proc .p_pid , buffer, NAME_BUFFER_SIZE);
102
+ object.Set (" path" , Napi::String::New (env, buffer));
103
+ object.Set (" pid" , Napi::Number::New (env, this ->processes [i].kp_proc .p_pid ));
104
+ object.Set (" ppid" , Napi::Number::New (env, this ->processes [i].kp_eproc .e_ppid ));
105
+ object.Set (" pgid" , Napi::Number::New (env, this ->processes [i].kp_eproc .e_pgid ));
106
+ result.Set (i, object);
107
+ }
108
+
109
+ this ->Callback ().Call ({result});
101
110
}
0 commit comments