OLD | NEW |
1 /* Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 #include "nacl_io/mount_node.h" | 5 #include "nacl_io/mount_node.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "nacl_io/kernel_wrap_real.h" | 13 #include "nacl_io/kernel_wrap_real.h" |
14 #include "nacl_io/mount.h" | 14 #include "nacl_io/mount.h" |
15 #include "nacl_io/osmman.h" | 15 #include "nacl_io/osmman.h" |
16 #include "sdk_util/auto_lock.h" | 16 #include "sdk_util/auto_lock.h" |
17 | 17 |
18 static const int USR_ID = 1001; | 18 static const int USR_ID = 1001; |
19 static const int GRP_ID = 1002; | 19 static const int GRP_ID = 1002; |
20 | 20 |
21 MountNode::MountNode(Mount* mount) | 21 MountNode::MountNode(Mount* mount) : mount_(mount) { |
22 : mount_(mount) { | |
23 memset(&stat_, 0, sizeof(stat_)); | 22 memset(&stat_, 0, sizeof(stat_)); |
24 stat_.st_gid = GRP_ID; | 23 stat_.st_gid = GRP_ID; |
25 stat_.st_uid = USR_ID; | 24 stat_.st_uid = USR_ID; |
26 | 25 |
27 // Mount should normally never be NULL, but may be null in tests. | 26 // Mount should normally never be NULL, but may be null in tests. |
28 // If NULL, at least set the inode to a valid (nonzero) value. | 27 // If NULL, at least set the inode to a valid (nonzero) value. |
29 if (mount_) | 28 if (mount_) |
30 mount_->OnNodeCreated(this); | 29 mount_->OnNodeCreated(this); |
31 else | 30 else |
32 stat_.st_ino = 1; | 31 stat_.st_ino = 1; |
33 } | 32 } |
34 | 33 |
35 MountNode::~MountNode() { | 34 MountNode::~MountNode() {} |
36 } | |
37 | 35 |
38 bool MountNode::Init(int perm) { | 36 Error MountNode::Init(int perm) { |
39 stat_.st_mode |= perm; | 37 stat_.st_mode |= perm; |
40 return true; | 38 return 0; |
41 } | 39 } |
42 | 40 |
43 void MountNode::Destroy() { | 41 void MountNode::Destroy() { |
44 if (mount_) { | 42 if (mount_) { |
45 mount_->OnNodeDestroyed(this); | 43 mount_->OnNodeDestroyed(this); |
46 } | 44 } |
47 } | 45 } |
48 | 46 |
49 int MountNode::FSync() { | 47 Error MountNode::FSync() { return 0; } |
50 return 0; | 48 |
| 49 Error MountNode::FTruncate(off_t length) { |
| 50 return EINVAL; |
51 } | 51 } |
52 | 52 |
53 int MountNode::FTruncate(off_t length) { | 53 Error MountNode::GetDents(size_t offs, |
54 errno = EINVAL; | 54 struct dirent* pdir, |
55 return -1; | 55 size_t count, |
| 56 int* out_bytes) { |
| 57 *out_bytes = 0; |
| 58 return ENOTDIR; |
56 } | 59 } |
57 | 60 |
58 int MountNode::GetDents(size_t offs, struct dirent* pdir, size_t count) { | 61 Error MountNode::GetStat(struct stat* pstat) { |
59 errno = ENOTDIR; | |
60 return -1; | |
61 } | |
62 | |
63 int MountNode::GetStat(struct stat* pstat) { | |
64 AutoLock lock(&lock_); | 62 AutoLock lock(&lock_); |
65 memcpy(pstat, &stat_, sizeof(stat_)); | 63 memcpy(pstat, &stat_, sizeof(stat_)); |
66 return 0; | 64 return 0; |
67 } | 65 } |
68 | 66 |
69 int MountNode::Ioctl(int request, char* arg) { | 67 Error MountNode::Ioctl(int request, char* arg) { |
70 errno = EINVAL; | 68 return EINVAL; |
71 return -1; | |
72 } | 69 } |
73 | 70 |
74 int MountNode::Read(size_t offs, void* buf, size_t count) { | 71 Error MountNode::Read(size_t offs, void* buf, size_t count, int* out_bytes) { |
75 errno = EINVAL; | 72 *out_bytes = 0; |
76 return -1; | 73 return EINVAL; |
77 } | 74 } |
78 | 75 |
79 int MountNode::Write(size_t offs, const void* buf, size_t count) { | 76 Error MountNode::Write(size_t offs, |
80 errno = EINVAL; | 77 const void* buf, |
81 return -1; | 78 size_t count, |
| 79 int* out_bytes) { |
| 80 *out_bytes = 0; |
| 81 return EINVAL; |
82 } | 82 } |
83 | 83 |
84 void* MountNode::MMap(void* addr, size_t length, int prot, int flags, | 84 Error MountNode::MMap(void* addr, |
85 size_t offset) { | 85 size_t length, |
| 86 int prot, |
| 87 int flags, |
| 88 size_t offset, |
| 89 void** out_addr) { |
| 90 *out_addr = NULL; |
| 91 |
86 // Never allow mmap'ing PROT_EXEC. The passthrough node supports this, but we | 92 // Never allow mmap'ing PROT_EXEC. The passthrough node supports this, but we |
87 // don't. Fortunately, glibc will fallback if this fails, so dlopen will | 93 // don't. Fortunately, glibc will fallback if this fails, so dlopen will |
88 // continue to work. | 94 // continue to work. |
89 if (prot & PROT_EXEC) { | 95 if (prot & PROT_EXEC) |
90 errno = EPERM; | 96 return EPERM; |
91 return MAP_FAILED; | |
92 } | |
93 | 97 |
94 // This default mmap support is just enough to make dlopen work. | 98 // This default mmap support is just enough to make dlopen work. |
95 // This implementation just reads from the mount into the mmap'd memory area. | 99 // This implementation just reads from the mount into the mmap'd memory area. |
96 void* new_addr = addr; | 100 void* new_addr = addr; |
97 int err = _real_mmap(&new_addr, length, prot | PROT_WRITE, flags | | 101 int mmap_error = _real_mmap( |
98 MAP_ANONYMOUS, -1, 0); | 102 &new_addr, length, prot | PROT_WRITE, flags | MAP_ANONYMOUS, -1, 0); |
99 if (new_addr == MAP_FAILED) { | 103 if (new_addr == MAP_FAILED) { |
100 _real_munmap(new_addr, length); | 104 _real_munmap(new_addr, length); |
101 errno = err; | 105 return mmap_error; |
102 return MAP_FAILED; | |
103 } | 106 } |
104 | 107 |
105 ssize_t cnt = Read(offset, new_addr, length); | 108 int bytes_read; |
106 if (cnt == -1) { | 109 Error read_error = Read(offset, new_addr, length, &bytes_read); |
| 110 if (read_error) { |
107 _real_munmap(new_addr, length); | 111 _real_munmap(new_addr, length); |
108 errno = ENOSYS; | 112 return read_error; |
109 return MAP_FAILED; | |
110 } | 113 } |
111 | 114 |
112 return new_addr; | 115 *out_addr = new_addr; |
| 116 return 0; |
113 } | 117 } |
114 | 118 |
115 int MountNode::GetLinks() { | 119 int MountNode::GetLinks() { return stat_.st_nlink; } |
116 return stat_.st_nlink; | 120 |
| 121 int MountNode::GetMode() { return stat_.st_mode & ~S_IFMT; } |
| 122 |
| 123 Error MountNode::GetSize(size_t* out_size) { |
| 124 *out_size = stat_.st_size; |
| 125 return 0; |
117 } | 126 } |
118 | 127 |
119 int MountNode::GetMode() { | 128 int MountNode::GetType() { return stat_.st_mode & S_IFMT; } |
120 return stat_.st_mode & ~S_IFMT; | 129 |
| 130 bool MountNode::IsaDir() { return (stat_.st_mode & S_IFDIR) != 0; } |
| 131 |
| 132 bool MountNode::IsaFile() { return (stat_.st_mode & S_IFREG) != 0; } |
| 133 |
| 134 bool MountNode::IsaTTY() { return (stat_.st_mode & S_IFCHR) != 0; } |
| 135 |
| 136 Error MountNode::AddChild(const std::string& name, MountNode* node) { |
| 137 return ENOTDIR; |
121 } | 138 } |
122 | 139 |
123 size_t MountNode::GetSize() { | 140 Error MountNode::RemoveChild(const std::string& name) { |
124 return stat_.st_size; | 141 return ENOTDIR; |
125 } | 142 } |
126 | 143 |
127 int MountNode::GetType() { | 144 Error MountNode::FindChild(const std::string& name, MountNode** out_node) { |
128 return stat_.st_mode & S_IFMT; | 145 *out_node = NULL; |
129 } | 146 return ENOTDIR; |
130 | |
131 bool MountNode::IsaDir() { | |
132 return (stat_.st_mode & S_IFDIR) != 0; | |
133 } | |
134 | |
135 bool MountNode::IsaFile() { | |
136 return (stat_.st_mode & S_IFREG) != 0; | |
137 } | |
138 | |
139 bool MountNode::IsaTTY() { | |
140 return (stat_.st_mode & S_IFCHR) != 0; | |
141 } | |
142 | |
143 | |
144 int MountNode:: AddChild(const std::string& name, MountNode* node) { | |
145 errno = ENOTDIR; | |
146 return -1; | |
147 } | |
148 | |
149 int MountNode::RemoveChild(const std::string& name) { | |
150 errno = ENOTDIR; | |
151 return -1; | |
152 } | |
153 | |
154 MountNode* MountNode::FindChild(const std::string& name) { | |
155 errno = ENOTDIR; | |
156 return NULL; | |
157 } | 147 } |
158 | 148 |
159 int MountNode::ChildCount() { | 149 int MountNode::ChildCount() { |
160 errno = ENOTDIR; | 150 return 0; |
161 return -1; | |
162 } | 151 } |
163 | 152 |
164 void MountNode::Link() { | 153 void MountNode::Link() { |
165 Acquire(); | 154 Acquire(); |
166 stat_.st_nlink++; | 155 stat_.st_nlink++; |
167 } | 156 } |
168 | 157 |
169 void MountNode::Unlink() { | 158 void MountNode::Unlink() { |
170 stat_.st_nlink--; | 159 stat_.st_nlink--; |
171 Release(); | 160 Release(); |
172 } | 161 } |
OLD | NEW |