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 "components/nacl/loader/nonsfi/irt_util.h"
14#include "native_client/src/trusted/service_runtime/include/sys/dirent.h"
15#include "native_client/src/trusted/service_runtime/include/sys/stat.h"
16#include "native_client/src/trusted/service_runtime/include/sys/unistd.h"
17
18namespace nacl {
19namespace nonsfi {
20namespace {
21
22int IrtClose(int fd) {
23  return CheckError(close(fd));
24}
25
26int IrtDup(int fd, int* newfd) {
27  return CheckErrorWithResult(dup(fd), newfd);
28}
29
30int IrtDup2(int fd, int newfd) {
31  return CheckError(dup2(fd, newfd));
32}
33
34int IrtRead(int fd, void* buf, size_t count, size_t* nread) {
35  return CheckErrorWithResult(read(fd, buf, count), nread);
36}
37
38int IrtWrite(int fd, const void* buf, size_t count, size_t* nwrote) {
39  return CheckErrorWithResult(write(fd, buf, count), nwrote);
40}
41
42int IrtSeek(int fd, nacl_abi_off_t offset, int whence,
43            nacl_abi_off_t* new_offset) {
44  return CheckErrorWithResult(lseek(fd, offset, whence), new_offset);
45}
46
47int IrtFstat(int fd, struct nacl_abi_stat* st) {
48  struct stat host_st;
49  if (fstat(fd, &host_st))
50    return errno;
51
52  StatToNaClAbiStat(host_st, st);
53  return 0;
54}
55
56int IrtGetDents(int fd, struct nacl_abi_dirent* buf, size_t count,
57                size_t* nread) {
58  // Note: getdents() can return several directory entries in packed format.
59  // So, here, because we need to convert the abi from host's to nacl's,
60  // there is no straightforward way to ensure reading only entries which can
61  // be fit to the buf after abi conversion actually.
62  // TODO(https://code.google.com/p/nativeclient/issues/detail?id=3734):
63  // Implement this method.
64  return ENOSYS;
65}
66
67}  // namespace
68
69// For seek, fstat and getdents, their argument types should be nacl_abi_X,
70// rather than host type, such as off_t, struct stat or struct dirent.
71// However, the definition of nacl_irt_fdio uses host types, so here we need
72// to cast them.
73const nacl_irt_fdio kIrtFdIO = {
74  IrtClose,
75  IrtDup,
76  IrtDup2,
77  IrtRead,
78  IrtWrite,
79  reinterpret_cast<int(*)(int, off_t, int, off_t*)>(IrtSeek),
80  reinterpret_cast<int(*)(int, struct stat*)>(IrtFstat),
81  reinterpret_cast<int(*)(int, struct dirent*, size_t, size_t*)>(IrtGetDents),
82};
83
84}  // namespace nonsfi
85}  // namespace nacl
86