OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <errno.h> |
| 6 #include <unistd.h> |
| 7 #include <sys/types.h> |
| 8 #include <sys/stat.h> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "components/nacl/loader/nonsfi/abi_conversion.h" |
| 12 #include "components/nacl/loader/nonsfi/irt_interfaces.h" |
| 13 #include "native_client/src/trusted/service_runtime/include/sys/dirent.h" |
| 14 #include "native_client/src/trusted/service_runtime/include/sys/stat.h" |
| 15 #include "native_client/src/trusted/service_runtime/include/sys/unistd.h" |
| 16 |
| 17 namespace nacl { |
| 18 namespace nonsfi { |
| 19 namespace { |
| 20 |
| 21 int IrtClose(int fd) { |
| 22 if (close(fd)) |
| 23 return errno; |
| 24 |
| 25 return 0; |
| 26 } |
| 27 |
| 28 int IrtDup(int fd, int* newfd) { |
| 29 int result = dup(fd); |
| 30 if (result < 0) |
| 31 return errno; |
| 32 |
| 33 *newfd = result; |
| 34 return 0; |
| 35 } |
| 36 |
| 37 int IrtDup2(int fd, int newfd) { |
| 38 if (dup2(fd, newfd) < 0) |
| 39 return errno; |
| 40 |
| 41 return 0; |
| 42 } |
| 43 |
| 44 int IrtRead(int fd, void* buf, size_t count, size_t* nread) { |
| 45 ssize_t result = read(fd, buf, count); |
| 46 if (result < 0) |
| 47 return errno; |
| 48 |
| 49 *nread = result; |
| 50 return 0; |
| 51 } |
| 52 |
| 53 int IrtWrite(int fd, const void* buf, size_t count, size_t* nwrote) { |
| 54 ssize_t result = write(fd, buf, count); |
| 55 if (result < 0) |
| 56 return errno; |
| 57 |
| 58 *nwrote = result; |
| 59 return 0; |
| 60 } |
| 61 |
| 62 int IrtSeek(int fd, nacl_abi_off_t offset, int whence, |
| 63 nacl_abi_off_t* new_offset) { |
| 64 off_t result = lseek(fd, offset, whence); |
| 65 if (result < 0) |
| 66 return errno; |
| 67 |
| 68 *new_offset = result; |
| 69 return 0; |
| 70 } |
| 71 |
| 72 int IrtFstat(int fd, struct nacl_abi_stat* st) { |
| 73 struct stat host_st; |
| 74 if (fstat(fd, &host_st)) |
| 75 return errno; |
| 76 |
| 77 StatToNaClAbiStat(host_st, st); |
| 78 return 0; |
| 79 } |
| 80 |
| 81 int IrtGetDents(int fd, struct nacl_abi_dirent* buf, size_t count, |
| 82 size_t* nread) { |
| 83 // Note: getdents() can return several directory entries in packed format. |
| 84 // So, here, because we need to convert the abi from host's to nacl's, |
| 85 // there is no straightforward way to ensure reading only entries which can |
| 86 // be fit to the buf after abi conversion actually. |
| 87 // TODO(https://code.google.com/p/nativeclient/issues/detail?id=3734): |
| 88 // Implement this method. |
| 89 return ENOSYS; |
| 90 } |
| 91 |
| 92 } // namespace |
| 93 |
| 94 // For seek, fstat and getdents, their argument types should be nacl_abi_X, |
| 95 // rather than host type, such as off_t, struct stat or struct dirent. |
| 96 // However, the definition of nacl_irt_fdio uses host types, so here we need |
| 97 // to cast them. |
| 98 const nacl_irt_fdio kIrtFdIO = { |
| 99 IrtClose, |
| 100 IrtDup, |
| 101 IrtDup2, |
| 102 IrtRead, |
| 103 IrtWrite, |
| 104 reinterpret_cast<int(*)(int, off_t, int, off_t*)>(IrtSeek), |
| 105 reinterpret_cast<int(*)(int, struct stat*)>(IrtFstat), |
| 106 reinterpret_cast<int(*)(int, struct dirent*, size_t, size_t*)>(IrtGetDents), |
| 107 }; |
| 108 |
| 109 } // namespace nonsfi |
| 110 } // namespace nacl |
OLD | NEW |