Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(752)

Side by Side Diff: runtime/bin/process_macos.cc

Issue 22827002: Change the allocation of the stdout and stderr collected by Process.runSync (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor fixes Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/process_linux.cc ('k') | runtime/bin/process_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(TARGET_OS_MACOS) 6 #if defined(TARGET_OS_MACOS)
7 7
8 #include "bin/process.h" 8 #include "bin/process.h"
9 9
10 #include <errno.h> // NOLINT 10 #include <errno.h> // NOLINT
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 VOID_TEMP_FAILURE_RETRY(close(write_out[0])); 560 VOID_TEMP_FAILURE_RETRY(close(write_out[0]));
561 FDUtils::SetNonBlocking(read_err[0]); 561 FDUtils::SetNonBlocking(read_err[0]);
562 *err = read_err[0]; 562 *err = read_err[0];
563 VOID_TEMP_FAILURE_RETRY(close(read_err[1])); 563 VOID_TEMP_FAILURE_RETRY(close(read_err[1]));
564 564
565 *id = pid; 565 *id = pid;
566 return 0; 566 return 0;
567 } 567 }
568 568
569 569
570 static bool ReadProcessBuffer(uint8_t** buffer, 570 class BufferList: public BufferListBase {
571 intptr_t* buffer_length, 571 public:
572 intptr_t available, 572 bool Read(int fd, intptr_t available) {
573 int fd) { 573 // Read all available bytes.
574 uint8_t* tmp = new uint8_t[*buffer_length + available]; 574 while (available > 0) {
575 if (tmp == NULL) FATAL("Allocation failed"); 575 if (free_size_ == 0) Allocate();
576 memmove(tmp, *buffer, *buffer_length); 576 ASSERT(free_size_ > 0);
577 delete[] *buffer; 577 ASSERT(free_size_ <= kBufferSize);
578 *buffer = tmp; 578 intptr_t block_size = dart::Utils::Minimum(free_size_, available);
579 intptr_t bytes = TEMP_FAILURE_RETRY(read( 579 intptr_t bytes = TEMP_FAILURE_RETRY(read(
580 fd, 580 fd,
581 reinterpret_cast<void*>(*buffer + *buffer_length), 581 reinterpret_cast<void*>(tail_->data_ + (kBufferSize - free_size_)),
582 available)); 582 block_size));
583 if (bytes < 0) return false; 583 if (bytes < 0) return false;
584 *buffer_length += bytes; 584 data_size_ += bytes;
585 return true; 585 free_size_ -= bytes;
586 } 586 available -= bytes;
587 }
588 return true;
589 }
590 };
587 591
588 592
589 static bool CloseProcessBuffers(struct pollfd fds[3]) { 593 static bool CloseProcessBuffers(struct pollfd fds[3]) {
590 int e = errno; 594 int e = errno;
591 VOID_TEMP_FAILURE_RETRY(close(fds[0].fd)); 595 VOID_TEMP_FAILURE_RETRY(close(fds[0].fd));
592 VOID_TEMP_FAILURE_RETRY(close(fds[1].fd)); 596 VOID_TEMP_FAILURE_RETRY(close(fds[1].fd));
593 VOID_TEMP_FAILURE_RETRY(close(fds[2].fd)); 597 VOID_TEMP_FAILURE_RETRY(close(fds[2].fd));
594 errno = e; 598 errno = e;
595 return false; 599 return false;
596 } 600 }
597 601
598 602
599 bool Process::Wait(intptr_t pid, 603 bool Process::Wait(intptr_t pid,
600 intptr_t in, 604 intptr_t in,
601 intptr_t out, 605 intptr_t out,
602 intptr_t err, 606 intptr_t err,
603 intptr_t exit_event, 607 intptr_t exit_event,
604 ProcessResult* result) { 608 ProcessResult* result) {
605 // Close input to the process right away. 609 // Close input to the process right away.
606 VOID_TEMP_FAILURE_RETRY(close(in)); 610 VOID_TEMP_FAILURE_RETRY(close(in));
607 611
608 uint8_t* out_data = NULL; 612 // There is no return from this function using Dart_PropagateError
609 intptr_t out_data_length = 0; 613 // as memory used by the buffer lists is freed through their
610 uint8_t* err_data = NULL; 614 // destructors.
611 intptr_t err_data_length = 0; 615 BufferList out_data;
616 BufferList err_data;
612 union { 617 union {
613 uint8_t bytes[8]; 618 uint8_t bytes[8];
614 int32_t ints[2]; 619 int32_t ints[2];
615 } exit_code_data; 620 } exit_code_data;
616 621
617 struct pollfd fds[3]; 622 struct pollfd fds[3];
618 fds[0].fd = out; 623 fds[0].fd = out;
619 fds[1].fd = err; 624 fds[1].fd = err;
620 fds[2].fd = exit_event; 625 fds[2].fd = exit_event;
621 626
622 for (int i = 0; i < 3; i++) { 627 for (int i = 0; i < 3; i++) {
623 fds[i].events = POLLIN; 628 fds[i].events = POLLIN;
624 } 629 }
625 630
626 int alive = 3; 631 int alive = 3;
627 while (alive > 0) { 632 while (alive > 0) {
628 // Blocking call waiting for events from the child process. 633 // Blocking call waiting for events from the child process.
629 if (TEMP_FAILURE_RETRY(poll(fds, alive, -1)) <= 0) { 634 if (TEMP_FAILURE_RETRY(poll(fds, alive, -1)) <= 0) {
630 return CloseProcessBuffers(fds); 635 return CloseProcessBuffers(fds);
631 } 636 }
637
638 // Process incoming data.
632 for (int i = 0; i < alive; i++) { 639 for (int i = 0; i < alive; i++) {
633 if (fds[i].revents & POLLIN) { 640 if (fds[i].revents & POLLIN) {
634 intptr_t avail = FDUtils::AvailableBytes(fds[i].fd); 641 intptr_t avail = FDUtils::AvailableBytes(fds[i].fd);
635 // On Mac OS POLLIN can be set with zero available 642 // On Mac OS POLLIN can be set with zero available
636 // bytes. POLLHUP is most likely also set in this case. 643 // bytes. POLLHUP is most likely also set in this case.
637 if (avail > 0) { 644 if (avail > 0) {
638 if (fds[i].fd == out) { 645 if (fds[i].fd == out) {
639 if (!ReadProcessBuffer(&out_data, &out_data_length, avail, out)) { 646 if (!out_data.Read(out, avail)) {
640 return CloseProcessBuffers(fds); 647 return CloseProcessBuffers(fds);
641 } 648 }
642 } else if (fds[i].fd == err) { 649 } else if (fds[i].fd == err) {
643 if (!ReadProcessBuffer(&err_data, &err_data_length, avail, err)) { 650 if (!err_data.Read(err, avail)) {
644 return CloseProcessBuffers(fds); 651 return CloseProcessBuffers(fds);
645 } 652 }
646 } else if (fds[i].fd == exit_event) { 653 } else if (fds[i].fd == exit_event) {
647 intptr_t avail = FDUtils::AvailableBytes(fds[i].fd);
648 if (avail == 8) { 654 if (avail == 8) {
649 intptr_t b = TEMP_FAILURE_RETRY(read(fds[i].fd, 655 intptr_t b = TEMP_FAILURE_RETRY(read(fds[i].fd,
650 exit_code_data.bytes, 8)); 656 exit_code_data.bytes, 8));
651 if (b != 8) { 657 if (b != 8) {
652 return CloseProcessBuffers(fds); 658 return CloseProcessBuffers(fds);
653 } 659 }
654 } 660 }
655 } else { 661 } else {
656 UNREACHABLE(); 662 UNREACHABLE();
657 } 663 }
658 continue;
659 } 664 }
660 } 665 }
666 }
661 667
668 // Process closed.
669 for (int i = 0; i < alive; i++) {
662 if (fds[i].revents & POLLHUP) { 670 if (fds[i].revents & POLLHUP) {
663 VOID_TEMP_FAILURE_RETRY(close(fds[i].fd)); 671 VOID_TEMP_FAILURE_RETRY(close(fds[i].fd));
664 alive--; 672 alive--;
665 if (i < alive) { 673 if (i < alive) {
666 fds[i] = fds[alive]; 674 fds[i] = fds[alive];
667 } 675 }
668 } 676 }
669 } 677 }
670 } 678 }
671 679
672 // All handles closed and all data read. 680 // All handles closed and all data read.
673 result->SetStdoutData(out_data, out_data_length); 681 result->set_stdout_data(out_data.GetData());
674 result->SetStderrData(err_data, err_data_length); 682 result->set_stderr_data(err_data.GetData());
675 683
676 // Calculate the exit code. 684 // Calculate the exit code.
677 intptr_t exit_code = exit_code_data.ints[0]; 685 intptr_t exit_code = exit_code_data.ints[0];
678 intptr_t negative = exit_code_data.ints[1]; 686 intptr_t negative = exit_code_data.ints[1];
679 if (negative) exit_code = -exit_code; 687 if (negative) exit_code = -exit_code;
680 result->set_exit_code(exit_code); 688 result->set_exit_code(exit_code);
681 689
682 return true; 690 return true;
683 } 691 }
684 692
685 693
686 bool Process::Kill(intptr_t id, int signal) { 694 bool Process::Kill(intptr_t id, int signal) {
687 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1); 695 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1);
688 } 696 }
689 697
690 698
691 void Process::TerminateExitCodeHandler() { 699 void Process::TerminateExitCodeHandler() {
692 ExitCodeHandler::TerminateExitCodeThread(); 700 ExitCodeHandler::TerminateExitCodeThread();
693 } 701 }
694 702
695 703
696 intptr_t Process::CurrentProcessId() { 704 intptr_t Process::CurrentProcessId() {
697 return static_cast<intptr_t>(getpid()); 705 return static_cast<intptr_t>(getpid());
698 } 706 }
699 707
700 } // namespace bin 708 } // namespace bin
701 } // namespace dart 709 } // namespace dart
702 710
703 #endif // defined(TARGET_OS_MACOS) 711 #endif // defined(TARGET_OS_MACOS)
OLDNEW
« no previous file with comments | « runtime/bin/process_linux.cc ('k') | runtime/bin/process_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698