This source file includes following definitions.
- ki_push_state_for_testing
- ki_init
- ki_init_ppapi
- ki_init_interface
- ki_is_initialized
- ki_uninit
- ki_get_proxy
- ki_chdir
- ki_exit
- ki_getcwd
- ki_getwd
- ki_dup
- ki_dup2
- ki_chmod
- ki_fchdir
- ki_fchmod
- ki_stat
- ki_mkdir
- ki_rmdir
- ki_mount
- ki_umount
- ki_open
- ki_pipe
- ki_read
- ki_write
- ki_fstat
- ki_getdents
- ki_ftruncate
- ki_fsync
- ki_fdatasync
- ki_isatty
- ki_close
- ki_lseek
- ki_remove
- ki_unlink
- ki_truncate
- ki_lstat
- ki_link
- ki_rename
- ki_symlink
- ki_access
- ki_readlink
- ki_utimes
- ki_mmap
- ki_munmap
- ki_open_resource
- ki_fcntl
- ki_ioctl
- ki_chown
- ki_fchown
- ki_lchown
- ki_utime
- ki_poll
- ki_select
- ki_tcflush
- ki_tcgetattr
- ki_tcsetattr
- ki_kill
- ki_killpg
- ki_sigaction
- ki_sigpause
- ki_sigpending
- ki_sigsuspend
- ki_signal
- ki_sigset
- ki_accept
- ki_bind
- ki_connect
- ki_gethostbyname
- ki_getaddrinfo
- ki_freeaddrinfo
- ki_getpeername
- ki_getsockname
- ki_getsockopt
- ki_listen
- ki_recv
- ki_recvfrom
- ki_recvmsg
- ki_send
- ki_sendto
- ki_sendmsg
- ki_setsockopt
- ki_shutdown
- ki_socket
- ki_socketpair
#include "nacl_io/kernel_intercept.h"
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "nacl_io/kernel_proxy.h"
#include "nacl_io/kernel_wrap.h"
#include "nacl_io/kernel_wrap_real.h"
#include "nacl_io/osmman.h"
#include "nacl_io/ossocket.h"
#include "nacl_io/pepper_interface.h"
#include "nacl_io/real_pepper_interface.h"
using namespace nacl_io;
#define ON_NOSYS_RETURN(x) \
if (!ki_is_initialized()) { \
errno = ENOSYS; \
return x; \
}
struct KernelInterceptState {
KernelProxy* kp;
bool kp_owned;
};
static KernelInterceptState s_state;
static KernelInterceptState s_saved_state;
int ki_push_state_for_testing() {
assert(s_saved_state.kp == NULL);
if (s_saved_state.kp != NULL)
return 1;
s_saved_state = s_state;
s_state.kp = NULL;
s_state.kp_owned = false;
return 0;
}
int ki_init(void* kp) {
return ki_init_ppapi(kp, 0, NULL);
}
int ki_init_ppapi(void* kp,
PP_Instance instance,
PPB_GetInterface get_browser_interface) {
assert(!s_state.kp);
if (s_state.kp != NULL)
return 1;
PepperInterface* ppapi = NULL;
if (instance && get_browser_interface)
ppapi = new RealPepperInterface(instance, get_browser_interface);
return ki_init_interface(kp, ppapi);
}
int ki_init_interface(void* kp, void* pepper_interface) {
assert(!s_state.kp);
if (s_state.kp != NULL)
return 1;
PepperInterface* ppapi = static_cast<PepperInterface*>(pepper_interface);
kernel_wrap_init();
if (kp == NULL) {
s_state.kp = new KernelProxy();
s_state.kp_owned = true;
} else {
s_state.kp = static_cast<KernelProxy*>(kp);
s_state.kp_owned = false;
}
if (s_state.kp->Init(ppapi) != 0)
return 1;
return 0;
}
int ki_is_initialized() {
return s_state.kp != NULL;
}
void ki_uninit() {
if (s_saved_state.kp == NULL)
kernel_wrap_uninit();
KernelProxy* delete_kp = s_state.kp_owned ? s_state.kp : NULL;
s_state = s_saved_state;
s_saved_state.kp = NULL;
s_saved_state.kp_owned = false;
if (delete_kp)
delete delete_kp;
}
nacl_io::KernelProxy* ki_get_proxy() {
return s_state.kp;
}
int ki_chdir(const char* path) {
ON_NOSYS_RETURN(-1);
return s_state.kp->chdir(path);
}
void ki_exit(int status) {
if (ki_is_initialized())
s_state.kp->exit(status);
_real_exit(status);
}
char* ki_getcwd(char* buf, size_t size) {
if (!ki_is_initialized()) {
if (size < 2) {
errno = ERANGE;
return NULL;
}
buf[0] = '.';
buf[1] = 0;
return buf;
}
return s_state.kp->getcwd(buf, size);
}
char* ki_getwd(char* buf) {
ON_NOSYS_RETURN(NULL);
return s_state.kp->getwd(buf);
}
int ki_dup(int oldfd) {
ON_NOSYS_RETURN(-1);
return s_state.kp->dup(oldfd);
}
int ki_dup2(int oldfd, int newfd) {
ON_NOSYS_RETURN(-1);
return s_state.kp->dup2(oldfd, newfd);
}
int ki_chmod(const char *path, mode_t mode) {
ON_NOSYS_RETURN(-1);
return s_state.kp->chmod(path, mode);
}
int ki_fchdir(int fd) {
ON_NOSYS_RETURN(-1);
return s_state.kp->fchdir(fd);
}
int ki_fchmod(int fd, mode_t mode) {
ON_NOSYS_RETURN(-1);
return s_state.kp->fchmod(fd, mode);
}
int ki_stat(const char *path, struct stat *buf) {
ON_NOSYS_RETURN(-1);
return s_state.kp->stat(path, buf);
}
int ki_mkdir(const char *path, mode_t mode) {
ON_NOSYS_RETURN(-1);
return s_state.kp->mkdir(path, mode);
}
int ki_rmdir(const char *path) {
ON_NOSYS_RETURN(-1);
return s_state.kp->rmdir(path);
}
int ki_mount(const char *source, const char *target, const char *filesystemtype,
unsigned long mountflags, const void *data) {
ON_NOSYS_RETURN(-1);
return s_state.kp->mount(source, target, filesystemtype, mountflags, data);
}
int ki_umount(const char *path) {
ON_NOSYS_RETURN(-1);
return s_state.kp->umount(path);
}
int ki_open(const char *path, int oflag) {
ON_NOSYS_RETURN(-1);
return s_state.kp->open(path, oflag);
}
int ki_pipe(int pipefds[2]) {
ON_NOSYS_RETURN(-1);
return s_state.kp->pipe(pipefds);
}
ssize_t ki_read(int fd, void *buf, size_t nbyte) {
ON_NOSYS_RETURN(-1);
return s_state.kp->read(fd, buf, nbyte);
}
ssize_t ki_write(int fd, const void *buf, size_t nbyte) {
ON_NOSYS_RETURN(-1);
return s_state.kp->write(fd, buf, nbyte);
}
int ki_fstat(int fd, struct stat *buf){
ON_NOSYS_RETURN(-1);
return s_state.kp->fstat(fd, buf);
}
int ki_getdents(int fd, void *buf, unsigned int count) {
ON_NOSYS_RETURN(-1);
return s_state.kp->getdents(fd, buf, count);
}
int ki_ftruncate(int fd, off_t length) {
ON_NOSYS_RETURN(-1);
return s_state.kp->ftruncate(fd, length);
}
int ki_fsync(int fd) {
ON_NOSYS_RETURN(-1);
return s_state.kp->fsync(fd);
}
int ki_fdatasync(int fd) {
ON_NOSYS_RETURN(-1);
return s_state.kp->fdatasync(fd);
}
int ki_isatty(int fd) {
ON_NOSYS_RETURN(0);
return s_state.kp->isatty(fd);
}
int ki_close(int fd) {
ON_NOSYS_RETURN(-1);
return s_state.kp->close(fd);
}
off_t ki_lseek(int fd, off_t offset, int whence) {
ON_NOSYS_RETURN(-1);
return s_state.kp->lseek(fd, offset, whence);
}
int ki_remove(const char* path) {
ON_NOSYS_RETURN(-1);
return s_state.kp->remove(path);
}
int ki_unlink(const char* path) {
ON_NOSYS_RETURN(-1);
return s_state.kp->unlink(path);
}
int ki_truncate(const char* path, off_t length) {
ON_NOSYS_RETURN(-1);
return s_state.kp->truncate(path, length);
}
int ki_lstat(const char* path, struct stat* buf) {
ON_NOSYS_RETURN(-1);
return s_state.kp->lstat(path, buf);
}
int ki_link(const char* oldpath, const char* newpath) {
ON_NOSYS_RETURN(-1);
return s_state.kp->link(oldpath, newpath);
}
int ki_rename(const char* path, const char* newpath) {
ON_NOSYS_RETURN(-1);
return s_state.kp->rename(path, newpath);
}
int ki_symlink(const char* oldpath, const char* newpath) {
ON_NOSYS_RETURN(-1);
return s_state.kp->symlink(oldpath, newpath);
}
int ki_access(const char* path, int amode) {
ON_NOSYS_RETURN(-1);
return s_state.kp->access(path, amode);
}
int ki_readlink(const char *path, char *buf, size_t count) {
ON_NOSYS_RETURN(-1);
return s_state.kp->readlink(path, buf, count);
}
int ki_utimes(const char *path, const struct timeval times[2]) {
ON_NOSYS_RETURN(-1);
return s_state.kp->utimes(path, times);
}
void* ki_mmap(void* addr, size_t length, int prot, int flags, int fd,
off_t offset) {
ON_NOSYS_RETURN(MAP_FAILED);
return s_state.kp->mmap(addr, length, prot, flags, fd, offset);
}
int ki_munmap(void* addr, size_t length) {
ON_NOSYS_RETURN(-1);
return s_state.kp->munmap(addr, length);
}
int ki_open_resource(const char* file) {
ON_NOSYS_RETURN(-1); return s_state.kp->open_resource(file);
}
int ki_fcntl(int d, int request, va_list args) {
ON_NOSYS_RETURN(-1);
return s_state.kp->fcntl(d, request, args);
}
int ki_ioctl(int d, int request, va_list args) {
ON_NOSYS_RETURN(-1);
return s_state.kp->ioctl(d, request, args);
}
int ki_chown(const char* path, uid_t owner, gid_t group) {
ON_NOSYS_RETURN(-1);
return s_state.kp->chown(path, owner, group);
}
int ki_fchown(int fd, uid_t owner, gid_t group) {
ON_NOSYS_RETURN(-1);
return s_state.kp->fchown(fd, owner, group);
}
int ki_lchown(const char* path, uid_t owner, gid_t group) {
ON_NOSYS_RETURN(-1);
return s_state.kp->lchown(path, owner, group);
}
int ki_utime(const char* filename, const struct utimbuf* times) {
ON_NOSYS_RETURN(-1);
return s_state.kp->utime(filename, times);
}
int ki_poll(struct pollfd *fds, nfds_t nfds, int timeout) {
return s_state.kp->poll(fds, nfds, timeout);
}
int ki_select(int nfds, fd_set* readfds, fd_set* writefds,
fd_set* exceptfds, struct timeval* timeout) {
return s_state.kp->select(nfds, readfds, writefds, exceptfds, timeout);
}
int ki_tcflush(int fd, int queue_selector) {
ON_NOSYS_RETURN(-1);
return s_state.kp->tcflush(fd, queue_selector);
}
int ki_tcgetattr(int fd, struct termios* termios_p) {
ON_NOSYS_RETURN(-1);
return s_state.kp->tcgetattr(fd, termios_p);
}
int ki_tcsetattr(int fd, int optional_actions,
const struct termios *termios_p) {
ON_NOSYS_RETURN(-1);
return s_state.kp->tcsetattr(fd, optional_actions, termios_p);
}
int ki_kill(pid_t pid, int sig) {
ON_NOSYS_RETURN(-1);
return s_state.kp->kill(pid, sig);
}
int ki_killpg(pid_t pid, int sig) {
errno = ENOSYS;
return -1;
}
int ki_sigaction(int signum, const struct sigaction* action,
struct sigaction* oaction) {
ON_NOSYS_RETURN(-1);
return s_state.kp->sigaction(signum, action, oaction);
}
int ki_sigpause(int sigmask) {
errno = ENOSYS;
return -1;
}
int ki_sigpending(sigset_t* set) {
errno = ENOSYS;
return -1;
}
int ki_sigsuspend(const sigset_t* set) {
errno = ENOSYS;
return -1;
}
sighandler_t ki_signal(int signum, sighandler_t handler) {
return ki_sigset(signum, handler);
}
sighandler_t ki_sigset(int signum, sighandler_t handler) {
ON_NOSYS_RETURN(SIG_ERR);
struct sigaction action;
struct sigaction oaction;
memset(&action, 0, sizeof(action));
memset(&oaction, 0, sizeof(oaction));
action.sa_handler = handler;
int rtn = s_state.kp->sigaction(signum, &action, &oaction);
if (rtn)
return SIG_ERR;
return oaction.sa_handler;
}
#ifdef PROVIDES_SOCKET_API
int ki_accept(int fd, struct sockaddr* addr, socklen_t* len) {
ON_NOSYS_RETURN(-1);
return s_state.kp->accept(fd, addr, len);
}
int ki_bind(int fd, const struct sockaddr* addr, socklen_t len) {
ON_NOSYS_RETURN(-1);
return s_state.kp->bind(fd, addr, len);
}
int ki_connect(int fd, const struct sockaddr* addr, socklen_t len) {
ON_NOSYS_RETURN(-1);
return s_state.kp->connect(fd, addr, len);
}
struct hostent* ki_gethostbyname(const char* name) {
ON_NOSYS_RETURN(NULL);
return s_state.kp->gethostbyname(name);
}
int ki_getaddrinfo(const char *node, const char *service,
const struct addrinfo *hints,
struct addrinfo **res) {
ON_NOSYS_RETURN(EAI_SYSTEM);
return s_state.kp->getaddrinfo(node, service, hints, res);
}
void ki_freeaddrinfo(struct addrinfo *res) {
s_state.kp->freeaddrinfo(res);
}
int ki_getpeername(int fd, struct sockaddr* addr, socklen_t* len) {
ON_NOSYS_RETURN(-1);
return s_state.kp->getpeername(fd, addr, len);
}
int ki_getsockname(int fd, struct sockaddr* addr, socklen_t* len) {
ON_NOSYS_RETURN(-1);
return s_state.kp->getsockname(fd, addr, len);
}
int ki_getsockopt(int fd, int lvl, int optname, void* optval, socklen_t* len) {
ON_NOSYS_RETURN(-1);
return s_state.kp->getsockopt(fd, lvl, optname, optval, len);
}
int ki_listen(int fd, int backlog) {
ON_NOSYS_RETURN(-1);
return s_state.kp->listen(fd, backlog);
}
ssize_t ki_recv(int fd, void* buf, size_t len, int flags) {
ON_NOSYS_RETURN(-1);
return s_state.kp->recv(fd, buf, len, flags);
}
ssize_t ki_recvfrom(int fd, void* buf, size_t len, int flags,
struct sockaddr* addr, socklen_t* addrlen) {
ON_NOSYS_RETURN(-1);
return s_state.kp->recvfrom(fd, buf, len, flags, addr, addrlen);
}
ssize_t ki_recvmsg(int fd, struct msghdr* msg, int flags) {
ON_NOSYS_RETURN(-1);
return s_state.kp->recvmsg(fd, msg, flags);
}
ssize_t ki_send(int fd, const void* buf, size_t len, int flags) {
ON_NOSYS_RETURN(-1);
return s_state.kp->send(fd, buf, len, flags);
}
ssize_t ki_sendto(int fd, const void* buf, size_t len, int flags,
const struct sockaddr* addr, socklen_t addrlen) {
ON_NOSYS_RETURN(-1);
return s_state.kp->sendto(fd, buf, len, flags, addr, addrlen);
}
ssize_t ki_sendmsg(int fd, const struct msghdr* msg, int flags) {
ON_NOSYS_RETURN(-1);
return s_state.kp->sendmsg(fd, msg, flags);
}
int ki_setsockopt(int fd, int lvl, int optname, const void* optval,
socklen_t len) {
ON_NOSYS_RETURN(-1);
return s_state.kp->setsockopt(fd, lvl, optname, optval, len);
}
int ki_shutdown(int fd, int how) {
ON_NOSYS_RETURN(-1);
return s_state.kp->shutdown(fd, how);
}
int ki_socket(int domain, int type, int protocol) {
ON_NOSYS_RETURN(-1);
return s_state.kp->socket(domain, type, protocol);
}
int ki_socketpair(int domain, int type, int protocol, int* sv) {
ON_NOSYS_RETURN(-1);
return s_state.kp->socketpair(domain, type, protocol, sv);
}
#endif