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