fixup! [M120 Migration] Notify media device state to webbrowser
[platform/framework/web/chromium-efl.git] / base / os_compat_android.cc
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.
4
5 #include "base/os_compat_android.h"
6
7 #include <asm/unistd.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <math.h>
11 #include <sys/stat.h>
12 #include <sys/syscall.h>
13 #include <unistd.h>
14 #include "base/strings/string_util.h"
15
16 #if !defined(__LP64__)
17 #include <time64.h>
18 #endif
19
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"
24
25 extern "C" {
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]) {
29   if (tv == nullptr)
30     return base::checked_cast<int>(syscall(__NR_utimensat, fd, NULL, NULL, 0));
31
32   if (tv[0].tv_usec < 0 || tv[0].tv_usec >= 1000000 ||
33       tv[1].tv_usec < 0 || tv[1].tv_usec >= 1000000) {
34     errno = EINVAL;
35     return -1;
36   }
37
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));
45 }
46
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)
56     return -1;
57   return static_cast<time_t>(result);
58 }
59 #endif
60
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).
63 //
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:
66 //
67 //     double c, s;
68 //     c = cos(angle);
69 //     s = sin(angle);
70 //
71 // Will generate machine code that looks like:
72 //
73 //     double c, s;
74 //     sincos(angle, &s, &c);
75 //
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!).
80 //
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.
86 //
87 #if defined(__GNUC__) && !defined(__clang__) && \
88     !defined(ANDROID_SINCOS_PROVIDED)
89
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.
94 //
95 #define GCC_NO_OPTIMIZE  __attribute__((optimize("O0")))
96
97 GCC_NO_OPTIMIZE
98 void sincos(double angle, double* s, double *c) {
99   *c = cos(angle);
100   *s = sin(angle);
101 }
102
103 GCC_NO_OPTIMIZE
104 void sincosf(float angle, float* s, float* c) {
105   *c = cosf(angle);
106   *s = sinf(angle);
107 }
108
109 #endif  // __GNUC__ && !__clang__
110
111 // An implementation of mkdtemp, since it is not exposed by the NDK
112 // for native API level 9 that we target.
113 //
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) {
119   if (!path) {
120     errno = EINVAL;
121     return nullptr;
122   }
123
124   const size_t path_len = strlen(path);
125
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)) {
130     errno = EINVAL;
131     return nullptr;
132   }
133
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, '/');
138   if (dirsep) {
139     *dirsep = '\0';  // Terminating directory path temporarily
140
141     base::stat_wrapper_t st;
142     int ret = base::File::Stat(path, &st);
143
144     *dirsep = '/';  // Restoring directory separator
145     if (ret < 0)  // Directory probably does not exist
146       return nullptr;
147     if (!S_ISDIR(st.st_mode)) {  // Not a directory
148       errno = ENOTDIR;
149       return nullptr;
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 (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;
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 nullptr;
171     }
172   }
173
174   // We reached the max number of tries.
175   return nullptr;
176 }
177
178 }  // extern "C"