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

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

Issue 21816002: Add Process.runSync for running processe synchronously. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixed Mac OS issue 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
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,
571 intptr_t* buffer_length,
572 intptr_t available,
573 int fd) {
574 uint8_t* tmp = new uint8_t[*buffer_length + available];
575 if (tmp == NULL) FATAL("Allocation failed");
576 memmove(tmp, *buffer, *buffer_length);
577 delete[] *buffer;
578 *buffer = tmp;
579 intptr_t bytes = TEMP_FAILURE_RETRY(read(
580 fd,
581 reinterpret_cast<void*>(*buffer + *buffer_length),
582 available));
583 if (bytes < 0) return false;
584 *buffer_length += bytes;
585 return true;
586 }
587
588
589 static bool CloseProcessBuffers(struct pollfd fds[3]) {
590 int e = errno;
591 VOID_TEMP_FAILURE_RETRY(close(fds[0].fd));
592 VOID_TEMP_FAILURE_RETRY(close(fds[1].fd));
593 VOID_TEMP_FAILURE_RETRY(close(fds[2].fd));
594 errno = e;
595 return false;
596 }
597
598
599 bool Process::Wait(intptr_t pid,
600 intptr_t in,
601 intptr_t out,
602 intptr_t err,
603 intptr_t exit_event,
604 ProcessResult* result) {
605 // Close input to the process right away.
606 VOID_TEMP_FAILURE_RETRY(close(in));
607
608 uint8_t* out_data = NULL;
609 intptr_t out_data_length = 0;
610 uint8_t* err_data = NULL;
611 intptr_t err_data_length = 0;
612 union {
613 uint8_t bytes[8];
614 int32_t ints[2];
615 } exit_code_data;
616
617 struct pollfd fds[3];
618 fds[0].fd = out;
619 fds[1].fd = err;
620 fds[2].fd = exit_event;
621
622 for (int i = 0; i < 3; i++) {
623 fds[i].events = POLLIN;
624 }
625
626 int alive = 3;
627 while (alive > 0) {
628 if (TEMP_FAILURE_RETRY(poll(fds, alive, -1)) <= 0) {
629 return CloseProcessBuffers(fds);
630 }
631 for (int i = 0; i < alive; i++) {
632 if (fds[i].revents & POLLIN) {
633 intptr_t avail = FDUtils::AvailableBytes(fds[i].fd);
634 // On Mac OS POLLIN can be set with zero available
635 // bytes. POLLHUP is most likely also set in this case.
636 if (avail > 0) {
637 if (fds[i].fd == out) {
638 if (!ReadProcessBuffer(&out_data, &out_data_length, avail, out)) {
639 return CloseProcessBuffers(fds);
640 }
641 } else if (fds[i].fd == err) {
642 if (!ReadProcessBuffer(&err_data, &err_data_length, avail, err)) {
643 return CloseProcessBuffers(fds);
644 }
645 } else if (fds[i].fd == exit_event) {
646 intptr_t avail = FDUtils::AvailableBytes(fds[i].fd);
647 if (avail == 8) {
648 intptr_t bytes = 0;
649 do {
650 intptr_t b = TEMP_FAILURE_RETRY(read(fds[i].fd,
651 exit_code_data.bytes, 8));
Bill Hesse 2013/08/09 15:32:23 Ditto - + bytes
Søren Gjesse 2013/08/12 06:52:38 Removed the loop here as well.
652 if (b > 0) {
653 bytes += b;
654 } else {
655 return CloseProcessBuffers(fds);
656 }
657 } while (bytes < 8);
658 }
659 } else {
660 UNREACHABLE();
661 }
662 continue;
663 }
664 }
665
666 if (fds[i].revents & POLLHUP) {
667 VOID_TEMP_FAILURE_RETRY(close(fds[i].fd));
668 alive--;
669 if (i < alive) {
670 fds[i] = fds[alive];
671 }
672 }
673 }
674 }
675
676 // All handles closed and all data read.
677 result->SetStdoutData(out_data, out_data_length);
678 result->SetStderrData(err_data, err_data_length);
679
680 // Calculate the exit code.
681 intptr_t exit_code = exit_code_data.ints[0];
682 intptr_t negative = exit_code_data.ints[1];
683 if (negative) exit_code = -exit_code;
684 result->set_exit_code(exit_code);
685
686 return true;
687 }
688
689
570 bool Process::Kill(intptr_t id, int signal) { 690 bool Process::Kill(intptr_t id, int signal) {
571 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1); 691 return (TEMP_FAILURE_RETRY(kill(id, signal)) != -1);
572 } 692 }
573 693
574 694
575 void Process::TerminateExitCodeHandler() { 695 void Process::TerminateExitCodeHandler() {
576 ExitCodeHandler::TerminateExitCodeThread(); 696 ExitCodeHandler::TerminateExitCodeThread();
577 } 697 }
578 698
579 699
580 intptr_t Process::CurrentProcessId() { 700 intptr_t Process::CurrentProcessId() {
581 return static_cast<intptr_t>(getpid()); 701 return static_cast<intptr_t>(getpid());
582 } 702 }
583 703
584 } // namespace bin 704 } // namespace bin
585 } // namespace dart 705 } // namespace dart
586 706
587 #endif // defined(TARGET_OS_MACOS) 707 #endif // defined(TARGET_OS_MACOS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698