OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 #include "base/KernelProxy.h" | |
6 #include <assert.h> | 7 #include <assert.h> |
8 #if __GLIBC__ | |
9 #include <netinet/in.h> | |
10 #endif | |
11 #include <sys/time.h> | |
7 #include <list> | 12 #include <list> |
8 #include <utility> | 13 #include <utility> |
9 #include "../console/ConsoleMount.h" | 14 #include "console/ConsoleMount.h" |
10 #include "../dev/DevMount.h" | 15 #include "dev/DevMount.h" |
11 #include "KernelProxy.h" | |
12 #include "MountManager.h" | 16 #include "MountManager.h" |
17 #include "net/BaseSocketSubSystem.h" | |
18 #include "net/SocketSubSystem.h" | |
19 #include "util/DebugPrint.h" | |
13 | 20 |
14 static pthread_once_t kp_once_ = PTHREAD_ONCE_INIT; | 21 static pthread_once_t kp_once_ = PTHREAD_ONCE_INIT; |
15 KernelProxy *KernelProxy::kp_instance_; | 22 KernelProxy *KernelProxy::kp_instance_; |
16 | 23 |
17 #ifndef MAXPATHLEN | 24 #ifndef MAXPATHLEN |
18 #define MAXPATHLEN 256 | 25 #define MAXPATHLEN 256 |
19 #endif | 26 #endif |
20 | 27 |
28 static const int64_t kMicrosecondsPerSecond = 1000 * 1000; | |
29 static const int64_t kNanosecondsPerMicrosecond = 1000; | |
30 | |
21 KernelProxy::KernelProxy() { | 31 KernelProxy::KernelProxy() { |
22 if (pthread_mutex_init(&kp_lock_, NULL)) assert(0); | 32 if (pthread_mutex_init(&kp_lock_, NULL)) assert(0); |
23 cwd_ = Path("/"); | 33 cwd_ = Path("/"); |
24 mm_.Init(); | 34 mm_.Init(); |
25 | 35 |
26 // Setup file descriptors 0, 1, and 2 for STDIN, STDOUT, and STDERR | 36 // Setup file descriptors 0, 1, and 2 for STDIN, STDOUT, and STDERR |
27 int ret = mkdir("/dev", 0777); | 37 int ret = mkdir("/dev", 0777); |
28 assert(ret == 0); | 38 assert(ret == 0); |
29 DevMount* dev_mount = new DevMount(); | 39 DevMount* dev_mount = new DevMount(); |
30 ret = mm_.AddMount(dev_mount, "/dev"); | 40 ret = mm_.AddMount(dev_mount, "/dev"); |
31 assert(ret == 0); | 41 assert(ret == 0); |
32 ret = mkdir("/dev/fd", 0777); | 42 ret = mkdir("/dev/fd", 0777); |
33 assert(ret == 0); | 43 assert(ret == 0); |
34 ConsoleMount *console_mount = new ConsoleMount(); | 44 ConsoleMount *console_mount = new ConsoleMount(); |
35 ret = mm_.AddMount(console_mount, "/dev/fd"); | 45 ret = mm_.AddMount(console_mount, "/dev/fd"); |
36 assert(ret == 0); | 46 assert(ret == 0); |
37 int fd = open("/dev/fd/0", O_CREAT | O_RDWR, 0); | 47 int fd = open("/dev/fd/0", O_CREAT | O_RDWR, 0); |
38 assert(fd == 0); | 48 assert(fd == 0); |
39 fd = open("/dev/fd/1", O_CREAT | O_RDWR, 0); | 49 fd = open("/dev/fd/1", O_CREAT | O_RDWR, 0); |
40 assert(fd == 1); | 50 assert(fd == 1); |
41 fd = open("/dev/fd/2", O_CREAT | O_RDWR, 0); | 51 fd = open("/dev/fd/2", O_CREAT | O_RDWR, 0); |
42 assert(fd == 2); | 52 assert(fd == 2); |
43 } | 53 } |
44 | 54 |
55 #ifdef __GLIBC__ | |
56 void KernelProxy::SetSocketSubSystem(BaseSocketSubSystem* bss) { | |
57 socket_subsystem_ = bss; | |
58 } | |
59 | |
60 void KernelProxy::RemoveFileStream(int fd) { | |
61 this->close(fd); | |
62 } | |
63 | |
64 int KernelProxy::AddFileStream(FileStream* stream) { | |
65 SimpleAutoLock lock(&kp_lock_); | |
66 | |
67 // Setup file handle. | |
68 int handle_slot = open_files_.Alloc(); | |
69 int fd = fds_.Alloc(); | |
70 FileDescriptor* file = fds_.At(fd); | |
71 file->handle = handle_slot; | |
72 FileHandle* handle = open_files_.At(handle_slot); | |
73 | |
74 // init should be safe because we have the kernel proxy lock | |
75 if (pthread_mutex_init(&handle->lock, NULL)) assert(0); | |
76 | |
77 handle->mount = (Mount*) NULL; | |
78 handle->stream = (FileStream*) NULL; | |
79 handle->use_count = 1; | |
80 | |
81 return fd; | |
82 } | |
83 #endif | |
84 | |
45 KernelProxy *KernelProxy::KPInstance() { | 85 KernelProxy *KernelProxy::KPInstance() { |
46 pthread_once(&kp_once_, Instantiate); | 86 pthread_once(&kp_once_, Instantiate); |
47 return kp_instance_; | 87 return kp_instance_; |
48 } | 88 } |
49 | 89 |
50 void KernelProxy::Instantiate() { | 90 void KernelProxy::Instantiate() { |
51 assert(!kp_instance_); | 91 assert(!kp_instance_); |
52 kp_instance_ = new KernelProxy(); | 92 kp_instance_ = new KernelProxy(); |
53 } | 93 } |
54 | 94 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 } | 210 } |
171 } | 211 } |
172 | 212 |
173 mount->Ref(st.st_ino); | 213 mount->Ref(st.st_ino); |
174 mount->Ref(); | 214 mount->Ref(); |
175 // Setup file handle. | 215 // Setup file handle. |
176 int handle_slot = open_files_.Alloc(); | 216 int handle_slot = open_files_.Alloc(); |
177 int fd = fds_.Alloc(); | 217 int fd = fds_.Alloc(); |
178 FileDescriptor* file = fds_.At(fd); | 218 FileDescriptor* file = fds_.At(fd); |
179 file->handle = handle_slot; | 219 file->handle = handle_slot; |
180 FileHandle *handle = open_files_.At(handle_slot); | 220 FileHandle* handle = open_files_.At(handle_slot); |
181 | 221 |
182 // init should be safe because we have the kernel proxy lock | 222 // init should be safe because we have the kernel proxy lock |
183 if (pthread_mutex_init(&handle->lock, NULL)) assert(0); | 223 if (pthread_mutex_init(&handle->lock, NULL)) assert(0); |
184 | 224 |
185 handle->mount = mount; | 225 handle->mount = mount; |
226 #if __GLIBC__ | |
227 handle->stream = NULL; | |
228 #endif | |
186 handle->node = st.st_ino; | 229 handle->node = st.st_ino; |
187 handle->flags = flags; | 230 handle->flags = flags; |
188 handle->use_count = 1; | 231 handle->use_count = 1; |
189 | 232 |
190 if (flags & O_APPEND) { | 233 if (flags & O_APPEND) { |
191 handle->offset = st.st_size; | 234 handle->offset = st.st_size; |
192 } else { | 235 } else { |
193 handle->offset = 0; | 236 handle->offset = 0; |
194 } | 237 } |
195 | 238 |
196 return fd; | 239 return fd; |
197 } | 240 } |
198 | 241 |
242 #if __GLIBC__ | |
243 struct hostent* KernelProxy::gethostbyname(const char* name) { | |
244 struct hostent *res = (struct hostent*) malloc(sizeof(struct hostent)); | |
Dmitry Polukhin
2012/05/30 15:13:32
- This implementation leaks memory because caller
vissi
2012/05/31 13:35:21
Now memory is allocated only once. Should I change
| |
245 if (!res) return NULL; | |
246 res->h_addr_list = (char**) malloc(sizeof(char*) * 2); | |
247 if (!res->h_addr_list) return NULL; | |
248 res->h_addr_list[0] = (char*) malloc(sizeof(long int)); | |
249 if (!res->h_addr_list[0]) return NULL; | |
250 *((long int*)res->h_addr_list[0]) = socket_subsystem_->gethostbyname(name); | |
251 res->h_addr_list[1] = NULL; | |
252 res->h_length = sizeof(long int); | |
253 return res; | |
254 } | |
255 #endif | |
256 | |
199 int KernelProxy::open(const std::string& path, int flags, mode_t mode) { | 257 int KernelProxy::open(const std::string& path, int flags, mode_t mode) { |
200 if (path.empty()) { | 258 if (path.empty()) { |
201 errno = EINVAL; | 259 errno = EINVAL; |
202 return -1; | 260 return -1; |
203 } | 261 } |
204 | 262 |
205 Path p(path); | 263 Path p(path); |
206 if (path[0] != '/') { | 264 if (path[0] != '/') { |
207 p = Path(cwd_.FormulatePath() + "/" + path); | 265 p = Path(cwd_.FormulatePath() + "/" + path); |
208 } | 266 } |
209 | 267 |
210 std::pair<Mount *, std::string> m_and_p = | 268 std::pair<Mount *, std::string> m_and_p = |
211 mm_.GetMount(p.FormulatePath()); | 269 mm_.GetMount(p.FormulatePath()); |
212 | 270 |
213 if (!(m_and_p.first)) { | 271 if (!(m_and_p.first)) { |
214 errno = ENOENT; | 272 errno = ENOENT; |
215 return -1; | 273 return -1; |
216 } | 274 } |
217 | 275 |
218 return OpenHandle(m_and_p.first, m_and_p.second, flags, mode); | 276 return OpenHandle(m_and_p.first, m_and_p.second, flags, mode); |
219 } | 277 } |
220 | 278 |
221 int KernelProxy::close(int fd) { | 279 int KernelProxy::close(int fd) { |
222 SimpleAutoLock lock(&kp_lock_); | 280 SimpleAutoLock* lock = new SimpleAutoLock(&kp_lock_); |
223 | 281 |
224 FileDescriptor* file = fds_.At(fd); | 282 FileDescriptor* file = fds_.At(fd); |
225 if (file == NULL) { | 283 if (file == NULL) { |
226 errno = EBADF; | 284 errno = EBADF; |
227 return -1; | 285 return -1; |
228 } | 286 } |
229 int h = file->handle; | 287 int h = file->handle; |
230 fds_.Free(fd); | 288 fds_.Free(fd); |
231 FileHandle *handle = open_files_.At(h); | 289 FileHandle* handle = open_files_.At(h); |
232 if (handle == NULL) { | 290 if (handle == NULL) { |
233 errno = EBADF; | 291 errno = EBADF; |
234 return -1; | 292 return -1; |
235 } | 293 } |
236 handle->use_count--; | 294 handle->use_count--; |
237 ino_t node = handle->node; | 295 ino_t node = handle->node; |
238 Mount *mount = handle->mount; | 296 if (handle->mount) { |
239 if (handle->use_count <= 0) { | 297 Mount* mount = handle->mount; |
240 open_files_.Free(h); | 298 if (handle->use_count <= 0) { |
241 mount->Unref(node); | 299 open_files_.Free(h); |
300 mount->Unref(node); | |
301 } | |
302 mount->Unref(); | |
303 #if __GLIBC__ | |
304 } else { | |
305 FileStream* stream = handle->stream; | |
306 delete lock; | |
307 socket_subsystem_->close(stream); | |
308 SimpleAutoLock lock(&kp_lock_); | |
309 if (handle->use_count <= 0) { | |
310 open_files_.Free(h); | |
311 } | |
312 return 0; | |
313 #endif | |
242 } | 314 } |
243 mount->Unref(); | 315 delete lock; |
244 return 0; | 316 return 0; |
245 } | 317 } |
246 | 318 |
247 ssize_t KernelProxy::read(int fd, void *buf, size_t count) { | 319 ssize_t KernelProxy::read(int fd, void *buf, size_t count) { |
248 FileHandle *handle; | 320 FileHandle* handle; |
249 // check if fd is valid and handle exists | 321 // check if fd is valid and handle exists |
250 if (!(handle = GetFileHandle(fd))) { | 322 if (!(handle = GetFileHandle(fd))) { |
251 errno = EBADF; | 323 errno = EBADF; |
252 return -1; | 324 return -1; |
253 } | 325 } |
254 | 326 |
255 SimpleAutoLock(&handle->lock); | 327 if (handle->mount) { |
328 SimpleAutoLock(&handle->lock); | |
329 // Check that this file handle can be read from. | |
330 if ((handle->flags & O_ACCMODE) == O_WRONLY || | |
331 is_dir(handle->mount, handle->node)) { | |
332 errno = EBADF; | |
333 return -1; | |
334 } | |
256 | 335 |
257 // Check that this file handle can be read from. | 336 ssize_t n = handle->mount->Read(handle->node, handle->offset, buf, count); |
258 if ((handle->flags & O_ACCMODE) == O_WRONLY || | 337 if (n > 0) { |
259 is_dir(handle->mount, handle->node)) { | 338 handle->offset += n; |
260 errno = EBADF; | 339 } |
261 return -1; | 340 return n; |
341 #if __GLIBC__ | |
342 } else if (handle->stream) { | |
343 // TODO(vissi): more elaborate implementation | |
344 return recv(fd, buf, count, 0); | |
345 #endif | |
262 } | 346 } |
263 | 347 errno = EBADF; |
264 ssize_t n = handle->mount->Read(handle->node, handle->offset, buf, count); | 348 return -1; |
265 if (n > 0) { | |
266 handle->offset += n; | |
267 } | |
268 return n; | |
269 } | 349 } |
270 | 350 |
271 ssize_t KernelProxy::write(int fd, const void *buf, size_t count) { | 351 ssize_t KernelProxy::write(int fd, const void *buf, size_t count) { |
272 FileHandle *handle; | 352 FileHandle* handle; |
273 | 353 |
274 // check if fd is valid and handle exists | 354 // check if fd is valid and handle exists |
275 if (!(handle = GetFileHandle(fd))) { | 355 if (!(handle = GetFileHandle(fd))) { |
276 errno = EBADF; | 356 errno = EBADF; |
277 return -1; | 357 return -1; |
278 } | 358 } |
279 | 359 |
280 SimpleAutoLock(&handle->lock); | 360 if (handle->mount) { |
361 SimpleAutoLock(&handle->lock); | |
362 // Check that this file handle can be written to. | |
363 if ((handle->flags & O_ACCMODE) == O_RDONLY || | |
364 is_dir(handle->mount, handle->node)) { | |
365 errno = EBADF; | |
366 return -1; | |
367 } | |
281 | 368 |
282 // Check that this file handle can be written to. | 369 ssize_t n = handle->mount->Write(handle->node, handle->offset, buf, count); |
283 if ((handle->flags & O_ACCMODE) == O_RDONLY || | 370 |
284 is_dir(handle->mount, handle->node)) { | 371 if (n > 0) { |
285 errno = EBADF; | 372 handle->offset += n; |
286 return -1; | 373 } |
374 return n; | |
375 #if __GLIBC__ | |
376 } else if (handle->stream) { | |
377 // TODO(vissi): more elaborate implementation | |
378 return send(fd, buf, count, 0); | |
379 #endif | |
287 } | 380 } |
288 | 381 errno = EBADF; |
289 ssize_t n = handle->mount->Write(handle->node, handle->offset, buf, count); | 382 return -1; |
290 | |
291 if (n > 0) { | |
292 handle->offset += n; | |
293 } | |
294 return n; | |
295 } | 383 } |
296 | 384 |
297 int KernelProxy::fstat(int fd, struct stat *buf) { | 385 int KernelProxy::fstat(int fd, struct stat *buf) { |
298 FileHandle *handle; | 386 FileHandle* handle; |
299 | 387 |
300 // check if fd is valid and handle exists | 388 // check if fd is valid and handle exists |
301 if (!(handle = GetFileHandle(fd))) { | 389 if (!(handle = GetFileHandle(fd))) { |
302 errno = EBADF; | 390 errno = EBADF; |
303 return -1; | 391 return -1; |
304 } | 392 } |
305 SimpleAutoLock(&handle->lock); | 393 SimpleAutoLock(&handle->lock); |
306 return handle->mount->Stat(handle->node, buf); | 394 return handle->mount->Stat(handle->node, buf); |
307 } | 395 } |
308 | 396 |
309 int KernelProxy::ioctl(int fd, unsigned long request) { | 397 int KernelProxy::ioctl(int fd, unsigned long request) { |
310 errno = ENOSYS; | 398 errno = ENOSYS; |
311 fprintf(stderr, "ioctl has not been implemented!\n"); | 399 fprintf(stderr, "ioctl has not been implemented!\n"); |
312 assert(0); | 400 assert(0); |
313 return -1; | 401 return -1; |
314 } | 402 } |
315 | 403 |
316 int KernelProxy::kill(pid_t pid, int sig) { | 404 int KernelProxy::kill(pid_t pid, int sig) { |
317 errno = ENOSYS; | 405 errno = ENOSYS; |
318 fprintf(stderr, "kill has not been implemented!\n"); | 406 fprintf(stderr, "kill has not been implemented!\n"); |
319 assert(0); | 407 assert(0); |
320 return -1; | 408 return -1; |
321 } | 409 } |
322 | 410 |
323 int KernelProxy::getdents(int fd, void *buf, unsigned int count) { | 411 int KernelProxy::getdents(int fd, void *buf, unsigned int count) { |
324 FileHandle *handle; | 412 FileHandle* handle; |
325 | 413 |
326 // check if fd is valid and handle exists | 414 // check if fd is valid and handle exists |
327 if (!(handle = GetFileHandle(fd))) { | 415 if (!(handle = GetFileHandle(fd))) { |
328 errno = EBADF; | 416 errno = EBADF; |
329 return -1; | 417 return -1; |
330 } | 418 } |
331 | 419 |
332 SimpleAutoLock(&handle->lock); | 420 SimpleAutoLock(&handle->lock); |
333 | 421 |
334 int ret = handle->mount->Getdents(handle->node, handle->offset, | 422 int ret = handle->mount->Getdents(handle->node, handle->offset, |
335 (struct dirent*)buf, count); | 423 (struct dirent*)buf, count); |
336 | 424 |
337 if (ret != -1) { | 425 if (ret != -1) { |
338 // TODO(bradnelson): think of a better interface for Mount::Getdents. | 426 // TODO(bradnelson): think of a better interface for Mount::Getdents. |
339 // http://code.google.com/p/naclports/issues/detail?id=18 | 427 // http://code.google.com/p/naclports/issues/detail?id=18 |
340 handle->offset += ret / sizeof(struct dirent); | 428 handle->offset += ret / sizeof(struct dirent); |
341 } | 429 } |
342 return ret; | 430 return ret; |
343 } | 431 } |
344 | 432 |
345 int KernelProxy::fsync(int fd) { | 433 int KernelProxy::fsync(int fd) { |
346 FileHandle *handle; | 434 FileHandle* handle; |
347 | 435 |
348 if (!(handle = GetFileHandle(fd))) { | 436 if (!(handle = GetFileHandle(fd))) { |
349 errno = EBADF; | 437 errno = EBADF; |
350 return -1; | 438 return -1; |
351 } | 439 } |
352 SimpleAutoLock(&handle->lock); | 440 SimpleAutoLock(&handle->lock); |
353 return handle->mount->Fsync(handle->node); | 441 return handle->mount->Fsync(handle->node); |
354 } | 442 } |
355 | 443 |
356 int KernelProxy::isatty(int fd) { | 444 int KernelProxy::isatty(int fd) { |
357 FileHandle *handle; | 445 FileHandle* handle; |
358 | 446 |
359 if (!(handle = GetFileHandle(fd))) { | 447 if (!(handle = GetFileHandle(fd))) { |
360 errno = EBADF; | 448 errno = EBADF; |
361 return 0; | 449 return 0; |
362 } | 450 } |
363 SimpleAutoLock(&handle->lock); | |
364 return handle->mount->Isatty(handle->node); | 451 return handle->mount->Isatty(handle->node); |
365 } | 452 } |
366 | 453 |
454 int KernelProxy::dup2(int fd, int newfd) { | |
455 SimpleAutoLock lock(&kp_lock_); | |
456 int handle_slot = fds_.At(fd)->handle; | |
457 if (fds_.AllocAt(fd) != fd) return -1; | |
458 FileDescriptor* file = fds_.At(newfd); | |
459 file->handle = handle_slot; | |
460 return 0; | |
461 } | |
462 | |
463 #if __GLIBC__ | |
464 int KernelProxy::IsReady(int nfds, fd_set* fds, | |
465 bool (FileStream::*is_ready)(), bool apply) { | |
466 if (!fds) | |
467 return 0; | |
468 | |
469 int nset = 0; | |
470 for (int i = 0; i < nfds; i++) { | |
471 if (FD_ISSET(i, fds)) { | |
472 FileStream* stream = GetFileHandle(i) > 0 ? | |
473 GetFileHandle(i)->stream : NULL; | |
474 if (!stream) | |
475 return -1; | |
476 if ((stream->*is_ready)()) { | |
477 if (!apply) | |
478 return 1; | |
479 else | |
480 nset++; | |
481 } else { | |
482 if (apply) | |
483 FD_CLR(i, fds); | |
484 } | |
485 } | |
486 } | |
487 return nset; | |
488 } | |
489 #endif | |
490 | |
367 int KernelProxy::dup(int oldfd) { | 491 int KernelProxy::dup(int oldfd) { |
368 SimpleAutoLock lock(&kp_lock_); | 492 SimpleAutoLock lock(&kp_lock_); |
369 | 493 |
494 FileHandle* fh = GetFileHandle(oldfd); | |
495 if (!fh) { | |
496 errno = EBADF; | |
497 return -1; | |
498 } | |
499 #if __GLIBC__ | |
500 if (fh->mount == NULL) { | |
501 FileStream* stream = GetFileHandle(oldfd) > 0 | |
502 ? GetFileHandle(oldfd)->stream : NULL; | |
503 if (!stream) | |
504 return EBADF; | |
505 SimpleAutoLock lock(&kp_lock_); | |
506 int handle_slot = fds_.At(oldfd)->handle; | |
507 int newfd = fds_.Alloc(); | |
508 FileDescriptor* file = fds_.At(newfd); | |
509 file->handle = handle_slot; | |
510 return newfd; | |
511 } | |
512 #endif | |
370 FileDescriptor* oldfile = fds_.At(oldfd); | 513 FileDescriptor* oldfile = fds_.At(oldfd); |
371 if (oldfile == NULL) { | 514 if (oldfile == NULL) { |
372 errno = EBADF; | 515 errno = EBADF; |
373 return -1; | 516 return -1; |
374 } | 517 } |
375 int newfd = fds_.Alloc(); | 518 int newfd = fds_.Alloc(); |
376 FileDescriptor *newfile = fds_.At(newfd); | 519 FileDescriptor *newfile = fds_.At(newfd); |
377 int h = oldfile->handle; | 520 int h = oldfile->handle; |
378 newfile->handle = h; | 521 newfile->handle = h; |
379 FileHandle *handle = open_files_.At(h); | 522 FileHandle* handle = open_files_.At(h); |
380 // init should be safe because we have the kernel proxy lock | 523 // init should be safe because we have the kernel proxy lock |
381 if (pthread_mutex_init(&handle->lock, NULL)) assert(0); | 524 if (pthread_mutex_init(&handle->lock, NULL)) assert(0); |
382 | 525 |
383 if (handle == NULL) { | 526 if (handle == NULL) { |
384 errno = EBADF; | 527 errno = EBADF; |
385 return -1; | 528 return -1; |
386 } | 529 } |
387 ++handle->use_count; | 530 ++handle->use_count; |
388 handle->mount->Ref(handle->node); | 531 handle->mount->Ref(handle->node); |
389 handle->mount->Ref(); | 532 handle->mount->Ref(); |
390 return newfd; | 533 return newfd; |
391 } | 534 } |
392 | 535 |
393 off_t KernelProxy::lseek(int fd, off_t offset, int whence) { | 536 off_t KernelProxy::lseek(int fd, off_t offset, int whence) { |
394 FileHandle *handle; | 537 FileHandle* handle; |
395 // check if fd is valid and handle exists | 538 // check if fd is valid and handle exists |
396 if (!(handle = GetFileHandle(fd))) { | 539 if (!(handle = GetFileHandle(fd))) { |
397 errno = EBADF; | 540 errno = EBADF; |
398 return -1; | 541 return -1; |
399 } | 542 } |
400 | 543 |
401 SimpleAutoLock(&handle->lock); | 544 SimpleAutoLock(&handle->lock); |
402 | 545 |
403 off_t next; | 546 off_t next; |
404 ssize_t len; | 547 ssize_t len; |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
611 ((amode & X_OK) && !(mode & X_OK))) { | 754 ((amode & X_OK) && !(mode & X_OK))) { |
612 errno = EACCES; | 755 errno = EACCES; |
613 return -1; | 756 return -1; |
614 } | 757 } |
615 } | 758 } |
616 // By now we have checked access permissions for | 759 // By now we have checked access permissions for |
617 // each component of the path. | 760 // each component of the path. |
618 return 0; | 761 return 0; |
619 } | 762 } |
620 | 763 |
621 KernelProxy::FileHandle *KernelProxy::GetFileHandle(int fd) { | 764 KernelProxy::FileHandle* KernelProxy::GetFileHandle(int fd) { |
622 SimpleAutoLock lock(&kp_lock_); | 765 SimpleAutoLock lock(&kp_lock_); |
623 FileDescriptor *file = fds_.At(fd); | 766 FileDescriptor *file = fds_.At(fd); |
624 if (!file) { | 767 if (!file) { |
625 return NULL; | 768 return NULL; |
626 } | 769 } |
627 return open_files_.At(file->handle); | 770 return open_files_.At(file->handle); |
628 } | 771 } |
629 | 772 |
630 int KernelProxy::mkdir(const std::string& path, mode_t mode) { | 773 int KernelProxy::mkdir(const std::string& path, mode_t mode) { |
631 if (path.empty()) { | 774 if (path.empty()) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
669 if (mm_.InMountRootPath(abs_path)) { | 812 if (mm_.InMountRootPath(abs_path)) { |
670 errno = EBUSY; | 813 errno = EBUSY; |
671 return -1; | 814 return -1; |
672 } | 815 } |
673 | 816 |
674 return mount->Rmdir(buf.st_ino); | 817 return mount->Rmdir(buf.st_ino); |
675 } | 818 } |
676 | 819 |
677 #ifdef __GLIBC__ | 820 #ifdef __GLIBC__ |
678 int KernelProxy::socket(int domain, int type, int protocol) { | 821 int KernelProxy::socket(int domain, int type, int protocol) { |
679 errno = ENOSYS; | 822 SimpleAutoLock lock(&kp_lock_); |
680 fprintf(stderr, "socket has not been implemented!\n"); | 823 int handle_slot = open_files_.Alloc(); |
681 return -1; | 824 int fd = fds_.Alloc(); |
825 FileDescriptor* file = fds_.At(fd); | |
826 file->handle = handle_slot; | |
827 FileHandle* handle = open_files_.At(handle_slot); | |
828 // this means it is a socket (not a mount handle) and it's implementation | |
829 // determined by handle->stream type is defined later | |
830 handle->mount = NULL; | |
831 handle->stream = NULL; | |
832 handle->use_count = 1; | |
833 return fd; | |
682 } | 834 } |
683 | 835 |
684 int KernelProxy::accept(int sockfd, struct sockaddr *addr, | 836 int KernelProxy::accept(int sockfd, struct sockaddr *addr, |
685 socklen_t* addrlen) { | 837 socklen_t* addrlen) { |
686 errno = ENOSYS; | 838 if (GetFileHandle(sockfd) == 0) return EBADF; |
687 fprintf(stderr, "accept has not been implemented!\n"); | 839 FileStream* ret = socket_subsystem_->accept(GetFileHandle(sockfd)->stream, |
688 return -1; | 840 addr, addrlen); |
841 if (!ret) | |
842 return AddFileStream(ret); | |
843 else | |
844 return -1; | |
689 } | 845 } |
690 | 846 |
691 int KernelProxy::bind(int sockfd, const struct sockaddr *addr, | 847 int KernelProxy::bind(int sockfd, const struct sockaddr *addr, |
692 socklen_t addrlen) { | 848 socklen_t addrlen) { |
693 errno = ENOSYS; | 849 if (GetFileHandle(sockfd) == 0) return EBADF; |
Dmitry Polukhin
2012/05/30 15:13:32
It is formally allowed to put simple then statemen
vissi
2012/05/31 13:35:21
Done.
| |
694 fprintf(stderr, "bind has not been implemented!\n"); | 850 struct sockaddr_in* in_addr = (struct sockaddr_in*)addr; |
695 return -1; | 851 return socket_subsystem_->bind(&(GetFileHandle(sockfd)->stream), |
852 in_addr->sin_addr.s_addr, | |
Dmitry Polukhin
2012/05/30 15:13:32
Here and below need 2 idents.
vissi
2012/05/31 13:35:21
Done.
| |
853 (unsigned short) ntohs(in_addr->sin_port)); | |
696 } | 854 } |
697 | 855 |
698 int KernelProxy::listen(int sockfd, int backlog) { | 856 int KernelProxy::listen(int sockfd, int backlog) { |
699 errno = ENOSYS; | 857 if (GetFileHandle(sockfd) == 0) return EBADF; |
700 fprintf(stderr, "listen has not been implemented!\n"); | 858 return socket_subsystem_->listen(GetFileHandle(sockfd)->stream, backlog); |
701 return -1; | |
702 } | 859 } |
703 | 860 |
704 int KernelProxy::connect(int sockfd, const struct sockaddr *addr, | 861 int KernelProxy::connect(int sockfd, const struct sockaddr *addr, |
705 socklen_t addrlen) { | 862 socklen_t addrlen) { |
706 errno = ENOSYS; | 863 if (GetFileHandle(sockfd) == 0) return EBADF; |
707 fprintf(stderr, "connect has not been implemented!\n"); | 864 struct sockaddr_in* in_addr = (struct sockaddr_in*)addr; |
708 return -1; | 865 return socket_subsystem_->connect(&(GetFileHandle(sockfd)->stream), |
866 in_addr->sin_addr.s_addr, | |
867 (unsigned short) ntohs(in_addr->sin_port)); | |
709 } | 868 } |
710 | 869 |
711 int KernelProxy::send(int sockfd, const void *buf, size_t len, int flags) { | 870 int KernelProxy::send(int sockfd, const void *buf, size_t len, int flags) { |
712 errno = ENOSYS; | 871 size_t nwr; |
713 fprintf(stderr, "send has not been implemented!\n"); | 872 if (GetFileHandle(sockfd) == 0) return EBADF; |
714 return -1; | 873 socket_subsystem_->write(GetFileHandle(sockfd)->stream, (const char*)buf, |
874 len, &nwr); | |
875 return nwr; | |
715 } | 876 } |
716 | 877 |
717 int KernelProxy::sendmsg(int sockfd, const struct msghdr *msg, int flags) { | 878 int KernelProxy::sendmsg(int sockfd, const struct msghdr *msg, int flags) { |
718 errno = ENOSYS; | 879 errno = ENOSYS; |
719 fprintf(stderr, "sendmsg has not been implemented!\n"); | 880 fprintf(stderr, "sendmsg has not been implemented!\n"); |
720 return -1; | 881 return -1; |
721 } | 882 } |
722 | 883 |
723 int KernelProxy::sendto(int sockfd, const void *buf, size_t len, int flags, | 884 int KernelProxy::sendto(int sockfd, const void *buf, size_t len, int flags, |
724 const struct sockaddr *dest_addr, socklen_t addrlen) { | 885 const struct sockaddr *dest_addr, socklen_t addrlen) { |
725 errno = ENOSYS; | 886 errno = ENOSYS; |
726 fprintf(stderr, "sendto has not been implemented!\n"); | 887 fprintf(stderr, "sendto has not been implemented!\n"); |
727 return -1; | 888 return -1; |
728 } | 889 } |
729 | 890 |
730 int KernelProxy::recv(int sockfd, void *buf, size_t len, int flags) { | 891 int KernelProxy::recv(int sockfd, void *buf, size_t len, int flags) { |
731 errno = ENOSYS; | 892 size_t nread; |
Dmitry Polukhin
2012/05/30 15:13:32
It is C style to put variable declaration at the b
vissi
2012/05/31 13:35:21
Done.
| |
732 fprintf(stderr, "recv has not been implemented!\n"); | 893 if (GetFileHandle(sockfd) == 0) return EBADF; |
733 return -1; | 894 socket_subsystem_->read(GetFileHandle(sockfd)->stream, |
895 reinterpret_cast<char*>(buf), len, &nread); | |
896 return nread; | |
734 } | 897 } |
735 | 898 |
736 int KernelProxy::recvmsg(int sockfd, struct msghdr *msg, int flags) { | 899 int KernelProxy::recvmsg(int sockfd, struct msghdr *msg, int flags) { |
737 errno = ENOSYS; | 900 errno = ENOSYS; |
738 fprintf(stderr, "recvmsg has not been implemented!\n"); | 901 fprintf(stderr, "recvmsg has not been implemented!\n"); |
739 return -1; | 902 return -1; |
740 } | 903 } |
741 | 904 |
742 int KernelProxy::recvfrom(int sockfd, void *buf, size_t len, int flags, | 905 int KernelProxy::recvfrom(int sockfd, void *buf, size_t len, int flags, |
743 struct sockaddr *dest_addr, socklen_t* addrlen) { | 906 struct sockaddr *dest_addr, socklen_t* addrlen) { |
744 errno = ENOSYS; | 907 errno = ENOSYS; |
745 fprintf(stderr, "recvfrom has not been implemented!\n"); | 908 fprintf(stderr, "recvfrom has not been implemented!\n"); |
746 return -1; | 909 return -1; |
747 } | 910 } |
748 | 911 |
749 int KernelProxy::select(int nfds, fd_set *readfds, fd_set *writefds, | 912 int KernelProxy::select(int nfds, fd_set *readfds, fd_set *writefds, |
750 fd_set* exceptfds, const struct timeval *timeout) { | 913 fd_set* exceptfds, const struct timeval *timeout) { |
751 errno = ENOSYS; | 914 |
752 fprintf(stderr, "select has not been implemented!\n"); | 915 timespec ts_abs; |
753 return -1; | 916 if (timeout) { |
917 timespec ts; | |
918 TIMEVAL_TO_TIMESPEC(timeout, &ts); | |
919 timeval tv_now; | |
920 gettimeofday(&tv_now, NULL); | |
921 int64_t current_time_us = | |
922 tv_now.tv_sec * kMicrosecondsPerSecond + tv_now.tv_usec; | |
923 int64_t wakeup_time_us = | |
924 current_time_us + | |
925 timeout->tv_sec * kMicrosecondsPerSecond + timeout->tv_usec; | |
926 ts_abs.tv_sec = wakeup_time_us / kMicrosecondsPerSecond; | |
927 ts_abs.tv_nsec = | |
928 (wakeup_time_us - ts_abs.tv_sec * kMicrosecondsPerSecond) * | |
929 kNanosecondsPerMicrosecond; | |
930 } | |
931 | |
932 while (!(IsReady(nfds, readfds, &FileStream::is_read_ready, false) || | |
933 IsReady(nfds, writefds, &FileStream::is_write_ready, false) || | |
934 IsReady(nfds, exceptfds, &FileStream::is_exception, false))) { | |
935 Mutex::Lock lock(select_mutex()); | |
936 if (timeout) { | |
937 if (!timeout->tv_sec && !timeout->tv_usec) | |
938 break; | |
939 | |
940 if (select_cond().timedwait(select_mutex(), &ts_abs)) { | |
941 if (errno == ETIMEDOUT) | |
942 break; | |
943 else | |
944 return -1; | |
945 } | |
946 } else { | |
947 select_cond().wait(select_mutex()); | |
948 } | |
949 } | |
950 | |
951 int nread = IsReady(nfds, readfds, &FileStream::is_read_ready, true); | |
952 int nwrite = IsReady(nfds, writefds, &FileStream::is_write_ready, true); | |
953 int nexcpt = IsReady(nfds, exceptfds, &FileStream::is_exception, true); | |
954 if (nread < 0 || nwrite < 0 || nexcpt < 0) { | |
955 errno = EBADF; | |
956 return -1; | |
957 } | |
958 return nread + nwrite + nexcpt; | |
754 } | 959 } |
755 | 960 |
756 int KernelProxy::pselect(int nfds, fd_set *readfds, fd_set *writefds, | 961 int KernelProxy::pselect(int nfds, fd_set *readfds, fd_set *writefds, |
757 fd_set* exceptfds, const struct timeval *timeout, void* sigmask) { | 962 fd_set* exceptfds, const struct timeval *timeout, void* sigmask) { |
758 errno = ENOSYS; | 963 errno = ENOSYS; |
759 fprintf(stderr, "pselect has not been implemented!\n"); | 964 fprintf(stderr, "pselect has not been implemented!\n"); |
760 return -1; | 965 return -1; |
761 } | 966 } |
762 | 967 |
763 int KernelProxy::getpeername(int sockfd, struct sockaddr *addr, | 968 int KernelProxy::getpeername(int sockfd, struct sockaddr *addr, |
(...skipping 18 matching lines...) Expand all Loading... | |
782 } | 987 } |
783 | 988 |
784 int KernelProxy::setsockopt(int sockfd, int level, int optname, | 989 int KernelProxy::setsockopt(int sockfd, int level, int optname, |
785 const void *optval, socklen_t optlen) { | 990 const void *optval, socklen_t optlen) { |
786 errno = ENOSYS; | 991 errno = ENOSYS; |
787 fprintf(stderr, "setsockopt has not been implemented!\n"); | 992 fprintf(stderr, "setsockopt has not been implemented!\n"); |
788 return -1; | 993 return -1; |
789 } | 994 } |
790 | 995 |
791 int KernelProxy::shutdown(int sockfd, int how) { | 996 int KernelProxy::shutdown(int sockfd, int how) { |
792 errno = ENOSYS; | 997 if (GetFileHandle(sockfd) == 0) return EBADF; |
793 fprintf(stderr, "shutdown has not been implemented!\n"); | 998 return socket_subsystem_->shutdown(GetFileHandle(sockfd)->stream, how); |
794 return -1; | |
795 } | 999 } |
796 | 1000 |
797 int KernelProxy::epoll_create(int size) { | 1001 int KernelProxy::epoll_create(int size) { |
798 errno = ENOSYS; | 1002 errno = ENOSYS; |
799 fprintf(stderr, "epoll_create has not been implemented!\n"); | 1003 fprintf(stderr, "epoll_create has not been implemented!\n"); |
800 return -1; | 1004 return -1; |
801 } | 1005 } |
802 | 1006 |
803 int KernelProxy::epoll_create1(int flags) { | 1007 int KernelProxy::epoll_create1(int flags) { |
804 errno = ENOSYS; | 1008 errno = ENOSYS; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
841 | 1045 |
842 int KernelProxy::ppoll(struct pollfd *fds, nfds_t nfds, | 1046 int KernelProxy::ppoll(struct pollfd *fds, nfds_t nfds, |
843 const struct timespec *timeout, | 1047 const struct timespec *timeout, |
844 const sigset_t *sigmask, size_t sigset_size) { | 1048 const sigset_t *sigmask, size_t sigset_size) { |
845 errno = ENOSYS; | 1049 errno = ENOSYS; |
846 fprintf(stderr, "ppoll has not been implemented!\n"); | 1050 fprintf(stderr, "ppoll has not been implemented!\n"); |
847 return -1; | 1051 return -1; |
848 } | 1052 } |
849 #endif | 1053 #endif |
850 | 1054 |
OLD | NEW |