Chromium Code Reviews| 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/irt_interfaces.h" | |
| 12 #include "native_client/src/trusted/service_runtime/include/sys/dirent.h" | |
| 13 #include "native_client/src/trusted/service_runtime/include/sys/stat.h" | |
| 14 #include "native_client/src/trusted/service_runtime/include/sys/unistd.h" | |
| 15 | |
| 16 namespace nacl { | |
| 17 namespace nonsfi { | |
| 18 namespace { | |
| 19 | |
| 20 int IrtClose(int fd) { | |
| 21 if (::close(fd) < 0) | |
| 22 return errno; | |
| 23 return 0; | |
| 24 } | |
| 25 | |
| 26 int IrtDup(int fd, int* newfd) { | |
| 27 int result = ::dup(fd); | |
| 28 if (result < 0) | |
| 29 return errno; | |
| 30 | |
| 31 *newfd = result; | |
| 32 return 0; | |
| 33 } | |
| 34 | |
| 35 int IrtDup2(int fd, int newfd) { | |
| 36 if (::dup2(fd, newfd) < 0) | |
| 37 return errno; | |
| 38 return 0; | |
| 39 } | |
| 40 | |
| 41 int IrtRead(int fd, void* buf, size_t count, size_t* nread) { | |
| 42 ssize_t result = ::read(fd, buf, count); | |
| 43 if (result < 0) | |
| 44 return errno; | |
| 45 | |
| 46 *nread =result; | |
| 47 return 0; | |
| 48 } | |
| 49 | |
| 50 int IrtWrite(int fd, const void* buf, size_t count, size_t* nwrote) { | |
| 51 ssize_t result = ::write(fd, buf, count); | |
| 52 if (result < 0) | |
| 53 return errno; | |
| 54 | |
| 55 *nwrote = result; | |
| 56 return 0; | |
| 57 } | |
| 58 | |
| 59 int IrtSeek(int fd, nacl_abi_off_t offset, int whence, | |
| 60 nacl_abi_off_t* new_offset) { | |
| 61 off_t result = ::lseek(fd, offset, whence); | |
|
hamaji
2014/01/09 10:47:22
Same as fstat. Probably better to check if this ca
hidehiko
2014/01/09 12:41:19
Yes, I've checked. common.gypi defines _FILE_OFFSE
| |
| 62 if (result < 0) | |
| 63 return errno; | |
| 64 *new_offset = result; | |
| 65 return 0; | |
| 66 } | |
| 67 | |
| 68 int IrtFstat(int fd, struct nacl_abi_stat* st) { | |
| 69 struct stat host_st; | |
| 70 if (::fstat(fd, &host_st) < 0) | |
|
hamaji
2014/01/09 10:47:22
As locally chatted, I think we need to call fstat6
hidehiko
2014/01/09 12:41:19
Done.
| |
| 71 return errno; | |
| 72 | |
| 73 memset(st, 0, sizeof(*st)); | |
| 74 st->nacl_abi_st_dev = 0; | |
|
hamaji
2014/01/09 10:47:22
You'll need to use this code when you implement Ir
hidehiko
2014/01/09 12:41:19
Yes, I was planning to do it in a following CL (w/
| |
| 75 st->nacl_abi_st_ino = host_st.st_ino; | |
| 76 nacl_abi_mode_t m; | |
| 77 switch (host_st.st_mode & S_IFMT) { | |
| 78 case S_IFREG: | |
| 79 m = NACL_ABI_S_IFREG; | |
| 80 break; | |
| 81 case S_IFDIR: | |
| 82 m = NACL_ABI_S_IFDIR; | |
| 83 break; | |
| 84 case S_IFCHR: | |
| 85 m = NACL_ABI_S_IFCHR; | |
| 86 break; | |
|
hamaji
2014/01/09 10:47:22
I'm not sure, but I'd translate all possible value
hidehiko
2014/01/09 12:41:19
I just mimic'ed the NaCl's original implementation
| |
| 87 default: | |
| 88 LOG(ERROR) << "Unusual NaCl descriptor type."; | |
|
hamaji
2014/01/09 10:47:22
LOG(ERROR) might be over kill? And, how about outp
hidehiko
2014/01/09 12:41:19
Done. Redcued to LOG(WARNING).
| |
| 89 m = NACL_ABI_S_UNSUP; | |
| 90 } | |
| 91 if (host_st.st_mode & S_IRUSR) | |
| 92 m |= NACL_ABI_S_IRUSR; | |
| 93 if (host_st.st_mode & S_IWUSR) | |
| 94 m |= NACL_ABI_S_IWUSR; | |
| 95 if (host_st.st_mode & S_IXUSR) | |
| 96 m |= NACL_ABI_S_IXUSR; | |
|
hamaji
2014/01/09 10:47:22
Is it intentional we are dropping S_I*GRP and S_I*
hidehiko
2014/01/09 12:41:19
Acknowledged.
| |
| 97 st->nacl_abi_st_mode = m; | |
| 98 st->nacl_abi_st_nlink = host_st.st_nlink; | |
| 99 st->nacl_abi_st_uid = -1; // Not root | |
| 100 st->nacl_abi_st_gid = -1; // Not wheel | |
| 101 st->nacl_abi_st_rdev = 0; | |
| 102 st->nacl_abi_st_size = host_st.st_size; | |
| 103 st->nacl_abi_st_blksize = 0; | |
| 104 st->nacl_abi_st_blocks = 0; | |
| 105 st->nacl_abi_st_atime = host_st.st_atime; | |
| 106 st->nacl_abi_st_mtime = host_st.st_mtime; | |
| 107 st->nacl_abi_st_ctime = host_st.st_ctime; | |
|
hamaji
2014/01/09 10:47:22
Any reason we don't copy nano seconds? For compati
hidehiko
2014/01/09 12:41:19
Acknowledged.
| |
| 108 | |
| 109 return 0; | |
| 110 } | |
| 111 | |
| 112 int IrtGetDents(int fd, struct nacl_abi_dirent* buf, size_t count, | |
| 113 size_t* nread) { | |
| 114 // Note: getdents() can return several directory entries in packed format. | |
| 115 // So, here, because we need to convert the abi from host's to nacl's, | |
| 116 // there is no straightforward way to ensure reading only entries which can | |
| 117 // be fit to the buf after abi conversion actually. | |
|
hamaji
2014/01/09 10:47:22
TODO maybe?
hidehiko
2014/01/09 12:41:19
Done.
| |
| 118 return ENOSYS; | |
| 119 } | |
| 120 | |
| 121 } // namespace | |
| 122 | |
| 123 // for seek, fstat and getdents, their argument types should be nacl_abi_X, | |
| 124 // rather than host type, such as off_t, struct stat or struct dirent. | |
| 125 // However, the definition of nacl_irt_fdio uses host types, so here we need | |
| 126 // to cast them. | |
| 127 const nacl_irt_fdio kIrtFdIO = { | |
| 128 IrtClose, | |
| 129 IrtDup, | |
| 130 IrtDup2, | |
| 131 IrtRead, | |
| 132 IrtWrite, | |
| 133 reinterpret_cast<int(*)(int, off_t, int, off_t*)>(IrtSeek), | |
| 134 reinterpret_cast<int(*)(int, struct stat*)>(IrtFstat), | |
| 135 reinterpret_cast<int(*)(int, struct dirent*, size_t, size_t*)>(IrtGetDents), | |
| 136 }; | |
| 137 | |
| 138 } // namespace nonsfi | |
| 139 } // namespace nacl | |
| OLD | NEW |