Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/kernel Add F_GETPATH, presented to tech-kern.



details:   https://anonhg.NetBSD.org/src/rev/028c8c77432b
branches:  trunk
changeset: 965403:028c8c77432b
user:      christos <christos%NetBSD.org@localhost>
date:      Sun Sep 15 16:25:57 2019 +0000

description:
Add F_GETPATH, presented to tech-kern.

diffstat:

 lib/libc/sys/fcntl.2   |  34 +++++++++++++++++-
 sys/kern/sys_descrip.c |  49 +++++++++++++++++---------
 sys/sys/fcntl.h        |   3 +-
 tests/kernel/Makefile  |   5 +-
 tests/kernel/t_fcntl.c |  89 ++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 158 insertions(+), 22 deletions(-)

diffs (290 lines):

diff -r 0120727ca443 -r 028c8c77432b lib/libc/sys/fcntl.2
--- a/lib/libc/sys/fcntl.2      Sun Sep 15 16:16:36 2019 +0000
+++ b/lib/libc/sys/fcntl.2      Sun Sep 15 16:25:57 2019 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: fcntl.2,v 1.41 2013/12/28 20:03:22 dholland Exp $
+.\"    $NetBSD: fcntl.2,v 1.42 2019/09/15 16:25:58 christos Exp $
 .\"
 .\" Copyright (c) 1983, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)fcntl.2    8.2 (Berkeley) 1/12/94
 .\"
-.Dd January 23, 2012
+.Dd September 15, 2019
 .Dt FCNTL 2
 .Os
 .Sh NAME
@@ -154,6 +154,14 @@
 Set or clear the
 .Dv O_NOSIGPIPE
 in the file descriptor.
+.It Dv F_GETPATH
+Place a pathname corresponding to 
+.Fa fd
+in the buffer pointed by
+.Fa arg .
+.Fa arg
+should be pointing to a buffer of at least
+.Fa MAXPATHLEN .
 .El
 .Pp
 The set of valid flags for the
@@ -316,6 +324,28 @@
 request fails or blocks respectively when another process has existing
 locks on bytes in the specified region and the type of any of those
 locks conflicts with the type specified in the request.
+.Sh NOTES
+Tbe
+.Dv F_GETPATH
+functionality is implemented using the reverse
+.Xr namei 9
+cache.
+The implications of this are: 
+.Bl -bullet -compact
+.It 
+For hard links where the file descriptor can resolve to multiple pathnames,
+the first entry found in the cache is returned.
+.It
+.Dv F_GETPATH
+may fail if the corresponding entry has been evicted from the LRU
+.Xr namei 9
+cache and return
+.Dv ENOENT .
+.It
+File descriptors that don't point to vnodes are not handled, as
+well as symbolic links since there is currently no way to obtain
+a file descriptor pointing to a symbolic link.
+.El
 .Sh RETURN VALUES
 Upon successful completion, the value returned depends on
 .Fa cmd
diff -r 0120727ca443 -r 028c8c77432b sys/kern/sys_descrip.c
--- a/sys/kern/sys_descrip.c    Sun Sep 15 16:16:36 2019 +0000
+++ b/sys/kern/sys_descrip.c    Sun Sep 15 16:25:57 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sys_descrip.c,v 1.34 2019/08/26 10:19:08 maxv Exp $    */
+/*     $NetBSD: sys_descrip.c,v 1.35 2019/09/15 16:25:57 christos Exp $        */
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.34 2019/08/26 10:19:08 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_descrip.c,v 1.35 2019/09/15 16:25:57 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -239,12 +239,9 @@
        proc_t *p;
        int error, flg;
 
-       if ((fp = fd_getfile(fd)) == NULL)
-               return EBADF;
-       if (fp->f_type != DTYPE_VNODE) {
-               fd_putfile(fd);
-               return EINVAL;
-       }
+       if ((error = fd_getvnode(fd, &fp)) != 0)
+               return error;
+
        vp = fp->f_vnode;
        if (fl->l_whence == SEEK_CUR)
                fl->l_start += fp->f_offset;
@@ -315,6 +312,26 @@
        return error;
 }
 
+static int
+do_fcntl_getpath(struct lwp *l, file_t *fp, char *upath)
+{
+       char *kpath;
+       int error;
+
+       if (fp->f_type != DTYPE_VNODE)
+               return EOPNOTSUPP;
+
+       kpath = PNBUF_GET();
+
+       error = vnode_to_path(kpath, MAXPATHLEN, fp->f_vnode, l, l->l_proc);
+       if (!error)
+               error = copyoutstr(kpath, upath, MAXPATHLEN, NULL);
+
+       PNBUF_PUT(kpath);
+
+       return error;
+}
+       
 /*
  * The file control system call.
  */
@@ -371,7 +388,7 @@
        }
 
        if ((fp = fd_getfile(fd)) == NULL)
