| 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_object.h" | 5 #include "nacl_io/kernel_object.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 #include <pthread.h> | 10 #include <pthread.h> |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <map> | 13 #include <map> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "nacl_io/filesystem.h" | 17 #include "nacl_io/filesystem.h" |
| 18 #include "nacl_io/kernel_handle.h" | 18 #include "nacl_io/kernel_handle.h" |
| 19 #include "nacl_io/node.h" | 19 #include "nacl_io/node.h" |
| 20 | 20 |
| 21 #include "sdk_util/auto_lock.h" | 21 #include "sdk_util/auto_lock.h" |
| 22 #include "sdk_util/ref_object.h" | 22 #include "sdk_util/ref_object.h" |
| 23 #include "sdk_util/scoped_ref.h" | 23 #include "sdk_util/scoped_ref.h" |
| 24 | 24 |
| 25 namespace nacl_io { | 25 namespace nacl_io { |
| 26 | 26 |
| 27 KernelObject::KernelObject() { cwd_ = "/"; } | 27 KernelObject::KernelObject() { |
| 28 cwd_ = "/"; |
| 29 } |
| 28 | 30 |
| 29 KernelObject::~KernelObject() {}; | 31 KernelObject::~KernelObject() {}; |
| 30 | 32 |
| 31 Error KernelObject::AttachFsAtPath(const ScopedFilesystem& fs, | 33 Error KernelObject::AttachFsAtPath(const ScopedFilesystem& fs, |
| 32 const std::string& path) { | 34 const std::string& path) { |
| 33 std::string abs_path = GetAbsParts(path).Join(); | 35 std::string abs_path = GetAbsParts(path).Join(); |
| 34 | 36 |
| 35 AUTO_LOCK(fs_lock_); | 37 AUTO_LOCK(fs_lock_); |
| 36 if (filesystems_.find(abs_path) != filesystems_.end()) | 38 if (filesystems_.find(abs_path) != filesystems_.end()) |
| 37 return EBUSY; | 39 return EBUSY; |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 return EBADF; | 178 return EBADF; |
| 177 | 179 |
| 178 Descriptor_t& desc = handle_map_[fd]; | 180 Descriptor_t& desc = handle_map_[fd]; |
| 179 if (!desc.handle) | 181 if (!desc.handle) |
| 180 return EBADF; | 182 return EBADF; |
| 181 | 183 |
| 182 *out_handle = desc.handle; | 184 *out_handle = desc.handle; |
| 183 return 0; | 185 return 0; |
| 184 } | 186 } |
| 185 | 187 |
| 186 Error KernelObject::AcquireHandleAndPath(int fd, ScopedKernelHandle* out_handle, | 188 Error KernelObject::AcquireHandleAndPath(int fd, |
| 187 std::string* out_path){ | 189 ScopedKernelHandle* out_handle, |
| 190 std::string* out_path) { |
| 188 out_handle->reset(NULL); | 191 out_handle->reset(NULL); |
| 189 | 192 |
| 190 AUTO_LOCK(handle_lock_); | 193 AUTO_LOCK(handle_lock_); |
| 191 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) | 194 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) |
| 192 return EBADF; | 195 return EBADF; |
| 193 | 196 |
| 194 Descriptor_t& desc = handle_map_[fd]; | 197 Descriptor_t& desc = handle_map_[fd]; |
| 195 if (!desc.handle) | 198 if (!desc.handle) |
| 196 return EBADF; | 199 return EBADF; |
| 197 | 200 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 216 free_fds_.pop_back(); | 219 free_fds_.pop_back(); |
| 217 handle_map_[id] = descriptor; | 220 handle_map_[id] = descriptor; |
| 218 } else { | 221 } else { |
| 219 id = handle_map_.size(); | 222 id = handle_map_.size(); |
| 220 handle_map_.push_back(descriptor); | 223 handle_map_.push_back(descriptor); |
| 221 } | 224 } |
| 222 | 225 |
| 223 return id; | 226 return id; |
| 224 } | 227 } |
| 225 | 228 |
| 226 void KernelObject::FreeAndReassignFD(int fd, const ScopedKernelHandle& handle, | 229 void KernelObject::FreeAndReassignFD(int fd, |
| 230 const ScopedKernelHandle& handle, |
| 227 const std::string& path) { | 231 const std::string& path) { |
| 228 if (NULL == handle) { | 232 if (NULL == handle) { |
| 229 FreeFD(fd); | 233 FreeFD(fd); |
| 230 } else { | 234 } else { |
| 231 AUTO_LOCK(handle_lock_); | 235 AUTO_LOCK(handle_lock_); |
| 232 | 236 |
| 233 // If the required FD is larger than the current set, grow the set | 237 // If the required FD is larger than the current set, grow the set |
| 234 if (fd >= (int)handle_map_.size()) | 238 if (fd >= (int)handle_map_.size()) |
| 235 handle_map_.resize(fd + 1); | 239 handle_map_.resize(fd + 1); |
| 236 | 240 |
| 237 // This path will be from an existing handle, and absolute. | 241 // This path will be from an existing handle, and absolute. |
| 238 handle_map_[fd] = Descriptor_t(handle, path); | 242 handle_map_[fd] = Descriptor_t(handle, path); |
| 239 } | 243 } |
| 240 } | 244 } |
| 241 | 245 |
| 242 void KernelObject::FreeFD(int fd) { | 246 void KernelObject::FreeFD(int fd) { |
| 243 AUTO_LOCK(handle_lock_); | 247 AUTO_LOCK(handle_lock_); |
| 244 | 248 |
| 245 handle_map_[fd].handle.reset(NULL); | 249 handle_map_[fd].handle.reset(NULL); |
| 246 free_fds_.push_back(fd); | 250 free_fds_.push_back(fd); |
| 247 | 251 |
| 248 // Force lower numbered FD to be available first. | 252 // Force lower numbered FD to be available first. |
| 249 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); | 253 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); |
| 250 } | 254 } |
| 251 | 255 |
| 252 } // namespace nacl_io | 256 } // namespace nacl_io |
| OLD | NEW |