blob: e21830161d1bc92b5a1519107ed628f2e6740bc3 [file] [log] [blame]
Ben Murdochca12bfa2013-07-23 11:17:05 +01001// 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.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00004
5#include <sys/types.h> // Include something that will define __GLIBC__.
6
7// The entire file is wrapped in this #if. We do this so this .cc file can be
8// compiled, even on a non-newlib build.
9#if defined(__native_client__) && !defined(__GLIBC__)
10
11#include "nacl_io/kernel_wrap.h"
12#include <dirent.h>
13#include <errno.h>
14#include <irt.h>
15#include <sys/mman.h>
16#include <sys/stat.h>
Ben Murdochca12bfa2013-07-23 11:17:05 +010017#include <sys/time.h>
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000018#include "nacl_io/kernel_intercept.h"
19
20EXTERN_C_BEGIN
21
Ben Murdoch58e6fbe2013-07-26 10:20:38 +010022// Macro to get the REAL function pointer
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000023#define REAL(name) __nacl_irt_##name##_real
Ben Murdoch58e6fbe2013-07-26 10:20:38 +010024
25// Macro to get the WRAP function
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000026#define WRAP(name) __nacl_irt_##name##_wrap
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000027
Ben Murdoch58e6fbe2013-07-26 10:20:38 +010028// Declare REAL function pointer and assign it the REAL function.
29#define DECLARE_REAL_PTR(group, name) \
30 typeof(__libnacl_irt_##group.name) REAL(name) = __libnacl_irt_##group.name;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000031
Ben Murdoch58e6fbe2013-07-26 10:20:38 +010032// Switch IRT's pointer to the REAL pointer
33#define USE_REAL(group, name) \
34 __libnacl_irt_##group.name = (typeof(REAL(name))) REAL(name); \
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000035
Ben Murdoch58e6fbe2013-07-26 10:20:38 +010036// Switch the IRT's pointer to the WRAP function
37#define USE_WRAP(group, name) \
38 __libnacl_irt_##group.name = (typeof(REAL(name))) WRAP(name); \
39
40
41
42extern struct nacl_irt_fdio __libnacl_irt_fdio;
43extern struct nacl_irt_filename __libnacl_irt_filename;
44extern struct nacl_irt_memory __libnacl_irt_memory;
45
46// Create function pointers to the REAL implementation
47#define EXPAND_SYMBOL_LIST_OPERATION(OP) \
48 OP(fdio, close); \
49 OP(fdio, dup); \
50 OP(fdio, dup2); \
51 OP(fdio, fstat); \
52 OP(fdio, getdents); \
53 OP(fdio, read); \
54 OP(fdio, seek); \
55 OP(fdio, write); \
56 OP(filename, open); \
57 OP(filename, stat); \
58 OP(memory, mmap); \
59 OP(memory, munmap);
60
61
62EXPAND_SYMBOL_LIST_OPERATION(DECLARE_REAL_PTR);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000063
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000064int WRAP(close)(int fd) {
65 return (ki_close(fd) < 0) ? errno : 0;
66}
67
68int WRAP(dup)(int fd, int* newfd) {
69 *newfd = ki_dup(fd);
70 return (*newfd < 0) ? errno : 0;
71}
72
73int WRAP(dup2)(int fd, int newfd) {
74 newfd = ki_dup2(fd, newfd);
75 return (newfd < 0) ? errno : 0;
76}
77
Ben Murdocheb525c52013-07-10 11:40:50 +010078int WRAP(fstat)(int fd, struct stat* buf) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000079 return (ki_fstat(fd, buf) < 0) ? errno : 0;
80}
81
Ben Murdocheb525c52013-07-10 11:40:50 +010082int WRAP(getdents)(int fd, dirent* buf, size_t count, size_t* nread) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000083 return (ki_getdents(fd, buf, count) < 0) ? errno : 0;
84}
85
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000086int WRAP(mmap)(void** addr, size_t length, int prot, int flags, int fd,
87 off_t offset) {
88 if (flags & MAP_ANONYMOUS)
89 return REAL(mmap)(addr, length, prot, flags, fd, offset);
90
91 *addr = ki_mmap(*addr, length, prot, flags, fd, offset);
92 return *addr == (void*)-1 ? errno : 0;
93}
94
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000095int WRAP(munmap)(void* addr, size_t length) {
96 // Always let the real munmap run on the address range. It is not an error if
97 // there are no mapped pages in that range.
98 ki_munmap(addr, length);
99 return REAL(munmap)(addr, length);
100}
101
102int WRAP(open)(const char* pathname, int oflag, mode_t cmode, int* newfd) {
103 *newfd = ki_open(pathname, oflag);
104 return (*newfd < 0) ? errno : 0;
105}
106
Ben Murdocheb525c52013-07-10 11:40:50 +0100107int WRAP(read)(int fd, void* buf, size_t count, size_t* nread) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000108 if (!ki_is_initialized())
109 return REAL(read)(fd, buf, count, nread);
110
111 ssize_t signed_nread = ki_read(fd, buf, count);
112 *nread = static_cast<size_t>(signed_nread);
113 return (signed_nread < 0) ? errno : 0;
114}
115
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000116int WRAP(seek)(int fd, off_t offset, int whence, off_t* new_offset) {
117 *new_offset = ki_lseek(fd, offset, whence);
118 return (*new_offset < 0) ? errno : 0;
119}
120
Ben Murdoch32409262013-08-07 11:04:47 +0100121int WRAP(stat)(const char* pathname, struct stat* buf) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000122 return (ki_stat(pathname, buf) < 0) ? errno : 0;
123}
124
Ben Murdoch32409262013-08-07 11:04:47 +0100125int WRAP(write)(int fd, const void* buf, size_t count, size_t* nwrote) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000126 if (!ki_is_initialized())
127 return REAL(write)(fd, buf, count, nwrote);
128
129 ssize_t signed_nwrote = ki_write(fd, buf, count);
130 *nwrote = static_cast<size_t>(signed_nwrote);
131 return (signed_nwrote < 0) ? errno : 0;
132}
133
Ben Murdoch32409262013-08-07 11:04:47 +0100134// Socket functions
135int accept(int fd, struct sockaddr* addr, socklen_t* len) {
136 return ki_accept(fd, addr, len);
137}
138
139int bind(int fd, const struct sockaddr* addr, socklen_t len) {
140 return ki_bind(fd, addr, len);
141}
142
143int connect(int fd, const struct sockaddr* addr, socklen_t len) {
144 return ki_connect(fd, addr, len);
145}
146
Ben Murdochbb1529c2013-08-08 10:24:53 +0100147struct hostent* gethostbyname(const char* name) {
148 return ki_gethostbyname(name);
149}
150
Ben Murdoch32409262013-08-07 11:04:47 +0100151int getpeername(int fd, struct sockaddr* addr, socklen_t* len) {
152 return ki_getpeername(fd, addr, len);
153}
154
155int getsockname(int fd, struct sockaddr* addr, socklen_t* len) {
156 return ki_getsockname(fd, addr, len);
157}
Ben Murdochbb1529c2013-08-08 10:24:53 +0100158
Ben Murdoch32409262013-08-07 11:04:47 +0100159int getsockopt(int fd, int lvl, int optname, void* optval, socklen_t* len) {
160 return ki_getsockopt(fd, lvl, optname, optval, len);
161}
162
Ben Murdochbb1529c2013-08-08 10:24:53 +0100163void herror(const char* s) {
164 return ki_herror(s);
165}
166
167const char* hstrerror(int err) {
168 return ki_hstrerror(err);
169}
170
Ben Murdoch32409262013-08-07 11:04:47 +0100171int listen(int fd, int backlog) {
172 return ki_listen(fd, backlog);
173}
174
175ssize_t recv(int fd, void* buf, size_t len, int flags) {
176 return ki_recv(fd, buf, len, flags);
177}
178
179ssize_t recvfrom(int fd, void* buf, size_t len, int flags,
180 struct sockaddr* addr, socklen_t* addrlen) {
181 return ki_recvfrom(fd, buf, len, flags, addr, addrlen);
182}
183
184ssize_t recvmsg(int fd, struct msghdr* msg, int flags) {
185 return ki_recvmsg(fd, msg, flags);
186}
187
188ssize_t send(int fd, const void* buf, size_t len, int flags) {
189 return ki_send(fd, buf, len, flags);
190}
191
192ssize_t sendto(int fd, const void* buf, size_t len, int flags,
193 const struct sockaddr* addr, socklen_t addrlen) {
194 return ki_sendto(fd, buf, len, flags, addr, addrlen);
195}
196
197ssize_t sendmsg(int fd, const struct msghdr* msg, int flags) {
198 return ki_sendmsg(fd, msg, flags);
199}
200
201int setsockopt(int fd, int lvl, int optname, const void* optval,
202 socklen_t len) {
203 return ki_setsockopt(fd, lvl, optname, optval, len);
204}
205
206int shutdown(int fd, int how) {
207 return ki_shutdown(fd, how);
208}
209
210int socket(int domain, int type, int protocol) {
211 return ki_socket(domain, type, protocol);
212}
213
214int socketpair(int domain, int type, int protocol, int* sv) {
215 return ki_socketpair(domain, type, protocol, sv);
216}
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000217
218// "real" functions, i.e. the unwrapped original functions.
219
220int _real_close(int fd) {
221 return REAL(close)(fd);
222}
223
Ben Murdoch32409262013-08-07 11:04:47 +0100224int _real_fstat(int fd, struct stat* buf) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000225 return REAL(fstat)(fd, buf);
226}
227
Ben Murdoch32409262013-08-07 11:04:47 +0100228int _real_getdents(int fd, dirent* nacl_buf, size_t nacl_count, size_t* nread) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000229 return REAL(getdents)(fd, nacl_buf, nacl_count, nread);
230}
231
232int _real_lseek(int fd, off_t offset, int whence, off_t* new_offset) {
233 return REAL(seek)(fd, offset, whence, new_offset);
234}
235
236int _real_mkdir(const char* pathname, mode_t mode) {
237 return ENOSYS;
238}
239
240int _real_mmap(void** addr, size_t length, int prot, int flags, int fd,
241 off_t offset) {
242 return REAL(mmap)(addr, length, prot, flags, fd, offset);
243}
244
245int _real_munmap(void* addr, size_t length) {
246 return REAL(munmap)(addr, length);
247}
248
249int _real_open(const char* pathname, int oflag, mode_t cmode, int* newfd) {
250 return REAL(open)(pathname, oflag, cmode, newfd);
251}
252
253int _real_open_resource(const char* file, int* fd) {
254 return ENOSYS;
255}
256
Ben Murdoch32409262013-08-07 11:04:47 +0100257int _real_read(int fd, void* buf, size_t count, size_t* nread) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000258 return REAL(read)(fd, buf, count, nread);
259}
260
261int _real_rmdir(const char* pathname) {
262 return ENOSYS;
263}
264
Ben Murdoch32409262013-08-07 11:04:47 +0100265int _real_write(int fd, const void* buf, size_t count, size_t* nwrote) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000266 return REAL(write)(fd, buf, count, nwrote);
267}
268
Ben Murdochca12bfa2013-07-23 11:17:05 +0100269uint64_t usec_since_epoch() {
270 struct timeval tv;
271 gettimeofday(&tv, NULL);
272 return tv.tv_usec + (tv.tv_sec * 1000000);
273}
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000274
Ben Murdoch58e6fbe2013-07-26 10:20:38 +0100275static bool s_wrapped = false;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000276void kernel_wrap_init() {
Ben Murdoch58e6fbe2013-07-26 10:20:38 +0100277 if (!s_wrapped) {
278 EXPAND_SYMBOL_LIST_OPERATION(USE_WRAP);
279 s_wrapped = true;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000280 }
281}
282
Ben Murdoch58e6fbe2013-07-26 10:20:38 +0100283void kernel_wrap_uninit() {
284 if (s_wrapped) {
285 EXPAND_SYMBOL_LIST_OPERATION(USE_REAL);
286 s_wrapped = false;
287 }
288}
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000289
290EXTERN_C_END
291
292
293#endif // defined(__native_client__) && !defined(__GLIBC__)
Ben Murdocheb525c52013-07-10 11:40:50 +0100294