Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 // The Chromium build system defines __linux__ even for native client builds, | 5 // The Chromium build system defines __linux__ even for native client builds, |
| 6 // so guard against __native_client__ being defined as well. | 6 // so guard against __native_client__ being defined as well. |
| 7 #if defined(WIN32) || (defined(__linux__) && !defined(__native_client__)) | 7 #if defined(WIN32) || (defined(__linux__) && !defined(__native_client__)) |
| 8 | 8 |
| 9 #include <errno.h> | 9 #include <errno.h> |
| 10 | 10 |
| 11 #ifdef __linux__ | |
| 12 #include <dirent.h> | |
| 13 #include <stddef.h> | |
| 14 #include <string.h> | |
| 15 #include <sys/syscall.h> | |
| 16 #include <unistd.h> | |
| 17 struct linux_dirent { | |
| 18 unsigned long d_ino; /* Inode number */ | |
| 19 unsigned long d_off; /* Offset to next linux_dirent */ | |
| 20 unsigned short d_reclen; /* Length of this linux_dirent */ | |
| 21 char d_name[]; /* Filename (null-terminated) */ | |
| 22 }; | |
| 23 #endif | |
| 24 | |
| 11 #include "nacl_io/kernel_wrap.h" | 25 #include "nacl_io/kernel_wrap.h" |
| 12 #include "nacl_io/kernel_wrap_real.h" | 26 #include "nacl_io/kernel_wrap_real.h" |
| 13 | 27 |
| 14 // "real" functions, i.e. the unwrapped original functions. For Windows/Linux | 28 // "real" functions, i.e. the unwrapped original functions. For Windows/Linux |
| 15 // host builds we don't wrap, so the real functions aren't accessible. In most | 29 // host builds we don't wrap, so the real functions aren't accessible. In most |
| 16 // cases, we just fail. | 30 // cases, we just fail. |
| 17 | 31 |
| 18 int _real_close(int fd) { | 32 int _real_close(int fd) { |
| 19 return ENOSYS; | 33 return ENOSYS; |
| 20 } | 34 } |
| 21 | 35 |
| 22 int _real_fstat(int fd, struct stat* buf) { | 36 int _real_fstat(int fd, struct stat* buf) { |
| 23 return 0; | 37 return fstat(fd, buf); |
| 24 } | 38 } |
| 25 | 39 |
| 26 int _real_getdents(int fd, void* nacl_buf, size_t nacl_count, size_t* nread) { | 40 int _real_getdents(int fd, void* in_buf, size_t in_count, size_t* nread) { |
| 41 #ifdef __linux__ | |
| 42 int in_offset = 0; | |
| 43 // "buf" contains linux_dirent(s); "in_buf" contains dirent(s). | |
| 44 // dirents(s) are smaller than linux_dirent(s), so in_count bytes buffer | |
| 45 // is enough | |
|
binji
2015/01/07 19:14:59
Will this do the right thing if you request one di
Sam Clegg
2015/01/08 12:59:07
in_count is normally much bigger than the size of
| |
| 46 char* buf = (char*)alloca(in_count); | |
| 47 int offset = 0; | |
| 48 int count; | |
| 49 | |
| 50 count = syscall(SYS_getdents, fd, buf, in_count); | |
| 51 if (count < 0) | |
| 52 return errno; | |
| 53 | |
| 54 while (offset < count) { | |
| 55 struct linux_dirent* d = (struct linux_dirent*)(buf + offset); | |
| 56 struct dirent* nacl_d = (struct dirent*)((char*)in_buf + in_offset); | |
|
binji
2015/01/07 19:14:59
consistency between in_ and nacl_ prefixes?
Sam Clegg
2015/01/08 12:59:07
Done.
| |
| 57 nacl_d->d_ino = d->d_ino; | |
| 58 nacl_d->d_off = d->d_off; | |
| 59 nacl_d->d_reclen = d->d_reclen; | |
| 60 size_t d_name_len = d->d_reclen - offsetof(linux_dirent, d_name); | |
| 61 memcpy(nacl_d->d_name, d->d_name, d_name_len); | |
| 62 | |
| 63 offset += d->d_reclen; | |
| 64 in_offset += nacl_d->d_reclen; | |
| 65 } | |
| 66 | |
| 67 *nread = in_offset; | |
| 68 return 0; | |
| 69 #else | |
| 27 return ENOSYS; | 70 return ENOSYS; |
| 71 #endif | |
| 28 } | 72 } |
| 29 | 73 |
| 30 int _real_lseek(int fd, off_t offset, int whence, off_t* new_offset) { | 74 int _real_lseek(int fd, off_t offset, int whence, off_t* new_offset) { |
| 31 return ENOSYS; | 75 return ENOSYS; |
| 32 } | 76 } |
| 33 | 77 |
| 34 int _real_mkdir(const char* pathname, mode_t mode) { | 78 int _real_mkdir(const char* pathname, mode_t mode) { |
| 35 return ENOSYS; | 79 return ENOSYS; |
| 36 } | 80 } |
| 37 | 81 |
| 38 int _real_mmap(void** addr, | 82 int _real_mmap(void** addr, |
| 39 size_t length, | 83 size_t length, |
| 40 int prot, | 84 int prot, |
| 41 int flags, | 85 int flags, |
| 42 int fd, | 86 int fd, |
| 43 off_t offset) { | 87 off_t offset) { |
| 44 return ENOSYS; | 88 return ENOSYS; |
| 45 } | 89 } |
| 46 | 90 |
| 47 int _real_munmap(void* addr, size_t length) { | 91 int _real_munmap(void* addr, size_t length) { |
| 48 return ENOSYS; | 92 return ENOSYS; |
| 49 } | 93 } |
| 50 | 94 |
| 51 int _real_open(const char* pathname, int oflag, mode_t mode, int* newfd) { | 95 int _real_open(const char* pathname, int oflag, mode_t mode, int* newfd) { |
| 52 return ENOSYS; | 96 int fd = open(pathname, oflag, mode); |
| 97 if (fd < 0) | |
| 98 return errno; | |
| 99 | |
| 100 *newfd = fd; | |
| 101 return 0; | |
| 53 } | 102 } |
| 54 | 103 |
| 55 int _real_open_resource(const char* file, int* fd) { | 104 int _real_open_resource(const char* file, int* fd) { |
| 56 return ENOSYS; | 105 return ENOSYS; |
| 57 } | 106 } |
| 58 | 107 |
| 59 int _real_read(int fd, void* buf, size_t count, size_t* nread) { | 108 int _real_read(int fd, void* buf, size_t count, size_t* nread) { |
| 60 *nread = count; | 109 int rtn = read(fd, buf, count); |
| 110 if (rtn < 0) | |
| 111 return errno; | |
| 112 | |
| 113 *nread = rtn; | |
| 61 return 0; | 114 return 0; |
| 62 } | 115 } |
| 63 | 116 |
| 64 int _real_rmdir(const char* pathname) { | 117 int _real_rmdir(const char* pathname) { |
| 65 return ENOSYS; | 118 return ENOSYS; |
| 66 } | 119 } |
| 67 | 120 |
| 68 int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) { | 121 int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) { |
| 69 int rtn = write(fd, buf, count); | 122 int rtn = write(fd, buf, count); |
| 70 if (rtn < 0) | 123 if (rtn < 0) |
| 71 return -1; | 124 return errno; |
| 72 | 125 |
| 73 *nwrote = rtn; | 126 *nwrote = rtn; |
| 74 return 0; | 127 return 0; |
| 75 } | 128 } |
| 76 | 129 |
| 77 void _real_exit(int status) { | 130 void _real_exit(int status) { |
| 78 exit(status); | 131 exit(status); |
| 79 } | 132 } |
| 80 | 133 |
| 81 int _real_getcwd(char* pathname, size_t len) { | 134 int _real_getcwd(char* pathname, size_t len) { |
| 82 return ENOSYS; | 135 return ENOSYS; |
| 83 } | 136 } |
| 84 | 137 |
| 85 #endif | 138 #endif |
| 86 | 139 |
| 87 // The Chromium build system defines __linux__ even for native client builds, | 140 // The Chromium build system defines __linux__ even for native client builds, |
| 88 // so guard against __native_client__ being defined as well. | 141 // so guard against __native_client__ being defined as well. |
| 89 #if defined(__linux__) && !defined(__native_client__) | 142 #if defined(__linux__) && !defined(__native_client__) |
| 90 | 143 |
| 91 void kernel_wrap_init() { | 144 void kernel_wrap_init() { |
| 92 } | 145 } |
| 93 | 146 |
| 94 void kernel_wrap_uninit() { | 147 void kernel_wrap_uninit() { |
| 95 } | 148 } |
| 96 | 149 |
| 97 #endif | 150 #endif |
| OLD | NEW |