1 // Copyright 2013 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 #include "nacl_io/node.h"
17 #include "nacl_io/filesystem.h"
18 #include "nacl_io/kernel_handle.h"
19 #include "nacl_io/kernel_wrap_real.h"
20 #include "nacl_io/osmman.h"
21 #include "sdk_util/auto_lock.h"
25 static const int USR_ID = 1001;
26 static const int GRP_ID = 1002;
28 Node::Node(Filesystem* filesystem) : filesystem_(filesystem) {
29 memset(&stat_, 0, sizeof(stat_));
30 stat_.st_gid = GRP_ID;
31 stat_.st_uid = USR_ID;
32 stat_.st_mode = S_IRALL | S_IWALL;
34 // Filesystem should normally never be NULL, but may be null in tests.
35 // If NULL, at least set the inode to a valid (nonzero) value.
37 filesystem_->OnNodeCreated(this);
45 Error Node::Init(int open_flags) {
49 void Node::Destroy() {
51 filesystem_->OnNodeDestroyed(this);
55 EventEmitter* Node::GetEventEmitter() {
59 uint32_t Node::GetEventStatus() {
60 if (GetEventEmitter())
61 return GetEventEmitter()->GetEventStatus();
63 return POLLIN | POLLOUT;
66 bool Node::CanOpen(int open_flags) {
67 switch (open_flags & 3) {
69 return (stat_.st_mode & S_IRALL) != 0;
71 return (stat_.st_mode & S_IWALL) != 0;
73 return (stat_.st_mode & S_IRALL) != 0 && (stat_.st_mode & S_IWALL) != 0;
83 Error Node::FTruncate(off_t length) {
87 Error Node::GetDents(size_t offs,
95 Error Node::GetStat(struct stat* pstat) {
96 AUTO_LOCK(node_lock_);
97 memcpy(pstat, &stat_, sizeof(stat_));
101 Error Node::Ioctl(int request, ...) {
103 va_start(ap, request);
104 Error rtn = VIoctl(request, ap);
109 Error Node::VIoctl(int request, va_list args) {
113 Error Node::Read(const HandleAttr& attr,
121 Error Node::Write(const HandleAttr& attr,
129 Error Node::MMap(void* addr,
137 // Never allow mmap'ing PROT_EXEC. The passthrough node supports this, but we
138 // don't. Fortunately, glibc will fallback if this fails, so dlopen will
140 if (prot & PROT_EXEC)
143 // This default mmap support is just enough to make dlopen work. This
144 // implementation just reads from the filesystem into the mmap'd memory area.
145 void* new_addr = addr;
146 int mmap_error = _real_mmap(
147 &new_addr, length, prot | PROT_WRITE, flags | MAP_ANONYMOUS, -1, 0);
148 if (new_addr == MAP_FAILED) {
149 _real_munmap(new_addr, length);
157 Error read_error = Read(data, new_addr, length, &bytes_read);
159 _real_munmap(new_addr, length);
163 *out_addr = new_addr;
167 Error Node::Tcflush(int queue_selector) {
171 Error Node::Tcgetattr(struct termios* termios_p) {
175 Error Node::Tcsetattr(int optional_actions, const struct termios* termios_p) {
179 int Node::GetLinks() {
180 return stat_.st_nlink;
183 int Node::GetMode() {
184 return stat_.st_mode & ~S_IFMT;
187 Error Node::GetSize(off_t* out_size) {
188 *out_size = stat_.st_size;
192 int Node::GetType() {
193 return stat_.st_mode & S_IFMT;
196 void Node::SetType(int type) {
197 assert((type & ~S_IFMT) == 0);
198 stat_.st_mode &= ~S_IFMT;
199 stat_.st_mode |= type;
202 bool Node::IsaDir() {
203 return GetType() == S_IFDIR;
206 bool Node::IsaFile() {
207 return GetType() == S_IFREG;
210 bool Node::IsaSock() {
211 return GetType() == S_IFSOCK;
214 Error Node::Isatty() {
218 Error Node::AddChild(const std::string& name, const ScopedNode& node) {
222 Error Node::RemoveChild(const std::string& name) {
226 Error Node::FindChild(const std::string& name, ScopedNode* out_node) {
227 out_node->reset(NULL);
231 int Node::ChildCount() {
239 void Node::Unlink() {
243 } // namespace nacl_io