Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5 | #include <errno.h> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 6 | #include <fcntl.h> |
| 7 | #include <signal.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10 | #include <sys/stat.h> |
| 11 | #include <sys/time.h> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | #include <sys/types.h> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 13 | #include <sys/wait.h> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 15 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16 | #include "src/d8.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 17 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 18 | #if !V8_OS_NACL |
| 19 | #include <sys/select.h> |
| 20 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 21 | |
| 22 | namespace v8 { |
| 23 | |
| 24 | |
| 25 | // If the buffer ends in the middle of a UTF-8 sequence then we return |
| 26 | // the length of the string up to but not including the incomplete UTF-8 |
| 27 | // sequence. If the buffer ends with a valid UTF-8 sequence then we |
| 28 | // return the whole buffer. |
| 29 | static int LengthWithoutIncompleteUtf8(char* buffer, int len) { |
| 30 | int answer = len; |
| 31 | // 1-byte encoding. |
| 32 | static const int kUtf8SingleByteMask = 0x80; |
| 33 | static const int kUtf8SingleByteValue = 0x00; |
| 34 | // 2-byte encoding. |
| 35 | static const int kUtf8TwoByteMask = 0xe0; |
| 36 | static const int kUtf8TwoByteValue = 0xc0; |
| 37 | // 3-byte encoding. |
| 38 | static const int kUtf8ThreeByteMask = 0xf0; |
| 39 | static const int kUtf8ThreeByteValue = 0xe0; |
| 40 | // 4-byte encoding. |
| 41 | static const int kUtf8FourByteMask = 0xf8; |
| 42 | static const int kUtf8FourByteValue = 0xf0; |
| 43 | // Subsequent bytes of a multi-byte encoding. |
| 44 | static const int kMultiByteMask = 0xc0; |
| 45 | static const int kMultiByteValue = 0x80; |
| 46 | int multi_byte_bytes_seen = 0; |
| 47 | while (answer > 0) { |
| 48 | int c = buffer[answer - 1]; |
| 49 | // Ends in valid single-byte sequence? |
| 50 | if ((c & kUtf8SingleByteMask) == kUtf8SingleByteValue) return answer; |
| 51 | // Ends in one or more subsequent bytes of a multi-byte value? |
| 52 | if ((c & kMultiByteMask) == kMultiByteValue) { |
| 53 | multi_byte_bytes_seen++; |
| 54 | answer--; |
| 55 | } else { |
| 56 | if ((c & kUtf8TwoByteMask) == kUtf8TwoByteValue) { |
| 57 | if (multi_byte_bytes_seen >= 1) { |
| 58 | return answer + 2; |
| 59 | } |
| 60 | return answer - 1; |
| 61 | } else if ((c & kUtf8ThreeByteMask) == kUtf8ThreeByteValue) { |
| 62 | if (multi_byte_bytes_seen >= 2) { |
| 63 | return answer + 3; |
| 64 | } |
| 65 | return answer - 1; |
| 66 | } else if ((c & kUtf8FourByteMask) == kUtf8FourByteValue) { |
| 67 | if (multi_byte_bytes_seen >= 3) { |
| 68 | return answer + 4; |
| 69 | } |
| 70 | return answer - 1; |
| 71 | } else { |
| 72 | return answer; // Malformed UTF-8. |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | // Suspends the thread until there is data available from the child process. |
| 81 | // Returns false on timeout, true on data ready. |
| 82 | static bool WaitOnFD(int fd, |
| 83 | int read_timeout, |
| 84 | int total_timeout, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 85 | const struct timeval& start_time) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | fd_set readfds, writefds, exceptfds; |
| 87 | struct timeval timeout; |
| 88 | int gone = 0; |
| 89 | if (total_timeout != -1) { |
| 90 | struct timeval time_now; |
| 91 | gettimeofday(&time_now, NULL); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | time_t seconds = time_now.tv_sec - start_time.tv_sec; |
| 93 | gone = static_cast<int>(seconds * 1000 + |
| 94 | (time_now.tv_usec - start_time.tv_usec) / 1000); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | if (gone >= total_timeout) return false; |
| 96 | } |
| 97 | FD_ZERO(&readfds); |
| 98 | FD_ZERO(&writefds); |
| 99 | FD_ZERO(&exceptfds); |
| 100 | FD_SET(fd, &readfds); |
| 101 | FD_SET(fd, &exceptfds); |
| 102 | if (read_timeout == -1 || |
| 103 | (total_timeout != -1 && total_timeout - gone < read_timeout)) { |
| 104 | read_timeout = total_timeout - gone; |
| 105 | } |
| 106 | timeout.tv_usec = (read_timeout % 1000) * 1000; |
| 107 | timeout.tv_sec = read_timeout / 1000; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 108 | #if V8_OS_NACL |
| 109 | // PNaCL has no support for select. |
| 110 | int number_of_fds_ready = -1; |
| 111 | #else |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | int number_of_fds_ready = select(fd + 1, |
| 113 | &readfds, |
| 114 | &writefds, |
| 115 | &exceptfds, |
| 116 | read_timeout != -1 ? &timeout : NULL); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 117 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 118 | return number_of_fds_ready == 1; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | // Checks whether we ran out of time on the timeout. Returns true if we ran out |
| 123 | // of time, false if we still have time. |
| 124 | static bool TimeIsOut(const struct timeval& start_time, const int& total_time) { |
| 125 | if (total_time == -1) return false; |
| 126 | struct timeval time_now; |
| 127 | gettimeofday(&time_now, NULL); |
| 128 | // Careful about overflow. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 129 | int seconds = static_cast<int>(time_now.tv_sec - start_time.tv_sec); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | if (seconds > 100) { |
| 131 | if (seconds * 1000 > total_time) return true; |
| 132 | return false; |
| 133 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 134 | int useconds = static_cast<int>(time_now.tv_usec - start_time.tv_usec); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | if (seconds * 1000000 + useconds > total_time * 1000) { |
| 136 | return true; |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | // A utility class that does a non-hanging waitpid on the child process if we |
| 143 | // bail out of the System() function early. If you don't ever do a waitpid on |
| 144 | // a subprocess then it turns into one of those annoying 'zombie processes'. |
| 145 | class ZombieProtector { |
| 146 | public: |
| 147 | explicit ZombieProtector(int pid): pid_(pid) { } |
| 148 | ~ZombieProtector() { if (pid_ != 0) waitpid(pid_, NULL, 0); } |
| 149 | void ChildIsDeadNow() { pid_ = 0; } |
| 150 | private: |
| 151 | int pid_; |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | // A utility class that closes a file descriptor when it goes out of scope. |
| 156 | class OpenFDCloser { |
| 157 | public: |
| 158 | explicit OpenFDCloser(int fd): fd_(fd) { } |
| 159 | ~OpenFDCloser() { close(fd_); } |
| 160 | private: |
| 161 | int fd_; |
| 162 | }; |
| 163 | |
| 164 | |
| 165 | // A utility class that takes the array of command arguments and puts then in an |
| 166 | // array of new[]ed UTF-8 C strings. Deallocates them again when it goes out of |
| 167 | // scope. |
| 168 | class ExecArgs { |
| 169 | public: |
| 170 | ExecArgs() { |
| 171 | exec_args_[0] = NULL; |
| 172 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 173 | bool Init(Isolate* isolate, Local<Value> arg0, Local<Array> command_args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 174 | String::Utf8Value prog(arg0); |
| 175 | if (*prog == NULL) { |
| 176 | const char* message = |
| 177 | "os.system(): String conversion of program name failed"; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 178 | isolate->ThrowException( |
| 179 | String::NewFromUtf8(isolate, message, NewStringType::kNormal) |
| 180 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | int len = prog.length() + 3; |
| 184 | char* c_arg = new char[len]; |
| 185 | snprintf(c_arg, len, "%s", *prog); |
| 186 | exec_args_[0] = c_arg; |
| 187 | int i = 1; |
| 188 | for (unsigned j = 0; j < command_args->Length(); i++, j++) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 189 | Local<Value> arg( |
| 190 | command_args->Get(isolate->GetCurrentContext(), |
| 191 | Integer::New(isolate, j)).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 192 | String::Utf8Value utf8_arg(arg); |
| 193 | if (*utf8_arg == NULL) { |
| 194 | exec_args_[i] = NULL; // Consistent state for destructor. |
| 195 | const char* message = |
| 196 | "os.system(): String conversion of argument failed."; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 197 | isolate->ThrowException( |
| 198 | String::NewFromUtf8(isolate, message, NewStringType::kNormal) |
| 199 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | int len = utf8_arg.length() + 1; |
| 203 | char* c_arg = new char[len]; |
| 204 | snprintf(c_arg, len, "%s", *utf8_arg); |
| 205 | exec_args_[i] = c_arg; |
| 206 | } |
| 207 | exec_args_[i] = NULL; |
| 208 | return true; |
| 209 | } |
| 210 | ~ExecArgs() { |
| 211 | for (unsigned i = 0; i < kMaxArgs; i++) { |
| 212 | if (exec_args_[i] == NULL) { |
| 213 | return; |
| 214 | } |
| 215 | delete [] exec_args_[i]; |
| 216 | exec_args_[i] = 0; |
| 217 | } |
| 218 | } |
| 219 | static const unsigned kMaxArgs = 1000; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 220 | char* const* arg_array() const { return exec_args_; } |
| 221 | const char* arg0() const { return exec_args_[0]; } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 222 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 223 | private: |
| 224 | char* exec_args_[kMaxArgs + 1]; |
| 225 | }; |
| 226 | |
| 227 | |
| 228 | // Gets the optional timeouts from the arguments to the system() call. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 229 | static bool GetTimeouts(const v8::FunctionCallbackInfo<v8::Value>& args, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 230 | int* read_timeout, |
| 231 | int* total_timeout) { |
| 232 | if (args.Length() > 3) { |
| 233 | if (args[3]->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 234 | *total_timeout = args[3] |
| 235 | ->Int32Value(args.GetIsolate()->GetCurrentContext()) |
| 236 | .FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 237 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 238 | args.GetIsolate()->ThrowException( |
| 239 | String::NewFromUtf8(args.GetIsolate(), |
| 240 | "system: Argument 4 must be a number", |
| 241 | NewStringType::kNormal).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | if (args.Length() > 2) { |
| 246 | if (args[2]->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 247 | *read_timeout = args[2] |
| 248 | ->Int32Value(args.GetIsolate()->GetCurrentContext()) |
| 249 | .FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 251 | args.GetIsolate()->ThrowException( |
| 252 | String::NewFromUtf8(args.GetIsolate(), |
| 253 | "system: Argument 3 must be a number", |
| 254 | NewStringType::kNormal).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | } |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | static const int kReadFD = 0; |
| 263 | static const int kWriteFD = 1; |
| 264 | |
| 265 | |
| 266 | // This is run in the child process after fork() but before exec(). It normally |
| 267 | // ends with the child process being replaced with the desired child program. |
| 268 | // It only returns if an error occurred. |
| 269 | static void ExecSubprocess(int* exec_error_fds, |
| 270 | int* stdout_fds, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 271 | const ExecArgs& exec_args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 272 | close(exec_error_fds[kReadFD]); // Don't need this in the child. |
| 273 | close(stdout_fds[kReadFD]); // Don't need this in the child. |
| 274 | close(1); // Close stdout. |
| 275 | dup2(stdout_fds[kWriteFD], 1); // Dup pipe fd to stdout. |
| 276 | close(stdout_fds[kWriteFD]); // Don't need the original fd now. |
| 277 | fcntl(exec_error_fds[kWriteFD], F_SETFD, FD_CLOEXEC); |
| 278 | execvp(exec_args.arg0(), exec_args.arg_array()); |
| 279 | // Only get here if the exec failed. Write errno to the parent to tell |
| 280 | // them it went wrong. If it went well the pipe is closed. |
| 281 | int err = errno; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 282 | ssize_t bytes_written; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 283 | do { |
| 284 | bytes_written = write(exec_error_fds[kWriteFD], &err, sizeof(err)); |
| 285 | } while (bytes_written == -1 && errno == EINTR); |
| 286 | // Return (and exit child process). |
| 287 | } |
| 288 | |
| 289 | |
| 290 | // Runs in the parent process. Checks that the child was able to exec (closing |
| 291 | // the file desriptor), or reports an error if it failed. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 292 | static bool ChildLaunchedOK(Isolate* isolate, int* exec_error_fds) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 293 | ssize_t bytes_read; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 294 | int err; |
| 295 | do { |
| 296 | bytes_read = read(exec_error_fds[kReadFD], &err, sizeof(err)); |
| 297 | } while (bytes_read == -1 && errno == EINTR); |
| 298 | if (bytes_read != 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 299 | isolate->ThrowException( |
| 300 | String::NewFromUtf8(isolate, strerror(err), NewStringType::kNormal) |
| 301 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | return true; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | // Accumulates the output from the child in a string handle. Returns true if it |
| 309 | // succeeded or false if an exception was thrown. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 310 | static Local<Value> GetStdout(Isolate* isolate, int child_fd, |
| 311 | const struct timeval& start_time, |
| 312 | int read_timeout, int total_timeout) { |
| 313 | Local<String> accumulator = String::Empty(isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 314 | |
| 315 | int fullness = 0; |
| 316 | static const int kStdoutReadBufferSize = 4096; |
| 317 | char buffer[kStdoutReadBufferSize]; |
| 318 | |
| 319 | if (fcntl(child_fd, F_SETFL, O_NONBLOCK) != 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 320 | return isolate->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 321 | String::NewFromUtf8(isolate, strerror(errno), NewStringType::kNormal) |
| 322 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | int bytes_read; |
| 326 | do { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 327 | bytes_read = static_cast<int>( |
| 328 | read(child_fd, buffer + fullness, kStdoutReadBufferSize - fullness)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 329 | if (bytes_read == -1) { |
| 330 | if (errno == EAGAIN) { |
| 331 | if (!WaitOnFD(child_fd, |
| 332 | read_timeout, |
| 333 | total_timeout, |
| 334 | start_time) || |
| 335 | (TimeIsOut(start_time, total_timeout))) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 336 | return isolate->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 337 | String::NewFromUtf8(isolate, "Timed out waiting for output", |
| 338 | NewStringType::kNormal).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 339 | } |
| 340 | continue; |
| 341 | } else if (errno == EINTR) { |
| 342 | continue; |
| 343 | } else { |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | if (bytes_read + fullness > 0) { |
| 348 | int length = bytes_read == 0 ? |
| 349 | bytes_read + fullness : |
| 350 | LengthWithoutIncompleteUtf8(buffer, bytes_read + fullness); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 351 | Local<String> addition = |
| 352 | String::NewFromUtf8(isolate, buffer, NewStringType::kNormal, length) |
| 353 | .ToLocalChecked(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 354 | accumulator = String::Concat(accumulator, addition); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 355 | fullness = bytes_read + fullness - length; |
| 356 | memcpy(buffer, buffer + length, fullness); |
| 357 | } |
| 358 | } while (bytes_read != 0); |
| 359 | return accumulator; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | // Modern Linux has the waitid call, which is like waitpid, but more useful |
| 364 | // if you want a timeout. If we don't have waitid we can't limit the time |
| 365 | // waiting for the process to exit without losing the information about |
| 366 | // whether it exited normally. In the common case this doesn't matter because |
| 367 | // we don't get here before the child has closed stdout and most programs don't |
| 368 | // do that before they exit. |
| 369 | // |
| 370 | // We're disabling usage of waitid in Mac OS X because it doens't work for us: |
| 371 | // a parent process hangs on waiting while a child process is already a zombie. |
| 372 | // See http://code.google.com/p/v8/issues/detail?id=401. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 373 | #if defined(WNOWAIT) && !defined(ANDROID) && !defined(__APPLE__) \ |
| 374 | && !defined(__NetBSD__) |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 375 | #if !defined(__FreeBSD__) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 376 | #define HAS_WAITID 1 |
| 377 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 378 | #endif |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 379 | |
| 380 | |
| 381 | // Get exit status of child. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 382 | static bool WaitForChild(Isolate* isolate, |
| 383 | int pid, |
| 384 | ZombieProtector& child_waiter, // NOLINT |
| 385 | const struct timeval& start_time, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 386 | int read_timeout, |
| 387 | int total_timeout) { |
| 388 | #ifdef HAS_WAITID |
| 389 | |
| 390 | siginfo_t child_info; |
| 391 | child_info.si_pid = 0; |
| 392 | int useconds = 1; |
| 393 | // Wait for child to exit. |
| 394 | while (child_info.si_pid == 0) { |
| 395 | waitid(P_PID, pid, &child_info, WEXITED | WNOHANG | WNOWAIT); |
| 396 | usleep(useconds); |
| 397 | if (useconds < 1000000) useconds <<= 1; |
| 398 | if ((read_timeout != -1 && useconds / 1000 > read_timeout) || |
| 399 | (TimeIsOut(start_time, total_timeout))) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 400 | isolate->ThrowException( |
| 401 | String::NewFromUtf8(isolate, |
| 402 | "Timed out waiting for process to terminate", |
| 403 | NewStringType::kNormal).ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | kill(pid, SIGINT); |
| 405 | return false; |
| 406 | } |
| 407 | } |
| 408 | if (child_info.si_code == CLD_KILLED) { |
| 409 | char message[999]; |
| 410 | snprintf(message, |
| 411 | sizeof(message), |
| 412 | "Child killed by signal %d", |
| 413 | child_info.si_status); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 414 | isolate->ThrowException( |
| 415 | String::NewFromUtf8(isolate, message, NewStringType::kNormal) |
| 416 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 417 | return false; |
| 418 | } |
| 419 | if (child_info.si_code == CLD_EXITED && child_info.si_status != 0) { |
| 420 | char message[999]; |
| 421 | snprintf(message, |
| 422 | sizeof(message), |
| 423 | "Child exited with status %d", |
| 424 | child_info.si_status); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 425 | isolate->ThrowException( |
| 426 | String::NewFromUtf8(isolate, message, NewStringType::kNormal) |
| 427 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 428 | return false; |
| 429 | } |
| 430 | |
| 431 | #else // No waitid call. |
| 432 | |
| 433 | int child_status; |
| 434 | waitpid(pid, &child_status, 0); // We hang here if the child doesn't exit. |
| 435 | child_waiter.ChildIsDeadNow(); |
| 436 | if (WIFSIGNALED(child_status)) { |
| 437 | char message[999]; |
| 438 | snprintf(message, |
| 439 | sizeof(message), |
| 440 | "Child killed by signal %d", |
| 441 | WTERMSIG(child_status)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 442 | isolate->ThrowException( |
| 443 | String::NewFromUtf8(isolate, message, NewStringType::kNormal) |
| 444 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 445 | return false; |
| 446 | } |
| 447 | if (WEXITSTATUS(child_status) != 0) { |
| 448 | char message[999]; |
| 449 | int exit_status = WEXITSTATUS(child_status); |
| 450 | snprintf(message, |
| 451 | sizeof(message), |
| 452 | "Child exited with status %d", |
| 453 | exit_status); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 454 | isolate->ThrowException( |
| 455 | String::NewFromUtf8(isolate, message, NewStringType::kNormal) |
| 456 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | |
| 460 | #endif // No waitid call. |
| 461 | |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | // Implementation of the system() function (see d8.h for details). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 467 | void Shell::System(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 468 | HandleScope scope(args.GetIsolate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 469 | int read_timeout = -1; |
| 470 | int total_timeout = -1; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 471 | if (!GetTimeouts(args, &read_timeout, &total_timeout)) return; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 472 | Local<Array> command_args; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 473 | if (args.Length() > 1) { |
| 474 | if (!args[1]->IsArray()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 475 | args.GetIsolate()->ThrowException( |
| 476 | String::NewFromUtf8(args.GetIsolate(), |
| 477 | "system: Argument 2 must be an array", |
| 478 | NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 479 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 480 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 481 | command_args = Local<Array>::Cast(args[1]); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 482 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 483 | command_args = Array::New(args.GetIsolate(), 0); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 484 | } |
| 485 | if (command_args->Length() > ExecArgs::kMaxArgs) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 486 | args.GetIsolate()->ThrowException( |
| 487 | String::NewFromUtf8(args.GetIsolate(), "Too many arguments to system()", |
| 488 | NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 489 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 490 | } |
| 491 | if (args.Length() < 1) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 492 | args.GetIsolate()->ThrowException( |
| 493 | String::NewFromUtf8(args.GetIsolate(), "Too few arguments to system()", |
| 494 | NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 495 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | struct timeval start_time; |
| 499 | gettimeofday(&start_time, NULL); |
| 500 | |
| 501 | ExecArgs exec_args; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 502 | if (!exec_args.Init(args.GetIsolate(), args[0], command_args)) { |
| 503 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 504 | } |
| 505 | int exec_error_fds[2]; |
| 506 | int stdout_fds[2]; |
| 507 | |
| 508 | if (pipe(exec_error_fds) != 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 509 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 510 | String::NewFromUtf8(args.GetIsolate(), "pipe syscall failed.", |
| 511 | NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 512 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 513 | } |
| 514 | if (pipe(stdout_fds) != 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 515 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 516 | String::NewFromUtf8(args.GetIsolate(), "pipe syscall failed.", |
| 517 | NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 518 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | pid_t pid = fork(); |
| 522 | if (pid == 0) { // Child process. |
| 523 | ExecSubprocess(exec_error_fds, stdout_fds, exec_args); |
| 524 | exit(1); |
| 525 | } |
| 526 | |
| 527 | // Parent process. Ensure that we clean up if we exit this function early. |
| 528 | ZombieProtector child_waiter(pid); |
| 529 | close(exec_error_fds[kWriteFD]); |
| 530 | close(stdout_fds[kWriteFD]); |
| 531 | OpenFDCloser error_read_closer(exec_error_fds[kReadFD]); |
| 532 | OpenFDCloser stdout_read_closer(stdout_fds[kReadFD]); |
| 533 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 534 | Isolate* isolate = args.GetIsolate(); |
| 535 | if (!ChildLaunchedOK(isolate, exec_error_fds)) return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 536 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 537 | Local<Value> accumulator = GetStdout(isolate, stdout_fds[kReadFD], start_time, |
| 538 | read_timeout, total_timeout); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 539 | if (accumulator->IsUndefined()) { |
| 540 | kill(pid, SIGINT); // On timeout, kill the subprocess. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 541 | args.GetReturnValue().Set(accumulator); |
| 542 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 545 | if (!WaitForChild(isolate, pid, child_waiter, start_time, read_timeout, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 546 | total_timeout)) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 547 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 550 | args.GetReturnValue().Set(accumulator); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 554 | void Shell::ChangeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 555 | if (args.Length() != 1) { |
| 556 | const char* message = "chdir() takes one argument"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 557 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 558 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 559 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 560 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 561 | } |
| 562 | String::Utf8Value directory(args[0]); |
| 563 | if (*directory == NULL) { |
| 564 | const char* message = "os.chdir(): String conversion of argument failed."; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 565 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 566 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 567 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 568 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 569 | } |
| 570 | if (chdir(*directory) != 0) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 571 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 572 | String::NewFromUtf8(args.GetIsolate(), strerror(errno), |
| 573 | NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 574 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 575 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 579 | void Shell::SetUMask(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 580 | if (args.Length() != 1) { |
| 581 | const char* message = "umask() takes one argument"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 582 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 583 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 584 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 585 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 586 | } |
| 587 | if (args[0]->IsNumber()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 588 | #if V8_OS_NACL |
| 589 | // PNaCL has no support for umask. |
| 590 | int previous = 0; |
| 591 | #else |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 592 | int previous = umask( |
| 593 | args[0]->Int32Value(args.GetIsolate()->GetCurrentContext()).FromJust()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 594 | #endif |
| 595 | args.GetReturnValue().Set(previous); |
| 596 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 597 | } else { |
| 598 | const char* message = "umask() argument must be numeric"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 599 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 600 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 601 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 602 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 607 | static bool CheckItsADirectory(Isolate* isolate, char* directory) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 608 | struct stat stat_buf; |
| 609 | int stat_result = stat(directory, &stat_buf); |
| 610 | if (stat_result != 0) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 611 | isolate->ThrowException( |
| 612 | String::NewFromUtf8(isolate, strerror(errno), NewStringType::kNormal) |
| 613 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 614 | return false; |
| 615 | } |
| 616 | if ((stat_buf.st_mode & S_IFDIR) != 0) return true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 617 | isolate->ThrowException( |
| 618 | String::NewFromUtf8(isolate, strerror(EEXIST), NewStringType::kNormal) |
| 619 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 620 | return false; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | // Returns true for success. Creates intermediate directories as needed. No |
| 625 | // error if the directory exists already. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 626 | static bool mkdirp(Isolate* isolate, char* directory, mode_t mask) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 627 | int result = mkdir(directory, mask); |
| 628 | if (result == 0) return true; |
| 629 | if (errno == EEXIST) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 630 | return CheckItsADirectory(isolate, directory); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 631 | } else if (errno == ENOENT) { // Intermediate path element is missing. |
| 632 | char* last_slash = strrchr(directory, '/'); |
| 633 | if (last_slash == NULL) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 634 | isolate->ThrowException( |
| 635 | String::NewFromUtf8(isolate, strerror(errno), NewStringType::kNormal) |
| 636 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 637 | return false; |
| 638 | } |
| 639 | *last_slash = 0; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 640 | if (!mkdirp(isolate, directory, mask)) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 641 | *last_slash = '/'; |
| 642 | result = mkdir(directory, mask); |
| 643 | if (result == 0) return true; |
| 644 | if (errno == EEXIST) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 645 | return CheckItsADirectory(isolate, directory); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 646 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 647 | isolate->ThrowException( |
| 648 | String::NewFromUtf8(isolate, strerror(errno), NewStringType::kNormal) |
| 649 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 650 | return false; |
| 651 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 652 | isolate->ThrowException( |
| 653 | String::NewFromUtf8(isolate, strerror(errno), NewStringType::kNormal) |
| 654 | .ToLocalChecked()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 655 | return false; |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 660 | void Shell::MakeDirectory(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 661 | mode_t mask = 0777; |
| 662 | if (args.Length() == 2) { |
| 663 | if (args[1]->IsNumber()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 664 | mask = args[1] |
| 665 | ->Int32Value(args.GetIsolate()->GetCurrentContext()) |
| 666 | .FromJust(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 667 | } else { |
| 668 | const char* message = "mkdirp() second argument must be numeric"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 669 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 670 | String::NewFromUtf8(args.GetIsolate(), message, |
| 671 | NewStringType::kNormal).ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 672 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 673 | } |
| 674 | } else if (args.Length() != 1) { |
| 675 | const char* message = "mkdirp() takes one or two arguments"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 676 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 677 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 678 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 679 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 680 | } |
| 681 | String::Utf8Value directory(args[0]); |
| 682 | if (*directory == NULL) { |
| 683 | const char* message = "os.mkdirp(): String conversion of argument failed."; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 684 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 685 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 686 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 687 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 688 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 689 | mkdirp(args.GetIsolate(), *directory, mask); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 693 | void Shell::RemoveDirectory(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 694 | if (args.Length() != 1) { |
| 695 | const char* message = "rmdir() takes one or two arguments"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 696 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 697 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 698 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 699 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 700 | } |
| 701 | String::Utf8Value directory(args[0]); |
| 702 | if (*directory == NULL) { |
| 703 | const char* message = "os.rmdir(): String conversion of argument failed."; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 704 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 705 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 706 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 707 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 708 | } |
| 709 | rmdir(*directory); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 713 | void Shell::SetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 714 | if (args.Length() != 2) { |
| 715 | const char* message = "setenv() takes two arguments"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 716 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 717 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 718 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 719 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 720 | } |
| 721 | String::Utf8Value var(args[0]); |
| 722 | String::Utf8Value value(args[1]); |
| 723 | if (*var == NULL) { |
| 724 | const char* message = |
| 725 | "os.setenv(): String conversion of variable name failed."; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 726 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 727 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 728 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 729 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 730 | } |
| 731 | if (*value == NULL) { |
| 732 | const char* message = |
| 733 | "os.setenv(): String conversion of variable contents failed."; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 734 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 735 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 736 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 737 | return; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 738 | } |
| 739 | setenv(*var, *value, 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 743 | void Shell::UnsetEnvironment(const v8::FunctionCallbackInfo<v8::Value>& args) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 744 | if (args.Length() != 1) { |
| 745 | const char* message = "unsetenv() takes one argument"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 746 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 747 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 748 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 749 | return; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 750 | } |
| 751 | String::Utf8Value var(args[0]); |
| 752 | if (*var == NULL) { |
| 753 | const char* message = |
| 754 | "os.setenv(): String conversion of variable name failed."; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 755 | args.GetIsolate()->ThrowException( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 756 | String::NewFromUtf8(args.GetIsolate(), message, NewStringType::kNormal) |
| 757 | .ToLocalChecked()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 758 | return; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 759 | } |
| 760 | unsetenv(*var); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 764 | void Shell::AddOSMethods(Isolate* isolate, Local<ObjectTemplate> os_templ) { |
| 765 | os_templ->Set(String::NewFromUtf8(isolate, "system", NewStringType::kNormal) |
| 766 | .ToLocalChecked(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 767 | FunctionTemplate::New(isolate, System)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 768 | os_templ->Set(String::NewFromUtf8(isolate, "chdir", NewStringType::kNormal) |
| 769 | .ToLocalChecked(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 770 | FunctionTemplate::New(isolate, ChangeDirectory)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 771 | os_templ->Set(String::NewFromUtf8(isolate, "setenv", NewStringType::kNormal) |
| 772 | .ToLocalChecked(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 773 | FunctionTemplate::New(isolate, SetEnvironment)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 774 | os_templ->Set(String::NewFromUtf8(isolate, "unsetenv", NewStringType::kNormal) |
| 775 | .ToLocalChecked(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 776 | FunctionTemplate::New(isolate, UnsetEnvironment)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 777 | os_templ->Set(String::NewFromUtf8(isolate, "umask", NewStringType::kNormal) |
| 778 | .ToLocalChecked(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 779 | FunctionTemplate::New(isolate, SetUMask)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 780 | os_templ->Set(String::NewFromUtf8(isolate, "mkdirp", NewStringType::kNormal) |
| 781 | .ToLocalChecked(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 782 | FunctionTemplate::New(isolate, MakeDirectory)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 783 | os_templ->Set(String::NewFromUtf8(isolate, "rmdir", NewStringType::kNormal) |
| 784 | .ToLocalChecked(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 785 | FunctionTemplate::New(isolate, RemoveDirectory)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | } // namespace v8 |