2006-03-01 15:38:10 +03:00
|
|
|
/* $NetBSD: vfs_getcwd.c,v 1.30 2006/03/01 12:38:21 yamt Exp $ */
|
1999-03-22 20:01:55 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1999 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Bill Sommerfeld.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2006-03-01 15:38:10 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.30 2006/03/01 12:38:21 yamt Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/dirent.h>
|
|
|
|
#include <ufs/ufs/dir.h> /* XXX only for DIRBLKSIZ */
|
|
|
|
|
2003-01-18 13:06:22 +03:00
|
|
|
#include <sys/sa.h>
|
1999-03-22 20:01:55 +03:00
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
|
1999-07-05 00:16:57 +04:00
|
|
|
/*
|
|
|
|
* Vnode variable naming conventions in this file:
|
|
|
|
*
|
|
|
|
* rvp: the current root we're aiming towards.
|
|
|
|
* lvp, *lvpp: the "lower" vnode
|
|
|
|
* uvp, *uvpp: the "upper" vnode.
|
|
|
|
*
|
|
|
|
* Since all the vnodes we're dealing with are directories, and the
|
|
|
|
* lookups are going *up* in the filesystem rather than *down*, the
|
|
|
|
* usual "pvp" (parent) or "dvp" (directory) naming conventions are
|
|
|
|
* too confusing.
|
|
|
|
*/
|
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
/*
|
|
|
|
* XXX Will infinite loop in certain cases if a directory read reliably
|
|
|
|
* returns EINVAL on last block.
|
|
|
|
* XXX is EINVAL the right thing to return if a directory is malformed?
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1999-07-05 00:16:57 +04:00
|
|
|
* XXX Untested vs. mount -o union; probably does the wrong thing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find parent vnode of *lvpp, return in *uvpp
|
|
|
|
*
|
|
|
|
* If we care about the name, scan it looking for name of directory
|
|
|
|
* entry pointing at lvp.
|
1999-03-22 20:01:55 +03:00
|
|
|
*
|
|
|
|
* Place the name in the buffer which starts at bufp, immediately
|
|
|
|
* before *bpp, and move bpp backwards to point at the start of it.
|
1999-06-21 09:11:09 +04:00
|
|
|
*
|
1999-07-05 00:16:57 +04:00
|
|
|
* On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
|
|
|
|
* On exit, *uvpp is either NULL or is a locked vnode reference.
|
1999-03-22 20:01:55 +03:00
|
|
|
*/
|
|
|
|
static int
|
2005-06-06 03:47:48 +04:00
|
|
|
getcwd_scandir(struct vnode **lvpp, struct vnode **uvpp, char **bpp,
|
2005-12-11 15:16:03 +03:00
|
|
|
char *bufp, struct lwp *l)
|
1999-03-22 20:01:55 +03:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
int eofflag;
|
|
|
|
off_t off;
|
|
|
|
int tries;
|
|
|
|
struct uio uio;
|
|
|
|
struct iovec iov;
|
|
|
|
char *dirbuf = NULL;
|
|
|
|
int dirbuflen;
|
|
|
|
ino_t fileno;
|
|
|
|
struct vattr va;
|
1999-07-05 00:16:57 +04:00
|
|
|
struct vnode *uvp = NULL;
|
2004-02-17 04:45:34 +03:00
|
|
|
struct vnode *lvp = *lvpp;
|
2005-12-11 15:16:03 +03:00
|
|
|
struct ucred *ucred = l->l_proc->p_ucred;
|
1999-03-22 20:01:55 +03:00
|
|
|
struct componentname cn;
|
|
|
|
int len, reclen;
|
|
|
|
tries = 0;
|
|
|
|
|
1999-06-21 09:11:09 +04:00
|
|
|
/*
|
|
|
|
* If we want the filename, get some info we need while the
|
|
|
|
* current directory is still locked.
|
|
|
|
*/
|
|
|
|
if (bufp != NULL) {
|
2005-12-11 15:16:03 +03:00
|
|
|
error = VOP_GETATTR(lvp, &va, ucred, l);
|
1999-06-21 09:11:09 +04:00
|
|
|
if (error) {
|
1999-07-05 00:16:57 +04:00
|
|
|
vput(lvp);
|
|
|
|
*lvpp = NULL;
|
|
|
|
*uvpp = NULL;
|
1999-06-21 09:11:09 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
/*
|
|
|
|
* Ok, we have to do it the hard way..
|
1999-06-21 09:11:09 +04:00
|
|
|
* Next, get parent vnode using lookup of ..
|
1999-03-22 20:01:55 +03:00
|
|
|
*/
|
|
|
|
cn.cn_nameiop = LOOKUP;
|
1999-06-21 09:11:09 +04:00
|
|
|
cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
|
2005-12-11 15:16:03 +03:00
|
|
|
cn.cn_lwp = l;
|
|
|
|
cn.cn_cred = ucred;
|
1999-03-22 20:01:55 +03:00
|
|
|
cn.cn_pnbuf = NULL;
|
|
|
|
cn.cn_nameptr = "..";
|
|
|
|
cn.cn_namelen = 2;
|
|
|
|
cn.cn_hash = 0;
|
|
|
|
cn.cn_consume = 0;
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
/*
|
1999-07-05 00:16:57 +04:00
|
|
|
* At this point, lvp is locked and will be unlocked by the lookup.
|
|
|
|
* On successful return, *uvpp will be locked
|
1999-03-22 20:01:55 +03:00
|
|
|
*/
|
1999-07-05 00:16:57 +04:00
|
|
|
error = VOP_LOOKUP(lvp, uvpp, &cn);
|
1999-03-22 20:01:55 +03:00
|
|
|
if (error) {
|
1999-07-05 00:16:57 +04:00
|
|
|
vput(lvp);
|
|
|
|
*lvpp = NULL;
|
|
|
|
*uvpp = NULL;
|
1999-03-22 20:01:55 +03:00
|
|
|
return error;
|
|
|
|
}
|
1999-07-05 00:16:57 +04:00
|
|
|
uvp = *uvpp;
|
1999-03-22 20:01:55 +03:00
|
|
|
|
|
|
|
/* If we don't care about the pathname, we're done */
|
1999-06-21 09:11:09 +04:00
|
|
|
if (bufp == NULL) {
|
1999-07-05 00:16:57 +04:00
|
|
|
vrele(lvp);
|
|
|
|
*lvpp = NULL;
|
1999-03-22 20:01:55 +03:00
|
|
|
return 0;
|
1999-06-21 09:11:09 +04:00
|
|
|
}
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
fileno = va.va_fileid;
|
|
|
|
|
|
|
|
dirbuflen = DIRBLKSIZ;
|
|
|
|
if (dirbuflen < va.va_blocksize)
|
|
|
|
dirbuflen = va.va_blocksize;
|
|
|
|
dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
unionread:
|
|
|
|
#endif
|
|
|
|
off = 0;
|
|
|
|
do {
|
|
|
|
/* call VOP_READDIR of parent */
|
|
|
|
iov.iov_base = dirbuf;
|
|
|
|
iov.iov_len = dirbuflen;
|
|
|
|
|
|
|
|
uio.uio_iov = &iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
|
|
|
uio.uio_offset = off;
|
|
|
|
uio.uio_resid = dirbuflen;
|
|
|
|
uio.uio_rw = UIO_READ;
|
2006-03-01 15:38:10 +03:00
|
|
|
UIO_SETUP_SYSSPACE(&uio);
|
1999-03-22 20:01:55 +03:00
|
|
|
|
|
|
|
eofflag = 0;
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
error = VOP_READDIR(uvp, &uio, ucred, &eofflag, 0, 0);
|
1999-03-22 20:01:55 +03:00
|
|
|
|
|
|
|
off = uio.uio_offset;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try again if NFS tosses its cookies.
|
|
|
|
* XXX this can still loop forever if the directory is busted
|
|
|
|
* such that the second or subsequent page of it always
|
|
|
|
* returns EINVAL
|
|
|
|
*/
|
|
|
|
if ((error == EINVAL) && (tries < 3)) {
|
|
|
|
off = 0;
|
|
|
|
tries++;
|
|
|
|
continue; /* once more, with feeling */
|
|
|
|
}
|
1999-03-25 05:32:18 +03:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
if (!error) {
|
|
|
|
char *cpos;
|
|
|
|
struct dirent *dp;
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
cpos = dirbuf;
|
|
|
|
tries = 0;
|
2004-02-17 04:45:34 +03:00
|
|
|
|
|
|
|
/* scan directory page looking for matching vnode */
|
|
|
|
for (len = (dirbuflen - uio.uio_resid); len > 0;
|
|
|
|
len -= reclen) {
|
1999-03-22 20:01:55 +03:00
|
|
|
dp = (struct dirent *) cpos;
|
|
|
|
reclen = dp->d_reclen;
|
|
|
|
|
|
|
|
/* check for malformed directory.. */
|
2005-08-19 06:04:03 +04:00
|
|
|
if (reclen < _DIRENT_MINSIZE(dp)) {
|
1999-03-22 20:01:55 +03:00
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* XXX should perhaps do VOP_LOOKUP to
|
|
|
|
* check that we got back to the right place,
|
|
|
|
* but getting the locking games for that
|
|
|
|
* right would be heinous.
|
|
|
|
*/
|
|
|
|
if ((dp->d_type != DT_WHT) &&
|
|
|
|
(dp->d_fileno == fileno)) {
|
|
|
|
char *bp = *bpp;
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
bp -= dp->d_namlen;
|
|
|
|
if (bp <= bufp) {
|
|
|
|
error = ERANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
memcpy(bp, dp->d_name, dp->d_namlen);
|
|
|
|
error = 0;
|
|
|
|
*bpp = bp;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
cpos += reclen;
|
|
|
|
}
|
2000-12-15 14:52:14 +03:00
|
|
|
} else
|
|
|
|
goto out;
|
1999-03-25 05:32:18 +03:00
|
|
|
} while (!eofflag);
|
1999-03-22 20:01:55 +03:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* Deal with mount -o union, which unions only the
|
|
|
|
* root directory of the mount.
|
|
|
|
*/
|
1999-07-05 00:16:57 +04:00
|
|
|
if ((uvp->v_flag & VROOT) &&
|
|
|
|
(uvp->v_mount->mnt_flag & MNT_UNION)) {
|
|
|
|
struct vnode *tvp = uvp;
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-07-05 00:16:57 +04:00
|
|
|
uvp = uvp->v_mount->mnt_vnodecovered;
|
1999-03-22 20:01:55 +03:00
|
|
|
vput(tvp);
|
1999-07-05 00:16:57 +04:00
|
|
|
VREF(uvp);
|
|
|
|
*uvpp = uvp;
|
|
|
|
error = vn_lock(uvp, LK_EXCLUSIVE | LK_RETRY);
|
1999-03-22 20:01:55 +03:00
|
|
|
if (error != 0) {
|
1999-07-05 00:16:57 +04:00
|
|
|
vrele(uvp);
|
|
|
|
*uvpp = uvp = NULL;
|
1999-03-22 20:01:55 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
goto unionread;
|
|
|
|
}
|
2004-02-17 04:45:34 +03:00
|
|
|
#endif
|
1999-03-22 20:01:55 +03:00
|
|
|
error = ENOENT;
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
out:
|
1999-07-05 00:16:57 +04:00
|
|
|
vrele(lvp);
|
|
|
|
*lvpp = NULL;
|
1999-03-22 20:01:55 +03:00
|
|
|
free(dirbuf, M_TEMP);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look in the vnode-to-name reverse cache to see if
|
|
|
|
* we can find things the easy way.
|
|
|
|
*
|
1999-06-21 09:11:09 +04:00
|
|
|
* XXX vget failure path is untested.
|
|
|
|
*
|
1999-07-05 00:16:57 +04:00
|
|
|
* On entry, *lvpp is a locked vnode reference.
|
1999-06-21 09:11:09 +04:00
|
|
|
* On exit, one of the following is the case:
|
1999-07-05 00:16:57 +04:00
|
|
|
* 0) Both *lvpp and *uvpp are NULL and failure is returned.
|
|
|
|
* 1) *uvpp is NULL, *lvpp remains locked and -1 is returned (cache miss)
|
2004-02-17 04:45:34 +03:00
|
|
|
* 2) *uvpp is a locked vnode reference, *lvpp is vput and NULL'ed
|
1999-06-21 09:11:09 +04:00
|
|
|
* and 0 is returned (cache hit)
|
1999-03-22 20:01:55 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2005-06-06 03:47:48 +04:00
|
|
|
getcwd_getcache(struct vnode **lvpp, struct vnode **uvpp, char **bpp,
|
|
|
|
char *bufp)
|
1999-03-22 20:01:55 +03:00
|
|
|
{
|
1999-07-05 00:16:57 +04:00
|
|
|
struct vnode *lvp, *uvp = NULL;
|
2004-02-17 04:29:39 +03:00
|
|
|
char *obp = *bpp;
|
1999-03-22 20:01:55 +03:00
|
|
|
int error;
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-07-05 00:16:57 +04:00
|
|
|
lvp = *lvpp;
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-06-21 09:11:09 +04:00
|
|
|
/*
|
|
|
|
* This returns 0 on a cache hit, -1 on a clean cache miss,
|
|
|
|
* or an errno on other failure.
|
|
|
|
*/
|
1999-07-05 00:16:57 +04:00
|
|
|
error = cache_revlookup(lvp, uvpp, bpp, bufp);
|
1999-06-21 09:11:09 +04:00
|
|
|
if (error) {
|
|
|
|
if (error != -1) {
|
1999-07-05 00:16:57 +04:00
|
|
|
vput(lvp);
|
|
|
|
*lvpp = NULL;
|
|
|
|
*uvpp = NULL;
|
1999-06-21 09:11:09 +04:00
|
|
|
}
|
1999-03-22 20:01:55 +03:00
|
|
|
return error;
|
1999-06-21 09:11:09 +04:00
|
|
|
}
|
1999-07-05 00:16:57 +04:00
|
|
|
uvp = *uvpp;
|
1999-06-21 09:11:09 +04:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
/*
|
1999-06-21 09:11:09 +04:00
|
|
|
* Since we're going up, we have to release the current lock
|
|
|
|
* before we take the parent lock.
|
1999-03-22 20:01:55 +03:00
|
|
|
*/
|
|
|
|
|
1999-07-05 00:16:57 +04:00
|
|
|
VOP_UNLOCK(lvp, 0);
|
1999-06-21 09:11:09 +04:00
|
|
|
|
2003-06-29 22:43:21 +04:00
|
|
|
error = vget(uvp, LK_EXCLUSIVE | LK_RETRY);
|
1999-06-21 09:11:09 +04:00
|
|
|
/*
|
2004-02-17 04:35:33 +03:00
|
|
|
* Verify that vget succeeded while we were waiting for the
|
|
|
|
* lock.
|
1999-06-21 09:11:09 +04:00
|
|
|
*/
|
2003-07-30 16:10:57 +04:00
|
|
|
if (error) {
|
1999-06-21 09:11:09 +04:00
|
|
|
/*
|
2004-02-17 04:35:33 +03:00
|
|
|
* Oops, we missed. If the vget failed try to get our
|
|
|
|
* lock back; if that works, rewind the `bp' and tell
|
|
|
|
* caller to try things the hard way, otherwise give
|
|
|
|
* up.
|
1999-06-21 09:11:09 +04:00
|
|
|
*/
|
1999-07-05 00:16:57 +04:00
|
|
|
*uvpp = NULL;
|
|
|
|
error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
|
2004-02-17 04:35:33 +03:00
|
|
|
if (error == 0) {
|
|
|
|
*bpp = obp;
|
1999-07-05 00:16:57 +04:00
|
|
|
return -1;
|
2004-02-17 04:35:33 +03:00
|
|
|
}
|
1999-03-22 20:01:55 +03:00
|
|
|
}
|
1999-07-05 00:16:57 +04:00
|
|
|
vrele(lvp);
|
|
|
|
*lvpp = NULL;
|
1999-06-21 09:11:09 +04:00
|
|
|
|
1999-07-05 00:16:57 +04:00
|
|
|
return error;
|
1999-03-22 20:01:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* common routine shared by sys___getcwd() and vn_isunder()
|
|
|
|
*/
|
|
|
|
|
2004-02-17 04:45:34 +03:00
|
|
|
int
|
2005-06-06 03:47:48 +04:00
|
|
|
getcwd_common(struct vnode *lvp, struct vnode *rvp, char **bpp, char *bufp,
|
2005-12-11 15:16:03 +03:00
|
|
|
int limit, int flags, struct lwp *l)
|
1999-03-22 20:01:55 +03:00
|
|
|
{
|
2005-12-11 15:16:03 +03:00
|
|
|
struct cwdinfo *cwdi = l->l_proc->p_cwdi;
|
|
|
|
struct ucred *ucred = l->l_proc->p_ucred;
|
1999-07-05 00:16:57 +04:00
|
|
|
struct vnode *uvp = NULL;
|
1999-04-05 07:33:31 +04:00
|
|
|
char *bp = NULL;
|
1999-03-22 20:01:55 +03:00
|
|
|
int error;
|
1999-07-11 13:27:23 +04:00
|
|
|
int perms = VEXEC;
|
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
if (rvp == NULL) {
|
1999-04-30 22:42:58 +04:00
|
|
|
rvp = cwdi->cwdi_rdir;
|
1999-03-22 20:01:55 +03:00
|
|
|
if (rvp == NULL)
|
|
|
|
rvp = rootvnode;
|
|
|
|
}
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
VREF(rvp);
|
1999-07-05 00:16:57 +04:00
|
|
|
VREF(lvp);
|
1999-03-22 20:01:55 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Error handling invariant:
|
|
|
|
* Before a `goto out':
|
1999-07-05 00:16:57 +04:00
|
|
|
* lvp is either NULL, or locked and held.
|
|
|
|
* uvp is either NULL, or locked and held.
|
1999-03-22 20:01:55 +03:00
|
|
|
*/
|
|
|
|
|
1999-07-05 00:16:57 +04:00
|
|
|
error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
|
1999-03-22 20:01:55 +03:00
|
|
|
if (error) {
|
1999-07-05 00:16:57 +04:00
|
|
|
vrele(lvp);
|
|
|
|
lvp = NULL;
|
1999-03-22 20:01:55 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (bufp)
|
|
|
|
bp = *bpp;
|
|
|
|
/*
|
|
|
|
* this loop will terminate when one of the following happens:
|
|
|
|
* - we hit the root
|
|
|
|
* - getdirentries or lookup fails
|
|
|
|
* - we run out of space in the buffer.
|
|
|
|
*/
|
1999-07-05 00:16:57 +04:00
|
|
|
if (lvp == rvp) {
|
1999-06-21 09:11:09 +04:00
|
|
|
if (bp)
|
|
|
|
*(--bp) = '/';
|
1999-03-22 20:01:55 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
do {
|
1999-07-05 00:16:57 +04:00
|
|
|
if (lvp->v_type != VDIR) {
|
1999-03-22 20:01:55 +03:00
|
|
|
error = ENOTDIR;
|
|
|
|
goto out;
|
|
|
|
}
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
/*
|
|
|
|
* access check here is optional, depending on
|
|
|
|
* whether or not caller cares.
|
|
|
|
*/
|
|
|
|
if (flags & GETCWD_CHECK_ACCESS) {
|
2005-12-11 15:16:03 +03:00
|
|
|
error = VOP_ACCESS(lvp, perms, ucred, l);
|
1999-03-22 20:01:55 +03:00
|
|
|
if (error)
|
|
|
|
goto out;
|
1999-07-11 13:27:23 +04:00
|
|
|
perms = VEXEC|VREAD;
|
1999-03-22 20:01:55 +03:00
|
|
|
}
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-03-22 20:01:55 +03:00
|
|
|
/*
|
|
|
|
* step up if we're a covered vnode..
|
|
|
|
*/
|
1999-07-05 00:16:57 +04:00
|
|
|
while (lvp->v_flag & VROOT) {
|
1999-03-22 20:01:55 +03:00
|
|
|
struct vnode *tvp;
|
|
|
|
|
1999-07-05 00:16:57 +04:00
|
|
|
if (lvp == rvp)
|
1999-03-22 20:01:55 +03:00
|
|
|
goto out;
|
2004-02-17 04:45:34 +03:00
|
|
|
|
1999-07-05 00:16:57 +04:00
|
|
|
tvp = lvp;
|
|
|
|
lvp = lvp->v_mount->mnt_vnodecovered;
|
1999-03-22 20:01:55 +03:00
|
|
|
vput(tvp);
|
|
|
|
/*
|
|
|
|
* hodie natus est radici frater
|
|
|
|
*/
|
1999-07-05 00:16:57 +04:00
|
|
|
if (lvp == NULL) {
|
1999-03-22 20:01:55 +03:00
|
|
|
error = ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
1999-07-05 00:16:57 +04:00
|
|
|
VREF(lvp);
|
|
|
|
error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
|
1999-03-22 20:01:55 +03:00
|
|
|
if (error != 0) {
|
1999-07-05 00:16:57 +04:00
|
|
|
vrele(lvp);
|
|
|
|
lvp = NULL;
|
1999-03-22 20:01:55 +03:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Look in the name cache; if that fails, look in the
|
|
|
|
* directory..
|
|
|
|
*/
|
2003-06-30 02:28:00 +04:00
|
|
|
error = getcwd_getcache(&lvp, &uvp, &bp, bufp);
|
1999-03-22 20:01:55 +03:00
|
|
|
if (error == -1)
|
2005-12-11 15:16:03 +03:00
|
|
|
error = getcwd_scandir(&lvp, &uvp, &bp, bufp, l);
|
1999-03-22 20:01:55 +03:00
|
|
|
if (error)
|
|
|
|
goto out;
|
2004-02-17 04:45:34 +03:00
|
|
|
#if DIAGNOSTIC
|
1999-07-05 00:16:57 +04:00
|
|
|
if (lvp != NULL)
|
|
|
|
panic("getcwd: oops, forgot to null lvp");
|
1999-03-22 20:01:55 +03:00
|
|
|
if (bufp && (bp <= bufp)) {
|
|
|
|
panic("getcwd: oops, went back too far");
|
|
|
|
}
|
2004-02-17 04:45:34 +03:00
|
|
|
#endif
|
|
|
|
if (bp)
|
1999-06-21 09:11:09 +04:00
|
|
|
*(--bp) = '/';
|
1999-07-05 00:16:57 +04:00
|
|
|
lvp = uvp;
|
|
|
|
uvp = NULL;
|
1999-03-22 20:01:55 +03:00
|
|
|
limit--;
|
2004-02-17 04:45:34 +03:00
|
|
|
} while ((lvp != rvp) && (limit > 0));
|
1999-03-22 20:01:55 +03:00
|
|
|
|
|
|
|
out:
|
1999-06-21 09:11:09 +04:00
|
|
|
if (bpp)
|
|
|
|
*bpp = bp;
|
1999-07-05 00:16:57 +04:00
|
|
|
if (uvp)
|
|
|
|
vput(uvp);
|
|
|
|
if (lvp)
|
|
|
|
vput(lvp);
|
1999-03-22 20:01:55 +03:00
|
|
|
vrele(rvp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if one directory can be found inside another in the directory
|
|
|
|
* hierarchy.
|
|
|
|
*
|
|
|
|
* Intended to be used in chroot, chdir, fchdir, etc., to ensure that
|
|
|
|
* chroot() actually means something.
|
|
|
|
*/
|
1999-12-15 11:36:44 +03:00
|
|
|
int
|
2005-12-11 15:16:03 +03:00
|
|
|
vn_isunder(struct vnode *lvp, struct vnode *rvp, struct lwp *l)
|
1999-03-22 20:01:55 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2005-12-11 15:16:03 +03:00
|
|
|
error = getcwd_common(lvp, rvp, NULL, NULL, MAXPATHLEN / 2, 0, l);
|
1999-03-22 20:01:55 +03:00
|
|
|
|
|
|
|
if (!error)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1999-03-25 07:45:56 +03:00
|
|
|
/*
|
|
|
|
* Returns true if proc p1's root directory equal to or under p2's
|
|
|
|
* root directory.
|
|
|
|
*
|
|
|
|
* Intended to be used from ptrace/procfs sorts of things.
|
|
|
|
*/
|
|
|
|
|
1999-12-15 11:36:44 +03:00
|
|
|
int
|
2005-12-11 15:16:03 +03:00
|
|
|
proc_isunder(struct proc *p1, struct lwp *l2)
|
1999-03-25 07:45:56 +03:00
|
|
|
{
|
1999-04-30 22:42:58 +04:00
|
|
|
struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
|
2005-12-11 15:16:03 +03:00
|
|
|
struct vnode *r2 = l2->l_proc->p_cwdi->cwdi_rdir;
|
1999-04-30 22:42:58 +04:00
|
|
|
|
1999-03-25 07:45:56 +03:00
|
|
|
if (r1 == NULL)
|
|
|
|
return (r2 == NULL);
|
|
|
|
else if (r2 == NULL)
|
|
|
|
return 1;
|
|
|
|
else
|
2005-12-11 15:16:03 +03:00
|
|
|
return vn_isunder(r1, r2, l2);
|
1999-03-25 07:45:56 +03:00
|
|
|
}
|
|
|
|
|
1999-07-05 00:16:57 +04:00
|
|
|
/*
|
|
|
|
* Find pathname of process's current directory.
|
|
|
|
*
|
|
|
|
* Use vfs vnode-to-name reverse cache; if that fails, fall back
|
|
|
|
* to reading directory contents.
|
|
|
|
*/
|
|
|
|
|
1999-12-15 11:36:44 +03:00
|
|
|
int
|
2005-06-06 03:47:48 +04:00
|
|
|
sys___getcwd(struct lwp *l, void *v, register_t *retval)
|
1999-03-22 20:01:55 +03:00
|
|
|
{
|
2000-03-30 13:27:11 +04:00
|
|
|
struct sys___getcwd_args /* {
|
1999-03-22 20:01:55 +03:00
|
|
|
syscallarg(char *) bufp;
|
|
|
|
syscallarg(size_t) length;
|
|
|
|
} */ *uap = v;
|
|
|
|
|
|
|
|
int error;
|
|
|
|
char *path;
|
|
|
|
char *bp, *bend;
|
|
|
|
int len = SCARG(uap, length);
|
|
|
|
int lenused;
|
|
|
|
|
2004-02-17 04:45:34 +03:00
|
|
|
if (len > MAXPATHLEN * 4)
|
|
|
|
len = MAXPATHLEN * 4;
|
1999-06-19 22:01:26 +04:00
|
|
|
else if (len < 2)
|
1999-03-22 20:01:55 +03:00
|
|
|
return ERANGE;
|
|
|
|
|
|
|
|
path = (char *)malloc(len, M_TEMP, M_WAITOK);
|
|
|
|
if (!path)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
bp = &path[len];
|
|
|
|
bend = bp;
|
|
|
|
*(--bp) = '\0';
|
|
|
|
|
1999-06-21 09:11:09 +04:00
|
|
|
/*
|
|
|
|
* 5th argument here is "max number of vnodes to traverse".
|
|
|
|
* Since each entry takes up at least 2 bytes in the output buffer,
|
|
|
|
* limit it to N/2 vnodes for an N byte buffer.
|
|
|
|
*/
|
2005-12-11 15:16:03 +03:00
|
|
|
error = getcwd_common(l->l_proc->p_cwdi->cwdi_cdir, NULL, &bp, path,
|
|
|
|
len/2, GETCWD_CHECK_ACCESS, l);
|
1999-03-22 20:01:55 +03:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
lenused = bend - bp;
|
|
|
|
*retval = lenused;
|
|
|
|
/* put the result into user buffer */
|
|
|
|
error = copyout(bp, SCARG(uap, bufp), lenused);
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(path, M_TEMP);
|
|
|
|
return error;
|
|
|
|
}
|