1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#if defined(WIN32)
#define _CRT_RAND_S
#endif
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include "nacl_io/kernel_wrap_real.h"
#include "nacl_io/mount_dev.h"
#include "nacl_io/mount_node.h"
#include "nacl_io/mount_node_dir.h"
#include "nacl_io/mount_node_tty.h"
#include "nacl_io/osunistd.h"
#include "nacl_io/pepper_interface.h"
#include "sdk_util/auto_lock.h"
#if defined(__native_client__)
#include <irt.h>
#elif defined(WIN32)
#include <stdlib.h>
#endif
namespace nacl_io {
namespace {
class RealNode : public MountNode {
public:
RealNode(Mount* mount, int fd);
virtual Error Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes);
virtual Error Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes);
virtual Error GetStat(struct stat* stat);
protected:
int fd_;
};
class NullNode : public MountNodeCharDevice {
public:
explicit NullNode(Mount* mount) : MountNodeCharDevice(mount) {}
virtual Error Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes);
virtual Error Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes);
};
class ConsoleNode : public MountNodeCharDevice {
public:
ConsoleNode(Mount* mount, PP_LogLevel level);
virtual Error Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes);
private:
PP_LogLevel level_;
};
class ZeroNode : public MountNode {
public:
explicit ZeroNode(Mount* mount);
virtual Error Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes);
virtual Error Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes);
};
class UrandomNode : public MountNode {
public:
explicit UrandomNode(Mount* mount);
virtual Error Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes);
virtual Error Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes);
private:
#if defined(__native_client__)
nacl_irt_random random_interface_;
bool interface_ok_;
#endif
};
RealNode::RealNode(Mount* mount, int fd) : MountNode(mount), fd_(fd) {
SetType(S_IFCHR);
}
Error RealNode::Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes) {
*out_bytes = 0;
size_t readcnt;
int err = _real_read(fd_, buf, count, &readcnt);
if (err)
return err;
*out_bytes = static_cast<int>(readcnt);
return 0;
}
Error RealNode::Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes) {
*out_bytes = 0;
size_t writecnt;
int err = _real_write(fd_, buf, count, &writecnt);
if (err)
return err;
*out_bytes = static_cast<int>(writecnt);
return 0;
}
Error RealNode::GetStat(struct stat* stat) { return _real_fstat(fd_, stat); }
Error NullNode::Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes) {
*out_bytes = 0;
return 0;
}
Error NullNode::Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes) {
*out_bytes = count;
return 0;
}
ConsoleNode::ConsoleNode(Mount* mount, PP_LogLevel level)
: MountNodeCharDevice(mount), level_(level) {
}
Error ConsoleNode::Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes) {
*out_bytes = 0;
ConsoleInterface* con_intr = mount_->ppapi()->GetConsoleInterface();
VarInterface* var_intr = mount_->ppapi()->GetVarInterface();
if (!(var_intr && con_intr))
return ENOSYS;
const char* var_data = static_cast<const char*>(buf);
uint32_t len = static_cast<uint32_t>(count);
struct PP_Var val = var_intr->VarFromUtf8(var_data, len);
con_intr->Log(mount_->ppapi()->GetInstance(), level_, val);
*out_bytes = count;
return 0;
}
ZeroNode::ZeroNode(Mount* mount) : MountNode(mount) { SetType(S_IFCHR); }
Error ZeroNode::Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes) {
memset(buf, 0, count);
*out_bytes = count;
return 0;
}
Error ZeroNode::Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes) {
*out_bytes = count;
return 0;
}
UrandomNode::UrandomNode(Mount* mount) : MountNode(mount) {
SetType(S_IFCHR);
#if defined(__native_client__)
size_t result = nacl_interface_query(
NACL_IRT_RANDOM_v0_1, &random_interface_, sizeof(random_interface_));
interface_ok_ = result != 0;
#endif
}
Error UrandomNode::Read(const HandleAttr& attr,
void* buf,
size_t count,
int* out_bytes) {
*out_bytes = 0;
#if defined(__native_client__)
if (!interface_ok_)
return EBADF;
size_t nread;
int error = (*random_interface_.get_random_bytes)(buf, count, &nread);
if (error)
return error;
#elif defined(WIN32)
char* out = static_cast<char*>(buf);
size_t bytes_left = count;
while (bytes_left) {
unsigned int random_int;
errno_t err = rand_s(&random_int);
if (err) {
*out_bytes = count - bytes_left;
return err;
}
int bytes_to_copy = std::min(bytes_left, sizeof(random_int));
memcpy(out, &random_int, bytes_to_copy);
out += bytes_to_copy;
bytes_left -= bytes_to_copy;
}
#endif
*out_bytes = count;
return 0;
}
Error UrandomNode::Write(const HandleAttr& attr,
const void* buf,
size_t count,
int* out_bytes) {
*out_bytes = count;
return 0;
}
} // namespace
Error MountDev::Access(const Path& path, int a_mode) {
ScopedMountNode node;
int error = root_->FindChild(path.Join(), &node);
if (error)
return error;
// Don't allow execute access.
if (a_mode & X_OK)
return EACCES;
return 0;
}
Error MountDev::Open(const Path& path,
int open_flags,
ScopedMountNode* out_node) {
out_node->reset(NULL);
// Don't allow creating any files.
if (open_flags & O_CREAT)
return EINVAL;
return root_->FindChild(path.Join(), out_node);
}
Error MountDev::Unlink(const Path& path) { return EPERM; }
Error MountDev::Mkdir(const Path& path, int permissions) { return EPERM; }
Error MountDev::Rmdir(const Path& path) { return EPERM; }
Error MountDev::Remove(const Path& path) { return EPERM; }
Error MountDev::Rename(const Path& path, const Path& newpath) { return EPERM; }
MountDev::MountDev() {}
#define INITIALIZE_DEV_NODE(path, klass) \
error = root_->AddChild(path, ScopedMountNode(new klass(this))); \
if (error) \
return error;
#define INITIALIZE_DEV_NODE_1(path, klass, arg) \
error = root_->AddChild(path, ScopedMountNode(new klass(this, arg))); \
if (error) \
return error;
Error MountDev::Init(const MountInitArgs& args) {
Error error = Mount::Init(args);
if (error)
return error;
root_.reset(new MountNodeDir(this));
INITIALIZE_DEV_NODE("/null", NullNode);
INITIALIZE_DEV_NODE("/zero", ZeroNode);
INITIALIZE_DEV_NODE("/urandom", UrandomNode);
INITIALIZE_DEV_NODE_1("/console0", ConsoleNode, PP_LOGLEVEL_TIP);
INITIALIZE_DEV_NODE_1("/console1", ConsoleNode, PP_LOGLEVEL_LOG);
INITIALIZE_DEV_NODE_1("/console2", ConsoleNode, PP_LOGLEVEL_WARNING);
INITIALIZE_DEV_NODE_1("/console3", ConsoleNode, PP_LOGLEVEL_ERROR);
INITIALIZE_DEV_NODE("/tty", MountNodeTty);
INITIALIZE_DEV_NODE_1("/stdin", RealNode, 0);
INITIALIZE_DEV_NODE_1("/stdout", RealNode, 1);
INITIALIZE_DEV_NODE_1("/stderr", RealNode, 2);
return 0;
}
} // namespace nacl_io
|