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/kernel_handle.h" 16 #include "nacl_io/mount.h" 17 #include "nacl_io/mount_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) : handle(h), flags(0) {} 35 36 ScopedKernelHandle handle; 37 int flags; 38 }; 39 typedef std::vector<Descriptor_t> HandleMap_t; 40 typedef std::map<std::string, ScopedMount> MountMap_t; 41 42 KernelObject(); 43 virtual ~KernelObject(); 44 45 // Attach the given Mount object at the specified path. 46 Error AttachMountAtPath(const ScopedMount& mnt, const std::string& path); 47 48 // Unmap the Mount object from the specified path and release it. 49 Error DetachMountAtPath(const std::string& path); 50 51 // Find the mount for the given path, and acquires it and return a 52 // path relative to the mount. 53 // Assumes |out_mount| and |rel_path| are non-NULL. 54 Error AcquireMountAndRelPath(const std::string& path, 55 ScopedMount* out_mount, 56 Path* rel_path); 57 58 // Find the mount and node for the given path, acquiring/creating it as 59 // specified by the |oflags|. 60 // Assumes |out_mount| and |out_node| are non-NULL. 61 Error AcquireMountAndNode(const std::string& path, 62 int oflags, 63 ScopedMount* out_mount, 64 ScopedMountNode* out_node); 65 66 // Get FD-specific flags (currently only FD_CLOEXEC is supported). 67 Error GetFDFlags(int fd, int* out_flags); 68 // Set FD-specific flags (currently only FD_CLOEXEC is supported). 69 Error SetFDFlags(int fd, int flags); 70 71 // Convert from FD to KernelHandle, and acquire the handle. 72 // Assumes |out_handle| is non-NULL. 73 Error AcquireHandle(int fd, ScopedKernelHandle* out_handle); 74 75 // Allocate a new fd and assign the handle to it, while 76 // ref counting the handle and associated mount. 77 // Assumes |handle| is non-NULL; 78 int AllocateFD(const ScopedKernelHandle& handle); 79 80 // Assumes |handle| is non-NULL; 81 void FreeAndReassignFD(int fd, const ScopedKernelHandle& handle); 82 void FreeFD(int fd); 83 84 // Returns or sets CWD 85 std::string GetCWD(); 86 Error SetCWD(const std::string& path); 87 88 // Returns parts of the absolute path for the given relative path 89 Path GetAbsParts(const std::string& path); 90 91 private: 92 std::string cwd_; 93 std::vector<int> free_fds_; 94 HandleMap_t handle_map_; 95 MountMap_t mounts_; 96 97 // Lock to protect free_fds_ and handle_map_. 98 sdk_util::SimpleLock handle_lock_; 99 100 // Lock to protect handle_map_. 101 sdk_util::SimpleLock mount_lock_; 102 103 // Lock to protect cwd_. 104 sdk_util::SimpleLock cwd_lock_; 105 106 DISALLOW_COPY_AND_ASSIGN(KernelObject); 107 }; 108 109 } // namespace nacl_io 110 111 #endif // LIBRARIES_NACL_IO_KERNEL_OBJECT_H_ 112