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_
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"
21 #include "sdk_util/macros.h"
22 #include "sdk_util/simple_lock.h"
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.
30 // All calls are assumed to be a relative path.
34 Descriptor_t() : flags(0) {}
35 explicit Descriptor_t(const ScopedKernelHandle
& h
,
36 const std::string
& open_path
)
37 : handle(h
), flags(0), path(open_path
) {}
39 ScopedKernelHandle handle
;
43 typedef std::vector
<Descriptor_t
> HandleMap_t
;
44 typedef std::map
<std::string
, ScopedFilesystem
> FsMap_t
;
47 virtual ~KernelObject();
49 // Attach the given Filesystem object at the specified path.
50 Error
AttachFsAtPath(const ScopedFilesystem
& fs
, const std::string
& path
);
52 // Unmap the Filesystem object from the specified path and release it.
53 Error
DetachFsAtPath(const std::string
& path
, ScopedFilesystem
* out_fs
);
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
,
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
);
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
);
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
);
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());
88 // Assumes |handle| is non-NULL;
89 void FreeAndReassignFD(int fd
,
90 const ScopedKernelHandle
& handle
,
91 const std::string
& path
);
94 // Returns or sets CWD
96 Error
SetCWD(const std::string
& path
);
99 // Also returns current umask (like POSIX's umask(2))
100 mode_t
SetUmask(mode_t
);
102 // Returns parts of the absolute path for the given relative path
103 Path
GetAbsParts(const std::string
& path
);
108 std::set
<int> free_fds_
;
109 HandleMap_t handle_map_
;
110 FsMap_t filesystems_
;
112 // Lock to protect free_fds_ and handle_map_.
113 sdk_util::SimpleLock handle_lock_
;
115 // Lock to protect filesystems_.
116 sdk_util::SimpleLock fs_lock_
;
118 // Lock to protect cwd_.
119 sdk_util::SimpleLock cwd_lock_
;
120 // Lock to protect umask_.
121 sdk_util::SimpleLock umask_lock_
;
123 DISALLOW_COPY_AND_ASSIGN(KernelObject
);
126 } // namespace nacl_io
128 #endif // LIBRARIES_NACL_IO_KERNEL_OBJECT_H_