Make ncval-annotate and ncval-stubout executable
[nativeclient.git] / service_runtime / nacl_desc_dir.c
blobd56e6537bac67b318543f33a5637a2e7c93d0669
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 if (!NaClDescCtor(basep)) {
73 return 0;
75 self->hd = hd;
76 basep->vtbl = &kNaClDescDirDescVtbl;
77 return 1;
80 void NaClDescDirDescDtor(struct NaClDesc *vself)
82 struct NaClDescDirDesc *self = (struct NaClDescDirDesc *) vself;
84 NaClLog(4, "NaClDescDirDescDtor(0x%08x).\n",
85 (uintptr_t) vself);
86 NaClHostDirClose(self->hd);
87 free(self->hd);
88 self->hd = NULL;
89 NaClDescDtor(&self->base);
92 struct NaClDescDirDesc *NaClDescDirDescMake(struct NaClHostDir *nhdp)
94 struct NaClDescDirDesc *ndp;
96 ndp = malloc(sizeof *ndp);
97 if (NULL == ndp) {
98 NaClLog(LOG_FATAL,
99 "NaClDescDirDescMake: no memory for 0x%08x\n",
100 (uintptr_t) nhdp);
102 if (!NaClDescDirDescCtor(ndp, nhdp)) {
103 NaClLog(LOG_FATAL,
104 "NaClDescDirDescMake: NaClDescDirDescCtor(0x%08x,0x%08x) failed\n",
105 (uintptr_t) ndp,
106 (uintptr_t) nhdp);
108 return ndp;
111 struct NaClDescDirDesc *NaClDescDirDescOpen(char *path)
113 struct NaClHostDir *nhdp;
115 nhdp = malloc(sizeof *nhdp);
116 if (NULL == nhdp) {
117 NaClLog(LOG_FATAL, "NaClDescDirDescOpen: no memory for %s\n", path);
119 if (!NaClHostDirOpen(nhdp, path)) {
120 NaClLog(LOG_FATAL, "NaClDescDirDescOpen: NaClHostDirOpen failed for %s\n",
121 path);
123 return NaClDescDirDescMake(nhdp);
126 ssize_t NaClDescDirDescRead(struct NaClDesc *vself,
127 struct NaClDescEffector *effp,
128 void *buf,
129 size_t len)
131 /* NaClLog(LOG_ERROR, "NaClDescDirDescRead: Read not allowed on dir\n"); */
132 return NaClDescDirDescGetdents(vself, effp, buf, len);
133 /* return -NACL_ABI_EINVAL; */
136 ssize_t NaClDescDirDescGetdents(struct NaClDesc *vself,
137 struct NaClDescEffector *effp,
138 void *dirp,
139 size_t count)
141 struct NaClDescDirDesc *self = (struct NaClDescDirDesc *) vself;
142 struct nacl_abi_dirent *direntp = (struct nacl_abi_dirent *) dirp;
143 ssize_t retval;
145 NaClLog(3, "NaClDescDirDescGetdents(0x%08x, %u):\n", dirp, count);
146 retval = NaClHostDirGetdents(self->hd, dirp, count);
147 NaClLog(3, "NaClDescDirDescGetdents(d_ino=%u, d_off=%u, d_reclen=%u, "
148 "d_name='%s')\n",
149 direntp->nacl_abi_d_ino,
150 direntp->nacl_abi_d_off,
151 direntp->nacl_abi_d_reclen,
152 direntp->nacl_abi_d_name);
153 return retval;
156 int NaClDescDirDescClose(struct NaClDesc *vself,
157 struct NaClDescEffector *effp)
159 NaClDescUnref(vself);
160 return 0;
163 int NaClDescDirDescExternalizeSize(struct NaClDesc *vself,
164 size_t *nbytes,
165 size_t *nhandles)
167 *nbytes = 0;
168 *nhandles = 1;
169 return 0;
172 struct NaClDescVtbl const kNaClDescDirDescVtbl = {
173 NaClDescDirDescDtor,
174 NaClDescMapNotImplemented,
175 NaClDescUnmapUnsafeNotImplemented,
176 NaClDescUnmapNotImplemented,
177 NaClDescDirDescRead,
178 NaClDescWriteNotImplemented,
179 NaClDescSeekNotImplemented,
180 NaClDescIoctlNotImplemented,
181 NaClDescFstatNotImplemented,
182 NaClDescDirDescClose,
183 NaClDescDirDescGetdents,
184 NACL_DESC_DIR,
185 NaClDescDirDescExternalizeSize,
186 NaClDescExternalizeNotImplemented,
187 NaClDescLockNotImplemented,
188 NaClDescTryLockNotImplemented,
189 NaClDescUnlockNotImplemented,
190 NaClDescWaitNotImplemented,
191 NaClDescTimedWaitAbsNotImplemented,
192 NaClDescSignalNotImplemented,
193 NaClDescBroadcastNotImplemented,
194 NaClDescSendMsgNotImplemented,
195 NaClDescRecvMsgNotImplemented,
196 NaClDescConnectAddrNotImplemented,
197 NaClDescAcceptConnNotImplemented,
198 NaClDescPostNotImplemented,
199 NaClDescSemWaitNotImplemented,
200 NaClDescGetValueNotImplemented,
203 int NaClDescDirInternalize(struct NaClDesc **baseptr,
204 struct NaClDescXferState *xfer)
206 NaClLog(LOG_ERROR, "NaClDescDirDescInternalize: not implemented for dir\n");
207 return -NACL_ABI_EINVAL;