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