Skip to content

Commit 040b9db

Browse files
joyeecheungtargos
authored andcommitted
src: save exec path when initializing Environment
PR-URL: #28252 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 168c127 commit 040b9db

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

src/env-inl.h

+4
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,10 @@ inline const std::vector<std::string>& Environment::exec_argv() {
635635
return exec_argv_;
636636
}
637637

638+
inline const std::string& Environment::exec_path() const {
639+
return exec_path_;
640+
}
641+
638642
#if HAVE_INSPECTOR
639643
inline void Environment::set_coverage_directory(const char* dir) {
640644
coverage_directory_ = std::string(dir);

src/env.cc

+27
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,32 @@ void Environment::CreateProperties() {
261261
set_process_object(process_object);
262262
}
263263

264+
std::string GetExecPath(const std::vector<std::string>& argv) {
265+
char exec_path_buf[2 * PATH_MAX];
266+
size_t exec_path_len = sizeof(exec_path_buf);
267+
std::string exec_path;
268+
if (uv_exepath(exec_path_buf, &exec_path_len) == 0) {
269+
exec_path = std::string(exec_path_buf, exec_path_len);
270+
} else {
271+
exec_path = argv[0];
272+
}
273+
274+
// On OpenBSD process.execPath will be relative unless we
275+
// get the full path before process.execPath is used.
276+
#if defined(__OpenBSD__)
277+
uv_fs_t req;
278+
req.ptr = nullptr;
279+
if (0 ==
280+
uv_fs_realpath(env->event_loop(), &req, exec_path.c_str(), nullptr)) {
281+
CHECK_NOT_NULL(req.ptr);
282+
exec_path = std::string(static_cast<char*>(req.ptr));
283+
}
284+
uv_fs_req_cleanup(&req);
285+
#endif
286+
287+
return exec_path;
288+
}
289+
264290
Environment::Environment(IsolateData* isolate_data,
265291
Local<Context> context,
266292
const std::vector<std::string>& args,
@@ -274,6 +300,7 @@ Environment::Environment(IsolateData* isolate_data,
274300
timer_base_(uv_now(isolate_data->event_loop())),
275301
exec_argv_(exec_args),
276302
argv_(args),
303+
exec_path_(GetExecPath(args)),
277304
should_abort_on_uncaught_toggle_(isolate_, 1),
278305
stream_base_state_(isolate_, StreamBase::kNumStreamBaseStateFields),
279306
flags_(flags),

src/env.h

+2
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ class Environment : public MemoryRetainer {
850850
void InitializeLibuv(bool start_profiler_idle_notifier);
851851
inline const std::vector<std::string>& exec_argv();
852852
inline const std::vector<std::string>& argv();
853+
const std::string& exec_path() const;
853854

854855
typedef void (*HandleCleanupCb)(Environment* env,
855856
uv_handle_t* handle,
@@ -1239,6 +1240,7 @@ class Environment : public MemoryRetainer {
12391240
std::shared_ptr<HostPort> inspector_host_port_;
12401241
std::vector<std::string> exec_argv_;
12411242
std::vector<std::string> argv_;
1243+
std::string exec_path_;
12421244

12431245
uint32_t module_id_counter_ = 0;
12441246
uint32_t script_id_counter_ = 0;

src/node_process_object.cc

+9-31
Original file line numberDiff line numberDiff line change
@@ -180,37 +180,15 @@ void PatchProcessObject(const FunctionCallbackInfo<Value>& args) {
180180
#undef V
181181

182182
// process.execPath
183-
{
184-
char exec_path_buf[2 * PATH_MAX];
185-
size_t exec_path_len = sizeof(exec_path_buf);
186-
std::string exec_path;
187-
if (uv_exepath(exec_path_buf, &exec_path_len) == 0) {
188-
exec_path = std::string(exec_path_buf, exec_path_len);
189-
} else {
190-
exec_path = env->argv()[0];
191-
}
192-
// On OpenBSD process.execPath will be relative unless we
193-
// get the full path before process.execPath is used.
194-
#if defined(__OpenBSD__)
195-
uv_fs_t req;
196-
req.ptr = nullptr;
197-
if (0 ==
198-
uv_fs_realpath(env->event_loop(), &req, exec_path.c_str(), nullptr)) {
199-
CHECK_NOT_NULL(req.ptr);
200-
exec_path = std::string(static_cast<char*>(req.ptr));
201-
}
202-
uv_fs_req_cleanup(&req);
203-
#endif
204-
process
205-
->Set(context,
206-
FIXED_ONE_BYTE_STRING(isolate, "execPath"),
207-
String::NewFromUtf8(isolate,
208-
exec_path.c_str(),
209-
NewStringType::kInternalized,
210-
exec_path.size())
211-
.ToLocalChecked())
212-
.Check();
213-
}
183+
process
184+
->Set(context,
185+
FIXED_ONE_BYTE_STRING(isolate, "execPath"),
186+
String::NewFromUtf8(isolate,
187+
env->exec_path().c_str(),
188+
NewStringType::kInternalized,
189+
env->exec_path().size())
190+
.ToLocalChecked())
191+
.Check();
214192

215193
// process.debugPort
216194
CHECK(process

0 commit comments

Comments
 (0)