Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / libraries / nacl_io / kernel_proxy.h
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/fs_factory.h"
13 #include "nacl_io/host_resolver.h"
14 #include "nacl_io/kernel_object.h"
15 #include "nacl_io/nacl_io.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 #include "nacl_io/stream/stream_fs.h"
21
22 struct fuse_operations;
23 struct timeval;
24
25 namespace nacl_io {
26
27 class PepperInterface;
28
29
30 // KernelProxy provide one-to-one mapping for libc kernel calls.  Calls to the
31 // proxy will result in IO access to the provided Filesystem and Node objects.
32 //
33 // NOTE: The KernelProxy does not directly take any kernel locks, all locking
34 // is done by the parent class KernelObject. Instead, KernelProxy is
35 // responsible for taking the locks of the KernelHandle, and Node objects. For
36 // this reason, a KernelObject call should not be done while holding a handle
37 // or node lock. In addition, to ensure locking order, a KernelHandle lock
38 // must never be taken after taking the associated Node'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, FsFactory*> FsFactoryMap_t;
45
46   KernelProxy();
47   virtual ~KernelProxy();
48
49   // |ppapi| may be NULL. If so, no filesystem that uses pepper calls can be
50   // mounted.
51   virtual Error Init(PepperInterface* ppapi);
52
53   // Register/Unregister a new filesystem type. See the documentation in
54   // nacl_io.h for more info.
55   bool RegisterFsType(const char* fs_type, fuse_operations* fuse_ops);
56   bool UnregisterFsType(const char* fs_type);
57
58   bool RegisterExitHandler(nacl_io_exit_handler_t exit_handler,
59                            void* user_data);
60
61   virtual int pipe(int pipefds[2]);
62
63   // NaCl-only function to read resources specified in the NMF file.
64   virtual int open_resource(const char* file);
65
66   // KernelHandle and FD allocation and manipulation functions.
67   virtual int open(const char* path, int open_flags);
68   virtual int close(int fd);
69   virtual int dup(int fd);
70   virtual int dup2(int fd, int newfd);
71
72   virtual void exit(int status);
73
74   // Path related System calls handled by KernelProxy (not filesystem-specific)
75   virtual int chdir(const char* path);
76   virtual char* getcwd(char* buf, size_t size);
77   virtual char* getwd(char* buf);
78   virtual int mount(const char *source,
79                     const char *target,
80                     const char *filesystemtype,
81                     unsigned long mountflags,
82                     const void *data);
83   virtual int umount(const char *path);
84
85   // Stub system calls that don't do anything (yet), handled by KernelProxy.
86   virtual int chown(const char* path, uid_t owner, gid_t group);
87   virtual int fchown(int fd, uid_t owner, gid_t group);
88   virtual int lchown(const char* path, uid_t owner, gid_t group);
89   virtual int utime(const char* filename, const struct utimbuf* times);
90
91   // System calls that take a path as an argument: The kernel proxy will look
92   // for the Node associated to the path. To find the node, the kernel proxy
93   // calls the corresponding filesystem's GetNode() method. The corresponding
94   // method will be called. If the node cannot be found, errno is set and -1 is
95   // returned.
96   virtual int chmod(const char *path, mode_t mode);
97   virtual int mkdir(const char *path, mode_t mode);
98   virtual int rmdir(const char *path);
99   virtual int stat(const char *path, struct stat *buf);
100
101   // System calls that take a file descriptor as an argument:
102   // The kernel proxy will determine to which filesystem the file
103   // descriptor's corresponding file handle belongs.  The
104   // associated filesystem's function will be called.
105   virtual ssize_t read(int fd, void *buf, size_t nbyte);
106   virtual ssize_t write(int fd, const void *buf, size_t nbyte);
107
108   virtual int fchmod(int fd, int prot);
109   virtual int fcntl(int fd, int request, va_list args);
110   virtual int fstat(int fd, struct stat *buf);
111   virtual int getdents(int fd, void *buf, unsigned int count);
112   virtual int fchdir(int fd);
113   virtual int ftruncate(int fd, off_t length);
114   virtual int fsync(int fd);
115   virtual int fdatasync(int fd);
116   virtual int isatty(int fd);
117   virtual int ioctl(int fd, int request, va_list args);
118
119   // lseek() relies on the filesystem's Stat() to determine whether or not the
120   // file handle corresponding to fd is a directory
121   virtual off_t lseek(int fd, off_t offset, int whence);
122
123   // remove() uses the filesystem's GetNode() and Stat() to determine whether
124   // or not the path corresponds to a directory or a file. The filesystem's
125   // Rmdir() or Unlink() is called accordingly.
126   virtual int remove(const char* path);
127   // unlink() is a simple wrapper around the filesystem's Unlink function.
128   virtual int unlink(const char* path);
129   virtual int truncate(const char* path, off_t len);
130   virtual int lstat(const char* path, struct stat* buf);
131   virtual int rename(const char* path, const char* newpath);
132   // access() uses the Filesystem's Stat().
133   virtual int access(const char* path, int amode);
134   virtual int readlink(const char *path, char *buf, size_t count);
135   virtual int utimes(const char *filename, const struct timeval times[2]);
136
137   virtual int link(const char* oldpath, const char* newpath);
138   virtual int symlink(const char* oldpath, const char* newpath);
139
140   virtual void* mmap(void* addr,
141                      size_t length,
142                      int prot,
143                      int flags,
144                      int fd,
145                      size_t offset);
146   virtual int munmap(void* addr, size_t length);
147   virtual int tcflush(int fd, int queue_selector);
148   virtual int tcgetattr(int fd, struct termios* termios_p);
149   virtual int tcsetattr(int fd, int optional_actions,
150                            const struct termios *termios_p);
151
152   virtual int kill(pid_t pid, int sig);
153   virtual int sigaction(int signum, const struct sigaction* action,
154                         struct sigaction* oaction);
155
156 #ifdef PROVIDES_SOCKET_API
157   virtual int select(int nfds, fd_set* readfds, fd_set* writefds,
158                     fd_set* exceptfds, struct timeval* timeout);
159
160   virtual int poll(struct pollfd *fds, nfds_t nfds, int timeout);
161
162   // Socket support functions
163   virtual int accept(int fd, struct sockaddr* addr, socklen_t* len);
164   virtual int bind(int fd, const struct sockaddr* addr, socklen_t len);
165   virtual int connect(int fd, const struct sockaddr* addr, socklen_t len);
166   virtual struct hostent* gethostbyname(const char* name);
167   virtual void freeaddrinfo(struct addrinfo *res);
168   virtual int getaddrinfo(const char* node, const char* service,
169                           const struct addrinfo* hints,
170                           struct addrinfo** res);
171   virtual int getpeername(int fd, struct sockaddr* addr, socklen_t* len);
172   virtual int getsockname(int fd, struct sockaddr* addr, socklen_t* len);
173   virtual int getsockopt(int fd,
174                          int lvl,
175                          int optname,
176                          void* optval,
177                          socklen_t* len);
178   virtual int listen(int fd, int backlog);
179   virtual ssize_t recv(int fd,
180                        void* buf,
181                        size_t len,
182                        int flags);
183   virtual ssize_t recvfrom(int fd,
184                            void* buf,
185                            size_t len,
186                            int flags,
187                            struct sockaddr* addr,
188                            socklen_t* addrlen);
189   virtual ssize_t recvmsg(int fd, struct msghdr* msg, int flags);
190   virtual ssize_t send(int fd, const void* buf, size_t len, int flags);
191   virtual ssize_t sendto(int fd,
192                          const void* buf,
193                          size_t len,
194                          int flags,
195                          const struct sockaddr* addr,
196                          socklen_t addrlen);
197   virtual ssize_t sendmsg(int fd, const struct msghdr* msg, int flags);
198   virtual int setsockopt(int fd,
199                          int lvl,
200                          int optname,
201                          const void* optval,
202                          socklen_t len);
203   virtual int shutdown(int fd, int how);
204   virtual int socket(int domain, int type, int protocol);
205   virtual int socketpair(int domain, int type, int protocol, int* sv);
206 #endif  // PROVIDES_SOCKET_API
207
208  protected:
209   FsFactoryMap_t factories_;
210   sdk_util::ScopedRef<StreamFs> stream_mount_;
211   int dev_;
212   PepperInterface* ppapi_;
213   static KernelProxy *s_instance_;
214   struct sigaction sigwinch_handler_;
215   nacl_io_exit_handler_t exit_handler_;
216   void* exit_handler_user_data_;
217 #ifdef PROVIDES_SOCKET_API
218   HostResolver host_resolver_;
219 #endif
220
221 #ifdef PROVIDES_SOCKET_API
222   virtual int AcquireSocketHandle(int fd, ScopedKernelHandle* handle);
223 #endif
224
225   ScopedEventEmitter signal_emitter_;
226   DISALLOW_COPY_AND_ASSIGN(KernelProxy);
227 };
228
229 }  // namespace nacl_io
230
231 #endif  // LIBRARIES_NACL_IO_KERNEL_PROXY_H_