Chromium Code Reviews| 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> |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 | 145 |
| 146 Error KernelObject::GetFDFlags(int fd, int* out_flags) { | 146 Error KernelObject::GetFDFlags(int fd, int* out_flags) { |
| 147 AUTO_LOCK(handle_lock_); | 147 AUTO_LOCK(handle_lock_); |
| 148 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) | 148 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) |
| 149 return EBADF; | 149 return EBADF; |
| 150 | 150 |
| 151 *out_flags = handle_map_[fd].flags; | 151 *out_flags = handle_map_[fd].flags; |
| 152 return 0; | 152 return 0; |
| 153 } | 153 } |
| 154 | 154 |
| 155 std::string KernelObject::GetFDPath(int fd) { | |
| 156 AUTO_LOCK(handle_lock_); | |
| 157 | |
| 158 FdPathMap_t::iterator iter = fd_paths_.find(fd); | |
| 159 if (iter == fd_paths_.end()) { | |
|
binji
2014/01/07 21:40:29
nit: no braces for single-line ifs
| |
| 160 return ""; | |
|
binji
2014/01/07 21:40:29
return std::string();
| |
| 161 } | |
| 162 | |
| 163 return iter->second; | |
| 164 } | |
| 165 | |
| 155 Error KernelObject::SetFDFlags(int fd, int flags) { | 166 Error KernelObject::SetFDFlags(int fd, int flags) { |
| 156 AUTO_LOCK(handle_lock_); | 167 AUTO_LOCK(handle_lock_); |
| 157 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) | 168 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) |
| 158 return EBADF; | 169 return EBADF; |
| 159 | 170 |
| 160 // Only setting of FD_CLOEXEC is supported. | 171 // Only setting of FD_CLOEXEC is supported. |
| 161 if (flags & ~FD_CLOEXEC) | 172 if (flags & ~FD_CLOEXEC) |
| 162 return EINVAL; | 173 return EINVAL; |
| 163 | 174 |
| 164 handle_map_[fd].flags = flags; | 175 handle_map_[fd].flags = flags; |
| 165 return 0; | 176 return 0; |
| 166 } | 177 } |
| 167 | 178 |
| 168 Error KernelObject::AcquireHandle(int fd, ScopedKernelHandle* out_handle) { | 179 Error KernelObject::AcquireHandle(int fd, ScopedKernelHandle* out_handle) { |
| 169 out_handle->reset(NULL); | 180 out_handle->reset(NULL); |
| 170 | 181 |
| 171 AUTO_LOCK(handle_lock_); | 182 AUTO_LOCK(handle_lock_); |
| 172 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) | 183 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) |
| 173 return EBADF; | 184 return EBADF; |
| 174 | 185 |
| 175 *out_handle = handle_map_[fd].handle; | 186 *out_handle = handle_map_[fd].handle; |
| 176 if (out_handle) | 187 if (out_handle) |
| 177 return 0; | 188 return 0; |
| 178 | 189 |
| 179 return EBADF; | 190 return EBADF; |
| 180 } | 191 } |
| 181 | 192 |
| 182 int KernelObject::AllocateFD(const ScopedKernelHandle& handle) { | 193 int KernelObject::AllocateFD(const ScopedKernelHandle& handle, |
| 194 std::string path) { | |
| 183 AUTO_LOCK(handle_lock_); | 195 AUTO_LOCK(handle_lock_); |
| 184 int id; | 196 int id; |
| 185 | 197 |
| 186 Descriptor_t descriptor(handle); | 198 Descriptor_t descriptor(handle); |
| 187 | 199 |
| 188 // If we can recycle and FD, use that first | 200 // If we can recycle and FD, use that first |
| 189 if (free_fds_.size()) { | 201 if (free_fds_.size()) { |
| 190 id = free_fds_.front(); | 202 id = free_fds_.front(); |
| 191 // Force lower numbered FD to be available first. | 203 // Force lower numbered FD to be available first. |
| 192 std::pop_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); | 204 std::pop_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); |
| 193 free_fds_.pop_back(); | 205 free_fds_.pop_back(); |
| 194 handle_map_[id] = descriptor; | 206 handle_map_[id] = descriptor; |
| 195 } else { | 207 } else { |
| 196 id = handle_map_.size(); | 208 id = handle_map_.size(); |
| 197 handle_map_.push_back(descriptor); | 209 handle_map_.push_back(descriptor); |
| 198 } | 210 } |
| 211 // With our final fd identifier, we insert into our fd_paths_ mapping as long | |
| 212 // as path is non-NULL. | |
| 213 if (!path.empty()) { | |
| 214 std::string abs_path = GetAbsParts(path).Join(); | |
| 215 fd_paths_[id] = abs_path; | |
|
binji
2014/01/07 21:40:29
nit: fs_paths_[id] = GetAbsParts(path).Join();
| |
| 216 } | |
| 199 return id; | 217 return id; |
| 200 } | 218 } |
| 201 | 219 |
| 202 void KernelObject::FreeAndReassignFD(int fd, const ScopedKernelHandle& handle) { | 220 void KernelObject::FreeAndReassignFD(int fd, const ScopedKernelHandle& handle) { |
| 203 if (NULL == handle) { | 221 if (NULL == handle) { |
| 204 FreeFD(fd); | 222 FreeFD(fd); |
| 205 } else { | 223 } else { |
| 206 AUTO_LOCK(handle_lock_); | 224 AUTO_LOCK(handle_lock_); |
| 207 | 225 |
| 208 // If the required FD is larger than the current set, grow the set | 226 // If the required FD is larger than the current set, grow the set |
| 209 if (fd >= (int)handle_map_.size()) | 227 if (fd >= (int)handle_map_.size()) |
| 210 handle_map_.resize(fd + 1); | 228 handle_map_.resize(fd + 1); |
| 211 | 229 |
| 212 handle_map_[fd] = Descriptor_t(handle); | 230 handle_map_[fd] = Descriptor_t(handle); |
|
binji
2014/01/07 21:40:29
assign to fd_paths_[fd] here too (used by dup2)
| |
| 213 } | 231 } |
| 214 } | 232 } |
| 215 | 233 |
| 216 void KernelObject::FreeFD(int fd) { | 234 void KernelObject::FreeFD(int fd) { |
| 217 AUTO_LOCK(handle_lock_); | 235 AUTO_LOCK(handle_lock_); |
| 218 | 236 |
| 219 handle_map_[fd].handle.reset(NULL); | 237 handle_map_[fd].handle.reset(NULL); |
| 220 free_fds_.push_back(fd); | 238 free_fds_.push_back(fd); |
| 221 | 239 |
| 222 // Force lower numbered FD to be available first. | 240 // Force lower numbered FD to be available first. |
| 223 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); | 241 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); |
| 242 // | |
| 243 // Remove the FD from the map of fd to absolute path | |
| 244 fd_paths_.erase(fd); | |
| 224 } | 245 } |
| 225 | 246 |
| 226 } // namespace nacl_io | 247 } // namespace nacl_io |
| OLD | NEW |