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 #ifdef __GLIBC__ |
11 #include <netdb.h> | |
11 #include <poll.h> | 12 #include <poll.h> |
12 #endif | 13 #endif |
13 #include <pthread.h> | 14 #include <pthread.h> |
14 #ifdef __GLIBC__ | 15 #ifdef __GLIBC__ |
15 #include <sys/epoll.h> | 16 #include <sys/epoll.h> |
16 #endif | 17 #endif |
17 #include <sys/stat.h> | 18 #include <sys/stat.h> |
18 #include <string> | 19 #include <string> |
19 #include "../base/Mount.h" | 20 #include "../base/Mount.h" |
20 #include "../base/MountManager.h" | 21 #include "../base/MountManager.h" |
22 #include "../net/BaseSocketSubSystem.h" | |
21 #include "../util/Path.h" | 23 #include "../util/Path.h" |
22 #include "../util/SimpleAutoLock.h" | 24 #include "../util/PthreadHelpers.h" |
23 #include "../util/SlotAllocator.h" | 25 #include "../util/SlotAllocator.h" |
24 | 26 |
25 // KernelProxy handles all of the system calls. System calls are either | 27 // KernelProxy handles all of the system calls. System calls are either |
26 // (1) handled entirely by the KernelProxy, (2) processed by the | 28 // (1) handled entirely by the KernelProxy, (2) processed by the |
27 // KernelProxy and then passed to a Mount, or (3) proccessed by the | 29 // KernelProxy and then passed to a Mount, or (3) proccessed by the |
28 // KernelProxy using other system calls implemented by the Mount. | 30 // KernelProxy using other system calls implemented by the Mount. |
29 class KernelProxy { | 31 class KernelProxy { |
30 public: | 32 public: |
31 virtual ~KernelProxy() {} | 33 virtual ~KernelProxy() {} |
32 | 34 |
33 // Obtain the singleton instance of the kernel proxy. If no instance | 35 // Obtain the singleton instance of the kernel proxy. If no instance |
34 // has been instantiated, one will be instantiated and returned. | 36 // has been instantiated, one will be instantiated and returned. |
35 static KernelProxy *KPInstance(); | 37 static KernelProxy *KPInstance(); |
38 #ifdef __GLIBC__ | |
39 // Set socket subsystem reference (not in constructor because it needs to be | |
40 // created separately and only if you need sockets in your app) | |
41 void SetSocketSubSystem(BaseSocketSubSystem* bss); | |
42 #endif | |
36 | 43 |
37 // System calls handled by KernelProxy (not mount-specific) | 44 // System calls handled by KernelProxy (not mount-specific) |
38 int chdir(const std::string& path); | 45 int chdir(const std::string& path); |
39 bool getcwd(std::string *buf, size_t size); | 46 bool getcwd(std::string *buf, size_t size); |
40 bool getwd(std::string *buf); | 47 bool getwd(std::string *buf); |
41 int dup(int oldfd); | 48 int dup(int oldfd); |
49 int dup2(int oldfd, int newfd); | |
42 | 50 |
43 // System calls that take a path as an argument: | 51 // System calls that take a path as an argument: |
44 // The kernel proxy will look for the Node associated to the path. To | 52 // 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() | 53 // find the node, the kernel proxy calls the corresponding mounts GetNode() |
46 // method. The corresponding method will be called. If the node | 54 // method. The corresponding method will be called. If the node |
47 // cannot be found, errno is set and -1 is returned. | 55 // cannot be found, errno is set and -1 is returned. |
48 int chmod(const std::string& path, mode_t mode); | 56 int chmod(const std::string& path, mode_t mode); |
49 int stat(const std::string& path, struct stat *buf); | 57 int stat(const std::string& path, struct stat *buf); |
50 int mkdir(const std::string& path, mode_t mode); | 58 int mkdir(const std::string& path, mode_t mode); |
51 int rmdir(const std::string& path); | 59 int rmdir(const std::string& path); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 int recv(int sockfd, void *buf, size_t len, int flags); | 110 int recv(int sockfd, void *buf, size_t len, int flags); |
103 int recvmsg(int sockfd, struct msghdr *msg, int flags); | 111 int recvmsg(int sockfd, struct msghdr *msg, int flags); |
104 int recvfrom(int sockfd, void *buf, size_t len, int flags, | 112 int recvfrom(int sockfd, void *buf, size_t len, int flags, |
105 struct sockaddr *dest_addr, socklen_t* addrlen); | 113 struct sockaddr *dest_addr, socklen_t* addrlen); |
106 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, | 114 int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
107 const struct timeval *timeout); | 115 const struct timeval *timeout); |
108 int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, | 116 int pselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, |
109 const struct timeval *timeout, void* sigmask); | 117 const struct timeval *timeout, void* sigmask); |
110 int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen); | 118 int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen); |
111 int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen); | 119 int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen); |
120 struct hostent* gethostbyname(const char* name); | |
121 int getaddrinfo(const char* hostname, const char* servname, | |
122 const struct addrinfo* hints, struct addrinfo** res); | |
123 void freeaddrinfo(struct addrinfo* ai); | |
124 int getnameinfo(const struct sockaddr *sa, socklen_t salen, | |
125 char *host, socklen_t hostlen, | |
126 char *serv, socklen_t servlen, unsigned int flags); | |
112 int getsockopt(int sockfd, int level, int optname, void *optval, | 127 int getsockopt(int sockfd, int level, int optname, void *optval, |
113 socklen_t* optlen); | 128 socklen_t* optlen); |
114 int setsockopt(int sockfd, int level, int optname, const void *optval, | 129 int setsockopt(int sockfd, int level, int optname, const void *optval, |
115 socklen_t optlen); | 130 socklen_t optlen); |
116 int shutdown(int sockfd, int how); | 131 int shutdown(int sockfd, int how); |
117 int epoll_create(int size); | 132 int epoll_create(int size); |
118 int epoll_create1(int flags); | 133 int epoll_create1(int flags); |
119 int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); | 134 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, | 135 int epoll_wait(int epfd, struct epoll_event *events, int maxevents, |
121 int timeout); | 136 int timeout); |
122 int epoll_pwait(int epfd, struct epoll_event *events, int maxevents, | 137 int epoll_pwait(int epfd, struct epoll_event *events, int maxevents, |
123 int timeout, const sigset_t *sigmask, size_t sigset_size); | 138 int timeout, const sigset_t *sigmask, size_t sigset_size); |
124 int socketpair(int domain, int type, int protocol, int sv[2]); | 139 int socketpair(int domain, int type, int protocol, int sv[2]); |
125 int poll(struct pollfd *fds, nfds_t nfds, int timeout); | 140 int poll(struct pollfd *fds, nfds_t nfds, int timeout); |
126 int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, | 141 int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, |
127 const sigset_t *sigmask, size_t sigset_size); | 142 const sigset_t *sigmask, size_t sigset_size); |
128 #endif | 143 #endif |
129 MountManager *mm() { return &mm_; } | 144 MountManager *mm() { return &mm_; } |
130 | 145 |
146 #ifdef __GLIBC__ | |
147 int AddSocket(Socket* stream); | |
148 void RemoveSocket(int fd); | |
149 | |
150 int IsReady(int nfds, fd_set* fds, bool (Socket::*is_ready)(), | |
151 bool apply); | |
152 Cond& select_cond() { return select_cond_; } | |
153 Mutex& select_mutex() { return select_mutex_; } | |
154 #endif // __GLIBC__ | |
131 private: | 155 private: |
132 struct FileDescriptor { | 156 struct FileDescriptor { |
133 // An index in open_files_ table | 157 // An index in open_files_ table |
134 int handle; | 158 int handle; |
135 }; | 159 }; |
136 | 160 |
161 // used for select() signals | |
162 Cond select_cond_; | |
163 Mutex select_mutex_; | |
164 | |
137 struct FileHandle { | 165 struct FileHandle { |
138 Mount *mount; | 166 Mount *mount; |
167 #ifdef __GLIBC__ | |
168 Socket* stream; | |
Evgeniy Stepanov
2012/06/04 10:27:20
add a comment about types of handles (file/socket/
vissi
2012/06/04 12:43:37
Done.
Evgeniy Stepanov
2012/06/04 13:10:18
where?
vissi
2012/06/04 13:26:35
Forgot to include it, please, check now.
| |
169 #endif | |
139 ino_t node; | 170 ino_t node; |
140 off_t offset; | 171 off_t offset; |
141 int flags; | 172 int flags; |
142 int use_count; | 173 int use_count; |
143 pthread_mutex_t lock; | 174 pthread_mutex_t lock; |
144 }; | 175 }; |
176 FileHandle* GetFileHandle(int fd); | |
145 | 177 |
178 #ifdef __GLIBC__ | |
179 BaseSocketSubSystem* socket_subsystem_; | |
180 #endif | |
146 Path cwd_; | 181 Path cwd_; |
147 int max_path_len_; | 182 int max_path_len_; |
148 MountManager mm_; | 183 MountManager mm_; |
149 pthread_mutex_t kp_lock_; | 184 pthread_mutex_t kp_lock_; |
150 static KernelProxy *kp_instance_; | 185 static KernelProxy *kp_instance_; |
151 | 186 |
152 SlotAllocator<FileDescriptor> fds_; | 187 SlotAllocator<FileDescriptor> fds_; |
153 SlotAllocator<FileHandle> open_files_; | 188 SlotAllocator<FileHandle> open_files_; |
154 | 189 |
155 FileHandle *GetFileHandle(int fd); | |
156 int OpenHandle(Mount *mount, const std::string& path, int oflag, mode_t mode); | 190 int OpenHandle(Mount *mount, const std::string& path, int oflag, mode_t mode); |
157 | 191 |
158 KernelProxy(); | 192 KernelProxy(); |
159 static void Instantiate(); | 193 static void Instantiate(); |
160 }; | 194 }; |
161 | 195 |
162 #endif // PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_KERNELPROXY_H_ | 196 #endif // PACKAGES_LIBRARIES_NACL_MOUNTS_BASE_KERNELPROXY_H_ |
OLD | NEW |