| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> | 5 #include <errno.h> |
| 6 #include <fcntl.h> | 6 #include <fcntl.h> |
| 7 #include <signal.h> | 7 #include <signal.h> |
| 8 #include <stdlib.h> | 8 #include <stdlib.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 static bool WaitOnFD(int fd, | 82 static bool WaitOnFD(int fd, |
| 83 int read_timeout, | 83 int read_timeout, |
| 84 int total_timeout, | 84 int total_timeout, |
| 85 const struct timeval& start_time) { | 85 const struct timeval& start_time) { |
| 86 fd_set readfds, writefds, exceptfds; | 86 fd_set readfds, writefds, exceptfds; |
| 87 struct timeval timeout; | 87 struct timeval timeout; |
| 88 int gone = 0; | 88 int gone = 0; |
| 89 if (total_timeout != -1) { | 89 if (total_timeout != -1) { |
| 90 struct timeval time_now; | 90 struct timeval time_now; |
| 91 gettimeofday(&time_now, NULL); | 91 gettimeofday(&time_now, NULL); |
| 92 int seconds = time_now.tv_sec - start_time.tv_sec; | 92 time_t seconds = time_now.tv_sec - start_time.tv_sec; |
| 93 gone = seconds * 1000 + (time_now.tv_usec - start_time.tv_usec) / 1000; | 93 gone = static_cast<int>(seconds * 1000 + |
| 94 (time_now.tv_usec - start_time.tv_usec) / 1000); |
| 94 if (gone >= total_timeout) return false; | 95 if (gone >= total_timeout) return false; |
| 95 } | 96 } |
| 96 FD_ZERO(&readfds); | 97 FD_ZERO(&readfds); |
| 97 FD_ZERO(&writefds); | 98 FD_ZERO(&writefds); |
| 98 FD_ZERO(&exceptfds); | 99 FD_ZERO(&exceptfds); |
| 99 FD_SET(fd, &readfds); | 100 FD_SET(fd, &readfds); |
| 100 FD_SET(fd, &exceptfds); | 101 FD_SET(fd, &exceptfds); |
| 101 if (read_timeout == -1 || | 102 if (read_timeout == -1 || |
| 102 (total_timeout != -1 && total_timeout - gone < read_timeout)) { | 103 (total_timeout != -1 && total_timeout - gone < read_timeout)) { |
| 103 read_timeout = total_timeout - gone; | 104 read_timeout = total_timeout - gone; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 118 } | 119 } |
| 119 | 120 |
| 120 | 121 |
| 121 // Checks whether we ran out of time on the timeout. Returns true if we ran out | 122 // Checks whether we ran out of time on the timeout. Returns true if we ran out |
| 122 // of time, false if we still have time. | 123 // of time, false if we still have time. |
| 123 static bool TimeIsOut(const struct timeval& start_time, const int& total_time) { | 124 static bool TimeIsOut(const struct timeval& start_time, const int& total_time) { |
| 124 if (total_time == -1) return false; | 125 if (total_time == -1) return false; |
| 125 struct timeval time_now; | 126 struct timeval time_now; |
| 126 gettimeofday(&time_now, NULL); | 127 gettimeofday(&time_now, NULL); |
| 127 // Careful about overflow. | 128 // Careful about overflow. |
| 128 int seconds = time_now.tv_sec - start_time.tv_sec; | 129 int seconds = static_cast<int>(time_now.tv_sec - start_time.tv_sec); |
| 129 if (seconds > 100) { | 130 if (seconds > 100) { |
| 130 if (seconds * 1000 > total_time) return true; | 131 if (seconds * 1000 > total_time) return true; |
| 131 return false; | 132 return false; |
| 132 } | 133 } |
| 133 int useconds = time_now.tv_usec - start_time.tv_usec; | 134 int useconds = static_cast<int>(time_now.tv_usec - start_time.tv_usec); |
| 134 if (seconds * 1000000 + useconds > total_time * 1000) { | 135 if (seconds * 1000000 + useconds > total_time * 1000) { |
| 135 return true; | 136 return true; |
| 136 } | 137 } |
| 137 return false; | 138 return false; |
| 138 } | 139 } |
| 139 | 140 |
| 140 | 141 |
| 141 // A utility class that does a non-hanging waitpid on the child process if we | 142 // A utility class that does a non-hanging waitpid on the child process if we |
| 142 // bail out of the System() function early. If you don't ever do a waitpid on | 143 // bail out of the System() function early. If you don't ever do a waitpid on |
| 143 // a subprocess then it turns into one of those annoying 'zombie processes'. | 144 // a subprocess then it turns into one of those annoying 'zombie processes'. |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 close(exec_error_fds[kReadFD]); // Don't need this in the child. | 258 close(exec_error_fds[kReadFD]); // Don't need this in the child. |
| 258 close(stdout_fds[kReadFD]); // Don't need this in the child. | 259 close(stdout_fds[kReadFD]); // Don't need this in the child. |
| 259 close(1); // Close stdout. | 260 close(1); // Close stdout. |
| 260 dup2(stdout_fds[kWriteFD], 1); // Dup pipe fd to stdout. | 261 dup2(stdout_fds[kWriteFD], 1); // Dup pipe fd to stdout. |
| 261 close(stdout_fds[kWriteFD]); // Don't need the original fd now. | 262 close(stdout_fds[kWriteFD]); // Don't need the original fd now. |
| 262 fcntl(exec_error_fds[kWriteFD], F_SETFD, FD_CLOEXEC); | 263 fcntl(exec_error_fds[kWriteFD], F_SETFD, FD_CLOEXEC); |
| 263 execvp(exec_args.arg0(), exec_args.arg_array()); | 264 execvp(exec_args.arg0(), exec_args.arg_array()); |
| 264 // Only get here if the exec failed. Write errno to the parent to tell | 265 // Only get here if the exec failed. Write errno to the parent to tell |
| 265 // them it went wrong. If it went well the pipe is closed. | 266 // them it went wrong. If it went well the pipe is closed. |
| 266 int err = errno; | 267 int err = errno; |
| 267 int bytes_written; | 268 ssize_t bytes_written; |
| 268 do { | 269 do { |
| 269 bytes_written = write(exec_error_fds[kWriteFD], &err, sizeof(err)); | 270 bytes_written = write(exec_error_fds[kWriteFD], &err, sizeof(err)); |
| 270 } while (bytes_written == -1 && errno == EINTR); | 271 } while (bytes_written == -1 && errno == EINTR); |
| 271 // Return (and exit child process). | 272 // Return (and exit child process). |
| 272 } | 273 } |
| 273 | 274 |
| 274 | 275 |
| 275 // Runs in the parent process. Checks that the child was able to exec (closing | 276 // Runs in the parent process. Checks that the child was able to exec (closing |
| 276 // the file desriptor), or reports an error if it failed. | 277 // the file desriptor), or reports an error if it failed. |
| 277 static bool ChildLaunchedOK(Isolate* isolate, int* exec_error_fds) { | 278 static bool ChildLaunchedOK(Isolate* isolate, int* exec_error_fds) { |
| 278 int bytes_read; | 279 ssize_t bytes_read; |
| 279 int err; | 280 int err; |
| 280 do { | 281 do { |
| 281 bytes_read = read(exec_error_fds[kReadFD], &err, sizeof(err)); | 282 bytes_read = read(exec_error_fds[kReadFD], &err, sizeof(err)); |
| 282 } while (bytes_read == -1 && errno == EINTR); | 283 } while (bytes_read == -1 && errno == EINTR); |
| 283 if (bytes_read != 0) { | 284 if (bytes_read != 0) { |
| 284 isolate->ThrowException(String::NewFromUtf8(isolate, strerror(err))); | 285 isolate->ThrowException(String::NewFromUtf8(isolate, strerror(err))); |
| 285 return false; | 286 return false; |
| 286 } | 287 } |
| 287 return true; | 288 return true; |
| 288 } | 289 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 301 static const int kStdoutReadBufferSize = 4096; | 302 static const int kStdoutReadBufferSize = 4096; |
| 302 char buffer[kStdoutReadBufferSize]; | 303 char buffer[kStdoutReadBufferSize]; |
| 303 | 304 |
| 304 if (fcntl(child_fd, F_SETFL, O_NONBLOCK) != 0) { | 305 if (fcntl(child_fd, F_SETFL, O_NONBLOCK) != 0) { |
| 305 return isolate->ThrowException( | 306 return isolate->ThrowException( |
| 306 String::NewFromUtf8(isolate, strerror(errno))); | 307 String::NewFromUtf8(isolate, strerror(errno))); |
| 307 } | 308 } |
| 308 | 309 |
| 309 int bytes_read; | 310 int bytes_read; |
| 310 do { | 311 do { |
| 311 bytes_read = read(child_fd, | 312 bytes_read = static_cast<int>( |
| 312 buffer + fullness, | 313 read(child_fd, buffer + fullness, kStdoutReadBufferSize - fullness)); |
| 313 kStdoutReadBufferSize - fullness); | |
| 314 if (bytes_read == -1) { | 314 if (bytes_read == -1) { |
| 315 if (errno == EAGAIN) { | 315 if (errno == EAGAIN) { |
| 316 if (!WaitOnFD(child_fd, | 316 if (!WaitOnFD(child_fd, |
| 317 read_timeout, | 317 read_timeout, |
| 318 total_timeout, | 318 total_timeout, |
| 319 start_time) || | 319 start_time) || |
| 320 (TimeIsOut(start_time, total_timeout))) { | 320 (TimeIsOut(start_time, total_timeout))) { |
| 321 return isolate->ThrowException( | 321 return isolate->ThrowException( |
| 322 String::NewFromUtf8(isolate, "Timed out waiting for output")); | 322 String::NewFromUtf8(isolate, "Timed out waiting for output")); |
| 323 } | 323 } |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 717 FunctionTemplate::New(isolate, UnsetEnvironment)); | 717 FunctionTemplate::New(isolate, UnsetEnvironment)); |
| 718 os_templ->Set(String::NewFromUtf8(isolate, "umask"), | 718 os_templ->Set(String::NewFromUtf8(isolate, "umask"), |
| 719 FunctionTemplate::New(isolate, SetUMask)); | 719 FunctionTemplate::New(isolate, SetUMask)); |
| 720 os_templ->Set(String::NewFromUtf8(isolate, "mkdirp"), | 720 os_templ->Set(String::NewFromUtf8(isolate, "mkdirp"), |
| 721 FunctionTemplate::New(isolate, MakeDirectory)); | 721 FunctionTemplate::New(isolate, MakeDirectory)); |
| 722 os_templ->Set(String::NewFromUtf8(isolate, "rmdir"), | 722 os_templ->Set(String::NewFromUtf8(isolate, "rmdir"), |
| 723 FunctionTemplate::New(isolate, RemoveDirectory)); | 723 FunctionTemplate::New(isolate, RemoveDirectory)); |
| 724 } | 724 } |
| 725 | 725 |
| 726 } // namespace v8 | 726 } // namespace v8 |
| OLD | NEW |