Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(285)

Side by Side Diff: libraries/nacl-mounts/base/KernelProxy.cc

Issue 10392070: Socket subsystem implementation (Closed) Base URL: http://naclports.googlecode.com/svn/trunk/src/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « libraries/nacl-mounts/base/KernelProxy.h ('k') | libraries/nacl-mounts/base/MountManager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #ifndef __GLIBC__
9 #include <nacl-mounts/net/newlib_compat.h>
10 #else
11 #include <netinet/in.h>
12 #endif
13 #include <sys/time.h>
7 #include <list> 14 #include <list>
8 #include <utility> 15 #include <utility>
9 #include "../console/ConsoleMount.h" 16 #include "console/ConsoleMount.h"
10 #include "../dev/DevMount.h" 17 #include "dev/DevMount.h"
11 #include "KernelProxy.h"
12 #include "MountManager.h" 18 #include "MountManager.h"
19 #include "net/BaseSocketSubSystem.h"
20 #include "net/SocketSubSystem.h"
21 #include "util/DebugPrint.h"
13 22
14 static pthread_once_t kp_once_ = PTHREAD_ONCE_INIT; 23 static pthread_once_t kp_once_ = PTHREAD_ONCE_INIT;
15 KernelProxy *KernelProxy::kp_instance_; 24 KernelProxy *KernelProxy::kp_instance_;
16 25
17 #ifndef MAXPATHLEN 26 #ifndef MAXPATHLEN
18 #define MAXPATHLEN 256 27 #define MAXPATHLEN 256
19 #endif 28 #endif
20 29
30 static const int64_t kMicrosecondsPerSecond = 1000 * 1000;
31 static const int64_t kNanosecondsPerMicrosecond = 1000;
32
21 KernelProxy::KernelProxy() { 33 KernelProxy::KernelProxy() {
22 if (pthread_mutex_init(&kp_lock_, NULL)) assert(0); 34 if (pthread_mutex_init(&kp_lock_, NULL)) assert(0);
23 cwd_ = Path("/"); 35 cwd_ = Path("/");
24 mm_.Init(); 36 mm_.Init();
25 37
26 // Setup file descriptors 0, 1, and 2 for STDIN, STDOUT, and STDERR 38 // Setup file descriptors 0, 1, and 2 for STDIN, STDOUT, and STDERR
27 int ret = mkdir("/dev", 0777); 39 int ret = mkdir("/dev", 0777);
28 assert(ret == 0); 40 assert(ret == 0);
29 DevMount* dev_mount = new DevMount(); 41 DevMount* dev_mount = new DevMount();
30 ret = mm_.AddMount(dev_mount, "/dev"); 42 ret = mm_.AddMount(dev_mount, "/dev");
31 assert(ret == 0); 43 assert(ret == 0);
32 ret = mkdir("/dev/fd", 0777); 44 ret = mkdir("/dev/fd", 0777);
33 assert(ret == 0); 45 assert(ret == 0);
34 ConsoleMount *console_mount = new ConsoleMount(); 46 ConsoleMount *console_mount = new ConsoleMount();
35 ret = mm_.AddMount(console_mount, "/dev/fd"); 47 ret = mm_.AddMount(console_mount, "/dev/fd");
36 assert(ret == 0); 48 assert(ret == 0);
37 int fd = open("/dev/fd/0", O_CREAT | O_RDWR, 0); 49 int fd = open("/dev/fd/0", O_CREAT | O_RDWR, 0);
38 assert(fd == 0); 50 assert(fd == 0);
39 fd = open("/dev/fd/1", O_CREAT | O_RDWR, 0); 51 fd = open("/dev/fd/1", O_CREAT | O_RDWR, 0);
40 assert(fd == 1); 52 assert(fd == 1);
41 fd = open("/dev/fd/2", O_CREAT | O_RDWR, 0); 53 fd = open("/dev/fd/2", O_CREAT | O_RDWR, 0);
42 assert(fd == 2); 54 assert(fd == 2);
43 } 55 }
44 56
57 void KernelProxy::SetSocketSubSystem(BaseSocketSubSystem* bss) {
58 socket_subsystem_ = bss;
59 }
60
61 void KernelProxy::RemoveSocket(int fd) {
62 this->close(fd);
63 }
64
65 int KernelProxy::AddSocket(Socket* stream) {
66 SimpleAutoLock lock(&kp_lock_);
67
68 // Setup file handle.
69 int handle_slot = open_files_.Alloc();
70 int fd = fds_.Alloc();
71 FileDescriptor* file = fds_.At(fd);
72 file->handle = handle_slot;
73 FileHandle* handle = open_files_.At(handle_slot);
74
75 // init should be safe because we have the kernel proxy lock
76 if (pthread_mutex_init(&handle->lock, NULL)) assert(0);
77
78 handle->mount = reinterpret_cast<Mount*>(NULL);
79 handle->stream = reinterpret_cast<Socket*>(NULL);
80 handle->use_count = 1;
81
82 return fd;
83 }
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
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 handle->stream = NULL;
186 handle->node = st.st_ino; 227 handle->node = st.st_ino;
187 handle->flags = flags; 228 handle->flags = flags;
188 handle->use_count = 1; 229 handle->use_count = 1;
189 230
190 if (flags & O_APPEND) { 231 if (flags & O_APPEND) {
191 handle->offset = st.st_size; 232 handle->offset = st.st_size;
192 } else { 233 } else {
193 handle->offset = 0; 234 handle->offset = 0;
194 } 235 }
195 236
196 return fd; 237 return fd;
197 } 238 }
198 239
240 static __thread struct hostent* ghn_res = NULL;
241 static __thread char** ghn_addr_list = NULL;
242 static __thread char* ghn_item = NULL;
243
244 struct hostent* KernelProxy::gethostbyname(const char* name) {
245 struct hostent *res = ghn_res == NULL
246 ? (ghn_res = (struct hostent*) malloc(sizeof(struct hostent)))
247 : ghn_res;
248 if (!res) return NULL;
249 res->h_addr_list = ghn_addr_list == NULL
250 ? (ghn_addr_list = reinterpret_cast<char**>(malloc(sizeof(char*) * 2)))
251 : ghn_addr_list;
252 if (!res->h_addr_list) return NULL;
253 res->h_addr_list[0] = ghn_item == NULL
254 ? (ghn_item = reinterpret_cast<char*>(malloc(sizeof(uint32_t))))
255 : ghn_item;
256 if (!res->h_addr_list[0]) return NULL;
257 *(reinterpret_cast<uint32_t*>(res->h_addr_list[0])) =
258 socket_subsystem_->gethostbyname(name);
259 res->h_addr_list[1] = NULL;
260 res->h_length = sizeof(uint32_t);
261 return res;
262 }
263
264 int KernelProxy::getaddrinfo(const char* hostname, const char* servname,
265 const struct addrinfo* hints, struct addrinfo** res) {
266 return socket_subsystem_->getaddrinfo(
267 hostname, servname, hints, res);
268 }
269
270 void KernelProxy::freeaddrinfo(struct addrinfo* ai) {
271 return socket_subsystem_->freeaddrinfo(ai);
272 }
273
274 int KernelProxy::getnameinfo(const struct sockaddr *sa, socklen_t salen,
275 char* host, socklen_t hostlen,
276 char* serv, socklen_t servlen, unsigned int flags) {
277 return socket_subsystem_->getnameinfo(
278 sa, salen, host, hostlen, serv, servlen, flags);
279 }
280
199 int KernelProxy::open(const std::string& path, int flags, mode_t mode) { 281 int KernelProxy::open(const std::string& path, int flags, mode_t mode) {
200 if (path.empty()) { 282 if (path.empty()) {
201 errno = EINVAL; 283 errno = EINVAL;
202 return -1; 284 return -1;
203 } 285 }
204 286
205 Path p(path); 287 Path p(path);
206 if (path[0] != '/') { 288 if (path[0] != '/') {
207 p = Path(cwd_.FormulatePath() + "/" + path); 289 p = Path(cwd_.FormulatePath() + "/" + path);
208 } 290 }
(...skipping 12 matching lines...) Expand all
221 int KernelProxy::close(int fd) { 303 int KernelProxy::close(int fd) {
222 SimpleAutoLock lock(&kp_lock_); 304 SimpleAutoLock lock(&kp_lock_);
223 305
224 FileDescriptor* file = fds_.At(fd); 306 FileDescriptor* file = fds_.At(fd);
225 if (file == NULL) { 307 if (file == NULL) {
226 errno = EBADF; 308 errno = EBADF;
227 return -1; 309 return -1;
228 } 310 }
229 int h = file->handle; 311 int h = file->handle;
230 fds_.Free(fd); 312 fds_.Free(fd);
231 FileHandle *handle = open_files_.At(h); 313 FileHandle* handle = open_files_.At(h);
232 if (handle == NULL) { 314 if (handle == NULL) {
233 errno = EBADF; 315 errno = EBADF;
234 return -1; 316 return -1;
235 } 317 }
236 handle->use_count--; 318 handle->use_count--;
237 ino_t node = handle->node; 319 ino_t node = handle->node;
238 Mount *mount = handle->mount; 320 if (handle->mount) {
239 if (handle->use_count <= 0) { 321 Mount* mount = handle->mount;
240 open_files_.Free(h); 322 if (handle->use_count <= 0) {
241 mount->Unref(node); 323 open_files_.Free(h);
324 mount->Unref(node);
325 }
326 mount->Unref();
327 } else {
328 Socket* stream = handle->stream;
329 socket_subsystem_->close(stream);
330 if (handle->use_count <= 0) {
331 open_files_.Free(h);
332 }
333 return 0;
242 } 334 }
243 mount->Unref();
244 return 0; 335 return 0;
245 } 336 }
246 337
247 ssize_t KernelProxy::read(int fd, void *buf, size_t count) { 338 ssize_t KernelProxy::read(int fd, void *buf, size_t count) {
248 FileHandle *handle; 339 FileHandle* handle;
249 // check if fd is valid and handle exists 340 // check if fd is valid and handle exists
250 if (!(handle = GetFileHandle(fd))) { 341 if (!(handle = GetFileHandle(fd))) {
251 errno = EBADF; 342 errno = EBADF;
252 return -1; 343 return -1;
253 } 344 }
254 345
255 SimpleAutoLock(&handle->lock); 346 if (handle->mount) {
347 SimpleAutoLock(&handle->lock);
348 // Check that this file handle can be read from.
349 if ((handle->flags & O_ACCMODE) == O_WRONLY ||
350 is_dir(handle->mount, handle->node)) {
351 errno = EBADF;
352 return -1;
353 }
256 354
257 // Check that this file handle can be read from. 355 ssize_t n = handle->mount->Read(handle->node, handle->offset, buf, count);
258 if ((handle->flags & O_ACCMODE) == O_WRONLY || 356 if (n > 0) {
259 is_dir(handle->mount, handle->node)) { 357 handle->offset += n;
260 errno = EBADF; 358 }
261 return -1; 359 return n;
360 } else if (handle->stream) {
361 // TODO(vissi): more elaborate implementation
362 return recv(fd, buf, count, 0);
262 } 363 }
263 364 errno = EBADF;
264 ssize_t n = handle->mount->Read(handle->node, handle->offset, buf, count); 365 return -1;
265 if (n > 0) {
266 handle->offset += n;
267 }
268 return n;
269 } 366 }
270 367
271 ssize_t KernelProxy::write(int fd, const void *buf, size_t count) { 368 ssize_t KernelProxy::write(int fd, const void *buf, size_t count) {
272 FileHandle *handle; 369 FileHandle* handle;
273 370
274 // check if fd is valid and handle exists 371 // check if fd is valid and handle exists
275 if (!(handle = GetFileHandle(fd))) { 372 if (!(handle = GetFileHandle(fd))) {
276 errno = EBADF; 373 errno = EBADF;
277 return -1; 374 return -1;
278 } 375 }
279 376
280 SimpleAutoLock(&handle->lock); 377 if (handle->mount) {
378 SimpleAutoLock(&handle->lock);
379 // Check that this file handle can be written to.
380 if ((handle->flags & O_ACCMODE) == O_RDONLY ||
381 is_dir(handle->mount, handle->node)) {
382 errno = EBADF;
383 return -1;
384 }
281 385
282 // Check that this file handle can be written to. 386 ssize_t n = handle->mount->Write(handle->node, handle->offset, buf, count);
283 if ((handle->flags & O_ACCMODE) == O_RDONLY || 387
284 is_dir(handle->mount, handle->node)) { 388 if (n > 0) {
285 errno = EBADF; 389 handle->offset += n;
286 return -1; 390 }
391 return n;
392 } else if (handle->stream) {
393 // TODO(vissi): more elaborate implementation
394 return send(fd, buf, count, 0);
287 } 395 }
288 396 errno = EBADF;
289 ssize_t n = handle->mount->Write(handle->node, handle->offset, buf, count); 397 return -1;
290
291 if (n > 0) {
292 handle->offset += n;
293 }
294 return n;
295 } 398 }
296 399
297 int KernelProxy::fstat(int fd, struct stat *buf) { 400 int KernelProxy::fstat(int fd, struct stat *buf) {
298 FileHandle *handle; 401 FileHandle* handle;
299 402
300 // check if fd is valid and handle exists 403 // check if fd is valid and handle exists
301 if (!(handle = GetFileHandle(fd))) { 404 if (!(handle = GetFileHandle(fd))) {
302 errno = EBADF; 405 errno = EBADF;
303 return -1; 406 return -1;
304 } 407 }
305 SimpleAutoLock(&handle->lock); 408 SimpleAutoLock(&handle->lock);
306 return handle->mount->Stat(handle->node, buf); 409 return handle->mount->Stat(handle->node, buf);
307 } 410 }
308 411
309 int KernelProxy::ioctl(int fd, unsigned long request) { 412 int KernelProxy::ioctl(int fd, unsigned long request) {
310 errno = ENOSYS; 413 errno = ENOSYS;
311 fprintf(stderr, "ioctl has not been implemented!\n"); 414 fprintf(stderr, "ioctl has not been implemented!\n");
312 assert(0); 415 assert(0);
313 return -1; 416 return -1;
314 } 417 }
315 418
316 int KernelProxy::kill(pid_t pid, int sig) { 419 int KernelProxy::kill(pid_t pid, int sig) {
317 errno = ENOSYS; 420 errno = ENOSYS;
318 fprintf(stderr, "kill has not been implemented!\n"); 421 fprintf(stderr, "kill has not been implemented!\n");
319 assert(0); 422 assert(0);
320 return -1; 423 return -1;
321 } 424 }
322 425
323 int KernelProxy::getdents(int fd, void *buf, unsigned int count) { 426 int KernelProxy::getdents(int fd, void *buf, unsigned int count) {
324 FileHandle *handle; 427 FileHandle* handle;
325 428
326 // check if fd is valid and handle exists 429 // check if fd is valid and handle exists
327 if (!(handle = GetFileHandle(fd))) { 430 if (!(handle = GetFileHandle(fd))) {
328 errno = EBADF; 431 errno = EBADF;
329 return -1; 432 return -1;
330 } 433 }
331 434
332 SimpleAutoLock(&handle->lock); 435 SimpleAutoLock(&handle->lock);
333 436
334 int ret = handle->mount->Getdents(handle->node, handle->offset, 437 int ret = handle->mount->Getdents(handle->node, handle->offset,
335 (struct dirent*)buf, count); 438 (struct dirent*)buf, count);
336 439
337 if (ret != -1) { 440 if (ret != -1) {
338 // TODO(bradnelson): think of a better interface for Mount::Getdents. 441 // TODO(bradnelson): think of a better interface for Mount::Getdents.
339 // http://code.google.com/p/naclports/issues/detail?id=18 442 // http://code.google.com/p/naclports/issues/detail?id=18
340 handle->offset += ret / sizeof(struct dirent); 443 handle->offset += ret / sizeof(struct dirent);
341 } 444 }
342 return ret; 445 return ret;
343 } 446 }
344 447
345 int KernelProxy::fsync(int fd) { 448 int KernelProxy::fsync(int fd) {
346 FileHandle *handle; 449 FileHandle* handle;
347 450
348 if (!(handle = GetFileHandle(fd))) { 451 if (!(handle = GetFileHandle(fd))) {
349 errno = EBADF; 452 errno = EBADF;
350 return -1; 453 return -1;
351 } 454 }
352 SimpleAutoLock(&handle->lock); 455 SimpleAutoLock(&handle->lock);
353 return handle->mount->Fsync(handle->node); 456 return handle->mount->Fsync(handle->node);
354 } 457 }
355 458
356 int KernelProxy::isatty(int fd) { 459 int KernelProxy::isatty(int fd) {
357 FileHandle *handle; 460 FileHandle* handle;
358 461
359 if (!(handle = GetFileHandle(fd))) { 462 if (!(handle = GetFileHandle(fd))) {
360 errno = EBADF; 463 errno = EBADF;
361 return 0; 464 return 0;
362 } 465 }
363 SimpleAutoLock(&handle->lock);
364 return handle->mount->Isatty(handle->node); 466 return handle->mount->Isatty(handle->node);
365 } 467 }
366 468
469 int KernelProxy::dup2(int fd, int newfd) {
470 SimpleAutoLock lock(&kp_lock_);
471 int handle_slot = fds_.At(fd)->handle;
472 if (fds_.AllocAt(fd) != fd) return -1;
473 FileDescriptor* file = fds_.At(newfd);
474 file->handle = handle_slot;
475 return 0;
476 }
477
478 int KernelProxy::IsReady(int nfds, fd_set* fds,
479 bool (Socket::*is_ready)(), bool apply) {
480 if (!fds)
481 return 0;
482
483 int nset = 0;
484 for (int i = 0; i < nfds; i++) {
485 if (FD_ISSET(i, fds)) {
486 Socket* stream = GetFileHandle(i) > 0 ?
487 GetFileHandle(i)->stream : NULL;
488 if (!stream)
489 return -1;
490 if ((stream->*is_ready)()) {
491 if (!apply)
492 return 1;
493 else
494 nset++;
495 } else {
496 if (apply)
497 FD_CLR(i, fds);
498 }
499 }
500 }
501 return nset;
502 }
503
367 int KernelProxy::dup(int oldfd) { 504 int KernelProxy::dup(int oldfd) {
368 SimpleAutoLock lock(&kp_lock_); 505 SimpleAutoLock lock(&kp_lock_);
369 506
507 FileHandle* fh = GetFileHandle(oldfd);
508 if (!fh) {
509 errno = EBADF;
510 return -1;
511 }
512 if (fh->mount == NULL) {
513 Socket* stream = GetFileHandle(oldfd) > 0
514 ? GetFileHandle(oldfd)->stream : NULL;
515 if (!stream)
516 return EBADF;
517 SimpleAutoLock lock(&kp_lock_);
518 int handle_slot = fds_.At(oldfd)->handle;
519 int newfd = fds_.Alloc();
520 FileDescriptor* file = fds_.At(newfd);
521 file->handle = handle_slot;
522 return newfd;
523 }
370 FileDescriptor* oldfile = fds_.At(oldfd); 524 FileDescriptor* oldfile = fds_.At(oldfd);
371 if (oldfile == NULL) { 525 if (oldfile == NULL) {
372 errno = EBADF; 526 errno = EBADF;
373 return -1; 527 return -1;
374 } 528 }
375 int newfd = fds_.Alloc(); 529 int newfd = fds_.Alloc();
376 FileDescriptor *newfile = fds_.At(newfd); 530 FileDescriptor *newfile = fds_.At(newfd);
377 int h = oldfile->handle; 531 int h = oldfile->handle;
378 newfile->handle = h; 532 newfile->handle = h;
379 FileHandle *handle = open_files_.At(h); 533 FileHandle* handle = open_files_.At(h);
380 // init should be safe because we have the kernel proxy lock 534 // init should be safe because we have the kernel proxy lock
381 if (pthread_mutex_init(&handle->lock, NULL)) assert(0); 535 if (pthread_mutex_init(&handle->lock, NULL)) assert(0);
382 536
383 if (handle == NULL) { 537 if (handle == NULL) {
384 errno = EBADF; 538 errno = EBADF;
385 return -1; 539 return -1;
386 } 540 }
387 ++handle->use_count; 541 ++handle->use_count;
388 handle->mount->Ref(handle->node); 542 handle->mount->Ref(handle->node);
389 handle->mount->Ref(); 543 handle->mount->Ref();
390 return newfd; 544 return newfd;
391 } 545 }
392 546
393 off_t KernelProxy::lseek(int fd, off_t offset, int whence) { 547 off_t KernelProxy::lseek(int fd, off_t offset, int whence) {
394 FileHandle *handle; 548 FileHandle* handle;
395 // check if fd is valid and handle exists 549 // check if fd is valid and handle exists
396 if (!(handle = GetFileHandle(fd))) { 550 if (!(handle = GetFileHandle(fd))) {
397 errno = EBADF; 551 errno = EBADF;
398 return -1; 552 return -1;
399 } 553 }
400 554
401 SimpleAutoLock(&handle->lock); 555 SimpleAutoLock(&handle->lock);
402 556
403 off_t next; 557 off_t next;
404 ssize_t len; 558 ssize_t len;
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 ((amode & X_OK) && !(mode & X_OK))) { 765 ((amode & X_OK) && !(mode & X_OK))) {
612 errno = EACCES; 766 errno = EACCES;
613 return -1; 767 return -1;
614 } 768 }
615 } 769 }
616 // By now we have checked access permissions for 770 // By now we have checked access permissions for
617 // each component of the path. 771 // each component of the path.
618 return 0; 772 return 0;
619 } 773 }
620 774
621 KernelProxy::FileHandle *KernelProxy::GetFileHandle(int fd) { 775 KernelProxy::FileHandle* KernelProxy::GetFileHandle(int fd) {
622 SimpleAutoLock lock(&kp_lock_); 776 SimpleAutoLock lock(&kp_lock_);
623 FileDescriptor *file = fds_.At(fd); 777 FileDescriptor *file = fds_.At(fd);
624 if (!file) { 778 if (!file) {
625 return NULL; 779 return NULL;
626 } 780 }
627 return open_files_.At(file->handle); 781 return open_files_.At(file->handle);
628 } 782 }
629 783
630 int KernelProxy::mkdir(const std::string& path, mode_t mode) { 784 int KernelProxy::mkdir(const std::string& path, mode_t mode) {
631 if (path.empty()) { 785 if (path.empty()) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 821
668 // Check if a mount is mounted on abs_path or on a subdirectory of abs_path 822 // Check if a mount is mounted on abs_path or on a subdirectory of abs_path
669 if (mm_.InMountRootPath(abs_path)) { 823 if (mm_.InMountRootPath(abs_path)) {
670 errno = EBUSY; 824 errno = EBUSY;
671 return -1; 825 return -1;
672 } 826 }
673 827
674 return mount->Rmdir(buf.st_ino); 828 return mount->Rmdir(buf.st_ino);
675 } 829 }
676 830
677 #ifdef __GLIBC__
678 int KernelProxy::socket(int domain, int type, int protocol) { 831 int KernelProxy::socket(int domain, int type, int protocol) {
679 errno = ENOSYS; 832 SimpleAutoLock lock(&kp_lock_);
680 fprintf(stderr, "socket has not been implemented!\n"); 833 int handle_slot = open_files_.Alloc();
681 return -1; 834 int fd = fds_.Alloc();
835 FileDescriptor* file = fds_.At(fd);
836 file->handle = handle_slot;
837 FileHandle* handle = open_files_.At(handle_slot);
838 // this means it is a socket (not a mount handle) and it's implementation
839 // determined by handle->stream type is defined later
840 handle->mount = NULL;
841 handle->stream = NULL;
842 handle->use_count = 1;
843 return fd;
682 } 844 }
683 845
684 int KernelProxy::accept(int sockfd, struct sockaddr *addr, 846 int KernelProxy::accept(int sockfd, struct sockaddr *addr,
685 socklen_t* addrlen) { 847 socklen_t* addrlen) {
686 errno = ENOSYS; 848 if (GetFileHandle(sockfd) == 0)
687 fprintf(stderr, "accept has not been implemented!\n"); 849 return EBADF;
688 return -1; 850 Socket* ret = socket_subsystem_->accept(GetFileHandle(sockfd)->stream,
851 addr, addrlen);
852 if (ret)
853 return AddSocket(ret);
854 else
855 return -1;
689 } 856 }
690 857
691 int KernelProxy::bind(int sockfd, const struct sockaddr *addr, 858 int KernelProxy::bind(int sockfd, const struct sockaddr *addr,
692 socklen_t addrlen) { 859 socklen_t addrlen) {
693 errno = ENOSYS; 860 if (GetFileHandle(sockfd) == 0)
694 fprintf(stderr, "bind has not been implemented!\n"); 861 return EBADF;
695 return -1; 862 struct sockaddr_in* in_addr = (struct sockaddr_in*)addr;
863 return socket_subsystem_->bind(&(GetFileHandle(sockfd)->stream),
864 addr, addrlen);
696 } 865 }
697 866
698 int KernelProxy::listen(int sockfd, int backlog) { 867 int KernelProxy::listen(int sockfd, int backlog) {
699 errno = ENOSYS; 868 if (GetFileHandle(sockfd) == 0)
700 fprintf(stderr, "listen has not been implemented!\n"); 869 return EBADF;
701 return -1; 870 return socket_subsystem_->listen(GetFileHandle(sockfd)->stream, backlog);
702 } 871 }
703 872
704 int KernelProxy::connect(int sockfd, const struct sockaddr *addr, 873 int KernelProxy::connect(int sockfd, const struct sockaddr *addr,
705 socklen_t addrlen) { 874 socklen_t addrlen) {
706 errno = ENOSYS; 875 if (GetFileHandle(sockfd) == 0)
707 fprintf(stderr, "connect has not been implemented!\n"); 876 return EBADF;
708 return -1; 877 struct sockaddr_in* in_addr = (struct sockaddr_in*)addr;
878 return socket_subsystem_->connect(&(GetFileHandle(sockfd)->stream),
879 addr, addrlen);
709 } 880 }
710 881
711 int KernelProxy::send(int sockfd, const void *buf, size_t len, int flags) { 882 int KernelProxy::send(int sockfd, const void *buf, size_t len, int flags) {
712 errno = ENOSYS; 883 if (GetFileHandle(sockfd) == 0)
713 fprintf(stderr, "send has not been implemented!\n"); 884 return EBADF;
714 return -1; 885 size_t nwr;
886 socket_subsystem_->write(GetFileHandle(sockfd)->stream, (const char*)buf,
887 len, &nwr);
888 return nwr;
715 } 889 }
716 890
717 int KernelProxy::sendmsg(int sockfd, const struct msghdr *msg, int flags) { 891 int KernelProxy::sendmsg(int sockfd, const struct msghdr *msg, int flags) {
718 errno = ENOSYS; 892 errno = ENOSYS;
719 fprintf(stderr, "sendmsg has not been implemented!\n"); 893 fprintf(stderr, "sendmsg has not been implemented!\n");
720 return -1; 894 return -1;
721 } 895 }
722 896
723 int KernelProxy::sendto(int sockfd, const void *buf, size_t len, int flags, 897 int KernelProxy::sendto(int sockfd, const void *buf, size_t len, int flags,
724 const struct sockaddr *dest_addr, socklen_t addrlen) { 898 const struct sockaddr *dest_addr, socklen_t addrlen) {
725 errno = ENOSYS; 899 errno = ENOSYS;
726 fprintf(stderr, "sendto has not been implemented!\n"); 900 fprintf(stderr, "sendto has not been implemented!\n");
727 return -1; 901 return -1;
728 } 902 }
729 903
730 int KernelProxy::recv(int sockfd, void *buf, size_t len, int flags) { 904 int KernelProxy::recv(int sockfd, void *buf, size_t len, int flags) {
731 errno = ENOSYS; 905 if (GetFileHandle(sockfd) == 0)
732 fprintf(stderr, "recv has not been implemented!\n"); 906 return EBADF;
733 return -1; 907 size_t nread;
908 socket_subsystem_->read(GetFileHandle(sockfd)->stream,
909 reinterpret_cast<char*>(buf), len, &nread);
910 return nread;
734 } 911 }
735 912
736 int KernelProxy::recvmsg(int sockfd, struct msghdr *msg, int flags) { 913 int KernelProxy::recvmsg(int sockfd, struct msghdr *msg, int flags) {
737 errno = ENOSYS; 914 errno = ENOSYS;
738 fprintf(stderr, "recvmsg has not been implemented!\n"); 915 fprintf(stderr, "recvmsg has not been implemented!\n");
739 return -1; 916 return -1;
740 } 917 }
741 918
742 int KernelProxy::recvfrom(int sockfd, void *buf, size_t len, int flags, 919 int KernelProxy::recvfrom(int sockfd, void *buf, size_t len, int flags,
743 struct sockaddr *dest_addr, socklen_t* addrlen) { 920 struct sockaddr *dest_addr, socklen_t* addrlen) {
744 errno = ENOSYS; 921 errno = ENOSYS;
745 fprintf(stderr, "recvfrom has not been implemented!\n"); 922 fprintf(stderr, "recvfrom has not been implemented!\n");
746 return -1; 923 return -1;
747 } 924 }
748 925
926 #ifndef TIMEVAL_TO_TIMESPEC
927 /* Macros for converting between `struct timeval' and `struct timespec'. */
928 # define TIMEVAL_TO_TIMESPEC(tv, ts) { \
929 (ts)->tv_sec = (tv)->tv_sec; \
930 (ts)->tv_nsec = (tv)->tv_usec * 1000; \
931 }
932 # define TIMESPEC_TO_TIMEVAL(tv, ts) { \
933 (tv)->tv_sec = (ts)->tv_sec; \
934 (tv)->tv_usec = (ts)->tv_nsec / 1000; \
935 }
936 #endif
937
749 int KernelProxy::select(int nfds, fd_set *readfds, fd_set *writefds, 938 int KernelProxy::select(int nfds, fd_set *readfds, fd_set *writefds,
750 fd_set* exceptfds, const struct timeval *timeout) { 939 fd_set* exceptfds, const struct timeval *timeout) {
751 errno = ENOSYS; 940 timespec ts_abs;
752 fprintf(stderr, "select has not been implemented!\n"); 941 if (timeout) {
753 return -1; 942 timespec ts;
943 TIMEVAL_TO_TIMESPEC(timeout, &ts);
944 timeval tv_now;
945 gettimeofday(&tv_now, NULL);
946 int64_t current_time_us =
947 tv_now.tv_sec * kMicrosecondsPerSecond + tv_now.tv_usec;
948 int64_t wakeup_time_us =
949 current_time_us +
950 timeout->tv_sec * kMicrosecondsPerSecond + timeout->tv_usec;
951 ts_abs.tv_sec = wakeup_time_us / kMicrosecondsPerSecond;
952 ts_abs.tv_nsec =
953 (wakeup_time_us - ts_abs.tv_sec * kMicrosecondsPerSecond) *
954 kNanosecondsPerMicrosecond;
955 }
956
957 while (!(IsReady(nfds, readfds, &Socket::is_read_ready, false) ||
958 IsReady(nfds, writefds, &Socket::is_write_ready, false) ||
959 IsReady(nfds, exceptfds, &Socket::is_exception, false))) {
960 SimpleAutoLock lock(select_mutex().get());
961 if (timeout) {
962 if (!timeout->tv_sec && !timeout->tv_usec)
963 break;
964
965 if (select_cond().timedwait(select_mutex(), &ts_abs)) {
966 if (errno == ETIMEDOUT)
967 break;
968 else
969 return -1;
970 }
971 } else {
972 select_cond().wait(select_mutex());
973 }
974 }
975
976 int nread = IsReady(nfds, readfds, &Socket::is_read_ready, true);
977 int nwrite = IsReady(nfds, writefds, &Socket::is_write_ready, true);
978 int nexcpt = IsReady(nfds, exceptfds, &Socket::is_exception, true);
979 if (nread < 0 || nwrite < 0 || nexcpt < 0) {
980 errno = EBADF;
981 return -1;
982 }
983 return nread + nwrite + nexcpt;
754 } 984 }
755 985
756 int KernelProxy::pselect(int nfds, fd_set *readfds, fd_set *writefds, 986 int KernelProxy::pselect(int nfds, fd_set *readfds, fd_set *writefds,
757 fd_set* exceptfds, const struct timeval *timeout, void* sigmask) { 987 fd_set* exceptfds, const struct timeval *timeout, void* sigmask) {
758 errno = ENOSYS; 988 errno = ENOSYS;
759 fprintf(stderr, "pselect has not been implemented!\n"); 989 fprintf(stderr, "pselect has not been implemented!\n");
760 return -1; 990 return -1;
761 } 991 }
762 992
763 int KernelProxy::getpeername(int sockfd, struct sockaddr *addr, 993 int KernelProxy::getpeername(int sockfd, struct sockaddr *addr,
(...skipping 18 matching lines...) Expand all
782 } 1012 }
783 1013
784 int KernelProxy::setsockopt(int sockfd, int level, int optname, 1014 int KernelProxy::setsockopt(int sockfd, int level, int optname,
785 const void *optval, socklen_t optlen) { 1015 const void *optval, socklen_t optlen) {
786 errno = ENOSYS; 1016 errno = ENOSYS;
787 fprintf(stderr, "setsockopt has not been implemented!\n"); 1017 fprintf(stderr, "setsockopt has not been implemented!\n");
788 return -1; 1018 return -1;
789 } 1019 }
790 1020
791 int KernelProxy::shutdown(int sockfd, int how) { 1021 int KernelProxy::shutdown(int sockfd, int how) {
792 errno = ENOSYS; 1022 FileHandle* fh = GetFileHandle(sockfd);
793 fprintf(stderr, "shutdown has not been implemented!\n"); 1023 if (fh == 0)
794 return -1; 1024 return EBADF;
795 } 1025 return socket_subsystem_->shutdown(fh->stream, how);
796
797 int KernelProxy::epoll_create(int size) {
798 errno = ENOSYS;
799 fprintf(stderr, "epoll_create has not been implemented!\n");
800 return -1;
801 }
802
803 int KernelProxy::epoll_create1(int flags) {
804 errno = ENOSYS;
805 fprintf(stderr, "epoll_create1 has not been implemented!\n");
806 return -1;
807 }
808
809 int KernelProxy::epoll_ctl(int epfd, int op, int fd,
810 struct epoll_event *event) {
811 errno = ENOSYS;
812 fprintf(stderr, "epoll_ctl has not been implemented!\n");
813 return -1;
814 }
815
816 int KernelProxy::epoll_wait(int epfd, struct epoll_event *events, int maxevents,
817 int timeout) {
818 errno = ENOSYS;
819 fprintf(stderr, "epoll_wait has not been implemented!\n");
820 return -1;
821 }
822
823 int KernelProxy::epoll_pwait(int epfd, struct epoll_event *events,
824 int maxevents, int timeout, const sigset_t *sigmask, size_t sigset_size) {
825 errno = ENOSYS;
826 fprintf(stderr, "epoll_pwait has not been implemented!\n");
827 return -1;
828 } 1026 }
829 1027
830 int KernelProxy::socketpair(int domain, int type, int protocol, int sv[2]) { 1028 int KernelProxy::socketpair(int domain, int type, int protocol, int sv[2]) {
831 errno = ENOSYS; 1029 errno = ENOSYS;
832 fprintf(stderr, "socketpair has not been implemented!\n"); 1030 fprintf(stderr, "socketpair has not been implemented!\n");
833 return -1; 1031 return -1;
834 } 1032 }
835 1033
836 int KernelProxy::poll(struct pollfd *fds, nfds_t nfds, int timeout) {
837 errno = ENOSYS;
838 fprintf(stderr, "poll has not been implemented!\n");
839 return -1;
840 }
841
842 int KernelProxy::ppoll(struct pollfd *fds, nfds_t nfds,
843 const struct timespec *timeout,
844 const sigset_t *sigmask, size_t sigset_size) {
845 errno = ENOSYS;
846 fprintf(stderr, "ppoll has not been implemented!\n");
847 return -1;
848 }
849 #endif
850
OLDNEW
« no previous file with comments | « libraries/nacl-mounts/base/KernelProxy.h ('k') | libraries/nacl-mounts/base/MountManager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698