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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include "utils/convert_utils_base.h"
  22. namespace mindspore {
  23. DuplexPipe::~DuplexPipe() {
  24. // pid_ < 0 means the child process is invalid or closed, pid_ == 0 means this process is child
  25. if (pid_ > 0) {
  26. (void)kill(pid_, SIGKILL);
  27. }
  28. }
  29. int DuplexPipe::Open(const std::initializer_list<std::string> &arg_list, bool append_fds) {
  30. if (pipe(fd1_) == -1) {
  31. DP_EXCEPTION << "pipe 1 failed, errno: " << errno;
  32. }
  33. if (pipe(fd2_) == -1) {
  34. close(fd1_[0]);
  35. close(fd1_[1]);
  36. DP_EXCEPTION << "pipe 2 failed, errno: " << errno;
  37. }
  38. pid_ = fork();
  39. if (pid_ < 0) {
  40. close(fd1_[0]);
  41. close(fd1_[1]);
  42. close(fd2_[0]);
  43. close(fd2_[1]);
  44. DP_EXCEPTION << "fork failed, errno: " << errno;
  45. } else if (pid_ == 0) { // Remote process
  46. // Here cannot record log before execvp called, because glog will warn "File Exist" if log to file.
  47. remote_stdout_ = dup(STDOUT_FILENO);
  48. remote_stdin_ = dup(STDIN_FILENO);
  49. close(fd1_[1]);
  50. close(fd2_[0]);
  51. if (!append_fds) {
  52. dup2(fd1_[0], STDIN_FILENO);
  53. dup2(fd2_[1], STDOUT_FILENO);
  54. }
  55. std::vector<const char *> args;
  56. std::transform(arg_list.begin(), arg_list.end(), std::back_inserter(args),
  57. [](const std::string &arg) -> const char * { return arg.c_str(); });
  58. if (append_fds) {
  59. std::string fd10 = std::to_string(fd1_[0]).c_str();
  60. args.emplace_back(fd10.c_str());
  61. std::string fd21 = std::to_string(fd2_[1]).c_str();
  62. args.emplace_back(fd21.c_str());
  63. }
  64. args.emplace_back(nullptr);
  65. if (execvp(args[0], const_cast<char *const *>(&args[0])) == -1) {
  66. DP_EXCEPTION << "execute " << args[0] << " failed, errno: " << errno;
  67. }
  68. } else { // Local process
  69. DP_INFO << "Local process, id: " << getpid() << ", " << fd2_[0] << "/" << fd1_[1];
  70. local_stdout_ = dup(STDOUT_FILENO);
  71. local_stdin_ = dup(STDIN_FILENO);
  72. close(fd1_[0]);
  73. close(fd2_[1]);
  74. signal_handler_ = std::make_shared<SignalHandler>(weak_from_this(), &pid_);
  75. }
  76. return 0;
  77. }
  78. void DuplexPipe::Write(const std::string &buf, bool flush) const {
  79. // Write the string into pipe
  80. if (write(fd1_[1], buf.data(), buf.size()) == -1) {
  81. DP_ERROR << "write failed, errno: " << errno;
  82. return;
  83. }
  84. if (flush) {
  85. // Flush into the pipe
  86. if (write(fd1_[1], "\n", 1) == -1) {
  87. DP_ERROR << "write failed, errno: " << errno;
  88. return;
  89. }
  90. }
  91. DP_DEBUG << "<< [" << buf << "]";
  92. }
  93. std::string DuplexPipe::Read() {
  94. // Read the string from pipe
  95. std::string buf;
  96. // Read one line or multiple lines
  97. while (1) {
  98. SetTimeOut();
  99. ssize_t size = read(fd2_[0], c_buf_, kBufferSize); // MAYBE BLOCKED, Till reading something
  100. if (size <= 0) {
  101. break;
  102. }
  103. CancelTimeOut();
  104. bool line_end = c_buf_[size - 1] == '\n';
  105. buf.append(c_buf_, LongToSize(line_end ? size - 1 : size)); // Copy without the last '\n'
  106. if (line_end) {
  107. break;
  108. }
  109. }
  110. DP_DEBUG << ">> [" << buf << "]";
  111. return buf;
  112. }
  113. void DuplexPipe::WriteWithStdout(const std::string &buf, bool flush) {
  114. dup2(fd1_[1], STDOUT_FILENO);
  115. // Write the string into pipe
  116. std::cout << buf;
  117. if (flush) {
  118. // Flush into the pipe
  119. std::cout << std::endl;
  120. }
  121. dup2(local_stdout_, STDOUT_FILENO);
  122. }
  123. std::string DuplexPipe::ReadWithStdin() {
  124. std::string buf;
  125. dup2(fd2_[0], STDIN_FILENO);
  126. // Maybe blocked
  127. SetTimeOut();
  128. std::getline(std::cin, buf); // Not use 'std::cin >>' to include space
  129. CancelTimeOut();
  130. dup2(local_stdin_, STDIN_FILENO);
  131. return buf;
  132. }
  133. DuplexPipe &DuplexPipe::operator<<(const std::string &buf) {
  134. Write(buf);
  135. return *this;
  136. }
  137. DuplexPipe &DuplexPipe::operator>>(std::string &buf) {
  138. buf = Read();
  139. return *this;
  140. }
  141. void DuplexPipe::Close() {
  142. close(fd1_[0]);
  143. close(fd1_[1]);
  144. close(fd2_[0]);
  145. close(fd2_[1]);
  146. pid_ = -1;
  147. }
  148. DuplexPipe::SignalHandler::SignalHandler(const std::weak_ptr<DuplexPipe> &dp, pid_t *pid) {
  149. dp_ = dp;
  150. child_pid_ = pid;
  151. signal(SIGCHLD, SigChildHandler);
  152. signal(SIGPIPE, SigPipeHandler);
  153. }
  154. DuplexPipe::SignalHandler::~SignalHandler() {}
  155. void DuplexPipe::SignalHandler::SetAlarm(unsigned int interval_secs) const {
  156. signal(SIGALRM, SigAlarmHandler);
  157. alarm(interval_secs);
  158. }
  159. void DuplexPipe::SignalHandler::CancelAlarm() const { (void)alarm(0); }
  160. void DuplexPipe::SignalHandler::SigAlarmHandler(int sig) {
  161. DP_INFO << "Signal: " << sig << ", child_pid_: " << child_pid_;
  162. auto shared_dp = dp_.lock();
  163. if (shared_dp != nullptr) {
  164. shared_dp->NotifyTimeOut();
  165. }
  166. if (child_pid_ != nullptr) {
  167. *child_pid_ = -1;
  168. }
  169. }
  170. void DuplexPipe::SignalHandler::SigPipeHandler(int sig) {
  171. DP_INFO << "Signal: " << sig << ", child_pid_: " << child_pid_;
  172. auto shared_dp = dp_.lock();
  173. if (shared_dp != nullptr) {
  174. shared_dp->NotifyFinalize();
  175. }
  176. if (child_pid_ != nullptr) {
  177. *child_pid_ = -1;
  178. }
  179. }
  180. void DuplexPipe::SignalHandler::SigChildHandler(int) {
  181. int status;
  182. if (child_pid_ != nullptr) {
  183. (void)waitpid(*child_pid_, &status, WNOHANG | WUNTRACED);
  184. *child_pid_ = -1;
  185. }
  186. }
  187. } // namespace mindspore