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::RemoveSocket(int fd) { |
| 61 this->close(fd); |
| 62 } |
| 63 |
| 64 int KernelProxy::AddSocket(Socket* 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 = (Socket*) 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 static __thread struct hostent* ghn_res = NULL; |
| 243 static __thread char** ghn_addr_list = NULL; |
| 244 static __thread char* ghn_item = NULL; |
| 245 |
| 246 #if __GLIBC__ |
| 247 struct hostent* KernelProxy::gethostbyname(const char* name) { |
| 248 struct hostent *res = ghn_res == NULL |
| 249 ? (ghn_res = (struct hostent*) malloc(sizeof(struct hostent))) |
| 250 : ghn_res; |
| 251 if (!res) return NULL; |
| 252 res->h_addr_list = ghn_addr_list == NULL |
| 253 ? (ghn_addr_list = (char**) malloc(sizeof(char*) * 2)) |
| 254 : ghn_addr_list; |
| 255 if (!res->h_addr_list) return NULL; |
| 256 res->h_addr_list[0] = ghn_item == NULL |
| 257 ? (ghn_item = (char*) malloc(sizeof(long int))) |
| 258 : ghn_item; |
| 259 if (!res->h_addr_list[0]) return NULL; |
| 260 *((long int*)res->h_addr_list[0]) = socket_subsystem_->gethostbyname(name); |
| 261 res->h_addr_list[1] = NULL; |
| 262 res->h_length = sizeof(long int); |
| 263 return res; |
| 264 } |
| 265 |
| 266 int KernelProxy::getaddrinfo(const char* hostname, const char* servname, |
| 267 const struct addrinfo* hints, struct addrinfo** res) { |
| 268 dbgprintf("getaddrinfo: %s %s\n", |
| 269 hostname ? hostname : "", servname ? servname : ""); |
| 270 return socket_subsystem_->getaddrinfo( |
| 271 hostname, servname, hints, res); |
| 272 } |
| 273 |
| 274 void KernelProxy::freeaddrinfo(struct addrinfo* ai) { |
| 275 dbgprintf("freeaddrinfo\n"); |
| 276 return socket_subsystem_->freeaddrinfo(ai); |
| 277 } |
| 278 |
| 279 int KernelProxy::getnameinfo(const struct sockaddr *sa, socklen_t salen, |
| 280 char *host, socklen_t hostlen, |
| 281 char *serv, socklen_t servlen, unsigned int flags) { |
| 282 dbgprintf("getnameinfo\n"); |
| 283 return socket_subsystem_->getnameinfo( |
| 284 sa, salen, host, hostlen, serv, servlen, flags); |
| 285 } |
| 286 |
| 287 #endif |
| 288 |
199 int KernelProxy::open(const std::string& path, int flags, mode_t mode) { | 289 int KernelProxy::open(const std::string& path, int flags, mode_t mode) { |
200 if (path.empty()) { | 290 if (path.empty()) { |
201 errno = EINVAL; | 291 errno = EINVAL; |
202 return -1; | 292 return -1; |
203 } | 293 } |
204 | 294 |
205 Path p(path); | 295 Path p(path); |
206 if (path[0] != '/') { | 296 if (path[0] != '/') { |
207 p = Path(cwd_.FormulatePath() + "/" + path); | 297 p = Path(cwd_.FormulatePath() + "/" + path); |
208 } | 298 } |
209 | 299 |
210 std::pair<Mount *, std::string> m_and_p = | 300 std::pair<Mount *, std::string> m_and_p = |
211 mm_.GetMount(p.FormulatePath()); | 301 mm_.GetMount(p.FormulatePath()); |
212 | 302 |
213 if (!(m_and_p.first)) { | 303 if (!(m_and_p.first)) { |
214 errno = ENOENT; | 304 errno = ENOENT; |
215 return -1; | 305 return -1; |
216 } | 306 } |
217 | 307 |
218 return OpenHandle(m_and_p.first, m_and_p.second, flags, mode); | 308 return OpenHandle(m_and_p.first, m_and_p.second, flags, mode); |
219 } | 309 } |
220 | 310 |
221 int KernelProxy::close(int fd) { | 311 int KernelProxy::close(int fd) { |
222 SimpleAutoLock lock(&kp_lock_); | 312 SimpleAutoLock* lock = new SimpleAutoLock(&kp_lock_); |
223 | 313 |
224 FileDescriptor* file = fds_.At(fd); | 314 FileDescriptor* file = fds_.At(fd); |
225 if (file == NULL) { | 315 if (file == NULL) { |
226 errno = EBADF; | 316 errno = EBADF; |
227 return -1; | 317 return -1; |
228 } | 318 } |
229 int h = file->handle; | 319 int h = file->handle; |
230 fds_.Free(fd); | 320 fds_.Free(fd); |
231 FileHandle *handle = open_files_.At(h); | 321 FileHandle* handle = open_files_.At(h); |
232 if (handle == NULL) { | 322 if (handle == NULL) { |
233 errno = EBADF; | 323 errno = EBADF; |
234 return -1; | 324 return -1; |
235 } | 325 } |
236 handle->use_count--; | 326 handle->use_count--; |
237 ino_t node = handle->node; | 327 ino_t node = handle->node; |
238 Mount *mount = handle->mount; | 328 if (handle->mount) { |
239 if (handle->use_count <= 0) { | 329 Mount* mount = handle->mount; |
240 open_files_.Free(h); | 330 if (handle->use_count <= 0) { |
241 mount->Unref(node); | 331 open_files_.Free(h); |
| 332 mount->Unref(node); |
| 333 } |
| 334 mount->Unref(); |
| 335 #if __GLIBC__ |
| 336 } else { |
| 337 Socket* stream = handle->stream; |
| 338 delete lock; |
| 339 socket_subsystem_->close(stream); |
| 340 SimpleAutoLock lock(&kp_lock_); |
| 341 if (handle->use_count <= 0) { |
| 342 open_files_.Free(h); |
| 343 } |
| 344 return 0; |
| 345 #endif |
242 } | 346 } |
243 mount->Unref(); | 347 delete lock; |
244 return 0; | 348 return 0; |
245 } | 349 } |
246 | 350 |
247 ssize_t KernelProxy::read(int fd, void *buf, size_t count) { | 351 ssize_t KernelProxy::read(int fd, void *buf, size_t count) { |
248 FileHandle *handle; | 352 FileHandle* handle; |
249 // check if fd is valid and handle exists | 353 // check if fd is valid and handle exists |
250 if (!(handle = GetFileHandle(fd))) { | 354 if (!(handle = GetFileHandle(fd))) { |
251 errno = EBADF; | 355 errno = EBADF; |
252 return -1; | 356 return -1; |
253 } | 357 } |
254 | 358 |
255 SimpleAutoLock(&handle->lock); | 359 if (handle->mount) { |
| 360 SimpleAutoLock(&handle->lock); |
| 361 // Check that this file handle can be read from. |
| 362 if ((handle->flags & O_ACCMODE) == O_WRONLY || |
| 363 is_dir(handle->mount, handle->node)) { |
| 364 errno = EBADF; |
| 365 return -1; |
| 366 } |
256 | 367 |
257 // Check that this file handle can be read from. | 368 ssize_t n = handle->mount->Read(handle->node, handle->offset, buf, count); |
258 if ((handle->flags & O_ACCMODE) == O_WRONLY || | 369 if (n > 0) { |
259 is_dir(handle->mount, handle->node)) { | 370 handle->offset += n; |
260 errno = EBADF; | 371 } |
261 return -1; | 372 return n; |
| 373 #if __GLIBC__ |
| 374 } else if (handle->stream) { |
| 375 // TODO(vissi): more elaborate implementation |
| 376 return recv(fd, buf, count, 0); |
| 377 #endif |
262 } | 378 } |
263 | 379 errno = EBADF; |
264 ssize_t n = handle->mount->Read(handle->node, handle->offset, buf, count); | 380 return -1; |
265 if (n > 0) { | |
266 handle->offset += n; | |
267 } | |
268 return n; | |
269 } | 381 } |
270 | 382 |
271 ssize_t KernelProxy::write(int fd, const void *buf, size_t count) { | 383 ssize_t KernelProxy::write(int fd, const void *buf, size_t count) { |
272 FileHandle *handle; | 384 FileHandle* handle; |
273 | 385 |
274 // check if fd is valid and handle exists | 386 // check if fd is valid and handle exists |
275 if (!(handle = GetFileHandle(fd))) { | 387 if (!(handle = GetFileHandle(fd))) { |
276 errno = EBADF; | 388 errno = EBADF; |
277 return -1; | 389 return -1; |
278 } | 390 } |
279 | 391 |
280 SimpleAutoLock(&handle->lock); | 392 if (handle->mount) { |
| 393 SimpleAutoLock(&handle->lock); |
| 394 // Check that this file handle can be written to. |
| 395 if ((handle->flags & O_ACCMODE) == O_RDONLY || |
| 396 is_dir(handle->mount, handle->node)) { |
| 397 errno = EBADF; |
| 398 return -1; |
| 399 } |
281 | 400 |
282 // Check that this file handle can be written to. | 401 ssize_t n = handle->mount->Write(handle->node, handle->offset, buf, count); |
283 if ((handle->flags & O_ACCMODE) == O_RDONLY || | 402 |
284 is_dir(handle->mount, handle->node)) { | 403 if (n > 0) { |
285 errno = EBADF; | 404 handle->offset += n; |
286 return -1; | 405 } |
| 406 return n; |
| 407 #if __GLIBC__ |
| 408 } else if (handle->stream) { |
| 409 // TODO(vissi): more elaborate implementation |
| 410 return send(fd, buf, count, 0); |
| 411 #endif |
287 } | 412 } |
288 | 413 errno = EBADF; |
289 ssize_t n = handle->mount->Write(handle->node, handle->offset, buf, count); | 414 return -1; |
290 | |
291 if (n > 0) { | |
292 handle->offset += n; | |
293 } | |
294 return n; | |
295 } | 415 } |
296 | 416 |
297 int KernelProxy::fstat(int fd, struct stat *buf) { | 417 int KernelProxy::fstat(int fd, struct stat *buf) { |
298 FileHandle *handle; | 418 FileHandle* handle; |
299 | 419 |
300 // check if fd is valid and handle exists | 420 // check if fd is valid and handle exists |
301 if (!(handle = GetFileHandle(fd))) { | 421 if (!(handle = GetFileHandle(fd))) { |
302 errno = EBADF; | 422 errno = EBADF; |
303 return -1; | 423 return -1; |
304 } | 424 } |
305 SimpleAutoLock(&handle->lock); | 425 SimpleAutoLock(&handle->lock); |
306 return handle->mount->Stat(handle->node, buf); | 426 return handle->mount->Stat(handle->node, buf); |
307 } | 427 } |
308 | 428 |
309 int KernelProxy::ioctl(int fd, unsigned long request) { | 429 int KernelProxy::ioctl(int fd, unsigned long request) { |
310 errno = ENOSYS; | 430 errno = ENOSYS; |
311 fprintf(stderr, "ioctl has not been implemented!\n"); | 431 fprintf(stderr, "ioctl has not been implemented!\n"); |
312 assert(0); | 432 assert(0); |
313 return -1; | 433 return -1; |
314 } | 434 } |
315 | 435 |
316 int KernelProxy::kill(pid_t pid, int sig) { | 436 int KernelProxy::kill(pid_t pid, int sig) { |
317 errno = ENOSYS; | 437 errno = ENOSYS; |
318 fprintf(stderr, "kill has not been implemented!\n"); | 438 fprintf(stderr, "kill has not been implemented!\n"); |
319 assert(0); | 439 assert(0); |
320 return -1; | 440 return -1; |
321 } | 441 } |
322 | 442 |
323 int KernelProxy::getdents(int fd, void *buf, unsigned int count) { | 443 int KernelProxy::getdents(int fd, void *buf, unsigned int count) { |
324 FileHandle *handle; | 444 FileHandle* handle; |
325 | 445 |
326 // check if fd is valid and handle exists | 446 // check if fd is valid and handle exists |
327 if (!(handle = GetFileHandle(fd))) { | 447 if (!(handle = GetFileHandle(fd))) { |
328 errno = EBADF; | 448 errno = EBADF; |
329 return -1; | 449 return -1; |
330 } | 450 } |
331 | 451 |
332 SimpleAutoLock(&handle->lock); | 452 SimpleAutoLock(&handle->lock); |
333 | 453 |
334 int ret = handle->mount->Getdents(handle->node, handle->offset, | 454 int ret = handle->mount->Getdents(handle->node, handle->offset, |
335 (struct dirent*)buf, count); | 455 (struct dirent*)buf, count); |
336 | 456 |
337 if (ret != -1) { | 457 if (ret != -1) { |
338 // TODO(bradnelson): think of a better interface for Mount::Getdents. | 458 // TODO(bradnelson): think of a better interface for Mount::Getdents. |
339 // http://code.google.com/p/naclports/issues/detail?id=18 | 459 // http://code.google.com/p/naclports/issues/detail?id=18 |
340 handle->offset += ret / sizeof(struct dirent); | 460 handle->offset += ret / sizeof(struct dirent); |
341 } | 461 } |
342 return ret; | 462 return ret; |
343 } | 463 } |
344 | 464 |
345 int KernelProxy::fsync(int fd) { | 465 int KernelProxy::fsync(int fd) { |
346 FileHandle *handle; | 466 FileHandle* handle; |
347 | 467 |
348 if (!(handle = GetFileHandle(fd))) { | 468 if (!(handle = GetFileHandle(fd))) { |
349 errno = EBADF; | 469 errno = EBADF; |
350 return -1; | 470 return -1; |
351 } | 471 } |
352 SimpleAutoLock(&handle->lock); | 472 SimpleAutoLock(&handle->lock); |
353 return handle->mount->Fsync(handle->node); | 473 return handle->mount->Fsync(handle->node); |
354 } | 474 } |
355 | 475 |
356 int KernelProxy::isatty(int fd) { | 476 int KernelProxy::isatty(int fd) { |
357 FileHandle *handle; | 477 FileHandle* handle; |
358 | 478 |
359 if (!(handle = GetFileHandle(fd))) { | 479 if (!(handle = GetFileHandle(fd))) { |
360 errno = EBADF; | 480 errno = EBADF; |
361 return 0; | 481 return 0; |
362 } | 482 } |
363 SimpleAutoLock(&handle->lock); | |
364 return handle->mount->Isatty(handle->node); | 483 return handle->mount->Isatty(handle->node); |
365 } | 484 } |
366 | 485 |
| 486 int KernelProxy::dup2(int fd, int newfd) { |
| 487 SimpleAutoLock lock(&kp_lock_); |
| 488 int handle_slot = fds_.At(fd)->handle; |
| 489 if (fds_.AllocAt(fd) != fd) return -1; |
| 490 FileDescriptor* file = fds_.At(newfd); |
| 491 file->handle = handle_slot; |
| 492 return 0; |
| 493 } |
| 494 |
| 495 #if __GLIBC__ |
| 496 int KernelProxy::IsReady(int nfds, fd_set* fds, |
| 497 bool (Socket::*is_ready)(), bool apply) { |
| 498 if (!fds) |
| 499 return 0; |
| 500 |
| 501 int nset = 0; |
| 502 for (int i = 0; i < nfds; i++) { |
| 503 if (FD_ISSET(i, fds)) { |
| 504 Socket* stream = GetFileHandle(i) > 0 ? |
| 505 GetFileHandle(i)->stream : NULL; |
| 506 if (!stream) |
| 507 return -1; |
| 508 if ((stream->*is_ready)()) { |
| 509 if (!apply) |
| 510 return 1; |
| 511 else |
| 512 nset++; |
| 513 } else { |
| 514 if (apply) |
| 515 FD_CLR(i, fds); |
| 516 } |
| 517 } |
| 518 } |
| 519 return nset; |
| 520 } |
| 521 #endif |
| 522 |
367 int KernelProxy::dup(int oldfd) { | 523 int KernelProxy::dup(int oldfd) { |
368 SimpleAutoLock lock(&kp_lock_); | 524 SimpleAutoLock lock(&kp_lock_); |
369 | 525 |
| 526 FileHandle* fh = GetFileHandle(oldfd); |
| 527 if (!fh) { |
| 528 errno = EBADF; |
| 529 return -1; |
| 530 } |
| 531 #if __GLIBC__ |
| 532 if (fh->mount == NULL) { |
| 533 Socket* stream = GetFileHandle(oldfd) > 0 |
| 534 ? GetFileHandle(oldfd)->stream : NULL; |
| 535 if (!stream) |
| 536 return EBADF; |
| 537 SimpleAutoLock lock(&kp_lock_); |
| 538 int handle_slot = fds_.At(oldfd)->handle; |
| 539 int newfd = fds_.Alloc(); |
| 540 FileDescriptor* file = fds_.At(newfd); |
| 541 file->handle = handle_slot; |
| 542 return newfd; |
| 543 } |
| 544 #endif |
370 FileDescriptor* oldfile = fds_.At(oldfd); | 545 FileDescriptor* oldfile = fds_.At(oldfd); |
371 if (oldfile == NULL) { | 546 if (oldfile == NULL) { |
372 errno = EBADF; | 547 errno = EBADF; |
373 return -1; | 548 return -1; |
374 } | 549 } |
375 int newfd = fds_.Alloc(); | 550 int newfd = fds_.Alloc(); |
376 FileDescriptor *newfile = fds_.At(newfd); | 551 FileDescriptor *newfile = fds_.At(newfd); |
377 int h = oldfile->handle; | 552 int h = oldfile->handle; |
378 newfile->handle = h; | 553 newfile->handle = h; |
379 FileHandle *handle = open_files_.At(h); | 554 FileHandle* handle = open_files_.At(h); |
380 // init should be safe because we have the kernel proxy lock | 555 // init should be safe because we have the kernel proxy lock |
381 if (pthread_mutex_init(&handle->lock, NULL)) assert(0); | 556 if (pthread_mutex_init(&handle->lock, NULL)) assert(0); |
382 | 557 |
383 if (handle == NULL) { | 558 if (handle == NULL) { |
384 errno = EBADF; | 559 errno = EBADF; |
385 return -1; | 560 return -1; |
386 } | 561 } |
387 ++handle->use_count; | 562 ++handle->use_count; |
388 handle->mount->Ref(handle->node); | 563 handle->mount->Ref(handle->node); |
389 handle->mount->Ref(); | 564 handle->mount->Ref(); |
390 return newfd; | 565 return newfd; |
391 } | 566 } |
392 | 567 |
393 off_t KernelProxy::lseek(int fd, off_t offset, int whence) { | 568 off_t KernelProxy::lseek(int fd, off_t offset, int whence) { |
394 FileHandle *handle; | 569 FileHandle* handle; |
395 // check if fd is valid and handle exists | 570 // check if fd is valid and handle exists |
396 if (!(handle = GetFileHandle(fd))) { | 571 if (!(handle = GetFileHandle(fd))) { |
397 errno = EBADF; | 572 errno = EBADF; |
398 return -1; | 573 return -1; |
399 } | 574 } |
400 | 575 |
401 SimpleAutoLock(&handle->lock); | 576 SimpleAutoLock(&handle->lock); |
402 | 577 |
403 off_t next; | 578 off_t next; |
404 ssize_t len; | 579 ssize_t len; |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 ((amode & X_OK) && !(mode & X_OK))) { | 786 ((amode & X_OK) && !(mode & X_OK))) { |
612 errno = EACCES; | 787 errno = EACCES; |
613 return -1; | 788 return -1; |
614 } | 789 } |
615 } | 790 } |
616 // By now we have checked access permissions for | 791 // By now we have checked access permissions for |
617 // each component of the path. | 792 // each component of the path. |
618 return 0; | 793 return 0; |
619 } | 794 } |
620 | 795 |
621 KernelProxy::FileHandle *KernelProxy::GetFileHandle(int fd) { | 796 KernelProxy::FileHandle* KernelProxy::GetFileHandle(int fd) { |
622 SimpleAutoLock lock(&kp_lock_); | 797 SimpleAutoLock lock(&kp_lock_); |
623 FileDescriptor *file = fds_.At(fd); | 798 FileDescriptor *file = fds_.At(fd); |
624 if (!file) { | 799 if (!file) { |
625 return NULL; | 800 return NULL; |
626 } | 801 } |
627 return open_files_.At(file->handle); | 802 return open_files_.At(file->handle); |
628 } | 803 } |
629 | 804 |
630 int KernelProxy::mkdir(const std::string& path, mode_t mode) { | 805 int KernelProxy::mkdir(const std::string& path, mode_t mode) { |
631 if (path.empty()) { | 806 if (path.empty()) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 if (mm_.InMountRootPath(abs_path)) { | 844 if (mm_.InMountRootPath(abs_path)) { |
670 errno = EBUSY; | 845 errno = EBUSY; |
671 return -1; | 846 return -1; |
672 } | 847 } |
673 | 848 |
674 return mount->Rmdir(buf.st_ino); | 849 return mount->Rmdir(buf.st_ino); |
675 } | 850 } |
676 | 851 |
677 #ifdef __GLIBC__ | 852 #ifdef __GLIBC__ |
678 int KernelProxy::socket(int domain, int type, int protocol) { | 853 int KernelProxy::socket(int domain, int type, int protocol) { |
679 errno = ENOSYS; | 854 SimpleAutoLock lock(&kp_lock_); |
680 fprintf(stderr, "socket has not been implemented!\n"); | 855 int handle_slot = open_files_.Alloc(); |
681 return -1; | 856 int fd = fds_.Alloc(); |
| 857 FileDescriptor* file = fds_.At(fd); |
| 858 file->handle = handle_slot; |
| 859 FileHandle* handle = open_files_.At(handle_slot); |
| 860 // this means it is a socket (not a mount handle) and it's implementation |
| 861 // determined by handle->stream type is defined later |
| 862 handle->mount = NULL; |
| 863 handle->stream = NULL; |
| 864 handle->use_count = 1; |
| 865 return fd; |
682 } | 866 } |
683 | 867 |
684 int KernelProxy::accept(int sockfd, struct sockaddr *addr, | 868 int KernelProxy::accept(int sockfd, struct sockaddr *addr, |
685 socklen_t* addrlen) { | 869 socklen_t* addrlen) { |
686 errno = ENOSYS; | 870 if (GetFileHandle(sockfd) == 0) |
687 fprintf(stderr, "accept has not been implemented!\n"); | 871 return EBADF; |
688 return -1; | 872 Socket* ret = socket_subsystem_->accept(GetFileHandle(sockfd)->stream, |
| 873 addr, addrlen); |
| 874 if (ret) |
| 875 return AddSocket(ret); |
| 876 else |
| 877 return -1; |
689 } | 878 } |
690 | 879 |
691 int KernelProxy::bind(int sockfd, const struct sockaddr *addr, | 880 int KernelProxy::bind(int sockfd, const struct sockaddr *addr, |
692 socklen_t addrlen) { | 881 socklen_t addrlen) { |
693 errno = ENOSYS; | 882 if (GetFileHandle(sockfd) == 0) |
694 fprintf(stderr, "bind has not been implemented!\n"); | 883 return EBADF; |
695 return -1; | 884 struct sockaddr_in* in_addr = (struct sockaddr_in*)addr; |
| 885 return socket_subsystem_->bind(&(GetFileHandle(sockfd)->stream), |
| 886 addr, addrlen); |
696 } | 887 } |
697 | 888 |
698 int KernelProxy::listen(int sockfd, int backlog) { | 889 int KernelProxy::listen(int sockfd, int backlog) { |
699 errno = ENOSYS; | 890 if (GetFileHandle(sockfd) == 0) |
700 fprintf(stderr, "listen has not been implemented!\n"); | 891 return EBADF; |
701 return -1; | 892 return socket_subsystem_->listen(GetFileHandle(sockfd)->stream, backlog); |
702 } | 893 } |
703 | 894 |
704 int KernelProxy::connect(int sockfd, const struct sockaddr *addr, | 895 int KernelProxy::connect(int sockfd, const struct sockaddr *addr, |
705 socklen_t addrlen) { | 896 socklen_t addrlen) { |
706 errno = ENOSYS; | 897 if (GetFileHandle(sockfd) == 0) |
707 fprintf(stderr, "connect has not been implemented!\n"); | 898 return EBADF; |
708 return -1; | 899 struct sockaddr_in* in_addr = (struct sockaddr_in*)addr; |
| 900 return socket_subsystem_->connect(&(GetFileHandle(sockfd)->stream), |
| 901 addr, addrlen); |
709 } | 902 } |
710 | 903 |
711 int KernelProxy::send(int sockfd, const void *buf, size_t len, int flags) { | 904 int KernelProxy::send(int sockfd, const void *buf, size_t len, int flags) { |
712 errno = ENOSYS; | 905 if (GetFileHandle(sockfd) == 0) |
713 fprintf(stderr, "send has not been implemented!\n"); | 906 return EBADF; |
714 return -1; | 907 size_t nwr; |
| 908 socket_subsystem_->write(GetFileHandle(sockfd)->stream, (const char*)buf, |
| 909 len, &nwr); |
| 910 return nwr; |
715 } | 911 } |
716 | 912 |
717 int KernelProxy::sendmsg(int sockfd, const struct msghdr *msg, int flags) { | 913 int KernelProxy::sendmsg(int sockfd, const struct msghdr *msg, int flags) { |
718 errno = ENOSYS; | 914 errno = ENOSYS; |
719 fprintf(stderr, "sendmsg has not been implemented!\n"); | 915 fprintf(stderr, "sendmsg has not been implemented!\n"); |
720 return -1; | 916 return -1; |
721 } | 917 } |
722 | 918 |
723 int KernelProxy::sendto(int sockfd, const void *buf, size_t len, int flags, | 919 int KernelProxy::sendto(int sockfd, const void *buf, size_t len, int flags, |
724 const struct sockaddr *dest_addr, socklen_t addrlen) { | 920 const struct sockaddr *dest_addr, socklen_t addrlen) { |
725 errno = ENOSYS; | 921 errno = ENOSYS; |
726 fprintf(stderr, "sendto has not been implemented!\n"); | 922 fprintf(stderr, "sendto has not been implemented!\n"); |
727 return -1; | 923 return -1; |
728 } | 924 } |
729 | 925 |
730 int KernelProxy::recv(int sockfd, void *buf, size_t len, int flags) { | 926 int KernelProxy::recv(int sockfd, void *buf, size_t len, int flags) { |
731 errno = ENOSYS; | 927 if (GetFileHandle(sockfd) == 0) |
732 fprintf(stderr, "recv has not been implemented!\n"); | 928 return EBADF; |
733 return -1; | 929 size_t nread; |
| 930 socket_subsystem_->read(GetFileHandle(sockfd)->stream, |
| 931 reinterpret_cast<char*>(buf), len, &nread); |
| 932 return nread; |
734 } | 933 } |
735 | 934 |
736 int KernelProxy::recvmsg(int sockfd, struct msghdr *msg, int flags) { | 935 int KernelProxy::recvmsg(int sockfd, struct msghdr *msg, int flags) { |
737 errno = ENOSYS; | 936 errno = ENOSYS; |
738 fprintf(stderr, "recvmsg has not been implemented!\n"); | 937 fprintf(stderr, "recvmsg has not been implemented!\n"); |
739 return -1; | 938 return -1; |
740 } | 939 } |
741 | 940 |
742 int KernelProxy::recvfrom(int sockfd, void *buf, size_t len, int flags, | 941 int KernelProxy::recvfrom(int sockfd, void *buf, size_t len, int flags, |
743 struct sockaddr *dest_addr, socklen_t* addrlen) { | 942 struct sockaddr *dest_addr, socklen_t* addrlen) { |
744 errno = ENOSYS; | 943 errno = ENOSYS; |
745 fprintf(stderr, "recvfrom has not been implemented!\n"); | 944 fprintf(stderr, "recvfrom has not been implemented!\n"); |
746 return -1; | 945 return -1; |
747 } | 946 } |
748 | 947 |
749 int KernelProxy::select(int nfds, fd_set *readfds, fd_set *writefds, | 948 int KernelProxy::select(int nfds, fd_set *readfds, fd_set *writefds, |
750 fd_set* exceptfds, const struct timeval *timeout) { | 949 fd_set* exceptfds, const struct timeval *timeout) { |
751 errno = ENOSYS; | 950 timespec ts_abs; |
752 fprintf(stderr, "select has not been implemented!\n"); | 951 if (timeout) { |
753 return -1; | 952 timespec ts; |
| 953 TIMEVAL_TO_TIMESPEC(timeout, &ts); |
| 954 timeval tv_now; |
| 955 gettimeofday(&tv_now, NULL); |
| 956 int64_t current_time_us = |
| 957 tv_now.tv_sec * kMicrosecondsPerSecond + tv_now.tv_usec; |
| 958 int64_t wakeup_time_us = |
| 959 current_time_us + |
| 960 timeout->tv_sec * kMicrosecondsPerSecond + timeout->tv_usec; |
| 961 ts_abs.tv_sec = wakeup_time_us / kMicrosecondsPerSecond; |
| 962 ts_abs.tv_nsec = |
| 963 (wakeup_time_us - ts_abs.tv_sec * kMicrosecondsPerSecond) * |
| 964 kNanosecondsPerMicrosecond; |
| 965 } |
| 966 |
| 967 while (!(IsReady(nfds, readfds, &Socket::is_read_ready, false) || |
| 968 IsReady(nfds, writefds, &Socket::is_write_ready, false) || |
| 969 IsReady(nfds, exceptfds, &Socket::is_exception, false))) { |
| 970 SimpleAutoLock lock(select_mutex().get()); |
| 971 if (timeout) { |
| 972 if (!timeout->tv_sec && !timeout->tv_usec) |
| 973 break; |
| 974 |
| 975 if (select_cond().timedwait(select_mutex(), &ts_abs)) { |
| 976 if (errno == ETIMEDOUT) |
| 977 break; |
| 978 else |
| 979 return -1; |
| 980 } |
| 981 } else { |
| 982 select_cond().wait(select_mutex()); |
| 983 } |
| 984 } |
| 985 |
| 986 int nread = IsReady(nfds, readfds, &Socket::is_read_ready, true); |
| 987 int nwrite = IsReady(nfds, writefds, &Socket::is_write_ready, true); |
| 988 int nexcpt = IsReady(nfds, exceptfds, &Socket::is_exception, true); |
| 989 if (nread < 0 || nwrite < 0 || nexcpt < 0) { |
| 990 errno = EBADF; |
| 991 return -1; |
| 992 } |
| 993 return nread + nwrite + nexcpt; |
754 } | 994 } |
755 | 995 |
756 int KernelProxy::pselect(int nfds, fd_set *readfds, fd_set *writefds, | 996 int KernelProxy::pselect(int nfds, fd_set *readfds, fd_set *writefds, |
757 fd_set* exceptfds, const struct timeval *timeout, void* sigmask) { | 997 fd_set* exceptfds, const struct timeval *timeout, void* sigmask) { |
758 errno = ENOSYS; | 998 errno = ENOSYS; |
759 fprintf(stderr, "pselect has not been implemented!\n"); | 999 fprintf(stderr, "pselect has not been implemented!\n"); |
760 return -1; | 1000 return -1; |
761 } | 1001 } |
762 | 1002 |
763 int KernelProxy::getpeername(int sockfd, struct sockaddr *addr, | 1003 int KernelProxy::getpeername(int sockfd, struct sockaddr *addr, |
(...skipping 18 matching lines...) Expand all Loading... |
782 } | 1022 } |
783 | 1023 |
784 int KernelProxy::setsockopt(int sockfd, int level, int optname, | 1024 int KernelProxy::setsockopt(int sockfd, int level, int optname, |
785 const void *optval, socklen_t optlen) { | 1025 const void *optval, socklen_t optlen) { |
786 errno = ENOSYS; | 1026 errno = ENOSYS; |
787 fprintf(stderr, "setsockopt has not been implemented!\n"); | 1027 fprintf(stderr, "setsockopt has not been implemented!\n"); |
788 return -1; | 1028 return -1; |
789 } | 1029 } |
790 | 1030 |
791 int KernelProxy::shutdown(int sockfd, int how) { | 1031 int KernelProxy::shutdown(int sockfd, int how) { |
792 errno = ENOSYS; | 1032 if (GetFileHandle(sockfd) == 0) |
793 fprintf(stderr, "shutdown has not been implemented!\n"); | 1033 return EBADF; |
794 return -1; | 1034 return socket_subsystem_->shutdown(GetFileHandle(sockfd)->stream, how); |
795 } | 1035 } |
796 | 1036 |
797 int KernelProxy::epoll_create(int size) { | 1037 int KernelProxy::epoll_create(int size) { |
798 errno = ENOSYS; | 1038 errno = ENOSYS; |
799 fprintf(stderr, "epoll_create has not been implemented!\n"); | 1039 fprintf(stderr, "epoll_create has not been implemented!\n"); |
800 return -1; | 1040 return -1; |
801 } | 1041 } |
802 | 1042 |
803 int KernelProxy::epoll_create1(int flags) { | 1043 int KernelProxy::epoll_create1(int flags) { |
804 errno = ENOSYS; | 1044 errno = ENOSYS; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
841 | 1081 |
842 int KernelProxy::ppoll(struct pollfd *fds, nfds_t nfds, | 1082 int KernelProxy::ppoll(struct pollfd *fds, nfds_t nfds, |
843 const struct timespec *timeout, | 1083 const struct timespec *timeout, |
844 const sigset_t *sigmask, size_t sigset_size) { | 1084 const sigset_t *sigmask, size_t sigset_size) { |
845 errno = ENOSYS; | 1085 errno = ENOSYS; |
846 fprintf(stderr, "ppoll has not been implemented!\n"); | 1086 fprintf(stderr, "ppoll has not been implemented!\n"); |
847 return -1; | 1087 return -1; |
848 } | 1088 } |
849 #endif | 1089 #endif |
850 | 1090 |
OLD | NEW |