OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <sys/types.h> // Include something that will define __GLIBC__. | 5 #include <sys/types.h> // Include something that will define __GLIBC__. |
6 | 6 |
7 // The entire file is wrapped in this #if. We do this so this .cc file can be | 7 // The entire file is wrapped in this #if. We do this so this .cc file can be |
8 // compiled, even on a non-newlib build. | 8 // compiled, even on a non-newlib build. |
9 #if defined(__native_client__) && !defined(__GLIBC__) && !defined(__BIONIC__) | 9 #if defined(__native_client__) && !defined(__GLIBC__) && !defined(__BIONIC__) |
10 | 10 |
11 #include "nacl_io/kernel_wrap.h" | 11 #include "nacl_io/kernel_wrap.h" |
12 | 12 |
13 #include <dirent.h> | 13 #include <dirent.h> |
14 #include <errno.h> | 14 #include <errno.h> |
15 #include <irt.h> | 15 #include <irt.h> |
16 #include <irt_dev.h> | 16 #include <irt_dev.h> |
17 #include <sys/mman.h> | 17 #include <sys/mman.h> |
18 #include <sys/stat.h> | 18 #include <sys/stat.h> |
19 #include <sys/time.h> | 19 #include <sys/time.h> |
20 | 20 |
21 #include "nacl_io/kernel_intercept.h" | 21 #include "nacl_io/kernel_intercept.h" |
22 #include "nacl_io/kernel_wrap_real.h" | 22 #include "nacl_io/kernel_wrap_real.h" |
23 #include "nacl_io/log.h" | |
23 | 24 |
24 EXTERN_C_BEGIN | 25 EXTERN_C_BEGIN |
25 | 26 |
26 // Macro to get the REAL function pointer | 27 // Macro to get the REAL function pointer |
27 #define REAL(name) __nacl_irt_##name##_real | 28 #define REAL(name) __nacl_irt_##name##_real |
28 | 29 |
29 // Macro to get the WRAP function | 30 // Macro to get the WRAP function |
30 #define WRAP(name) __nacl_irt_##name##_wrap | 31 #define WRAP(name) __nacl_irt_##name##_wrap |
31 | 32 |
32 // Declare REAL function pointer. | 33 // Declare REAL function pointer. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 OP(dev_filename, link); \ | 83 OP(dev_filename, link); \ |
83 OP(dev_filename, rename); \ | 84 OP(dev_filename, rename); \ |
84 OP(dev_filename, symlink); \ | 85 OP(dev_filename, symlink); \ |
85 OP(dev_filename, chmod); \ | 86 OP(dev_filename, chmod); \ |
86 OP(dev_filename, access); \ | 87 OP(dev_filename, access); \ |
87 OP(dev_filename, readlink); \ | 88 OP(dev_filename, readlink); \ |
88 OP(dev_filename, utimes); \ | 89 OP(dev_filename, utimes); \ |
89 OP(memory, mmap); \ | 90 OP(memory, mmap); \ |
90 OP(memory, munmap); | 91 OP(memory, munmap); |
91 | 92 |
93 // Most kernal intercept functions (ki_*) return -1 and set errno accordingly. | |
94 // However the IRT wrappers are expected to return errno on failure. Here we | |
95 // check that the ki_ function actually set errno and fall back to EIO if it | |
96 // didn't. | |
97 #define RTN_ERRNO_IF(cond) \ | |
98 if (cond) { \ | |
99 int rtn = errno; \ | |
100 if (rtn == 0) rtn = EIO; \ | |
101 return rtn; \ | |
102 } | |
103 | |
104 #define ERRNO_RTN(function_call) \ | |
binji
2014/05/06 23:25:54
make this consistent with the other kernel_wrap_*
Sam Clegg
2014/05/07 00:29:03
Done.
| |
105 RTN_ERRNO_IF(function_call < 0); \ | |
106 return 0; | |
92 | 107 |
93 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR); | 108 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR); |
94 | 109 |
95 int WRAP(close)(int fd) { | 110 int WRAP(close)(int fd) { |
96 return (ki_close(fd) < 0) ? errno : 0; | 111 ERRNO_RTN(ki_close(fd)); |
binji
2014/05/06 23:25:54
do this stuff for kernel_wrap_bionic too
Sam Clegg
2014/05/07 00:29:03
Done.
| |
97 } | 112 } |
98 | 113 |
99 int WRAP(dup)(int fd, int* newfd) { | 114 int WRAP(dup)(int fd, int* newfd) { |
100 *newfd = ki_dup(fd); | 115 *newfd = ki_dup(fd); |
101 return (*newfd < 0) ? errno : 0; | 116 RTN_ERRNO_IF(*newfd < 0); |
binji
2014/05/06 23:29:52
ERRNO_RTN
Sam Clegg
2014/05/07 00:29:03
Done.
| |
117 return 0; | |
102 } | 118 } |
103 | 119 |
104 int WRAP(dup2)(int fd, int newfd) { | 120 int WRAP(dup2)(int fd, int newfd) { |
105 newfd = ki_dup2(fd, newfd); | 121 newfd = ki_dup2(fd, newfd); |
106 return (newfd < 0) ? errno : 0; | 122 RTN_ERRNO_IF(newfd < 0); |
binji
2014/05/06 23:29:52
ERRNO_RTN
Sam Clegg
2014/05/07 00:29:03
Done.
| |
123 return 0; | |
107 } | 124 } |
108 | 125 |
109 void WRAP(exit)(int status) { | 126 void WRAP(exit)(int status) { |
110 ki_exit(status); | 127 ki_exit(status); |
111 } | 128 } |
112 | 129 |
113 int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) { | 130 int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) { |
114 ssize_t signed_nread = ki_read(fd, buf, count); | 131 ssize_t signed_nread = ki_read(fd, buf, count); |
115 *nread = static_cast<size_t>(signed_nread); | 132 *nread = static_cast<size_t>(signed_nread); |
116 return (signed_nread < 0) ? errno : 0; | 133 return (signed_nread < 0) ? errno : 0; |
binji
2014/05/06 23:31:49
missed some:
read, write, seek, getdents ...
Sam Clegg
2014/05/07 00:29:03
Done.
| |
117 } | 134 } |
118 | 135 |
119 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) { | 136 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) { |
120 ssize_t signed_nwrote = ki_write(fd, buf, count); | 137 ssize_t signed_nwrote = ki_write(fd, buf, count); |
121 *nwrote = static_cast<size_t>(signed_nwrote); | 138 *nwrote = static_cast<size_t>(signed_nwrote); |
122 return (signed_nwrote < 0) ? errno : 0; | 139 return (signed_nwrote < 0) ? errno : 0; |
123 } | 140 } |
124 | 141 |
125 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) { | 142 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) { |
126 *new_offset = ki_lseek(fd, offset, whence); | 143 *new_offset = ki_lseek(fd, offset, whence); |
127 return (*new_offset < 0) ? errno : 0; | 144 return (*new_offset < 0) ? errno : 0; |
128 } | 145 } |
129 | 146 |
130 int WRAP(fstat)(int fd, struct stat* buf) { | 147 int WRAP(fstat)(int fd, struct stat* buf) { |
131 return (ki_fstat(fd, buf) < 0) ? errno : 0; | 148 ERRNO_RTN(ki_fstat(fd, buf)); |
132 } | 149 } |
133 | 150 |
134 int WRAP(getdents)(int fd, dirent* buf, size_t count, size_t* nread) { | 151 int WRAP(getdents)(int fd, dirent* buf, size_t count, size_t* nread) { |
135 int rtn = ki_getdents(fd, buf, count); | 152 int rtn = ki_getdents(fd, buf, count); |
136 if (rtn < 0) | 153 if (rtn < 0) |
137 return errno; | 154 return errno; |
138 *nread = rtn; | 155 *nread = rtn; |
139 return 0; | 156 return 0; |
140 } | 157 } |
141 | 158 |
142 int WRAP(fchdir)(int fd) { | 159 int WRAP(fchdir)(int fd) { |
143 return (ki_fchdir(fd) < 0) ? errno : 0; | 160 ERRNO_RTN(ki_fchdir(fd)); |
144 } | 161 } |
145 | 162 |
146 int WRAP(fchmod)(int fd, mode_t mode) { | 163 int WRAP(fchmod)(int fd, mode_t mode) { |
147 return (ki_fchmod(fd, mode) < 0) ? errno : 0; | 164 ERRNO_RTN(ki_fchmod(fd, mode)); |
148 } | 165 } |
149 | 166 |
150 int WRAP(fsync)(int fd) { | 167 int WRAP(fsync)(int fd) { |
151 return (ki_fsync(fd) < 0) ? errno : 0; | 168 ERRNO_RTN(ki_fsync(fd)); |
152 } | 169 } |
153 | 170 |
154 int WRAP(fdatasync)(int fd) { | 171 int WRAP(fdatasync)(int fd) { |
155 return (ki_fdatasync(fd) < 0) ? errno : 0; | 172 ERRNO_RTN(ki_fdatasync(fd)); |
156 } | 173 } |
157 | 174 |
158 int WRAP(ftruncate)(int fd, off_t length) { | 175 int WRAP(ftruncate)(int fd, off_t length) { |
159 return (ki_ftruncate(fd, length) < 0) ? errno : 0; | 176 ERRNO_RTN(ki_ftruncate(fd, length)); |
160 } | 177 } |
161 | 178 |
162 int WRAP(isatty)(int fd, int* result) { | 179 int WRAP(isatty)(int fd, int* result) { |
163 *result = ki_isatty(fd); | 180 *result = ki_isatty(fd); |
164 if (*result == 0) | 181 if (*result == 0) |
165 return errno; | 182 return errno; |
166 return 0; | 183 return 0; |
167 } | 184 } |
168 | 185 |
169 int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd, | 186 int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd, |
(...skipping 11 matching lines...) Expand all Loading... | |
181 ki_munmap(addr, length); | 198 ki_munmap(addr, length); |
182 return REAL(munmap)(addr, length); | 199 return REAL(munmap)(addr, length); |
183 } | 200 } |
184 | 201 |
185 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) { | 202 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) { |
186 *newfd = ki_open(pathname, oflag); | 203 *newfd = ki_open(pathname, oflag); |
187 return (*newfd < 0) ? errno : 0; | 204 return (*newfd < 0) ? errno : 0; |
188 } | 205 } |
189 | 206 |
190 int WRAP(stat)(const char* pathname, struct stat* buf) { | 207 int WRAP(stat)(const char* pathname, struct stat* buf) { |
191 return (ki_stat(pathname, buf) < 0) ? errno : 0; | 208 ERRNO_RTN(ki_stat(pathname, buf)); |
192 } | 209 } |
193 | 210 |
194 int WRAP(mkdir)(const char* pathname, mode_t mode) { | 211 int WRAP(mkdir)(const char* pathname, mode_t mode) { |
195 return (ki_mkdir(pathname, mode) < 0) ? errno : 0; | 212 ERRNO_RTN(ki_mkdir(pathname, mode)); |
196 } | 213 } |
197 | 214 |
198 int WRAP(rmdir)(const char* pathname) { | 215 int WRAP(rmdir)(const char* pathname) { |
199 return (ki_rmdir(pathname) < 0) ? errno : 0; | 216 ERRNO_RTN(ki_rmdir(pathname)); |
200 } | 217 } |
201 | 218 |
202 int WRAP(chdir)(const char* pathname) { | 219 int WRAP(chdir)(const char* pathname) { |
203 return (ki_chdir(pathname) < 0) ? errno : 0; | 220 ERRNO_RTN(ki_chdir(pathname)); |
204 } | 221 } |
205 | 222 |
206 int WRAP(getcwd)(char* pathname, size_t len) { | 223 int WRAP(getcwd)(char* pathname, size_t len) { |
207 char* rtn = ki_getcwd(pathname, len); | 224 char* rtn = ki_getcwd(pathname, len); |
208 if (NULL == rtn) | 225 if (NULL == rtn) |
209 return errno; | 226 return errno; |
210 return 0; | 227 return 0; |
211 } | 228 } |
212 | 229 |
213 int WRAP(unlink)(const char* pathname) { | 230 int WRAP(unlink)(const char* pathname) { |
214 return (ki_unlink(pathname) < 0) ? errno : 0; | 231 ERRNO_RTN(ki_unlink(pathname)); |
215 } | 232 } |
216 | 233 |
217 int WRAP(truncate)(const char* pathname, off_t length) { | 234 int WRAP(truncate)(const char* pathname, off_t length) { |
218 return (ki_truncate(pathname, length) < 0) ? errno : 0; | 235 ERRNO_RTN(ki_truncate(pathname, length)); |
219 } | 236 } |
220 | 237 |
221 int WRAP(lstat)(const char* pathname, struct stat* buf) { | 238 int WRAP(lstat)(const char* pathname, struct stat* buf) { |
222 return (ki_lstat(pathname, buf) < 0) ? errno : 0; | 239 ERRNO_RTN(ki_lstat(pathname, buf)); |
223 } | 240 } |
224 | 241 |
225 int WRAP(link)(const char* pathname, const char* newpath) { | 242 int WRAP(link)(const char* pathname, const char* newpath) { |
226 return (ki_link(pathname, newpath) < 0) ? errno : 0; | 243 ERRNO_RTN(ki_link(pathname, newpath)); |
227 } | 244 } |
228 | 245 |
229 int WRAP(rename)(const char* pathname, const char* newpath) { | 246 int WRAP(rename)(const char* pathname, const char* newpath) { |
230 return (ki_rename(pathname, newpath) < 0) ? errno : 0; | 247 ERRNO_RTN(ki_rename(pathname, newpath)); |
231 } | 248 } |
232 | 249 |
233 int WRAP(symlink)(const char* pathname, const char* newpath) { | 250 int WRAP(symlink)(const char* pathname, const char* newpath) { |
234 return (ki_symlink(pathname, newpath) < 0) ? errno : 0; | 251 ERRNO_RTN(ki_symlink(pathname, newpath)); |
235 } | 252 } |
236 | 253 |
237 int WRAP(chmod)(const char* pathname, mode_t mode) { | 254 int WRAP(chmod)(const char* pathname, mode_t mode) { |
238 return (ki_chmod(pathname, mode) < 0) ? errno : 0; | 255 ERRNO_RTN(ki_chmod(pathname, mode)); |
239 } | 256 } |
240 | 257 |
241 int WRAP(access)(const char* pathname, int amode) { | 258 int WRAP(access)(const char* pathname, int amode) { |
242 return (ki_access(pathname, amode) < 0) ? errno : 0; | 259 ERRNO_RTN(ki_access(pathname, amode)); |
243 } | 260 } |
244 | 261 |
245 int WRAP(readlink)(const char* pathname, char *buf, | 262 int WRAP(readlink)(const char* pathname, char *buf, |
246 size_t count, size_t *nread) { | 263 size_t count, size_t *nread) { |
247 int rtn = ki_readlink(pathname, buf, count); | 264 int rtn = ki_readlink(pathname, buf, count); |
248 if (rtn < 0) | 265 RTN_ERRNO_IF(rtn < 0); |
249 return errno; | |
250 *nread = rtn; | 266 *nread = rtn; |
251 return 0; | 267 return 0; |
252 } | 268 } |
253 | 269 |
254 int WRAP(utimes)(const char* pathname, const struct timeval times[2]) { | 270 int WRAP(utimes)(const char* pathname, const struct timeval times[2]) { |
255 return (ki_utimes(pathname, times) < 0) ? errno : 0; | 271 ERRNO_RTN(ki_utimes(pathname, times)); |
256 } | 272 } |
257 | 273 |
258 static void assign_real_pointers() { | 274 static void assign_real_pointers() { |
259 static bool assigned = false; | 275 static bool assigned = false; |
260 if (!assigned) { | 276 if (!assigned) { |
261 __libnacl_irt_dev_filename_init(); | 277 __libnacl_irt_dev_filename_init(); |
262 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR) | 278 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR) |
263 assigned = true; | 279 assigned = true; |
264 } | 280 } |
265 } | 281 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 | 351 |
336 int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) { | 352 int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) { |
337 CHECK_REAL(write); | 353 CHECK_REAL(write); |
338 return REAL(write)(fd, buf, count, nwrote); | 354 return REAL(write)(fd, buf, count, nwrote); |
339 } | 355 } |
340 | 356 |
341 static bool s_wrapped = false; | 357 static bool s_wrapped = false; |
342 | 358 |
343 void kernel_wrap_init() { | 359 void kernel_wrap_init() { |
344 if (!s_wrapped) { | 360 if (!s_wrapped) { |
361 LOG_TRACE("kernel_wrap_init"); | |
345 assign_real_pointers(); | 362 assign_real_pointers(); |
346 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP) | 363 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP) |
347 s_wrapped = true; | 364 s_wrapped = true; |
348 } | 365 } |
349 } | 366 } |
350 | 367 |
351 void kernel_wrap_uninit() { | 368 void kernel_wrap_uninit() { |
352 if (s_wrapped) { | 369 if (s_wrapped) { |
370 LOG_TRACE("kernel_wrap_uninit"); | |
353 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL) | 371 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL) |
354 s_wrapped = false; | 372 s_wrapped = false; |
355 } | 373 } |
356 } | 374 } |
357 | 375 |
358 EXTERN_C_END | 376 EXTERN_C_END |
359 | 377 |
360 #endif // defined(__native_client__) && !defined(__GLIBC__) ... | 378 #endif // defined(__native_client__) && !defined(__GLIBC__) ... |
OLD | NEW |