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