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 "nacl_io/kernel_intercept.h" | 5 #include "nacl_io/kernel_intercept.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 | 10 |
| 11 #include "nacl_io/kernel_proxy.h" | 11 #include "nacl_io/kernel_proxy.h" |
| 12 #include "nacl_io/kernel_wrap.h" | 12 #include "nacl_io/kernel_wrap.h" |
| 13 #include "nacl_io/kernel_wrap_real.h" | 13 #include "nacl_io/kernel_wrap_real.h" |
| 14 #include "nacl_io/log.h" | 14 #include "nacl_io/log.h" |
| 15 #include "nacl_io/osmman.h" | 15 #include "nacl_io/osmman.h" |
| 16 #include "nacl_io/ossocket.h" | 16 #include "nacl_io/ossocket.h" |
| 17 #include "nacl_io/ostime.h" | 17 #include "nacl_io/ostime.h" |
| 18 #include "nacl_io/pepper_interface.h" | 18 #include "nacl_io/pepper_interface.h" |
| 19 #include "nacl_io/real_pepper_interface.h" | 19 #include "nacl_io/real_pepper_interface.h" |
| 20 | 20 |
| 21 using namespace nacl_io; | 21 using namespace nacl_io; |
| 22 | 22 |
| 23 #define ON_NOSYS_RETURN(x) \ | 23 #define ON_NOSYS_RETURN(x) \ |
| 24 if (!ki_is_initialized()) { \ | 24 if (!ki_is_initialized()) { \ |
| 25 errno = ENOSYS; \ | 25 errno = ENOSYS; \ |
| 26 return x; \ | 26 return x; \ |
| 27 } | 27 } |
| 28 | 28 |
| 29 #define TRACE_KP_CALLS 0 | |
| 30 | |
| 31 #if TRACE_KP_CALLS | |
| 32 #define TRACE_KP nacl_io_log | |
| 33 #else | |
| 34 #define TRACE_KP(...) | |
| 35 #endif | |
| 36 | |
| 37 #define KP_CALL(METHOD, ARGS) \ | |
|
binji
2014/12/02 18:31:56
I like this in general, but I don't like that you'
Sam Clegg
2015/01/13 18:46:29
Done.
| |
| 38 ON_NOSYS_RETURN(-1); \ | |
| 39 int rtn = s_state.kp-> METHOD ARGS; \ | |
| 40 TRACE_KP("ki_" #METHOD " -> %d\n", rtn); \ | |
| 41 return rtn; | |
| 42 | |
| 29 struct KernelInterceptState { | 43 struct KernelInterceptState { |
| 30 KernelProxy* kp; | 44 KernelProxy* kp; |
| 31 PepperInterface* ppapi; | 45 PepperInterface* ppapi; |
| 32 bool kp_owned; | 46 bool kp_owned; |
| 33 }; | 47 }; |
| 34 | 48 |
| 35 static KernelInterceptState s_state; | 49 static KernelInterceptState s_state; |
| 36 | 50 |
| 37 // The the test code we want to be able to save the previous kernel | 51 // The the test code we want to be able to save the previous kernel |
| 38 // proxy when intialising and restore it on uninit. | 52 // proxy when intialising and restore it on uninit. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 129 delete state_to_delete.kp; | 143 delete state_to_delete.kp; |
| 130 | 144 |
| 131 delete state_to_delete.ppapi; | 145 delete state_to_delete.ppapi; |
| 132 return 0; | 146 return 0; |
| 133 } | 147 } |
| 134 | 148 |
| 135 nacl_io::KernelProxy* ki_get_proxy() { | 149 nacl_io::KernelProxy* ki_get_proxy() { |
| 136 return s_state.kp; | 150 return s_state.kp; |
| 137 } | 151 } |
| 138 | 152 |
| 139 int ki_chdir(const char* path) { | |
| 140 ON_NOSYS_RETURN(-1); | |
| 141 return s_state.kp->chdir(path); | |
| 142 } | |
| 143 | |
| 144 void ki_exit(int status) { | 153 void ki_exit(int status) { |
| 145 if (ki_is_initialized()) | 154 if (ki_is_initialized()) |
| 146 s_state.kp->exit(status); | 155 s_state.kp->exit(status); |
| 147 | 156 |
| 148 _real_exit(status); | 157 _real_exit(status); |
| 149 } | 158 } |
| 150 | 159 |
| 151 char* ki_getcwd(char* buf, size_t size) { | 160 char* ki_getcwd(char* buf, size_t size) { |
| 152 // gtest uses getcwd in a static initializer and expects it to always | 161 // gtest uses getcwd in a static initializer and expects it to always |
| 153 // succeed. If we haven't initialized kernel-intercept yet, then try | 162 // succeed. If we haven't initialized kernel-intercept yet, then try |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 166 return buf; | 175 return buf; |
| 167 } | 176 } |
| 168 return s_state.kp->getcwd(buf, size); | 177 return s_state.kp->getcwd(buf, size); |
| 169 } | 178 } |
| 170 | 179 |
| 171 char* ki_getwd(char* buf) { | 180 char* ki_getwd(char* buf) { |
| 172 ON_NOSYS_RETURN(NULL); | 181 ON_NOSYS_RETURN(NULL); |
| 173 return s_state.kp->getwd(buf); | 182 return s_state.kp->getwd(buf); |
| 174 } | 183 } |
| 175 | 184 |
| 185 int ki_chdir(const char* path) { | |
| 186 KP_CALL(chdir, (path)); | |
| 187 } | |
| 188 | |
| 176 int ki_dup(int oldfd) { | 189 int ki_dup(int oldfd) { |
| 177 ON_NOSYS_RETURN(-1); | 190 KP_CALL(dup, (oldfd)); |
| 178 return s_state.kp->dup(oldfd); | |
| 179 } | 191 } |
| 180 | 192 |
| 181 int ki_dup2(int oldfd, int newfd) { | 193 int ki_dup2(int oldfd, int newfd) { |
| 182 ON_NOSYS_RETURN(-1); | 194 KP_CALL(dup2, (oldfd, newfd)); |
| 183 return s_state.kp->dup2(oldfd, newfd); | |
| 184 } | 195 } |
| 185 | 196 |
| 186 int ki_chmod(const char* path, mode_t mode) { | 197 int ki_chmod(const char* path, mode_t mode) { |
| 187 ON_NOSYS_RETURN(-1); | 198 KP_CALL(chmod, (path, mode)); |
| 188 return s_state.kp->chmod(path, mode); | |
| 189 } | 199 } |
| 190 | 200 |
| 191 int ki_fchdir(int fd) { | 201 int ki_fchdir(int fd) { |
| 192 ON_NOSYS_RETURN(-1); | 202 KP_CALL(fchdir, (fd)); |
| 193 return s_state.kp->fchdir(fd); | |
| 194 } | 203 } |
| 195 | 204 |
| 196 int ki_fchmod(int fd, mode_t mode) { | 205 int ki_fchmod(int fd, mode_t mode) { |
| 197 ON_NOSYS_RETURN(-1); | 206 KP_CALL(fchmod, (fd, mode)); |
| 198 return s_state.kp->fchmod(fd, mode); | |
| 199 } | 207 } |
| 200 | 208 |
| 201 int ki_stat(const char* path, struct stat* buf) { | 209 int ki_stat(const char* path, struct stat* buf) { |
| 202 ON_NOSYS_RETURN(-1); | 210 KP_CALL(stat, (path, buf)); |
| 203 return s_state.kp->stat(path, buf); | |
| 204 } | 211 } |
| 205 | 212 |
| 206 int ki_mkdir(const char* path, mode_t mode) { | 213 int ki_mkdir(const char* path, mode_t mode) { |
| 207 ON_NOSYS_RETURN(-1); | 214 KP_CALL(mkdir, (path, mode)); |
| 208 return s_state.kp->mkdir(path, mode); | |
| 209 } | 215 } |
| 210 | 216 |
| 211 int ki_rmdir(const char* path) { | 217 int ki_rmdir(const char* path) { |
| 212 ON_NOSYS_RETURN(-1); | 218 KP_CALL(rmdir, (path)); |
| 213 return s_state.kp->rmdir(path); | |
| 214 } | 219 } |
| 215 | 220 |
| 216 int ki_mount(const char* source, | 221 int ki_mount(const char* source, |
| 217 const char* target, | 222 const char* target, |
| 218 const char* filesystemtype, | 223 const char* filesystemtype, |
| 219 unsigned long mountflags, | 224 unsigned long mountflags, |
| 220 const void* data) { | 225 const void* data) { |
| 221 ON_NOSYS_RETURN(-1); | 226 KP_CALL(mount, (source, target, filesystemtype, mountflags, data)); |
| 222 return s_state.kp->mount(source, target, filesystemtype, mountflags, data); | |
| 223 } | 227 } |
| 224 | 228 |
| 225 int ki_umount(const char* path) { | 229 int ki_umount(const char* path) { |
| 226 ON_NOSYS_RETURN(-1); | 230 KP_CALL(umount, (path)); |
| 227 return s_state.kp->umount(path); | |
| 228 } | 231 } |
| 229 | 232 |
| 230 int ki_open(const char* path, int oflag, mode_t mode) { | 233 int ki_open(const char* path, int oflag, mode_t mode) { |
| 231 ON_NOSYS_RETURN(-1); | 234 KP_CALL(open, (path, oflag, mode)); |
| 232 return s_state.kp->open(path, oflag, mode); | |
| 233 } | 235 } |
| 234 | 236 |
| 235 int ki_pipe(int pipefds[2]) { | 237 int ki_pipe(int pipefds[2]) { |
| 236 ON_NOSYS_RETURN(-1); | 238 KP_CALL(pipe, (pipefds)); |
| 237 return s_state.kp->pipe(pipefds); | |
| 238 } | 239 } |
| 239 | 240 |
| 240 ssize_t ki_read(int fd, void* buf, size_t nbyte) { | 241 ssize_t ki_read(int fd, void* buf, size_t nbyte) { |
| 241 ON_NOSYS_RETURN(-1); | 242 ON_NOSYS_RETURN(-1); |
| 242 return s_state.kp->read(fd, buf, nbyte); | 243 return s_state.kp->read(fd, buf, nbyte); |
| 243 } | 244 } |
| 244 | 245 |
| 245 ssize_t ki_write(int fd, const void* buf, size_t nbyte) { | 246 ssize_t ki_write(int fd, const void* buf, size_t nbyte) { |
| 246 ON_NOSYS_RETURN(-1); | 247 ON_NOSYS_RETURN(-1); |
| 247 return s_state.kp->write(fd, buf, nbyte); | 248 return s_state.kp->write(fd, buf, nbyte); |
| 248 } | 249 } |
| 249 | 250 |
| 250 int ki_fstat(int fd, struct stat* buf) { | 251 int ki_fstat(int fd, struct stat* buf) { |
| 251 ON_NOSYS_RETURN(-1); | 252 KP_CALL(fstat, (fd, buf)); |
| 252 return s_state.kp->fstat(fd, buf); | |
| 253 } | 253 } |
| 254 | 254 |
| 255 int ki_getdents(int fd, void* buf, unsigned int count) { | 255 int ki_getdents(int fd, void* buf, unsigned int count) { |
| 256 ON_NOSYS_RETURN(-1); | 256 KP_CALL(getdents, (fd, buf, count)); |
| 257 return s_state.kp->getdents(fd, buf, count); | |
| 258 } | 257 } |
| 259 | 258 |
| 260 int ki_ftruncate(int fd, off_t length) { | 259 int ki_ftruncate(int fd, off_t length) { |
| 261 ON_NOSYS_RETURN(-1); | 260 KP_CALL(ftruncate, (fd, length)); |
| 262 return s_state.kp->ftruncate(fd, length); | |
| 263 } | 261 } |
| 264 | 262 |
| 265 int ki_fsync(int fd) { | 263 int ki_fsync(int fd) { |
| 266 ON_NOSYS_RETURN(-1); | 264 KP_CALL(fsync, (fd)); |
| 267 return s_state.kp->fsync(fd); | |
| 268 } | 265 } |
| 269 | 266 |
| 270 int ki_fdatasync(int fd) { | 267 int ki_fdatasync(int fd) { |
| 271 ON_NOSYS_RETURN(-1); | 268 KP_CALL(fdatasync, (fd)); |
| 272 return s_state.kp->fdatasync(fd); | |
| 273 } | 269 } |
| 274 | 270 |
| 275 int ki_isatty(int fd) { | 271 int ki_isatty(int fd) { |
| 276 ON_NOSYS_RETURN(0); | 272 ON_NOSYS_RETURN(0); |
| 277 return s_state.kp->isatty(fd); | 273 return s_state.kp->isatty(fd); |
| 278 } | 274 } |
| 279 | 275 |
| 280 int ki_close(int fd) { | 276 int ki_close(int fd) { |
| 281 ON_NOSYS_RETURN(-1); | 277 KP_CALL(close, (fd)); |
| 282 return s_state.kp->close(fd); | |
| 283 } | 278 } |
| 284 | 279 |
| 285 off_t ki_lseek(int fd, off_t offset, int whence) { | 280 off_t ki_lseek(int fd, off_t offset, int whence) { |
| 286 ON_NOSYS_RETURN(-1); | 281 ON_NOSYS_RETURN(-1); |
| 287 return s_state.kp->lseek(fd, offset, whence); | 282 return s_state.kp->lseek(fd, offset, whence); |
| 288 } | 283 } |
| 289 | 284 |
| 290 int ki_remove(const char* path) { | 285 int ki_remove(const char* path) { |
| 291 ON_NOSYS_RETURN(-1); | 286 KP_CALL(remove, (path)); |
| 292 return s_state.kp->remove(path); | |
| 293 } | 287 } |
| 294 | 288 |
| 295 int ki_unlink(const char* path) { | 289 int ki_unlink(const char* path) { |
| 296 ON_NOSYS_RETURN(-1); | 290 KP_CALL(unlink, (path)); |
| 297 return s_state.kp->unlink(path); | |
| 298 } | 291 } |
| 299 | 292 |
| 300 int ki_truncate(const char* path, off_t length) { | 293 int ki_truncate(const char* path, off_t length) { |
| 301 ON_NOSYS_RETURN(-1); | 294 KP_CALL(truncate, (path, length)); |
| 302 return s_state.kp->truncate(path, length); | |
| 303 } | 295 } |
| 304 | 296 |
| 305 int ki_lstat(const char* path, struct stat* buf) { | 297 int ki_lstat(const char* path, struct stat* buf) { |
| 306 ON_NOSYS_RETURN(-1); | 298 KP_CALL(lstat, (path, buf)); |
| 307 return s_state.kp->lstat(path, buf); | |
| 308 } | 299 } |
| 309 | 300 |
| 310 int ki_link(const char* oldpath, const char* newpath) { | 301 int ki_link(const char* oldpath, const char* newpath) { |
| 311 ON_NOSYS_RETURN(-1); | 302 KP_CALL(link, (oldpath, newpath)); |
| 312 return s_state.kp->link(oldpath, newpath); | |
| 313 } | 303 } |
| 314 | 304 |
| 315 int ki_rename(const char* path, const char* newpath) { | 305 int ki_rename(const char* path, const char* newpath) { |
| 316 ON_NOSYS_RETURN(-1); | 306 KP_CALL(rename, (path, newpath)); |
| 317 return s_state.kp->rename(path, newpath); | |
| 318 } | 307 } |
| 319 | 308 |
| 320 int ki_symlink(const char* oldpath, const char* newpath) { | 309 int ki_symlink(const char* oldpath, const char* newpath) { |
| 321 ON_NOSYS_RETURN(-1); | 310 KP_CALL(symlink, (oldpath, newpath)); |
| 322 return s_state.kp->symlink(oldpath, newpath); | |
| 323 } | 311 } |
| 324 | 312 |
| 325 int ki_access(const char* path, int amode) { | 313 int ki_access(const char* path, int amode) { |
| 326 ON_NOSYS_RETURN(-1); | 314 KP_CALL(access, (path, amode)); |
| 327 return s_state.kp->access(path, amode); | |
| 328 } | 315 } |
| 329 | 316 |
| 330 int ki_readlink(const char* path, char* buf, size_t count) { | 317 int ki_readlink(const char* path, char* buf, size_t count) { |
| 331 ON_NOSYS_RETURN(-1); | 318 KP_CALL(readlink, (path, buf, count)); |
| 332 return s_state.kp->readlink(path, buf, count); | |
| 333 } | 319 } |
| 334 | 320 |
| 335 int ki_utimes(const char* path, const struct timeval times[2]) { | 321 int ki_utimes(const char* path, const struct timeval times[2]) { |
| 336 ON_NOSYS_RETURN(-1); | 322 ON_NOSYS_RETURN(-1); |
| 337 // Implement in terms of utimens. | 323 // Implement in terms of utimens. |
| 338 if (!times) { | 324 if (!times) { |
| 339 return s_state.kp->utimens(path, NULL); | 325 return s_state.kp->utimens(path, NULL); |
| 340 } | 326 } |
| 341 | 327 |
| 342 struct timespec ts[2]; | 328 struct timespec ts[2]; |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 634 int ki_socket(int domain, int type, int protocol) { | 620 int ki_socket(int domain, int type, int protocol) { |
| 635 ON_NOSYS_RETURN(-1); | 621 ON_NOSYS_RETURN(-1); |
| 636 return s_state.kp->socket(domain, type, protocol); | 622 return s_state.kp->socket(domain, type, protocol); |
| 637 } | 623 } |
| 638 | 624 |
| 639 int ki_socketpair(int domain, int type, int protocol, int* sv) { | 625 int ki_socketpair(int domain, int type, int protocol, int* sv) { |
| 640 ON_NOSYS_RETURN(-1); | 626 ON_NOSYS_RETURN(-1); |
| 641 return s_state.kp->socketpair(domain, type, protocol, sv); | 627 return s_state.kp->socketpair(domain, type, protocol, sv); |
| 642 } | 628 } |
| 643 #endif // PROVIDES_SOCKET_API | 629 #endif // PROVIDES_SOCKET_API |
| OLD | NEW |