blob: 2f5b4db44c02f16f179802af0c37906ff24719d4 [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>
nileshagrawal@chromium.orgf9d216a2011-12-16 12:18:13 +090012#include <time64.h>
13
felipeg@chromium.org406c7ba2012-07-17 01:12:16 +090014#include "base/rand_util.h"
jingzhao@chromium.org1b42c242011-09-29 13:43:54 +090015#include "base/stringprintf.h"
tfarina@chromium.orgb6d49112013-03-30 23:29:00 +090016#include "base/strings/string_piece.h"
michaelbai@google.com2251c622011-06-22 07:34:50 +090017
nileshagrawal@chromium.orge4153252013-06-07 07:42:47 +090018extern "C" {
michaelbai@google.com2251c622011-06-22 07:34:50 +090019// There is no futimes() avaiable in Bionic, so we provide our own
20// implementation until it is there.
michaelbai@google.com2251c622011-06-22 07:34:50 +090021int futimes(int fd, const struct timeval tv[2]) {
nileshagrawal@chromium.orge4153252013-06-07 07:42:47 +090022 if (tv == NULL)
23 return syscall(__NR_utimensat, fd, NULL, NULL, 0);
24
25 if (tv[0].tv_usec < 0 || tv[0].tv_usec >= 1000000 ||
26 tv[1].tv_usec < 0 || tv[1].tv_usec >= 1000000) {
27 errno = EINVAL;
28 return -1;
29 }
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 syscall(__NR_utimensat, fd, NULL, ts, 0);
michaelbai@google.com2251c622011-06-22 07:34:50 +090038}
39
nileshagrawal@chromium.orgf9d216a2011-12-16 12:18:13 +090040// Android has only timegm64() and no timegm().
41// We replicate the behaviour of timegm() when the result overflows time_t.
42time_t timegm(struct tm* const t) {
43 // time_t is signed on Android.
44 static const time_t kTimeMax = ~(1 << (sizeof(time_t) * CHAR_BIT - 1));
45 static const time_t kTimeMin = (1 << (sizeof(time_t) * CHAR_BIT - 1));
46 time64_t result = timegm64(t);
47 if (result < kTimeMin || result > kTimeMax)
48 return -1;
49 return result;
50}
51
felipeg@chromium.org406c7ba2012-07-17 01:12:16 +090052// 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
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
87GCC_NO_OPTIMIZE
88void sincos(double angle, double* s, double *c) {
89 *c = cos(angle);
90 *s = sin(angle);
91}
92
93GCC_NO_OPTIMIZE
94void sincosf(float angle, float* s, float* c) {
95 *c = cosf(angle);
96 *s = sinf(angle);
97}
98
99#endif // __GNUC__ && !__clang__
100
101// An implementation of mkdtemp, since it is not exposed by the NDK
102// for native API level 9 that we target.
103//
104// For any changes in the mkdtemp function, you should manually run the unittest
105// OsCompatAndroidTest.DISABLED_TestMkdTemp in your local machine to check if it
106// passes. Please don't enable it, since it creates a directory and may be
107// source of flakyness.
108char* mkdtemp(char* path) {
109 if (path == NULL) {
110 errno = EINVAL;
111 return NULL;
112 }
113
114 const int path_len = strlen(path);
115
116 // The last six characters of 'path' must be XXXXXX.
117 const base::StringPiece kSuffix("XXXXXX");
118 const int kSuffixLen = kSuffix.length();
119 if (!base::StringPiece(path, path_len).ends_with(kSuffix)) {
120 errno = EINVAL;
121 return NULL;
122 }
123
124 // If the path contains a directory, as in /tmp/foo/XXXXXXXX, make sure
125 // that /tmp/foo exists, otherwise we're going to loop a really long
126 // time for nothing below
127 char* dirsep = strrchr(path, '/');
128 if (dirsep != NULL) {
129 struct stat st;
130 int ret;
131
132 *dirsep = '\0'; // Terminating directory path temporarily
133
134 ret = stat(path, &st);
135
136 *dirsep = '/'; // Restoring directory separator
137 if (ret < 0) // Directory probably does not exist
138 return NULL;
139 if (!S_ISDIR(st.st_mode)) { // Not a directory
140 errno = ENOTDIR;
141 return NULL;
142 }
143 }
144
145 // Max number of tries using different random suffixes.
146 const int kMaxTries = 100;
147
148 // Now loop until we CAN create a directory by that name or we reach the max
149 // number of tries.
150 for (int i = 0; i < kMaxTries; ++i) {
151 // Fill the suffix XXXXXX with a random string composed of a-z chars.
152 for (int pos = 0; pos < kSuffixLen; ++pos) {
153 char rand_char = static_cast<char>(base::RandInt('a', 'z'));
154 path[path_len - kSuffixLen + pos] = rand_char;
155 }
156 if (mkdir(path, 0700) == 0) {
157 // We just created the directory succesfully.
158 return path;
159 }
160 if (errno != EEXIST) {
161 // The directory doesn't exist, but an error occured
162 return NULL;
163 }
164 }
165
166 // We reached the max number of tries.
167 return NULL;
168}
169
michaelbai@google.com2251c622011-06-22 07:34:50 +0900170} // extern "C"