Chromium Code Reviews| Index: components/nacl/loader/nonsfi/irt_basic.cc |
| diff --git a/components/nacl/loader/nonsfi/irt_basic.cc b/components/nacl/loader/nonsfi/irt_basic.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d457dff75a93572e2212b77406c9ddf81b3d1481 |
| --- /dev/null |
| +++ b/components/nacl/loader/nonsfi/irt_basic.cc |
| @@ -0,0 +1,106 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <errno.h> |
| +#include <sched.h> |
| +#include <stdlib.h> |
| +#include <sys/time.h> |
| +#include <time.h> |
| +#include <unistd.h> |
| + |
| +#include "components/nacl/loader/nonsfi/irt_interfaces.h" |
| +#include "native_client/src/trusted/service_runtime/include/sys/time.h" |
| +#include "native_client/src/trusted/service_runtime/include/sys/unistd.h" |
| + |
| +namespace nacl { |
| +namespace nonsfi { |
| +namespace { |
| + |
| +void IrtExit(int status) { |
| + ::_exit(status); |
|
Mark Seaborn
2014/01/10 05:12:20
Maybe leave off the "::" prefixes unless there's a
hidehiko
2014/01/10 07:27:47
Done.
|
| +} |
| + |
| +int IrtGetToD(struct nacl_abi_timeval* tv) { |
| + struct timeval host_tv; |
| + if (::gettimeofday(&host_tv, NULL) == -1) |
|
Mark Seaborn
2014/01/10 05:12:20
Nit: in general, "!= 0" is slightly more defensive
hidehiko
2014/01/10 07:27:47
Done.
|
| + return errno; |
| + tv->nacl_abi_tv_sec = host_tv.tv_sec; |
| + tv->nacl_abi_tv_usec = host_tv.tv_usec; |
| + return 0; |
| +} |
| + |
| +int IrtClock(nacl_abi_clock_t* ticks) { |
| + clock_t host_ticks = ::clock(); |
| + if (host_ticks == static_cast<nacl_abi_clock_t>(-1)) |
| + return EOVERFLOW; |
| + *ticks = host_ticks; |
| + return 0; |
| +} |
| + |
| +int IrtNanoSleep(const struct nacl_abi_timespec* req, |
| + struct nacl_abi_timespec* rem) { |
| + if (!req) |
| + return EFAULT; |
| + |
| + struct timespec host_req; |
| + host_req.tv_sec = req->tv_sec; |
| + host_req.tv_nsec = req->tv_nsec; |
| + struct timespec host_rem; |
| + if (::nanosleep(&host_req, &host_rem) == -1) |
|
Mark Seaborn
2014/01/10 05:12:20
"!= 0" (nit)
hidehiko
2014/01/10 07:27:47
Done.
|
| + return errno; |
| + |
| + if (rem) { |
| + rem->tv_sec = host_rem.tv_sec; |
| + rem->tv_nsec = host_rem.tv_nsec; |
| + } |
| + return 0; |
| +} |
| + |
| +int IrtSchedYield() { |
| + if (::sched_yield() == -1) |
| + return errno; |
| + |
| + return 0; |
| +} |
| + |
| +int IrtSysconf(int name, int* value) { |
| + int result; |
| + switch (name) { |
| + case NACL_ABI__SC_NPROCESSORS_ONLN: |
| + errno = 0; |
| + result = ::sysconf(_SC_NPROCESSORS_ONLN); |
| + break; |
| + case NACL_ABI__SC_PAGESIZE: |
| + errno = 0; |
| + result = ::sysconf(_SC_PAGESIZE); |
| + break; |
| + default: |
| + errno = EINVAL; |
|
Mark Seaborn
2014/01/10 05:12:20
Just "return EINVAL" here?
hidehiko
2014/01/10 07:27:47
Done.
|
| + result = -1; |
| + } |
| + |
| + if (result == -1 && errno == EINVAL) |
| + return EINVAL; |
| + |
| + *value = result; |
| + return 0; |
| +} |
| + |
| +} // namespace |
| + |
| +// For gettod, clock and nanosleep, their argument types should be nacl_abi_X, |
| +// rather than host type, such as timeval or clock_t etc. However, the |
| +// definition of nacl_irt_basic uses host types, so here we need to cast them. |
| +const nacl_irt_basic kIrtBasic = { |
| + IrtExit, |
| + reinterpret_cast<int(*)(struct timeval*)>(IrtGetToD), |
| + reinterpret_cast<int(*)(clock_t*)>(IrtClock), |
| + reinterpret_cast<int(*)(const struct timespec*, struct timespec*)>( |
| + IrtNanoSleep), |
| + IrtSchedYield, |
| + IrtSysconf, |
| +}; |
| + |
| +} // namespace nonsfi |
| +} // namespace nacl |