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.
5 #include "nacl_io/kernel_object.h"
17 #include "nacl_io/filesystem.h"
18 #include "nacl_io/kernel_handle.h"
19 #include "nacl_io/node.h"
21 #include "sdk_util/auto_lock.h"
22 #include "sdk_util/ref_object.h"
23 #include "sdk_util/scoped_ref.h"
27 KernelObject::KernelObject() {
31 KernelObject::~KernelObject() {};
33 Error KernelObject::AttachFsAtPath(const ScopedFilesystem& fs,
34 const std::string& path) {
35 std::string abs_path = GetAbsParts(path).Join();
38 if (filesystems_.find(abs_path) != filesystems_.end())
41 filesystems_[abs_path] = fs;
45 Error KernelObject::DetachFsAtPath(const std::string& path,
46 ScopedFilesystem* out_fs) {
47 std::string abs_path = GetAbsParts(path).Join();
50 FsMap_t::iterator it = filesystems_.find(abs_path);
51 if (filesystems_.end() == it)
54 // It is only legal to unmount if there are no open references
55 if (it->second->RefCount() != 1)
60 filesystems_.erase(it);
64 // Uses longest prefix to find the filesystem for the give path, then
65 // acquires the filesystem and returns it with a relative path.
66 Error KernelObject::AcquireFsAndRelPath(const std::string& path,
67 ScopedFilesystem* out_fs,
69 Path abs_parts = GetAbsParts(path);
76 // Find longest prefix
77 size_t max = abs_parts.Size();
78 for (size_t len = 0; len < abs_parts.Size(); len++) {
79 FsMap_t::iterator it = filesystems_.find(abs_parts.Range(0, max - len));
80 if (it != filesystems_.end()) {
82 rel_parts->Append(abs_parts.Range(max - len, max));
92 // Given a path, acquire the associated filesystem and node, creating the
93 // node if needed based on the provided flags.
94 Error KernelObject::AcquireFsAndNode(const std::string& path,
96 ScopedFilesystem* out_fs,
97 ScopedNode* out_node) {
100 out_node->reset(NULL);
101 Error error = AcquireFsAndRelPath(path, out_fs, &rel_parts);
105 error = (*out_fs)->Open(rel_parts, oflags, out_node);
112 Path KernelObject::GetAbsParts(const std::string& path) {
113 AUTO_LOCK(cwd_lock_);
115 Path abs_parts(cwd_);
116 if (path[0] == '/') {
120 abs_parts.Append(path);
126 std::string KernelObject::GetCWD() {
127 AUTO_LOCK(cwd_lock_);
128 std::string out = cwd_;
133 Error KernelObject::SetCWD(const std::string& path) {
134 std::string abs_path = GetAbsParts(path).Join();
139 Error error = AcquireFsAndNode(abs_path, O_RDONLY, &fs, &node);
143 if ((node->GetType() & S_IFDIR) == 0)
146 AUTO_LOCK(cwd_lock_);
151 Error KernelObject::GetFDFlags(int fd, int* out_flags) {
152 AUTO_LOCK(handle_lock_);
153 if (fd < 0 || fd >= static_cast<int>(handle_map_.size()))
156 *out_flags = handle_map_[fd].flags;
160 Error KernelObject::SetFDFlags(int fd, int flags) {
161 AUTO_LOCK(handle_lock_);
162 if (fd < 0 || fd >= static_cast<int>(handle_map_.size()))
165 // Only setting of FD_CLOEXEC is supported.
166 if (flags & ~FD_CLOEXEC)
169 handle_map_[fd].flags = flags;
173 Error KernelObject::AcquireHandle(int fd, ScopedKernelHandle* out_handle) {
174 out_handle->reset(NULL);
176 AUTO_LOCK(handle_lock_);
177 if (fd < 0 || fd >= static_cast<int>(handle_map_.size()))
180 Descriptor_t& desc = handle_map_[fd];
184 *out_handle = desc.handle;
188 Error KernelObject::AcquireHandleAndPath(int fd,
189 ScopedKernelHandle* out_handle,
190 std::string* out_path) {
191 out_handle->reset(NULL);
193 AUTO_LOCK(handle_lock_);
194 if (fd < 0 || fd >= static_cast<int>(handle_map_.size()))
197 Descriptor_t& desc = handle_map_[fd];
201 *out_handle = desc.handle;
202 *out_path = desc.path;
206 int KernelObject::AllocateFD(const ScopedKernelHandle& handle,
207 const std::string& path) {
208 AUTO_LOCK(handle_lock_);
211 std::string abs_path = GetAbsParts(path).Join();
212 Descriptor_t descriptor(handle, abs_path);
214 // If we can recycle and FD, use that first
215 if (free_fds_.size()) {
216 id = free_fds_.front();
217 // Force lower numbered FD to be available first.
218 std::pop_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>());
219 free_fds_.pop_back();
220 handle_map_[id] = descriptor;
222 id = handle_map_.size();
223 handle_map_.push_back(descriptor);
229 void KernelObject::FreeAndReassignFD(int fd,
230 const ScopedKernelHandle& handle,
231 const std::string& path) {
232 if (NULL == handle) {
235 AUTO_LOCK(handle_lock_);
237 // If the required FD is larger than the current set, grow the set
238 if (fd >= (int)handle_map_.size())
239 handle_map_.resize(fd + 1);
241 // This path will be from an existing handle, and absolute.
242 handle_map_[fd] = Descriptor_t(handle, path);
246 void KernelObject::FreeFD(int fd) {
247 AUTO_LOCK(handle_lock_);
249 handle_map_[fd].handle.reset(NULL);
250 free_fds_.push_back(fd);
252 // Force lower numbered FD to be available first.
253 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>());
256 } // namespace nacl_io