kernel_intercept.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
146a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com/* Copyright (c) 2012 The Chromium Authors. All rights reserved.
246a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com * Use of this source code is governed by a BSD-style license that can be
346a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com * found in the LICENSE file.
446a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com */
546a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com#include <errno.h>
646a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com#include "nacl_io/kernel_intercept.h"
746a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com#include "nacl_io/kernel_proxy.h"
80797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com#include "nacl_io/kernel_wrap.h"
946a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com#include "nacl_io/pepper_interface.h"
1046a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com#include "nacl_io/pepper_interface.h"
115c265d6fd3537c99304b153fc49066db3be46d46bsalomon@google.com#include "nacl_io/real_pepper_interface.h"
125c265d6fd3537c99304b153fc49066db3be46d46bsalomon@google.com
135c265d6fd3537c99304b153fc49066db3be46d46bsalomon@google.com
143ac5eb571bda3422a2868ddcf67ac731e7242a92reed@google.comstatic KernelProxy* s_kp;
153ac5eb571bda3422a2868ddcf67ac731e7242a92reed@google.com
163ac5eb571bda3422a2868ddcf67ac731e7242a92reed@google.comvoid ki_init(void* kp) {
173ac5eb571bda3422a2868ddcf67ac731e7242a92reed@google.com  ki_init_ppapi(kp, 0, NULL);
185c265d6fd3537c99304b153fc49066db3be46d46bsalomon@google.com}
193ac5eb571bda3422a2868ddcf67ac731e7242a92reed@google.com
203ac5eb571bda3422a2868ddcf67ac731e7242a92reed@google.comvoid ki_init_ppapi(void* kp,
213ac5eb571bda3422a2868ddcf67ac731e7242a92reed@google.com                   PP_Instance instance,
223ac5eb571bda3422a2868ddcf67ac731e7242a92reed@google.com                   PPB_GetInterface get_browser_interface) {
235c265d6fd3537c99304b153fc49066db3be46d46bsalomon@google.com  kernel_wrap_init();
240797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com
250797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com  if (kp == NULL) kp = new KernelProxy();
260797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com  s_kp = static_cast<KernelProxy*>(kp);
2746a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com
2846a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com  PepperInterface* ppapi = NULL;
290797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com  if (instance && get_browser_interface)
3088cb22b6b4816c7a9ca6c5b795965b4606f9eb7bcommit-bot@chromium.org    ppapi = new RealPepperInterface(instance, get_browser_interface);
3146a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com
3246a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com  s_kp->Init(ppapi);
330797c2cceadd7dfc2e7f9efa30b611d18efcdcddbsalomon@google.com}
3446a8600405e678718271f62c5994119b8d3241e9robertphillips@google.com
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
176int ki_ioctl(int d, int request, char* argp) {
177  return s_kp->ioctl(d, request, argp);
178}
179
180int ki_chown(const char* path, uid_t owner, gid_t group) {
181  return s_kp->chown(path, owner, group);
182}
183
184int ki_fchown(int fd, uid_t owner, gid_t group) {
185  return s_kp->fchown(fd, owner, group);
186}
187
188int ki_lchown(const char* path, uid_t owner, gid_t group) {
189  return s_kp->lchown(path, owner, group);
190}
191
192int ki_utime(const char* filename, const struct utimbuf* times) {
193  return s_kp->utime(filename, times);
194}
195