vfs_getcwd.c revision 1.8 1 /* $NetBSD: vfs_getcwd.c,v 1.8 1999/06/21 05:11:09 sommerfeld Exp $ */
2
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/param.h>
40 #include <sys/systm.h>
41 #include <sys/namei.h>
42 #include <sys/filedesc.h>
43 #include <sys/kernel.h>
44 #include <sys/file.h>
45 #include <sys/stat.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/proc.h>
49 #include <sys/uio.h>
50 #include <sys/malloc.h>
51 #include <sys/dirent.h>
52 #include <ufs/ufs/dir.h> /* XXX only for DIRBLKSIZ */
53
54 #include <sys/syscallargs.h>
55
56 static int
57 getcwd_scandir __P((struct vnode **, struct vnode **,
58 char **, char *, struct proc *));
59 static int
60 getcwd_getcache __P((struct vnode **, struct vnode **,
61 char **, char *));
62 static int
63 getcwd_common __P((struct vnode *, struct vnode *,
64 char **, char *, int, int, struct proc *));
65
66 int vn_isunder __P((struct vnode *, struct vnode *, struct proc *));
67
68 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
69
70 /*
71 * XXX Will infinite loop in certain cases if a directory read reliably
72 * returns EINVAL on last block.
73 * XXX is EINVAL the right thing to return if a directory is malformed?
74 */
75
76 /*
77 * Find parent vnode of cvp, return in *pvpp
78 * Scan it looking for name of directory entry pointing at cvp.
79 *
80 * Place the name in the buffer which starts at bufp, immediately
81 * before *bpp, and move bpp backwards to point at the start of it.
82 *
83 * On entry, *cvpp is a locked vnode reference; on exit, it is vput and NULL'ed
84 * On exit, *pvpp is either NULL or is a locked vnode reference.
85 */
86 static int
87 getcwd_scandir(cvpp, pvpp, bpp, bufp, p)
88 struct vnode **cvpp;
89 struct vnode **pvpp;
90 char **bpp;
91 char *bufp;
92 struct proc *p;
93 {
94 int error = 0;
95 int eofflag;
96 off_t off;
97 int tries;
98 struct uio uio;
99 struct iovec iov;
100 char *dirbuf = NULL;
101 int dirbuflen;
102 ino_t fileno;
103 struct vattr va;
104 struct vnode *pvp = NULL;
105 struct vnode *cvp = *cvpp;
106 struct componentname cn;
107 int len, reclen;
108 tries = 0;
109
110 /*
111 * If we want the filename, get some info we need while the
112 * current directory is still locked.
113 */
114 if (bufp != NULL) {
115 error = VOP_GETATTR(cvp, &va, p->p_ucred, p);
116 if (error) {
117 vput(cvp);
118 *cvpp = NULL;
119 *pvpp = NULL;
120 return error;
121 }
122 }
123
124 /*
125 * Ok, we have to do it the hard way..
126 * Next, get parent vnode using lookup of ..
127 */
128 cn.cn_nameiop = LOOKUP;
129 cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
130 cn.cn_proc = p;
131 cn.cn_cred = p->p_ucred;
132 cn.cn_pnbuf = NULL;
133 cn.cn_nameptr = "..";
134 cn.cn_namelen = 2;
135 cn.cn_hash = 0;
136 cn.cn_consume = 0;
137
138 /*
139 * At this point, cvp is locked and will be unlocked by the lookup.
140 * On successful return, *pvpp will be locked
141 */
142 error = VOP_LOOKUP(cvp, pvpp, &cn);
143 if (error) {
144 vput(cvp);
145 *cvpp = NULL;
146 *pvpp = NULL;
147 return error;
148 }
149 pvp = *pvpp;
150
151 /* If we don't care about the pathname, we're done */
152 if (bufp == NULL) {
153 vrele(cvp);
154 *cvpp = NULL;
155 return 0;
156 }
157
158 fileno = va.va_fileid;
159
160 dirbuflen = DIRBLKSIZ;
161 if (dirbuflen < va.va_blocksize)
162 dirbuflen = va.va_blocksize;
163 dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
164
165 #if 0
166 unionread:
167 #endif
168 off = 0;
169 do {
170 /* call VOP_READDIR of parent */
171 iov.iov_base = dirbuf;
172 iov.iov_len = dirbuflen;
173
174 uio.uio_iov = &iov;
175 uio.uio_iovcnt = 1;
176 uio.uio_offset = off;
177 uio.uio_resid = dirbuflen;
178 uio.uio_segflg = UIO_SYSSPACE;
179 uio.uio_rw = UIO_READ;
180 uio.uio_procp = p;
181
182 eofflag = 0;
183
184 error = VOP_READDIR(pvp, &uio, p->p_ucred, &eofflag, 0, 0);
185
186 off = uio.uio_offset;
187
188 /*
189 * Try again if NFS tosses its cookies.
190 * XXX this can still loop forever if the directory is busted
191 * such that the second or subsequent page of it always
192 * returns EINVAL
193 */
194 if ((error == EINVAL) && (tries < 3)) {
195 off = 0;
196 tries++;
197 continue; /* once more, with feeling */
198 }
199
200 if (!error) {
201 char *cpos;
202 struct dirent *dp;
203
204 cpos = dirbuf;
205 tries = 0;
206
207 /* scan directory page looking for matching vnode */
208 for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) {
209 dp = (struct dirent *) cpos;
210 reclen = dp->d_reclen;
211
212 /* check for malformed directory.. */
213 if (reclen < DIRENT_MINSIZE) {
214 error = EINVAL;
215 goto out;
216 }
217 /*
218 * XXX should perhaps do VOP_LOOKUP to
219 * check that we got back to the right place,
220 * but getting the locking games for that
221 * right would be heinous.
222 */
223 if ((dp->d_type != DT_WHT) &&
224 (dp->d_fileno == fileno)) {
225 char *bp = *bpp;
226 bp -= dp->d_namlen;
227
228 if (bp <= bufp) {
229 error = ERANGE;
230 goto out;
231 }
232 memcpy(bp, dp->d_name, dp->d_namlen);
233 error = 0;
234 *bpp = bp;
235 goto out;
236 }
237 cpos += reclen;
238 }
239 }
240 } while (!eofflag);
241 #if 0
242 /*
243 * Deal with mount -o union, which unions only the
244 * root directory of the mount.
245 */
246 if ((pvp->v_flag & VROOT) &&
247 (pvp->v_mount->mnt_flag & MNT_UNION)) {
248 struct vnode *tvp = pvp;
249 pvp = pvp->v_mount->mnt_vnodecovered;
250 vput(tvp);
251 VREF(pvp);
252 *pvpp = pvp;
253 error = vn_lock(pvp, LK_EXCLUSIVE | LK_RETRY);
254 if (error != 0) {
255 vrele(pvp);
256 *pvpp = pvp = NULL;
257 goto out;
258 }
259 goto unionread;
260 }
261 #endif
262 error = ENOENT;
263
264 out:
265 vrele(cvp);
266 *cvpp = NULL;
267 free(dirbuf, M_TEMP);
268 return error;
269 }
270
271 /*
272 * Look in the vnode-to-name reverse cache to see if
273 * we can find things the easy way.
274 *
275 * XXX vget failure path is untested.
276 *
277 * On entry, *vpp is a locked vnode reference.
278 * On exit, one of the following is the case:
279 * 0) Both *vpp and *vpp are NULL and failure is returned.
280 * 1) *dvpp is NULL, *vpp remains locked and -1 is returned (cache miss)
281 * 2) *dvpp is a locked vnode reference, *vpp is vput and NULL'ed
282 * and 0 is returned (cache hit)
283 */
284
285 static int
286 getcwd_getcache(vpp, dvpp, bpp, bufp)
287 struct vnode **vpp, **dvpp;
288 char **bpp;
289 char *bufp;
290 {
291 struct vnode *cvp, *pvp = NULL;
292 int error;
293 int vpid;
294
295 cvp = *vpp;
296
297 /*
298 * This returns 0 on a cache hit, -1 on a clean cache miss,
299 * or an errno on other failure.
300 */
301 error = cache_revlookup(cvp, dvpp, bpp, bufp);
302 if (error) {
303 if (error != -1) {
304 vput(cvp);
305 *vpp = NULL;
306 *dvpp = NULL;
307 }
308 return error;
309 }
310 pvp = *dvpp;
311 vpid = pvp->v_id;
312
313 /*
314 * Since we're going up, we have to release the current lock
315 * before we take the parent lock.
316 */
317
318 VOP_UNLOCK(cvp, 0);
319
320 error = vget(pvp, LK_EXCLUSIVE | LK_RETRY);
321 if (error != 0)
322 *dvpp = NULL;
323 /*
324 * Check that vnode capability didn't change while we were waiting
325 * for the lock.
326 */
327 if (error || (vpid != pvp->v_id)) {
328 /*
329 * oops, it did. do this the hard way.
330 */
331 if (!error) vput(pvp);
332 error = vn_lock(cvp, LK_EXCLUSIVE | LK_RETRY);
333 *dvpp = NULL;
334 return -1;
335 }
336 vrele(cvp);
337 *vpp = NULL;
338
339 return 0;
340 }
341
342 /*
343 * common routine shared by sys___getcwd() and vn_isunder()
344 */
345
346 #define GETCWD_CHECK_ACCESS 0x0001
347
348 static int getcwd_common (dvp, rvp, bpp, bufp, limit, flags, p)
349 struct vnode *dvp;
350 struct vnode *rvp;
351 char **bpp;
352 char *bufp;
353 int limit;
354 int flags;
355 struct proc *p;
356 {
357 struct cwdinfo *cwdi = p->p_cwdi;
358 struct vnode *pvp = NULL;
359 char *bp = NULL;
360 int error;
361
362 if (rvp == NULL) {
363 rvp = cwdi->cwdi_rdir;
364 if (rvp == NULL)
365 rvp = rootvnode;
366 }
367
368 VREF(rvp);
369 VREF(dvp);
370
371 /*
372 * Error handling invariant:
373 * Before a `goto out':
374 * dvp is either NULL, or locked and held.
375 * pvp is either NULL, or locked and held.
376 */
377
378 error = vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
379 if (error) {
380 vrele(dvp);
381 dvp = NULL;
382 goto out;
383 }
384 if (bufp)
385 bp = *bpp;
386 /*
387 * this loop will terminate when one of the following happens:
388 * - we hit the root
389 * - getdirentries or lookup fails
390 * - we run out of space in the buffer.
391 */
392 if (dvp == rvp) {
393 if (bp)
394 *(--bp) = '/';
395 goto out;
396 }
397 do {
398 if (dvp->v_type != VDIR) {
399 error = ENOTDIR;
400 goto out;
401 }
402
403 /*
404 * access check here is optional, depending on
405 * whether or not caller cares.
406 */
407 if (flags & GETCWD_CHECK_ACCESS) {
408 error = VOP_ACCESS(dvp, VEXEC|VREAD, p->p_ucred, p);
409 if (error)
410 goto out;
411 }
412
413 /*
414 * step up if we're a covered vnode..
415 */
416 while (dvp->v_flag & VROOT) {
417 struct vnode *tvp;
418
419 if (dvp == rvp)
420 goto out;
421
422 tvp = dvp;
423 dvp = dvp->v_mount->mnt_vnodecovered;
424 vput(tvp);
425 /*
426 * hodie natus est radici frater
427 */
428 if (dvp == NULL) {
429 error = ENOENT;
430 goto out;
431 }
432 VREF(dvp);
433 error = vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
434 if (error != 0) {
435 vrele(dvp);
436 dvp = NULL;
437 goto out;
438 }
439 }
440 /*
441 * Look in the name cache; if that fails, look in the
442 * directory..
443 */
444 error = getcwd_getcache(&dvp, &pvp, &bp, bufp);
445 if (error == -1)
446 error = getcwd_scandir(&dvp, &pvp, &bp, bufp, p);
447 if (error)
448 goto out;
449 #if DIAGNOSTIC
450 if (dvp != NULL)
451 panic("getcwd: oops, forgot to null dvp");
452 if (bufp && (bp <= bufp)) {
453 panic("getcwd: oops, went back too far");
454 }
455 #endif
456 if (bp)
457 *(--bp) = '/';
458 dvp = pvp;
459 pvp = NULL;
460 limit--;
461 } while ((dvp != rvp) && (limit > 0));
462
463 out:
464 if (bpp)
465 *bpp = bp;
466 if (pvp)
467 vput(pvp);
468 if (dvp)
469 vput(dvp);
470 vrele(rvp);
471 return error;
472 }
473
474 /*
475 * Check if one directory can be found inside another in the directory
476 * hierarchy.
477 *
478 * Intended to be used in chroot, chdir, fchdir, etc., to ensure that
479 * chroot() actually means something.
480 */
481 int vn_isunder(dvp, rvp, p)
482 struct vnode *dvp;
483 struct vnode *rvp;
484 struct proc *p;
485 {
486 int error;
487
488 error = getcwd_common (dvp, rvp, NULL, NULL, MAXPATHLEN/2, 0, p);
489
490 if (!error)
491 return 1;
492 else
493 return 0;
494 }
495
496 /*
497 * Returns true if proc p1's root directory equal to or under p2's
498 * root directory.
499 *
500 * Intended to be used from ptrace/procfs sorts of things.
501 */
502
503 int proc_isunder (p1, p2)
504 struct proc *p1;
505 struct proc *p2;
506 {
507 struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
508 struct vnode *r2 = p2->p_cwdi->cwdi_rdir;
509
510 if (r1 == NULL)
511 return (r2 == NULL);
512 else if (r2 == NULL)
513 return 1;
514 else
515 return vn_isunder(r1, r2, p2);
516 }
517
518 int sys___getcwd(p, v, retval)
519 struct proc *p;
520 void *v;
521 register_t *retval;
522 {
523 register struct sys___getcwd_args /* {
524 syscallarg(char *) bufp;
525 syscallarg(size_t) length;
526 } */ *uap = v;
527
528 int error;
529 char *path;
530 char *bp, *bend;
531 int len = SCARG(uap, length);
532 int lenused;
533
534 if (len > MAXPATHLEN*4)
535 len = MAXPATHLEN*4;
536 else if (len < 2)
537 return ERANGE;
538
539 path = (char *)malloc(len, M_TEMP, M_WAITOK);
540 if (!path)
541 return ENOMEM;
542
543 bp = &path[len];
544 bend = bp;
545 *(--bp) = '\0';
546
547 /*
548 * 5th argument here is "max number of vnodes to traverse".
549 * Since each entry takes up at least 2 bytes in the output buffer,
550 * limit it to N/2 vnodes for an N byte buffer.
551 */
552 error = getcwd_common (p->p_cwdi->cwdi_cdir, NULL, &bp, path, len/2,
553 GETCWD_CHECK_ACCESS, p);
554
555 if (error)
556 goto out;
557 lenused = bend - bp;
558 *retval = lenused;
559 /* put the result into user buffer */
560 error = copyout(bp, SCARG(uap, bufp), lenused);
561
562 out:
563 free(path, M_TEMP);
564 return error;
565 }
566
567
568
569 /*
570 * Find pathname of process's current directory.
571 *
572 * Use vfs vnode-to-name reverse cache; if that fails, fall back
573 * to reading directory contents.
574 */
575
576 /*
577 * XXX Untested vs. mount -o union; probably does the wrong thing.
578 * XXX Untested vs chroot
579 * XXX most error paths probably work, but many locking-related ones
580 * aren't tested well.
581 */
582 #if 0
583
584 int
585 sys___getcwd(p, v, retval)
586 struct proc *p;
587 void *v;
588 register_t *retval;
589 {
590 register struct sys___getcwd_args /* {
591 syscallarg(char *) bufp;
592 syscallarg(size_t) length;
593 } */ *uap = v;
594
595 struct cwdinfo *cwdi = p->p_cwdi;
596 struct vnode *cvp = NULL, *pvp = NULL, *rootvp = NULL;
597 int error;
598 char *path;
599 char *bp, *bend;
600 int len = SCARG(uap, length);
601 int lenused;
602
603 if ((len < 2) || (len > MAXPATHLEN*4))
604 return ERANGE;
605
606 path = (char *)malloc(len, M_TEMP, M_WAITOK);
607 if (!path)
608 return ENOMEM;
609
610 bp = &path[len];
611 bend = bp;
612 *(--bp) = '\0';
613
614 rootvp = cwdi->cwdi_rdir;
615 if (rootvp == NULL)
616 rootvp = rootvnode;
617
618 cvp = cwdi->cwdi_cdir;
619
620 VREF(rootvp);
621 VREF(cvp);
622
623 /*
624 * Error handling invariant:
625 * Before a `goto out':
626 * cvp is either NULL, or locked and held.
627 * pvp is either NULL, or locked and held.
628 */
629
630 error = vn_lock(cvp, LK_EXCLUSIVE | LK_RETRY);
631 if (error) {
632 vrele(cvp);
633 cvp = NULL;
634 goto out;
635 }
636 /*
637 * this loop will terminate when one of the following happens:
638 * - we hit the root
639 * - getdirentries or lookup fails
640 * - we run out of space in the buffer.
641 */
642 if (cvp == rootvp) {
643 *(--bp) = '/';
644 goto hitroot;
645 }
646 do {
647 /*
648 * so, are we even allowed to look at this directory?
649 */
650
651 error = VOP_ACCESS(cvp, VEXEC|VREAD, p->p_ucred, p);
652 if (error)
653 goto out;
654
655 /*
656 * step up if we're a covered vnode..
657 */
658 while (cvp->v_flag & VROOT) {
659 struct vnode *tvp;
660
661 if (cvp == rootvp)
662 goto hitroot;
663
664 tvp = cvp;
665 cvp = cvp->v_mount->mnt_vnodecovered;
666 vput(tvp);
667 VREF(cvp);
668 error = vn_lock(cvp, LK_EXCLUSIVE | LK_RETRY);
669 if (error != 0) {
670 vrele(cvp);
671 cvp = NULL;
672 goto out;
673 }
674 }
675 /*
676 * Look in the name cache; if that fails, look in the directory..
677 */
678 error = getcwd_getcache(&cvp, &pvp, &bp, path);
679 if (error == -1)
680 error = getcwd_scandir(cvp, &pvp, &bp, path, p);
681
682 if (error)
683 goto out;
684 if (bp <= path) {
685 error = ERANGE;
686 goto out;
687 }
688 *(--bp) = '/';
689
690 vput(cvp);
691 cvp = pvp;
692 pvp = NULL;
693
694 } while (cvp != rootvp);
695 hitroot:
696
697 lenused = bend - bp;
698 *retval = lenused;
699 /* put the result into user buffer */
700 error = copyout(bp, SCARG(uap, bufp), lenused);
701
702 out:
703 if (pvp)
704 vput(pvp);
705 if (cvp)
706 vput(cvp);
707 vrele(rootvp);
708 free(path, M_TEMP);
709 return error;
710 }
711 #endif
712