1 |
|
|
#include "env-inl.h" |
2 |
|
|
#include "node_external_reference.h" |
3 |
|
|
#include "node_internals.h" |
4 |
|
|
#include "node_metadata.h" |
5 |
|
|
#include "node_options-inl.h" |
6 |
|
|
#include "node_process.h" |
7 |
|
|
#include "node_revert.h" |
8 |
|
|
#include "util-inl.h" |
9 |
|
|
|
10 |
|
|
#include <climits> // PATH_MAX |
11 |
|
|
|
12 |
|
|
namespace node { |
13 |
|
|
using v8::Context; |
14 |
|
|
using v8::DEFAULT; |
15 |
|
|
using v8::EscapableHandleScope; |
16 |
|
|
using v8::Function; |
17 |
|
|
using v8::FunctionCallbackInfo; |
18 |
|
|
using v8::FunctionTemplate; |
19 |
|
|
using v8::Integer; |
20 |
|
|
using v8::Isolate; |
21 |
|
|
using v8::Local; |
22 |
|
|
using v8::MaybeLocal; |
23 |
|
|
using v8::Name; |
24 |
|
|
using v8::NewStringType; |
25 |
|
|
using v8::None; |
26 |
|
|
using v8::Object; |
27 |
|
|
using v8::PropertyCallbackInfo; |
28 |
|
|
using v8::SideEffectType; |
29 |
|
|
using v8::String; |
30 |
|
|
using v8::Value; |
31 |
|
|
|
32 |
|
27 |
static void ProcessTitleGetter(Local<Name> property, |
33 |
|
|
const PropertyCallbackInfo<Value>& info) { |
34 |
|
54 |
std::string title = GetProcessTitle("node"); |
35 |
|
54 |
info.GetReturnValue().Set( |
36 |
|
54 |
String::NewFromUtf8(info.GetIsolate(), title.data(), |
37 |
|
54 |
NewStringType::kNormal, title.size()) |
38 |
|
|
.ToLocalChecked()); |
39 |
|
27 |
} |
40 |
|
|
|
41 |
|
99 |
static void ProcessTitleSetter(Local<Name> property, |
42 |
|
|
Local<Value> value, |
43 |
|
|
const PropertyCallbackInfo<void>& info) { |
44 |
|
198 |
node::Utf8Value title(info.GetIsolate(), value); |
45 |
|
|
TRACE_EVENT_METADATA1( |
46 |
✓✓ |
99 |
"__metadata", "process_name", "name", TRACE_STR_COPY(*title)); |
47 |
|
99 |
uv_set_process_title(*title); |
48 |
|
195 |
} |
49 |
✓✓ |
99 |
|
50 |
|
101 |
static void DebugPortGetter(Local<Name> property, |
51 |
|
2 |
const PropertyCallbackInfo<Value>& info) { |
52 |
|
101 |
Environment* env = Environment::GetCurrent(info); |
53 |
|
202 |
ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port()); |
54 |
|
101 |
int port = host_port->port(); |
55 |
|
202 |
info.GetReturnValue().Set(port); |
56 |
|
101 |
} |
57 |
|
|
|
58 |
|
|
static void DebugPortSetter(Local<Name> property, |
59 |
|
|
Local<Value> value, |
60 |
|
|
const PropertyCallbackInfo<void>& info) { |
61 |
|
|
Environment* env = Environment::GetCurrent(info); |
62 |
|
|
int32_t port = value->Int32Value(env->context()).FromMaybe(0); |
63 |
|
|
ExclusiveAccess<HostPort>::Scoped host_port(env->inspector_host_port()); |
64 |
|
|
host_port->set_port(static_cast<int>(port)); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
18 |
static void GetParentProcessId(Local<Name> property, |
68 |
|
|
const PropertyCallbackInfo<Value>& info) { |
69 |
|
54 |
info.GetReturnValue().Set(uv_os_getppid()); |
70 |
|
18 |
} |
71 |
|
|
|
72 |
|
439 |
MaybeLocal<Object> CreateProcessObject(Environment* env) { |
73 |
|
439 |
Isolate* isolate = env->isolate(); |
74 |
|
439 |
EscapableHandleScope scope(isolate); |
75 |
|
439 |
Local<Context> context = env->context(); |
76 |
|
|
|
77 |
|
439 |
Local<FunctionTemplate> process_template = FunctionTemplate::New(isolate); |
78 |
|
878 |
process_template->SetClassName(env->process_string()); |
79 |
|
|
Local<Function> process_ctor; |
80 |
|
|
Local<Object> process; |
81 |
✓✗✗✓ ✗✓ |
1756 |
if (!process_template->GetFunction(context).ToLocal(&process_ctor) || |
82 |
|
1317 |
!process_ctor->NewInstance(context).ToLocal(&process)) { |
83 |
|
|
return MaybeLocal<Object>(); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
// process.version |
87 |
|
1756 |
READONLY_PROPERTY(process, |
88 |
|
|
"version", |
89 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION)); |
90 |
|
|
|
91 |
|
|
// process.versions |
92 |
|
439 |
Local<Object> versions = Object::New(env->isolate()); |
93 |
|
1317 |
READONLY_PROPERTY(process, "versions", versions); |
94 |
|
|
|
95 |
|
|
#define V(key) \ |
96 |
|
|
if (!per_process::metadata.versions.key.empty()) { \ |
97 |
|
|
READONLY_STRING_PROPERTY( \ |
98 |
|
|
versions, #key, per_process::metadata.versions.key); \ |
99 |
|
|
} |
100 |
✓✗✓✗
|
2195 |
NODE_VERSIONS_KEYS(V) |
101 |
|
|
#undef V |
102 |
|
1317 |
|
103 |
✓✗ |
2195 |
// process.arch |
104 |
|
3951 |
READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch); |
105 |
✓✗ |
1756 |
|
106 |
|
2195 |
// process.platform |
107 |
✓✗ |
3512 |
READONLY_STRING_PROPERTY(process, "platform", per_process::metadata.platform); |
108 |
|
2195 |
|
109 |
✓✗ |
1756 |
// process.release |
110 |
|
2634 |
Local<Object> release = Object::New(env->isolate()); |
111 |
✓✗ |
3073 |
READONLY_PROPERTY(process, "release", release); |
112 |
|
3512 |
READONLY_STRING_PROPERTY(release, "name", per_process::metadata.release.name); |
113 |
✓✗ |
1756 |
#if NODE_VERSION_IS_LTS |
114 |
|
1756 |
READONLY_STRING_PROPERTY(release, "lts", per_process::metadata.release.lts); |
115 |
✓✗ |
1756 |
#endif // NODE_VERSION_IS_LTS |
116 |
|
1756 |
|
117 |
✓✗ |
1756 |
#ifdef NODE_HAS_RELEASE_URLS |
118 |
|
1756 |
READONLY_STRING_PROPERTY( |
119 |
✓✗ |
1756 |
release, "sourceUrl", per_process::metadata.release.source_url); |
120 |
|
1756 |
READONLY_STRING_PROPERTY( |
121 |
✓✗ |
1756 |
release, "headersUrl", per_process::metadata.release.headers_url); |
122 |
|
1756 |
#ifdef _WIN32 |
123 |
✓✗ |
1756 |
READONLY_STRING_PROPERTY( |
124 |
|
1756 |
release, "libUrl", per_process::metadata.release.lib_url); |
125 |
✓✗ |
1756 |
#endif // _WIN32 |
126 |
|
1756 |
#endif // NODE_HAS_RELEASE_URLS |
127 |
✓✗ |
2634 |
|
128 |
|
878 |
// process._rawDebug: may be overwritten later in JS land, but should be |
129 |
|
1317 |
// available from the beginning for debugging purposes |
130 |
|
439 |
env->SetMethod(process, "_rawDebug", RawDebug); |
131 |
|
|
|
132 |
|
439 |
return scope.Escape(process); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
4998 |
void PatchProcessObject(const FunctionCallbackInfo<Value>& args) { |
136 |
|
4998 |
Isolate* isolate = args.GetIsolate(); |
137 |
|
4998 |
Local<Context> context = isolate->GetCurrentContext(); |
138 |
|
4998 |
Environment* env = Environment::GetCurrent(context); |
139 |
✗✓ |
9996 |
CHECK(args[0]->IsObject()); |
140 |
|
9996 |
Local<Object> process = args[0].As<Object>(); |
141 |
|
|
|
142 |
|
|
// process.title |
143 |
✓✓✗✓
|
14994 |
CHECK(process |
144 |
|
|
->SetAccessor( |
145 |
|
|
context, |
146 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "title"), |
147 |
|
|
ProcessTitleGetter, |
148 |
|
|
env->owns_process_state() ? ProcessTitleSetter : nullptr, |
149 |
|
|
Local<Value>(), |
150 |
|
|
DEFAULT, |
151 |
|
|
None, |
152 |
|
|
SideEffectType::kHasNoSideEffect) |
153 |
|
|
.FromJust()); |
154 |
|
|
|
155 |
|
|
// process.argv |
156 |
|
9996 |
process->Set(context, |
157 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "argv"), |
158 |
|
19992 |
ToV8Value(context, env->argv()).ToLocalChecked()).Check(); |
159 |
|
|
|
160 |
|
|
// process.execArgv |
161 |
|
9996 |
process->Set(context, |
162 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "execArgv"), |
163 |
|
9996 |
ToV8Value(context, env->exec_argv()) |
164 |
|
14994 |
.ToLocalChecked()).Check(); |
165 |
|
|
|
166 |
|
19992 |
READONLY_PROPERTY(process, "pid", |
167 |
|
|
Integer::New(isolate, uv_os_getpid())); |
168 |
|
|
|
169 |
✗✓ |
14994 |
CHECK(process->SetAccessor(context, |
170 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "ppid"), |
171 |
|
|
GetParentProcessId).FromJust()); |
172 |
|
|
|
173 |
|
|
// --security-revert flags |
174 |
|
|
#define V(code, _, __) \ |
175 |
|
|
do { \ |
176 |
|
|
if (IsReverted(SECURITY_REVERT_ ## code)) { \ |
177 |
|
|
READONLY_PROPERTY(process, "REVERT_" #code, True(isolate)); \ |
178 |
|
|
} \ |
179 |
|
|
} while (0); |
180 |
|
|
SECURITY_REVERSIONS(V) |
181 |
|
|
#undef V |
182 |
|
|
|
183 |
|
|
// process.execPath |
184 |
|
|
process |
185 |
|
9996 |
->Set(context, |
186 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "execPath"), |
187 |
|
9996 |
String::NewFromUtf8(isolate, |
188 |
|
4998 |
env->exec_path().c_str(), |
189 |
|
|
NewStringType::kInternalized, |
190 |
|
9996 |
env->exec_path().size()) |
191 |
|
14994 |
.ToLocalChecked()) |
192 |
|
|
.Check(); |
193 |
|
|
|
194 |
|
|
// process.debugPort |
195 |
✓✓✗✓
|
14994 |
CHECK(process |
196 |
|
|
->SetAccessor(context, |
197 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "debugPort"), |
198 |
|
|
DebugPortGetter, |
199 |
|
|
env->owns_process_state() ? DebugPortSetter : nullptr, |
200 |
|
|
Local<Value>()) |
201 |
|
|
.FromJust()); |
202 |
|
4998 |
} |
203 |
|
|
|
204 |
|
4601 |
void RegisterProcessExternalReferences(ExternalReferenceRegistry* registry) { |
205 |
|
4601 |
registry->Register(RawDebug); |
206 |
|
4601 |
} |
207 |
|
|
|
208 |
|
|
} // namespace node |
209 |
|
|
|
210 |
✓✗✓✗
|
18635 |
NODE_MODULE_EXTERNAL_REFERENCE(process_object, |
211 |
|
|
node::RegisterProcessExternalReferences) |