You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

duplex_pipe.cc 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/duplex_pipe.h"
  17. #include <sys/wait.h>
  18. #include <iostream>
  19. #include <vector>
  20. #include <algorithm>
  21. namespace mindspore {
  22. int DuplexPipe::Open(std::initializer_list<std::string> arg_list, bool append_fds) {
  23. if (pipe(fd1_) == -1) {
  24. DP_EXCEPTION << "pipe 1 failed, errno: " << errno;
  25. }
  26. if (pipe(fd2_) == -1) {
  27. close(fd1_[0]);
  28. close(fd1_[1]);
  29. DP_EXCEPTION << "pipe 2 failed, errno: " << errno;
  30. }
  31. pid_ = fork();
  32. if (pid_ < 0) {
  33. close(fd1_[0]);
  34. close(fd1_[1]);
  35. close(fd2_[0]);
  36. close(fd2_[1]);
  37. DP_EXCEPTION << "fork failed, errno: " << errno;
  38. } else if (pid_ == 0) { // Remote process
  39. remote_stdout_ = dup(STDOUT_FILENO);
  40. remote_stdin_ = dup(STDIN_FILENO);
  41. close(fd1_[1]);
  42. close(fd2_[0]);
  43. if (!append_fds) {
  44. dup2(fd1_[0], STDIN_FILENO);
  45. dup2(fd2_[1], STDOUT_FILENO);
  46. }
  47. std::vector<const char *> args;
  48. std::transform(arg_list.begin(), arg_list.end(), std::back_inserter(args),
  49. [](const std::string &arg) -> const char * { return arg.c_str(); });
  50. if (append_fds) {
  51. std::string fd10 = std::to_string(fd1_[0]).c_str();
  52. args.emplace_back(fd10.c_str());
  53. std::string fd21 = std::to_string(fd2_[1]).c_str();
  54. args.emplace_back(fd21.c_str());
  55. }
  56. args.emplace_back(nullptr);
  57. if (execvp(args[0], const_cast<char *const *>(&args[0])) == -1) {
  58. DP_EXCEPTION << "execute " << args[0] << " failed, errno: " << errno;
  59. }
  60. } else { // Local process
  61. DP_INFO << "Local process, id: " << getpid() << ", " << fd2_[0] << "/" << fd1_[1];
  62. local_stdout_ = dup(STDOUT_FILENO);
  63. local_stdin_ = dup(STDIN_FILENO);
  64. close(fd1_[0]);
  65. close(fd2_[1]);
  66. signal_handler_ = std::make_shared<SignalHandler>(shared_from_this(), pid_);
  67. }
  68. return 0;
  69. }
  70. void DuplexPipe::Write(const std::string &buf, bool flush) {
  71. // Write the string into pipe
  72. if (write(fd1_[1], buf.data(), buf.size()) == -1) {
  73. DP_ERROR << "write failed, errno: " << errno;
  74. return;
  75. }
  76. if (flush) {
  77. // Flush into the pipe
  78. if (write(fd1_[1], "\n", 1) == -1) {
  79. DP_ERROR << "write failed, errno: " << errno;
  80. return;
  81. }
  82. }
  83. DP_DEBUG << "<< [" << buf << "]";
  84. }
  85. std::string DuplexPipe::Read() {
  86. // Read the string from pipe
  87. std::string buf;
  88. // Read one line or multiple lines
  89. while (1) {
  90. SetTimeOut();
  91. ssize_t size = read(fd2_[0], c_buf_, kBufferSize); // MAYBE BLOCKED, Till reading something
  92. if (size <= 0) {
  93. break;
  94. }
  95. CancelTimeOut();
  96. bool line_end = c_buf_[size - 1] == '\n';
  97. buf.append(c_buf_, line_end ? size - 1 : size); // Copy without the last '\n'
  98. if (line_end) {
  99. break;
  100. }
  101. }
  102. DP_DEBUG << ">> [" << buf << "]";
  103. return buf;
  104. }
  105. void DuplexPipe::WriteWithStdout(const std::string &buf, bool flush) {
  106. dup2(fd1_[1], STDOUT_FILENO);
  107. // Write the string into pipe
  108. std::cout << buf;
  109. if (flush) {
  110. // Flush into the pipe
  111. std::cout << std::endl;
  112. }
  113. dup2(local_stdout_, STDOUT_FILENO);
  114. }
  115. std::string DuplexPipe::ReadWithStdin() {
  116. std::string buf;
  117. dup2(fd2_[0], STDIN_FILENO);
  118. // Maybe blocked
  119. SetTimeOut();
  120. std::getline(std::cin, buf); // Not use 'std::cin >>' to include space
  121. CancelTimeOut();
  122. dup2(local_stdin_, STDIN_FILENO);
  123. return buf;
  124. }
  125. DuplexPipe &DuplexPipe::operator<<(const std::string &buf) {
  126. Write(buf);
  127. return *this;
  128. }
  129. DuplexPipe &DuplexPipe::operator>>(std::string &buf) {
  130. buf = Read();
  131. return *this;
  132. }
  133. void DuplexPipe::Close() {
  134. close(fd1_[0]);
  135. close(fd1_[1]);
  136. close(fd2_[0]);
  137. close(fd2_[1]);
  138. }
  139. DuplexPipe::SignalHandler::SignalHandler(std::shared_ptr<DuplexPipe> dp, pid_t pid) {
  140. dp_ = dp;
  141. child_pid_ = pid;
  142. signal(SIGCHLD, SigChildHandler);
  143. signal(SIGPIPE, SigPipeHandler);
  144. }
  145. DuplexPipe::SignalHandler::~SignalHandler() { dp_.reset(); }
  146. void DuplexPipe::SignalHandler::SetAlarm(unsigned int interval_secs) {
  147. signal(SIGALRM, SigAlarmHandler);
  148. alarm(interval_secs);
  149. }
  150. void DuplexPipe::SignalHandler::CancelAlarm() { alarm(0); }
  151. void DuplexPipe::SignalHandler::SigAlarmHandler(int sig) {
  152. DP_INFO << "Signal: " << sig << ", child_pid_: " << child_pid_;
  153. if (dp_ != nullptr) {
  154. dp_->NotifyTimeOut();
  155. }
  156. }
  157. void DuplexPipe::SignalHandler::SigPipeHandler(int sig) {
  158. DP_INFO << "Signal: " << sig << ", child_pid_: " << child_pid_;
  159. if (dp_ != nullptr) {
  160. dp_->NotifyFinalize();
  161. }
  162. }
  163. void DuplexPipe::SignalHandler::SigChildHandler(int sig) {
  164. int status;
  165. (void)waitpid(child_pid_, &status, WNOHANG | WUNTRACED);
  166. }
  167. } // namespace mindspore