| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "nacl_io/kernel_handle.h" | 5 #include "nacl_io/kernel_handle.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <pthread.h> | 8 #include <pthread.h> |
| 9 | 9 |
| 10 #include "nacl_io/filesystem.h" | 10 #include "nacl_io/filesystem.h" |
| 11 #include "nacl_io/node.h" | 11 #include "nacl_io/node.h" |
| 12 #include "nacl_io/osunistd.h" | 12 #include "nacl_io/osunistd.h" |
| 13 #include "nacl_io/socket/socket_node.h" | 13 #include "nacl_io/socket/socket_node.h" |
| 14 | 14 |
| 15 #include "sdk_util/auto_lock.h" | 15 #include "sdk_util/auto_lock.h" |
| 16 | 16 |
| 17 namespace nacl_io { | 17 namespace nacl_io { |
| 18 | 18 |
| 19 // It is only legal to construct a handle while the kernel lock is held. | 19 // It is only legal to construct a handle while the kernel lock is held. |
| 20 KernelHandle::KernelHandle() : filesystem_(NULL), node_(NULL) {} | 20 KernelHandle::KernelHandle() : filesystem_(NULL), node_(NULL) { |
| 21 } |
| 21 | 22 |
| 22 KernelHandle::KernelHandle(const ScopedFilesystem& fs, const ScopedNode& node) | 23 KernelHandle::KernelHandle(const ScopedFilesystem& fs, const ScopedNode& node) |
| 23 : filesystem_(fs), node_(node) {} | 24 : filesystem_(fs), node_(node) { |
| 25 } |
| 24 | 26 |
| 25 KernelHandle::~KernelHandle() { | 27 KernelHandle::~KernelHandle() { |
| 26 // Force release order for cases where filesystem_ is not ref'd by mounting. | 28 // Force release order for cases where filesystem_ is not ref'd by mounting. |
| 27 node_.reset(NULL); | 29 node_.reset(NULL); |
| 28 filesystem_.reset(NULL); | 30 filesystem_.reset(NULL); |
| 29 } | 31 } |
| 30 | 32 |
| 31 // Returns the SocketNode* if this node is a socket. | 33 // Returns the SocketNode* if this node is a socket. |
| 32 SocketNode* KernelHandle::socket_node() { | 34 SocketNode* KernelHandle::socket_node() { |
| 33 if (node_.get() && node_->IsaSock()) | 35 if (node_.get() && node_->IsaSock()) |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 const int mutable_flags = O_ASYNC | O_NONBLOCK; | 148 const int mutable_flags = O_ASYNC | O_NONBLOCK; |
| 147 flags &= mutable_flags; | 149 flags &= mutable_flags; |
| 148 handle_attr_.flags &= ~mutable_flags; | 150 handle_attr_.flags &= ~mutable_flags; |
| 149 handle_attr_.flags |= flags; | 151 handle_attr_.flags |= flags; |
| 150 return 0; | 152 return 0; |
| 151 } | 153 } |
| 152 } | 154 } |
| 153 return ENOSYS; | 155 return ENOSYS; |
| 154 } | 156 } |
| 155 | 157 |
| 156 Error KernelHandle::Accept(PP_Resource* new_sock, struct sockaddr* addr, | 158 Error KernelHandle::Accept(PP_Resource* new_sock, |
| 159 struct sockaddr* addr, |
| 157 socklen_t* len) { | 160 socklen_t* len) { |
| 158 SocketNode* sock = socket_node(); | 161 SocketNode* sock = socket_node(); |
| 159 if (!sock) | 162 if (!sock) |
| 160 return ENOTSOCK; | 163 return ENOTSOCK; |
| 161 | 164 |
| 162 AUTO_LOCK(handle_lock_); | 165 AUTO_LOCK(handle_lock_); |
| 163 return sock->Accept(handle_attr_, new_sock, addr, len); | 166 return sock->Accept(handle_attr_, new_sock, addr, len); |
| 164 } | 167 } |
| 165 | 168 |
| 166 Error KernelHandle::Connect(const struct sockaddr* addr, socklen_t len) { | 169 Error KernelHandle::Connect(const struct sockaddr* addr, socklen_t len) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 193 if (!sock) | 196 if (!sock) |
| 194 return ENOTSOCK; | 197 return ENOTSOCK; |
| 195 if (OpenMode() == O_WRONLY) | 198 if (OpenMode() == O_WRONLY) |
| 196 return EACCES; | 199 return EACCES; |
| 197 | 200 |
| 198 AUTO_LOCK(handle_lock_); | 201 AUTO_LOCK(handle_lock_); |
| 199 return sock->RecvFrom(handle_attr_, buf, len, flags, src_addr, addrlen, | 202 return sock->RecvFrom(handle_attr_, buf, len, flags, src_addr, addrlen, |
| 200 out_len); | 203 out_len); |
| 201 } | 204 } |
| 202 | 205 |
| 203 Error KernelHandle::Send(const void* buf, | 206 Error KernelHandle::Send(const void* buf, size_t len, int flags, int* out_len) { |
| 204 size_t len, | |
| 205 int flags, | |
| 206 int* out_len) { | |
| 207 SocketNode* sock = socket_node(); | 207 SocketNode* sock = socket_node(); |
| 208 if (!sock) | 208 if (!sock) |
| 209 return ENOTSOCK; | 209 return ENOTSOCK; |
| 210 if (OpenMode() == O_RDONLY) | 210 if (OpenMode() == O_RDONLY) |
| 211 return EACCES; | 211 return EACCES; |
| 212 | 212 |
| 213 AUTO_LOCK(handle_lock_); | 213 AUTO_LOCK(handle_lock_); |
| 214 return sock->Send(handle_attr_, buf, len, flags, out_len); | 214 return sock->Send(handle_attr_, buf, len, flags, out_len); |
| 215 } | 215 } |
| 216 | 216 |
| 217 Error KernelHandle::SendTo(const void* buf, | 217 Error KernelHandle::SendTo(const void* buf, |
| 218 size_t len, | 218 size_t len, |
| 219 int flags, | 219 int flags, |
| 220 const struct sockaddr* dest_addr, | 220 const struct sockaddr* dest_addr, |
| 221 socklen_t addrlen, | 221 socklen_t addrlen, |
| 222 int* out_len) { | 222 int* out_len) { |
| 223 SocketNode* sock = socket_node(); | 223 SocketNode* sock = socket_node(); |
| 224 if (!sock) | 224 if (!sock) |
| 225 return ENOTSOCK; | 225 return ENOTSOCK; |
| 226 if (OpenMode() == O_RDONLY) | 226 if (OpenMode() == O_RDONLY) |
| 227 return EACCES; | 227 return EACCES; |
| 228 | 228 |
| 229 AUTO_LOCK(handle_lock_); | 229 AUTO_LOCK(handle_lock_); |
| 230 return sock->SendTo(handle_attr_, buf, len, flags, dest_addr, addrlen, | 230 return sock->SendTo(handle_attr_, buf, len, flags, dest_addr, addrlen, |
| 231 out_len); | 231 out_len); |
| 232 } | 232 } |
| 233 | 233 |
| 234 } // namespace nacl_io | 234 } // namespace nacl_io |
| OLD | NEW |