| OLD | NEW |
| (Empty) |
| 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 | |
| 3 * found in the LICENSE file. | |
| 4 */ | |
| 5 | |
| 6 #include <sys/types.h> // Include something that will define __GLIBC__. | |
| 7 | |
| 8 // The entire file is wrapped in this #if. We do this so this .cc file can be | |
| 9 // compiled, even on a non-glibc build. | |
| 10 #if defined(__native_client__) && defined(__GLIBC__) | |
| 11 | |
| 12 #include "nacl_mounts/kernel_wrap.h" | |
| 13 #include <alloca.h> | |
| 14 #include <dirent.h> | |
| 15 #include <errno.h> | |
| 16 #include <irt.h> | |
| 17 #include <irt_syscalls.h> | |
| 18 #include <nacl_stat.h> | |
| 19 #include <string.h> | |
| 20 #include <sys/stat.h> | |
| 21 #include "nacl_mounts/kernel_intercept.h" | |
| 22 | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 void stat_to_nacl_stat(const struct stat* buf, nacl_abi_stat* nacl_buf) { | |
| 27 memset(nacl_buf, 0, sizeof(struct nacl_abi_stat)); | |
| 28 nacl_buf->nacl_abi_st_dev = buf->st_dev; | |
| 29 nacl_buf->nacl_abi_st_ino = buf->st_ino; | |
| 30 nacl_buf->nacl_abi_st_mode = buf->st_mode; | |
| 31 nacl_buf->nacl_abi_st_nlink = buf->st_nlink; | |
| 32 nacl_buf->nacl_abi_st_uid = buf->st_uid; | |
| 33 nacl_buf->nacl_abi_st_gid = buf->st_gid; | |
| 34 nacl_buf->nacl_abi_st_rdev = buf->st_rdev; | |
| 35 nacl_buf->nacl_abi_st_size = buf->st_size; | |
| 36 nacl_buf->nacl_abi_st_blksize = buf->st_blksize; | |
| 37 nacl_buf->nacl_abi_st_blocks = buf->st_blocks; | |
| 38 nacl_buf->nacl_abi_st_atime = buf->st_atime; | |
| 39 nacl_buf->nacl_abi_st_mtime = buf->st_mtime; | |
| 40 nacl_buf->nacl_abi_st_ctime = buf->st_ctime; | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 // From native_client/src/trusted/service_runtime/include/sys/dirent.h | |
| 46 | |
| 47 #ifndef nacl_abi___ino_t_defined | |
| 48 #define nacl_abi___ino_t_defined | |
| 49 typedef int64_t nacl_abi___ino_t; | |
| 50 typedef nacl_abi___ino_t nacl_abi_ino_t; | |
| 51 #endif | |
| 52 | |
| 53 #ifndef nacl_abi___off_t_defined | |
| 54 #define nacl_abi___off_t_defined | |
| 55 typedef int64_t nacl_abi__off_t; | |
| 56 typedef nacl_abi__off_t nacl_abi_off_t; | |
| 57 #endif | |
| 58 | |
| 59 /* We need a way to define the maximum size of a name. */ | |
| 60 #ifndef MAXNAMLEN | |
| 61 # ifdef NAME_MAX | |
| 62 # define MAXNAMLEN NAME_MAX | |
| 63 # else | |
| 64 # define MAXNAMLEN 255 | |
| 65 # endif | |
| 66 #endif | |
| 67 | |
| 68 struct nacl_abi_dirent { | |
| 69 nacl_abi_ino_t nacl_abi_d_ino; | |
| 70 nacl_abi_off_t nacl_abi_d_off; | |
| 71 uint16_t nacl_abi_d_reclen; | |
| 72 char nacl_abi_d_name[MAXNAMLEN + 1]; | |
| 73 }; | |
| 74 | |
| 75 static const int d_name_shift = offsetof (dirent, d_name) - | |
| 76 offsetof (struct nacl_abi_dirent, nacl_abi_d_name); | |
| 77 | |
| 78 EXTERN_C_BEGIN | |
| 79 | |
| 80 #define REAL(name) __nacl_irt_##name##_real | |
| 81 #define WRAP(name) __nacl_irt_##name##_wrap | |
| 82 #define MUX(name) __nacl_irt_##name | |
| 83 #define DECLARE(name) typeof(MUX(name)) REAL(name); | |
| 84 #define DO_WRAP(name) do { \ | |
| 85 REAL(name) = MUX(name); \ | |
| 86 MUX(name) = (typeof(REAL(name))) WRAP(name); \ | |
| 87 } while (0) | |
| 88 | |
| 89 DECLARE(chdir) | |
| 90 DECLARE(close) | |
| 91 DECLARE(dup) | |
| 92 DECLARE(dup2) | |
| 93 DECLARE(fstat) | |
| 94 DECLARE(getcwd) | |
| 95 DECLARE(getdents) | |
| 96 DECLARE(mkdir) | |
| 97 DECLARE(open) | |
| 98 DECLARE(read) | |
| 99 DECLARE(rmdir) | |
| 100 DECLARE(seek) | |
| 101 DECLARE(stat) | |
| 102 DECLARE(write) | |
| 103 | |
| 104 int access(const char* path, int amode) NOTHROW { | |
| 105 return ki_access(path, amode); | |
| 106 } | |
| 107 | |
| 108 int chdir(const char* path) NOTHROW { | |
| 109 return ki_chdir(path); | |
| 110 } | |
| 111 | |
| 112 int chmod(const char* path, mode_t mode) NOTHROW { | |
| 113 return ki_chmod(path, mode); | |
| 114 } | |
| 115 | |
| 116 int WRAP(chdir) (const char* pathname) { | |
| 117 return (ki_chdir(pathname)) ? errno : 0; | |
| 118 } | |
| 119 | |
| 120 int WRAP(close)(int fd) { | |
| 121 return (ki_close(fd) < 0) ? errno : 0; | |
| 122 } | |
| 123 | |
| 124 int WRAP(dup)(int fd, int* newfd) NOTHROW { | |
| 125 *newfd = ki_dup(fd); | |
| 126 return (*newfd < 0) ? errno : 0; | |
| 127 } | |
| 128 | |
| 129 int WRAP(dup2)(int fd, int newfd) NOTHROW { | |
| 130 return (ki_dup2(fd, newfd) < 0) ? errno : 0; | |
| 131 } | |
| 132 | |
| 133 int WRAP(fstat)(int fd, struct nacl_abi_stat *nacl_buf) { | |
| 134 struct stat buf; | |
| 135 memset(&buf, 0, sizeof(struct stat)); | |
| 136 int res = ki_fstat(fd, &buf); | |
| 137 if (res < 0) | |
| 138 return errno; | |
| 139 stat_to_nacl_stat(&buf, nacl_buf); | |
| 140 return 0; | |
| 141 } | |
| 142 | |
| 143 int fsync(int fd) { | |
| 144 return ki_fsync(fd); | |
| 145 } | |
| 146 | |
| 147 char* getcwd(char* buf, size_t size) NOTHROW { | |
| 148 return ki_getcwd(buf, size); | |
| 149 } | |
| 150 | |
| 151 char* WRAP(getcwd)(char* buf, size_t size) { | |
| 152 return ki_getcwd(buf, size); | |
| 153 } | |
| 154 | |
| 155 char* getwd(char* buf) NOTHROW { | |
| 156 return ki_getwd(buf); | |
| 157 } | |
| 158 | |
| 159 int getdents(int fd, void* buf, unsigned int count) NOTHROW { | |
| 160 return ki_getdents(fd, buf, count); | |
| 161 } | |
| 162 | |
| 163 int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, size_t *nread) { | |
| 164 int nacl_offset = 0; | |
| 165 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s). | |
| 166 // nacl_abi_dirent(s) are smaller than dirent(s), so nacl_count bytes buffer | |
| 167 // is enough | |
| 168 char* buf = (char*)alloca(nacl_count); | |
| 169 int offset = 0; | |
| 170 int count; | |
| 171 | |
| 172 count = ki_getdents(fd, buf, nacl_count); | |
| 173 if (count < 0) | |
| 174 return errno; | |
| 175 | |
| 176 while (offset < count) { | |
| 177 dirent* d = (dirent*)(buf + offset); | |
| 178 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)((char*)nacl_buf + nacl_offset); | |
| 179 nacl_d->nacl_abi_d_ino = d->d_ino; | |
| 180 nacl_d->nacl_abi_d_off = d->d_off; | |
| 181 nacl_d->nacl_abi_d_reclen = d->d_reclen - d_name_shift; | |
| 182 size_t d_name_len = d->d_reclen - offsetof(dirent, d_name); | |
| 183 memcpy(nacl_d->nacl_abi_d_name, d->d_name, d_name_len); | |
| 184 | |
| 185 offset += d->d_reclen; | |
| 186 nacl_offset += nacl_d->nacl_abi_d_reclen; | |
| 187 } | |
| 188 | |
| 189 *nread = nacl_offset; | |
| 190 return 0; | |
| 191 } | |
| 192 | |
| 193 int isatty(int fd) NOTHROW { | |
| 194 return ki_isatty(fd); | |
| 195 } | |
| 196 | |
| 197 int link(const char* oldpath, const char* newpath) NOTHROW { | |
| 198 return ki_link(oldpath, newpath); | |
| 199 } | |
| 200 | |
| 201 int mkdir(const char* path, mode_t mode) NOTHROW { | |
| 202 return ki_mkdir(path, mode); | |
| 203 } | |
| 204 | |
| 205 int WRAP(mkdir) (const char* pathname, mode_t mode) { | |
| 206 return (ki_mkdir(pathname, mode)) ? errno : 0; | |
| 207 } | |
| 208 | |
| 209 int mount(const char* source, const char* target, const char* filesystemtype, | |
| 210 unsigned long mountflags, const void* data) NOTHROW { | |
| 211 return ki_mount(source, target, filesystemtype, mountflags, data); | |
| 212 } | |
| 213 | |
| 214 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) { | |
| 215 *newfd = ki_open(pathname, oflag); | |
| 216 return (*newfd < 0) ? errno : 0; | |
| 217 } | |
| 218 | |
| 219 int WRAP(read)(int fd, void *buf, size_t count, size_t *nread) { | |
| 220 if (!ki_is_initialized()) | |
| 221 return REAL(read)(fd, buf, count, nread); | |
| 222 | |
| 223 *nread = ki_read(fd, buf, count); | |
| 224 return (*nread < 0) ? errno : 0; | |
| 225 } | |
| 226 | |
| 227 int remove(const char* path) NOTHROW { | |
| 228 return ki_remove(path); | |
| 229 } | |
| 230 | |
| 231 int rmdir(const char* path) NOTHROW { | |
| 232 return ki_rmdir(path); | |
| 233 } | |
| 234 | |
| 235 int WRAP(rmdir)(const char* pathname) { | |
| 236 return (ki_rmdir(pathname) < 0) ? errno : 0; | |
| 237 } | |
| 238 | |
| 239 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) { | |
| 240 *new_offset = ki_lseek(fd, offset, whence); | |
| 241 return (*new_offset < 0) ? errno : 0; | |
| 242 } | |
| 243 | |
| 244 int WRAP(stat)(const char *pathname, struct nacl_abi_stat *nacl_buf) { | |
| 245 struct stat buf; | |
| 246 memset(&buf, 0, sizeof(struct stat)); | |
| 247 int res = ki_stat(pathname, &buf); | |
| 248 if (res < 0) | |
| 249 return errno; | |
| 250 stat_to_nacl_stat(&buf, nacl_buf); | |
| 251 return 0; | |
| 252 } | |
| 253 | |
| 254 int symlink(const char* oldpath, const char* newpath) NOTHROW { | |
| 255 return ki_symlink(oldpath, newpath); | |
| 256 } | |
| 257 | |
| 258 int umount(const char* path) NOTHROW { | |
| 259 return ki_umount(path); | |
| 260 } | |
| 261 | |
| 262 int unlink(const char* path) NOTHROW { | |
| 263 return ki_unlink(path); | |
| 264 } | |
| 265 | |
| 266 int WRAP(write)(int fd, const void *buf, size_t count, size_t *nwrote) { | |
| 267 if (!ki_is_initialized()) | |
| 268 return REAL(write)(fd, buf, count, nwrote); | |
| 269 | |
| 270 *nwrote = ki_write(fd, buf, count); | |
| 271 return (*nwrote < 0) ? errno : 0; | |
| 272 } | |
| 273 | |
| 274 void kernel_wrap_init() { | |
| 275 static bool wrapped = false; | |
| 276 | |
| 277 if (!wrapped) { | |
| 278 wrapped = true; | |
| 279 DO_WRAP(chdir); | |
| 280 DO_WRAP(close); | |
| 281 DO_WRAP(dup); | |
| 282 DO_WRAP(dup2); | |
| 283 DO_WRAP(fstat); | |
| 284 DO_WRAP(getcwd); | |
| 285 DO_WRAP(getdents); | |
| 286 DO_WRAP(mkdir); | |
| 287 DO_WRAP(open); | |
| 288 DO_WRAP(read); | |
| 289 DO_WRAP(rmdir); | |
| 290 DO_WRAP(seek); | |
| 291 DO_WRAP(stat); | |
| 292 DO_WRAP(write); | |
| 293 } | |
| 294 } | |
| 295 | |
| 296 EXTERN_C_END | |
| 297 | |
| 298 | |
| 299 #endif // defined(__native_client__) && defined(__GLIBC__) | |
| OLD | NEW |