os_compat_android.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/os_compat_android.h"
  5. #include <asm/unistd.h>
  6. #include <errno.h>
  7. #include <limits.h>
  8. #include <math.h>
  9. #include <sys/stat.h>
  10. #include <sys/syscall.h>
  11. #include <unistd.h>
  12. #include "base/strings/string_util.h"
  13. #if !defined(__LP64__)
  14. #include <time64.h>
  15. #endif
  16. #include "base/files/file.h"
  17. #include "base/numerics/safe_conversions.h"
  18. #include "base/rand_util.h"
  19. #include "base/strings/string_piece.h"
  20. extern "C" {
  21. // There is no futimes() avaiable in Bionic, so we provide our own
  22. // implementation until it is there.
  23. int futimes(int fd, const struct timeval tv[2]) {
  24. if (tv == nullptr)
  25. return base::checked_cast<int>(syscall(__NR_utimensat, fd, NULL, NULL, 0));
  26. if (tv[0].tv_usec < 0 || tv[0].tv_usec >= 1000000 ||
  27. tv[1].tv_usec < 0 || tv[1].tv_usec >= 1000000) {
  28. errno = EINVAL;
  29. return -1;
  30. }
  31. // Convert timeval to timespec.
  32. struct timespec ts[2];
  33. ts[0].tv_sec = tv[0].tv_sec;
  34. ts[0].tv_nsec = tv[0].tv_usec * 1000;
  35. ts[1].tv_sec = tv[1].tv_sec;
  36. ts[1].tv_nsec = tv[1].tv_usec * 1000;
  37. return base::checked_cast<int>(syscall(__NR_utimensat, fd, NULL, ts, 0));
  38. }
  39. #if !defined(__LP64__)
  40. // 32-bit Android has only timegm64() and not timegm().
  41. // We replicate the behaviour of timegm() when the result overflows time_t.
  42. time_t timegm(struct tm* const t) {
  43. // time_t is signed on Android.
  44. static const time_t kTimeMax = ~(1L << (sizeof(time_t) * CHAR_BIT - 1));
  45. static const time_t kTimeMin = (1L << (sizeof(time_t) * CHAR_BIT - 1));
  46. time64_t result = timegm64(t);
  47. if (result < kTimeMin || result > kTimeMax)
  48. return -1;
  49. return static_cast<time_t>(result);
  50. }
  51. #endif
  52. // The following is only needed when building with GCC 4.6 or higher
  53. // (i.e. not with Android GCC 4.4.3, nor with Clang).
  54. //
  55. // GCC is now capable of optimizing successive calls to sin() and cos() into
  56. // a single call to sincos(). This means that source code that looks like:
  57. //
  58. // double c, s;
  59. // c = cos(angle);
  60. // s = sin(angle);
  61. //
  62. // Will generate machine code that looks like:
  63. //
  64. // double c, s;
  65. // sincos(angle, &s, &c);
  66. //
  67. // Unfortunately, sincos() and friends are not part of the Android libm.so
  68. // library provided by the NDK for API level 9. When the optimization kicks
  69. // in, it makes the final build fail with a puzzling message (puzzling
  70. // because 'sincos' doesn't appear anywhere in the sources!).
  71. //
  72. // To solve this, we provide our own implementation of the sincos() function
  73. // and related friends. Note that we must also explicitely tell GCC to disable
  74. // optimizations when generating these. Otherwise, the generated machine code
  75. // for each function would simply end up calling itself, resulting in a
  76. // runtime crash due to stack overflow.
  77. //
  78. #if defined(__GNUC__) && !defined(__clang__) && \
  79. !defined(ANDROID_SINCOS_PROVIDED)
  80. // For the record, Clang does not support the 'optimize' attribute.
  81. // In the unlikely event that it begins performing this optimization too,
  82. // we'll have to find a different way to achieve this. NOTE: Tested with O1
  83. // which still performs the optimization.
  84. //
  85. #define GCC_NO_OPTIMIZE __attribute__((optimize("O0")))
  86. GCC_NO_OPTIMIZE
  87. void sincos(double angle, double* s, double *c) {
  88. *c = cos(angle);
  89. *s = sin(angle);
  90. }
  91. GCC_NO_OPTIMIZE
  92. void sincosf(float angle, float* s, float* c) {
  93. *c = cosf(angle);
  94. *s = sinf(angle);
  95. }
  96. #endif // __GNUC__ && !__clang__
  97. // An implementation of mkdtemp, since it is not exposed by the NDK
  98. // for native API level 9 that we target.
  99. //
  100. // For any changes in the mkdtemp function, you should manually run the unittest
  101. // OsCompatAndroidTest.DISABLED_TestMkdTemp in your local machine to check if it
  102. // passes. Please don't enable it, since it creates a directory and may be
  103. // source of flakyness.
  104. char* mkdtemp(char* path) {
  105. if (!path) {
  106. errno = EINVAL;
  107. return nullptr;
  108. }
  109. const size_t path_len = strlen(path);
  110. // The last six characters of 'path' must be XXXXXX.
  111. const base::StringPiece kSuffix("XXXXXX");
  112. const size_t kSuffixLen = kSuffix.length();
  113. if (!base::EndsWith(base::StringPiece(path, path_len), kSuffix)) {
  114. errno = EINVAL;
  115. return nullptr;
  116. }
  117. // If the path contains a directory, as in /tmp/foo/XXXXXXXX, make sure
  118. // that /tmp/foo exists, otherwise we're going to loop a really long
  119. // time for nothing below
  120. char* dirsep = strrchr(path, '/');
  121. if (dirsep) {
  122. *dirsep = '\0'; // Terminating directory path temporarily
  123. base::stat_wrapper_t st;
  124. int ret = base::File::Stat(path, &st);
  125. *dirsep = '/'; // Restoring directory separator
  126. if (ret < 0) // Directory probably does not exist
  127. return nullptr;
  128. if (!S_ISDIR(st.st_mode)) { // Not a directory
  129. errno = ENOTDIR;
  130. return nullptr;
  131. }
  132. }
  133. // Max number of tries using different random suffixes.
  134. const int kMaxTries = 100;
  135. // Now loop until we CAN create a directory by that name or we reach the max
  136. // number of tries.
  137. for (int i = 0; i < kMaxTries; ++i) {
  138. // Fill the suffix XXXXXX with a random string composed of a-z chars.
  139. for (size_t pos = 0; pos < kSuffixLen; ++pos) {
  140. char rand_char = static_cast<char>(base::RandInt('a', 'z'));
  141. path[path_len - kSuffixLen + pos] = rand_char;
  142. }
  143. if (mkdir(path, 0700) == 0) {
  144. // We just created the directory succesfully.
  145. return path;
  146. }
  147. if (errno != EEXIST) {
  148. // The directory doesn't exist, but an error occured
  149. return nullptr;
  150. }
  151. }
  152. // We reached the max number of tries.
  153. return nullptr;
  154. }
  155. } // extern "C"