os.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2016 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 <algorithm>
  17. #include <cerrno>
  18. #include <cstdint>
  19. #include <cstring>
  20. #include "android-base/logging.h"
  21. #include "wifilogd/local_utils.h"
  22. #include "wifilogd/os.h"
  23. namespace android {
  24. namespace wifilogd {
  25. using local_utils::GetMaxVal;
  26. namespace {
  27. constexpr auto kMaxNanoSeconds = 1000 * 1000 * 1000 - 1;
  28. }
  29. constexpr int Os::kInvalidFd;
  30. Os::Os() : raw_os_(new RawOs()) {}
  31. Os::Os(std::unique_ptr<RawOs> raw_os) : raw_os_(std::move(raw_os)) {}
  32. Os::~Os() {}
  33. std::tuple<int, Os::Errno> Os::GetControlSocket(
  34. const std::string& socket_name) {
  35. int sock_fd = raw_os_->GetControlSocket(socket_name.c_str());
  36. if (sock_fd < 0) {
  37. return {kInvalidFd, errno};
  38. } else {
  39. return {sock_fd, 0};
  40. }
  41. }
  42. Os::Timestamp Os::GetTimestamp(clockid_t clock_id) const {
  43. struct timespec now_timespec;
  44. int failed = raw_os_->ClockGettime(clock_id, &now_timespec);
  45. if (failed) {
  46. LOG(FATAL) << "Unexpected error: " << std::strerror(errno);
  47. }
  48. CHECK(now_timespec.tv_nsec <= kMaxNanoSeconds);
  49. Timestamp now_timestamp;
  50. now_timestamp.secs = SAFELY_CLAMP(
  51. now_timespec.tv_sec, uint32_t, 0,
  52. // The upper-bound comes from the source-type on 32-bit platforms,
  53. // and the dest-type on 64-bit platforms. Using min(), we can figure out
  54. // which type to use for the upper bound, without resorting to macros.
  55. std::min(static_cast<uintmax_t>(GetMaxVal(now_timestamp.secs)),
  56. static_cast<uintmax_t>(GetMaxVal(now_timespec.tv_sec))));
  57. now_timestamp.nsecs =
  58. SAFELY_CLAMP(now_timespec.tv_nsec, uint32_t, 0, kMaxNanoSeconds);
  59. return now_timestamp;
  60. }
  61. void Os::Nanosleep(uint32_t sleep_time_nsec) {
  62. struct timespec sleep_timespec = {
  63. 0, // tv_sec
  64. SAFELY_CLAMP(sleep_time_nsec, decltype(timespec::tv_nsec), 0, kMaxNanos)};
  65. int failed = 0;
  66. do {
  67. struct timespec remaining_timespec;
  68. failed = raw_os_->Nanosleep(&sleep_timespec, &remaining_timespec);
  69. sleep_timespec = remaining_timespec;
  70. } while (failed && errno == EINTR && sleep_timespec.tv_nsec > 0);
  71. if (failed && errno != EINTR) {
  72. // The only other documented errors for the underlying nanosleep() call are
  73. // EFAULT and EINVAL. But we always pass valid pointers, and the values in
  74. // |sleep_timespec| are always valid.
  75. LOG(FATAL) << "Unexpected error: " << std::strerror(errno);
  76. }
  77. }
  78. std::tuple<size_t, Os::Errno> Os::ReceiveDatagram(int fd, void* buf,
  79. size_t buflen) {
  80. // recv() takes a size_t, but returns an ssize_t. That means that the largest
  81. // successful read that recv() can report is the maximal ssize_t. Passing a
  82. // larger |buflen| risks mistakenly reporting a truncated read.
  83. CHECK(buflen <= GetMaxVal<ssize_t>());
  84. const ssize_t res = raw_os_->Recv(fd, buf, buflen, MSG_TRUNC);
  85. if (res < 0) {
  86. return {0, errno};
  87. }
  88. // Due to the MSG_TRUNC flag, |res| may reasonably be larger than
  89. // |buflen|. In such cases, |res| indicates the full size of the datagram,
  90. // before being truncated to fit our buffer. Hence, we omit the
  91. // buffer-overflow CHECK that exists in Write().
  92. return {res, 0};
  93. }
  94. std::tuple<size_t, Os::Errno> Os::Write(int fd, const void* buf,
  95. size_t buflen) {
  96. // write() takes a size_t, but returns an ssize_t. That means that the
  97. // largest successful write that write() can report is the maximal ssize_t.
  98. // Passing a larger |buflen| risks mistakenly reporting a truncated write.
  99. CHECK(buflen <= GetMaxVal<ssize_t>());
  100. const ssize_t res = raw_os_->Write(fd, buf, buflen);
  101. if (res < 0) {
  102. return {0, errno};
  103. }
  104. CHECK(res <=
  105. SAFELY_CLAMP(buflen, ssize_t, 0,
  106. GetMaxVal<ssize_t>())); // Abort on buffer overflow.
  107. // Note that |res| may be less than buflen. However, a) a short write is
  108. // not an error, and b) |errno| may be stale, as |errno| is only guaranteed to
  109. // be set if an error occurred. Hence, we return Errno of 0 unconditionally.
  110. // See http://yarchive.net/comp/linux/write_error_return.html
  111. return {res, 0};
  112. }
  113. } // namespace wifilogd
  114. } // namespace android