15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <errno.h>
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <unistd.h>
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <sys/types.h>
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <sys/stat.h>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/logging.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/nacl/loader/nonsfi/abi_conversion.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/nacl/loader/nonsfi/irt_interfaces.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/nacl/loader/nonsfi/irt_util.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "native_client/src/trusted/service_runtime/include/sys/dirent.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "native_client/src/trusted/service_runtime/include/sys/stat.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "native_client/src/trusted/service_runtime/include/sys/unistd.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace nacl {
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace nonsfi {
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int IrtClose(int fd) {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return CheckError(close(fd));
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int IrtDup(int fd, int* newfd) {
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return CheckErrorWithResult(dup(fd), newfd);
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int IrtDup2(int fd, int newfd) {
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return CheckError(dup2(fd, newfd));
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int IrtRead(int fd, void* buf, size_t count, size_t* nread) {
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return CheckErrorWithResult(read(fd, buf, count), nread);
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int IrtWrite(int fd, const void* buf, size_t count, size_t* nwrote) {
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return CheckErrorWithResult(write(fd, buf, count), nwrote);
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int IrtSeek(int fd, nacl_abi_off_t offset, int whence,
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            nacl_abi_off_t* new_offset) {
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return CheckErrorWithResult(lseek(fd, offset, whence), new_offset);
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int IrtFstat(int fd, struct nacl_abi_stat* st) {
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  struct stat host_st;
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (fstat(fd, &host_st))
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return errno;
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  StatToNaClAbiStat(host_st, st);
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return 0;
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)int IrtGetDents(int fd, struct nacl_abi_dirent* buf, size_t count,
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                size_t* nread) {
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Note: getdents() can return several directory entries in packed format.
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // So, here, because we need to convert the abi from host's to nacl's,
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // there is no straightforward way to ensure reading only entries which can
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // be fit to the buf after abi conversion actually.
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(https://code.google.com/p/nativeclient/issues/detail?id=3734):
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Implement this method.
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return ENOSYS;
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// For seek, fstat and getdents, their argument types should be nacl_abi_X,
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// rather than host type, such as off_t, struct stat or struct dirent.
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// However, the definition of nacl_irt_fdio uses host types, so here we need
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// to cast them.
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)const nacl_irt_fdio kIrtFdIO = {
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  IrtClose,
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  IrtDup,
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  IrtDup2,
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  IrtRead,
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  IrtWrite,
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reinterpret_cast<int(*)(int, off_t, int, off_t*)>(IrtSeek),
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reinterpret_cast<int(*)(int, struct stat*)>(IrtFstat),
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  reinterpret_cast<int(*)(int, struct dirent*, size_t, size_t*)>(IrtGetDents),
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace nonsfi
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace nacl
86