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 #ifndef LIBRARIES_NACL_IO_KERNEL_OBJECT_H_
6 #define LIBRARIES_NACL_IO_KERNEL_OBJECT_H_
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"
20 #include "sdk_util/macros.h"
21 #include "sdk_util/simple_lock.h"
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.
29 // All calls are assumed to be a relative path.
33 Descriptor_t() : flags(0) {}
34 explicit Descriptor_t(const ScopedKernelHandle& h) : handle(h), flags(0) {}
36 ScopedKernelHandle handle;
39 typedef std::vector<Descriptor_t> HandleMap_t;
40 typedef std::map<std::string, ScopedMount> MountMap_t;
43 virtual ~KernelObject();
45 // Attach the given Mount object at the specified path.
46 Error AttachMountAtPath(const ScopedMount& mnt, const std::string& path);
48 // Unmap the Mount object from the specified path and release it.
49 Error DetachMountAtPath(const std::string& path);
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,
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,
63 ScopedMount* out_mount,
64 ScopedMountNode* out_node);
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);
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);
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);
80 // Assumes |handle| is non-NULL;
81 void FreeAndReassignFD(int fd, const ScopedKernelHandle& handle);
84 // Returns or sets CWD
86 Error SetCWD(const std::string& path);
88 // Returns parts of the absolute path for the given relative path
89 Path GetAbsParts(const std::string& path);
93 std::vector<int> free_fds_;
94 HandleMap_t handle_map_;
97 // Lock to protect free_fds_ and handle_map_.
98 sdk_util::SimpleLock handle_lock_;
100 // Lock to protect handle_map_.
101 sdk_util::SimpleLock mount_lock_;
103 // Lock to protect cwd_.
104 sdk_util::SimpleLock cwd_lock_;
106 DISALLOW_COPY_AND_ASSIGN(KernelObject);
109 } // namespace nacl_io
111 #endif // LIBRARIES_NACL_IO_KERNEL_OBJECT_H_