1 |
|
|
// Copyright Joyent, Inc. and other Node contributors. |
2 |
|
|
// |
3 |
|
|
// Permission is hereby granted, free of charge, to any person obtaining a |
4 |
|
|
// copy of this software and associated documentation files (the |
5 |
|
|
// "Software"), to deal in the Software without restriction, including |
6 |
|
|
// without limitation the rights to use, copy, modify, merge, publish, |
7 |
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit |
8 |
|
|
// persons to whom the Software is furnished to do so, subject to the |
9 |
|
|
// following conditions: |
10 |
|
|
// |
11 |
|
|
// The above copyright notice and this permission notice shall be included |
12 |
|
|
// in all copies or substantial portions of the Software. |
13 |
|
|
// |
14 |
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
15 |
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
16 |
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
17 |
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
18 |
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
19 |
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
20 |
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE. |
21 |
|
|
|
22 |
|
|
#include "env-inl.h" |
23 |
|
|
#include "string_bytes.h" |
24 |
|
|
|
25 |
|
|
#ifdef __MINGW32__ |
26 |
|
|
# include <io.h> |
27 |
|
|
#endif // __MINGW32__ |
28 |
|
|
|
29 |
|
|
#ifdef __POSIX__ |
30 |
|
|
# include <unistd.h> // gethostname, sysconf |
31 |
|
|
# include <climits> // PATH_MAX on Solaris. |
32 |
|
|
#endif // __POSIX__ |
33 |
|
|
|
34 |
|
|
#include <array> |
35 |
|
|
#include <cerrno> |
36 |
|
|
#include <cstring> |
37 |
|
|
|
38 |
|
|
namespace node { |
39 |
|
|
namespace os { |
40 |
|
|
|
41 |
|
|
using v8::Array; |
42 |
|
|
using v8::ArrayBuffer; |
43 |
|
|
using v8::Boolean; |
44 |
|
|
using v8::Context; |
45 |
|
|
using v8::Float64Array; |
46 |
|
|
using v8::FunctionCallbackInfo; |
47 |
|
|
using v8::Int32; |
48 |
|
|
using v8::Integer; |
49 |
|
|
using v8::Isolate; |
50 |
|
|
using v8::Local; |
51 |
|
|
using v8::MaybeLocal; |
52 |
|
|
using v8::NewStringType; |
53 |
|
|
using v8::Null; |
54 |
|
|
using v8::Number; |
55 |
|
|
using v8::Object; |
56 |
|
|
using v8::String; |
57 |
|
|
using v8::Value; |
58 |
|
|
|
59 |
|
|
|
60 |
|
29 |
static void GetHostname(const FunctionCallbackInfo<Value>& args) { |
61 |
|
29 |
Environment* env = Environment::GetCurrent(args); |
62 |
|
|
char buf[UV_MAXHOSTNAMESIZE]; |
63 |
|
29 |
size_t size = sizeof(buf); |
64 |
|
29 |
int r = uv_os_gethostname(buf, &size); |
65 |
|
|
|
66 |
✗✓ |
29 |
if (r != 0) { |
67 |
|
|
CHECK_GE(args.Length(), 1); |
68 |
|
|
env->CollectUVExceptionInfo(args[args.Length() - 1], r, |
69 |
|
|
"uv_os_gethostname"); |
70 |
|
|
return args.GetReturnValue().SetUndefined(); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
58 |
args.GetReturnValue().Set( |
74 |
|
58 |
String::NewFromUtf8(env->isolate(), buf).ToLocalChecked()); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
481 |
static void GetOSInformation(const FunctionCallbackInfo<Value>& args) { |
78 |
|
481 |
Environment* env = Environment::GetCurrent(args); |
79 |
|
|
uv_utsname_t info; |
80 |
|
481 |
int err = uv_os_uname(&info); |
81 |
|
|
|
82 |
✗✓ |
481 |
if (err != 0) { |
83 |
|
|
CHECK_GE(args.Length(), 1); |
84 |
|
|
env->CollectUVExceptionInfo(args[args.Length() - 1], err, "uv_os_uname"); |
85 |
|
|
return args.GetReturnValue().SetUndefined(); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
// [sysname, version, release] |
89 |
|
|
Local<Value> osInformation[] = { |
90 |
|
962 |
String::NewFromUtf8(env->isolate(), info.sysname).ToLocalChecked(), |
91 |
|
962 |
String::NewFromUtf8(env->isolate(), info.version).ToLocalChecked(), |
92 |
|
962 |
String::NewFromUtf8(env->isolate(), info.release).ToLocalChecked() |
93 |
|
1924 |
}; |
94 |
|
|
|
95 |
|
1443 |
args.GetReturnValue().Set(Array::New(env->isolate(), |
96 |
|
|
osInformation, |
97 |
|
|
arraysize(osInformation))); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
75 |
static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) { |
101 |
|
75 |
Environment* env = Environment::GetCurrent(args); |
102 |
|
75 |
Isolate* isolate = env->isolate(); |
103 |
|
|
|
104 |
|
|
uv_cpu_info_t* cpu_infos; |
105 |
|
|
int count; |
106 |
|
|
|
107 |
|
75 |
int err = uv_cpu_info(&cpu_infos, &count); |
108 |
✗✓ |
75 |
if (err) |
109 |
|
|
return; |
110 |
|
|
|
111 |
|
|
// It's faster to create an array packed with all the data and |
112 |
|
|
// assemble them into objects in JS than to call Object::Set() repeatedly |
113 |
|
|
// The array is in the format |
114 |
|
|
// [model, speed, (5 entries of cpu_times), model2, speed2, ...] |
115 |
|
150 |
std::vector<Local<Value>> result(count * 7); |
116 |
✓✓ |
6675 |
for (int i = 0, j = 0; i < count; i++) { |
117 |
|
6600 |
uv_cpu_info_t* ci = cpu_infos + i; |
118 |
|
13200 |
result[j++] = OneByteString(isolate, ci->model); |
119 |
|
13200 |
result[j++] = Number::New(isolate, ci->speed); |
120 |
|
13200 |
result[j++] = Number::New(isolate, ci->cpu_times.user); |
121 |
|
13200 |
result[j++] = Number::New(isolate, ci->cpu_times.nice); |
122 |
|
13200 |
result[j++] = Number::New(isolate, ci->cpu_times.sys); |
123 |
|
13200 |
result[j++] = Number::New(isolate, ci->cpu_times.idle); |
124 |
|
13200 |
result[j++] = Number::New(isolate, ci->cpu_times.irq); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
75 |
uv_free_cpu_info(cpu_infos, count); |
128 |
|
225 |
args.GetReturnValue().Set(Array::New(isolate, result.data(), result.size())); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
|
132 |
|
3 |
static void GetFreeMemory(const FunctionCallbackInfo<Value>& args) { |
133 |
|
3 |
double amount = uv_get_free_memory(); |
134 |
|
6 |
args.GetReturnValue().Set(amount); |
135 |
|
3 |
} |
136 |
|
|
|
137 |
|
|
|
138 |
|
72 |
static void GetTotalMemory(const FunctionCallbackInfo<Value>& args) { |
139 |
|
72 |
double amount = uv_get_total_memory(); |
140 |
|
144 |
args.GetReturnValue().Set(amount); |
141 |
|
72 |
} |
142 |
|
|
|
143 |
|
|
|
144 |
|
3 |
static void GetUptime(const FunctionCallbackInfo<Value>& args) { |
145 |
|
|
double uptime; |
146 |
|
3 |
int err = uv_uptime(&uptime); |
147 |
✓✗ |
3 |
if (err == 0) |
148 |
|
9 |
args.GetReturnValue().Set(uptime); |
149 |
|
3 |
} |
150 |
|
|
|
151 |
|
|
|
152 |
|
1 |
static void GetLoadAvg(const FunctionCallbackInfo<Value>& args) { |
153 |
✗✓ |
2 |
CHECK(args[0]->IsFloat64Array()); |
154 |
|
2 |
Local<Float64Array> array = args[0].As<Float64Array>(); |
155 |
✗✓ |
1 |
CHECK_EQ(array->Length(), 3); |
156 |
|
1 |
Local<ArrayBuffer> ab = array->Buffer(); |
157 |
|
1 |
double* loadavg = static_cast<double*>(ab->GetBackingStore()->Data()); |
158 |
|
1 |
uv_loadavg(loadavg); |
159 |
|
1 |
} |
160 |
|
|
|
161 |
|
|
|
162 |
|
107 |
static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) { |
163 |
|
107 |
Environment* env = Environment::GetCurrent(args); |
164 |
|
107 |
Isolate* isolate = env->isolate(); |
165 |
|
|
uv_interface_address_t* interfaces; |
166 |
|
|
int count, i; |
167 |
|
|
char ip[INET6_ADDRSTRLEN]; |
168 |
|
|
char netmask[INET6_ADDRSTRLEN]; |
169 |
|
|
std::array<char, 18> mac; |
170 |
|
|
Local<String> name, family; |
171 |
|
|
|
172 |
|
107 |
int err = uv_interface_addresses(&interfaces, &count); |
173 |
|
|
|
174 |
✗✓ |
107 |
if (err == UV_ENOSYS) |
175 |
|
|
return args.GetReturnValue().SetUndefined(); |
176 |
|
|
|
177 |
✗✓ |
107 |
if (err) { |
178 |
|
|
CHECK_GE(args.Length(), 1); |
179 |
|
|
env->CollectUVExceptionInfo(args[args.Length() - 1], errno, |
180 |
|
|
"uv_interface_addresses"); |
181 |
|
|
return args.GetReturnValue().SetUndefined(); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
107 |
Local<Value> no_scope_id = Integer::New(isolate, -1); |
185 |
|
214 |
std::vector<Local<Value>> result(count * 7); |
186 |
✓✓ |
535 |
for (i = 0; i < count; i++) { |
187 |
|
428 |
const char* const raw_name = interfaces[i].name; |
188 |
|
|
|
189 |
|
|
// Use UTF-8 on both Windows and Unixes (While it may be true that UNIX |
190 |
|
|
// systems are somewhat encoding-agnostic here, it’s more than reasonable |
191 |
|
|
// to assume UTF8 as the default as well. It’s what people will expect if |
192 |
|
|
// they name the interface from any input that uses UTF-8, which should be |
193 |
|
|
// the most frequent case by far these days.) |
194 |
|
856 |
name = String::NewFromUtf8(isolate, raw_name).ToLocalChecked(); |
195 |
|
|
|
196 |
|
2568 |
snprintf(mac.data(), |
197 |
|
|
mac.size(), |
198 |
|
|
"%02x:%02x:%02x:%02x:%02x:%02x", |
199 |
|
428 |
static_cast<unsigned char>(interfaces[i].phys_addr[0]), |
200 |
|
428 |
static_cast<unsigned char>(interfaces[i].phys_addr[1]), |
201 |
|
428 |
static_cast<unsigned char>(interfaces[i].phys_addr[2]), |
202 |
|
428 |
static_cast<unsigned char>(interfaces[i].phys_addr[3]), |
203 |
|
428 |
static_cast<unsigned char>(interfaces[i].phys_addr[4]), |
204 |
|
856 |
static_cast<unsigned char>(interfaces[i].phys_addr[5])); |
205 |
|
|
|
206 |
✓✓ |
428 |
if (interfaces[i].address.address4.sin_family == AF_INET) { |
207 |
|
214 |
uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip)); |
208 |
|
214 |
uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask)); |
209 |
|
214 |
family = env->ipv4_string(); |
210 |
✓✗ |
214 |
} else if (interfaces[i].address.address4.sin_family == AF_INET6) { |
211 |
|
214 |
uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip)); |
212 |
|
214 |
uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask)); |
213 |
|
214 |
family = env->ipv6_string(); |
214 |
|
|
} else { |
215 |
|
|
strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN); |
216 |
|
|
family = env->unknown_string(); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
856 |
result[i * 7] = name; |
220 |
|
856 |
result[i * 7 + 1] = OneByteString(isolate, ip); |
221 |
|
856 |
result[i * 7 + 2] = OneByteString(isolate, netmask); |
222 |
|
856 |
result[i * 7 + 3] = family; |
223 |
|
856 |
result[i * 7 + 4] = FIXED_ONE_BYTE_STRING(isolate, mac); |
224 |
|
856 |
result[i * 7 + 5] = |
225 |
✓✓ |
856 |
interfaces[i].is_internal ? True(isolate) : False(isolate); |
226 |
✓✓ |
428 |
if (interfaces[i].address.address4.sin_family == AF_INET6) { |
227 |
|
214 |
uint32_t scopeid = interfaces[i].address.address6.sin6_scope_id; |
228 |
|
428 |
result[i * 7 + 6] = Integer::NewFromUnsigned(isolate, scopeid); |
229 |
|
|
} else { |
230 |
|
214 |
result[i * 7 + 6] = no_scope_id; |
231 |
|
|
} |
232 |
|
|
} |
233 |
|
|
|
234 |
|
107 |
uv_free_interface_addresses(interfaces, count); |
235 |
|
321 |
args.GetReturnValue().Set(Array::New(isolate, result.data(), result.size())); |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
|
239 |
|
287 |
static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) { |
240 |
|
287 |
Environment* env = Environment::GetCurrent(args); |
241 |
|
|
char buf[PATH_MAX]; |
242 |
|
|
|
243 |
|
287 |
size_t len = sizeof(buf); |
244 |
|
287 |
const int err = uv_os_homedir(buf, &len); |
245 |
|
|
|
246 |
✗✓ |
287 |
if (err) { |
247 |
|
|
CHECK_GE(args.Length(), 1); |
248 |
|
|
env->CollectUVExceptionInfo(args[args.Length() - 1], err, "uv_os_homedir"); |
249 |
|
|
return args.GetReturnValue().SetUndefined(); |
250 |
|
|
} |
251 |
|
|
|
252 |
|
574 |
Local<String> home = String::NewFromUtf8(env->isolate(), |
253 |
|
|
buf, |
254 |
|
|
NewStringType::kNormal, |
255 |
|
574 |
len).ToLocalChecked(); |
256 |
|
574 |
args.GetReturnValue().Set(home); |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
|
260 |
|
3 |
static void GetUserInfo(const FunctionCallbackInfo<Value>& args) { |
261 |
|
3 |
Environment* env = Environment::GetCurrent(args); |
262 |
|
|
uv_passwd_t pwd; |
263 |
|
|
enum encoding encoding; |
264 |
|
|
|
265 |
✓✓ |
6 |
if (args[0]->IsObject()) { |
266 |
|
4 |
Local<Object> options = args[0].As<Object>(); |
267 |
|
|
MaybeLocal<Value> maybe_encoding = options->Get(env->context(), |
268 |
|
6 |
env->encoding_string()); |
269 |
|
|
Local<Value> encoding_opt; |
270 |
✓✓ |
2 |
if (!maybe_encoding.ToLocal(&encoding_opt)) |
271 |
|
1 |
return; |
272 |
|
|
|
273 |
|
1 |
encoding = ParseEncoding(env->isolate(), encoding_opt, UTF8); |
274 |
|
|
} else { |
275 |
|
1 |
encoding = UTF8; |
276 |
|
|
} |
277 |
|
|
|
278 |
|
2 |
const int err = uv_os_get_passwd(&pwd); |
279 |
|
|
|
280 |
✗✓ |
2 |
if (err) { |
281 |
|
|
CHECK_GE(args.Length(), 2); |
282 |
|
|
env->CollectUVExceptionInfo(args[args.Length() - 1], err, |
283 |
|
|
"uv_os_get_passwd"); |
284 |
|
|
return args.GetReturnValue().SetUndefined(); |
285 |
|
|
} |
286 |
|
|
|
287 |
|
6 |
auto free_passwd = OnScopeLeave([&]() { uv_os_free_passwd(&pwd); }); |
288 |
|
|
|
289 |
|
|
Local<Value> error; |
290 |
|
|
|
291 |
|
2 |
Local<Value> uid = Number::New(env->isolate(), pwd.uid); |
292 |
|
2 |
Local<Value> gid = Number::New(env->isolate(), pwd.gid); |
293 |
|
|
MaybeLocal<Value> username = StringBytes::Encode(env->isolate(), |
294 |
|
2 |
pwd.username, |
295 |
|
|
encoding, |
296 |
|
2 |
&error); |
297 |
|
|
MaybeLocal<Value> homedir = StringBytes::Encode(env->isolate(), |
298 |
|
2 |
pwd.homedir, |
299 |
|
|
encoding, |
300 |
|
2 |
&error); |
301 |
|
|
MaybeLocal<Value> shell; |
302 |
|
|
|
303 |
✗✓ |
2 |
if (pwd.shell == nullptr) |
304 |
|
|
shell = Null(env->isolate()); |
305 |
|
|
else |
306 |
|
2 |
shell = StringBytes::Encode(env->isolate(), pwd.shell, encoding, &error); |
307 |
|
|
|
308 |
✓✗✓✗ ✗✓✗✓
|
6 |
if (username.IsEmpty() || homedir.IsEmpty() || shell.IsEmpty()) { |
309 |
|
|
CHECK(!error.IsEmpty()); |
310 |
|
|
env->isolate()->ThrowException(error); |
311 |
|
|
return; |
312 |
|
|
} |
313 |
|
|
|
314 |
|
2 |
Local<Object> entry = Object::New(env->isolate()); |
315 |
|
|
|
316 |
|
8 |
entry->Set(env->context(), env->uid_string(), uid).Check(); |
317 |
|
8 |
entry->Set(env->context(), env->gid_string(), gid).Check(); |
318 |
|
4 |
entry->Set(env->context(), |
319 |
|
|
env->username_string(), |
320 |
|
8 |
username.ToLocalChecked()).Check(); |
321 |
|
4 |
entry->Set(env->context(), |
322 |
|
|
env->homedir_string(), |
323 |
|
8 |
homedir.ToLocalChecked()).Check(); |
324 |
|
4 |
entry->Set(env->context(), |
325 |
|
|
env->shell_string(), |
326 |
|
8 |
shell.ToLocalChecked()).Check(); |
327 |
|
|
|
328 |
✓✗ |
4 |
args.GetReturnValue().Set(entry); |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
|
332 |
|
80 |
static void SetPriority(const FunctionCallbackInfo<Value>& args) { |
333 |
|
80 |
Environment* env = Environment::GetCurrent(args); |
334 |
|
|
|
335 |
✗✓ |
80 |
CHECK_EQ(args.Length(), 3); |
336 |
✗✓ |
160 |
CHECK(args[0]->IsInt32()); |
337 |
✗✓ |
160 |
CHECK(args[1]->IsInt32()); |
338 |
|
|
|
339 |
|
240 |
const int pid = args[0].As<Int32>()->Value(); |
340 |
|
240 |
const int priority = args[1].As<Int32>()->Value(); |
341 |
|
80 |
const int err = uv_os_setpriority(pid, priority); |
342 |
|
|
|
343 |
✓✓ |
80 |
if (err) { |
344 |
✗✓ |
40 |
CHECK(args[2]->IsObject()); |
345 |
|
20 |
env->CollectUVExceptionInfo(args[2], err, "uv_os_setpriority"); |
346 |
|
|
} |
347 |
|
|
|
348 |
|
160 |
args.GetReturnValue().Set(err); |
349 |
|
80 |
} |
350 |
|
|
|
351 |
|
|
|
352 |
|
60 |
static void GetPriority(const FunctionCallbackInfo<Value>& args) { |
353 |
|
60 |
Environment* env = Environment::GetCurrent(args); |
354 |
|
|
|
355 |
✗✓ |
60 |
CHECK_EQ(args.Length(), 2); |
356 |
✗✓ |
120 |
CHECK(args[0]->IsInt32()); |
357 |
|
|
|
358 |
|
180 |
const int pid = args[0].As<Int32>()->Value(); |
359 |
|
|
int priority; |
360 |
|
60 |
const int err = uv_os_getpriority(pid, &priority); |
361 |
|
|
|
362 |
✗✓ |
60 |
if (err) { |
363 |
|
|
CHECK(args[1]->IsObject()); |
364 |
|
|
env->CollectUVExceptionInfo(args[1], err, "uv_os_getpriority"); |
365 |
|
|
return; |
366 |
|
|
} |
367 |
|
|
|
368 |
|
180 |
args.GetReturnValue().Set(priority); |
369 |
|
|
} |
370 |
|
|
|
371 |
|
|
|
372 |
|
482 |
void Initialize(Local<Object> target, |
373 |
|
|
Local<Value> unused, |
374 |
|
|
Local<Context> context, |
375 |
|
|
void* priv) { |
376 |
|
482 |
Environment* env = Environment::GetCurrent(context); |
377 |
|
482 |
env->SetMethod(target, "getHostname", GetHostname); |
378 |
|
482 |
env->SetMethod(target, "getLoadAvg", GetLoadAvg); |
379 |
|
482 |
env->SetMethod(target, "getUptime", GetUptime); |
380 |
|
482 |
env->SetMethod(target, "getTotalMem", GetTotalMemory); |
381 |
|
482 |
env->SetMethod(target, "getFreeMem", GetFreeMemory); |
382 |
|
482 |
env->SetMethod(target, "getCPUs", GetCPUInfo); |
383 |
|
482 |
env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses); |
384 |
|
482 |
env->SetMethod(target, "getHomeDirectory", GetHomeDirectory); |
385 |
|
482 |
env->SetMethod(target, "getUserInfo", GetUserInfo); |
386 |
|
482 |
env->SetMethod(target, "setPriority", SetPriority); |
387 |
|
482 |
env->SetMethod(target, "getPriority", GetPriority); |
388 |
|
482 |
env->SetMethod(target, "getOSInformation", GetOSInformation); |
389 |
|
964 |
target->Set(env->context(), |
390 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "isBigEndian"), |
391 |
|
2410 |
Boolean::New(env->isolate(), IsBigEndian())).Check(); |
392 |
|
482 |
} |
393 |
|
|
|
394 |
|
|
} // namespace os |
395 |
|
|
} // namespace node |
396 |
|
|
|
397 |
✓✗✓✗
|
18704 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(os, node::os::Initialize) |