Sat Jan 28 21:54:57 2017 UTC ()
avoid the shadow


(christos)
diff -r1.16 -r1.17 src/sys/compat/linux32/common/linux32_dirent.c

cvs diff -r1.16 -r1.17 src/sys/compat/linux32/common/linux32_dirent.c (switch to unified diff)

--- src/sys/compat/linux32/common/linux32_dirent.c 2017/01/28 15:09:55 1.16
+++ src/sys/compat/linux32/common/linux32_dirent.c 2017/01/28 21:54:57 1.17
@@ -1,297 +1,297 @@ @@ -1,297 +1,297 @@
1/* $NetBSD: linux32_dirent.c,v 1.16 2017/01/28 15:09:55 christos Exp $ */ 1/* $NetBSD: linux32_dirent.c,v 1.17 2017/01/28 21:54:57 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved. 4 * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software 14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement: 15 * must display the following acknowledgement:
16 * This product includes software developed by Emmanuel Dreyfus 16 * This product includes software developed by Emmanuel Dreyfus
17 * 4. The name of the author may not be used to endorse or promote  17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior written  18 * products derived from this software without specific prior written
19 * permission. 19 * permission.
20 * 20 *
21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''  21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS  24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE. 31 * POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#include <sys/cdefs.h> 34#include <sys/cdefs.h>
35 35
36__KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.16 2017/01/28 15:09:55 christos Exp $"); 36__KERNEL_RCSID(0, "$NetBSD: linux32_dirent.c,v 1.17 2017/01/28 21:54:57 christos Exp $");
37 37
38#include <sys/types.h> 38#include <sys/types.h>
39#include <sys/param.h> 39#include <sys/param.h>
40#include <sys/fstypes.h> 40#include <sys/fstypes.h>
41#include <sys/signal.h> 41#include <sys/signal.h>
42#include <sys/dirent.h> 42#include <sys/dirent.h>
43#include <sys/kernel.h> 43#include <sys/kernel.h>
44#include <sys/fcntl.h> 44#include <sys/fcntl.h>
45#include <sys/file.h> 45#include <sys/file.h>
46#include <sys/filedesc.h> 46#include <sys/filedesc.h>
47#include <sys/malloc.h> 47#include <sys/malloc.h>
48#include <sys/select.h> 48#include <sys/select.h>
49#include <sys/proc.h> 49#include <sys/proc.h>
50#include <sys/ucred.h> 50#include <sys/ucred.h>
51#include <sys/vnode.h> 51#include <sys/vnode.h>
52#include <sys/swap.h> 52#include <sys/swap.h>
53 53
54#include <machine/types.h> 54#include <machine/types.h>
55 55
56#include <sys/syscallargs.h> 56#include <sys/syscallargs.h>
57 57
58#include <compat/netbsd32/netbsd32.h> 58#include <compat/netbsd32/netbsd32.h>
59#include <compat/netbsd32/netbsd32_conv.h> 59#include <compat/netbsd32/netbsd32_conv.h>
60#include <compat/netbsd32/netbsd32_syscallargs.h> 60#include <compat/netbsd32/netbsd32_syscallargs.h>
61 61
62#include <compat/linux/common/linux_types.h> 62#include <compat/linux/common/linux_types.h>
63#include <compat/linux/common/linux_signal.h> 63#include <compat/linux/common/linux_signal.h>
64#include <compat/linux/common/linux_machdep.h> 64#include <compat/linux/common/linux_machdep.h>
65#include <compat/linux/common/linux_misc.h> 65#include <compat/linux/common/linux_misc.h>
66#include <compat/linux/common/linux_oldolduname.h> 66#include <compat/linux/common/linux_oldolduname.h>
67#include <compat/linux/common/linux_dirent.h> 67#include <compat/linux/common/linux_dirent.h>
68#include <compat/linux/common/linux_ipc.h> 68#include <compat/linux/common/linux_ipc.h>
69#include <compat/linux/common/linux_sem.h> 69#include <compat/linux/common/linux_sem.h>
70#include <compat/linux/linux_syscallargs.h> 70#include <compat/linux/linux_syscallargs.h>
71 71
72#include <compat/linux32/common/linux32_types.h> 72#include <compat/linux32/common/linux32_types.h>
73#include <compat/linux32/common/linux32_signal.h> 73#include <compat/linux32/common/linux32_signal.h>
74#include <compat/linux32/common/linux32_machdep.h> 74#include <compat/linux32/common/linux32_machdep.h>
75#include <compat/linux32/common/linux32_sysctl.h> 75#include <compat/linux32/common/linux32_sysctl.h>
76#include <compat/linux32/common/linux32_socketcall.h> 76#include <compat/linux32/common/linux32_socketcall.h>
77#include <compat/linux32/linux32_syscallargs.h> 77#include <compat/linux32/linux32_syscallargs.h>
78 78
79/* 79/*
80 * Linux 'readdir' call. This code is mostly taken from the 80 * Linux 'readdir' call. This code is mostly taken from the
81 * SunOS getdents call (see compat/sunos/sunos_misc.c), though 81 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
82 * an attempt has been made to keep it a little cleaner (failing 82 * an attempt has been made to keep it a little cleaner (failing
83 * miserably, because of the cruft needed if count 1 is passed). 83 * miserably, because of the cruft needed if count 1 is passed).
84 * 84 *
85 * The d_off field should contain the offset of the next valid entry, 85 * The d_off field should contain the offset of the next valid entry,
86 * but in Linux it has the offset of the entry itself. We emulate 86 * but in Linux it has the offset of the entry itself. We emulate
87 * that bug here. 87 * that bug here.
88 * 88 *
89 * Read in BSD-style entries, convert them, and copy them out. 89 * Read in BSD-style entries, convert them, and copy them out.
90 * 90 *
91 * Note that this doesn't handle union-mounted filesystems. 91 * Note that this doesn't handle union-mounted filesystems.
92 */ 92 */
93 93
94int 94int
95linux32_sys_getdents(struct lwp *l, const struct linux32_sys_getdents_args *uap, register_t *retval) 95linux32_sys_getdents(struct lwp *l, const struct linux32_sys_getdents_args *uap, register_t *retval)
96{ 96{
97 /* { 97 /* {
98 syscallarg(int) fd; 98 syscallarg(int) fd;
99 syscallarg(linux32_direntp_t) dent; 99 syscallarg(linux32_direntp_t) dent;
100 syscallarg(unsigned int) count; 100 syscallarg(unsigned int) count;
101 } */ 101 } */
102 struct dirent *bdp; 102 struct dirent *bdp;
103 struct vnode *vp; 103 struct vnode *vp;
104 char *inp, *tbuf; /* BSD-format */ 104 char *inp, *tbuf; /* BSD-format */
105 int len, reclen; /* BSD-format */ 105 int len, reclen; /* BSD-format */
106 char *outp; /* Linux-format */ 106 char *outp; /* Linux-format */
107 int resid, linux32_reclen = 0; /* Linux-format */ 107 int resid, linux32_reclen = 0; /* Linux-format */
108 struct file *fp; 108 struct file *fp;
109 struct uio auio; 109 struct uio auio;
110 struct iovec aiov; 110 struct iovec aiov;
111 struct linux32_dirent idb; 111 struct linux32_dirent idb;
112 off_t off; /* true file offset */ 112 off_t off; /* true file offset */
113 int buflen, error, eofflag, nbytes, oldcall; 113 int buflen, error, eofflag, nbytes, oldcall;
114 struct vattr va; 114 struct vattr va;
115 off_t *cookiebuf = NULL, *cookie; 115 off_t *cookiebuf = NULL, *cookie;
116 int ncookies; 116 int ncookies;
117 117
118 /* fd_getvnode() will use the descriptor for us */ 118 /* fd_getvnode() will use the descriptor for us */
119 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0) 119 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
120 return (error); 120 return (error);
121 121
122 if ((fp->f_flag & FREAD) == 0) { 122 if ((fp->f_flag & FREAD) == 0) {
123 error = EBADF; 123 error = EBADF;
124 goto out1; 124 goto out1;
125 } 125 }
126 126
127 vp = (struct vnode *)fp->f_data; 127 vp = (struct vnode *)fp->f_data;
128 if (vp->v_type != VDIR) { 128 if (vp->v_type != VDIR) {
129 error = ENOTDIR; 129 error = ENOTDIR;
130 goto out1; 130 goto out1;
131 } 131 }
132 132
133 vn_lock(vp, LK_SHARED | LK_RETRY); 133 vn_lock(vp, LK_SHARED | LK_RETRY);
134 error = VOP_GETATTR(vp, &va, l->l_cred); 134 error = VOP_GETATTR(vp, &va, l->l_cred);
135 VOP_UNLOCK(vp); 135 VOP_UNLOCK(vp);
136 if (error) 136 if (error)
137 goto out1; 137 goto out1;
138 138
139 nbytes = SCARG(uap, count); 139 nbytes = SCARG(uap, count);
140 if (nbytes == 1) { /* emulating old, broken behaviour */ 140 if (nbytes == 1) { /* emulating old, broken behaviour */
141 nbytes = sizeof (idb); 141 nbytes = sizeof (idb);
142 buflen = max(va.va_blocksize, nbytes); 142 buflen = max(va.va_blocksize, nbytes);
143 oldcall = 1; 143 oldcall = 1;
144 } else { 144 } else {
145 buflen = min(MAXBSIZE, nbytes); 145 buflen = min(MAXBSIZE, nbytes);
146 if (buflen < va.va_blocksize) 146 if (buflen < va.va_blocksize)
147 buflen = va.va_blocksize; 147 buflen = va.va_blocksize;
148 oldcall = 0; 148 oldcall = 0;
149 } 149 }
150 tbuf = malloc(buflen, M_TEMP, M_WAITOK); 150 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
151 151
152 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 152 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
153 off = fp->f_offset; 153 off = fp->f_offset;
154again: 154again:
155 aiov.iov_base = tbuf; 155 aiov.iov_base = tbuf;
156 aiov.iov_len = buflen; 156 aiov.iov_len = buflen;
157 auio.uio_iov = &aiov; 157 auio.uio_iov = &aiov;
158 auio.uio_iovcnt = 1; 158 auio.uio_iovcnt = 1;
159 auio.uio_rw = UIO_READ; 159 auio.uio_rw = UIO_READ;
160 auio.uio_resid = buflen; 160 auio.uio_resid = buflen;
161 auio.uio_offset = off; 161 auio.uio_offset = off;
162 UIO_SETUP_SYSSPACE(&auio); 162 UIO_SETUP_SYSSPACE(&auio);
163 /* 163 /*
164 * First we read into the malloc'ed buffer, then 164 * First we read into the malloc'ed buffer, then
165 * we massage it into user space, one record at a time. 165 * we massage it into user space, one record at a time.
166 */ 166 */
167 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf, 167 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
168 &ncookies); 168 &ncookies);
169 if (error) 169 if (error)
170 goto out; 170 goto out;
171 171
172 inp = tbuf; 172 inp = tbuf;
173 outp = (void *)SCARG_P32(uap, dent); 173 outp = (void *)SCARG_P32(uap, dent);
174 resid = nbytes; 174 resid = nbytes;
175 if ((len = buflen - auio.uio_resid) == 0) 175 if ((len = buflen - auio.uio_resid) == 0)
176 goto eof; 176 goto eof;
177 177
178 for (cookie = cookiebuf; len > 0; len -= reclen) { 178 for (cookie = cookiebuf; len > 0; len -= reclen) {
179 bdp = (struct dirent *)inp; 179 bdp = (struct dirent *)inp;
180 reclen = bdp->d_reclen; 180 reclen = bdp->d_reclen;
181 if (reclen & 3) 181 if (reclen & 3)
182 panic("linux32_readdir"); 182 panic("linux32_readdir");
183 if (bdp->d_fileno == 0) { 183 if (bdp->d_fileno == 0) {
184 inp += reclen; /* it is a hole; squish it out */ 184 inp += reclen; /* it is a hole; squish it out */
185 if (cookie) 185 if (cookie)
186 off = *cookie++; 186 off = *cookie++;
187 else 187 else
188 off += reclen; 188 off += reclen;
189 continue; 189 continue;
190 } 190 }
191 linux32_reclen = LINUX_RECLEN(&idb, bdp->d_namlen); 191 linux32_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
192 if (reclen > len || resid < linux32_reclen) { 192 if (reclen > len || resid < linux32_reclen) {
193 /* entry too big for buffer, so just stop */ 193 /* entry too big for buffer, so just stop */
194 outp++; 194 outp++;
195 break; 195 break;
196 } 196 }
197 /* 197 /*
198 * Massage in place to make a Linux-shaped dirent (otherwise 198 * Massage in place to make a Linux-shaped dirent (otherwise
199 * we have to worry about touching user memory outside of 199 * we have to worry about touching user memory outside of
200 * the copyout() call). 200 * the copyout() call).
201 */ 201 */
202 idb.d_ino = bdp->d_fileno; 202 idb.d_ino = bdp->d_fileno;
203 /* 203 /*
204 * The old readdir() call misuses the offset and reclen fields. 204 * The old readdir() call misuses the offset and reclen fields.
205 */ 205 */
206 if (oldcall) { 206 if (oldcall) {
207 idb.d_off = (linux32_off_t)linux32_reclen; 207 idb.d_off = (linux32_off_t)linux32_reclen;
208 idb.d_reclen = (u_short)bdp->d_namlen; 208 idb.d_reclen = (u_short)bdp->d_namlen;
209 } else { 209 } else {
210 if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) { 210 if (sizeof (idb.d_off) <= 4 && (off >> 32) != 0) {
211 compat_offseterr(vp, "linux32_getdents"); 211 compat_offseterr(vp, "linux32_getdents");
212 error = EINVAL; 212 error = EINVAL;
213 goto out; 213 goto out;
214 } 214 }
215 idb.d_off = (linux32_off_t)off; 215 idb.d_off = (linux32_off_t)off;
216 idb.d_reclen = (u_short)linux32_reclen; 216 idb.d_reclen = (u_short)linux32_reclen;
217 } 217 }
218 size_t l = MIN(sizeof(idb.d_name) - 1, bdp->d_namlen + 1); 218 size_t dirl = MIN(sizeof(idb.d_name) - 1, bdp->d_namlen + 1);
219 memcpy(idb.d_name, bdp->d_name, l); 219 memcpy(idb.d_name, bdp->d_name, dirl);
220 idb.d_name[l + 1] = bdp->d_type; 220 idb.d_name[dirl + 1] = bdp->d_type;
221 if ((error = copyout((void *)&idb, outp, linux32_reclen))) 221 if ((error = copyout((void *)&idb, outp, linux32_reclen)))
222 goto out; 222 goto out;
223 /* advance past this real entry */ 223 /* advance past this real entry */
224 inp += reclen; 224 inp += reclen;
225 if (cookie) 225 if (cookie)
226 off = *cookie++; /* each entry points to itself */ 226 off = *cookie++; /* each entry points to itself */
227 else 227 else
228 off += reclen; 228 off += reclen;
229 /* advance output past Linux-shaped entry */ 229 /* advance output past Linux-shaped entry */
230 outp += linux32_reclen; 230 outp += linux32_reclen;
231 resid -= linux32_reclen; 231 resid -= linux32_reclen;
232 if (oldcall) 232 if (oldcall)
233 break; 233 break;
234 } 234 }
235 235
236 /* if we squished out the whole block, try again */ 236 /* if we squished out the whole block, try again */
237 if (outp == (void *)SCARG_P32(uap, dent)) { 237 if (outp == (void *)SCARG_P32(uap, dent)) {
238 if (cookiebuf) 238 if (cookiebuf)
239 free(cookiebuf, M_TEMP); 239 free(cookiebuf, M_TEMP);
240 cookiebuf = NULL; 240 cookiebuf = NULL;
241 goto again; 241 goto again;
242 } 242 }
243 fp->f_offset = off; /* update the vnode offset */ 243 fp->f_offset = off; /* update the vnode offset */
244 244
245 if (oldcall) 245 if (oldcall)
246 nbytes = resid + linux32_reclen; 246 nbytes = resid + linux32_reclen;
247 247
248eof: 248eof:
249 *retval = nbytes - resid; 249 *retval = nbytes - resid;
250out: 250out:
251 VOP_UNLOCK(vp); 251 VOP_UNLOCK(vp);
252 if (cookiebuf) 252 if (cookiebuf)
253 free(cookiebuf, M_TEMP); 253 free(cookiebuf, M_TEMP);
254 free(tbuf, M_TEMP); 254 free(tbuf, M_TEMP);
255out1: 255out1:
256 fd_putfile(SCARG(uap, fd)); 256 fd_putfile(SCARG(uap, fd));
257 return error; 257 return error;
258} 258}
259 259
260int 260int
261linux32_sys_getdents64(struct lwp *l, const struct linux32_sys_getdents64_args *uap, register_t *retval) 261linux32_sys_getdents64(struct lwp *l, const struct linux32_sys_getdents64_args *uap, register_t *retval)
262{ 262{
263 /* { 263 /* {
264 syscallarg(int) fd; 264 syscallarg(int) fd;
265 syscallarg(linux32_dirent64p_t) dent; 265 syscallarg(linux32_dirent64p_t) dent;
266 syscallarg(unsigned int) count; 266 syscallarg(unsigned int) count;
267 } */ 267 } */
268 struct linux_sys_getdents64_args ua; 268 struct linux_sys_getdents64_args ua;
269 269
270 NETBSD32TO64_UAP(fd); 270 NETBSD32TO64_UAP(fd);
271 NETBSD32TOP_UAP(dent, struct linux_dirent64); 271 NETBSD32TOP_UAP(dent, struct linux_dirent64);
272 NETBSD32TO64_UAP(count); 272 NETBSD32TO64_UAP(count);
273 273
274 return linux_sys_getdents64(l, &ua, retval); 274 return linux_sys_getdents64(l, &ua, retval);
275} 275}
276 276
277int 277int
278linux32_sys_readdir(struct lwp *l, const struct linux32_sys_readdir_args *uap, register_t *retval) 278linux32_sys_readdir(struct lwp *l, const struct linux32_sys_readdir_args *uap, register_t *retval)
279{ 279{
280 /* { 280 /* {
281 syscallarg(int) fd; 281 syscallarg(int) fd;
282 syscallarg(struct linux32_direntp_t) dent; 282 syscallarg(struct linux32_direntp_t) dent;
283 syscallarg(unsigned int) count; 283 syscallarg(unsigned int) count;
284 } */ 284 } */
285 int error; 285 int error;
286 struct linux32_sys_getdents_args da; 286 struct linux32_sys_getdents_args da;
287 287
288 SCARG(&da, fd) = SCARG(uap, fd); 288 SCARG(&da, fd) = SCARG(uap, fd);
289 SCARG(&da, dent) = SCARG(uap, dent); 289 SCARG(&da, dent) = SCARG(uap, dent);
290 SCARG(&da, count) = 1; 290 SCARG(&da, count) = 1;
291 291
292 error = linux32_sys_getdents(l, &da, retval); 292 error = linux32_sys_getdents(l, &da, retval);
293 if (error == 0 && *retval > 1) 293 if (error == 0 && *retval > 1)
294 *retval = 1; 294 *retval = 1;
295 295
296 return error; 296 return error;
297} 297}