Unix Technical Forum

In-kernel getcwd()

This is a discussion on In-kernel getcwd() within the lucky.openbsd.tech forums, part of the OpenBSD category; --> Hi, This diff moves getcwd() into the kernel, and is originally from Marius. The only changes I've made to ...


Go Back   Unix Technical Forum > Unix Operating Systems > OpenBSD > lucky.openbsd.tech

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-22-2008, 01:17 PM
Pedro Martelletto
 
Posts: n/a
Default In-kernel getcwd()

Hi,

This diff moves getcwd() into the kernel, and is originally from Marius.
The only changes I've made to it were the COMPAT_NETBSD chunk at the end
(so it doesn't break Alpha's kernel build - thanks Jolan) and the ones
necessary for it to apply against -current (since this diff is almost an
year old).

Initially there was a problem with it, which was fixed in revision 1.19
of vfs_cache.c (it wasn't caching vnodes correctly). Since then, I've
had no problems with this diff, and I'm sure at least Nikolay and Jolan
have been running with it on a variety of machines (for > 2 months now).

Marius has been quite busy lately, and I'm afraid that if it didn't get
sent out now, this diff would miss the next release timing. So pardon me
for 'stealing' your work, Marius. :-)

This was adapted from NetBSD.

-p.

Index: sys/kern/vfs_getcwd.c
================================================== =================
RCS file: sys/kern/vfs_getcwd.c
diff -N sys/kern/vfs_getcwd.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ sys/kern/vfs_getcwd.c 27 Mar 2006 15:40:02 -0000
@@ -0,0 +1,560 @@
+/* $OpenBSD$ */
+/* $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.
+ * 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.
+ */
+
+#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 */
+
+#include <sys/syscallargs.h>
+
+static int getcwd_scandir(struct vnode **, struct vnode **,
+ char **, char *, struct proc *);
+static int getcwd_getcache(struct vnode **, struct vnode **, char **, char *);
+static int getcwd_common(struct vnode *, struct vnode *,
+ char **, char *, int, int, struct proc *);
+
+static int vn_isunder(struct vnode *, struct vnode *, struct proc *);
+
+#define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
+
+/*
+ * 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.
+ */
+
+/*
+ * XXX: is EINVAL the right thing to return if a directory is
+ * malformed?
+ */
+
+
+/*
+ * 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.
+ *
+ * Place the name in the buffer which starts at bufp, immediately
+ * before *bpp, and move bpp backwards to point at the start of it.
+ *
+ * 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.
+ */
+static int
+getcwd_scandir(struct vnode **lvpp, struct vnode **uvpp,
+ char **bpp, char *bufp, struct proc *p)
+{
+ 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;
+ struct vnode *lvp = *lvpp;
+ struct componentname cn;
+ int len, reclen;
+ 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;
+ return error;
+ }
+ }
+
+ /*
+ * Ok, we have to do it the hard way..
+ * Next, get parent vnode using lookup of ..
+ */
+ 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_hash = 0;
+ cn.cn_consume = 0;
+
+ /*
+ * At this point, lvp is locked and will be unlocked by the lookup.
+ * On successful return, *uvpp will be locked
+ */
+ error = VOP_LOOKUP(lvp, uvpp, &cn);
+ if (error) {
+ vput(lvp);
+ *lvpp = NULL;
+ *uvpp = NULL;
+ return error;
+ }
+ uvp = *uvpp;
+
+ /* If we don't care about the pathname, we're done */
+ if (bufp == NULL) {
+ vrele(lvp);
+ *lvpp = NULL;
+ return 0;
+ }
+
+ fileno = va.va_fileid;
+
+ dirbuflen = DIRBLKSIZ;
+ if (dirbuflen < va.va_blocksize)
+ dirbuflen = va.va_blocksize;
+ dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
+
+ off = 0;
+ do {
+ char *cpos;
+ struct dirent *dp;
+
+ /* 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_segflg = UIO_SYSSPACE;
+ uio.uio_rw = UIO_READ;
+ uio.uio_procp = p;
+
+ eofflag = 0;
+
+ error = VOP_READDIR(uvp, &uio, p->p_ucred, &eofflag, 0, 0);
+
+ off = uio.uio_offset;
+
+ /*
+ * Try again if NFS tosses its cookies.
+ */
+ if (error == EINVAL && tries < 3) {
+ tries++;
+ off = 0;
+ continue;
+ } else if (error != 0) {
+ /*
+ * We simply bail on error, even if we don't
+ * have eofflag yet. This is the exact
+ * behavior of the old userland getcwd()
+ */
+ goto out;
+ }
+
+ /* No error */
+ cpos = dirbuf;
+ tries = 0;
+
+ /* scan directory page looking for matching vnode */
+ for (len = (dirbuflen - uio.uio_resid); len > 0;
+ len -= reclen) {
+ dp = (struct dirent *)cpos;
+ reclen = dp->d_reclen;
+
+ /* check for malformed directory.. */
+ if (reclen < DIRENT_MINSIZE) {
+ 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_fileno == fileno) {
+ char *bp = *bpp;
+ bp -= dp->d_namlen;
+
+ if (bp <= bufp) {
+ error = ERANGE;
+ goto out;
+ }
+ bcopy(dp->d_name, bp, dp->d_namlen);
+ error = 0;
+ *bpp = bp;
+ goto out;
+ }
+ cpos += reclen;
+ }
+
+ } while (!eofflag);
+
+ error = ENOENT;
+
+out:
+ vrele(lvp);
+ *lvpp = NULL;
+ free(dirbuf, M_TEMP);
+ return error;
+}
+
+/*
+ * Look in the vnode-to-name reverse cache to see if
+ * we can find things the easy way.
+ *
+ * XXX vget failure path is untested.
+ *
+ * On entry, *lvpp is a locked vnode reference.
+ * On exit, one of the following is the case:
+ * 0) Both *lvpp and *uvpp are NULL and failure is returned.
+ * 1) *uvpp is NULL, *lvpp remains locked and -1 is returned (cache miss)
+ * 2) *uvpp is a locked vnode reference, *lvpp is vput and NULL'ed
+ * and 0 is returned (cache hit)
+ */
+
+static int
+getcwd_getcache(struct vnode **lvpp, struct vnode **uvpp,
+ char **bpp, char *bufp)
+{
+ struct vnode *lvp, *uvp = NULL;
+ int error;
+ int vpid;
+ struct proc *p = curproc;
+
+ lvp = *lvpp;
+
+ /*
+ * This returns 0 on a cache hit, -1 on a clean cache miss, or
+ * an errno on other failure.
+ */
+ error = cache_revlookup(lvp, uvpp, bpp, bufp);
+ if (error) {
+ if (error != -1) {
+ vput(lvp);
+ *lvpp = NULL;
+ *uvpp = NULL;
+ }
+ return error;
+ }
+ uvp = *uvpp;
+ vpid = uvp->v_id;
+
+ /*
+ * Since we're going up, we have to release the current lock
+ * before we take the parent lock.
+ */
+
+ VOP_UNLOCK(lvp, 0, p);
+
+ error = vget(uvp, LK_EXCLUSIVE | LK_RETRY, p);
+ if (error != 0)
+ *uvpp = NULL;
+ /*
+ * Verify that vget succeeded, and check that vnode capability
+ * didn't change while we were waiting for the lock.
+ *
+ * XXX: this is kind of nasty to have to check here. It
+ * should really be done in cache_revlookup() (see comments
+ * there, too).
+ */
+ if (error || (vpid != uvp->v_id)) {
+ /*
+ * Oops, we missed. If the vget failed, or the
+ * capability changed, try to get our lock back; if
+ * that works, tell caller to try things the hard way,
+ * otherwise give up.
+ */
+ if (!error)
+ vput(uvp);
+ *uvpp = NULL;
+
+ error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
+
+ if (!error)
+ return -1;
+ }
+ vrele(lvp);
+ *lvpp = NULL;
+ return (error);
+}
+
+/*
+ * common routine shared by sys___getcwd() and vn_isunder()
+ */
+
+#define GETCWD_CHECK_ACCESS 0x0001
+
+static int
+getcwd_common(struct vnode *lvp, struct vnode *rvp,
+ char **bpp, char *bufp, int limit, int flags, struct proc *p)
+{
+ struct filedesc *fdp = p->p_fd;
+ struct vnode *uvp = NULL;
+ char *bp = NULL;
+ int error;
+ int perms = VEXEC;
+
+ if (rvp == NULL) {
+ rvp = fdp->fd_rdir;
+ if (rvp == NULL)
+ rvp = rootvnode;
+ }
+
+ VREF(rvp);
+ VREF(lvp);
+
+ /*
+ * Error handling invariant:
+ * Before a `goto out':
+ * lvp is either NULL, or locked and held.
+ * uvp is either NULL, or locked and held.
+ */
+
+ error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
+ if (error) {
+ vrele(lvp);
+ lvp = NULL;
+ 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.
+ */
+ if (lvp == rvp) {
+ if (bp)
+ *(--bp) = '/';
+ goto out;
+ }
+ do {
+ if (lvp->v_type != VDIR) {
+ error = ENOTDIR;
+ goto out;
+ }
+
+ /*
+ * access check here is optional, depending on
+ * whether or not caller cares.
+ */
+ if (flags & GETCWD_CHECK_ACCESS) {
+ error = VOP_ACCESS(lvp, perms, p->p_ucred, p);
+ if (error)
+ goto out;
+ perms = VEXEC|VREAD;
+ }
+
+ /*
+ * step up if we're a covered vnode..
+ */
+ while (lvp->v_flag & VROOT) {
+ struct vnode *tvp;
+
+ if (lvp == rvp)
+ goto out;
+
+ tvp = lvp;
+ lvp = lvp->v_mount->mnt_vnodecovered;
+ vput(tvp);
+ /*
+ * hodie natus est radici frater
+ */
+ if (lvp == NULL) {
+ error = ENOENT;
+ goto out;
+ }
+ VREF(lvp);
+ error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
+ if (error != 0) {
+ vrele(lvp);
+ lvp = NULL;
+ goto out;
+ }
+ }
+ /*
+ * Look in the name cache; if that fails, look in the
+ * directory..
+ */
+ error = getcwd_getcache(&lvp, &uvp, &bp, bufp);
+ if (error == -1)
+ error = getcwd_scandir(&lvp, &uvp, &bp, bufp, p);
+ if (error)
+ goto out;
+#ifdef DIAGNOSTIC
+ if (lvp != NULL)
+ panic("getcwd: oops, forgot to null lvp");
+ if (bufp && (bp <= bufp)) {
+ panic("getcwd: oops, went back too far");
+ }
+#endif
+ if (bp)
+ *(--bp) = '/';
+ lvp = uvp;
+ uvp = NULL;
+ limit--;
+ } while ((lvp != rvp) && (limit > 0));
+
+out:
+ if (bpp)
+ *bpp = bp;
+ if (uvp)
+ vput(uvp);
+ if (lvp)
+ vput(lvp);
+ 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.
+ */
+static int
+vn_isunder(struct vnode *lvp, struct vnode *rvp, struct proc *p)
+{
+ int error;
+
+ error = getcwd_common(lvp, rvp, NULL, NULL, MAXPATHLEN/2, 0, p);
+
+ if (!error)
+ return 1;
+ else
+ return 0;
+}
+
+/*
+ * 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.
+ */
+
+int
+proc_isunder(struct proc *p1, struct proc *p2)
+{
+ struct vnode *r1 = p1->p_fd->fd_rdir;
+ struct vnode *r2 = p2->p_fd->fd_rdir;
+
+ if (r1 == NULL)
+ return (r2 == NULL);
+ else if (r2 == NULL)
+ return 1;
+ else
+ return vn_isunder(r1, r2, p2);
+}
+
+/*
+ * Find pathname of process's current directory.
+ *
+ * Use vfs vnode-to-name reverse cache; if that fails, fall back
+ * to reading directory contents.
+ */
+
+int
+sys___getcwd(struct proc *p, void *v, register_t *retval)
+{
+ register struct sys___getcwd_args /* {
+ syscallarg(char *) buf;
+ syscallarg(size_t) len;
+ } */ *uap = v;
+
+ int error;
+ char *path;
+ char *bp, *bend;
+ int len = SCARG(uap, len);
+ int lenused;
+
+ if (len > MAXPATHLEN*4)
+ len = MAXPATHLEN*4;
+ else if (len < 2)
+ return ERANGE;
+
+ path = (char *)malloc(len, M_TEMP, M_WAITOK);
+
+ bp = &path[len];
+ bend = bp;
+ *(--bp) = '\0';
+
+ /*
+ * 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.
+ */
+ error = getcwd_common(p->p_fd->fd_cdir, NULL, &bp, path, len/2,
+ GETCWD_CHECK_ACCESS, p);
+
+ if (error)
+ goto out;
+ lenused = bend - bp;
+ *retval = lenused;
+ /* put the result into user buffer */
+ error = copyout(bp, SCARG(uap, buf), lenused);
+
+out:
+ free(path, M_TEMP);
+ return error;
+}
Index: sys/kern/init_sysent.c
================================================== =================
RCS file: /cvs/src/sys/kern/init_sysent.c,v
retrieving revision 1.93
diff -u -r1.93 init_sysent.c
--- sys/kern/init_sysent.c 30 Dec 2005 04:06:28 -0000 1.93
+++ sys/kern/init_sysent.c 27 Mar 2006 15:40:03 -0000
@@ -1,4 +1,4 @@
-/* $OpenBSD: init_sysent.c,v 1.93 2005/12/30 04:06:28 tedu Exp $ */
+/* $OpenBSD$ */

/*
* System call switch table.
@@ -818,5 +818,7 @@
{ 0, 0,
sys_nosys }, /* 303 = unimplemented */
#endif
+ { 2, s(struct sys___getcwd_args),
+ sys___getcwd }, /* 304 = __getcwd */
};

Index: sys/kern/syscalls.c
================================================== =================
RCS file: /cvs/src/sys/kern/syscalls.c,v
retrieving revision 1.94
diff -u -r1.94 syscalls.c
--- sys/kern/syscalls.c 30 Dec 2005 04:06:28 -0000 1.94
+++ sys/kern/syscalls.c 27 Mar 2006 15:40:03 -0000
@@ -1,4 +1,4 @@
-/* $OpenBSD: syscalls.c,v 1.94 2005/12/30 04:06:28 tedu Exp $ */
+/* $OpenBSD$ */

/*
* System call names.
@@ -421,4 +421,5 @@
"#302 (unimplemented)", /* 302 = unimplemented */
"#303 (unimplemented)", /* 303 = unimplemented */
#endif
+ "__getcwd", /* 304 = __getcwd */
};
Index: sys/kern/syscalls.master
================================================== =================
RCS file: /cvs/src/sys/kern/syscalls.master,v
retrieving revision 1.81
diff -u -r1.81 syscalls.master
--- sys/kern/syscalls.master 30 Dec 2005 04:02:17 -0000 1.81
+++ sys/kern/syscalls.master 27 Mar 2006 15:40:03 -0000
@@ -608,3 +608,4 @@
302 UNIMPL
303 UNIMPL
#endif
+304 STD { int sys___getcwd(char *buf, size_t len); }
Index: lib/libc/shlib_version
================================================== =================
RCS file: /cvs/src/lib/libc/shlib_version,v
retrieving revision 1.96
diff -u -r1.96 shlib_version
@@ -1,4 +1,4 @@
major=39
-minor=0
+minor=1
# note: If changes were made to include/thread_private.h or if system
# calls were added/changed then libpthread must also be updated.
Index: lib/libc/gen/getcwd.c
================================================== =================
RCS file: /cvs/src/lib/libc/gen/getcwd.c,v
retrieving revision 1.14
diff -u -r1.14 getcwd.c
--- lib/libc/gen/getcwd.c 8 Aug 2005 08:05:34 -0000 1.14
+++ lib/libc/gen/getcwd.c 27 Mar 2006 15:40:03 -0000
@@ -1,232 +1,38 @@
-/* $OpenBSD: getcwd.c,v 1.14 2005/08/08 08:05:34 espie Exp $ */
+/* $OpenBSD$ */
+
/*
- * Copyright (c) 1989, 1991, 1993
- * The Regents of the University of California. All rights reserved.
+ * Copyright (c) 2005 Marius Eriksen <marius@openbsd.org>
*
- * 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. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
*
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <sys/param.h>
-#include <sys/stat.h>
-
#include <errno.h>
-#include <dirent.h>
-#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>

-#define ISDOT(dp) \
- (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
- (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
+int __getcwd(char *buf, size_t len);

char *
-getcwd(char *pt, size_t size)
+getcwd(char *buf, size_t size)
{
- struct dirent *dp;
- DIR *dir = NULL;
- dev_t dev;
- ino_t ino;
- int first;
- char *bpt, *bup;
- struct stat s;
- dev_t root_dev;
- ino_t root_ino;
- size_t ptsize, upsize;
- int save_errno;
- char *ept, *eup, *up;
-
- /*
- * If no buffer specified by the user, allocate one as necessary.
- * If a buffer is specified, the size has to be non-zero. The path
- * is built from the end of the buffer backwards.
- */
- if (pt) {
- ptsize = 0;
- if (!size) {
- errno = EINVAL;
- return (NULL);
- }
- ept = pt + size;
- } else {
- if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL)
- return (NULL);
- ept = pt + ptsize;
- }
- bpt = ept - 1;
- *bpt = '\0';
-
- /*
- * Allocate bytes for the string of "../"'s.
- * Should always be enough (it's 340 levels). If it's not, allocate
- * as necessary. Special * case the first stat, it's ".", not "..".
- */
- if ((up = malloc(upsize = MAXPATHLEN)) == NULL)
- goto err;
- eup = up + upsize;
- bup = up;
- up[0] = '.';
- up[1] = '\0';
-
- /* Save root values, so know when to stop. */
- if (stat("/", &s))
- goto err;
- root_dev = s.st_dev;
- root_ino = s.st_ino;
-
- errno = 0; /* XXX readdir has no error return. */
-
- for (first = 1;; first = 0) {
- /* Stat the current level. */
- if (lstat(up, &s))
- goto err;
-
- /* Save current node values. */
- ino = s.st_ino;
- dev = s.st_dev;
-
- /* Check for reaching root. */
- if (root_dev == dev && root_ino == ino) {
- *--bpt = '/';
- /*
- * It's unclear that it's a requirement to copy the
- * path to the beginning of the buffer, but it's always
- * been that way and stuff would probably break.
- */
- memmove(pt, bpt, ept - bpt);
- free(up);
- return (pt);
- }
-
- /*
- * Build pointer to the parent directory, allocating memory
- * as necessary. Max length is 3 for "../", the largest
- * possible component name, plus a trailing NUL.
- */
- if (bup + 3 + MAXNAMLEN + 1 >= eup) {
- char *nup;
-
- if ((nup = realloc(up, upsize *= 2)) == NULL)
- goto err;
- bup = nup + (bup - up);
- up = nup;
- eup = up + upsize;
- }
- *bup++ = '.';
- *bup++ = '.';
- *bup = '\0';
-
- /* Open and stat parent directory. */
- if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
- goto err;
-
- /* Add trailing slash for next directory. */
- *bup++ = '/';
-
- /*
- * If it's a mount point, have to stat each element because
- * the inode number in the directory is for the entry in the
- * parent directory, not the inode number of the mounted file.
- */
- save_errno = 0;
- if (s.st_dev == dev) {
- for (; {
- if (!(dp = readdir(dir)))
- goto notfound;
- if (dp->d_fileno == ino)
- break;
- }
- } else
- for (; {
- if (!(dp = readdir(dir)))
- goto notfound;
- if (ISDOT(dp))
- continue;
- memcpy(bup, dp->d_name, dp->d_namlen + 1);
-
- /* Save the first error for later. */
- if (lstat(up, &s)) {
- if (!save_errno)
- save_errno = errno;
- errno = 0;
- continue;
- }
- if (s.st_dev == dev && s.st_ino == ino)
- break;
- }
-
- /*
- * Check for length of the current name, preceding slash,
- * leading slash.
- */
- if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
- size_t len;
- char *npt;
-
- if (!ptsize) {
- errno = ERANGE;
- goto err;
- }
- len = ept - bpt;
- if ((npt = realloc(pt, ptsize *= 2)) == NULL)
- goto err;
- bpt = npt + (bpt - pt);
- pt = npt;
- ept = pt + ptsize;
- memmove(ept - len, bpt, len);
- bpt = ept - len;
- }
- if (!first)
- *--bpt = '/';
- bpt -= dp->d_namlen;
- memcpy(bpt, dp->d_name, dp->d_namlen);
- (void)closedir(dir);
-
- /* Truncate any file name. */
- *bup = '\0';
+ if (buf != NULL && size == 0) {
+ errno = EINVAL;
+ return (NULL);
}

-notfound:
- /*
- * If readdir set errno, use it, not any saved error; otherwise,
- * didn't find the current directory in its parent directory, set
- * errno to ENOENT.
- */
- if (!errno)
- errno = save_errno ? save_errno : ENOENT;
- /* FALLTHROUGH */
-err:
- save_errno = errno;
-
- if (ptsize)
- free(pt);
- free(up);
- if (dir)
- (void)closedir(dir);
-
- errno = save_errno;
+ if (buf == NULL &&
+ (buf = malloc(size = MAXPATHLEN)) == NULL)
+ return (NULL);

- return (NULL);
+ return (__getcwd(buf, size) < 0 ? NULL : buf);
}
Index: lib/libc/sys/Makefile.inc
================================================== =================
RCS file: /cvs/src/lib/libc/sys/Makefile.inc,v
retrieving revision 1.76
diff -u -r1.76 Makefile.inc
--- lib/libc/sys/Makefile.inc 13 Mar 2006 19:00:58 -0000 1.76
+++ lib/libc/sys/Makefile.inc 27 Mar 2006 15:40:04 -0000
@@ -57,7 +57,7 @@
sigaltstack.o socket.o socketpair.o stat.o statfs.o swapctl.o \
symlink.o sync.o sysarch.o umask.o unlink.o unmount.o \
utimes.o vadvise.o wait4.o write.o writev.o xfspioctl.o __semctl.o \
- __syscall.o __sysctl.o
+ __syscall.o __sysctl.o __getcwd.o

GASM= ${ASM:.o=.go}
PASM= ${ASM:.o=.po}
Index: lib/libpthread/shlib_version
================================================== =================
RCS file: /cvs/src/lib/libpthread/shlib_version,v
retrieving revision 1.17
diff -u -r1.17 shlib_version
@@ -1,2 +1,2 @@
major=6
-minor=2
+minor=3
Index: sys/compat/linux/linux_getcwd.c
================================================== =================
RCS file: /cvs/src/sys/compat/linux/linux_getcwd.c,v
retrieving revision 1.7
diff -u -r1.7 linux_getcwd.c
--- sys/compat/linux/linux_getcwd.c 18 Jun 2005 18:09:42 -0000 1.7
+++ sys/compat/linux/linux_getcwd.c 27 Mar 2006 15:40:04 -0000
@@ -47,7 +47,6 @@
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/proc.h>
-int proc_isunder(struct proc *, struct proc*); /* missing from proc.h */
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/dirent.h>
@@ -73,9 +72,6 @@
linux_getcwd_common(struct vnode *, struct vnode *,
char **, char *, int, int, struct proc *);

-static int
-linux_vn_isunder(struct vnode *, struct vnode *, struct proc *);
-
#define DIRENT_MINSIZE (sizeof(struct dirent) - (LINUX_MAXNAMLEN+1) + 4)

/*
@@ -489,51 +485,6 @@
vput(lvp);
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.
- */
-static int
-linux_vn_isunder(lvp, rvp, p)
- struct vnode *lvp;
- struct vnode *rvp;
- struct proc *p;
-{
- int error;
-
- error = linux_getcwd_common (lvp, rvp, NULL, NULL, MAXPATHLEN/2, 0, p);
-
- if (!error)
- return 1;
- else
- return 0;
-}
-
-/*
- * 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.
- */
-
-int proc_isunder (p1, p2)
- struct proc *p1;
- struct proc *p2;
-{
- struct vnode *r1 = p1->p_fd->fd_rdir;
- struct vnode *r2 = p2->p_fd->fd_rdir;
-
- if (r1 == NULL)
- return (r2 == NULL);
- else if (r2 == NULL)
- return 1;
- else
- return linux_vn_isunder(r1, r2, p2);
}

/*
Index: sys/conf/files
================================================== =================
RCS file: /cvs/src/sys/conf/files,v
retrieving revision 1.368
diff -u -r1.368 files
--- sys/conf/files 21 Mar 2006 18:40:54 -0000 1.368
+++ sys/conf/files 27 Mar 2006 15:40:04 -0000
@@ -667,6 +667,7 @@
file kern/vfs_sync.c
file kern/vfs_syscalls.c
file kern/vfs_vnops.c
+file kern/vfs_getcwd.c
file kern/vnode_if.c
file miscfs/deadfs/dead_vnops.c
file miscfs/fifofs/fifo_vnops.c fifo
Index: sys/sys/proc.h
================================================== =================
RCS file: /cvs/src/sys/sys/proc.h,v
retrieving revision 1.84
diff -u -r1.84 proc.h
--- sys/sys/proc.h 22 Dec 2005 06:55:03 -0000 1.84
+++ sys/sys/proc.h 27 Mar 2006 15:40:05 -0000
@@ -470,5 +470,8 @@
#if defined(MULTIPROCESSOR)
void proc_trampoline_mp(void); /* XXX */
#endif
+
+int proc_isunder(struct proc *, struct proc *);
+
#endif /* _KERNEL */
#endif /* !_SYS_PROC_H_ */
Index: sys/sys/syscall.h
================================================== =================
RCS file: /cvs/src/sys/sys/syscall.h,v
retrieving revision 1.92
diff -u -r1.92 syscall.h
--- sys/sys/syscall.h 30 Dec 2005 04:06:28 -0000 1.92
+++ sys/sys/syscall.h 27 Mar 2006 15:40:05 -0000
@@ -1,4 +1,4 @@
-/* $OpenBSD: syscall.h,v 1.92 2005/12/30 04:06:28 tedu Exp $ */
+/* $OpenBSD$ */

/*
* System call numbers.
@@ -688,4 +688,7 @@
/* syscall: "thrsigdivert" ret: "int" args: "sigset_t" */
#define SYS_thrsigdivert 303

-#define SYS_MAXSYSCALL 304
+/* syscall: "__getcwd" ret: "int" args: "char *" "size_t" */
+#define SYS___getcwd 304
+
+#define SYS_MAXSYSCALL 305
Index: sys/sys/syscallargs.h
================================================== =================
RCS file: /cvs/src/sys/sys/syscallargs.h,v
retrieving revision 1.94
diff -u -r1.94 syscallargs.h
--- sys/sys/syscallargs.h 30 Dec 2005 04:06:28 -0000 1.94
+++ sys/sys/syscallargs.h 27 Mar 2006 15:40:05 -0000
@@ -1,4 +1,4 @@
-/* $OpenBSD: syscallargs.h,v 1.94 2005/12/30 04:06:28 tedu Exp $ */
+/* $OpenBSD$ */

/*
* System call argument lists.
@@ -1236,6 +1236,11 @@
syscallarg(sigset_t) sigmask;
};

+struct sys___getcwd_args {
+ syscallarg(char *) buf;
+ syscallarg(size_t) len;
+};
+
/*
* System call prototypes.
*/
@@ -1553,3 +1558,4 @@
int sys_thrsigdivert(struct proc *, void *, register_t *);
#else
#endif
+int sys___getcwd(struct proc *, void *, register_t *);
Index: sys/compat/netbsd/netbsd_getcwd.c
================================================== =================
RCS file: /cvs/src/sys/compat/netbsd/netbsd_getcwd.c,v
retrieving revision 1.9
diff -u -r1.9 netbsd_getcwd.c
--- sys/compat/netbsd/netbsd_getcwd.c 18 Jun 2005 18:09:42 -0000 1.9
+++ sys/compat/netbsd/netbsd_getcwd.c 27 Mar 2006 15:40:05 -0000
@@ -47,7 +47,6 @@
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/proc.h>
-int proc_isunder(struct proc *, struct proc*); /* missing from proc.h */
#include <sys/uio.h>
#include <sys/malloc.h>
#include <sys/dirent.h>
@@ -67,9 +66,6 @@
netbsd_getcwd_common(struct vnode *, struct vnode *,
char **, char *, int, int, struct proc *);

-static int
-netbsd_vn_isunder(struct vnode *, struct vnode *, struct proc *);
-
#define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)

/*
@@ -483,51 +479,6 @@
vput(lvp);
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.
- */
-static int
-netbsd_vn_isunder(lvp, rvp, p)
- struct vnode *lvp;
- struct vnode *rvp;
- struct proc *p;
-{
- int error;
-
- error = netbsd_getcwd_common (lvp, rvp, NULL, NULL, MAXPATHLEN/2, 0, p);
-
- if (!error)
- return 1;
- else
- return 0;
-}
-
-/*
- * 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.
- */
-
-int proc_isunder (p1, p2)
- struct proc *p1;
- struct proc *p2;
-{
- struct vnode *r1 = p1->p_fd->fd_rdir;
- struct vnode *r2 = p2->p_fd->fd_rdir;
-
- if (r1 == NULL)
- return (r2 == NULL);
- else if (r2 == NULL)
- return 1;
- else
- return netbsd_vn_isunder(r1, r2, p2);
}

/*

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:34 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com