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