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. 4 5 #ifndef LIBRARIES_NACL_IO_KERNEL_OBJECT_H_ 6 #define LIBRARIES_NACL_IO_KERNEL_OBJECT_H_ 7 8 #include <pthread.h> 9 10 #include <map> 11 #include <string> 12 #include <vector> 13 14 #include "nacl_io/error.h" 15 #include "nacl_io/filesystem.h" 16 #include "nacl_io/kernel_handle.h" 17 #include "nacl_io/node.h" 18 #include "nacl_io/path.h" 19 20 #include "sdk_util/macros.h" 21 #include "sdk_util/simple_lock.h" 22 23 namespace nacl_io { 24 25 // KernelObject provides basic functionality for threadsafe access to kernel 26 // objects such as the CWD, mount points, file descriptors and file handles. 27 // All Kernel locks are private to ensure the lock order. 28 // 29 // All calls are assumed to be a relative path. 30 class KernelObject { 31 public: 32 struct Descriptor_t { 33 Descriptor_t() : flags(0) {} 34 explicit Descriptor_t(const ScopedKernelHandle& h, 35 const std::string& open_path) 36 : handle(h), flags(0), path(open_path) {} 37 38 ScopedKernelHandle handle; 39 int flags; 40 std::string path; 41 }; 42 typedef std::vector<Descriptor_t> HandleMap_t; 43 typedef std::map<std::string, ScopedFilesystem> FsMap_t; 44 45 KernelObject(); 46 virtual ~KernelObject(); 47 48 // Attach the given Filesystem object at the specified path. 49 Error AttachFsAtPath(const ScopedFilesystem& fs, const std::string& path); 50 51 // Unmap the Filesystem object from the specified path and release it. 52 Error DetachFsAtPath(const std::string& path, ScopedFilesystem* out_fs); 53 54 // Find the filesystem for the given path, and acquires it and return a 55 // path relative to the filesystem. 56 // Assumes |out_fs| and |rel_path| are non-NULL. 57 Error AcquireFsAndRelPath(const std::string& path, 58 ScopedFilesystem* out_fs, 59 Path* rel_path); 60 61 // Find the filesystem and node for the given path, acquiring/creating it as 62 // specified by the |oflags|. 63 // Assumes |out_fs| and |out_node| are non-NULL. 64 Error AcquireFsAndNode(const std::string& path, 65 int oflags, 66 ScopedFilesystem* out_fs, 67 ScopedNode* out_node); 68 69 // Get FD-specific flags (currently only FD_CLOEXEC is supported). 70 Error GetFDFlags(int fd, int* out_flags); 71 // Set FD-specific flags (currently only FD_CLOEXEC is supported). 72 Error SetFDFlags(int fd, int flags); 73 74 // Convert from FD to KernelHandle, and acquire the handle. 75 // Assumes |out_handle| is non-NULL. 76 Error AcquireHandle(int fd, ScopedKernelHandle* out_handle); 77 Error AcquireHandleAndPath(int fd, 78 ScopedKernelHandle* out_handle, 79 std::string* out_path); 80 81 // Allocate a new fd and assign the handle to it, while 82 // ref counting the handle and associated filesystem. 83 // Assumes |handle| is non-NULL; 84 int AllocateFD(const ScopedKernelHandle& handle, 85 const std::string& path = std::string()); 86 87 // Assumes |handle| is non-NULL; 88 void FreeAndReassignFD(int fd, 89 const ScopedKernelHandle& handle, 90 const std::string& path); 91 void FreeFD(int fd); 92 93 // Returns or sets CWD 94 std::string GetCWD(); 95 Error SetCWD(const std::string& path); 96 97 // Returns parts of the absolute path for the given relative path 98 Path GetAbsParts(const std::string& path); 99 100 private: 101 std::string cwd_; 102 std::vector<int> free_fds_; 103 HandleMap_t handle_map_; 104 FsMap_t filesystems_; 105 106 // Lock to protect free_fds_ and handle_map_. 107 sdk_util::SimpleLock handle_lock_; 108 109 // Lock to protect filesystems_. 110 sdk_util::SimpleLock fs_lock_; 111 112 // Lock to protect cwd_. 113 sdk_util::SimpleLock cwd_lock_; 114 115 DISALLOW_COPY_AND_ASSIGN(KernelObject); 116 }; 117 118 } // namespace nacl_io 119 120 #endif // LIBRARIES_NACL_IO_KERNEL_OBJECT_H_ 121