Module Name: src Committed By: christos Date: Sun Sep 15 16:25:58 UTC 2019
Modified Files: src/lib/libc/sys: fcntl.2 src/sys/kern: sys_descrip.c src/sys/sys: fcntl.h src/tests/kernel: Makefile Added Files: src/tests/kernel: t_fcntl.c Log Message: Add F_GETPATH, presented to tech-kern. To generate a diff of this commit: cvs rdiff -u -r1.41 -r1.42 src/lib/libc/sys/fcntl.2 cvs rdiff -u -r1.34 -r1.35 src/sys/kern/sys_descrip.c cvs rdiff -u -r1.50 -r1.51 src/sys/sys/fcntl.h cvs rdiff -u -r1.61 -r1.62 src/tests/kernel/Makefile cvs rdiff -u -r0 -r1.1 src/tests/kernel/t_fcntl.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/lib/libc/sys/fcntl.2 diff -u src/lib/libc/sys/fcntl.2:1.41 src/lib/libc/sys/fcntl.2:1.42 --- src/lib/libc/sys/fcntl.2:1.41 Sat Dec 28 15:03:22 2013 +++ src/lib/libc/sys/fcntl.2 Sun Sep 15 12:25:58 2019 @@ -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 @@ flag is set in the file descriptor. 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 @@ or an 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 Index: src/sys/kern/sys_descrip.c diff -u src/sys/kern/sys_descrip.c:1.34 src/sys/kern/sys_descrip.c:1.35 --- src/sys/kern/sys_descrip.c:1.34 Mon Aug 26 06:19:08 2019 +++ src/sys/kern/sys_descrip.c Sun Sep 15 12:25:57 2019 @@ -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 @@ do_fcntl_lock(int fd, int cmd, struct fl 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 @@ do_fcntl_lock(int fd, int cmd, struct fl 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 @@ sys_fcntl(struct lwp *l, const struct sy } 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 @@ sys_fcntl(struct lwp *l, const struct sy 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 @@ sys_flock(struct lwp *l, const struct sy 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; Index: src/sys/sys/fcntl.h diff -u src/sys/sys/fcntl.h:1.50 src/sys/sys/fcntl.h:1.51 --- src/sys/sys/fcntl.h:1.50 Tue Feb 20 13:20:05 2018 +++ src/sys/sys/fcntl.h Sun Sep 15 12:25:57 2019 @@ -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) */ Index: src/tests/kernel/Makefile diff -u src/tests/kernel/Makefile:1.61 src/tests/kernel/Makefile:1.62 --- src/tests/kernel/Makefile:1.61 Thu Aug 15 04:46:09 2019 +++ src/tests/kernel/Makefile Sun Sep 15 12:25:58 2019 @@ -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 @@ NOMAN= # defined 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 Added files: Index: src/tests/kernel/t_fcntl.c diff -u /dev/null src/tests/kernel/t_fcntl.c:1.1 --- /dev/null Sun Sep 15 12:25:58 2019 +++ src/tests/kernel/t_fcntl.c Sun Sep 15 12:25:58 2019 @@ -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(); +}