• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef LIBRARIES_NACL_IO_KERNEL_WRAP_H_
6 #define LIBRARIES_NACL_IO_KERNEL_WRAP_H_
7 
8 #include <assert.h>
9 #include <signal.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <sys/fcntl.h>
13 #include <sys/ioctl.h>
14 #include <sys/types.h>
15 
16 #include "nacl_io/ossocket.h"
17 #include "nacl_io/ostypes.h"
18 #include "nacl_io/osutime.h"
19 #include "sdk_util/macros.h"
20 
21 #if defined(__GLIBC__)
22 #include <sys/cdefs.h>
23 #define NOTHROW __THROW
24 #else
25 #define NOTHROW
26 #endif
27 
28 // Most kernel intercept functions (ki_*) return -1 and set the global errno.
29 // However, the IRT wrappers are expected to return errno on failure. These
30 // macros are used in the wrappers to check that the ki_ function actually
31 // set errno and to its value.
32 #define RTN_ERRNO_IF(cond) \
33   if (cond) {              \
34     assert(errno != 0);    \
35     return errno;          \
36   }
37 
38 #define ERRNO_RTN(cond)   \
39   RTN_ERRNO_IF(cond < 0); \
40   return 0;
41 
42 #if defined(WIN32)
43 typedef int chmod_mode_t;
44 typedef int getcwd_size_t;
45 typedef int read_ssize_t;
46 typedef int write_ssize_t;
47 #define NAME(x) _##x
48 #else
49 typedef mode_t chmod_mode_t;
50 typedef size_t getcwd_size_t;
51 typedef ssize_t read_ssize_t;
52 typedef ssize_t write_ssize_t;
53 #define NAME(x) x
54 #endif
55 
56 EXTERN_C_BEGIN
57 
58 void kernel_wrap_init(void);
59 void kernel_wrap_uninit(void);
60 
61 int NAME(access)(const char* path, int amode) NOTHROW;
62 int NAME(chdir)(const char* path) NOTHROW;
63 int NAME(chmod)(const char* path, chmod_mode_t mode) NOTHROW;
64 int chown(const char* path, uid_t owner, gid_t group) NOTHROW;
65 int NAME(close)(int fd);
66 int NAME(dup)(int oldfd) NOTHROW;
67 int NAME(dup2)(int oldfd, int newfd) NOTHROW;
68 int fchown(int fd, uid_t owner, gid_t group) NOTHROW;
69 #if defined(WIN32)
70 int _fstat32(int fd, struct _stat32* buf);
71 int _fstat64(int fd, struct _stat64* buf);
72 int _fstat32i64(int fd, struct _stat32i64* buf);
73 int _fstat64i32(int fd, struct _stat64i32* buf);
74 #elif !defined(__linux__)
75 struct stat;
76 extern int fstat(int fd, struct stat* buf) NOTHROW;
77 #endif
78 int fsync(int fd);
79 int ftruncate(int fd, off_t length) NOTHROW;
80 char* NAME(getcwd)(char* buf, getcwd_size_t size) NOTHROW;
81 char* getwd(char* buf) NOTHROW;
82 #if !defined(__BIONIC__)
83 int getdents(int fd, void* buf, unsigned int count) NOTHROW;
84 #else
85 struct dirent;
86 int getdents(unsigned int fd, struct dirent* buf, unsigned int count) NOTHROW;
87 #endif
88 int NAME(isatty)(int fd) NOTHROW;
89 int lchown(const char* path, uid_t owner, gid_t group) NOTHROW;
90 int link(const char* oldpath, const char* newpath) NOTHROW;
91 off_t NAME(lseek)(int fd, off_t offset, int whence) NOTHROW;
92 #if defined(WIN32)
93 int _mkdir(const char* path);
94 #else
95 int mkdir(const char* path, mode_t mode) NOTHROW;
96 #endif
97 void* mmap(void* addr, size_t length, int prot, int flags, int fd,
98            off_t offset) NOTHROW;
99 int mount(const char* source, const char* target, const char* filesystemtype,
100           unsigned long mountflags, const void* data) NOTHROW;
101 int munmap(void* addr, size_t length) NOTHROW;
102 int NAME(open)(const char* path, int oflag, ...);
103 read_ssize_t NAME(read)(int fd, void* buf, size_t nbyte);
104 int remove(const char* path) NOTHROW;
105 int NAME(rmdir)(const char* path) NOTHROW;
106 sighandler_t sigset(int sig, sighandler_t disp);
107 #if defined(WIN32)
108 int setenv(const char* name, const char* value, int overwrite);
109 int _stat32(const char* path, struct _stat32* buf);
110 int _stat64(const char* path, struct _stat64* buf);
111 int _stat32i64(const char* path, struct _stat32i64* buf);
112 int _stat64i32(const char* path, struct _stat64i32* buf);
113 #elif !defined(__linux__)
114 extern int stat(const char* path, struct stat* buf) NOTHROW;
115 #endif
116 int symlink(const char* oldpath, const char* newpath) NOTHROW;
117 int umount(const char* path) NOTHROW;
118 int NAME(unlink)(const char* path) NOTHROW;
119 int utime(const char* filename, const struct utimbuf* times) NOTHROW;
120 read_ssize_t NAME(write)(int fd, const void* buf, size_t nbyte);
121 
122 #ifdef PROVIDES_SOCKET_API
123 // Socket Functions
124 int accept(int fd, struct sockaddr* addr, socklen_t* len);
125 int bind(int fd, const struct sockaddr* addr, socklen_t len);
126 int connect(int fd, const struct sockaddr* addr, socklen_t len);
127 struct hostent* gethostbyname(const char* name);
128 int getpeername(int fd, struct sockaddr* addr, socklen_t* len);
129 int getsockname(int fd, struct sockaddr* addr, socklen_t* len);
130 int getsockopt(int fd, int lvl, int optname, void* optval, socklen_t* len);
131 int listen(int fd, int backlog);
132 ssize_t recv(int fd, void* buf, size_t len, int flags);
133 ssize_t recvfrom(int fd,
134                  void* buf,
135                  size_t len,
136                  int flags,
137                  struct sockaddr* addr,
138                  socklen_t* addrlen);
139 ssize_t recvmsg(int fd, struct msghdr* msg, int flags);
140 ssize_t send(int fd, const void* buf, size_t len, int flags);
141 ssize_t sendto(int fd,
142                const void* buf,
143                size_t len,
144                int flags,
145                const struct sockaddr* addr,
146                socklen_t addrlen);
147 ssize_t sendmsg(int fd, const struct msghdr* msg, int flags);
148 int setsockopt(int fd, int lvl, int optname, const void* optval, socklen_t len);
149 int shutdown(int fd, int how);
150 int socket(int domain, int type, int protocol);
151 int socketpair(int domain, int type, int protocl, int* sv);
152 #endif  // PROVIDES_SOCKET_API
153 
154 EXTERN_C_END
155 
156 #endif  // LIBRARIES_NACL_IO_KERNEL_WRAP_H_
157