Deleted Added
sdiff udiff text old ( 122861 ) new ( 133816 )
full compact
1/* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
2/* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
3/*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bill Sommerfeld.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <sys/cdefs.h>
40__FBSDID("$FreeBSD: head/sys/compat/linux/linux_getcwd.c 122861 2003-11-17 18:57:20Z rwatson $");
41
42#include "opt_compat.h"
43#include "opt_mac.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/namei.h>
48#include <sys/filedesc.h>
49#include <sys/kernel.h>
50#include <sys/file.h>
51#include <sys/stat.h>
52#include <sys/syscallsubr.h>
53#include <sys/vnode.h>
54#include <sys/mount.h>
55#include <sys/proc.h>
56#include <sys/uio.h>
57#include <sys/mac.h>
58#include <sys/malloc.h>
59#include <sys/dirent.h>
60#include <ufs/ufs/dir.h> /* XXX only for DIRBLKSIZ */
61
62#include <machine/../linux/linux.h>
63#include <machine/../linux/linux_proto.h>
64#include <compat/linux/linux_util.h>
65
66static int
67linux_getcwd_scandir(struct vnode **, struct vnode **,
68 char **, char *, struct thread *);
69static int
70linux_getcwd_common(struct vnode *, struct vnode *,
71 char **, char *, int, int, struct thread *);
72
73#define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
74
75/*
76 * Vnode variable naming conventions in this file:
77 *
78 * rvp: the current root we're aiming towards.
79 * lvp, *lvpp: the "lower" vnode
80 * uvp, *uvpp: the "upper" vnode.
81 *
82 * Since all the vnodes we're dealing with are directories, and the
83 * lookups are going *up* in the filesystem rather than *down*, the
84 * usual "pvp" (parent) or "dvp" (directory) naming conventions are
85 * too confusing.
86 */
87
88/*
89 * XXX Will infinite loop in certain cases if a directory read reliably
90 * returns EINVAL on last block.
91 * XXX is EINVAL the right thing to return if a directory is malformed?
92 */
93
94/*
95 * XXX Untested vs. mount -o union; probably does the wrong thing.
96 */
97
98/*
99 * Find parent vnode of *lvpp, return in *uvpp
100 *
101 * If we care about the name, scan it looking for name of directory
102 * entry pointing at lvp.
103 *
104 * Place the name in the buffer which starts at bufp, immediately
105 * before *bpp, and move bpp backwards to point at the start of it.
106 *
107 * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
108 * On exit, *uvpp is either NULL or is a locked vnode reference.
109 */
110static int
111linux_getcwd_scandir(lvpp, uvpp, bpp, bufp, td)
112 struct vnode **lvpp;
113 struct vnode **uvpp;
114 char **bpp;
115 char *bufp;
116 struct thread *td;
117{
118 int error = 0;
119 int eofflag;
120 off_t off;
121 int tries;
122 struct uio uio;
123 struct iovec iov;
124 char *dirbuf = NULL;
125 int dirbuflen;
126 ino_t fileno;
127 struct vattr va;
128 struct vnode *uvp = NULL;
129 struct vnode *lvp = *lvpp;
130 struct componentname cn;
131 int len, reclen;
132 tries = 0;
133
134 /*
135 * If we want the filename, get some info we need while the
136 * current directory is still locked.
137 */
138 if (bufp != NULL) {
139 error = VOP_GETATTR(lvp, &va, td->td_ucred, td);
140 if (error) {
141 vput(lvp);
142 *lvpp = NULL;
143 *uvpp = NULL;
144 return error;
145 }
146 }
147
148 /*
149 * Ok, we have to do it the hard way..
150 * Next, get parent vnode using lookup of ..
151 */
152 cn.cn_nameiop = LOOKUP;
153 cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
154 cn.cn_thread = td;
155 cn.cn_cred = td->td_ucred;
156 cn.cn_pnbuf = NULL;
157 cn.cn_nameptr = "..";
158 cn.cn_namelen = 2;
159 cn.cn_consume = 0;
160
161 /*
162 * At this point, lvp is locked and will be unlocked by the lookup.
163 * On successful return, *uvpp will be locked
164 */
165#ifdef MAC
166 error = mac_check_vnode_lookup(td->td_ucred, lvp, &cn);
167 if (error == 0)
168#endif
169 error = VOP_LOOKUP(lvp, uvpp, &cn);
170 if (error) {
171 vput(lvp);
172 *lvpp = NULL;
173 *uvpp = NULL;
174 return error;
175 }
176 uvp = *uvpp;
177
178 /* If we don't care about the pathname, we're done */
179 if (bufp == NULL) {
180 vrele(lvp);
181 *lvpp = NULL;
182 return 0;
183 }
184
185 fileno = va.va_fileid;
186
187 dirbuflen = DIRBLKSIZ;
188 if (dirbuflen < va.va_blocksize)
189 dirbuflen = va.va_blocksize;
190 dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
191
192#if 0
193unionread:
194#endif
195 off = 0;
196 do {
197 /* call VOP_READDIR of parent */
198 iov.iov_base = dirbuf;
199 iov.iov_len = dirbuflen;
200
201 uio.uio_iov = &iov;
202 uio.uio_iovcnt = 1;
203 uio.uio_offset = off;
204 uio.uio_resid = dirbuflen;
205 uio.uio_segflg = UIO_SYSSPACE;
206 uio.uio_rw = UIO_READ;
207 uio.uio_td = td;
208
209 eofflag = 0;
210
211#ifdef MAC
212 error = mac_check_vnode_readdir(td->td_ucred, uvp);
213 if (error == 0)
214#endif /* MAC */
215 error = VOP_READDIR(uvp, &uio, td->td_ucred, &eofflag,
216 0, 0);
217
218 off = uio.uio_offset;
219
220 /*
221 * Try again if NFS tosses its cookies.
222 * XXX this can still loop forever if the directory is busted
223 * such that the second or subsequent page of it always
224 * returns EINVAL
225 */
226 if ((error == EINVAL) && (tries < 3)) {
227 off = 0;
228 tries++;
229 continue; /* once more, with feeling */
230 }
231
232 if (!error) {
233 char *cpos;
234 struct dirent *dp;
235
236 cpos = dirbuf;
237 tries = 0;
238
239 /* scan directory page looking for matching vnode */
240 for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) {
241 dp = (struct dirent *) cpos;
242 reclen = dp->d_reclen;
243
244 /* check for malformed directory.. */
245 if (reclen < DIRENT_MINSIZE) {
246 error = EINVAL;
247 goto out;
248 }
249 /*
250 * XXX should perhaps do VOP_LOOKUP to
251 * check that we got back to the right place,
252 * but getting the locking games for that
253 * right would be heinous.
254 */
255 if ((dp->d_type != DT_WHT) &&
256 (dp->d_fileno == fileno)) {
257 char *bp = *bpp;
258 bp -= dp->d_namlen;
259
260 if (bp <= bufp) {
261 error = ERANGE;
262 goto out;
263 }
264 bcopy(dp->d_name, bp, dp->d_namlen);
265 error = 0;
266 *bpp = bp;
267 goto out;
268 }
269 cpos += reclen;
270 }
271 }
272 } while (!eofflag);
273 error = ENOENT;
274
275out:
276 vrele(lvp);
277 *lvpp = NULL;
278 free(dirbuf, M_TEMP);
279 return error;
280}
281
282
283/*
284 * common routine shared by sys___getcwd() and linux_vn_isunder()
285 */
286
287#define GETCWD_CHECK_ACCESS 0x0001
288
289static int
290linux_getcwd_common (lvp, rvp, bpp, bufp, limit, flags, td)
291 struct vnode *lvp;
292 struct vnode *rvp;
293 char **bpp;
294 char *bufp;
295 int limit;
296 int flags;
297 struct thread *td;
298{
299 struct filedesc *fdp = td->td_proc->p_fd;
300 struct vnode *uvp = NULL;
301 char *bp = NULL;
302 int error;
303 int perms = VEXEC;
304
305 if (rvp == NULL) {
306 rvp = fdp->fd_rdir;
307 if (rvp == NULL)
308 rvp = rootvnode;
309 }
310
311 VREF(rvp);
312 VREF(lvp);
313
314 /*
315 * Error handling invariant:
316 * Before a `goto out':
317 * lvp is either NULL, or locked and held.
318 * uvp is either NULL, or locked and held.
319 */
320
321 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, td);
322 if (error) {
323 vrele(lvp);
324 lvp = NULL;
325 goto out;
326 }
327 if (bufp)
328 bp = *bpp;
329 /*
330 * this loop will terminate when one of the following happens:
331 * - we hit the root
332 * - getdirentries or lookup fails
333 * - we run out of space in the buffer.
334 */
335 if (lvp == rvp) {
336 if (bp)
337 *(--bp) = '/';
338 goto out;
339 }
340 do {
341 if (lvp->v_type != VDIR) {
342 error = ENOTDIR;
343 goto out;
344 }
345
346 /*
347 * access check here is optional, depending on
348 * whether or not caller cares.
349 */
350 if (flags & GETCWD_CHECK_ACCESS) {
351 error = VOP_ACCESS(lvp, perms, td->td_ucred, td);
352 if (error)
353 goto out;
354 perms = VEXEC|VREAD;
355 }
356
357 /*
358 * step up if we're a covered vnode..
359 */
360 while (lvp->v_vflag & VV_ROOT) {
361 struct vnode *tvp;
362
363 if (lvp == rvp)
364 goto out;
365
366 tvp = lvp;
367 lvp = lvp->v_mount->mnt_vnodecovered;
368 vput(tvp);
369 /*
370 * hodie natus est radici frater
371 */
372 if (lvp == NULL) {
373 error = ENOENT;
374 goto out;
375 }
376 VREF(lvp);
377 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, td);
378 if (error != 0) {
379 vrele(lvp);
380 lvp = NULL;
381 goto out;
382 }
383 }
384 error = linux_getcwd_scandir(&lvp, &uvp, &bp, bufp, td);
385 if (error)
386 goto out;
387#if DIAGNOSTIC
388 if (lvp != NULL)
389 panic("getcwd: oops, forgot to null lvp");
390 if (bufp && (bp <= bufp)) {
391 panic("getcwd: oops, went back too far");
392 }
393#endif
394 if (bp)
395 *(--bp) = '/';
396 lvp = uvp;
397 uvp = NULL;
398 limit--;
399 } while ((lvp != rvp) && (limit > 0));
400
401out:
402 if (bpp)
403 *bpp = bp;
404 if (uvp)
405 vput(uvp);
406 if (lvp)
407 vput(lvp);
408 vrele(rvp);
409 return error;
410}
411
412
413/*
414 * Find pathname of process's current directory.
415 *
416 * Use vfs vnode-to-name reverse cache; if that fails, fall back
417 * to reading directory contents.
418 */
419
420int
421linux_getcwd(struct thread *td, struct linux_getcwd_args *args)
422{
423 caddr_t bp, bend, path;
424 int error, len, lenused;
425
426#ifdef DEBUG
427 printf("Linux-emul(%ld): getcwd(%p, %ld)\n", (long)td->td_proc->p_pid,
428 args->buf, (long)args->bufsize);
429#endif
430
431 len = args->bufsize;
432
433 if (len > MAXPATHLEN*4)
434 len = MAXPATHLEN*4;
435 else if (len < 2)
436 return ERANGE;
437
438 path = (char *)malloc(len, M_TEMP, M_WAITOK);
439
440 error = kern___getcwd(td, path, UIO_SYSSPACE, len);
441 if (!error) {
442 lenused = strlen(path) + 1;
443 if (lenused <= args->bufsize) {
444 td->td_retval[0] = lenused;
445 error = copyout(path, args->buf, lenused);
446 }
447 else
448 error = ERANGE;
449 } else {
450 bp = &path[len];
451 bend = bp;
452 *(--bp) = '\0';
453
454 /*
455 * 5th argument here is "max number of vnodes to traverse".
456 * Since each entry takes up at least 2 bytes in the output buffer,
457 * limit it to N/2 vnodes for an N byte buffer.
458 */
459
460 error = linux_getcwd_common (td->td_proc->p_fd->fd_cdir, NULL,
461 &bp, path, len/2, GETCWD_CHECK_ACCESS, td);
462
463 if (error)
464 goto out;
465 lenused = bend - bp;
466 td->td_retval[0] = lenused;
467 /* put the result into user buffer */
468 error = copyout(bp, args->buf, lenused);
469 }
470out:
471 free(path, M_TEMP);
472 return (error);
473}
474