OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 #ifndef PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_KERNELPROXY_H_ | 6 #ifndef PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_KERNELPROXY_H_ |
7 #define PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_KERNELPROXY_H_ | 7 #define PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_KERNELPROXY_H_ |
8 | 8 |
9 #include <errno.h> | 9 #include <errno.h> |
10 #ifdef __GLIBC__ | 10 #include <nacl-mounts/base/Mount.h> |
11 #include <poll.h> | 11 #include <nacl-mounts/base/MountManager.h> |
| 12 #include <nacl-mounts/net/BaseSocketSubSystem.h> |
| 13 #ifdef __GLIBC |
| 14 #include <nacl-mounts/net/newlib_compat.h> |
| 15 #endif |
| 16 #include <nacl-mounts/util/Path.h> |
| 17 #include <nacl-mounts/util/PthreadHelpers.h> |
| 18 #include <nacl-mounts/util/SlotAllocator.h> |
| 19 #ifndef __GLIBC__ |
| 20 #include <netdb.h> |
12 #endif | 21 #endif |
13 #include <pthread.h> | 22 #include <pthread.h> |
14 #ifdef __GLIBC__ | |
15 #include <sys/epoll.h> | |
16 #endif | |
17 #include <sys/stat.h> | 23 #include <sys/stat.h> |
18 #include <string> | 24 #include <string> |
19 #include "../base/Mount.h" | |
20 #include "../base/MountManager.h" | |
21 #include "../util/Path.h" | |
22 #include "../util/SimpleAutoLock.h" | |
23 #include "../util/SlotAllocator.h" | |
24 | 25 |
25 // KernelProxy handles all of the system calls. System calls are either | 26 // KernelProxy handles all of the system calls. System calls are either |
26 // (1) handled entirely by the KernelProxy, (2) processed by the | 27 // (1) handled entirely by the KernelProxy, (2) processed by the |
27 // KernelProxy and then passed to a Mount, or (3) proccessed by the | 28 // KernelProxy and then passed to a Mount, or (3) proccessed by the |
28 // KernelProxy using other system calls implemented by the Mount. | 29 // KernelProxy using other system calls implemented by the Mount. |
29 class KernelProxy { | 30 class KernelProxy { |
30 public: | 31 public: |
31 virtual ~KernelProxy() {} | 32 virtual ~KernelProxy() {} |
32 | 33 |
33 // Obtain the singleton instance of the kernel proxy. If no instance | 34 // Obtain the singleton instance of the kernel proxy. If no instance |
34 // has been instantiated, one will be instantiated and returned. | 35 // has been instantiated, one will be instantiated and returned. |
35 static KernelProxy *KPInstance(); | 36 static KernelProxy *KPInstance(); |
| 37 // Set socket subsystem reference (not in constructor because it needs to be |
| 38 // created separately and only if you need sockets in your app) |
| 39 void SetSocketSubSystem(BaseSocketSubSystem* bss); |
36 | 40 |
37 // System calls handled by KernelProxy (not mount-specific) | 41 // System calls handled by KernelProxy (not mount-specific) |
38 int chdir(const std::string& path); | 42 int chdir(const std::string& path); |
39 bool getcwd(std::string *buf, size_t size); | 43 bool getcwd(std::string *buf, size_t size); |
40 bool getwd(std::string *buf); | 44 bool getwd(std::string *buf); |
41 int dup(int oldfd); | 45 int dup(int oldfd); |
| 46 int dup2(int oldfd, int newfd); |
42 | 47 |
43 // System calls that take a path as an argument: | 48 // System calls that take a path as an argument: |
44 // The kernel proxy will look for the Node associated to the path. To | 49 // The kernel proxy will look for the Node associated to the path. To |
45 // find the node, the kernel proxy calls the corresponding mounts GetNode() | 50 // find the node, the kernel proxy calls the corresponding mounts GetNode() |
46 // method. The corresponding method will be called. If the node | 51 // method. The corresponding method will be called. If the node |
47 // cannot be found, errno is set and -1 is returned. | 52 // cannot be found, errno is set and -1 is returned. |
48 int chmod(const std::string& path, mode_t mode); | 53 int chmod(const std::string& path, mode_t mode); |
49 int stat(const std::string& path, struct stat *buf); | 54 int stat(const std::string& path, struct stat *buf); |
50 int mkdir(const std::string& path, mode_t mode); | 55 int mkdir(const std::string& path, mode_t mode); |
51 int rmdir(const std::string& path); | 56 int rmdir(const std::string& path); |
(...skipping 29 matching lines...) Expand all Loading... |
81 int unlink(const std::string& path); | 86 int unlink(const std::string& path); |
82 // access() uses the Mount's Stat(). | 87 // access() uses the Mount's Stat(). |
83 int access(const std::string& path, int amode); | 88 int access(const std::string& path, int amode); |
84 | 89 |
85 // TODO(arbenson): implement the following system calls | 90 // TODO(arbenson): implement the following system calls |
86 int ioctl(int fd, unsigned long request); | 91 int ioctl(int fd, unsigned long request); |
87 int link(const std::string& path1, const std::string& path2); | 92 int link(const std::string& path1, const std::string& path2); |
88 int symlink(const std::string& path1, const std::string& path2); | 93 int symlink(const std::string& path1, const std::string& path2); |
89 int kill(pid_t pid, int sig); | 94 int kill(pid_t pid, int sig); |
90 | 95 |
91 #ifdef __GLIBC__ | |
92 // TODO(vissi): implement the following system calls | 96 // TODO(vissi): implement the following system calls |
93 int socket(int domain, int type, int protocol); | 97 int socket(int domain, int type, int protocol); |
94 int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); | 98 int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); |
95 int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); | 99 int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
96 int listen(int sockfd, int backlog); | 100 int listen(int sockfd, int backlog); |
97 int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); | 101 int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
98 int send(int sockfd, const void *buf, size_t len, int flags); | 102 int send(int sockfd, const void *buf, size_t len, int flags); |
99 int sendmsg(int sockfd, const struct msghdr *msg, int flags); | 103 int sendmsg(int sockfd, const struct msghdr *msg, int flags); |
100 int sendto(int sockfd, const void *buf, size_t len, int flags, | 104 int sendto(int sockfd, const void *buf, size_t len, int flags, |
101 const struct sockaddr *dest_addr, socklen_t addrlen); | 105 const struct sockaddr *dest_addr, socklen_t addrlen); |
102 int recv(int sockfd, void *buf, size_t len, int flags); | 106 int recv(int sockfd, void *buf, size_t len, int flags); |
103 int recvmsg(int sockfd, struct msghdr *msg, int flags); | 107 int recvmsg(int sockfd, struct msghdr *msg, int flags); |
104 int recvfrom(int sockfd, void *buf, size_t len, int flags, | 108 int recvfrom(int sockfd, void *buf, size_t len, int flags, |
105 struct sockaddr *dest_addr, socklen_t* addrlen); | 109 struct sockaddr *dest_addr, socklen_t* addrlen); |
106 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, | 110 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
107 const struct timeval *timeout); | 111 const struct timeval *timeout); |
108 int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, | 112 int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
109 const struct timeval *timeout, void* sigmask); | 113 const struct timeval *timeout, void* sigmask); |
110 int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen); | 114 int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen); |
111 int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen); | 115 int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen); |
| 116 struct hostent* gethostbyname(const char* name); |
| 117 int getaddrinfo(const char* hostname, const char* servname, |
| 118 const struct addrinfo* hints, struct addrinfo** res); |
| 119 void freeaddrinfo(struct addrinfo* ai); |
| 120 int getnameinfo(const struct sockaddr *sa, socklen_t salen, |
| 121 char* host, socklen_t hostlen, |
| 122 char* serv, socklen_t servlen, unsigned int flags); |
112 int getsockopt(int sockfd, int level, int optname, void *optval, | 123 int getsockopt(int sockfd, int level, int optname, void *optval, |
113 socklen_t* optlen); | 124 socklen_t* optlen); |
114 int setsockopt(int sockfd, int level, int optname, const void *optval, | 125 int setsockopt(int sockfd, int level, int optname, const void *optval, |
115 socklen_t optlen); | 126 socklen_t optlen); |
116 int shutdown(int sockfd, int how); | 127 int shutdown(int sockfd, int how); |
117 int epoll_create(int size); | |
118 int epoll_create1(int flags); | |
119 int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); | |
120 int epoll_wait(int epfd, struct epoll_event *events, int maxevents, | |
121 int timeout); | |
122 int epoll_pwait(int epfd, struct epoll_event *events, int maxevents, | |
123 int timeout, const sigset_t *sigmask, size_t sigset_size); | |
124 int socketpair(int domain, int type, int protocol, int sv[2]); | 128 int socketpair(int domain, int type, int protocol, int sv[2]); |
125 int poll(struct pollfd *fds, nfds_t nfds, int timeout); | |
126 int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, | |
127 const sigset_t *sigmask, size_t sigset_size); | |
128 #endif | |
129 MountManager *mm() { return &mm_; } | 129 MountManager *mm() { return &mm_; } |
130 | 130 |
| 131 int AddSocket(Socket* stream); |
| 132 void RemoveSocket(int fd); |
| 133 |
| 134 int IsReady(int nfds, fd_set* fds, bool (Socket::*is_ready)(), |
| 135 bool apply); |
| 136 Cond& select_cond() { return select_cond_; } |
| 137 Mutex& select_mutex() { return select_mutex_; } |
131 private: | 138 private: |
132 struct FileDescriptor { | 139 struct FileDescriptor { |
133 // An index in open_files_ table | 140 // An index in open_files_ table |
134 int handle; | 141 int handle; |
135 }; | 142 }; |
136 | 143 |
| 144 // used for select() signals |
| 145 Cond select_cond_; |
| 146 Mutex select_mutex_; |
| 147 |
| 148 // if mount == NULL, it's a socket, stream == NULL is a consistent state |
| 149 // for socket that was just opened |
137 struct FileHandle { | 150 struct FileHandle { |
138 Mount *mount; | 151 Mount *mount; |
| 152 Socket* stream; |
139 ino_t node; | 153 ino_t node; |
140 off_t offset; | 154 off_t offset; |
141 int flags; | 155 int flags; |
142 int use_count; | 156 int use_count; |
143 pthread_mutex_t lock; | 157 pthread_mutex_t lock; |
144 }; | 158 }; |
| 159 FileHandle* GetFileHandle(int fd); |
145 | 160 |
| 161 BaseSocketSubSystem* socket_subsystem_; |
146 Path cwd_; | 162 Path cwd_; |
147 int max_path_len_; | 163 int max_path_len_; |
148 MountManager mm_; | 164 MountManager mm_; |
149 pthread_mutex_t kp_lock_; | 165 pthread_mutex_t kp_lock_; |
150 static KernelProxy *kp_instance_; | 166 static KernelProxy *kp_instance_; |
151 | 167 |
152 SlotAllocator<FileDescriptor> fds_; | 168 SlotAllocator<FileDescriptor> fds_; |
153 SlotAllocator<FileHandle> open_files_; | 169 SlotAllocator<FileHandle> open_files_; |
154 | 170 |
155 FileHandle *GetFileHandle(int fd); | |
156 int OpenHandle(Mount *mount, const std::string& path, int oflag, mode_t mode); | 171 int OpenHandle(Mount *mount, const std::string& path, int oflag, mode_t mode); |
157 | 172 |
158 KernelProxy(); | 173 KernelProxy(); |
159 static void Instantiate(); | 174 static void Instantiate(); |
160 }; | 175 }; |
161 | 176 |
162 #endif // PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_KERNELPROXY_H_ | 177 #endif // PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_KERNELPROXY_H_ |
OLD | NEW |