VerifyAndMapCode: Only map the code if it passes validation
[nativeclient.git] / service_runtime / nacl_desc_dir.c
blobdf34ad54d9aa210083fb3ee2d612bcf5d892778f
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. Directory descriptor abstraction.
36 #include "native_client/include/portability.h"
38 #include <stdlib.h>
40 #if NACL_WINDOWS
41 # include "io.h"
42 #endif
44 #include "native_client/service_runtime/nacl_config.h"
45 #include "native_client/service_runtime/nacl_log.h"
46 #include "native_client/service_runtime/nacl_desc_base.h"
47 #include "native_client/service_runtime/nacl_desc_dir.h"
48 #include "native_client/service_runtime/internal_errno.h"
50 #include "native_client/service_runtime/include/sys/dirent.h"
51 #include "native_client/service_runtime/include/sys/errno.h"
52 #include "native_client/service_runtime/include/sys/fcntl.h"
53 #include "native_client/service_runtime/include/sys/mman.h"
55 #include "native_client/intermodule_comm/nacl_imc_c.h"
58 * This file contains the implementation for the NaClDirDesc subclass
59 * of NaClDesc.
61 * NaClDescDirDesc is the subclass that wraps host-OS directory information.
65 * Takes ownership of hd, will close in Dtor.
67 int NaClDescDirDescCtor(struct NaClDescDirDesc *self,
68 struct NaClHostDir *hd)
70 struct NaClDesc *basep = (struct NaClDesc *) self;
72 basep->vtbl = (struct NaClDescVtbl *) NULL;
73 if (!NaClDescCtor(basep)) {
74 return 0;
76 self->hd = hd;
77 basep->vtbl = &kNaClDescDirDescVtbl;
78 return 1;
81 void NaClDescDirDescDtor(struct NaClDesc *vself)
83 struct NaClDescDirDesc *self = (struct NaClDescDirDesc *) vself;
85 NaClLog(4, "NaClDescDirDescDtor(0x%08"PRIxPTR").\n",
86 (uintptr_t) vself);
87 NaClHostDirClose(self->hd);
88 free(self->hd);
89 self->hd = NULL;
90 vself->vtbl = (struct NaClDescVtbl *) NULL;
91 NaClDescDtor(&self->base);
94 struct NaClDescDirDesc *NaClDescDirDescMake(struct NaClHostDir *nhdp)
96 struct NaClDescDirDesc *ndp;
98 ndp = malloc(sizeof *ndp);
99 if (NULL == ndp) {
100 NaClLog(LOG_FATAL,
101 "NaClDescDirDescMake: no memory for 0x%08"PRIxPTR"\n",
102 (uintptr_t) nhdp);
104 if (!NaClDescDirDescCtor(ndp, nhdp)) {
105 NaClLog(LOG_FATAL,
106 ("NaClDescDirDescMake:"
107 " NaClDescDirDescCtor(0x%08"PRIxPTR",0x%08"PRIxPTR") failed\n"),
108 (uintptr_t) ndp,
109 (uintptr_t) nhdp);
111 return ndp;
114 struct NaClDescDirDesc *NaClDescDirDescOpen(char *path)
116 struct NaClHostDir *nhdp;
118 nhdp = malloc(sizeof *nhdp);
119 if (NULL == nhdp) {
120 NaClLog(LOG_FATAL, "NaClDescDirDescOpen: no memory for %s\n", path);
122 if (!NaClHostDirOpen(nhdp, path)) {
123 NaClLog(LOG_FATAL, "NaClDescDirDescOpen: NaClHostDirOpen failed for %s\n",
124 path);
126 return NaClDescDirDescMake(nhdp);
129 ssize_t NaClDescDirDescRead(struct NaClDesc *vself,
130 struct NaClDescEffector *effp,
131 void *buf,
132 size_t len)
134 /* NaClLog(LOG_ERROR, "NaClDescDirDescRead: Read not allowed on dir\n"); */
135 return NaClDescDirDescGetdents(vself, effp, buf, len);
136 /* return -NACL_ABI_EINVAL; */
139 ssize_t NaClDescDirDescGetdents(struct NaClDesc *vself,
140 struct NaClDescEffector *effp,
141 void *dirp,
142 size_t count)
144 struct NaClDescDirDesc *self = (struct NaClDescDirDesc *) vself;
145 struct nacl_abi_dirent *direntp = (struct nacl_abi_dirent *) dirp;
146 ssize_t retval;
148 NaClLog(3, "NaClDescDirDescGetdents(0x%08"PRIxPTR", %"PRIuS"):\n",
149 (uintptr_t) dirp, count);
150 retval = NaClHostDirGetdents(self->hd, dirp, count);
151 NaClLog(3, "NaClDescDirDescGetdents(d_ino=%lu, d_off=%lu, d_reclen=%u, "
152 "d_name='%s')\n",
153 direntp->nacl_abi_d_ino,
154 direntp->nacl_abi_d_off,
155 direntp->nacl_abi_d_reclen,
156 direntp->nacl_abi_d_name);
157 return retval;
160 int NaClDescDirDescClose(struct NaClDesc *vself,
161 struct NaClDescEffector *effp)
163 NaClDescUnref(vself);
164 return 0;
167 int NaClDescDirDescExternalizeSize(struct NaClDesc *vself,
168 size_t *nbytes,
169 size_t *nhandles)
171 *nbytes = 0;
172 *nhandles = 1;
173 return 0;
176 struct NaClDescVtbl const kNaClDescDirDescVtbl = {
177 NaClDescDirDescDtor,
178 NaClDescMapNotImplemented,
179 NaClDescUnmapUnsafeNotImplemented,
180 NaClDescUnmapNotImplemented,
181 NaClDescDirDescRead,
182 NaClDescWriteNotImplemented,
183 NaClDescSeekNotImplemented,
184 NaClDescIoctlNotImplemented,
185 NaClDescFstatNotImplemented,
186 NaClDescDirDescClose,
187 NaClDescDirDescGetdents,
188 NACL_DESC_DIR,
189 NaClDescDirDescExternalizeSize,
190 NaClDescExternalizeNotImplemented,
191 NaClDescLockNotImplemented,
192 NaClDescTryLockNotImplemented,
193 NaClDescUnlockNotImplemented,
194 NaClDescWaitNotImplemented,
195 NaClDescTimedWaitAbsNotImplemented,
196 NaClDescSignalNotImplemented,
197 NaClDescBroadcastNotImplemented,
198 NaClDescSendMsgNotImplemented,
199 NaClDescRecvMsgNotImplemented,
200 NaClDescConnectAddrNotImplemented,
201 NaClDescAcceptConnNotImplemented,
202 NaClDescPostNotImplemented,
203 NaClDescSemWaitNotImplemented,
204 NaClDescGetValueNotImplemented,
207 int NaClDescDirInternalize(struct NaClDesc **baseptr,
208 struct NaClDescXferState *xfer)
210 NaClLog(LOG_ERROR, "NaClDescDirDescInternalize: not implemented for dir\n");
211 return -NACL_ABI_EINVAL;