1 // Copyright (c) 2012 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.
5 // The entire file is wrapped in this #if. We do this so this .cc file can be
6 // compiled, even on a non-Windows build.
9 #include "nacl_io/kernel_wrap.h"
15 #include <sys/types.h> // This must be included before <sys/stat.h>.
17 #include "nacl_io/kernel_intercept.h"
23 template <typename SrcStat, typename DstStat>
24 void CopyStat(const SrcStat* src, DstStat* dst) {
25 memset(dst, 0, sizeof(DstStat));
26 dst->st_dev = src->st_dev;
27 dst->st_ino = src->st_ino;
28 dst->st_mode = src->st_mode;
29 dst->st_nlink = src->st_nlink;
30 dst->st_uid = src->st_uid;
31 dst->st_gid = src->st_gid;
32 dst->st_rdev = src->st_rdev;
33 dst->st_size = src->st_size;
34 dst->st_atime = src->st_atime;
35 dst->st_mtime = src->st_mtime;
36 dst->st_ctime = src->st_ctime;
43 // This needs to be included because it is defined in read.c, which we wish to
44 // override. Define with dummy values for now... though this seems like it will
45 // break ftelli64/fgetpos/fstream.
46 char _lookuptrailbytes[256] = {0};
48 int _access(const char* path, int amode) {
49 return ki_access(path, amode);
52 int _chdir(const char* path) {
53 return ki_chdir(path);
56 int _chmod(const char* path, mode_t mode) {
57 return ki_chmod(path, mode);
64 int _close_nolock(int fd) {
72 int _dup2(int oldfd, int newfd) {
73 return ki_dup2(oldfd, newfd);
76 int _fstat32(int fd, struct _stat32* buf) {
78 int res = ki_fstat(fd, &ki_buf);
79 CopyStat(&ki_buf, buf);
83 int _fstat64(int fd, struct _stat64* buf) {
85 int res = ki_fstat(fd, &ki_buf);
86 CopyStat(&ki_buf, buf);
90 int _fstat32i64(int fd, struct _stat32i64* buf) {
92 int res = ki_fstat(fd, &ki_buf);
93 CopyStat(&ki_buf, buf);
97 int _fstat64i32(int fd, struct _stat64i32* buf) {
99 int res = ki_fstat(fd, &ki_buf);
100 CopyStat(&ki_buf, buf);
104 char* _getcwd(char* buf, int size) {
105 return ki_getcwd(buf, size);
108 int _isatty(int fd) {
109 return ki_isatty(fd);
112 off_t _lseek(int fd, off_t offset, int whence) {
113 return ki_lseek(fd, offset, whence);
116 int _mkdir(const char* path) {
117 return ki_mkdir(path, 0777);
120 int _open(const char* path, int oflag, ...) {
122 // TODO(binji): ki_open should use the pmode parameter. When it does, this
123 // will be necessary to add in.
126 if (oflag & _O_CREAT) {
127 va_start(list, oflag);
128 pmode = va_arg(list, int);
132 return ki_open(path, oflag);
135 int _sopen(const char* path, int oflag, int shflag) {
136 return ki_open(path, oflag);
139 errno_t _sopen_s(int* pfh, const char* path, int oflag, int shflag, int pmode) {
140 *pfh = ki_open(path, oflag);
141 return (*pfh < 0) ? errno : 0;
144 int _read(int fd, void* buf, size_t nbyte) {
145 if (!ki_is_initialized())
148 return ki_read(fd, buf, nbyte);
151 int _read_nolock(int fd, void* buf, size_t nbyte) {
152 if (!ki_is_initialized())
155 return ki_read(fd, buf, nbyte);
158 int _rmdir(const char* path) {
159 return ki_rmdir(path);
162 int setenv(const char* name, const char* value, int overwrite) {
163 if (0 == overwrite && NULL != getenv(name)) {
166 errno_t result = _putenv_s(name, value);
175 int _stat32(const char* path, struct _stat32* buf) {
177 int res = ki_stat(path, &ki_buf);
178 CopyStat(&ki_buf, buf);
182 int _stat64(const char* path, struct _stat64* buf) {
184 int res = ki_stat(path, &ki_buf);
185 CopyStat(&ki_buf, buf);
189 int _stat64i32(const char* path, struct _stat64i32* buf) {
191 int res = ki_stat(path, &ki_buf);
192 CopyStat(&ki_buf, buf);
196 int _stat32i64(const char* path, struct _stat32i64* buf) {
198 int res = ki_stat(path, &ki_buf);
199 CopyStat(&ki_buf, buf);
203 int _unlink(const char* path) {
204 return ki_unlink(path);
207 int _utime(const char* filename, const struct utimbuf* times) {
208 return ki_utime(filename, times);
211 int _write(int fd, const void* buf, size_t nbyte) {
212 if (!ki_is_initialized())
215 return ki_write(fd, buf, nbyte);
219 // "real" functions, i.e. the unwrapped original functions. On Windows we don't
220 // wrap, so the real functions aren't accessible. In most cases, we just fail.
222 int _real_close(int fd) {
226 int _real_fstat(int fd, struct stat *buf) {
230 int _real_getdents(int fd, void* nacl_buf, size_t nacl_count, size_t *nread) {
234 int _real_lseek(int fd, off_t offset, int whence, off_t* new_offset) {
238 int _real_mkdir(const char* pathname, mode_t mode) {
242 int _real_mmap(void** addr, size_t length, int prot, int flags, int fd,
247 int _real_munmap(void* addr, size_t length) {
251 int _real_open(const char* pathname, int oflag, mode_t cmode, int* newfd) {
255 int _real_open_resource(const char* file, int* fd) {
259 int _real_read(int fd, void *buf, size_t count, size_t *nread) {
264 int _real_rmdir(const char* pathname) {
268 int _real_write(int fd, const void *buf, size_t count, size_t *nwrote) {
273 #define USECS_FROM_WIN_TO_TO_UNIX_EPOCH 11644473600000LL
274 uint64_t usec_since_epoch() {
276 ULARGE_INTEGER ularge;
277 GetSystemTimeAsFileTime(&ft);
279 ularge.LowPart = ft.dwLowDateTime;
280 ularge.HighPart = ft.dwHighDateTime;
282 // Truncate to usec resolution.
283 return ularge.QuadPart / 10;
286 // Do nothing for Windows, we replace the library at link time.
287 void kernel_wrap_init() {
290 void kernel_wrap_uninit() {
295 #endif // defined(WIN32)