PipeRelay.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  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 "PipeRelay.h"
  17. #include <sys/select.h>
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <atomic>
  22. #include <android-base/logging.h>
  23. #include <utils/Thread.h>
  24. namespace android {
  25. namespace lshal {
  26. static constexpr struct timeval READ_TIMEOUT { .tv_sec = 1, .tv_usec = 0 };
  27. struct PipeRelay::RelayThread : public Thread {
  28. explicit RelayThread(int fd, std::ostream &os);
  29. bool threadLoop() override;
  30. void setFinished();
  31. private:
  32. int mFd;
  33. std::ostream &mOutStream;
  34. // If we were to use requestExit() and exitPending() instead, threadLoop()
  35. // may not run at all by the time ~PipeRelay is called (i.e. debug() has
  36. // returned from HAL). By using our own flag, we ensure that select() and
  37. // read() are executed until data are drained.
  38. std::atomic_bool mFinished;
  39. DISALLOW_COPY_AND_ASSIGN(RelayThread);
  40. };
  41. ////////////////////////////////////////////////////////////////////////////////
  42. PipeRelay::RelayThread::RelayThread(int fd, std::ostream &os)
  43. : mFd(fd), mOutStream(os), mFinished(false) {}
  44. bool PipeRelay::RelayThread::threadLoop() {
  45. char buffer[1024];
  46. fd_set set;
  47. FD_ZERO(&set);
  48. FD_SET(mFd, &set);
  49. struct timeval timeout = READ_TIMEOUT;
  50. int res = TEMP_FAILURE_RETRY(select(mFd + 1, &set, nullptr, nullptr, &timeout));
  51. if (res < 0) {
  52. PLOG(INFO) << "select() failed";
  53. return false;
  54. }
  55. if (res == 0 || !FD_ISSET(mFd, &set)) {
  56. if (mFinished) {
  57. LOG(WARNING) << "debug: timeout reading from pipe, output may be truncated.";
  58. return false;
  59. }
  60. // timeout, but debug() has not returned, so wait for HAL to finish.
  61. return true;
  62. }
  63. // FD_ISSET(mFd, &set) == true. Data available, start reading
  64. ssize_t n = TEMP_FAILURE_RETRY(read(mFd, buffer, sizeof(buffer)));
  65. if (n < 0) {
  66. PLOG(ERROR) << "read() failed";
  67. }
  68. if (n <= 0) {
  69. return false;
  70. }
  71. mOutStream.write(buffer, n);
  72. return true;
  73. }
  74. void PipeRelay::RelayThread::setFinished() {
  75. mFinished = true;
  76. }
  77. ////////////////////////////////////////////////////////////////////////////////
  78. PipeRelay::PipeRelay(std::ostream &os)
  79. : mInitCheck(NO_INIT) {
  80. int res = pipe(mFds);
  81. if (res < 0) {
  82. mInitCheck = -errno;
  83. return;
  84. }
  85. mThread = new RelayThread(mFds[0], os);
  86. mInitCheck = mThread->run("RelayThread");
  87. }
  88. void PipeRelay::CloseFd(int *fd) {
  89. if (*fd >= 0) {
  90. close(*fd);
  91. *fd = -1;
  92. }
  93. }
  94. PipeRelay::~PipeRelay() {
  95. CloseFd(&mFds[1]);
  96. if (mThread != nullptr) {
  97. mThread->setFinished();
  98. mThread->join();
  99. mThread.clear();
  100. }
  101. CloseFd(&mFds[0]);
  102. }
  103. status_t PipeRelay::initCheck() const {
  104. return mInitCheck;
  105. }
  106. int PipeRelay::fd() const {
  107. return mFds[1];
  108. }
  109. } // namespace lshal
  110. } // namespace android