Chromium Code Reviews| Index: native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc |
| diff --git a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc |
| index f895ffb0fee46d76c87053a7fd1ce587a84e191f..5c0a3b4c713faf47b17ec96af06f24f67dd040b8 100644 |
| --- a/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc |
| +++ b/native_client_sdk/src/libraries/nacl_io/kernel_wrap_glibc.cc |
| @@ -23,6 +23,7 @@ |
| #include "nacl_io/kernel_intercept.h" |
| #include "nacl_io/kernel_wrap_real.h" |
| +#include "nacl_io/log.h" |
| #include "nacl_io/osmman.h" |
| @@ -147,21 +148,36 @@ EXTERN_C_BEGIN |
| EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR); |
| +// Most kernal intercept functions (ki_*) return -1 and set errno accordingly. |
|
binji
2014/05/06 23:25:54
nit: kernel
Sam Clegg
2014/05/07 00:29:03
Done.
|
| +// However the IRT wrappers are expected to return errno on failure. Here we |
| +// check that the ki_ function actually set errno and fall back to EIO if it |
| +// didn't. |
| +#define RTN_ERRNO_IF(cond) \ |
| + if (cond) { \ |
| + int rtn = errno; \ |
| + if (rtn == 0) rtn = EIO; \ |
|
binji
2014/05/06 23:25:54
we should probably assert here, right?
Sam Clegg
2014/05/07 00:29:03
Done.
|
| + return rtn; \ |
| + } |
| + |
| int WRAP(chdir)(const char* pathname) { |
| - return (ki_chdir(pathname)) ? errno : 0; |
| + RTN_ERRNO_IF(ki_chdir(pathname) < 0); |
| + return 0; |
| } |
| int WRAP(close)(int fd) { |
| - return (ki_close(fd) < 0) ? errno : 0; |
| + RTN_ERRNO_IF(ki_close(fd) < 0); |
| + return 0; |
| } |
| int WRAP(dup)(int fd, int* newfd) NOTHROW { |
| *newfd = ki_dup(fd); |
| - return (*newfd < 0) ? errno : 0; |
| + RTN_ERRNO_IF(*newfd < 0); |
| + return 0; |
| } |
| int WRAP(dup2)(int fd, int newfd) NOTHROW { |
| - return (ki_dup2(fd, newfd) < 0) ? errno : 0; |
| + RTN_ERRNO_IF(ki_dup2(fd, newfd) < 0); |
| + return 0; |
| } |
| void WRAP(exit)(int status) { |
| @@ -172,15 +188,13 @@ int WRAP(fstat)(int fd, struct nacl_abi_stat *nacl_buf) { |
| struct stat buf; |
| memset(&buf, 0, sizeof(struct stat)); |
| int res = ki_fstat(fd, &buf); |
| - if (res < 0) |
| - return errno; |
| + RTN_ERRNO_IF(res < 0); |
| stat_to_nacl_stat(&buf, nacl_buf); |
| return 0; |
| } |
| int WRAP(getcwd)(char* buf, size_t size) { |
| - if (ki_getcwd(buf, size) == NULL) |
| - return errno; |
| + RTN_ERRNO_IF(ki_getcwd(buf, size) == NULL); |
| return 0; |
| } |
| @@ -194,8 +208,7 @@ int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, size_t *nread) { |
| int count; |
| count = ki_getdents(fd, buf, nacl_count); |
| - if (count < 0) |
| - return errno; |
| + RTN_ERRNO_IF(count < 0); |
| while (offset < count) { |
| dirent* d = (dirent*)(buf + offset); |
| @@ -215,7 +228,8 @@ int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, size_t *nread) { |
| } |
| int WRAP(mkdir)(const char* pathname, mode_t mode) { |
| - return (ki_mkdir(pathname, mode)) ? errno : 0; |
| + RTN_ERRNO_IF(ki_mkdir(pathname, mode) < 0); |
| + return 0; |
| } |
| int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd, |
| @@ -224,7 +238,8 @@ int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd, |
| return REAL(mmap)(addr, length, prot, flags, fd, offset); |
| *addr = ki_mmap(*addr, length, prot, flags, fd, offset); |
| - return *addr == (void*)-1 ? errno : 0; |
| + RTN_ERRNO_IF(*addr == (void*)-1); |
| + return 0; |
| } |
| int WRAP(munmap)(void* addr, size_t length) { |
| @@ -236,47 +251,52 @@ int WRAP(munmap)(void* addr, size_t length) { |
| int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) { |
| *newfd = ki_open(pathname, oflag); |
| - return (*newfd < 0) ? errno : 0; |
| + RTN_ERRNO_IF(*newfd < 0); |
| + return 0; |
| } |
| int WRAP(open_resource)(const char* file, int* fd) { |
| *fd = ki_open_resource(file); |
| - return (*fd < 0) ? errno : 0; |
| + RTN_ERRNO_IF(*fd < 0); |
| + return 0; |
| } |
| int WRAP(poll)(struct pollfd *fds, nfds_t nfds, int timeout, int* count) { |
| *count = ki_poll(fds, nfds, timeout); |
| - return (*count < 0) ? errno : 0; |
| - |
| + RTN_ERRNO_IF(*count < 0); |
| + return 0; |
| } |
| int WRAP(read)(int fd, void *buf, size_t count, size_t *nread) { |
| ssize_t signed_nread = ki_read(fd, buf, count); |
| *nread = static_cast<size_t>(signed_nread); |
| - return (signed_nread < 0) ? errno : 0; |
| + RTN_ERRNO_IF(signed_nread < 0); |
| + return 0; |
| } |
| int WRAP(rmdir)(const char* pathname) { |
| - return (ki_rmdir(pathname) < 0) ? errno : 0; |
| + RTN_ERRNO_IF(ki_rmdir(pathname) < 0); |
| + return 0; |
| } |
| int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) { |
| *new_offset = ki_lseek(fd, offset, whence); |
| - return (*new_offset < 0) ? errno : 0; |
| + RTN_ERRNO_IF(*new_offset < 0); |
| + return 0; |
| } |
| int WRAP(select)(int nfds, fd_set* readfds, fd_set* writefds, |
| fd_set* exceptfds, struct timeval* timeout, int* count) { |
| *count = ki_select(nfds, readfds, writefds, exceptfds, timeout); |
| - return (*count < 0) ? errno : 0; |
| + RTN_ERRNO_IF(*count < 0); |
| + return errno; |
|
binji
2014/05/06 23:29:52
return 0
Sam Clegg
2014/05/07 00:29:03
Done.
|
| } |
| int WRAP(stat)(const char *pathname, struct nacl_abi_stat *nacl_buf) { |
| struct stat buf; |
| memset(&buf, 0, sizeof(struct stat)); |
| int res = ki_stat(pathname, &buf); |
| - if (res < 0) |
| - return errno; |
| + RTN_ERRNO_IF(res < 0); |
| stat_to_nacl_stat(&buf, nacl_buf); |
| return 0; |
| } |
| @@ -284,7 +304,8 @@ int WRAP(stat)(const char *pathname, struct nacl_abi_stat *nacl_buf) { |
| int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) { |
| ssize_t signed_nwrote = ki_write(fd, buf, count); |
| *nwrote = static_cast<size_t>(signed_nwrote); |
| - return (signed_nwrote < 0) ? errno : 0; |
| + RTN_ERRNO_IF(signed_nwrote < 0); |
| + return 0; |
| } |
| static void assign_real_pointers() { |
| @@ -403,6 +424,7 @@ int _real_write(int fd, const void *buf, size_t count, size_t *nwrote) { |
| static bool s_wrapped = false; |
| void kernel_wrap_init() { |
| if (!s_wrapped) { |
| + LOG_TRACE("kernel_wrap_init"); |
| assign_real_pointers(); |
| EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP) |
| s_wrapped = true; |
| @@ -411,6 +433,7 @@ void kernel_wrap_init() { |
| void kernel_wrap_uninit() { |
| if (s_wrapped) { |
| + LOG_TRACE("kernel_wrap_uninit"); |
| EXPAND_SYMBOL_LIST_OPERATION(USE_REAL) |
| s_wrapped = false; |
| } |