Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_os.cc
Go to the documentation of this file.
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 "node.h"
23 #include "v8.h"
24 #include "env.h"
25 #include "env-inl.h"
26 #include "string_bytes.h"
27 
28 #include <errno.h>
29 #include <string.h>
30 
31 #ifdef __MINGW32__
32 # include <io.h>
33 #endif // __MINGW32__
34 
35 #ifdef __POSIX__
36 # include <limits.h> // PATH_MAX on Solaris.
37 # include <netdb.h> // MAXHOSTNAMELEN on Solaris.
38 # include <unistd.h> // gethostname, sysconf
39 # include <sys/param.h> // MAXHOSTNAMELEN on Linux and the BSDs.
40 # include <sys/utsname.h>
41 #endif // __POSIX__
42 
43 // Add Windows fallback.
44 #ifndef MAXHOSTNAMELEN
45 # define MAXHOSTNAMELEN 256
46 #endif // MAXHOSTNAMELEN
47 
48 namespace node {
49 namespace os {
50 
51 using v8::Array;
52 using v8::ArrayBuffer;
53 using v8::Boolean;
54 using v8::Context;
55 using v8::Float64Array;
56 using v8::Function;
57 using v8::FunctionCallbackInfo;
58 using v8::Integer;
59 using v8::Local;
60 using v8::MaybeLocal;
61 using v8::Null;
62 using v8::Number;
63 using v8::Object;
64 using v8::String;
65 using v8::Value;
66 
67 
68 static void GetHostname(const FunctionCallbackInfo<Value>& args) {
69  Environment* env = Environment::GetCurrent(args);
70  char buf[MAXHOSTNAMELEN + 1];
71 
72  if (gethostname(buf, sizeof(buf))) {
73 #ifdef __POSIX__
74  int errorno = errno;
75 #else // __MINGW32__
76  int errorno = WSAGetLastError();
77 #endif // __POSIX__
78  return env->ThrowErrnoException(errorno, "gethostname");
79  }
80  buf[sizeof(buf) - 1] = '\0';
81 
82  args.GetReturnValue().Set(OneByteString(env->isolate(), buf));
83 }
84 
85 
86 static void GetOSType(const FunctionCallbackInfo<Value>& args) {
87  Environment* env = Environment::GetCurrent(args);
88  const char* rval;
89 
90 #ifdef __POSIX__
91  struct utsname info;
92  if (uname(&info) < 0) {
93  return env->ThrowErrnoException(errno, "uname");
94  }
95  rval = info.sysname;
96 #else // __MINGW32__
97  rval = "Windows_NT";
98 #endif // __POSIX__
99 
100  args.GetReturnValue().Set(OneByteString(env->isolate(), rval));
101 }
102 
103 
104 static void GetOSRelease(const FunctionCallbackInfo<Value>& args) {
105  Environment* env = Environment::GetCurrent(args);
106  const char* rval;
107 
108 #ifdef __POSIX__
109  struct utsname info;
110  if (uname(&info) < 0) {
111  return env->ThrowErrnoException(errno, "uname");
112  }
113 # ifdef _AIX
114  char release[256];
115  snprintf(release, sizeof(release),
116  "%s.%s", info.version, info.release);
117  rval = release;
118 # else
119  rval = info.release;
120 # endif
121 #else // Windows
122  char release[256];
123  OSVERSIONINFOW info;
124 
125  info.dwOSVersionInfoSize = sizeof(info);
126 
127  // Don't complain that GetVersionEx is deprecated; there is no alternative.
128  #pragma warning(suppress : 4996)
129  if (GetVersionExW(&info) == 0)
130  return;
131 
132  snprintf(release,
133  sizeof(release),
134  "%d.%d.%d",
135  static_cast<int>(info.dwMajorVersion),
136  static_cast<int>(info.dwMinorVersion),
137  static_cast<int>(info.dwBuildNumber));
138  rval = release;
139 #endif // __POSIX__
140 
141  args.GetReturnValue().Set(OneByteString(env->isolate(), rval));
142 }
143 
144 
145 static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) {
146  Environment* env = Environment::GetCurrent(args);
147  uv_cpu_info_t* cpu_infos;
148  int count, i, field_idx;
149 
150  int err = uv_cpu_info(&cpu_infos, &count);
151  if (err)
152  return;
153 
154  CHECK(args[0]->IsFunction());
155  Local<Function> addfn = args[0].As<Function>();
156 
157  CHECK(args[1]->IsFloat64Array());
158  Local<Float64Array> array = args[1].As<Float64Array>();
159  CHECK_EQ(array->Length(), 6 * NODE_PUSH_VAL_TO_ARRAY_MAX);
160  Local<ArrayBuffer> ab = array->Buffer();
161  double* fields = static_cast<double*>(ab->GetContents().Data());
162 
163  CHECK(args[2]->IsArray());
164  Local<Array> cpus = args[2].As<Array>();
165 
166  Local<Value> model_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
167  int model_idx = 0;
168 
169  for (i = 0, field_idx = 0; i < count; i++) {
170  uv_cpu_info_t* ci = cpu_infos + i;
171 
172  fields[field_idx++] = ci->speed;
173  fields[field_idx++] = ci->cpu_times.user;
174  fields[field_idx++] = ci->cpu_times.nice;
175  fields[field_idx++] = ci->cpu_times.sys;
176  fields[field_idx++] = ci->cpu_times.idle;
177  fields[field_idx++] = ci->cpu_times.irq;
178  model_argv[model_idx++] = OneByteString(env->isolate(), ci->model);
179 
180  if (model_idx >= NODE_PUSH_VAL_TO_ARRAY_MAX) {
181  addfn->Call(env->context(), cpus, model_idx, model_argv).ToLocalChecked();
182  model_idx = 0;
183  field_idx = 0;
184  }
185  }
186 
187  if (model_idx > 0) {
188  addfn->Call(env->context(), cpus, model_idx, model_argv).ToLocalChecked();
189  }
190 
191  uv_free_cpu_info(cpu_infos, count);
192  args.GetReturnValue().Set(cpus);
193 }
194 
195 
196 static void GetFreeMemory(const FunctionCallbackInfo<Value>& args) {
197  double amount = uv_get_free_memory();
198  if (amount < 0)
199  return;
200  args.GetReturnValue().Set(amount);
201 }
202 
203 
204 static void GetTotalMemory(const FunctionCallbackInfo<Value>& args) {
205  double amount = uv_get_total_memory();
206  if (amount < 0)
207  return;
208  args.GetReturnValue().Set(amount);
209 }
210 
211 
212 static void GetUptime(const FunctionCallbackInfo<Value>& args) {
213  double uptime;
214  int err = uv_uptime(&uptime);
215  if (err == 0)
216  args.GetReturnValue().Set(uptime);
217 }
218 
219 
220 static void GetLoadAvg(const FunctionCallbackInfo<Value>& args) {
221  CHECK(args[0]->IsFloat64Array());
222  Local<Float64Array> array = args[0].As<Float64Array>();
223  CHECK_EQ(array->Length(), 3);
224  Local<ArrayBuffer> ab = array->Buffer();
225  double* loadavg = static_cast<double*>(ab->GetContents().Data());
226  uv_loadavg(loadavg);
227 }
228 
229 
230 static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
231  Environment* env = Environment::GetCurrent(args);
232  uv_interface_address_t* interfaces;
233  int count, i;
234  char ip[INET6_ADDRSTRLEN];
235  char netmask[INET6_ADDRSTRLEN];
236  char mac[18];
237  Local<Object> ret, o;
238  Local<String> name, family;
239  Local<Array> ifarr;
240 
241  int err = uv_interface_addresses(&interfaces, &count);
242 
243  ret = Object::New(env->isolate());
244 
245  if (err == UV_ENOSYS) {
246  return args.GetReturnValue().Set(ret);
247  } else if (err) {
248  return env->ThrowUVException(err, "uv_interface_addresses");
249  }
250 
251  for (i = 0; i < count; i++) {
252  const char* const raw_name = interfaces[i].name;
253 
254  // On Windows, the interface name is the UTF8-encoded friendly name and may
255  // contain non-ASCII characters. On UNIX, it's just a binary string with
256  // no particular encoding but we treat it as a one-byte Latin-1 string.
257 #ifdef _WIN32
258  name = String::NewFromUtf8(env->isolate(), raw_name);
259 #else
260  name = OneByteString(env->isolate(), raw_name);
261 #endif
262 
263  if (ret->Has(env->context(), name).FromJust()) {
264  ifarr = Local<Array>::Cast(ret->Get(name));
265  } else {
266  ifarr = Array::New(env->isolate());
267  ret->Set(name, ifarr);
268  }
269 
270  snprintf(mac,
271  18,
272  "%02x:%02x:%02x:%02x:%02x:%02x",
273  static_cast<unsigned char>(interfaces[i].phys_addr[0]),
274  static_cast<unsigned char>(interfaces[i].phys_addr[1]),
275  static_cast<unsigned char>(interfaces[i].phys_addr[2]),
276  static_cast<unsigned char>(interfaces[i].phys_addr[3]),
277  static_cast<unsigned char>(interfaces[i].phys_addr[4]),
278  static_cast<unsigned char>(interfaces[i].phys_addr[5]));
279 
280  if (interfaces[i].address.address4.sin_family == AF_INET) {
281  uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip));
282  uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask));
283  family = env->ipv4_string();
284  } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
285  uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
286  uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask));
287  family = env->ipv6_string();
288  } else {
289  strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
290  family = env->unknown_string();
291  }
292 
293  o = Object::New(env->isolate());
294  o->Set(env->address_string(), OneByteString(env->isolate(), ip));
295  o->Set(env->netmask_string(), OneByteString(env->isolate(), netmask));
296  o->Set(env->family_string(), family);
297  o->Set(env->mac_string(), FIXED_ONE_BYTE_STRING(env->isolate(), mac));
298 
299  if (interfaces[i].address.address4.sin_family == AF_INET6) {
300  uint32_t scopeid = interfaces[i].address.address6.sin6_scope_id;
301  o->Set(env->scopeid_string(),
302  Integer::NewFromUnsigned(env->isolate(), scopeid));
303  }
304 
305  const bool internal = interfaces[i].is_internal;
306  o->Set(env->internal_string(),
307  internal ? True(env->isolate()) : False(env->isolate()));
308 
309  ifarr->Set(ifarr->Length(), o);
310  }
311 
312  uv_free_interface_addresses(interfaces, count);
313  args.GetReturnValue().Set(ret);
314 }
315 
316 
317 static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
318  Environment* env = Environment::GetCurrent(args);
319  char buf[PATH_MAX];
320 
321  size_t len = sizeof(buf);
322  const int err = uv_os_homedir(buf, &len);
323 
324  if (err) {
325  return env->ThrowUVException(err, "uv_os_homedir");
326  }
327 
328  Local<String> home = String::NewFromUtf8(env->isolate(),
329  buf,
330  String::kNormalString,
331  len);
332  args.GetReturnValue().Set(home);
333 }
334 
335 
336 static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
337  Environment* env = Environment::GetCurrent(args);
338  uv_passwd_t pwd;
339  enum encoding encoding;
340 
341  if (args[0]->IsObject()) {
342  Local<Object> options = args[0].As<Object>();
343  MaybeLocal<Value> maybe_encoding = options->Get(env->context(),
344  env->encoding_string());
345  if (maybe_encoding.IsEmpty())
346  return;
347 
348  Local<Value> encoding_opt = maybe_encoding.ToLocalChecked();
349  encoding = ParseEncoding(env->isolate(), encoding_opt, UTF8);
350  } else {
351  encoding = UTF8;
352  }
353 
354  const int err = uv_os_get_passwd(&pwd);
355 
356  if (err) {
357  return env->ThrowUVException(err, "uv_os_get_passwd");
358  }
359 
360  Local<Value> error;
361 
362  Local<Value> uid = Number::New(env->isolate(), pwd.uid);
363  Local<Value> gid = Number::New(env->isolate(), pwd.gid);
364  MaybeLocal<Value> username = StringBytes::Encode(env->isolate(),
365  pwd.username,
366  encoding,
367  &error);
368  MaybeLocal<Value> homedir = StringBytes::Encode(env->isolate(),
369  pwd.homedir,
370  encoding,
371  &error);
372  MaybeLocal<Value> shell;
373 
374  if (pwd.shell == NULL)
375  shell = Null(env->isolate());
376  else
377  shell = StringBytes::Encode(env->isolate(), pwd.shell, encoding, &error);
378 
379  uv_os_free_passwd(&pwd);
380 
381  if (username.IsEmpty()) {
382  // TODO(addaleax): Use `error` itself here.
383  return env->ThrowUVException(UV_EINVAL,
384  "uv_os_get_passwd",
385  "Invalid character encoding for username");
386  }
387 
388  if (homedir.IsEmpty()) {
389  // TODO(addaleax): Use `error` itself here.
390  return env->ThrowUVException(UV_EINVAL,
391  "uv_os_get_passwd",
392  "Invalid character encoding for homedir");
393  }
394 
395  if (shell.IsEmpty()) {
396  // TODO(addaleax): Use `error` itself here.
397  return env->ThrowUVException(UV_EINVAL,
398  "uv_os_get_passwd",
399  "Invalid character encoding for shell");
400  }
401 
402  Local<Object> entry = Object::New(env->isolate());
403 
404  entry->Set(env->uid_string(), uid);
405  entry->Set(env->gid_string(), gid);
406  entry->Set(env->username_string(), username.ToLocalChecked());
407  entry->Set(env->homedir_string(), homedir.ToLocalChecked());
408  entry->Set(env->shell_string(), shell.ToLocalChecked());
409 
410  args.GetReturnValue().Set(entry);
411 }
412 
413 
414 void Initialize(Local<Object> target,
415  Local<Value> unused,
416  Local<Context> context) {
417  Environment* env = Environment::GetCurrent(context);
418  env->SetMethod(target, "getHostname", GetHostname);
419  env->SetMethod(target, "getLoadAvg", GetLoadAvg);
420  env->SetMethod(target, "getUptime", GetUptime);
421  env->SetMethod(target, "getTotalMem", GetTotalMemory);
422  env->SetMethod(target, "getFreeMem", GetFreeMemory);
423  env->SetMethod(target, "getCPUs", GetCPUInfo);
424  env->SetMethod(target, "getOSType", GetOSType);
425  env->SetMethod(target, "getOSRelease", GetOSRelease);
426  env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses);
427  env->SetMethod(target, "getHomeDirectory", GetHomeDirectory);
428  env->SetMethod(target, "getUserInfo", GetUserInfo);
429  target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "isBigEndian"),
430  Boolean::New(env->isolate(), IsBigEndian()));
431 }
432 
433 } // namespace os
434 } // namespace node
435 
unsigned char * buf
Definition: cares_wrap.cc:483
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, node::inspector::Agent::InitInspector)
int len
Definition: cares_wrap.cc:485
#define MAXHOSTNAMELEN
Definition: node_os.cc:45
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context)
Definition: node_os.cc:414
encoding
Definition: node.h:322
enum encoding ParseEncoding(const char *encoding, enum encoding default_encoding)
Definition: node.cc:1485
dtrace o
Definition: v8ustack.d:531
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
Definition: node_buffer.cc:241
NODE_DEPRECATED("Use ParseEncoding(isolate, ...)", inline enum encoding ParseEncoding(v8::Local< v8::Value > encoding_v, enum encoding default_encoding=LATIN1) { return ParseEncoding(v8::Isolate::GetCurrent(), encoding_v, default_encoding);}) NODE_EXTERN void FatalException(v8 NODE_DEPRECATED("Use FatalException(isolate, ...)", inline void FatalException(const v8::TryCatch &try_catch) { return FatalException(v8::Isolate::GetCurrent(), try_catch);}) NODE_EXTERN v8 NODE_EXTERN v8::Local< v8::Value > Encode(v8::Isolate *isolate, const uint16_t *buf, size_t len)
Definition: node.h:350