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-newlib build. | |
10 #if defined(__native_client__) && !defined(__GLIBC__) | |
11 | |
12 #include "nacl_mounts/kernel_wrap.h" | |
13 #include <dirent.h> | |
14 #include <errno.h> | |
15 #include <irt.h> | |
16 #include <sys/stat.h> | |
17 #include "nacl_mounts/kernel_intercept.h" | |
18 | |
19 EXTERN_C_BEGIN | |
20 | |
21 #define REAL(name) __nacl_irt_##name##_real | |
22 #define WRAP(name) __nacl_irt_##name##_wrap | |
23 #define STRUCT_NAME(group) __libnacl_irt_##group | |
24 #define DECLARE_STRUCT(group) \ | |
25 extern struct nacl_irt_##group STRUCT_NAME(group); | |
26 #define MUX(group, name) STRUCT_NAME(group).name | |
27 #define DECLARE(group, name) typeof(MUX(group, name)) REAL(name); | |
28 #define DO_WRAP(group, name) do { \ | |
29 REAL(name) = MUX(group, name); \ | |
30 MUX(group, name) = (typeof(REAL(name))) WRAP(name); \ | |
31 } while (0) | |
32 | |
33 DECLARE_STRUCT(fdio) | |
34 DECLARE_STRUCT(filename) | |
35 | |
36 DECLARE(fdio, close) | |
37 DECLARE(fdio, dup) | |
38 DECLARE(fdio, dup2) | |
39 DECLARE(fdio, fstat) | |
40 DECLARE(fdio, getdents) | |
41 DECLARE(fdio, read) | |
42 DECLARE(fdio, seek) | |
43 DECLARE(fdio, write) | |
44 DECLARE(filename, open) | |
45 DECLARE(filename, stat) | |
46 | |
47 | |
48 int access(const char* path, int amode) { | |
49 return ki_access(path, amode); | |
50 } | |
51 | |
52 int chdir(const char* path) { | |
53 return ki_chdir(path); | |
54 } | |
55 | |
56 int chmod(const char* path, mode_t mode) { | |
57 return ki_chmod(path, mode); | |
58 } | |
59 | |
60 int WRAP(close)(int fd) { | |
61 return (ki_close(fd) < 0) ? errno : 0; | |
62 } | |
63 | |
64 int WRAP(dup)(int fd, int* newfd) { | |
65 *newfd = ki_dup(fd); | |
66 return (*newfd < 0) ? errno : 0; | |
67 } | |
68 | |
69 int WRAP(dup2)(int fd, int newfd) { | |
70 return ki_dup2(fd, newfd); | |
71 } | |
72 | |
73 int WRAP(fstat)(int fd, struct stat *buf) { | |
74 return (ki_fstat(fd, buf) < 0) ? errno : 0; | |
75 } | |
76 | |
77 int fsync(int fd) { | |
78 return ki_fsync(fd); | |
79 } | |
80 | |
81 char* getcwd(char* buf, size_t size) { | |
82 return ki_getcwd(buf, size); | |
83 } | |
84 | |
85 char* getwd(char* buf) { | |
86 return ki_getwd(buf); | |
87 } | |
88 | |
89 int getdents(int fd, void* buf, unsigned int count) { | |
90 return ki_getdents(fd, buf, count); | |
91 } | |
92 | |
93 int WRAP(getdents)(int fd, dirent* buf, size_t count, size_t *nread) { | |
94 return (ki_getdents(fd, buf, count) < 0) ? errno : 0; | |
95 } | |
96 | |
97 int isatty(int fd) { | |
98 return ki_isatty(fd); | |
99 } | |
100 | |
101 int link(const char* oldpath, const char* newpath) { | |
102 return ki_link(oldpath, newpath); | |
103 } | |
104 | |
105 int mkdir(const char* path, mode_t mode) { | |
106 return ki_mkdir(path, mode); | |
107 } | |
108 | |
109 int mount(const char* source, const char* target, const char* filesystemtype, | |
110 unsigned long mountflags, const void* data) { | |
111 return ki_mount(source, target, filesystemtype, mountflags, data); | |
112 } | |
113 | |
114 int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) { | |
115 *newfd = ki_open(pathname, oflag); | |
116 return (*newfd < 0) ? errno : 0; | |
117 } | |
118 | |
119 int WRAP(read)(int fd, void *buf, size_t count, size_t *nread) { | |
120 if (!ki_is_initialized()) | |
121 return REAL(read)(fd, buf, count, nread); | |
122 | |
123 ssize_t signed_nread = ki_read(fd, buf, count); | |
124 *nread = static_cast<size_t>(signed_nread); | |
125 return (signed_nread < 0) ? errno : 0; | |
126 } | |
127 | |
128 int remove(const char* path) { | |
129 return ki_remove(path); | |
130 } | |
131 | |
132 int rmdir(const char* path) { | |
133 return ki_rmdir(path); | |
134 } | |
135 | |
136 int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) { | |
137 *new_offset = ki_lseek(fd, offset, whence); | |
138 return (*new_offset < 0) ? errno : 0; | |
139 } | |
140 | |
141 int WRAP(stat)(const char *pathname, struct stat *buf) { | |
142 return (ki_stat(pathname, buf) < 0) ? errno : 0; | |
143 } | |
144 | |
145 int symlink(const char* oldpath, const char* newpath) { | |
146 return ki_symlink(oldpath, newpath); | |
147 } | |
148 | |
149 int umount(const char* path) { | |
150 return ki_umount(path); | |
151 } | |
152 | |
153 int unlink(const char* path) { | |
154 return ki_unlink(path); | |
155 } | |
156 | |
157 int WRAP(write)(int fd, const void *buf, size_t count, size_t *nwrote) { | |
158 if (!ki_is_initialized()) | |
159 return REAL(write)(fd, buf, count, nwrote); | |
160 | |
161 ssize_t signed_nwrote = ki_write(fd, buf, count); | |
162 *nwrote = static_cast<size_t>(signed_nwrote); | |
163 return (signed_nwrote < 0) ? errno : 0; | |
164 } | |
165 | |
166 | |
167 void kernel_wrap_init() { | |
168 static bool wrapped = false; | |
169 | |
170 if (!wrapped) { | |
171 wrapped = true; | |
172 DO_WRAP(fdio, close); | |
173 DO_WRAP(fdio, dup); | |
174 DO_WRAP(fdio, dup2); | |
175 DO_WRAP(fdio, fstat); | |
176 DO_WRAP(fdio, getdents); | |
177 DO_WRAP(fdio, read); | |
178 DO_WRAP(fdio, seek); | |
179 DO_WRAP(fdio, write); | |
180 DO_WRAP(filename, open); | |
181 DO_WRAP(filename, stat); | |
182 } | |
183 } | |
184 | |
185 | |
186 EXTERN_C_END | |
187 | |
188 | |
189 #endif // defined(__native_client__) && !defined(__GLIBC__) | |
OLD | NEW |