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