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 "nacl_io/kernel_intercept.h"
10 #include "nacl_io/dbgprint.h"
11 #include "nacl_io/kernel_proxy.h"
12 #include "nacl_io/kernel_wrap.h"
13 #include "nacl_io/osmman.h"
14 #include "nacl_io/ossocket.h"
15 #include "nacl_io/pepper_interface.h"
16 #include "nacl_io/pepper_interface.h"
17 #include "nacl_io/real_pepper_interface.h"
19 using namespace nacl_io;
21 #define ON_NOSYS_RETURN(x) \
22 if (!ki_is_initialized()) { \
27 static KernelProxy* s_kp;
28 static bool s_kp_owned;
30 void ki_init(void* kp) {
31 ki_init_ppapi(kp, 0, NULL);
34 void ki_init_ppapi(void* kp,
36 PPB_GetInterface get_browser_interface) {
37 PepperInterface* ppapi = NULL;
38 if (instance && get_browser_interface)
39 ppapi = new RealPepperInterface(instance, get_browser_interface);
40 ki_init_interface(kp, ppapi);
43 void ki_init_interface(void* kp, void* pepper_interface) {
44 PepperInterface* ppapi = static_cast<PepperInterface*>(pepper_interface);
48 s_kp = new KernelProxy();
51 s_kp = static_cast<KernelProxy*>(kp);
58 int ki_register_fs_type(const char* fs_type, struct fuse_operations* fuse_ops) {
59 return s_kp->RegisterFsType(fs_type, fuse_ops);
62 int ki_unregister_fs_type(const char* fs_type) {
63 return s_kp->UnregisterFsType(fs_type);
66 int ki_is_initialized() {
77 int ki_chdir(const char* path) {
79 return s_kp->chdir(path);
82 char* ki_getcwd(char* buf, size_t size) {
83 // gtest uses getcwd in a static initializer. If we haven't initialized the
84 // kernel-intercept yet, just return ".".
85 if (!ki_is_initialized()) {
94 return s_kp->getcwd(buf, size);
97 char* ki_getwd(char* buf) {
98 ON_NOSYS_RETURN(NULL);
99 return s_kp->getwd(buf);
102 int ki_dup(int oldfd) {
104 return s_kp->dup(oldfd);
107 int ki_dup2(int oldfd, int newfd) {
109 return s_kp->dup2(oldfd, newfd);
112 int ki_chmod(const char *path, mode_t mode) {
114 return s_kp->chmod(path, mode);
117 int ki_fchdir(int fd) {
119 return s_kp->fchdir(fd);
122 int ki_fchmod(int fd, mode_t mode) {
124 return s_kp->fchmod(fd, mode);
127 int ki_stat(const char *path, struct stat *buf) {
129 return s_kp->stat(path, buf);
132 int ki_mkdir(const char *path, mode_t mode) {
134 return s_kp->mkdir(path, mode);
137 int ki_rmdir(const char *path) {
139 return s_kp->rmdir(path);
142 int ki_mount(const char *source, const char *target, const char *filesystemtype,
143 unsigned long mountflags, const void *data) {
145 return s_kp->mount(source, target, filesystemtype, mountflags, data);
148 int ki_umount(const char *path) {
150 return s_kp->umount(path);
153 int ki_open(const char *path, int oflag) {
155 return s_kp->open(path, oflag);
158 int ki_pipe(int pipefds[2]) {
160 return s_kp->pipe(pipefds);
163 ssize_t ki_read(int fd, void *buf, size_t nbyte) {
165 return s_kp->read(fd, buf, nbyte);
168 ssize_t ki_write(int fd, const void *buf, size_t nbyte) {
170 return s_kp->write(fd, buf, nbyte);
173 int ki_fstat(int fd, struct stat *buf){
175 return s_kp->fstat(fd, buf);
178 int ki_getdents(int fd, void *buf, unsigned int count) {
180 return s_kp->getdents(fd, buf, count);
183 int ki_ftruncate(int fd, off_t length) {
185 return s_kp->ftruncate(fd, length);
188 int ki_fsync(int fd) {
190 return s_kp->fsync(fd);
193 int ki_fdatasync(int fd) {
195 return s_kp->fdatasync(fd);
198 int ki_isatty(int fd) {
200 return s_kp->isatty(fd);
203 int ki_close(int fd) {
205 return s_kp->close(fd);
208 off_t ki_lseek(int fd, off_t offset, int whence) {
210 return s_kp->lseek(fd, offset, whence);
213 int ki_remove(const char* path) {
215 return s_kp->remove(path);
218 int ki_unlink(const char* path) {
220 return s_kp->unlink(path);
223 int ki_truncate(const char* path, off_t length) {
225 return s_kp->truncate(path, length);
228 int ki_lstat(const char* path, struct stat* buf) {
230 return s_kp->lstat(path, buf);
233 int ki_link(const char* oldpath, const char* newpath) {
235 return s_kp->link(oldpath, newpath);
238 int ki_rename(const char* path, const char* newpath) {
240 return s_kp->rename(path, newpath);
243 int ki_symlink(const char* oldpath, const char* newpath) {
245 return s_kp->symlink(oldpath, newpath);
248 int ki_access(const char* path, int amode) {
250 return s_kp->access(path, amode);
253 int ki_readlink(const char *path, char *buf, size_t count) {
255 return s_kp->readlink(path, buf, count);
258 int ki_utimes(const char *path, const struct timeval times[2]) {
260 return s_kp->utimes(path, times);
263 void* ki_mmap(void* addr, size_t length, int prot, int flags, int fd,
265 ON_NOSYS_RETURN(MAP_FAILED);
266 return s_kp->mmap(addr, length, prot, flags, fd, offset);
269 int ki_munmap(void* addr, size_t length) {
271 return s_kp->munmap(addr, length);
274 int ki_open_resource(const char* file) {
275 ON_NOSYS_RETURN(-1); return s_kp->open_resource(file);
278 int ki_fcntl(int d, int request, va_list args) {
280 return s_kp->fcntl(d, request, args);
283 int ki_ioctl(int d, int request, va_list args) {
285 return s_kp->ioctl(d, request, args);
288 int ki_chown(const char* path, uid_t owner, gid_t group) {
290 return s_kp->chown(path, owner, group);
293 int ki_fchown(int fd, uid_t owner, gid_t group) {
295 return s_kp->fchown(fd, owner, group);
298 int ki_lchown(const char* path, uid_t owner, gid_t group) {
300 return s_kp->lchown(path, owner, group);
303 int ki_utime(const char* filename, const struct utimbuf* times) {
305 return s_kp->utime(filename, times);
308 int ki_poll(struct pollfd *fds, nfds_t nfds, int timeout) {
309 return s_kp->poll(fds, nfds, timeout);
312 int ki_select(int nfds, fd_set* readfds, fd_set* writefds,
313 fd_set* exceptfds, struct timeval* timeout) {
314 return s_kp->select(nfds, readfds, writefds, exceptfds, timeout);
317 int ki_tcflush(int fd, int queue_selector) {
319 return s_kp->tcflush(fd, queue_selector);
322 int ki_tcgetattr(int fd, struct termios* termios_p) {
324 return s_kp->tcgetattr(fd, termios_p);
327 int ki_tcsetattr(int fd, int optional_actions,
328 const struct termios *termios_p) {
330 return s_kp->tcsetattr(fd, optional_actions, termios_p);
333 int ki_kill(pid_t pid, int sig) {
335 return s_kp->kill(pid, sig);
338 int ki_killpg(pid_t pid, int sig) {
343 int ki_sigaction(int signum, const struct sigaction* action,
344 struct sigaction* oaction) {
346 return s_kp->sigaction(signum, action, oaction);
349 int ki_sigpause(int sigmask) {
354 int ki_sigpending(sigset_t* set) {
359 int ki_sigsuspend(const sigset_t* set) {
364 sighandler_t ki_signal(int signum, sighandler_t handler) {
365 return ki_sigset(signum, handler);
368 sighandler_t ki_sigset(int signum, sighandler_t handler) {
369 ON_NOSYS_RETURN(SIG_ERR);
370 // Implement sigset(2) in terms of sigaction(2).
371 struct sigaction action;
372 struct sigaction oaction;
373 memset(&action, 0, sizeof(action));
374 memset(&oaction, 0, sizeof(oaction));
375 action.sa_handler = handler;
376 int rtn = s_kp->sigaction(signum, &action, &oaction);
379 return oaction.sa_handler;
382 #ifdef PROVIDES_SOCKET_API
384 int ki_accept(int fd, struct sockaddr* addr, socklen_t* len) {
386 return s_kp->accept(fd, addr, len);
389 int ki_bind(int fd, const struct sockaddr* addr, socklen_t len) {
391 return s_kp->bind(fd, addr, len);
394 int ki_connect(int fd, const struct sockaddr* addr, socklen_t len) {
396 return s_kp->connect(fd, addr, len);
399 struct hostent* ki_gethostbyname(const char* name) {
400 ON_NOSYS_RETURN(NULL);
401 return s_kp->gethostbyname(name);
404 int ki_getaddrinfo(const char *node, const char *service,
405 const struct addrinfo *hints,
406 struct addrinfo **res) {
407 ON_NOSYS_RETURN(EAI_SYSTEM);
408 return s_kp->getaddrinfo(node, service, hints, res);
411 void ki_freeaddrinfo(struct addrinfo *res) {
412 s_kp->freeaddrinfo(res);
415 int ki_getpeername(int fd, struct sockaddr* addr, socklen_t* len) {
417 return s_kp->getpeername(fd, addr, len);
420 int ki_getsockname(int fd, struct sockaddr* addr, socklen_t* len) {
422 return s_kp->getsockname(fd, addr, len);
425 int ki_getsockopt(int fd, int lvl, int optname, void* optval, socklen_t* len) {
427 return s_kp->getsockopt(fd, lvl, optname, optval, len);
430 int ki_listen(int fd, int backlog) {
432 return s_kp->listen(fd, backlog);
435 ssize_t ki_recv(int fd, void* buf, size_t len, int flags) {
437 return s_kp->recv(fd, buf, len, flags);
440 ssize_t ki_recvfrom(int fd, void* buf, size_t len, int flags,
441 struct sockaddr* addr, socklen_t* addrlen) {
443 return s_kp->recvfrom(fd, buf, len, flags, addr, addrlen);
446 ssize_t ki_recvmsg(int fd, struct msghdr* msg, int flags) {
448 return s_kp->recvmsg(fd, msg, flags);
451 ssize_t ki_send(int fd, const void* buf, size_t len, int flags) {
453 return s_kp->send(fd, buf, len, flags);
456 ssize_t ki_sendto(int fd, const void* buf, size_t len, int flags,
457 const struct sockaddr* addr, socklen_t addrlen) {
459 return s_kp->sendto(fd, buf, len, flags, addr, addrlen);
462 ssize_t ki_sendmsg(int fd, const struct msghdr* msg, int flags) {
464 return s_kp->sendmsg(fd, msg, flags);
467 int ki_setsockopt(int fd, int lvl, int optname, const void* optval,
470 return s_kp->setsockopt(fd, lvl, optname, optval, len);
473 int ki_shutdown(int fd, int how) {
475 return s_kp->shutdown(fd, how);
478 int ki_socket(int domain, int type, int protocol) {
480 return s_kp->socket(domain, type, protocol);
483 int ki_socketpair(int domain, int type, int protocol, int* sv) {
485 return s_kp->socketpair(domain, type, protocol, sv);
487 #endif // PROVIDES_SOCKET_API