ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / native_client_sdk / src / libraries / nacl_io / kernel_wrap_bionic.cc
blob4d9f6fc7b7221a840ca940b5e507d3c782648a2c
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.
5 #include <sys/types.h> // Include something that will define __BIONIC__.
7 #include "nacl_io/kernel_wrap.h" // IRT_EXT is turned on in this header.
9 // The entire file is wrapped in this #if. We do this so this .cc file can be
10 // compiled, even on a non-bionic build.
12 #if !defined(NACL_IO_IRT_EXT) && defined(__native_client__) && \
13 defined(__BIONIC__)
15 #include <alloca.h>
16 #include <assert.h>
17 #include <dirent.h>
18 #include <errno.h>
19 #include <irt_syscalls.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
24 #include "nacl_io/kernel_intercept.h"
25 #include "nacl_io/kernel_wrap_real.h"
26 #include "nacl_io/osmman.h"
28 namespace {
30 void stat_to_nacl_stat(const struct stat* buf, nacl_abi_stat* nacl_buf) {
31 memset(nacl_buf, 0, sizeof(struct nacl_abi_stat));
32 nacl_buf->nacl_abi_st_dev = buf->st_dev;
33 nacl_buf->nacl_abi_st_ino = buf->st_ino;
34 nacl_buf->nacl_abi_st_mode = buf->st_mode;
35 nacl_buf->nacl_abi_st_nlink = buf->st_nlink;
36 nacl_buf->nacl_abi_st_uid = buf->st_uid;
37 nacl_buf->nacl_abi_st_gid = buf->st_gid;
38 nacl_buf->nacl_abi_st_rdev = buf->st_rdev;
39 nacl_buf->nacl_abi_st_size = buf->st_size;
40 nacl_buf->nacl_abi_st_blksize = buf->st_blksize;
41 nacl_buf->nacl_abi_st_blocks = buf->st_blocks;
42 nacl_buf->nacl_abi_st_atime = buf->st_atime;
43 nacl_buf->nacl_abi_st_mtime = buf->st_mtime;
44 nacl_buf->nacl_abi_st_ctime = buf->st_ctime;
47 void nacl_stat_to_stat(const nacl_abi_stat* nacl_buf, struct stat* buf) {
48 memset(buf, 0, sizeof(struct stat));
49 buf->st_dev = nacl_buf->nacl_abi_st_dev;
50 buf->st_ino = nacl_buf->nacl_abi_st_ino;
51 buf->st_mode = nacl_buf->nacl_abi_st_mode;
52 buf->st_nlink = nacl_buf->nacl_abi_st_nlink;
53 buf->st_uid = nacl_buf->nacl_abi_st_uid;
54 buf->st_gid = nacl_buf->nacl_abi_st_gid;
55 buf->st_rdev = nacl_buf->nacl_abi_st_rdev;
56 buf->st_size = nacl_buf->nacl_abi_st_size;
57 buf->st_blksize = nacl_buf->nacl_abi_st_blksize;
58 buf->st_blocks = nacl_buf->nacl_abi_st_blocks;
59 buf->st_atime = nacl_buf->nacl_abi_st_atime;
60 buf->st_mtime = nacl_buf->nacl_abi_st_mtime;
61 buf->st_ctime = nacl_buf->nacl_abi_st_ctime;
64 } // namespace
66 // From native_client/src/trusted/service_runtime/include/sys/dirent.h
68 #ifndef nacl_abi___ino_t_defined
69 #define nacl_abi___ino_t_defined
70 typedef int64_t nacl_abi___ino_t;
71 typedef nacl_abi___ino_t nacl_abi_ino_t;
72 #endif
74 #ifndef nacl_abi___off_t_defined
75 #define nacl_abi___off_t_defined
76 typedef int64_t nacl_abi__off_t;
77 typedef nacl_abi__off_t nacl_abi_off_t;
78 #endif
80 /* We need a way to define the maximum size of a name. */
81 #ifndef MAXNAMLEN
82 # ifdef NAME_MAX
83 # define MAXNAMLEN NAME_MAX
84 # else
85 # define MAXNAMLEN 255
86 # endif
87 #endif
89 struct nacl_abi_dirent {
90 nacl_abi_ino_t nacl_abi_d_ino;
91 nacl_abi_off_t nacl_abi_d_off;
92 uint16_t nacl_abi_d_reclen;
93 char nacl_abi_d_name[MAXNAMLEN + 1];
96 static const int d_name_shift = offsetof (dirent, d_name) -
97 offsetof (struct nacl_abi_dirent, nacl_abi_d_name);
99 EXTERN_C_BEGIN
101 // Macro to get the REAL function pointer
102 #define REAL(name) __nacl_irt_##name##_real
104 // Macro to get the WRAP function
105 #define WRAP(name) __nacl_irt_##name##_wrap
107 // Declare REAL function pointer.
108 #define DECLARE_REAL_PTR(name) typeof(__nacl_irt_##name) REAL(name);
110 // Assign the REAL function pointer.
111 #define ASSIGN_REAL_PTR(name) REAL(name) = __nacl_irt_##name;
113 // Switch IRT's pointer to the REAL pointer
114 #define USE_REAL(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))REAL(name)
116 // Switch IRT's pointer to the WRAP function
117 #define USE_WRAP(name) __nacl_irt_##name = (typeof(__nacl_irt_##name))WRAP(name)
119 #define EXPAND_SYMBOL_LIST_OPERATION(OP) \
120 OP(chdir); \
121 OP(close); \
122 OP(dup); \
123 OP(dup2); \
124 OP(exit); \
125 OP(fchdir); \
126 OP(fchmod); \
127 OP(fdatasync); \
128 OP(fstat); \
129 OP(fsync); \
130 OP(getcwd); \
131 OP(getdents); \
132 OP(isatty); \
133 OP(lstat); \
134 OP(mkdir); \
135 OP(mmap); \
136 OP(munmap); \
137 OP(open); \
138 OP(open_resource); \
139 OP(poll); \
140 OP(read); \
141 OP(readlink); \
142 OP(rmdir); \
143 OP(seek); \
144 OP(stat); \
145 OP(truncate); \
146 OP(write);
148 EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR);
150 int WRAP(chdir)(const char* pathname) {
151 ERRNO_RTN(ki_chdir(pathname));
154 int WRAP(close)(int fd) {
155 ERRNO_RTN(ki_close(fd));
158 int WRAP(dup)(int fd, int* newfd) NOTHROW {
159 *newfd = ki_dup(fd);
160 ERRNO_RTN(*newfd);
163 int WRAP(dup2)(int fd, int newfd) NOTHROW {
164 ERRNO_RTN(ki_dup2(fd, newfd));
167 void WRAP(exit)(int status) {
168 ki_exit(status);
171 int WRAP(fchdir)(int fd) NOTHROW {
172 ERRNO_RTN(ki_fchdir(fd));
175 int WRAP(fchmod)(int fd, mode_t mode) NOTHROW {
176 ERRNO_RTN(ki_fchmod(fd, mode));
179 int WRAP(fdatasync)(int fd) NOTHROW {
180 ERRNO_RTN(ki_fdatasync(fd));
183 int WRAP(fstat)(int fd, struct nacl_abi_stat* nacl_buf) {
184 struct stat buf;
185 memset(&buf, 0, sizeof(struct stat));
186 int res = ki_fstat(fd, &buf);
187 RTN_ERRNO_IF(res < 0);
188 stat_to_nacl_stat(&buf, nacl_buf);
189 return 0;
192 int WRAP(fsync)(int fd) NOTHROW {
193 ERRNO_RTN(ki_fsync(fd));
196 int WRAP(getcwd)(char* buf, size_t size) {
197 RTN_ERRNO_IF(ki_getcwd(buf, size) == NULL);
198 return 0;
201 int WRAP(getdents)(int fd, dirent* nacl_buf, size_t nacl_count, size_t* nread) {
202 int nacl_offset = 0;
203 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
204 // nacl_abi_dirent(s) are smaller than dirent(s), so nacl_count bytes buffer
205 // is enough
206 char* buf = (char*)alloca(nacl_count);
207 int offset = 0;
208 int count;
210 count = ki_getdents(fd, buf, nacl_count);
211 RTN_ERRNO_IF(count < 0);
213 while (offset < count) {
214 dirent* d = (dirent*)(buf + offset);
215 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)((char*)nacl_buf + nacl_offset);
216 nacl_d->nacl_abi_d_ino = d->d_ino;
217 nacl_d->nacl_abi_d_off = d->d_off;
218 nacl_d->nacl_abi_d_reclen = d->d_reclen - d_name_shift;
219 size_t d_name_len = d->d_reclen - offsetof(dirent, d_name);
220 memcpy(nacl_d->nacl_abi_d_name, d->d_name, d_name_len);
222 offset += d->d_reclen;
223 nacl_offset += nacl_d->nacl_abi_d_reclen;
226 *nread = nacl_offset;
227 return 0;
230 int WRAP(isatty)(int fd, int* result) {
231 *result = ki_isatty(fd);
232 RTN_ERRNO_IF(*result == 0);
233 return 0;
236 int WRAP(lstat)(const char* path, struct nacl_abi_stat* nacl_buf) {
237 struct stat buf;
238 memset(&buf, 0, sizeof(struct stat));
239 int res = ki_lstat(path, &buf);
240 RTN_ERRNO_IF(res < 0);
241 stat_to_nacl_stat(&buf, nacl_buf);
242 return 0;
245 int WRAP(mkdir)(const char* pathname, mode_t mode) {
246 ERRNO_RTN(ki_mkdir(pathname, mode));
249 int WRAP(mmap)(void** addr,
250 size_t length,
251 int prot,
252 int flags,
253 int fd,
254 int64_t offset) {
255 if (flags & MAP_ANONYMOUS)
256 return REAL(mmap)(addr, length, prot, flags, fd, offset);
258 *addr = ki_mmap(*addr, length, prot, flags, fd, offset);
259 RTN_ERRNO_IF(*addr == (void*)-1)
260 return 0;
263 int WRAP(munmap)(void* addr, size_t length) {
264 // Always let the real munmap run on the address range. It is not an error if
265 // there are no mapped pages in that range.
266 ki_munmap(addr, length);
267 return REAL(munmap)(addr, length);
270 int WRAP(open)(const char* pathname, int oflag, mode_t mode, int* newfd) {
271 *newfd = ki_open(pathname, oflag, mode);
272 ERRNO_RTN(*newfd);
275 int WRAP(open_resource)(const char* file, int* fd) {
276 *fd = ki_open_resource(file);
277 ERRNO_RTN(*fd);
280 int WRAP(poll)(struct pollfd* fds, nfds_t nfds, int timeout, int* count) {
281 *count = ki_poll(fds, nfds, timeout);
282 ERRNO_RTN(*count);
285 int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) {
286 ssize_t signed_nread = ki_read(fd, buf, count);
287 *nread = static_cast<size_t>(signed_nread);
288 ERRNO_RTN(signed_nread);
291 int WRAP(readlink)(const char* path, char* buf, size_t count, size_t* nread) {
292 ssize_t signed_nread = ki_readlink(path, buf, count);
293 *nread = static_cast<size_t>(signed_nread);
294 ERRNO_RTN(signed_nread);
297 int WRAP(rmdir)(const char* pathname) {
298 ERRNO_RTN(ki_rmdir(pathname));
301 int WRAP(seek)(int fd, off64_t offset, int whence, int64_t* new_offset) {
302 *new_offset = ki_lseek(fd, offset, whence);
303 ERRNO_RTN(*new_offset);
306 int WRAP(select)(int nfds,
307 fd_set* readfds,
308 fd_set* writefds,
309 fd_set* exceptfds,
310 struct timeval* timeout,
311 int* count) {
312 *count = ki_select(nfds, readfds, writefds, exceptfds, timeout);
313 ERRNO_RTN(*count);
316 int WRAP(stat)(const char* pathname, struct nacl_abi_stat* nacl_buf) {
317 struct stat buf;
318 memset(&buf, 0, sizeof(struct stat));
319 int res = ki_stat(pathname, &buf);
320 RTN_ERRNO_IF(res < 0);
321 stat_to_nacl_stat(&buf, nacl_buf);
322 return 0;
325 int WRAP(truncate)(const char* name, int64_t len) {
326 ERRNO_RTN(ki_truncate(name, len));
329 int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) {
330 ssize_t signed_nwrote = ki_write(fd, buf, count);
331 *nwrote = static_cast<size_t>(signed_nwrote);
332 ERRNO_RTN(signed_nwrote);
335 static void assign_real_pointers() {
336 static bool assigned = false;
337 if (!assigned) {
338 EXPAND_SYMBOL_LIST_OPERATION(ASSIGN_REAL_PTR)
339 assigned = true;
343 #define CHECK_REAL(func) \
344 if (!REAL(func)) { \
345 assign_real_pointers(); \
346 if (!REAL(func)) \
347 return ENOSYS; \
350 // "real" functions, i.e. the unwrapped original functions.
352 int _real_close(int fd) {
353 CHECK_REAL(close);
354 return REAL(close)(fd);
357 void _real_exit(int status) {
358 REAL(exit)(status);
361 int _real_fchdir(int fd) {
362 CHECK_REAL(fchdir);
363 return REAL(fchdir)(fd);
366 int _real_fchmod(int fd, mode_t mode) {
367 CHECK_REAL(fchmod);
368 return REAL(fchmod)(fd, mode);
371 int _real_fdatasync(int fd) {
372 CHECK_REAL(fdatasync);
373 return REAL(fdatasync)(fd);
376 int _real_fstat(int fd, struct stat* buf) {
377 struct nacl_abi_stat st;
378 CHECK_REAL(fstat);
380 int err = REAL(fstat)(fd, (struct stat*)&st);
381 if (err) {
382 errno = err;
383 return -1;
386 nacl_stat_to_stat(&st, buf);
387 return 0;
390 int _real_fsync(int fd) {
391 CHECK_REAL(fsync);
392 return REAL(fsync)(fd);
395 int _real_getdents(int fd, void* buf, size_t count, size_t* nread) {
396 // "buf" contains dirent(s); "nacl_buf" contains nacl_abi_dirent(s).
397 // See WRAP(getdents) above.
398 char* nacl_buf = (char*)alloca(count);
399 size_t offset = 0;
400 size_t nacl_offset = 0;
401 size_t nacl_nread;
402 CHECK_REAL(getdents);
403 int err = REAL(getdents)(fd, (dirent*)nacl_buf, count, &nacl_nread);
404 if (err)
405 return err;
407 while (nacl_offset < nacl_nread) {
408 dirent* d = (dirent*)((char*)buf + offset);
409 nacl_abi_dirent* nacl_d = (nacl_abi_dirent*)(nacl_buf + nacl_offset);
410 d->d_ino = nacl_d->nacl_abi_d_ino;
411 d->d_off = nacl_d->nacl_abi_d_off;
412 d->d_reclen = nacl_d->nacl_abi_d_reclen + d_name_shift;
413 size_t d_name_len =
414 nacl_d->nacl_abi_d_reclen - offsetof(nacl_abi_dirent, nacl_abi_d_name);
415 memcpy(d->d_name, nacl_d->nacl_abi_d_name, d_name_len);
417 offset += d->d_reclen;
418 offset += nacl_d->nacl_abi_d_reclen;
421 *nread = offset;
422 return 0;
425 int _real_isatty(int fd, int* result) {
426 *result = isatty(fd);
427 return *result ? 0 : -1;
430 int _real_lseek(int fd, int64_t offset, int whence, int64_t* new_offset) {
431 CHECK_REAL(seek);
432 nacl_abi_off_t nacl_new_offs;
433 int ret = REAL(seek)(fd, offset, whence, &nacl_new_offs);
434 *new_offset = static_cast<off_t>(nacl_new_offs);
435 return ret;
438 int _real_lstat(const char* path, struct stat* buf) {
439 struct nacl_abi_stat st;
440 CHECK_REAL(lstat);
442 int err = REAL(lstat)(path, (struct stat*)&st);
443 if (err) {
444 errno = err;
445 return -1;
448 nacl_stat_to_stat(&st, buf);
449 return 0;
452 int _real_mkdir(const char* pathname, mode_t mode) {
453 CHECK_REAL(mkdir);
454 return REAL(mkdir)(pathname, mode);
457 int _real_mmap(void** addr,
458 size_t length,
459 int prot,
460 int flags,
461 int fd,
462 int64_t offset) {
463 CHECK_REAL(mmap);
464 return REAL(mmap)(addr, length, prot, flags, fd, offset);
467 int _real_munmap(void* addr, size_t length) {
468 CHECK_REAL(munmap);
469 return REAL(munmap)(addr, length);
472 int _real_open(const char* pathname, int oflag, mode_t mode, int* newfd) {
473 CHECK_REAL(open);
474 return REAL(open)(pathname, oflag, mode, newfd);
477 int _real_open_resource(const char* file, int* fd) {
478 CHECK_REAL(open_resource);
479 return REAL(open_resource)(file, fd);
482 int _real_read(int fd, void* buf, size_t count, size_t* nread) {
483 CHECK_REAL(read);
484 return REAL(read)(fd, buf, count, nread);
487 int _real_readlink(const char* path, char* buf, size_t count, size_t* nread) {
488 CHECK_REAL(readlink);
489 return REAL(readlink)(path, buf, count, nread);
492 int _real_rmdir(const char* pathname) {
493 CHECK_REAL(rmdir);
494 return REAL(rmdir)(pathname);
497 int _real_truncate(const char* pathname, int64_t len) {
498 CHECK_REAL(truncate);
499 return REAL(truncate)(pathname, len);
502 int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) {
503 CHECK_REAL(write);
504 return REAL(write)(fd, buf, count, nwrote);
507 int _real_getcwd(char* pathname, size_t len) {
508 CHECK_REAL(getcwd);
509 return REAL(getcwd)(pathname, len);
512 static bool s_wrapped = false;
514 void kernel_wrap_init() {
515 if (!s_wrapped) {
516 assign_real_pointers();
517 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP)
518 s_wrapped = true;
522 void kernel_wrap_uninit() {
523 if (s_wrapped) {
524 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL)
525 s_wrapped = false;
529 EXTERN_C_END
531 #endif // defined(__native_client__) && defined(__BIONIC__)