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/event_emitter.h" 12 #include "nacl_io/host_resolver.h" 13 #include "nacl_io/kernel_object.h" 14 #include "nacl_io/mount_factory.h" 15 #include "nacl_io/mount_stream.h" 16 #include "nacl_io/ossignal.h" 17 #include "nacl_io/ossocket.h" 18 #include "nacl_io/ostypes.h" 19 #include "nacl_io/osutime.h" 20 21 struct fuse_operations; 22 struct timeval; 23 24 namespace nacl_io { 25 26 class PepperInterface; 27 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). 42 class 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 Error Init(PepperInterface* ppapi); 52 53 // Register/Unregister a new mount type. See the documentation in nacl_io.h 54 // for more info. 55 bool RegisterMountType(const char* mount_type, fuse_operations* fuse_ops); 56 bool UnregisterMountType(const char* mount_type); 57 58 virtual int pipe(int pipefds[2]); 59 60 // NaCl-only function to read resources specified in the NMF file. 61 virtual int open_resource(const char* file); 62 63 // KernelHandle and FD allocation and manipulation functions. 64 virtual int open(const char* path, int open_flags); 65 virtual int close(int fd); 66 virtual int dup(int fd); 67 virtual int dup2(int fd, int newfd); 68 69 // Path related System calls handled by KernelProxy (not mount-specific) 70 virtual int chdir(const char* path); 71 virtual char* getcwd(char* buf, size_t size); 72 virtual char* getwd(char* buf); 73 virtual int mount(const char *source, 74 const char *target, 75 const char *filesystemtype, 76 unsigned long mountflags, 77 const void *data); 78 virtual int umount(const char *path); 79 80 // Stub system calls that don't do anything (yet), handled by KernelProxy. 81 virtual int chown(const char* path, uid_t owner, gid_t group); 82 virtual int fchown(int fd, uid_t owner, gid_t group); 83 virtual int lchown(const char* path, uid_t owner, gid_t group); 84 virtual int utime(const char* filename, const struct utimbuf* times); 85 86 // System calls that take a path as an argument: 87 // The kernel proxy will look for the Node associated to the path. To 88 // find the node, the kernel proxy calls the corresponding mount's GetNode() 89 // method. The corresponding method will be called. If the node 90 // cannot be found, errno is set and -1 is returned. 91 virtual int chmod(const char *path, mode_t mode); 92 virtual int mkdir(const char *path, mode_t mode); 93 virtual int rmdir(const char *path); 94 virtual int stat(const char *path, struct stat *buf); 95 96 // System calls that take a file descriptor as an argument: 97 // The kernel proxy will determine to which mount the file 98 // descriptor's corresponding file handle belongs. The 99 // associated mount's function will be called. 100 virtual ssize_t read(int fd, void *buf, size_t nbyte); 101 virtual ssize_t write(int fd, const void *buf, size_t nbyte); 102 103 virtual int fchmod(int fd, int prot); 104 virtual int fcntl(int fd, int request, va_list args); 105 virtual int fstat(int fd, struct stat *buf); 106 virtual int getdents(int fd, void *buf, unsigned int count); 107 virtual int fchdir(int fd); 108 virtual int ftruncate(int fd, off_t length); 109 virtual int fsync(int fd); 110 virtual int fdatasync(int fd); 111 virtual int isatty(int fd); 112 virtual int ioctl(int fd, int request, va_list args); 113 114 // lseek() relies on the mount's Stat() to determine whether or not the 115 // file handle corresponding to fd is a directory 116 virtual off_t lseek(int fd, off_t offset, int whence); 117 118 // remove() uses the mount's GetNode() and Stat() to determine whether or 119 // not the path corresponds to a directory or a file. The mount's Rmdir() 120 // or Unlink() is called accordingly. 121 virtual int remove(const char* path); 122 // unlink() is a simple wrapper around the mount's Unlink function. 123 virtual int unlink(const char* path); 124 virtual int truncate(const char* path, off_t len); 125 virtual int lstat(const char* path, struct stat* buf); 126 virtual int rename(const char* path, const char* newpath); 127 // access() uses the Mount's Stat(). 128 virtual int access(const char* path, int amode); 129 virtual int readlink(const char *path, char *buf, size_t count); 130 virtual int utimes(const char *filename, const struct timeval times[2]); 131 132 virtual int link(const char* oldpath, const char* newpath); 133 virtual int symlink(const char* oldpath, const char* newpath); 134 135 virtual void* mmap(void* addr, 136 size_t length, 137 int prot, 138 int flags, 139 int fd, 140 size_t offset); 141 virtual int munmap(void* addr, size_t length); 142 virtual int tcflush(int fd, int queue_selector); 143 virtual int tcgetattr(int fd, struct termios* termios_p); 144 virtual int tcsetattr(int fd, int optional_actions, 145 const struct termios *termios_p); 146 147 virtual int kill(pid_t pid, int sig); 148 virtual int sigaction(int signum, const struct sigaction* action, 149 struct sigaction* oaction); 150 151 #ifdef PROVIDES_SOCKET_API 152 virtual int select(int nfds, fd_set* readfds, fd_set* writefds, 153 fd_set* exceptfds, struct timeval* timeout); 154 155 virtual int poll(struct pollfd *fds, nfds_t nfds, int timeout); 156 157 // Socket support functions 158 virtual int accept(int fd, struct sockaddr* addr, socklen_t* len); 159 virtual int bind(int fd, const struct sockaddr* addr, socklen_t len); 160 virtual int connect(int fd, const struct sockaddr* addr, socklen_t len); 161 virtual struct hostent* gethostbyname(const char* name); 162 virtual int getpeername(int fd, struct sockaddr* addr, socklen_t* len); 163 virtual int getsockname(int fd, struct sockaddr* addr, socklen_t* len); 164 virtual int getsockopt(int fd, 165 int lvl, 166 int optname, 167 void* optval, 168 socklen_t* len); 169 virtual int listen(int fd, int backlog); 170 virtual ssize_t recv(int fd, 171 void* buf, 172 size_t len, 173 int flags); 174 virtual ssize_t recvfrom(int fd, 175 void* buf, 176 size_t len, 177 int flags, 178 struct sockaddr* addr, 179 socklen_t* addrlen); 180 virtual ssize_t recvmsg(int fd, struct msghdr* msg, int flags); 181 virtual ssize_t send(int fd, const void* buf, size_t len, int flags); 182 virtual ssize_t sendto(int fd, 183 const void* buf, 184 size_t len, 185 int flags, 186 const struct sockaddr* addr, 187 socklen_t addrlen); 188 virtual ssize_t sendmsg(int fd, const struct msghdr* msg, int flags); 189 virtual int setsockopt(int fd, 190 int lvl, 191 int optname, 192 const void* optval, 193 socklen_t len); 194 virtual int shutdown(int fd, int how); 195 virtual int socket(int domain, int type, int protocol); 196 virtual int socketpair(int domain, int type, int protocol, int* sv); 197 #endif // PROVIDES_SOCKET_API 198 199 protected: 200 MountFactoryMap_t factories_; 201 sdk_util::ScopedRef<MountStream> stream_mount_; 202 int dev_; 203 PepperInterface* ppapi_; 204 static KernelProxy *s_instance_; 205 struct sigaction sigwinch_handler_; 206 #ifdef PROVIDES_SOCKET_API 207 HostResolver host_resolver_; 208 #endif 209 210 #ifdef PROVIDES_SOCKET_API 211 virtual int AcquireSocketHandle(int fd, ScopedKernelHandle* handle); 212 #endif 213 214 ScopedEventEmitter signal_emitter_; 215 DISALLOW_COPY_AND_ASSIGN(KernelProxy); 216 }; 217 218 } // namespace nacl_io 219 220 #endif // LIBRARIES_NACL_IO_KERNEL_PROXY_H_ 221