7 #include <netinet/ip.h> 11 #include <sys/select.h> 12 #include <sys/socket.h> 15 #include <sys/types.h> 28 static int LengthWithoutIncompleteUtf8(
char* buffer,
int len) {
31 static const int kUtf8SingleByteMask = 0x80;
32 static const int kUtf8SingleByteValue = 0x00;
34 static const int kUtf8TwoByteMask = 0xE0;
35 static const int kUtf8TwoByteValue = 0xC0;
37 static const int kUtf8ThreeByteMask = 0xF0;
38 static const int kUtf8ThreeByteValue = 0xE0;
40 static const int kUtf8FourByteMask = 0xF8;
41 static const int kUtf8FourByteValue = 0xF0;
43 static const int kMultiByteMask = 0xC0;
44 static const int kMultiByteValue = 0x80;
45 int multi_byte_bytes_seen = 0;
47 int c = buffer[answer - 1];
49 if ((c & kUtf8SingleByteMask) == kUtf8SingleByteValue)
return answer;
51 if ((c & kMultiByteMask) == kMultiByteValue) {
52 multi_byte_bytes_seen++;
55 if ((c & kUtf8TwoByteMask) == kUtf8TwoByteValue) {
56 if (multi_byte_bytes_seen >= 1) {
60 }
else if ((c & kUtf8ThreeByteMask) == kUtf8ThreeByteValue) {
61 if (multi_byte_bytes_seen >= 2) {
65 }
else if ((c & kUtf8FourByteMask) == kUtf8FourByteValue) {
66 if (multi_byte_bytes_seen >= 3) {
81 static bool WaitOnFD(
int fd,
84 const struct timeval& start_time) {
85 fd_set readfds, writefds, exceptfds;
86 struct timeval timeout;
88 if (total_timeout != -1) {
89 struct timeval time_now;
90 gettimeofday(&time_now,
nullptr);
91 time_t seconds = time_now.tv_sec - start_time.tv_sec;
92 gone =
static_cast<int>(seconds * 1000 +
93 (time_now.tv_usec - start_time.tv_usec) / 1000);
94 if (gone >= total_timeout)
return false;
100 FD_SET(fd, &exceptfds);
101 if (read_timeout == -1 ||
102 (total_timeout != -1 && total_timeout - gone < read_timeout)) {
103 read_timeout = total_timeout - gone;
105 timeout.tv_usec = (read_timeout % 1000) * 1000;
106 timeout.tv_sec = read_timeout / 1000;
107 int number_of_fds_ready = select(fd + 1, &readfds, &writefds, &exceptfds,
108 read_timeout != -1 ? &timeout :
nullptr);
109 return number_of_fds_ready == 1;
115 static bool TimeIsOut(
const struct timeval& start_time,
const int& total_time) {
116 if (total_time == -1)
return false;
117 struct timeval time_now;
118 gettimeofday(&time_now,
nullptr);
120 int seconds =
static_cast<int>(time_now.tv_sec - start_time.tv_sec);
122 if (seconds * 1000 > total_time)
return true;
125 int useconds =
static_cast<int>(time_now.tv_usec - start_time.tv_usec);
126 if (seconds * 1000000 + useconds > total_time * 1000) {
140 if (pid_ != 0) waitpid(pid_,
nullptr, 0);
142 void ChildIsDeadNow() { pid_ = 0; }
163 ExecArgs() { exec_args_[0] =
nullptr; }
166 if (*prog ==
nullptr) {
167 const char* message =
168 "os.system(): String conversion of program name failed";
169 isolate->ThrowException(
174 int len = prog.length() + 3;
175 char* c_arg =
new char[len];
176 snprintf(c_arg, len,
"%s", *prog);
177 exec_args_[0] = c_arg;
179 for (
unsigned j = 0; j < command_args->Length();
i++, j++) {
181 command_args->Get(isolate->GetCurrentContext(),
182 Integer::New(isolate, j)).ToLocalChecked());
184 if (*utf8_arg ==
nullptr) {
185 exec_args_[
i] =
nullptr;
186 const char* message =
187 "os.system(): String conversion of argument failed.";
188 isolate->ThrowException(
193 int len = utf8_arg.length() + 1;
194 char* c_arg =
new char[len];
195 snprintf(c_arg, len,
"%s", *utf8_arg);
196 exec_args_[
i] = c_arg;
198 exec_args_[
i] =
nullptr;
202 for (
unsigned i = 0;
i < kMaxArgs;
i++) {
203 if (exec_args_[
i] ==
nullptr) {
206 delete [] exec_args_[
i];
207 exec_args_[
i] =
nullptr;
210 static const unsigned kMaxArgs = 1000;
211 char*
const* arg_array()
const {
return exec_args_; }
212 const char* arg0()
const {
return exec_args_[0]; }
215 char* exec_args_[kMaxArgs + 1];
222 int* total_timeout) {
223 if (args.Length() > 3) {
224 if (args[3]->IsNumber()) {
225 *total_timeout = args[3]
226 ->Int32Value(args.GetIsolate()->GetCurrentContext())
229 args.GetIsolate()->ThrowException(
231 "system: Argument 4 must be a number",
236 if (args.Length() > 2) {
237 if (args[2]->IsNumber()) {
238 *read_timeout = args[2]
239 ->Int32Value(args.GetIsolate()->GetCurrentContext())
242 args.GetIsolate()->ThrowException(
244 "system: Argument 3 must be a number",
253 static const int kReadFD = 0;
254 static const int kWriteFD = 1;
260 static void ExecSubprocess(
int* exec_error_fds,
262 const ExecArgs& exec_args) {
263 close(exec_error_fds[kReadFD]);
264 close(stdout_fds[kReadFD]);
266 dup2(stdout_fds[kWriteFD], 1);
267 close(stdout_fds[kWriteFD]);
268 fcntl(exec_error_fds[kWriteFD], F_SETFD, FD_CLOEXEC);
269 execvp(exec_args.arg0(), exec_args.arg_array());
273 ssize_t bytes_written;
275 bytes_written = write(exec_error_fds[kWriteFD], &err,
sizeof(err));
276 }
while (bytes_written == -1 && errno == EINTR);
283 static bool ChildLaunchedOK(Isolate* isolate,
int* exec_error_fds) {
287 bytes_read = read(exec_error_fds[kReadFD], &err,
sizeof(err));
288 }
while (bytes_read == -1 && errno == EINTR);
289 if (bytes_read != 0) {
290 isolate->ThrowException(
301 static Local<Value> GetStdout(Isolate* isolate,
int child_fd,
302 const struct timeval& start_time,
303 int read_timeout,
int total_timeout) {
307 static const int kStdoutReadBufferSize = 4096;
308 char buffer[kStdoutReadBufferSize];
310 if (fcntl(child_fd, F_SETFL, O_NONBLOCK) != 0) {
311 return isolate->ThrowException(
318 bytes_read =
static_cast<int>(
319 read(child_fd, buffer + fullness, kStdoutReadBufferSize - fullness));
320 if (bytes_read == -1) {
321 if (errno == EAGAIN) {
322 if (!WaitOnFD(child_fd,
326 (TimeIsOut(start_time, total_timeout))) {
327 return isolate->ThrowException(
332 }
else if (errno == EINTR) {
338 if (bytes_read + fullness > 0) {
339 int length = bytes_read == 0 ?
340 bytes_read + fullness :
341 LengthWithoutIncompleteUtf8(buffer, bytes_read + fullness);
342 Local<String> addition =
346 fullness = bytes_read + fullness - length;
347 memcpy(buffer, buffer + length, fullness);
349 }
while (bytes_read != 0);
364 #if defined(WNOWAIT) && !defined(ANDROID) && !defined(__APPLE__) && \ 365 !defined(__NetBSD__) && !defined(__Fuchsia__) 366 #if !defined(__FreeBSD__) 373 static bool WaitForChild(Isolate* isolate,
375 ZombieProtector& child_waiter,
376 const struct timeval& start_time,
381 siginfo_t child_info;
382 child_info.si_pid = 0;
385 while (child_info.si_pid == 0) {
386 waitid(P_PID, pid, &child_info, WEXITED | WNOHANG | WNOWAIT);
388 if (useconds < 1000000) useconds <<= 1;
389 if ((read_timeout != -1 && useconds / 1000 > read_timeout) ||
390 (TimeIsOut(start_time, total_timeout))) {
391 isolate->ThrowException(
393 "Timed out waiting for process to terminate",
399 if (child_info.si_code == CLD_KILLED) {
403 "Child killed by signal %d",
404 child_info.si_status);
405 isolate->ThrowException(
410 if (child_info.si_code == CLD_EXITED && child_info.si_status != 0) {
414 "Child exited with status %d",
415 child_info.si_status);
416 isolate->ThrowException(
422 #else // No waitid call. 425 waitpid(pid, &child_status, 0);
426 child_waiter.ChildIsDeadNow();
427 if (WIFSIGNALED(child_status)) {
431 "Child killed by signal %d",
432 WTERMSIG(child_status));
433 isolate->ThrowException(
438 if (WEXITSTATUS(child_status) != 0) {
440 int exit_status = WEXITSTATUS(child_status);
443 "Child exited with status %d",
445 isolate->ThrowException(
451 #endif // No waitid call. 459 HandleScope scope(args.GetIsolate());
460 int read_timeout = -1;
461 int total_timeout = -1;
462 if (!GetTimeouts(args, &read_timeout, &total_timeout))
return;
463 Local<Array> command_args;
464 if (args.Length() > 1) {
465 if (!args[1]->IsArray()) {
466 args.GetIsolate()->ThrowException(
468 "system: Argument 2 must be an array",
474 command_args =
Array::New(args.GetIsolate(), 0);
476 if (command_args->Length() > ExecArgs::kMaxArgs) {
477 args.GetIsolate()->ThrowException(
482 if (args.Length() < 1) {
483 args.GetIsolate()->ThrowException(
489 struct timeval start_time;
490 gettimeofday(&start_time,
nullptr);
493 if (!exec_args.Init(args.GetIsolate(), args[0], command_args)) {
496 int exec_error_fds[2];
499 if (pipe(exec_error_fds) != 0) {
500 args.GetIsolate()->ThrowException(
505 if (pipe(stdout_fds) != 0) {
506 args.GetIsolate()->ThrowException(
514 ExecSubprocess(exec_error_fds, stdout_fds, exec_args);
519 ZombieProtector child_waiter(pid);
520 close(exec_error_fds[kWriteFD]);
521 close(stdout_fds[kWriteFD]);
522 OpenFDCloser error_read_closer(exec_error_fds[kReadFD]);
523 OpenFDCloser stdout_read_closer(stdout_fds[kReadFD]);
525 Isolate* isolate = args.GetIsolate();
526 if (!ChildLaunchedOK(isolate, exec_error_fds))
return;
528 Local<Value> accumulator = GetStdout(isolate, stdout_fds[kReadFD], start_time,
529 read_timeout, total_timeout);
530 if (accumulator->IsUndefined()) {
532 args.GetReturnValue().Set(accumulator);
536 if (!WaitForChild(isolate, pid, child_waiter, start_time, read_timeout,
541 args.GetReturnValue().Set(accumulator);
546 if (args.Length() != 1) {
547 const char* message =
"chdir() takes one argument";
548 args.GetIsolate()->ThrowException(
553 String::Utf8Value directory(args.GetIsolate(), args[0]);
554 if (*directory ==
nullptr) {
555 const char* message =
"os.chdir(): String conversion of argument failed.";
556 args.GetIsolate()->ThrowException(
561 if (chdir(*directory) != 0) {
562 args.GetIsolate()->ThrowException(
571 if (args.Length() != 1) {
572 const char* message =
"umask() takes one argument";
573 args.GetIsolate()->ThrowException(
578 if (args[0]->IsNumber()) {
579 int previous = umask(
580 args[0]->Int32Value(args.GetIsolate()->GetCurrentContext()).FromJust());
581 args.GetReturnValue().Set(previous);
584 const char* message =
"umask() argument must be numeric";
585 args.GetIsolate()->ThrowException(
593 static bool CheckItsADirectory(Isolate* isolate,
char* directory) {
594 struct stat stat_buf;
595 int stat_result = stat(directory, &stat_buf);
596 if (stat_result != 0) {
597 isolate->ThrowException(
602 if ((stat_buf.st_mode & S_IFDIR) != 0)
return true;
603 isolate->ThrowException(
612 static bool mkdirp(Isolate* isolate,
char* directory, mode_t mask) {
613 int result = mkdir(directory, mask);
614 if (result == 0)
return true;
615 if (errno == EEXIST) {
616 return CheckItsADirectory(isolate, directory);
617 }
else if (errno == ENOENT) {
618 char* last_slash = strrchr(directory,
'/');
619 if (last_slash ==
nullptr) {
620 isolate->ThrowException(
626 if (!mkdirp(isolate, directory, mask))
return false;
628 result = mkdir(directory, mask);
629 if (result == 0)
return true;
630 if (errno == EEXIST) {
631 return CheckItsADirectory(isolate, directory);
633 isolate->ThrowException(
638 isolate->ThrowException(
648 if (args.Length() == 2) {
649 if (args[1]->IsNumber()) {
651 ->Int32Value(args.GetIsolate()->GetCurrentContext())
654 const char* message =
"mkdirp() second argument must be numeric";
655 args.GetIsolate()->ThrowException(
660 }
else if (args.Length() != 1) {
661 const char* message =
"mkdirp() takes one or two arguments";
662 args.GetIsolate()->ThrowException(
667 String::Utf8Value directory(args.GetIsolate(), args[0]);
668 if (*directory ==
nullptr) {
669 const char* message =
"os.mkdirp(): String conversion of argument failed.";
670 args.GetIsolate()->ThrowException(
675 mkdirp(args.GetIsolate(), *directory, mask);
680 if (args.Length() != 1) {
681 const char* message =
"rmdir() takes one or two arguments";
682 args.GetIsolate()->ThrowException(
687 String::Utf8Value directory(args.GetIsolate(), args[0]);
688 if (*directory ==
nullptr) {
689 const char* message =
"os.rmdir(): String conversion of argument failed.";
690 args.GetIsolate()->ThrowException(
700 if (args.Length() != 2) {
701 const char* message =
"setenv() takes two arguments";
702 args.GetIsolate()->ThrowException(
707 String::Utf8Value var(args.GetIsolate(), args[0]);
708 String::Utf8Value value(args.GetIsolate(), args[1]);
709 if (*var ==
nullptr) {
710 const char* message =
711 "os.setenv(): String conversion of variable name failed.";
712 args.GetIsolate()->ThrowException(
717 if (*value ==
nullptr) {
718 const char* message =
719 "os.setenv(): String conversion of variable contents failed.";
720 args.GetIsolate()->ThrowException(
725 setenv(*var, *value, 1);
730 if (args.Length() != 1) {
731 const char* message =
"unsetenv() takes one argument";
732 args.GetIsolate()->ThrowException(
737 String::Utf8Value var(args.GetIsolate(), args[0]);
738 if (*var ==
nullptr) {
739 const char* message =
740 "os.setenv(): String conversion of variable name failed.";
741 args.GetIsolate()->ThrowException(
749 char* Shell::ReadCharsFromTcpPort(
const char* name,
int* size_out) {
750 DCHECK_GE(Shell::options.read_from_tcp_port, 0);
752 int sockfd = socket(PF_INET, SOCK_STREAM, 0);
754 fprintf(stderr,
"Failed to create IPv4 socket\n");
760 sockaddr_in serv_addr;
761 memset(&serv_addr, 0,
sizeof(sockaddr_in));
762 serv_addr.sin_family = AF_INET;
763 serv_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
764 serv_addr.sin_port = htons(Shell::options.read_from_tcp_port);
766 if (connect(sockfd, reinterpret_cast<sockaddr*>(&serv_addr),
767 sizeof(serv_addr)) < 0) {
768 fprintf(stderr,
"Failed to connect to localhost:%d\n",
769 Shell::options.read_from_tcp_port);
792 size_t name_len = strlen(name) + 1;
793 while (sent_len < name_len) {
794 ssize_t sent_now = send(sockfd, name + sent_len, name_len - sent_len, 0);
796 fprintf(stderr,
"Failed to send %s to localhost:%d\n", name,
797 Shell::options.read_from_tcp_port);
801 sent_len += sent_now;
806 ssize_t received = 0;
810 received = recv(sockfd, &big_endian_file_length, 4, 0);
813 fprintf(stderr,
"Failed to receive %s's length from localhost:%d\n", name,
814 Shell::options.read_from_tcp_port);
819 int32_t file_length = bit_cast<int32_t>(htonl(big_endian_file_length));
821 if (file_length < 0) {
822 fprintf(stderr,
"Received length %d for %s from localhost:%d\n",
823 file_length, name, Shell::options.read_from_tcp_port);
829 char* chars =
new char[file_length];
832 ssize_t total_received = 0;
833 while (total_received < file_length) {
835 recv(sockfd, chars + total_received, file_length - total_received, 0);
837 fprintf(stderr,
"Failed to receive %s from localhost:%d\n", name,
838 Shell::options.read_from_tcp_port);
843 total_received += received;
847 *size_out = file_length;
851 void Shell::AddOSMethods(Isolate* isolate, Local<ObjectTemplate> os_templ) {
852 if (options.enable_os_system) {
static V8_INLINE Local< T > Cast(Local< S > that)
static Local< Array > New(Isolate *isolate, int length=0)
static V8_INLINE Local< String > Empty(Isolate *isolate)
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromUtf8(Isolate *isolate, const char *data, v8::NewStringType type, int length=-1)
static Local< FunctionTemplate > New(Isolate *isolate, FunctionCallback callback=nullptr, Local< Value > data=Local< Value >(), Local< Signature > signature=Local< Signature >(), int length=0, ConstructorBehavior behavior=ConstructorBehavior::kAllow, SideEffectType side_effect_type=SideEffectType::kHasSideEffect)
static Local< String > Concat(Isolate *isolate, Local< String > left, Local< String > right)