kernel_intercept.cc revision 58e6fbe4ee35d65e14b626c557d37565bf8ad179
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
7#include "nacl_io/kernel_intercept.h"
8#include "nacl_io/kernel_proxy.h"
9#include "nacl_io/kernel_wrap.h"
10#include "nacl_io/osmman.h"
11#include "nacl_io/pepper_interface.h"
12#include "nacl_io/pepper_interface.h"
13#include "nacl_io/real_pepper_interface.h"
14
15using namespace nacl_io;
16
17#define ON_NOSYS_RETURN(x)    \
18  if (!ki_is_initialized()) { \
19    errno = ENOSYS;           \
20    return x;                 \
21  }
22
23static KernelProxy* s_kp;
24
25void ki_init(void* kp) {
26  ki_init_ppapi(kp, 0, NULL);
27}
28
29void ki_init_ppapi(void* kp,
30                   PP_Instance instance,
31                   PPB_GetInterface get_browser_interface) {
32  kernel_wrap_init();
33
34  if (kp == NULL) kp = new KernelProxy();
35  s_kp = static_cast<KernelProxy*>(kp);
36
37  PepperInterface* ppapi = NULL;
38  if (instance && get_browser_interface)
39    ppapi = new RealPepperInterface(instance, get_browser_interface);
40
41  s_kp->Init(ppapi);
42}
43
44int ki_is_initialized() {
45  return s_kp != NULL;
46}
47
48void ki_uninit() {
49  kernel_wrap_uninit();
50  s_kp = NULL;
51}
52
53
54int ki_chdir(const char* path) {
55  ON_NOSYS_RETURN(-1);
56  return s_kp->chdir(path);
57}
58
59char* ki_getcwd(char* buf, size_t size) {
60  // gtest uses getcwd in a static initializer. If we haven't initialized the
61  // kernel-intercept yet, just return ".".
62  if (!ki_is_initialized()) {
63    if (size < 2) {
64      errno = ERANGE;
65      return NULL;
66    }
67    buf[0] = '.';
68    buf[1] = 0;
69    return buf;
70  }
71  return s_kp->getcwd(buf, size);
72}
73
74char* ki_getwd(char* buf) {
75  ON_NOSYS_RETURN(NULL);
76  return s_kp->getwd(buf);
77}
78
79int ki_dup(int oldfd) {
80  ON_NOSYS_RETURN(-1);
81  return s_kp->dup(oldfd);
82}
83
84int ki_dup2(int oldfd, int newfd) {
85  ON_NOSYS_RETURN(-1);
86  return s_kp->dup2(oldfd, newfd);
87}
88
89int ki_chmod(const char *path, mode_t mode) {
90  ON_NOSYS_RETURN(-1);
91  return s_kp->chmod(path, mode);
92}
93
94int ki_stat(const char *path, struct stat *buf) {
95  ON_NOSYS_RETURN(-1);
96  return s_kp->stat(path, buf);
97}
98
99int ki_mkdir(const char *path, mode_t mode) {
100  ON_NOSYS_RETURN(-1);
101  return s_kp->mkdir(path, mode);
102}
103
104int ki_rmdir(const char *path) {
105  ON_NOSYS_RETURN(-1);
106  return s_kp->rmdir(path);
107}
108
109int ki_mount(const char *source, const char *target, const char *filesystemtype,
110             unsigned long mountflags, const void *data) {
111  ON_NOSYS_RETURN(-1);
112  return s_kp->mount(source, target, filesystemtype, mountflags, data);
113}
114
115int ki_umount(const char *path) {
116  ON_NOSYS_RETURN(-1);
117  return s_kp->umount(path);
118}
119
120int ki_open(const char *path, int oflag) {
121  ON_NOSYS_RETURN(-1);
122  return s_kp->open(path, oflag);
123}
124
125ssize_t ki_read(int fd, void *buf, size_t nbyte) {
126  ON_NOSYS_RETURN(-1);
127  return s_kp->read(fd, buf, nbyte);
128}
129
130ssize_t ki_write(int fd, const void *buf, size_t nbyte) {
131  ON_NOSYS_RETURN(-1);
132  return s_kp->write(fd, buf, nbyte);
133}
134
135int ki_fstat(int fd, struct stat *buf){
136  ON_NOSYS_RETURN(-1);
137  return s_kp->fstat(fd, buf);
138}
139
140int ki_getdents(int fd, void *buf, unsigned int count) {
141  ON_NOSYS_RETURN(-1);
142  return s_kp->getdents(fd, buf, count);
143}
144
145int ki_ftruncate(int fd, off_t length) {
146  ON_NOSYS_RETURN(-1);
147  return s_kp->ftruncate(fd, length);
148}
149
150int ki_fsync(int fd) {
151  ON_NOSYS_RETURN(-1);
152  return s_kp->fsync(fd);
153}
154
155int ki_isatty(int fd) {
156  ON_NOSYS_RETURN(0);
157  return s_kp->isatty(fd);
158}
159
160int ki_close(int fd) {
161  ON_NOSYS_RETURN(-1);
162  return s_kp->close(fd);
163}
164
165off_t ki_lseek(int fd, off_t offset, int whence) {
166  ON_NOSYS_RETURN(-1);
167  return s_kp->lseek(fd, offset, whence);
168}
169
170int ki_remove(const char* path) {
171  ON_NOSYS_RETURN(-1);
172  return s_kp->remove(path);
173}
174
175int ki_unlink(const char* path) {
176  ON_NOSYS_RETURN(-1);
177  return s_kp->unlink(path);
178}
179
180int ki_access(const char* path, int amode) {
181  ON_NOSYS_RETURN(-1);
182  return s_kp->access(path, amode);
183}
184
185int ki_link(const char* oldpath, const char* newpath) {
186  ON_NOSYS_RETURN(-1);
187  return s_kp->link(oldpath, newpath);
188}
189
190int ki_symlink(const char* oldpath, const char* newpath) {
191  ON_NOSYS_RETURN(-1);
192  return s_kp->symlink(oldpath, newpath);
193}
194
195void* ki_mmap(void* addr, size_t length, int prot, int flags, int fd,
196              off_t offset) {
197  ON_NOSYS_RETURN(MAP_FAILED);
198  return s_kp->mmap(addr, length, prot, flags, fd, offset);
199}
200
201int ki_munmap(void* addr, size_t length) {
202  ON_NOSYS_RETURN(-1);
203  return s_kp->munmap(addr, length);
204}
205
206int ki_open_resource(const char* file) {
207  ON_NOSYS_RETURN(-1);  return s_kp->open_resource(file);
208}
209
210int ki_ioctl(int d, int request, char* argp) {
211  ON_NOSYS_RETURN(-1);
212  return s_kp->ioctl(d, request, argp);
213}
214
215int ki_chown(const char* path, uid_t owner, gid_t group) {
216  ON_NOSYS_RETURN(-1);
217  return s_kp->chown(path, owner, group);
218}
219
220int ki_fchown(int fd, uid_t owner, gid_t group) {
221  ON_NOSYS_RETURN(-1);
222  return s_kp->fchown(fd, owner, group);
223}
224
225int ki_lchown(const char* path, uid_t owner, gid_t group) {
226  ON_NOSYS_RETURN(-1);
227  return s_kp->lchown(path, owner, group);
228}
229
230int ki_utime(const char* filename, const struct utimbuf* times) {
231  ON_NOSYS_RETURN(-1);
232  return s_kp->utime(filename, times);
233}
234