Chromium Code Reviews| 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 __BIONIC__. | 5 #include <sys/types.h> // Include something that will define __BIONIC__. |
| 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-bionic build. | 8 // compiled, even on a non-bionic build. |
| 9 | 9 |
| 10 #if defined(__native_client__) && defined(__BIONIC__) | 10 #if defined(__native_client__) && defined(__BIONIC__) |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 110 REAL(name) = __nacl_irt_##name; | 110 REAL(name) = __nacl_irt_##name; |
| 111 | 111 |
| 112 // Switch IRT's pointer to the REAL pointer | 112 // Switch IRT's pointer to the REAL pointer |
| 113 #define USE_REAL(name) \ | 113 #define USE_REAL(name) \ |
| 114 __nacl_irt_##name = (typeof(__nacl_irt_##name)) REAL(name) | 114 __nacl_irt_##name = (typeof(__nacl_irt_##name)) REAL(name) |
| 115 | 115 |
| 116 // Switch IRT's pointer to the WRAP function | 116 // Switch IRT's pointer to the WRAP function |
| 117 #define USE_WRAP(name) \ | 117 #define USE_WRAP(name) \ |
| 118 __nacl_irt_##name = (typeof(__nacl_irt_##name)) WRAP(name) | 118 __nacl_irt_##name = (typeof(__nacl_irt_##name)) WRAP(name) |
| 119 | 119 |
| 120 | |
| 121 #define EXPAND_SYMBOL_LIST_OPERATION(OP) \ | 120 #define EXPAND_SYMBOL_LIST_OPERATION(OP) \ |
| 122 OP(chdir); \ | 121 OP(chdir); \ |
| 123 OP(close); \ | 122 OP(close); \ |
| 124 OP(dup); \ | 123 OP(dup); \ |
| 125 OP(dup2); \ | 124 OP(dup2); \ |
| 126 OP(exit); \ | 125 OP(exit); \ |
| 127 OP(fchdir); \ | 126 OP(fchdir); \ |
| 128 OP(fchmod); \ | 127 OP(fchmod); \ |
| 129 OP(fdatasync); \ | 128 OP(fdatasync); \ |
| 130 OP(fstat); \ | 129 OP(fstat); \ |
| 131 OP(fsync); \ | 130 OP(fsync); \ |
| 132 OP(getcwd); \ | 131 OP(getcwd); \ |
| 133 OP(getdents); \ | 132 OP(getdents); \ |
| 134 OP(isatty); \ | 133 OP(isatty); \ |
| 135 OP(lstat); \ | 134 OP(lstat); \ |
| 136 OP(mkdir); \ | 135 OP(mkdir); \ |
| 137 OP(mmap); \ | 136 OP(mmap); \ |
| 138 OP(munmap); \ | 137 OP(munmap); \ |
| 139 OP(open); \ | 138 OP(open); \ |
| 140 OP(open_resource); \ | 139 OP(open_resource); \ |
| 141 OP(poll); \ | 140 OP(poll); \ |
| 142 OP(read); \ | 141 OP(read); \ |
| 143 OP(readlink); \ | 142 OP(readlink); \ |
| 144 OP(rmdir); \ | 143 OP(rmdir); \ |
| 145 OP(seek); \ | 144 OP(seek); \ |
| 146 OP(stat); \ | 145 OP(stat); \ |
| 147 OP(truncate); \ | 146 OP(truncate); \ |
| 148 OP(write); \ | 147 OP(write); \ |
| 149 | 148 |
| 150 | |
| 151 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR); | 149 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR); |
| 152 | 150 |
| 153 int WRAP(chdir)(const char* pathname) { | 151 int WRAP(chdir)(const char* pathname) { |
| 154 return (ki_chdir(pathname)) ? errno : 0; | 152 ERRNO_RTN(ki_chdir(pathname)); |
| 155 } | 153 } |
| 156 | 154 |
| 157 int WRAP(close)(int fd) { | 155 int WRAP(close)(int fd) { |
| 158 return (ki_close(fd) < 0) ? errno : 0; | 156 ERRNO_RTN(ki_close(fd)); |
| 159 } | 157 } |
| 160 | 158 |
| 161 int WRAP(dup)(int fd, int* newfd) NOTHROW { | 159 int WRAP(dup)(int fd, int* newfd) NOTHROW { |
| 162 *newfd = ki_dup(fd); | 160 *newfd = ki_dup(fd); |
| 163 return (*newfd < 0) ? errno : 0; | 161 ERRNO_RTN(*newfd); |
| 164 } | 162 } |
| 165 | 163 |
| 166 int WRAP(dup2)(int fd, int newfd) NOTHROW { | 164 int WRAP(dup2)(int fd, int newfd) NOTHROW { |
| 167 return (ki_dup2(fd, newfd) < 0) ? errno : 0; | 165 ERRNO_RTN(ki_dup2(fd, newfd)); |
| 168 } | 166 } |
| 169 | 167 |
| 170 void WRAP(exit)(int status) { | 168 void WRAP(exit)(int status) { |
| 171 ki_exit(status); | 169 ki_exit(status); |
| 172 } | 170 } |
| 173 | 171 |
| 174 int WRAP(fchdir)(int fd) NOTHROW { | 172 int WRAP(fchdir)(int fd) NOTHROW { |
| 175 return (ki_fchdir(fd)) ? errno : 0; | 173 ERRNO_RTN(ki_fchdir(fd)); |
|
binji
2014/05/07 14:58:24
hm, these should have always failed with bionic.
sbc
2014/05/07 15:58:18
Yup..
| |
| 176 } | 174 } |
| 177 | 175 |
| 178 int WRAP(fchmod)(int fd, mode_t mode) NOTHROW { | 176 int WRAP(fchmod)(int fd, mode_t mode) NOTHROW { |
| 179 return (ki_fchmod(fd, mode)) ? errno : 0; | 177 ERRNO_RTN(ki_fchmod(fd, mode)); |
| 180 } | 178 } |
| 181 | 179 |
| 182 int WRAP(fdatasync)(int fd) NOTHROW { | 180 int WRAP(fdatasync)(int fd) NOTHROW { |
| 183 return (ki_fdatasync(fd)) ? errno : 0; | 181 ERRNO_RTN(ki_fdatasync(fd)); |
| 184 } | 182 } |
| 185 | 183 |
| 186 int WRAP(fstat)(int fd, struct nacl_abi_stat *nacl_buf) { | 184 int WRAP(fstat)(int fd, struct nacl_abi_stat *nacl_buf) { |
| 187 struct stat buf; | 185 struct stat buf; |
| 188 memset(&buf, 0, sizeof(struct stat)); | 186 memset(&buf, 0, sizeof(struct stat)); |
| 189 int res = ki_fstat(fd, &buf); | 187 int res = ki_fstat(fd, &buf); |
| 190 if (res < 0) | 188 RTN_ERRNO_IF(res < 0); |
| 191 return errno; | |
| 192 stat_to_nacl_stat(&buf, nacl_buf); | 189 stat_to_nacl_stat(&buf, nacl_buf); |
| 193 return 0; | 190 return 0; |
| 194 } | 191 } |
| 195 | 192 |
| 196 int WRAP(fsync)(int fd) NOTHROW { | 193 int WRAP(fsync)(int fd) NOTHROW { |
| 197 return (ki_fsync(fd)) ? errno : 0; | 194 ERRNO_RTN(ki_fsync(fd)); |
| 198 } | 195 } |
| 199 | 196 |
| 200 int WRAP(getcwd)(char* buf, size_t size) { | 197 int WRAP(getcwd)(char* buf, size_t size) { |
| 201 if (ki_getcwd(buf, size) == NULL) | 198 RTN_ERRNO_IF(ki_getcwd(buf, size) == NULL); |
| 202 return errno; | |
| 203 return 0; | 199 return 0; |
| 204 } | 200 } |
| 205 | 201 |
| 206 int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, size_t *nread) { | 202 int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, size_t *nread) { |
| 207 int nacl_offset = 0; | 203 int nacl_offset = 0; |
| 208 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s). | 204 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s). |
| 209 // nacl_abi_dirent(s) are smaller than dirent(s), so nacl_count bytes buffer | 205 // nacl_abi_dirent(s) are smaller than dirent(s), so nacl_count bytes buffer |
| 210 // is enough | 206 // is enough |
| 211 char* buf = (char*)alloca(nacl_count); | 207 char* buf = (char*)alloca(nacl_count); |
| 212 int offset = 0; | 208 int offset = 0; |
| 213 int count; | 209 int count; |
| 214 | 210 |
| 215 count = ki_getdents(fd, buf, nacl_count); | 211 count = ki_getdents(fd, buf, nacl_count); |
| 216 if (count < 0) | 212 RTN_ERRNO_IF(count < 0); |
| 217 return errno; | |
| 218 | 213 |
| 219 while (offset < count) { | 214 while (offset < count) { |
| 220 dirent* d = (dirent*)(buf + offset); | 215 dirent* d = (dirent*)(buf + offset); |
| 221 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)((char*)nacl_buf + nacl_offset); | 216 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)((char*)nacl_buf + nacl_offset); |
| 222 nacl_d->nacl_abi_d_ino = d->d_ino; | 217 nacl_d->nacl_abi_d_ino = d->d_ino; |
| 223 nacl_d->nacl_abi_d_off = d->d_off; | 218 nacl_d->nacl_abi_d_off = d->d_off; |
| 224 nacl_d->nacl_abi_d_reclen = d->d_reclen - d_name_shift; | 219 nacl_d->nacl_abi_d_reclen = d->d_reclen - d_name_shift; |
| 225 size_t d_name_len = d->d_reclen - offsetof(dirent, d_name); | 220 size_t d_name_len = d->d_reclen - offsetof(dirent, d_name); |
| 226 memcpy(nacl_d->nacl_abi_d_name, d->d_name, d_name_len); | 221 memcpy(nacl_d->nacl_abi_d_name, d->d_name, d_name_len); |
| 227 | 222 |
| 228 offset += d->d_reclen; | 223 offset += d->d_reclen; |
| 229 nacl_offset += nacl_d->nacl_abi_d_reclen; | 224 nacl_offset += nacl_d->nacl_abi_d_reclen; |
| 230 } | 225 } |
| 231 | 226 |
| 232 *nread = nacl_offset; | 227 *nread = nacl_offset; |
| 233 return 0; | 228 return 0; |
| 234 } | 229 } |
| 235 | 230 |
| 236 int WRAP(isatty)(int fd, int* result) { | 231 int WRAP(isatty)(int fd, int* result) { |
| 237 *result = ki_isatty(fd); | 232 *result = ki_isatty(fd); |
| 238 if (*result == 1) | 233 RTN_ERRNO_IF(*result == 0); |
|
binji
2014/05/07 14:58:24
hm, this was broken before too.
sbc
2014/05/07 15:58:18
Yup.. I fixed the bug in the newlib version but di
| |
| 239 return errno; | |
| 240 return 0; | 234 return 0; |
| 241 } | 235 } |
| 242 | 236 |
| 243 int WRAP(lstat)(const char *path, struct nacl_abi_stat* nacl_buf) { | 237 int WRAP(lstat)(const char *path, struct nacl_abi_stat* nacl_buf) { |
| 244 struct stat buf; | 238 struct stat buf; |
| 245 memset(&buf, 0, sizeof(struct stat)); | 239 memset(&buf, 0, sizeof(struct stat)); |
| 246 int res = ki_lstat(path, &buf); | 240 int res = ki_lstat(path, &buf); |
| 247 if (res < 0) | 241 RTN_ERRNO_IF(res < 0); |
| 248 return errno; | |
| 249 stat_to_nacl_stat(&buf, nacl_buf); | 242 stat_to_nacl_stat(&buf, nacl_buf); |
| 250 return 0; | 243 return 0; |
| 251 } | 244 } |
| 252 | 245 |
| 253 int WRAP(mkdir)(const char* pathname, mode_t mode) { | 246 int WRAP(mkdir)(const char* pathname, mode_t mode) { |
| 254 return (ki_mkdir(pathname, mode)) ? errno : 0; | 247 ERRNO_RTN(ki_mkdir(pathname, mode)); |
| 255 } | 248 } |
| 256 | 249 |
| 257 int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd, | 250 int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd, |
| 258 int64_t offset) { | 251 int64_t offset) { |
| 259 if (flags & MAP_ANONYMOUS) | 252 if (flags & MAP_ANONYMOUS) |
| 260 return REAL(mmap)(addr, length, prot, flags, fd, offset); | 253 return REAL(mmap)(addr, length, prot, flags, fd, offset); |
| 261 | 254 |
| 262 *addr = ki_mmap(*addr, length, prot, flags, fd, offset); | 255 *addr = ki_mmap(*addr, length, prot, flags, fd, offset); |
| 263 return *addr == (void*)-1 ? errno : 0; | 256 RTN_ERRNO_IF(*addr == (void*)-1) |
| 257 return 0; | |
| 264 } | 258 } |
| 265 | 259 |
| 266 int WRAP(munmap)(void* addr, size_t length) { | 260 int WRAP(munmap)(void* addr, size_t length) { |
| 267 // Always let the real munmap run on the address range. It is not an error if | 261 // Always let the real munmap run on the address range. It is not an error if |
| 268 // there are no mapped pages in that range. | 262 // there are no mapped pages in that range. |
| 269 ki_munmap(addr, length); | 263 ki_munmap(addr, length); |
| 270 return REAL(munmap)(addr, length); | 264 return REAL(munmap)(addr, length); |
| 271 } | 265 } |
| 272 | 266 |
| 273 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) { | 267 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) { |
| 274 *newfd = ki_open(pathname, oflag); | 268 *newfd = ki_open(pathname, oflag); |
| 275 return (*newfd < 0) ? errno : 0; | 269 ERRNO_RTN(*newfd); |
| 276 } | 270 } |
| 277 | 271 |
| 278 int WRAP(open_resource)(const char* file, int* fd) { | 272 int WRAP(open_resource)(const char* file, int* fd) { |
| 279 *fd = ki_open_resource(file); | 273 *fd = ki_open_resource(file); |
| 280 return (*fd < 0) ? errno : 0; | 274 ERRNO_RTN(*fd); |
| 281 } | 275 } |
| 282 | 276 |
| 283 int WRAP(poll)(struct pollfd *fds, nfds_t nfds, int timeout, int* count) { | 277 int WRAP(poll)(struct pollfd *fds, nfds_t nfds, int timeout, int* count) { |
| 284 *count = ki_poll(fds, nfds, timeout); | 278 *count = ki_poll(fds, nfds, timeout); |
| 285 return (*count < 0) ? errno : 0; | 279 ERRNO_RTN(*count); |
| 286 | 280 |
| 287 } | 281 } |
| 288 | 282 |
| 289 int WRAP(read)(int fd, void *buf, size_t count, size_t *nread) { | 283 int WRAP(read)(int fd, void *buf, size_t count, size_t *nread) { |
| 290 ssize_t signed_nread = ki_read(fd, buf, count); | 284 ssize_t signed_nread = ki_read(fd, buf, count); |
| 291 *nread = static_cast<size_t>(signed_nread); | 285 *nread = static_cast<size_t>(signed_nread); |
| 292 return (signed_nread < 0) ? errno : 0; | 286 ERRNO_RTN(signed_nread); |
| 293 } | 287 } |
| 294 | 288 |
| 295 int WRAP(readlink)(const char* path, char* buf, size_t count, size_t* nread) { | 289 int WRAP(readlink)(const char* path, char* buf, size_t count, size_t* nread) { |
| 296 ssize_t signed_nread = ki_readlink(path, buf, count); | 290 ssize_t signed_nread = ki_readlink(path, buf, count); |
| 297 *nread = static_cast<size_t>(signed_nread); | 291 *nread = static_cast<size_t>(signed_nread); |
| 298 return (signed_nread < 0) ? errno : 0; | 292 ERRNO_RTN(signed_nread); |
| 299 } | 293 } |
| 300 | 294 |
| 301 int WRAP(rmdir)(const char* pathname) { | 295 int WRAP(rmdir)(const char* pathname) { |
| 302 return (ki_rmdir(pathname) < 0) ? errno : 0; | 296 ERRNO_RTN(ki_rmdir(pathname)); |
| 303 } | 297 } |
| 304 | 298 |
| 305 int WRAP(seek)(int fd, off64_t offset, int whence, int64_t* new_offset) { | 299 int WRAP(seek)(int fd, off64_t offset, int whence, int64_t* new_offset) { |
| 306 *new_offset = ki_lseek(fd, offset, whence); | 300 *new_offset = ki_lseek(fd, offset, whence); |
| 307 return (*new_offset < 0) ? errno : 0; | 301 ERRNO_RTN(*new_offset); |
| 308 } | 302 } |
| 309 | 303 |
| 310 int WRAP(select)(int nfds, fd_set* readfds, fd_set* writefds, | 304 int WRAP(select)(int nfds, fd_set* readfds, fd_set* writefds, |
| 311 fd_set* exceptfds, struct timeval* timeout, int* count) { | 305 fd_set* exceptfds, struct timeval* timeout, int* count) { |
| 312 *count = ki_select(nfds, readfds, writefds, exceptfds, timeout); | 306 *count = ki_select(nfds, readfds, writefds, exceptfds, timeout); |
| 313 return (*count < 0) ? errno : 0; | 307 ERRNO_RTN(*count); |
| 314 } | 308 } |
| 315 | 309 |
| 316 int WRAP(stat)(const char *pathname, struct nacl_abi_stat *nacl_buf) { | 310 int WRAP(stat)(const char *pathname, struct nacl_abi_stat *nacl_buf) { |
| 317 struct stat buf; | 311 struct stat buf; |
| 318 memset(&buf, 0, sizeof(struct stat)); | 312 memset(&buf, 0, sizeof(struct stat)); |
| 319 int res = ki_stat(pathname, &buf); | 313 int res = ki_stat(pathname, &buf); |
| 320 if (res < 0) | 314 RTN_ERRNO_IF(res < 0); |
| 321 return errno; | |
| 322 stat_to_nacl_stat(&buf, nacl_buf); | 315 stat_to_nacl_stat(&buf, nacl_buf); |
| 323 return 0; | 316 return 0; |
| 324 } | 317 } |
| 325 | 318 |
| 326 int WRAP(truncate)(const char *name, int64_t len) { | 319 int WRAP(truncate)(const char *name, int64_t len) { |
| 327 return (ki_truncate(name, len)) ? errno : 0; | 320 ERRNO_RTN(ki_truncate(name, len)); |
| 328 } | 321 } |
| 329 | 322 |
| 330 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) { | 323 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) { |
| 331 ssize_t signed_nwrote = ki_write(fd, buf, count); | 324 ssize_t signed_nwrote = ki_write(fd, buf, count); |
| 332 *nwrote = static_cast<size_t>(signed_nwrote); | 325 *nwrote = static_cast<size_t>(signed_nwrote); |
| 333 return (signed_nwrote < 0) ? errno : 0; | 326 ERRNO_RTN(signed_nwrote); |
| 334 } | 327 } |
| 335 | 328 |
| 336 static void assign_real_pointers() { | 329 static void assign_real_pointers() { |
| 337 static bool assigned = false; | 330 static bool assigned = false; |
| 338 if (!assigned) { | 331 if (!assigned) { |
| 339 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR) | 332 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR) |
| 340 assigned = true; | 333 assigned = true; |
| 341 } | 334 } |
| 342 } | 335 } |
| 343 | 336 |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 511 void kernel_wrap_uninit() { | 504 void kernel_wrap_uninit() { |
| 512 if (s_wrapped) { | 505 if (s_wrapped) { |
| 513 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL) | 506 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL) |
| 514 s_wrapped = false; | 507 s_wrapped = false; |
| 515 } | 508 } |
| 516 } | 509 } |
| 517 | 510 |
| 518 EXTERN_C_END | 511 EXTERN_C_END |
| 519 | 512 |
| 520 #endif // defined(__native_client__) && defined(__GLIBC__) | 513 #endif // defined(__native_client__) && defined(__GLIBC__) |
| OLD | NEW |