imcplugin demo: Extend to support stat() call
[nativeclient.git] / service_runtime / nacl_host_desc.h
blob944bc0753a352ff6ef0ce3dab4229d1ab950369c
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Service Runtime. I/O Descriptor / Handle abstraction. Memory
34 * mapping using descriptors.
37 #ifndef NATIVE_CLIENT_SERVICE_RUNTIME_NACL_HOST_DESC_H__
38 #define NATIVE_CLIENT_SERVICE_RUNTIME_NACL_HOST_DESC_H__
40 #include <errno.h>
42 #include "native_client/include/portability.h"
43 #include "native_client/include/nacl_platform.h"
44 #include "native_client/include/nacl_base.h"
46 #include "native_client/service_runtime/nacl_sync.h"
48 #include "native_client/service_runtime/include/bits/mman.h"
50 #if NACL_LINUX || NACL_OSX
51 # include "native_client/service_runtime/linux/nacl_host_desc_types.h"
52 #elif NACL_WINDOWS
53 # include "native_client/service_runtime/win/nacl_host_desc_types.h"
54 #endif
57 * see NACL_MAP_PAGESIZE from nacl_config.h; map operations must be aligned
60 EXTERN_C_BEGIN
62 struct nacl_abi_stat;
64 static INLINE int NaClIsNegErrno(int val)
67 * On 64-bit Linux, the app has the entire 32-bit address space
68 * (kernel no longer occupies the top 1G), so what is an errno and
69 * what is an address is trickier: we require that our NACL_ABI_
70 * errno values be at most 64K.
72 return ((unsigned) val >= 0xffff0000u);
75 extern int NaClXlateErrno(int errnum);
77 extern int NaClXlateNaClSyncStatus(NaClSyncStatus status);
80 * Mapping data from a file.
82 * start_addr and len must be multiples of NACL_MAP_PAGESIZE.
84 * Of prot bits, only PROT_READ and PROT_WRITE are allowed. Of flags,
85 * only MAP_ANONYMOUS is allowed. start_addr must be specified, and
86 * this code will add in MAP_FIXED. start_address, len, and offset
87 * must be a multiple of NACL_MAP_PAGESIZE.
89 * Note that in Windows, in order for the mapping to be coherent, the
90 * mapping must arise from the same mapping handle and just using the
91 * same file handle won't ensure coherence. If the file mapping
92 * object were created and destroyed inside of NaClHostDescMap, there
93 * would never be any chance at coherence. One alternative is to
94 * create a file mapping object for each mapping mode. Native
95 * descriptors are then shareable, but only when the mode matches (!).
96 * A read-only shared mapping not seeing the changes made by a
97 * read-write mapping seem rather ugly.
99 * Instead of this ugliness, we just say that a map operation cannot
100 * request MAP_SHARED. Anonymous mappings ignore the descriptor
101 * argument.
103 * Underlying host-OS syscalls: mmap / MapViewOfFileEx
106 extern int NaClHostDescMap(struct NaClHostDesc *d,
107 void *start_addr,
108 size_t len,
109 int prot,
110 int flags,
111 off_t offset); /* 4GB file max */
114 * Undo a file mapping. The memory range specified by start_address,
115 * len must be memory that came from NaClHostDescMap.
117 * start_addr and len must be multiples of NACL_MAP_PAGESIZE.
119 * Underlying host-OS syscalls: mmap / UnmapViewOfFile/VirtualAlloc
121 extern int NaClHostDescUnmap(void *start_addr,
122 size_t len);
125 * Undo a file mapping. The memory range specified by start_address,
126 * len must be memory that came from NaClHostDescMap.
128 * start_addr and len must be multiples of NACL_MAP_PAGESIZE.
130 * Underlying host-OS syscalls: munmap / UnmapViewOfFile
132 extern int NaClHostDescUnmapUnsafe(void *start_addr,
133 size_t len);
136 * Constructor for a NaClHostDesc object.
138 * path should be a host-OS pathname to a file. No validation is
139 * done. mode should contain one of O_RDONLY, O_WRONLY, and O_RDWR,
140 * and can additionally contain O_CREAT, O_TRUNC, and O_APPEND.
142 * Uses raw syscall return convention, so returns 0 for success and
143 * non-zero (usually -NACL_ABI_EINVAL) for failure.
145 * We cannot return the platform descriptor/handle and be consistent
146 * with a largely POSIX-ish interface, since e.g. windows handles may
147 * be negative and might look like negative errno returns. Currently
148 * we use the posix API on windows, so it could work, but we may need
149 * to change later.
151 * Underlying host-OS functions: open / _s_open_s
153 extern int NaClHostDescOpen(struct NaClHostDesc *d,
154 char *path,
155 int mode,
156 int perms);
159 * Constructor for a NaClHostDesc object.
161 * Uses raw syscall return convention, so returns 0 for success and
162 * non-zero (usually -NACL_ABI_EINVAL) for failure.
164 * d is a POSIX-interface descriptor
166 * mode may only contain one of NACL_ABI_O_RDONLY, NACL_ABI_O_WRONLY,
167 * or NACL_ABI_O_RDWR, and must be the actual mode that d was opened
168 * with. Note that these are host OS access mode bits.
170 * Underlying host-OS functions: dup / _dup; mode is what posix_d was
171 * opened with
173 extern int NaClHostDescPosixDup(struct NaClHostDesc *d,
174 int posix_d,
175 int mode);
178 * Essentially the same as NaClHostDescPosixDup, but without the dup
179 * -- takes ownership of the descriptor rather than making a dup.
181 extern int NaClHostDescPosixTake(struct NaClHostDesc *d,
182 int posix_d,
183 int mode);
187 * Allocates a NaClHostDesc and invokes NaClHostDescPosixTake on it.
188 * Aborts process if no memory.
190 extern struct NaClHostDesc *NaClHostDescPosixMake(int posix_d,
191 int mode);
193 * Read data from an opened file into a memory buffer.
195 * buf is not validated.
197 * Underlying host-OS functions: read / _read
199 extern ssize_t NaClHostDescRead(struct NaClHostDesc *d,
200 void *buf,
201 size_t len);
205 * Write data from a memory buffer into an opened file.
207 * buf is not validated.
209 * Underlying host-OS functions: write / _write
211 extern ssize_t NaClHostDescWrite(struct NaClHostDesc *d,
212 void const *buf,
213 size_t count);
215 extern int NaClHostDescSeek(struct NaClHostDesc *d,
216 off_t offset,
217 int whence);
220 * TODO: Need to enumerate which request is supported and the
221 * size of the argument, as well as whether the arg is input or
222 * output.
224 extern int NaClHostDescIoctl(struct NaClHostDesc *d,
225 int request,
226 void *arg);
229 * Fstat.
231 extern int NaClHostDescFstat(struct NaClHostDesc *d,
232 struct nacl_abi_stat *nasp);
235 * Dtor for the NaClHostFile object. Close the file.
237 * Underlying host-OS functions: close(2) / _close
239 extern int NaClHostDescClose(struct NaClHostDesc *d);
241 extern int NaClHostDescStat(char const *host_os_pathname,
242 struct nacl_abi_stat *nasp);
245 * should DCE away when unused.
247 static INLINE int NaClProtMap(int abi_prot)
249 int host_os_prot;
251 host_os_prot = 0;
252 #define M(H) do { \
253 if (0 != (abi_prot & NACL_ABI_ ## H)) { \
254 host_os_prot |= H; \
256 } while (0)
257 M(PROT_READ);
258 M(PROT_WRITE);
259 M(PROT_EXEC);
260 #if PROT_NONE != 0
261 # error "NaClProtMap: PROT_NONE is not zero -- are mprotect flags bit values?"
262 #endif
263 return host_os_prot;
264 #undef M
267 EXTERN_C_END
269 #endif