-               return (EBADF);
+               return EBADF;
 
        if ((cmd & F_FSCTL)) {
                error = fcntl_forfs(fd, fp, cmd, SCARG(uap, arg));
@@ -463,6 +480,10 @@
                error = (*fp->f_ops->fo_ioctl)(fp, FIOSETOWN, &tmp);
                break;
 
+       case F_GETPATH:
+               error = do_fcntl_getpath(l, fp, SCARG(uap, arg));
+               break;
+
        default:
                error = EINVAL;
        }
@@ -606,15 +627,9 @@
 
        fd = SCARG(uap, fd);
        how = SCARG(uap, how);
-       error = 0;
 
-       if ((fp = fd_getfile(fd)) == NULL) {
-               return EBADF;
-       }
-       if (fp->f_type != DTYPE_VNODE) {
-               fd_putfile(fd);
-               return EOPNOTSUPP;
-       }
+       if ((error = fd_getvnode(fd, &fp)) != 0)
+               return error == EINVAL ? EOPNOTSUPP : error;
 
        vp = fp->f_vnode;
        lf.l_whence = SEEK_SET;
diff -r 0120727ca443 -r 028c8c77432b sys/sys/fcntl.h
--- a/sys/sys/fcntl.h   Sun Sep 15 16:16:36 2019 +0000
+++ b/sys/sys/fcntl.h   Sun Sep 15 16:25:57 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fcntl.h,v 1.50 2018/02/20 18:20:05 kamil Exp $ */
+/*     $NetBSD: fcntl.h,v 1.51 2019/09/15 16:25:57 christos Exp $      */
 
 /*-
  * Copyright (c) 1983, 1990, 1993
@@ -193,6 +193,7 @@
 #define        F_DUPFD_CLOEXEC 12              /* close on exec duplicated fd */
 #define        F_GETNOSIGPIPE  13              /* get SIGPIPE disposition */
 #define        F_SETNOSIGPIPE  14              /* set SIGPIPE disposition */
+#define        F_GETPATH       15              /* get pathname assosiated with fd */
 #endif
 
 /* file descriptor flags (F_GETFD, F_SETFD) */
diff -r 0120727ca443 -r 028c8c77432b tests/kernel/Makefile
--- a/tests/kernel/Makefile     Sun Sep 15 16:16:36 2019 +0000
+++ b/tests/kernel/Makefile     Sun Sep 15 16:25:57 2019 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.61 2019/08/15 08:46:09 kamil Exp $
+# $NetBSD: Makefile,v 1.62 2019/09/15 16:25:58 christos Exp $
 
 NOMAN=         # defined
 
@@ -7,7 +7,8 @@
 TESTSDIR=      ${TESTSBASE}/kernel
 
 TESTS_SUBDIRS+=        kqueue
-TESTS_C=       t_lock
+TESTS_C=       t_fcntl
+TESTS_C+=      t_lock
 TESTS_C+=      t_lockf
 TESTS_C+=      t_pty
 TESTS_C+=      t_mqueue
diff -r 0120727ca443 -r 028c8c77432b tests/kernel/t_fcntl.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/kernel/t_fcntl.c    Sun Sep 15 16:25:57 2019 +0000
@@ -0,0 +1,89 @@
+/*     $NetBSD: t_fcntl.c,v 1.1 2019/09/15 16:25:58 christos Exp $     */
+
+/*-
+ * Copyright (c) 2019 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * 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/types.h>
+#include <atf-c.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+ATF_TC(getpath);
+ATF_TC_HEAD(getpath, tc)
+{  
+
+       atf_tc_set_md_var(tc, "descr", "Checks fcntl(2) F_GETPATH");
+}
+
+static const struct {
+       const char *name;
+       int rv;
+} files[] = {
+       { "/bin/ls", 0 },
+       { "/bin/sh", 0 },
+       { "/dev/zero", 0 },
+       { "/dev/null", 0 },
+       { "/bin/chgrp", 0 },
+       { "/", ENOENT },
+};
+
+ATF_TC_BODY(getpath, tc)
+{
+       char path[MAXPATHLEN];
+       int fd, rv;
+
+       for (size_t i = 0; i < __arraycount(files); i++) {
+               fd = open(files[i].name, O_RDONLY|O_NOFOLLOW);
+               ATF_REQUIRE(fd != -1);
+               rv = fcntl(fd, F_GETPATH, path);
+               if (files[i].rv) {
+                       ATF_REQUIRE_MSG(errno == files[i].rv,
+                           "Unexpected error %d != %d for `%s'", errno,
+                           files[i].rv, files[i].name); 
+               } else {
+                       ATF_REQUIRE_MSG(rv != -1,
+                           "Can't get path for `%s' (%s)", files[i].name,
+                           strerror(errno));
+                       ATF_REQUIRE_MSG(strcmp(files[i].name, path) == 0,
+                           "Bad name `%s' != `%s'", path, files[i].name);
+               close(fd);
+               }
+       }
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+       ATF_TP_ADD_TC(tp, getpath); 
+
+       return atf_no_error();
+}



Home | Main Index | Thread Index | Old Index