|
2 | 2 | #include "instruments/hooks_wrapper.h" |
3 | 3 | #include <napi.h> |
4 | 4 |
|
| 5 | +#include <cerrno> |
| 6 | +#include <cstring> |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +#if !defined(_WIN32) |
| 11 | +#include <fcntl.h> |
| 12 | +#include <unistd.h> |
| 13 | +#endif |
| 14 | + |
5 | 15 | namespace codspeed_native { |
6 | 16 |
|
| 17 | +namespace { |
| 18 | + |
| 19 | +bool ContainsNulByte(const std::string &value) { |
| 20 | + return value.find('\0') != std::string::npos; |
| 21 | +} |
| 22 | + |
| 23 | +#if !defined(_WIN32) |
| 24 | +// `execv` keeps descriptors marked close-on-exec closed in the new image, and |
| 25 | +// libuv marks the stdio descriptors that way. Clear the flag like Node's own |
| 26 | +// `process.execve` does, otherwise the restarted process writes to /dev/null. |
| 27 | +int PersistStandardStream(int fd) { |
| 28 | + const int flags = fcntl(fd, F_GETFD, 0); |
| 29 | + if (flags < 0) { |
| 30 | + return flags; |
| 31 | + } |
| 32 | + return fcntl(fd, F_SETFD, flags & ~FD_CLOEXEC); |
| 33 | +} |
| 34 | +#endif |
| 35 | + |
| 36 | +// Replaces the current process image with `executable` using POSIX `execv`. |
| 37 | +// |
| 38 | +// Replacing the image in place (instead of spawning a child) keeps the PID, |
| 39 | +// the working directory, the environment and the open file descriptors -- |
| 40 | +// notably the runner FIFO used by instrument-hooks -- intact. On success this |
| 41 | +// function never returns; on failure it throws and the current image stays |
| 42 | +// usable. |
| 43 | +Napi::Value Restart(const Napi::CallbackInfo &info) { |
| 44 | + Napi::Env env = info.Env(); |
| 45 | + |
| 46 | + if (info.Length() != 2) { |
| 47 | + Napi::TypeError::New(env, "Expected 2 arguments: executable and args") |
| 48 | + .ThrowAsJavaScriptException(); |
| 49 | + return env.Undefined(); |
| 50 | + } |
| 51 | + |
| 52 | + if (!info[0].IsString()) { |
| 53 | + Napi::TypeError::New(env, "Expected string (executable)") |
| 54 | + .ThrowAsJavaScriptException(); |
| 55 | + return env.Undefined(); |
| 56 | + } |
| 57 | + |
| 58 | + if (!info[1].IsArray()) { |
| 59 | + Napi::TypeError::New(env, "Expected array of strings (args)") |
| 60 | + .ThrowAsJavaScriptException(); |
| 61 | + return env.Undefined(); |
| 62 | + } |
| 63 | + |
| 64 | + const std::string executable = info[0].As<Napi::String>().Utf8Value(); |
| 65 | + if (executable.empty()) { |
| 66 | + Napi::TypeError::New(env, "Expected non-empty string (executable)") |
| 67 | + .ThrowAsJavaScriptException(); |
| 68 | + return env.Undefined(); |
| 69 | + } |
| 70 | + if (ContainsNulByte(executable)) { |
| 71 | + Napi::TypeError::New(env, "executable must not contain NUL bytes") |
| 72 | + .ThrowAsJavaScriptException(); |
| 73 | + return env.Undefined(); |
| 74 | + } |
| 75 | + |
| 76 | + const Napi::Array args = info[1].As<Napi::Array>(); |
| 77 | + const uint32_t argc = args.Length(); |
| 78 | + if (argc == 0) { |
| 79 | + Napi::TypeError::New(env, "Expected at least argv[0] in args: pass the " |
| 80 | + "complete argv, including the program name") |
| 81 | + .ThrowAsJavaScriptException(); |
| 82 | + return env.Undefined(); |
| 83 | + } |
| 84 | + |
| 85 | + // Fill the strings before taking any pointer out of them: `execv` needs |
| 86 | + // stable storage, so the vector must not reallocate afterwards. |
| 87 | + std::vector<std::string> arg_strings; |
| 88 | + arg_strings.reserve(argc); |
| 89 | + for (uint32_t i = 0; i < argc; i++) { |
| 90 | + const Napi::Value arg = args.Get(i); |
| 91 | + if (!arg.IsString()) { |
| 92 | + Napi::TypeError::New(env, "Expected string (args[" + std::to_string(i) + |
| 93 | + "])") |
| 94 | + .ThrowAsJavaScriptException(); |
| 95 | + return env.Undefined(); |
| 96 | + } |
| 97 | + arg_strings.push_back(arg.As<Napi::String>().Utf8Value()); |
| 98 | + if (ContainsNulByte(arg_strings.back())) { |
| 99 | + Napi::TypeError::New(env, "args[" + std::to_string(i) + |
| 100 | + "] must not contain NUL bytes") |
| 101 | + .ThrowAsJavaScriptException(); |
| 102 | + return env.Undefined(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +#if defined(_WIN32) |
| 107 | + Napi::Error::New(env, "restart() is not supported on Windows: pass the " |
| 108 | + "required Node.js flags from the launcher") |
| 109 | + .ThrowAsJavaScriptException(); |
| 110 | + return env.Undefined(); |
| 111 | +#else |
| 112 | + std::vector<char *> argv; |
| 113 | + argv.reserve(arg_strings.size() + 1); |
| 114 | + for (std::string &arg : arg_strings) { |
| 115 | + argv.push_back(const_cast<char *>(arg.c_str())); |
| 116 | + } |
| 117 | + argv.push_back(nullptr); |
| 118 | + |
| 119 | + if (PersistStandardStream(0) < 0 || PersistStandardStream(1) < 0 || |
| 120 | + PersistStandardStream(2) < 0) { |
| 121 | + const int fcntl_errno = errno; |
| 122 | + Napi::Error::New(env, "Failed to keep stdio open across restart: " + |
| 123 | + std::string(std::strerror(fcntl_errno))) |
| 124 | + .ThrowAsJavaScriptException(); |
| 125 | + return env.Undefined(); |
| 126 | + } |
| 127 | + |
| 128 | + execv(executable.c_str(), argv.data()); |
| 129 | + |
| 130 | + // Reaching this point means the exec failed and the current process is still |
| 131 | + // running. Capture errno before any further N-API call can clobber it. |
| 132 | + const int exec_errno = errno; |
| 133 | + Napi::Error::New(env, "Failed to restart with execv(\"" + executable + |
| 134 | + "\"): " + std::strerror(exec_errno)) |
| 135 | + .ThrowAsJavaScriptException(); |
| 136 | + return env.Undefined(); |
| 137 | +#endif |
| 138 | +} |
| 139 | + |
| 140 | +} // namespace |
| 141 | + |
7 | 142 | Napi::Object Initialize(Napi::Env env, Napi::Object exports) { |
8 | 143 | codspeed_native::LinuxPerf::Initialize(env, exports); |
9 | 144 | codspeed_native::instruments::hooks_wrapper::Initialize(env, exports); |
| 145 | + exports.Set(Napi::String::New(env, "restart"), |
| 146 | + Napi::Function::New(env, Restart)); |
10 | 147 |
|
11 | 148 | return exports; |
12 | 149 | } |
|
0 commit comments