Home | History | Annotate | Line # | Download | only in kern
      1  1.63  riastrad /*	$NetBSD: vfs_getcwd.c,v 1.63 2024/12/07 02:27:38 riastradh Exp $	*/
      2   1.1  sommerfe 
      3   1.1  sommerfe /*-
      4  1.57        ad  * Copyright (c) 1999, 2020 The NetBSD Foundation, Inc.
      5   1.1  sommerfe  * All rights reserved.
      6   1.1  sommerfe  *
      7   1.1  sommerfe  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  sommerfe  * by Bill Sommerfeld.
      9   1.1  sommerfe  *
     10   1.1  sommerfe  * Redistribution and use in source and binary forms, with or without
     11   1.1  sommerfe  * modification, are permitted provided that the following conditions
     12   1.1  sommerfe  * are met:
     13   1.1  sommerfe  * 1. Redistributions of source code must retain the above copyright
     14   1.1  sommerfe  *    notice, this list of conditions and the following disclaimer.
     15   1.1  sommerfe  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  sommerfe  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  sommerfe  *    documentation and/or other materials provided with the distribution.
     18   1.1  sommerfe  *
     19   1.1  sommerfe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  sommerfe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  sommerfe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  sommerfe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  sommerfe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  sommerfe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  sommerfe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  sommerfe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  sommerfe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  sommerfe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  sommerfe  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  sommerfe  */
     31  1.15     lukem 
     32  1.15     lukem #include <sys/cdefs.h>
     33  1.63  riastrad __KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.63 2024/12/07 02:27:38 riastradh Exp $");
     34   1.1  sommerfe 
     35   1.1  sommerfe #include <sys/param.h>
     36  1.62  riastrad #include <sys/types.h>
     37  1.62  riastrad 
     38  1.62  riastrad #include <sys/dirent.h>
     39  1.62  riastrad #include <sys/file.h>
     40   1.1  sommerfe #include <sys/filedesc.h>
     41  1.62  riastrad #include <sys/kauth.h>
     42   1.1  sommerfe #include <sys/kernel.h>
     43  1.62  riastrad #include <sys/kmem.h>
     44   1.1  sommerfe #include <sys/mount.h>
     45  1.62  riastrad #include <sys/namei.h>
     46   1.1  sommerfe #include <sys/proc.h>
     47  1.63  riastrad #include <sys/sdt.h>
     48  1.62  riastrad #include <sys/stat.h>
     49  1.62  riastrad #include <sys/syscallargs.h>
     50  1.62  riastrad #include <sys/systm.h>
     51   1.1  sommerfe #include <sys/uio.h>
     52  1.62  riastrad #include <sys/vnode.h>
     53  1.31      elad 
     54   1.1  sommerfe #include <ufs/ufs/dir.h>	/* XXX only for DIRBLKSIZ */
     55   1.1  sommerfe 
     56   1.1  sommerfe 
     57   1.1  sommerfe /*
     58   1.9  sommerfe  * Vnode variable naming conventions in this file:
     59   1.9  sommerfe  *
     60   1.9  sommerfe  * rvp: the current root we're aiming towards.
     61   1.9  sommerfe  * lvp, *lvpp: the "lower" vnode
     62   1.9  sommerfe  * uvp, *uvpp: the "upper" vnode.
     63   1.9  sommerfe  *
     64   1.9  sommerfe  * Since all the vnodes we're dealing with are directories, and the
     65   1.9  sommerfe  * lookups are going *up* in the filesystem rather than *down*, the
     66   1.9  sommerfe  * usual "pvp" (parent) or "dvp" (directory) naming conventions are
     67   1.9  sommerfe  * too confusing.
     68   1.9  sommerfe  */
     69   1.9  sommerfe 
     70   1.9  sommerfe /*
     71   1.1  sommerfe  * XXX Will infinite loop in certain cases if a directory read reliably
     72   1.1  sommerfe  *	returns EINVAL on last block.
     73   1.1  sommerfe  * XXX is EINVAL the right thing to return if a directory is malformed?
     74   1.1  sommerfe  */
     75   1.1  sommerfe 
     76   1.1  sommerfe /*
     77   1.9  sommerfe  * XXX Untested vs. mount -o union; probably does the wrong thing.
     78   1.9  sommerfe  */
     79   1.9  sommerfe 
     80   1.9  sommerfe /*
     81   1.9  sommerfe  * Find parent vnode of *lvpp, return in *uvpp
     82   1.9  sommerfe  *
     83   1.9  sommerfe  * If we care about the name, scan it looking for name of directory
     84   1.9  sommerfe  * entry pointing at lvp.
     85   1.1  sommerfe  *
     86   1.1  sommerfe  * Place the name in the buffer which starts at bufp, immediately
     87   1.1  sommerfe  * before *bpp, and move bpp backwards to point at the start of it.
     88   1.8  sommerfe  *
     89   1.9  sommerfe  * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
     90   1.9  sommerfe  * On exit, *uvpp is either NULL or is a locked vnode reference.
     91   1.1  sommerfe  */
     92   1.1  sommerfe static int
     93  1.57        ad getcwd_scandir(struct vnode *lvp, struct vnode **uvpp, char **bpp,
     94  1.29  christos     char *bufp, struct lwp *l)
     95   1.1  sommerfe {
     96   1.1  sommerfe 	int     error = 0;
     97   1.1  sommerfe 	int     eofflag;
     98   1.1  sommerfe 	off_t   off;
     99   1.1  sommerfe 	int     tries;
    100   1.1  sommerfe 	struct uio uio;
    101   1.1  sommerfe 	struct iovec iov;
    102   1.1  sommerfe 	char   *dirbuf = NULL;
    103   1.1  sommerfe 	int	dirbuflen;
    104   1.1  sommerfe 	ino_t   fileno;
    105   1.1  sommerfe 	struct vattr va;
    106   1.9  sommerfe 	struct vnode *uvp = NULL;
    107  1.32        ad 	kauth_cred_t cred = l->l_cred;
    108   1.1  sommerfe 	struct componentname cn;
    109   1.1  sommerfe 	int len, reclen;
    110   1.1  sommerfe 	tries = 0;
    111   1.1  sommerfe 
    112  1.57        ad 	/* Need exclusive for UFS VOP_GETATTR (itimes) & VOP_LOOKUP. */
    113  1.57        ad 	KASSERT(VOP_ISLOCKED(lvp) == LK_EXCLUSIVE);
    114  1.57        ad 
    115   1.1  sommerfe 	/*
    116   1.8  sommerfe 	 * If we want the filename, get some info we need while the
    117   1.8  sommerfe 	 * current directory is still locked.
    118   1.8  sommerfe 	 */
    119   1.8  sommerfe 	if (bufp != NULL) {
    120  1.38     pooka 		error = VOP_GETATTR(lvp, &va, cred);
    121   1.8  sommerfe 		if (error) {
    122  1.57        ad 			VOP_UNLOCK(lvp);
    123   1.9  sommerfe 			*uvpp = NULL;
    124   1.8  sommerfe 			return error;
    125   1.8  sommerfe 		}
    126   1.8  sommerfe 	}
    127   1.8  sommerfe 
    128   1.8  sommerfe 	/*
    129   1.1  sommerfe 	 * Ok, we have to do it the hard way..
    130   1.8  sommerfe 	 * Next, get parent vnode using lookup of ..
    131   1.1  sommerfe 	 */
    132   1.1  sommerfe 	cn.cn_nameiop = LOOKUP;
    133   1.8  sommerfe 	cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
    134  1.31      elad 	cn.cn_cred = cred;
    135   1.1  sommerfe 	cn.cn_nameptr = "..";
    136   1.1  sommerfe 	cn.cn_namelen = 2;
    137  1.24     enami 
    138  1.50   hannken 	/* At this point, lvp is locked  */
    139   1.9  sommerfe 	error = VOP_LOOKUP(lvp, uvpp, &cn);
    140  1.57        ad 	VOP_UNLOCK(lvp);
    141   1.1  sommerfe 	if (error) {
    142   1.9  sommerfe 		*uvpp = NULL;
    143   1.1  sommerfe 		return error;
    144   1.1  sommerfe 	}
    145   1.9  sommerfe 	uvp = *uvpp;
    146   1.1  sommerfe 	/* If we don't care about the pathname, we're done */
    147   1.8  sommerfe 	if (bufp == NULL) {
    148   1.1  sommerfe 		return 0;
    149   1.8  sommerfe 	}
    150  1.24     enami 
    151   1.1  sommerfe 	fileno = va.va_fileid;
    152   1.1  sommerfe 
    153  1.49  dholland 	/* I guess UFS_DIRBLKSIZ is a good guess at a good size to use? */
    154  1.49  dholland 	dirbuflen = UFS_DIRBLKSIZ;
    155   1.1  sommerfe 	if (dirbuflen < va.va_blocksize)
    156   1.1  sommerfe 		dirbuflen = va.va_blocksize;
    157  1.43      yamt 	dirbuf = kmem_alloc(dirbuflen, KM_SLEEP);
    158   1.1  sommerfe 
    159  1.57        ad 	/* Now lvp is unlocked, try to lock uvp */
    160  1.57        ad 	error = vn_lock(uvp, LK_SHARED);
    161  1.57        ad 	if (error) {
    162  1.57        ad 		vrele(uvp);
    163  1.57        ad 		*uvpp = NULL;
    164  1.57        ad 		return error;
    165  1.57        ad 	}
    166  1.57        ad 
    167   1.1  sommerfe #if 0
    168   1.1  sommerfe unionread:
    169   1.1  sommerfe #endif
    170   1.1  sommerfe 	off = 0;
    171   1.1  sommerfe 	do {
    172   1.1  sommerfe 		/* call VOP_READDIR of parent */
    173   1.1  sommerfe 		iov.iov_base = dirbuf;
    174   1.1  sommerfe 		iov.iov_len = dirbuflen;
    175   1.1  sommerfe 
    176   1.1  sommerfe 		uio.uio_iov = &iov;
    177   1.1  sommerfe 		uio.uio_iovcnt = 1;
    178   1.1  sommerfe 		uio.uio_offset = off;
    179   1.1  sommerfe 		uio.uio_resid = dirbuflen;
    180   1.1  sommerfe 		uio.uio_rw = UIO_READ;
    181  1.30      yamt 		UIO_SETUP_SYSSPACE(&uio);
    182   1.1  sommerfe 
    183   1.1  sommerfe 		eofflag = 0;
    184   1.1  sommerfe 
    185  1.31      elad 		error = VOP_READDIR(uvp, &uio, cred, &eofflag, 0, 0);
    186   1.1  sommerfe 
    187   1.1  sommerfe 		off = uio.uio_offset;
    188   1.1  sommerfe 
    189   1.1  sommerfe 		/*
    190   1.1  sommerfe 		 * Try again if NFS tosses its cookies.
    191   1.1  sommerfe 		 * XXX this can still loop forever if the directory is busted
    192   1.1  sommerfe 		 * such that the second or subsequent page of it always
    193   1.1  sommerfe 		 * returns EINVAL
    194   1.1  sommerfe 		 */
    195   1.1  sommerfe 		if ((error == EINVAL) && (tries < 3)) {
    196   1.1  sommerfe 			off = 0;
    197   1.1  sommerfe 			tries++;
    198   1.1  sommerfe 			continue;	/* once more, with feeling */
    199   1.1  sommerfe 		}
    200   1.2   nathanw 
    201   1.1  sommerfe 		if (!error) {
    202   1.1  sommerfe 			char   *cpos;
    203   1.1  sommerfe 			struct dirent *dp;
    204  1.24     enami 
    205   1.1  sommerfe 			cpos = dirbuf;
    206   1.1  sommerfe 			tries = 0;
    207  1.24     enami 
    208  1.24     enami 			/* scan directory page looking for matching vnode */
    209  1.24     enami 			for (len = (dirbuflen - uio.uio_resid); len > 0;
    210  1.24     enami 			    len -= reclen) {
    211   1.1  sommerfe 				dp = (struct dirent *) cpos;
    212   1.1  sommerfe 				reclen = dp->d_reclen;
    213   1.1  sommerfe 
    214   1.1  sommerfe 				/* check for malformed directory.. */
    215  1.52  riastrad 				if (reclen < _DIRENT_MINSIZE(dp) ||
    216  1.52  riastrad 				    reclen > len) {
    217  1.63  riastrad 					error = SET_ERROR(EINVAL);
    218   1.1  sommerfe 					goto out;
    219   1.1  sommerfe 				}
    220   1.1  sommerfe 				/*
    221   1.1  sommerfe 				 * XXX should perhaps do VOP_LOOKUP to
    222   1.1  sommerfe 				 * check that we got back to the right place,
    223   1.1  sommerfe 				 * but getting the locking games for that
    224   1.1  sommerfe 				 * right would be heinous.
    225   1.1  sommerfe 				 */
    226   1.1  sommerfe 				if ((dp->d_type != DT_WHT) &&
    227   1.1  sommerfe 				    (dp->d_fileno == fileno)) {
    228   1.1  sommerfe 					char *bp = *bpp;
    229  1.24     enami 
    230   1.1  sommerfe 					bp -= dp->d_namlen;
    231   1.1  sommerfe 					if (bp <= bufp) {
    232  1.63  riastrad 						error = SET_ERROR(ERANGE);
    233   1.1  sommerfe 						goto out;
    234   1.1  sommerfe 					}
    235   1.1  sommerfe 					memcpy(bp, dp->d_name, dp->d_namlen);
    236   1.1  sommerfe 					error = 0;
    237   1.1  sommerfe 					*bpp = bp;
    238   1.1  sommerfe 					goto out;
    239   1.1  sommerfe 				}
    240   1.1  sommerfe 				cpos += reclen;
    241   1.1  sommerfe 			}
    242  1.14      fvdl 		} else
    243  1.14      fvdl 			goto out;
    244   1.2   nathanw 	} while (!eofflag);
    245   1.1  sommerfe #if 0
    246   1.1  sommerfe 	/*
    247   1.1  sommerfe 	 * Deal with mount -o union, which unions only the
    248   1.1  sommerfe 	 * root directory of the mount.
    249   1.1  sommerfe 	 */
    250  1.37        ad 	if ((uvp->v_vflag & VV_ROOT) &&
    251   1.9  sommerfe 	    (uvp->v_mount->mnt_flag & MNT_UNION)) {
    252   1.9  sommerfe 		struct vnode *tvp = uvp;
    253  1.24     enami 
    254   1.9  sommerfe 		uvp = uvp->v_mount->mnt_vnodecovered;
    255   1.1  sommerfe 		vput(tvp);
    256  1.44     pooka 		vref(uvp);
    257   1.9  sommerfe 		*uvpp = uvp;
    258  1.57        ad 		vn_lock(uvp, LK_SHARED | LK_RETRY);
    259   1.1  sommerfe 		goto unionread;
    260   1.1  sommerfe 	}
    261  1.24     enami #endif
    262  1.63  riastrad 	error = SET_ERROR(ENOENT);
    263  1.24     enami 
    264   1.1  sommerfe out:
    265  1.57        ad 	VOP_UNLOCK(uvp);
    266  1.43      yamt 	kmem_free(dirbuf, dirbuflen);
    267   1.1  sommerfe 	return error;
    268   1.1  sommerfe }
    269   1.1  sommerfe 
    270   1.1  sommerfe /*
    271   1.1  sommerfe  * common routine shared by sys___getcwd() and vn_isunder()
    272   1.1  sommerfe  */
    273  1.24     enami int
    274  1.27   thorpej getcwd_common(struct vnode *lvp, struct vnode *rvp, char **bpp, char *bufp,
    275  1.29  christos     int limit, int flags, struct lwp *l)
    276   1.1  sommerfe {
    277  1.29  christos 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
    278  1.32        ad 	kauth_cred_t cred = l->l_cred;
    279   1.9  sommerfe 	struct vnode *uvp = NULL;
    280   1.4  sommerfe 	char *bp = NULL;
    281   1.1  sommerfe 	int error;
    282  1.60  christos 	accmode_t accmode = VEXEC;
    283  1.10  sommerfe 
    284  1.34       chs 	error = 0;
    285   1.1  sommerfe 	if (rvp == NULL) {
    286   1.6   thorpej 		rvp = cwdi->cwdi_rdir;
    287   1.1  sommerfe 		if (rvp == NULL)
    288   1.1  sommerfe 			rvp = rootvnode;
    289   1.1  sommerfe 	}
    290  1.24     enami 
    291  1.44     pooka 	vref(rvp);
    292  1.44     pooka 	vref(lvp);
    293   1.1  sommerfe 
    294   1.1  sommerfe 	/*
    295   1.1  sommerfe 	 * Error handling invariant:
    296   1.1  sommerfe 	 * Before a `goto out':
    297  1.57        ad 	 *	lvp is either NULL, or held.
    298  1.57        ad 	 *	uvp is either NULL, or held.
    299   1.1  sommerfe 	 */
    300   1.1  sommerfe 
    301   1.1  sommerfe 	if (bufp)
    302   1.1  sommerfe 		bp = *bpp;
    303  1.34       chs 
    304   1.1  sommerfe 	/*
    305   1.1  sommerfe 	 * this loop will terminate when one of the following happens:
    306   1.1  sommerfe 	 *	- we hit the root
    307   1.1  sommerfe 	 *	- getdirentries or lookup fails
    308   1.1  sommerfe 	 *	- we run out of space in the buffer.
    309   1.1  sommerfe 	 */
    310   1.9  sommerfe 	if (lvp == rvp) {
    311   1.8  sommerfe 		if (bp)
    312   1.8  sommerfe 			*(--bp) = '/';
    313   1.1  sommerfe 		goto out;
    314   1.1  sommerfe 	}
    315   1.1  sommerfe 	do {
    316   1.1  sommerfe 		/*
    317   1.1  sommerfe 		 * access check here is optional, depending on
    318   1.1  sommerfe 		 * whether or not caller cares.
    319   1.1  sommerfe 		 */
    320  1.57        ad 		int chkaccess = (flags & GETCWD_CHECK_ACCESS);
    321  1.57        ad 		bool locked = false;
    322  1.24     enami 
    323   1.1  sommerfe 		/*
    324   1.1  sommerfe 		 * step up if we're a covered vnode..
    325  1.57        ad 		 * check access on the first vnode only.
    326   1.1  sommerfe 		 */
    327  1.57        ad 		if (lvp->v_vflag & VV_ROOT) {
    328  1.57        ad 			vn_lock(lvp, LK_SHARED | LK_RETRY);
    329  1.57        ad 			if (chkaccess) {
    330  1.60  christos 				error = VOP_ACCESS(lvp, accmode, cred);
    331  1.57        ad 				if (error) {
    332  1.57        ad 					VOP_UNLOCK(lvp);
    333  1.57        ad 					goto out;
    334  1.57        ad 				}
    335  1.57        ad 				chkaccess = 0;
    336  1.57        ad 			}
    337  1.57        ad 			while (lvp->v_vflag & VV_ROOT) {
    338  1.57        ad 				struct vnode *tvp;
    339   1.1  sommerfe 
    340  1.57        ad 				if (lvp == rvp) {
    341  1.57        ad 					VOP_UNLOCK(lvp);
    342  1.57        ad 					goto out;
    343  1.57        ad 				}
    344  1.24     enami 
    345  1.57        ad 				tvp = lvp->v_mount->mnt_vnodecovered;
    346  1.57        ad 				/*
    347  1.57        ad 				 * hodie natus est radici frater
    348  1.57        ad 				 */
    349  1.57        ad 				if (tvp == NULL) {
    350  1.57        ad 					VOP_UNLOCK(lvp);
    351  1.63  riastrad 					error = SET_ERROR(ENOENT);
    352  1.57        ad 					goto out;
    353  1.57        ad 				}
    354  1.57        ad 				vref(tvp);
    355  1.57        ad 				vput(lvp);
    356  1.57        ad 				lvp = tvp;
    357  1.57        ad 				if (lvp->v_vflag & VV_ROOT)
    358  1.57        ad 					vn_lock(lvp, LK_SHARED | LK_RETRY);
    359   1.1  sommerfe 			}
    360  1.57        ad 		}
    361  1.57        ad 
    362  1.57        ad 		/* Do we need to check access to the directory? */
    363  1.57        ad 		if (chkaccess && !cache_have_id(lvp)) {
    364  1.62  riastrad 			/*
    365  1.62  riastrad 			 * Need exclusive for UFS VOP_GETATTR (itimes)
    366  1.62  riastrad 			 * & VOP_LOOKUP.
    367  1.62  riastrad 			 */
    368  1.57        ad 			vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    369  1.60  christos 			error = VOP_ACCESS(lvp, accmode, cred);
    370  1.57        ad 			if (error) {
    371  1.57        ad 				VOP_UNLOCK(lvp);
    372   1.1  sommerfe 				goto out;
    373   1.1  sommerfe 			}
    374  1.57        ad 			chkaccess = 0;
    375  1.57        ad 			locked = true;
    376   1.1  sommerfe 		}
    377  1.57        ad 
    378   1.1  sommerfe 		/*
    379   1.1  sommerfe 		 * Look in the name cache; if that fails, look in the
    380   1.1  sommerfe 		 * directory..
    381   1.1  sommerfe 		 */
    382  1.57        ad 		error = cache_revlookup(lvp, &uvp, &bp, bufp, chkaccess,
    383  1.60  christos 		    accmode);
    384  1.33  christos 		if (error == -1) {
    385  1.57        ad 			if (!locked) {
    386  1.57        ad 				locked = true;
    387  1.57        ad 				vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
    388  1.57        ad 			}
    389  1.33  christos 			if (lvp->v_type != VDIR) {
    390  1.57        ad 				VOP_UNLOCK(lvp);
    391  1.63  riastrad 				error = SET_ERROR(ENOTDIR);
    392  1.33  christos 				goto out;
    393  1.33  christos 			}
    394  1.57        ad 			error = getcwd_scandir(lvp, &uvp, &bp, bufp, l);
    395  1.57        ad 			/* lvp now unlocked */
    396  1.57        ad 		} else if (locked) {
    397  1.57        ad 			VOP_UNLOCK(lvp);
    398  1.33  christos 		}
    399   1.1  sommerfe 		if (error)
    400   1.1  sommerfe 			goto out;
    401  1.24     enami #if DIAGNOSTIC
    402   1.1  sommerfe 		if (bufp && (bp <= bufp)) {
    403   1.1  sommerfe 			panic("getcwd: oops, went back too far");
    404   1.1  sommerfe 		}
    405  1.24     enami #endif
    406  1.60  christos 		accmode = VEXEC | VREAD;
    407  1.24     enami 		if (bp)
    408   1.8  sommerfe 			*(--bp) = '/';
    409  1.57        ad 		vrele(lvp);
    410   1.9  sommerfe 		lvp = uvp;
    411   1.9  sommerfe 		uvp = NULL;
    412   1.1  sommerfe 		limit--;
    413  1.24     enami 	} while ((lvp != rvp) && (limit > 0));
    414   1.1  sommerfe 
    415   1.1  sommerfe out:
    416   1.8  sommerfe 	if (bpp)
    417   1.8  sommerfe 		*bpp = bp;
    418   1.9  sommerfe 	if (uvp)
    419  1.57        ad 		vrele(uvp);
    420   1.9  sommerfe 	if (lvp)
    421  1.57        ad 		vrele(lvp);
    422   1.1  sommerfe 	vrele(rvp);
    423   1.1  sommerfe 	return error;
    424   1.1  sommerfe }
    425   1.1  sommerfe 
    426   1.1  sommerfe /*
    427   1.1  sommerfe  * Check if one directory can be found inside another in the directory
    428   1.1  sommerfe  * hierarchy.
    429   1.1  sommerfe  *
    430   1.1  sommerfe  * Intended to be used in chroot, chdir, fchdir, etc., to ensure that
    431   1.1  sommerfe  * chroot() actually means something.
    432   1.1  sommerfe  */
    433  1.11  jdolecek int
    434  1.29  christos vn_isunder(struct vnode *lvp, struct vnode *rvp, struct lwp *l)
    435   1.1  sommerfe {
    436   1.1  sommerfe 	int error;
    437   1.1  sommerfe 
    438  1.29  christos 	error = getcwd_common(lvp, rvp, NULL, NULL, MAXPATHLEN / 2, 0, l);
    439   1.1  sommerfe 
    440   1.1  sommerfe 	if (!error)
    441   1.1  sommerfe 		return 1;
    442   1.1  sommerfe 	else
    443   1.1  sommerfe 		return 0;
    444   1.1  sommerfe }
    445   1.3  sommerfe 
    446   1.3  sommerfe /*
    447   1.3  sommerfe  * Returns true if proc p1's root directory equal to or under p2's
    448   1.3  sommerfe  * root directory.
    449   1.3  sommerfe  *
    450   1.3  sommerfe  * Intended to be used from ptrace/procfs sorts of things.
    451   1.3  sommerfe  */
    452   1.3  sommerfe 
    453  1.11  jdolecek int
    454  1.29  christos proc_isunder(struct proc *p1, struct lwp *l2)
    455   1.3  sommerfe {
    456   1.6   thorpej 	struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
    457  1.29  christos 	struct vnode *r2 = l2->l_proc->p_cwdi->cwdi_rdir;
    458   1.6   thorpej 
    459   1.3  sommerfe 	if (r1 == NULL)
    460   1.3  sommerfe 		return (r2 == NULL);
    461   1.3  sommerfe 	else if (r2 == NULL)
    462   1.3  sommerfe 		return 1;
    463   1.3  sommerfe 	else
    464  1.29  christos 		return vn_isunder(r1, r2, l2);
    465   1.3  sommerfe }
    466   1.3  sommerfe 
    467   1.9  sommerfe /*
    468   1.9  sommerfe  * Find pathname of process's current directory.
    469   1.9  sommerfe  *
    470   1.9  sommerfe  * Use vfs vnode-to-name reverse cache; if that fails, fall back
    471   1.9  sommerfe  * to reading directory contents.
    472   1.9  sommerfe  */
    473   1.9  sommerfe 
    474  1.11  jdolecek int
    475  1.62  riastrad sys___getcwd(struct lwp *l, const struct sys___getcwd_args *uap,
    476  1.62  riastrad     register_t *retval)
    477   1.1  sommerfe {
    478  1.41       dsl 	/* {
    479   1.1  sommerfe 		syscallarg(char *) bufp;
    480   1.1  sommerfe 		syscallarg(size_t) length;
    481  1.41       dsl 	} */
    482   1.1  sommerfe 
    483   1.1  sommerfe 	int     error;
    484   1.1  sommerfe 	char   *path;
    485   1.1  sommerfe 	char   *bp, *bend;
    486   1.1  sommerfe 	int     len = SCARG(uap, length);
    487   1.1  sommerfe 	int	lenused;
    488  1.59        ad 	struct	cwdinfo *cwdi;
    489   1.1  sommerfe 
    490  1.24     enami 	if (len > MAXPATHLEN * 4)
    491  1.24     enami 		len = MAXPATHLEN * 4;
    492   1.7  sommerfe 	else if (len < 2)
    493  1.63  riastrad 		return SET_ERROR(ERANGE);
    494   1.1  sommerfe 
    495  1.43      yamt 	path = kmem_alloc(len, KM_SLEEP);
    496   1.1  sommerfe 	bp = &path[len];
    497   1.1  sommerfe 	bend = bp;
    498   1.1  sommerfe 	*(--bp) = '\0';
    499   1.1  sommerfe 
    500   1.8  sommerfe 	/*
    501   1.8  sommerfe 	 * 5th argument here is "max number of vnodes to traverse".
    502   1.8  sommerfe 	 * Since each entry takes up at least 2 bytes in the output buffer,
    503   1.8  sommerfe 	 * limit it to N/2 vnodes for an N byte buffer.
    504   1.8  sommerfe 	 */
    505  1.59        ad 	cwdi = l->l_proc->p_cwdi;
    506  1.59        ad 	rw_enter(&cwdi->cwdi_lock, RW_READER);
    507  1.62  riastrad 	error = getcwd_common(cwdi->cwdi_cdir, NULL, &bp, path,
    508  1.29  christos 	    len/2, GETCWD_CHECK_ACCESS, l);
    509  1.59        ad 	rw_exit(&cwdi->cwdi_lock);
    510   1.1  sommerfe 
    511   1.1  sommerfe 	if (error)
    512   1.1  sommerfe 		goto out;
    513   1.1  sommerfe 	lenused = bend - bp;
    514   1.1  sommerfe 	*retval = lenused;
    515   1.1  sommerfe 	/* put the result into user buffer */
    516   1.1  sommerfe 	error = copyout(bp, SCARG(uap, bufp), lenused);
    517   1.1  sommerfe 
    518   1.1  sommerfe out:
    519  1.43      yamt 	kmem_free(path, len);
    520   1.1  sommerfe 	return error;
    521   1.1  sommerfe }
    522  1.39  christos 
    523  1.39  christos /*
    524  1.54        ad  * Try to find a pathname for a vnode.  Since there is no mapping vnode ->
    525  1.54        ad  * parent directory, this needs the namecache to succeed.  Caller holds a
    526  1.46   hannken  * reference to the vnode.
    527  1.39  christos  */
    528  1.39  christos int
    529  1.39  christos vnode_to_path(char *path, size_t len, struct vnode *vp, struct lwp *curl,
    530  1.39  christos     struct proc *p)
    531  1.39  christos {
    532  1.39  christos 	struct proc *curp = curl->l_proc;
    533  1.39  christos 	int error, lenused, elen;
    534  1.39  christos 	char *bp, *bend;
    535  1.39  christos 	struct vnode *dvp;
    536  1.39  christos 
    537  1.58        ad 	KASSERT(vrefcnt(vp) > 0);
    538  1.46   hannken 
    539  1.39  christos 	bp = bend = &path[len];
    540  1.39  christos 	*(--bp) = '\0';
    541  1.39  christos 
    542  1.57        ad 	error = cache_revlookup(vp, &dvp, &bp, path, false, 0);
    543  1.39  christos 	if (error != 0)
    544  1.63  riastrad 		return (error == -1 ? SET_ERROR(ENOENT) : error);
    545  1.39  christos 
    546  1.39  christos 	*(--bp) = '/';
    547  1.46   hannken 	error = getcwd_common(dvp, NULL, &bp, path, len / 2,
    548  1.46   hannken 	    GETCWD_CHECK_ACCESS, curl);
    549  1.46   hannken 	vrele(dvp);
    550  1.53  christos 	if (error != 0)
    551  1.53  christos 		return error;
    552  1.39  christos 
    553  1.39  christos 	/*
    554  1.39  christos 	 * Strip off emulation path for emulated processes looking at
    555  1.39  christos 	 * the maps file of a process of the same emulation. (Won't
    556  1.39  christos 	 * work if /emul/xxx is a symlink..)
    557  1.39  christos 	 */
    558  1.39  christos 	if (curp->p_emul == p->p_emul && curp->p_emul->e_path != NULL) {
    559  1.39  christos 		elen = strlen(curp->p_emul->e_path);
    560  1.39  christos 		if (!strncmp(bp, curp->p_emul->e_path, elen))
    561  1.39  christos 			bp = &bp[elen];
    562  1.39  christos 	}
    563  1.39  christos 
    564  1.39  christos 	lenused = bend - bp;
    565  1.39  christos 
    566  1.39  christos 	memcpy(path, bp, lenused);
    567  1.53  christos 	path[lenused] = '\0';
    568  1.39  christos 
    569  1.39  christos 	return 0;
    570  1.39  christos }
    571