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