kernel_proxy.h revision 3551c9c881056c480085172ff9840cab31610854
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#ifndef LIBRARIES_NACL_IO_KERNEL_PROXY_H_ 6#define LIBRARIES_NACL_IO_KERNEL_PROXY_H_ 7 8#include <map> 9#include <string> 10 11#include "nacl_io/host_resolver.h" 12#include "nacl_io/kernel_object.h" 13#include "nacl_io/mount_factory.h" 14#include "nacl_io/mount_socket.h" 15#include "nacl_io/ossignal.h" 16#include "nacl_io/ossocket.h" 17#include "nacl_io/ostypes.h" 18#include "nacl_io/osutime.h" 19 20struct timeval; 21 22namespace nacl_io { 23 24class PepperInterface; 25class SignalEmitter; 26 27typedef sdk_util::ScopedRef<SignalEmitter> ScopedSignalEmitter; 28 29// KernelProxy provide one-to-one mapping for libc kernel calls. Calls to the 30// proxy will result in IO access to the provided Mount and MountNode objects. 31// 32// NOTE: The KernelProxy does not directly take any kernel locks, all locking 33// is done by the parent class KernelObject. Instead, KernelProxy is 34// responsible for taking the locks of the KernelHandle, and MountNode objects. 35// For this reason, a KernelObject call should not be done while holding 36// a handle or node lock. In addition, to ensure locking order, 37// a KernelHandle lock must never be taken after taking the associated 38// MountNode's lock. 39// 40// NOTE: The KernelProxy is the only class that should be setting errno. All 41// other classes should return Error (as defined by nacl_io/error.h). 42class KernelProxy : protected KernelObject { 43 public: 44 typedef std::map<std::string, MountFactory*> MountFactoryMap_t; 45 46 KernelProxy(); 47 virtual ~KernelProxy(); 48 49 // Takes ownership of |ppapi|. 50 // |ppapi| may be NULL. If so, no mount that uses pepper calls can be mounted. 51 virtual void Init(PepperInterface* ppapi); 52 53 // NaCl-only function to read resources specified in the NMF file. 54 virtual int open_resource(const char* file); 55 56 // KernelHandle and FD allocation and manipulation functions. 57 virtual int open(const char* path, int oflag); 58 virtual int close(int fd); 59 virtual int dup(int fd); 60 virtual int dup2(int fd, int newfd); 61 62 // Path related System calls handled by KernelProxy (not mount-specific) 63 virtual int chdir(const char* path); 64 virtual char* getcwd(char* buf, size_t size); 65 virtual char* getwd(char* buf); 66 virtual int mount(const char *source, 67 const char *target, 68 const char *filesystemtype, 69 unsigned long mountflags, 70 const void *data); 71 virtual int umount(const char *path); 72 73 // Stub system calls that don't do anything (yet), handled by KernelProxy. 74 virtual int chown(const char* path, uid_t owner, gid_t group); 75 virtual int fchown(int fd, uid_t owner, gid_t group); 76 virtual int lchown(const char* path, uid_t owner, gid_t group); 77 virtual int utime(const char* filename, const struct utimbuf* times); 78 79 // System calls that take a path as an argument: 80 // The kernel proxy will look for the Node associated to the path. To 81 // find the node, the kernel proxy calls the corresponding mount's GetNode() 82 // method. The corresponding method will be called. If the node 83 // cannot be found, errno is set and -1 is returned. 84 virtual int chmod(const char *path, mode_t mode); 85 virtual int mkdir(const char *path, mode_t mode); 86 virtual int rmdir(const char *path); 87 virtual int stat(const char *path, struct stat *buf); 88 89 // System calls that take a file descriptor as an argument: 90 // The kernel proxy will determine to which mount the file 91 // descriptor's corresponding file handle belongs. The 92 // associated mount's function will be called. 93 virtual ssize_t read(int fd, void *buf, size_t nbyte); 94 virtual ssize_t write(int fd, const void *buf, size_t nbyte); 95 96 virtual int fchmod(int fd, int prot); 97 virtual int fstat(int fd, struct stat *buf); 98 virtual int getdents(int fd, void *buf, unsigned int count); 99 virtual int ftruncate(int fd, off_t length); 100 virtual int fsync(int fd); 101 virtual int isatty(int fd); 102 virtual int ioctl(int d, int request, char *argp); 103 104 // lseek() relies on the mount's Stat() to determine whether or not the 105 // file handle corresponding to fd is a directory 106 virtual off_t lseek(int fd, off_t offset, int whence); 107 108 // remove() uses the mount's GetNode() and Stat() to determine whether or 109 // not the path corresponds to a directory or a file. The mount's Rmdir() 110 // or Unlink() is called accordingly. 111 virtual int remove(const char* path); 112 // unlink() is a simple wrapper around the mount's Unlink function. 113 virtual int unlink(const char* path); 114 // access() uses the Mount's Stat(). 115 virtual int access(const char* path, int amode); 116 117 virtual int link(const char* oldpath, const char* newpath); 118 virtual int symlink(const char* oldpath, const char* newpath); 119 120 virtual void* mmap(void* addr, 121 size_t length, 122 int prot, 123 int flags, 124 int fd, 125 size_t offset); 126 virtual int munmap(void* addr, size_t length); 127 virtual int tcflush(int fd, int queue_selector); 128 virtual int tcgetattr(int fd, struct termios* termios_p); 129 virtual int tcsetattr(int fd, int optional_actions, 130 const struct termios *termios_p); 131 132 virtual int kill(pid_t pid, int sig); 133 virtual sighandler_t sigset(int signum, sighandler_t handler); 134 135#ifdef PROVIDES_SOCKET_API 136 virtual int select(int nfds, fd_set* readfds, fd_set* writefds, 137 fd_set* exceptfds, struct timeval* timeout); 138 139 virtual int poll(struct pollfd *fds, nfds_t nfds, int timeout); 140 141 // Socket support functions 142 virtual int accept(int fd, struct sockaddr* addr, socklen_t* len); 143 virtual int bind(int fd, const struct sockaddr* addr, socklen_t len); 144 virtual int connect(int fd, const struct sockaddr* addr, socklen_t len); 145 virtual struct hostent* gethostbyname(const char* name); 146 virtual int getpeername(int fd, struct sockaddr* addr, socklen_t* len); 147 virtual int getsockname(int fd, struct sockaddr* addr, socklen_t* len); 148 virtual int getsockopt(int fd, 149 int lvl, 150 int optname, 151 void* optval, 152 socklen_t* len); 153 virtual int listen(int fd, int backlog); 154 virtual ssize_t recv(int fd, 155 void* buf, 156 size_t len, 157 int flags); 158 virtual ssize_t recvfrom(int fd, 159 void* buf, 160 size_t len, 161 int flags, 162 struct sockaddr* addr, 163 socklen_t* addrlen); 164 virtual ssize_t recvmsg(int fd, struct msghdr* msg, int flags); 165 virtual ssize_t send(int fd, const void* buf, size_t len, int flags); 166 virtual ssize_t sendto(int fd, 167 const void* buf, 168 size_t len, 169 int flags, 170 const struct sockaddr* addr, 171 socklen_t addrlen); 172 virtual ssize_t sendmsg(int fd, const struct msghdr* msg, int flags); 173 virtual int setsockopt(int fd, 174 int lvl, 175 int optname, 176 const void* optval, 177 socklen_t len); 178 virtual int shutdown(int fd, int how); 179 virtual int socket(int domain, int type, int protocol); 180 virtual int socketpair(int domain, int type, int protocol, int* sv); 181#endif // PROVIDES_SOCKET_API 182 183 protected: 184 MountFactoryMap_t factories_; 185 sdk_util::ScopedRef<MountSocket> socket_mount_; 186 int dev_; 187 PepperInterface* ppapi_; 188 static KernelProxy *s_instance_; 189 sighandler_t sigwinch_handler_; 190#ifdef PROVIDES_SOCKET_API 191 HostResolver host_resolver_; 192#endif 193 194#ifdef PROVIDES_SOCKET_API 195 virtual int AcquireSocketHandle(int fd, ScopedKernelHandle* handle); 196#endif 197 198 ScopedSignalEmitter signal_emitter_; 199 DISALLOW_COPY_AND_ASSIGN(KernelProxy); 200}; 201 202} // namespace nacl_io 203 204#endif // LIBRARIES_NACL_IO_KERNEL_PROXY_H_ 205