kernel_intercept.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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#include <errno.h>
6#include "nacl_io/kernel_intercept.h"
7#include "nacl_io/kernel_proxy.h"
8#include "nacl_io/kernel_wrap.h"
9#include "nacl_io/pepper_interface.h"
10#include "nacl_io/pepper_interface.h"
11#include "nacl_io/real_pepper_interface.h"
12
13
14static KernelProxy* s_kp;
15
16void ki_init(void* kp) {
17  ki_init_ppapi(kp, 0, NULL);
18}
19
20void ki_init_ppapi(void* kp,
21                   PP_Instance instance,
22                   PPB_GetInterface get_browser_interface) {
23  kernel_wrap_init();
24
25  if (kp == NULL) kp = new KernelProxy();
26  s_kp = static_cast<KernelProxy*>(kp);
27
28  PepperInterface* ppapi = NULL;
29  if (instance && get_browser_interface)
30    ppapi = new RealPepperInterface(instance, get_browser_interface);
31
32  s_kp->Init(ppapi);
33}
34
35int ki_is_initialized() {
36  return s_kp != NULL;
37}
38
39void ki_uninit() {
40  s_kp = NULL;
41}
42
43int ki_chdir(const char* path) {
44  return s_kp->chdir(path);
45}
46
47char* ki_getcwd(char* buf, size_t size) {
48  // gtest uses getcwd in a static initializer. If we haven't initialized the
49  // kernel-intercept yet, just return ".".
50  if (!ki_is_initialized()) {
51    if (size < 2) {
52      errno = ERANGE;
53      return NULL;
54    }
55    buf[0] = '.';
56    buf[1] = 0;
57    return buf;
58  }
59  return s_kp->getcwd(buf, size);
60}
61
62char* ki_getwd(char* buf) {
63  return s_kp->getwd(buf);
64}
65
66int ki_dup(int oldfd) {
67  return s_kp->dup(oldfd);
68}
69
70int ki_dup2(int oldfd, int newfd) {
71  return s_kp->dup2(oldfd, newfd);
72}
73
74int ki_chmod(const char *path, mode_t mode) {
75  return s_kp->chmod(path, mode);
76}
77
78int ki_stat(const char *path, struct stat *buf) {
79  return s_kp->stat(path, buf);
80}
81
82int ki_mkdir(const char *path, mode_t mode) {
83  return s_kp->mkdir(path, mode);
84}
85
86int ki_rmdir(const char *path) {
87  return s_kp->rmdir(path);
88}
89
90int ki_mount(const char *source, const char *target, const char *filesystemtype,
91             unsigned long mountflags, const void *data) {
92  return s_kp->mount(source, target, filesystemtype, mountflags, data);
93}
94
95int ki_umount(const char *path) {
96  return s_kp->umount(path);
97}
98
99int ki_open(const char *path, int oflag) {
100  return s_kp->open(path, oflag);
101}
102
103ssize_t ki_read(int fd, void *buf, size_t nbyte) {
104  return s_kp->read(fd, buf, nbyte);
105}
106
107ssize_t ki_write(int fd, const void *buf, size_t nbyte) {
108  return s_kp->write(fd, buf, nbyte);
109}
110
111int ki_fstat(int fd, struct stat *buf){
112  return s_kp->fstat(fd, buf);
113}
114
115int ki_getdents(int fd, void *buf, unsigned int count) {
116  return s_kp->getdents(fd, buf, count);
117}
118
119int ki_ftruncate(int fd, off_t length) {
120  return s_kp->ftruncate(fd, length);
121}
122
123int ki_fsync(int fd) {
124  return s_kp->fsync(fd);
125}
126
127int ki_isatty(int fd) {
128  if (!ki_is_initialized())
129    return 0;
130  return s_kp->isatty(fd);
131}
132
133int ki_close(int fd) {
134  if (!ki_is_initialized())
135    return 0;
136  return s_kp->close(fd);
137}
138
139off_t ki_lseek(int fd, off_t offset, int whence) {
140  return s_kp->lseek(fd, offset, whence);
141}
142
143int ki_remove(const char* path) {
144  return s_kp->remove(path);
145}
146
147int ki_unlink(const char* path) {
148  return s_kp->unlink(path);
149}
150
151int ki_access(const char* path, int amode) {
152  return s_kp->access(path, amode);
153}
154
155int ki_link(const char* oldpath, const char* newpath) {
156  return s_kp->link(oldpath, newpath);
157}
158
159int ki_symlink(const char* oldpath, const char* newpath) {
160  return s_kp->symlink(oldpath, newpath);
161}
162
163void* ki_mmap(void* addr, size_t length, int prot, int flags, int fd,
164              off_t offset) {
165  return s_kp->mmap(addr, length, prot, flags, fd, offset);
166}
167
168int ki_munmap(void* addr, size_t length) {
169  return s_kp->munmap(addr, length);
170}
171
172int ki_open_resource(const char* file) {
173  return s_kp->open_resource(file);
174}
175