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/filesystem.h"
16 #include "nacl_io/kernel_handle.h"
17 #include "nacl_io/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
,
35 const std::string
& open_path
)
36 : handle(h
), flags(0), path(open_path
) {}
38 ScopedKernelHandle handle
;
42 typedef std::vector
<Descriptor_t
> HandleMap_t
;
43 typedef std::map
<std::string
, ScopedFilesystem
> FsMap_t
;
46 virtual ~KernelObject();
48 // Attach the given Filesystem object at the specified path.
49 Error
AttachFsAtPath(const ScopedFilesystem
& fs
, const std::string
& path
);
51 // Unmap the Filesystem object from the specified path and release it.
52 Error
DetachFsAtPath(const std::string
& path
, ScopedFilesystem
* out_fs
);
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
,
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
,
66 ScopedFilesystem
* out_fs
,
67 ScopedNode
* out_node
);
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
);
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
);
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());
87 // Assumes |handle| is non-NULL;
88 void FreeAndReassignFD(int fd
,
89 const ScopedKernelHandle
& handle
,
90 const std::string
& path
);
93 // Returns or sets CWD
95 Error
SetCWD(const std::string
& path
);
97 // Returns parts of the absolute path for the given relative path
98 Path
GetAbsParts(const std::string
& path
);
102 std::vector
<int> free_fds_
;
103 HandleMap_t handle_map_
;
104 FsMap_t filesystems_
;
106 // Lock to protect free_fds_ and handle_map_.
107 sdk_util::SimpleLock handle_lock_
;
109 // Lock to protect filesystems_.
110 sdk_util::SimpleLock fs_lock_
;
112 // Lock to protect cwd_.
113 sdk_util::SimpleLock cwd_lock_
;
115 DISALLOW_COPY_AND_ASSIGN(KernelObject
);
118 } // namespace nacl_io
120 #endif // LIBRARIES_NACL_IO_KERNEL_OBJECT_H_