GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_os.cc Lines: 171 200 85.5 %
Date: 2020-07-19 22:14:24 Branches: 42 82 51.2 %

Line Branch Exec Source
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
407
static void GetOSInformation(const FunctionCallbackInfo<Value>& args) {
78
407
  Environment* env = Environment::GetCurrent(args);
79
  uv_utsname_t info;
80
407
  int err = uv_os_uname(&info);
81
82
407
  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
814
    String::NewFromUtf8(env->isolate(), info.sysname).ToLocalChecked(),
91
814
    String::NewFromUtf8(env->isolate(), info.version).ToLocalChecked(),
92
814
    String::NewFromUtf8(env->isolate(), info.release).ToLocalChecked()
93
1628
  };
94
95
1221
  args.GetReturnValue().Set(Array::New(env->isolate(),
96
                                       osInformation,
97
                                       arraysize(osInformation)));
98
}
99
100
77
static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) {
101
77
  Environment* env = Environment::GetCurrent(args);
102
77
  Isolate* isolate = env->isolate();
103
104
  uv_cpu_info_t* cpu_infos;
105
  int count;
106
107
77
  int err = uv_cpu_info(&cpu_infos, &count);
108
77
  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
154
  std::vector<Local<Value>> result(count * 7);
116
693
  for (int i = 0, j = 0; i < count; i++) {
117
616
    uv_cpu_info_t* ci = cpu_infos + i;
118
1232
    result[j++] = OneByteString(isolate, ci->model);
119
1232
    result[j++] = Number::New(isolate, ci->speed);
120
1232
    result[j++] = Number::New(isolate, ci->cpu_times.user);
121
1232
    result[j++] = Number::New(isolate, ci->cpu_times.nice);
122
1232
    result[j++] = Number::New(isolate, ci->cpu_times.sys);
123
1232
    result[j++] = Number::New(isolate, ci->cpu_times.idle);
124
1232
    result[j++] = Number::New(isolate, ci->cpu_times.irq);
125
  }
126
127
77
  uv_free_cpu_info(cpu_infos, count);
128
231
  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
70
static void GetTotalMemory(const FunctionCallbackInfo<Value>& args) {
139
70
  double amount = uv_get_total_memory();
140
140
  args.GetReturnValue().Set(amount);
141
70
}
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
2
static void GetLoadAvg(const FunctionCallbackInfo<Value>& args) {
153
4
  CHECK(args[0]->IsFloat64Array());
154
4
  Local<Float64Array> array = args[0].As<Float64Array>();
155
2
  CHECK_EQ(array->Length(), 3);
156
2
  Local<ArrayBuffer> ab = array->Buffer();
157
2
  double* loadavg = static_cast<double*>(ab->GetBackingStore()->Data());
158
2
  uv_loadavg(loadavg);
159
2
}
160
161
162
99
static void GetInterfaceAddresses(const FunctionCallbackInfo<Value>& args) {
163
99
  Environment* env = Environment::GetCurrent(args);
164
99
  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
99
  int err = uv_interface_addresses(&interfaces, &count);
173
174
99
  if (err == UV_ENOSYS)
175
    return args.GetReturnValue().SetUndefined();
176
177
99
  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
99
  Local<Value> no_scope_id = Integer::New(isolate, -1);
185
198
  std::vector<Local<Value>> result(count * 7);
186
693
  for (i = 0; i < count; i++) {
187
594
    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
1188
    name = String::NewFromUtf8(isolate, raw_name).ToLocalChecked();
195
196
3564
    snprintf(mac.data(),
197
             mac.size(),
198
             "%02x:%02x:%02x:%02x:%02x:%02x",
199
594
             static_cast<unsigned char>(interfaces[i].phys_addr[0]),
200
594
             static_cast<unsigned char>(interfaces[i].phys_addr[1]),
201
594
             static_cast<unsigned char>(interfaces[i].phys_addr[2]),
202
594
             static_cast<unsigned char>(interfaces[i].phys_addr[3]),
203
594
             static_cast<unsigned char>(interfaces[i].phys_addr[4]),
204
1188
             static_cast<unsigned char>(interfaces[i].phys_addr[5]));
205
206
594
    if (interfaces[i].address.address4.sin_family == AF_INET) {
207
297
      uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip));
208
297
      uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask));
209
297
      family = env->ipv4_string();
210
297
    } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
211
297
      uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
212
297
      uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask));
213
297
      family = env->ipv6_string();
214
    } else {
215
      strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
216
      family = env->unknown_string();
217
    }
218
219
1188
    result[i * 7] = name;
220
1188
    result[i * 7 + 1] = OneByteString(isolate, ip);
221
1188
    result[i * 7 + 2] = OneByteString(isolate, netmask);
222
1188
    result[i * 7 + 3] = family;
223
1188
    result[i * 7 + 4] = FIXED_ONE_BYTE_STRING(isolate, mac);
224
1188
    result[i * 7 + 5] =
225
1188
      interfaces[i].is_internal ? True(isolate) : False(isolate);
226
594
    if (interfaces[i].address.address4.sin_family == AF_INET6) {
227
297
      uint32_t scopeid = interfaces[i].address.address6.sin6_scope_id;
228
594
      result[i * 7 + 6] = Integer::NewFromUnsigned(isolate, scopeid);
229
    } else {
230
297
      result[i * 7 + 6] = no_scope_id;
231
    }
232
  }
233
234
99
  uv_free_interface_addresses(interfaces, count);
235
297
  args.GetReturnValue().Set(Array::New(isolate, result.data(), result.size()));
236
}
237
238
239
276
static void GetHomeDirectory(const FunctionCallbackInfo<Value>& args) {
240
276
  Environment* env = Environment::GetCurrent(args);
241
  char buf[PATH_MAX];
242
243
276
  size_t len = sizeof(buf);
244
276
  const int err = uv_os_homedir(buf, &len);
245
246
276
  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
552
  Local<String> home = String::NewFromUtf8(env->isolate(),
253
                                           buf,
254
                                           NewStringType::kNormal,
255
552
                                           len).ToLocalChecked();
256
552
  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
408
void Initialize(Local<Object> target,
373
                Local<Value> unused,
374
                Local<Context> context,
375
                void* priv) {
376
408
  Environment* env = Environment::GetCurrent(context);
377
408
  env->SetMethod(target, "getHostname", GetHostname);
378
408
  env->SetMethod(target, "getLoadAvg", GetLoadAvg);
379
408
  env->SetMethod(target, "getUptime", GetUptime);
380
408
  env->SetMethod(target, "getTotalMem", GetTotalMemory);
381
408
  env->SetMethod(target, "getFreeMem", GetFreeMemory);
382
408
  env->SetMethod(target, "getCPUs", GetCPUInfo);
383
408
  env->SetMethod(target, "getInterfaceAddresses", GetInterfaceAddresses);
384
408
  env->SetMethod(target, "getHomeDirectory", GetHomeDirectory);
385
408
  env->SetMethod(target, "getUserInfo", GetUserInfo);
386
408
  env->SetMethod(target, "setPriority", SetPriority);
387
408
  env->SetMethod(target, "getPriority", GetPriority);
388
408
  env->SetMethod(target, "getOSInformation", GetOSInformation);
389
816
  target->Set(env->context(),
390
              FIXED_ONE_BYTE_STRING(env->isolate(), "isBigEndian"),
391
2040
              Boolean::New(env->isolate(), IsBigEndian())).Check();
392
408
}
393
394
}  // namespace os
395
}  // namespace node
396
397

19959
NODE_MODULE_CONTEXT_AWARE_INTERNAL(os, node::os::Initialize)