#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.64 2026/03/15 12:02:57 yamt Exp $");
#include <sys/param.h>
#include <sys/types.h>
#include <sys/dirent.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/sdt.h>
#include <sys/stat.h>
#include <sys/syscallargs.h>
#include <sys/systm.h>
#include <sys/uio.h>
#include <sys/vnode.h>
#include <ufs/ufs/dir.h>
static int
getcwd_scandir(struct vnode *lvp, struct vnode **uvpp, char **bpp,
char *bufp, struct lwp *l)
{
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;
struct vnode *uvp = NULL;
kauth_cred_t cred = l->l_cred;
struct componentname cn;
int len, reclen;
tries = 0;
KASSERT(VOP_ISLOCKED(lvp) == LK_EXCLUSIVE);
if (bufp != NULL) {
error = VOP_GETATTR(lvp, &va, cred);
if (error) {
VOP_UNLOCK(lvp);
*uvpp = NULL;
return error;
}
}
cn.cn_nameiop = LOOKUP;
cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
cn.cn_cred = cred;
cn.cn_nameptr = "..";
cn.cn_namelen = 2;
error = VOP_LOOKUP(lvp, uvpp, &cn);
VOP_UNLOCK(lvp);
if (error) {
*uvpp = NULL;
return error;
}
uvp = *uvpp;
if (bufp == NULL) {
return 0;
}
fileno = va.va_fileid;
dirbuflen = UFS_DIRBLKSIZ;
if (dirbuflen < va.va_blocksize)
dirbuflen = va.va_blocksize;
dirbuf = kmem_alloc(dirbuflen, KM_SLEEP);
error = vn_lock(uvp, LK_SHARED);
if (error) {
vrele(uvp);
*uvpp = NULL;
return error;
}
#if 0
unionread:
#endif
off = 0;
do {
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;
UIO_SETUP_SYSSPACE(&uio);
eofflag = 0;
error = VOP_READDIR(uvp, &uio, cred, &eofflag, 0, 0);
off = uio.uio_offset;
if ((error == EINVAL) && (tries < 3)) {
off = 0;
tries++;
continue;
}
if (!error) {
char *cpos;
struct dirent *dp;
cpos = dirbuf;
tries = 0;
for (len = (dirbuflen - uio.uio_resid); len > 0;
len -= reclen) {
dp = (struct dirent *) cpos;
reclen = dp->d_reclen;
if (reclen < _DIRENT_MINSIZE(dp) ||
reclen > len) {
error = SET_ERROR(EINVAL);
goto out;
}
if ((dp->d_type != DT_WHT) &&
(dp->d_fileno == fileno)) {
char *bp = *bpp;
bp -= dp->d_namlen;
if (bp <= bufp) {
error = SET_ERROR(ERANGE);
goto out;
}
memcpy(bp, dp->d_name, dp->d_namlen);
error = 0;
*bpp = bp;
goto out;
}
cpos += reclen;
}
} else
goto out;
} while (!eofflag);
#if 0
if ((uvp->v_vflag & VV_ROOT) &&
(uvp->v_mount->mnt_flag & MNT_UNION)) {
struct vnode *tvp = uvp;
uvp = uvp->v_mount->mnt_vnodecovered;
vput(tvp);
vref(uvp);
*uvpp = uvp;
vn_lock(uvp, LK_SHARED | LK_RETRY);
goto unionread;
}
#endif
error = SET_ERROR(ENOENT);
out:
VOP_UNLOCK(uvp);
kmem_free(dirbuf, dirbuflen);
return error;
}
int
getcwd_common(struct vnode *lvp, struct vnode *rvp, char **bpp, char *bufp,
int limit, int flags, struct lwp *l)
{
struct cwdinfo *cwdi = l->l_proc->p_cwdi;
kauth_cred_t cred = l->l_cred;
struct vnode *uvp = NULL;
char *bp = NULL;
int error;
accmode_t accmode = VEXEC;
error = 0;
if (rvp == NULL) {
rvp = cwdi->cwdi_rdir;
if (rvp == NULL)
rvp = rootvnode;
}
vref(rvp);
vref(lvp);
if (bufp)
bp = *bpp;
if (lvp == rvp) {
if (bp)
*(--bp) = '/';
goto out;
}
do {
int chkaccess = (flags & GETCWD_CHECK_ACCESS);
bool locked = false;
if (lvp->v_vflag & VV_ROOT) {
vn_lock(lvp, LK_SHARED | LK_RETRY);
if (chkaccess) {
error = VOP_ACCESS(lvp, accmode, cred);
if (error) {
VOP_UNLOCK(lvp);
goto out;
}
chkaccess = 0;
}
while (lvp->v_vflag & VV_ROOT) {
struct vnode *tvp;
if (lvp == rvp) {
VOP_UNLOCK(lvp);
goto out;
}
tvp = lvp->v_mount->mnt_vnodecovered;
if (tvp == NULL) {
VOP_UNLOCK(lvp);
error = SET_ERROR(ENOENT);
goto out;
}
vref(tvp);
vput(lvp);
lvp = tvp;
if (lvp->v_vflag & VV_ROOT)
vn_lock(lvp, LK_SHARED | LK_RETRY);
}
}
if (chkaccess && !cache_have_id(lvp)) {
vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
error = VOP_ACCESS(lvp, accmode, cred);
if (error) {
VOP_UNLOCK(lvp);
goto out;
}
chkaccess = 0;
locked = true;
}
error = cache_revlookup(lvp, &uvp, &bp, bufp, chkaccess,
accmode);
if (error == -1) {
if (!locked) {
locked = true;
vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
}
if (lvp->v_type != VDIR) {
VOP_UNLOCK(lvp);
error = SET_ERROR(ENOTDIR);
goto out;
}
error = getcwd_scandir(lvp, &uvp, &bp, bufp, l);
} else if (locked) {
VOP_UNLOCK(lvp);
}
if (error)
goto out;
#if DIAGNOSTIC
if (bufp && (bp <= bufp)) {
panic("getcwd: oops, went back too far");
}
#endif
accmode = VEXEC | VREAD;
if (bp)
*(--bp) = '/';
vrele(lvp);
lvp = uvp;
uvp = NULL;
limit--;
} while ((lvp != rvp) && (limit > 0));
out:
if (bpp)
*bpp = bp;
if (uvp)
vrele(uvp);
if (lvp)
vrele(lvp);
vrele(rvp);
return error;
}
int
vn_isunder(struct vnode *lvp, struct vnode *rvp, struct lwp *l)
{
int error;
error = getcwd_common(lvp, rvp, NULL, NULL, MAXPATHLEN / 2, 0, l);
if (!error)
return 1;
else
return 0;
}
int
proc_isunder(struct proc *p1, struct lwp *l2)
{
struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
struct vnode *r2 = l2->l_proc->p_cwdi->cwdi_rdir;
if (r1 == NULL)
return (r2 == NULL);
else if (r2 == NULL)
return 1;
else
return vn_isunder(r1, r2, l2);
}
int
sys___getcwd(struct lwp *l, const struct sys___getcwd_args *uap,
register_t *retval)
{
int error;
char *path;
char *bp, *bend;
int len = SCARG(uap, length);
int lenused;
struct cwdinfo *cwdi;
if (len > MAXPATHLEN * 4)
len = MAXPATHLEN * 4;
else if (len < 2)
return SET_ERROR(ERANGE);
path = kmem_alloc(len, KM_SLEEP);
bp = &path[len];
bend = bp;
*(--bp) = '\0';
cwdi = l->l_proc->p_cwdi;
rw_enter(&cwdi->cwdi_lock, RW_READER);
error = getcwd_common(cwdi->cwdi_cdir, NULL, &bp, path,
len/2, GETCWD_CHECK_ACCESS, l);
rw_exit(&cwdi->cwdi_lock);
if (error)
goto out;
lenused = bend - bp;
*retval = lenused;
error = copyout(bp, SCARG(uap, bufp), lenused);
out:
kmem_free(path, len);
return error;
}
int
vnode_to_path(char *path, size_t len, struct vnode *vp, struct lwp *curl,
struct proc *p)
{
struct proc *curp = curl->l_proc;
int error, lenused, elen;
char *bp, *bend;
struct vnode *dvp;
KASSERT(vrefcnt(vp) > 0);
bp = bend = &path[len];
*(--bp) = '\0';
error = cache_revlookup(vp, &dvp, &bp, path, false, 0);
if (error != 0)
return (error == -1 ? SET_ERROR(ENOENT) : error);
*(--bp) = '/';
error = getcwd_common(dvp, NULL, &bp, path, len / 2,
GETCWD_CHECK_ACCESS, curl);
vrele(dvp);
if (error != 0)
return error;
if (curp->p_emul == p->p_emul && curp->p_emul->e_path != NULL) {
elen = strlen(curp->p_emul->e_path);
if (!strncmp(bp, curp->p_emul->e_path, elen))
bp = &bp[elen];
}
lenused = bend - bp;
memcpy(path, bp, lenused);
path[lenused] = '\0';
return 0;
}