2022-12-05 15:18:37 -08:00
|
|
|
/* $OpenBSD: vfs_getcwd.c,v 1.38 2022/12/05 23:18:37 deraadt Exp $ */
|
2006-04-28 01:34:31 -07:00
|
|
|
/* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/filedesc.h>
|
|
|
|
#include <sys/stat.h>
|
2014-12-16 10:30:03 -08:00
|
|
|
#include <sys/lock.h>
|
2006-04-28 01:34:31 -07:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/mount.h>
|
2019-05-30 06:10:23 -07:00
|
|
|
#include <sys/ktrace.h>
|
2006-04-28 01:34:31 -07:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/dirent.h>
|
2006-04-29 18:03:51 -07:00
|
|
|
#include <ufs/ufs/dir.h> /* only for DIRBLKSIZ */
|
2006-04-28 01:34:31 -07:00
|
|
|
|
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
|
|
|
|
|
2006-04-29 18:03:51 -07:00
|
|
|
/* Find parent vnode of *lvpp, return in *uvpp */
|
2006-05-01 13:03:06 -07:00
|
|
|
int
|
2006-05-01 14:08:44 -07:00
|
|
|
vfs_getcwd_scandir(struct vnode **lvpp, struct vnode **uvpp, char **bpp,
|
2006-04-29 18:03:51 -07:00
|
|
|
char *bufp, struct proc *p)
|
2006-04-28 01:34:31 -07:00
|
|
|
{
|
2017-09-06 05:36:24 -07:00
|
|
|
int eofflag, tries, dirbuflen = 0, len, reclen, error = 0;
|
2006-04-29 18:03:51 -07:00
|
|
|
off_t off;
|
2006-04-28 01:34:31 -07:00
|
|
|
struct uio uio;
|
|
|
|
struct iovec iov;
|
2006-04-29 18:03:51 -07:00
|
|
|
char *dirbuf = NULL;
|
|
|
|
ino_t fileno;
|
2006-04-28 01:34:31 -07:00
|
|
|
struct vattr va;
|
|
|
|
struct vnode *uvp = NULL;
|
2022-12-05 15:18:37 -08:00
|
|
|
struct vnode *lvp = *lvpp;
|
2006-04-28 01:34:31 -07:00
|
|
|
struct componentname cn;
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
tries = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we want the filename, get some info we need while the
|
|
|
|
* current directory is still locked.
|
|
|
|
*/
|
|
|
|
if (bufp != NULL) {
|
|
|
|
error = VOP_GETATTR(lvp, &va, p->p_ucred, p);
|
|
|
|
if (error) {
|
|
|
|
vput(lvp);
|
|
|
|
*lvpp = NULL;
|
|
|
|
*uvpp = NULL;
|
2006-04-29 14:31:15 -07:00
|
|
|
return (error);
|
2006-04-28 01:34:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cn.cn_nameiop = LOOKUP;
|
|
|
|
cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
|
|
|
|
cn.cn_proc = p;
|
|
|
|
cn.cn_cred = p->p_ucred;
|
|
|
|
cn.cn_pnbuf = NULL;
|
|
|
|
cn.cn_nameptr = "..";
|
|
|
|
cn.cn_namelen = 2;
|
|
|
|
cn.cn_consume = 0;
|
2006-04-29 18:03:51 -07:00
|
|
|
|
|
|
|
/* Get parent vnode using lookup of '..' */
|
2006-04-28 01:34:31 -07:00
|
|
|
error = VOP_LOOKUP(lvp, uvpp, &cn);
|
|
|
|
if (error) {
|
|
|
|
vput(lvp);
|
|
|
|
*lvpp = NULL;
|
|
|
|
*uvpp = NULL;
|
2006-04-29 14:31:15 -07:00
|
|
|
return (error);
|
2006-04-28 01:34:31 -07:00
|
|
|
}
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
uvp = *uvpp;
|
|
|
|
|
|
|
|
/* If we don't care about the pathname, we're done */
|
|
|
|
if (bufp == NULL) {
|
2017-07-28 14:54:49 -07:00
|
|
|
error = 0;
|
|
|
|
goto out;
|
2006-04-28 01:34:31 -07:00
|
|
|
}
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
fileno = va.va_fileid;
|
|
|
|
|
|
|
|
dirbuflen = DIRBLKSIZ;
|
|
|
|
if (dirbuflen < va.va_blocksize)
|
|
|
|
dirbuflen = va.va_blocksize;
|
2017-07-28 14:54:49 -07:00
|
|
|
/* XXX we need some limit for fuse, 1 MB should be enough */
|
|
|
|
if (dirbuflen > 0xfffff) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-05-17 05:52:12 -07:00
|
|
|
dirbuf = malloc(dirbuflen, M_TEMP, M_WAITOK);
|
2006-04-28 01:34:31 -07:00
|
|
|
|
|
|
|
off = 0;
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
do {
|
|
|
|
char *cpos;
|
|
|
|
struct dirent *dp;
|
|
|
|
|
|
|
|
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_segflg = UIO_SYSSPACE;
|
|
|
|
uio.uio_rw = UIO_READ;
|
|
|
|
uio.uio_procp = p;
|
|
|
|
|
|
|
|
eofflag = 0;
|
|
|
|
|
2006-04-29 18:03:51 -07:00
|
|
|
/* Call VOP_READDIR of parent */
|
Switch time_t, ino_t, clock_t, and struct kevent's ident and data
members to 64bit types. Assign new syscall numbers for (almost
all) the syscalls that involve the affected types, including anything
with time_t, timeval, itimerval, timespec, rusage, dirent, stat,
or kevent arguments. Add a d_off member to struct dirent and replace
getdirentries() with getdents(), thus immensely simplifying and
accelerating telldir/seekdir. Build perl with -DBIG_TIME.
Bump the major on every single base library: the compat bits included
here are only good enough to make the transition; the T32 compat
option will be burned as soon as we've reached the new world are
are happy with the snapshots for all architectures.
DANGER: ABI incompatibility. Updating to this kernel requires extra
work or you won't be able to login: install a snapshot instead.
Much assistance in fixing userland issues from deraadt@ and tedu@
and build assistance from todd@ and otto@
2013-08-12 22:52:02 -07:00
|
|
|
error = VOP_READDIR(uvp, &uio, p->p_ucred, &eofflag);
|
2006-04-28 01:34:31 -07:00
|
|
|
|
|
|
|
off = uio.uio_offset;
|
|
|
|
|
2006-04-29 18:03:51 -07:00
|
|
|
/* Try again if NFS tosses its cookies */
|
2006-04-28 01:34:31 -07:00
|
|
|
if (error == EINVAL && tries < 3) {
|
|
|
|
tries++;
|
|
|
|
off = 0;
|
|
|
|
continue;
|
2006-04-29 18:03:51 -07:00
|
|
|
} else if (error) {
|
|
|
|
goto out; /* Old userland getcwd() behaviour */
|
2006-04-28 01:34:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cpos = dirbuf;
|
|
|
|
tries = 0;
|
|
|
|
|
2006-04-29 18:03:51 -07:00
|
|
|
/* Scan directory page looking for matching vnode */
|
2006-04-28 01:34:31 -07:00
|
|
|
for (len = (dirbuflen - uio.uio_resid); len > 0;
|
|
|
|
len -= reclen) {
|
|
|
|
dp = (struct dirent *)cpos;
|
|
|
|
reclen = dp->d_reclen;
|
|
|
|
|
2006-04-29 18:03:51 -07:00
|
|
|
/* Check for malformed directory */
|
2017-07-28 14:54:49 -07:00
|
|
|
if (reclen < DIRENT_RECSIZE(1) || reclen > len) {
|
2006-04-28 01:34:31 -07:00
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
if (dp->d_fileno == fileno) {
|
|
|
|
char *bp = *bpp;
|
|
|
|
|
2017-07-28 14:54:49 -07:00
|
|
|
if (offsetof(struct dirent, d_name) +
|
|
|
|
dp->d_namlen > reclen) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
bp -= dp->d_namlen;
|
2006-04-28 01:34:31 -07:00
|
|
|
if (bp <= bufp) {
|
|
|
|
error = ERANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2013-06-09 06:15:26 -07:00
|
|
|
memmove(bp, dp->d_name, dp->d_namlen);
|
2006-04-28 01:34:31 -07:00
|
|
|
error = 0;
|
|
|
|
*bpp = bp;
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
goto out;
|
|
|
|
}
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
cpos += reclen;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (!eofflag);
|
|
|
|
|
|
|
|
error = ENOENT;
|
|
|
|
|
|
|
|
out:
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
vrele(lvp);
|
|
|
|
*lvpp = NULL;
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2014-11-02 19:08:00 -08:00
|
|
|
free(dirbuf, M_TEMP, dirbuflen);
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-29 14:31:15 -07:00
|
|
|
return (error);
|
2006-04-28 01:34:31 -07:00
|
|
|
}
|
|
|
|
|
2006-04-29 18:03:51 -07:00
|
|
|
/* Do a lookup in the vnode-to-name reverse */
|
2006-05-01 13:03:06 -07:00
|
|
|
int
|
2006-05-01 14:08:44 -07:00
|
|
|
vfs_getcwd_getcache(struct vnode **lvpp, struct vnode **uvpp, char **bpp,
|
2006-04-29 18:03:51 -07:00
|
|
|
char *bufp)
|
2006-04-28 01:34:31 -07:00
|
|
|
{
|
|
|
|
struct vnode *lvp, *uvp = NULL;
|
2007-08-07 00:41:59 -07:00
|
|
|
char *obp;
|
2006-04-29 18:03:51 -07:00
|
|
|
int error, vpid;
|
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
lvp = *lvpp;
|
2010-01-14 15:12:11 -08:00
|
|
|
obp = *bpp; /* Save original position to restore to on error */
|
2006-04-28 01:34:31 -07:00
|
|
|
|
|
|
|
error = cache_revlookup(lvp, uvpp, bpp, bufp);
|
|
|
|
if (error) {
|
|
|
|
if (error != -1) {
|
|
|
|
vput(lvp);
|
|
|
|
*lvpp = NULL;
|
|
|
|
*uvpp = NULL;
|
|
|
|
}
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-29 14:31:15 -07:00
|
|
|
return (error);
|
2006-04-28 01:34:31 -07:00
|
|
|
}
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
uvp = *uvpp;
|
|
|
|
vpid = uvp->v_id;
|
|
|
|
|
|
|
|
|
2006-04-29 18:03:51 -07:00
|
|
|
/* Release current lock before acquiring the parent lock */
|
2018-04-27 20:13:04 -07:00
|
|
|
VOP_UNLOCK(lvp);
|
2006-04-28 01:34:31 -07:00
|
|
|
|
2018-05-26 23:02:14 -07:00
|
|
|
error = vget(uvp, LK_EXCLUSIVE | LK_RETRY);
|
2006-04-29 18:03:51 -07:00
|
|
|
if (error)
|
2006-04-28 01:34:31 -07:00
|
|
|
*uvpp = NULL;
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2007-05-29 21:27:42 -07:00
|
|
|
/*
|
|
|
|
* Verify that vget() succeeded, and check that vnode capability
|
|
|
|
* didn't change while we were waiting for the lock.
|
|
|
|
*/
|
|
|
|
if (error || (vpid != uvp->v_id)) {
|
2006-04-28 01:34:31 -07:00
|
|
|
/*
|
2006-04-29 18:03:51 -07:00
|
|
|
* Try to get our lock back. If that works, tell the caller to
|
|
|
|
* try things the hard way, otherwise give up.
|
2006-04-28 01:34:31 -07:00
|
|
|
*/
|
2007-05-29 21:27:42 -07:00
|
|
|
if (!error)
|
|
|
|
vput(uvp);
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
*uvpp = NULL;
|
2022-12-05 15:18:37 -08:00
|
|
|
|
2018-05-01 19:24:55 -07:00
|
|
|
error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
|
2007-08-07 00:41:59 -07:00
|
|
|
if (!error) {
|
|
|
|
*bpp = obp; /* restore the buffer */
|
2006-04-29 14:31:15 -07:00
|
|
|
return (-1);
|
2007-08-07 00:41:59 -07:00
|
|
|
}
|
2006-04-28 01:34:31 -07:00
|
|
|
}
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
vrele(lvp);
|
|
|
|
*lvpp = NULL;
|
2006-04-29 18:03:51 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2019-05-13 15:55:27 -07:00
|
|
|
/* Common routine shared by sys___getcwd() and vn_isunder() and sys___realpath() */
|
2006-05-01 13:03:06 -07:00
|
|
|
int
|
2006-05-01 14:08:44 -07:00
|
|
|
vfs_getcwd_common(struct vnode *lvp, struct vnode *rvp, char **bpp, char *bufp,
|
2006-04-29 17:34:34 -07:00
|
|
|
int limit, int flags, struct proc *p)
|
2006-04-28 01:34:31 -07:00
|
|
|
{
|
|
|
|
struct filedesc *fdp = p->p_fd;
|
|
|
|
struct vnode *uvp = NULL;
|
|
|
|
char *bp = NULL;
|
2006-04-29 17:34:34 -07:00
|
|
|
int error, perms = VEXEC;
|
2006-04-28 01:34:31 -07:00
|
|
|
|
|
|
|
if (rvp == NULL) {
|
|
|
|
rvp = fdp->fd_rdir;
|
|
|
|
if (rvp == NULL)
|
|
|
|
rvp = rootvnode;
|
|
|
|
}
|
|
|
|
|
2009-07-09 15:29:55 -07:00
|
|
|
vref(rvp);
|
|
|
|
vref(lvp);
|
2006-04-28 01:34:31 -07:00
|
|
|
|
2018-05-01 19:24:55 -07:00
|
|
|
error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
|
2006-04-28 01:34:31 -07:00
|
|
|
if (error) {
|
|
|
|
vrele(lvp);
|
|
|
|
lvp = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
if (bufp)
|
|
|
|
bp = *bpp;
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
if (lvp == rvp) {
|
|
|
|
if (bp)
|
|
|
|
*(--bp) = '/';
|
|
|
|
goto out;
|
|
|
|
}
|
2006-04-29 17:34:34 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This loop will terminate when we hit the root, VOP_READDIR() or
|
|
|
|
* VOP_LOOKUP() fails, or we run out of space in the user buffer.
|
|
|
|
*/
|
2006-04-28 01:34:31 -07:00
|
|
|
do {
|
|
|
|
if (lvp->v_type != VDIR) {
|
|
|
|
error = ENOTDIR;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-04-29 17:34:34 -07:00
|
|
|
|
|
|
|
/* Check for access if caller cares */
|
2006-04-28 01:34:31 -07:00
|
|
|
if (flags & GETCWD_CHECK_ACCESS) {
|
|
|
|
error = VOP_ACCESS(lvp, perms, p->p_ucred, p);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
perms = VEXEC|VREAD;
|
|
|
|
}
|
2006-04-29 17:34:34 -07:00
|
|
|
|
|
|
|
/* Step up if we're a covered vnode */
|
2006-04-28 01:34:31 -07:00
|
|
|
while (lvp->v_flag & VROOT) {
|
|
|
|
struct vnode *tvp;
|
|
|
|
|
|
|
|
if (lvp == rvp)
|
|
|
|
goto out;
|
2022-12-05 15:18:37 -08:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
tvp = lvp;
|
|
|
|
lvp = lvp->v_mount->mnt_vnodecovered;
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
vput(tvp);
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
if (lvp == NULL) {
|
|
|
|
error = ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2009-07-09 15:29:55 -07:00
|
|
|
vref(lvp);
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2018-05-01 19:24:55 -07:00
|
|
|
error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
|
2006-04-29 17:34:34 -07:00
|
|
|
if (error) {
|
2006-04-28 01:34:31 -07:00
|
|
|
vrele(lvp);
|
|
|
|
lvp = NULL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2006-04-29 17:34:34 -07:00
|
|
|
|
|
|
|
/* Look in the name cache */
|
2006-05-01 14:08:44 -07:00
|
|
|
error = vfs_getcwd_getcache(&lvp, &uvp, &bp, bufp);
|
2006-04-29 17:34:34 -07:00
|
|
|
|
|
|
|
if (error == -1) {
|
|
|
|
/* If that fails, look in the directory */
|
2006-05-01 14:08:44 -07:00
|
|
|
error = vfs_getcwd_scandir(&lvp, &uvp, &bp, bufp, p);
|
2006-04-29 17:34:34 -07:00
|
|
|
}
|
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
if (error)
|
|
|
|
goto out;
|
2006-04-29 17:34:34 -07:00
|
|
|
|
|
|
|
#ifdef DIAGNOSTIC
|
2006-04-28 01:34:31 -07:00
|
|
|
if (lvp != NULL)
|
|
|
|
panic("getcwd: oops, forgot to null lvp");
|
|
|
|
if (bufp && (bp <= bufp)) {
|
|
|
|
panic("getcwd: oops, went back too far");
|
|
|
|
}
|
2006-04-29 17:34:34 -07:00
|
|
|
#endif
|
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
if (bp)
|
|
|
|
*(--bp) = '/';
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
lvp = uvp;
|
|
|
|
uvp = NULL;
|
|
|
|
limit--;
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
} while ((lvp != rvp) && (limit > 0));
|
|
|
|
|
|
|
|
out:
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
if (bpp)
|
|
|
|
*bpp = bp;
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
if (uvp)
|
|
|
|
vput(uvp);
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
if (lvp)
|
|
|
|
vput(lvp);
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
vrele(rvp);
|
2006-04-29 17:34:34 -07:00
|
|
|
|
2006-04-29 14:31:15 -07:00
|
|
|
return (error);
|
2006-04-28 01:34:31 -07:00
|
|
|
}
|
|
|
|
|
2006-04-29 17:08:13 -07:00
|
|
|
/* Find pathname of a process's current directory */
|
2006-04-28 01:34:31 -07:00
|
|
|
int
|
|
|
|
sys___getcwd(struct proc *p, void *v, register_t *retval)
|
|
|
|
{
|
2006-04-29 17:08:13 -07:00
|
|
|
struct sys___getcwd_args *uap = v;
|
2019-05-30 06:10:23 -07:00
|
|
|
int error, len = SCARG(uap, len);
|
|
|
|
char *path, *bp;
|
2006-04-29 17:08:13 -07:00
|
|
|
|
|
|
|
if (len > MAXPATHLEN * 4)
|
|
|
|
len = MAXPATHLEN * 4;
|
2006-04-28 01:34:31 -07:00
|
|
|
else if (len < 2)
|
2006-04-29 14:31:15 -07:00
|
|
|
return (ERANGE);
|
2006-04-28 01:34:31 -07:00
|
|
|
|
2006-05-17 05:52:12 -07:00
|
|
|
path = malloc(len, M_TEMP, M_WAITOK);
|
2006-04-28 01:34:31 -07:00
|
|
|
|
2019-05-30 06:34:54 -07:00
|
|
|
bp = &path[len - 1];
|
|
|
|
*bp = '\0';
|
2006-04-28 01:34:31 -07: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.
|
|
|
|
*/
|
2006-05-01 14:08:44 -07:00
|
|
|
error = vfs_getcwd_common(p->p_fd->fd_cdir, NULL, &bp, path, len/2,
|
2006-04-28 01:34:31 -07:00
|
|
|
GETCWD_CHECK_ACCESS, p);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
goto out;
|
2006-04-29 17:08:13 -07:00
|
|
|
|
|
|
|
/* Put the result into user buffer */
|
2019-05-30 06:10:23 -07:00
|
|
|
error = copyoutstr(bp, SCARG(uap, buf), MAXPATHLEN, NULL);
|
2006-04-28 01:34:31 -07:00
|
|
|
|
2019-05-30 06:11:53 -07:00
|
|
|
#ifdef KTRACE
|
|
|
|
if (KTRPOINT(p, KTR_NAMEI))
|
|
|
|
ktrnamei(p, bp);
|
|
|
|
#endif
|
|
|
|
|
2006-04-28 01:34:31 -07:00
|
|
|
out:
|
2014-11-02 19:08:00 -08:00
|
|
|
free(path, M_TEMP, len);
|
2006-04-29 17:08:13 -07:00
|
|
|
|
2006-04-29 14:31:15 -07:00
|
|
|
return (error);
|
2006-04-28 01:34:31 -07:00
|
|
|
}
|