Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/fstat Add memfd_create(2) from GSoC 2023 by Theodore...



details:   https://anonhg.NetBSD.org/src/rev/7a5610292ad9
branches:  trunk
changeset: 377381:7a5610292ad9
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Jul 10 02:31:54 2023 +0000

description:
Add memfd_create(2) from GSoC 2023 by Theodore Preduta

diffstat:

 distrib/sets/lists/comp/mi                  |    5 +-
 lib/libc/sys/Makefile.inc                   |    7 +-
 lib/libc/sys/fcntl.2                        |  120 +++++++-
 lib/libc/sys/memfd_create.2                 |  125 ++++++++
 sys/compat/linux/arch/amd64/syscalls.master |  143 +++++++++-
 sys/compat/linux/common/linux_fcntl.h       |    4 +-
 sys/compat/linux/common/linux_file.c        |   12 +-
 sys/compat/linux/common/linux_misc.c        |   71 ++++-
 sys/compat/linux/common/linux_sysctl.c      |    8 +-
 sys/kern/sys_descrip.c                      |   50 +-
 sys/kern/sys_memfd.c                        |  408 ++++++++++++++++++++++++++++
 sys/kern/syscalls.master                    |    4 +-
 sys/kern/vfs_syscalls.c                     |   43 +-
 sys/kern/vfs_vnops.c                        |   33 ++-
 sys/sys/fcntl.h                             |   13 +-
 sys/sys/file.h                              |    8 +-
 sys/sys/mman.h                              |   29 +-
 usr.bin/fstat/fstat.c                       |    5 +-
 usr.bin/fstat/misc.c                        |   47 +++-
 19 files changed, 1043 insertions(+), 92 deletions(-)

diffs (truncated from 1611 to 300 lines):

diff -r cc254a4eccb8 -r 7a5610292ad9 distrib/sets/lists/comp/mi
--- a/distrib/sets/lists/comp/mi        Sun Jul 09 20:24:06 2023 +0000
+++ b/distrib/sets/lists/comp/mi        Mon Jul 10 02:31:54 2023 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: mi,v 1.2436 2023/07/08 17:43:13 christos Exp $
+#      $NetBSD: mi,v 1.2437 2023/07/10 02:31:54 christos Exp $
 #
 # Note: don't delete entries from here - mark them as "obsolete" instead.
 ./etc/mtree/set.comp                           comp-sys-root
@@ -4826,6 +4826,7 @@
 ./usr/share/man/cat2/lutimes.0                 comp-c-catman           .cat
 ./usr/share/man/cat2/m68k_sync_icache.0                comp-c-catman           .cat
 ./usr/share/man/cat2/madvise.0                 comp-c-catman           .cat
+./usr/share/man/cat2/memfd_create.0            comp-c-catman           .cat
 ./usr/share/man/cat2/mincore.0                 comp-c-catman           .cat
 ./usr/share/man/cat2/minherit.0                        comp-c-catman           .cat
 ./usr/share/man/cat2/mkdir.0                   comp-c-catman           .cat
@@ -13315,6 +13316,7 @@
 ./usr/share/man/html2/lutimes.html             comp-c-htmlman          html
 ./usr/share/man/html2/m68k_sync_icache.html    comp-c-htmlman          html
 ./usr/share/man/html2/madvise.html             comp-c-htmlman          html
+./usr/share/man/html2/memfd_create.html                comp-c-htmlman          html
 ./usr/share/man/html2/mincore.html             comp-c-htmlman          html
 ./usr/share/man/html2/minherit.html            comp-c-htmlman          html
 ./usr/share/man/html2/mkdir.html               comp-c-htmlman          html
@@ -21632,6 +21634,7 @@
 ./usr/share/man/man2/lutimes.2                 comp-c-man              .man
 ./usr/share/man/man2/m68k_sync_icache.2                comp-c-man              .man
 ./usr/share/man/man2/madvise.2                 comp-c-man              .man
+./usr/share/man/man2/memfd_create.2            comp-c-man              .man
 ./usr/share/man/man2/mincore.2                 comp-c-man              .man
 ./usr/share/man/man2/minherit.2                        comp-c-man              .man
 ./usr/share/man/man2/mkdir.2                   comp-c-man              .man
diff -r cc254a4eccb8 -r 7a5610292ad9 lib/libc/sys/Makefile.inc
--- a/lib/libc/sys/Makefile.inc Sun Jul 09 20:24:06 2023 +0000
+++ b/lib/libc/sys/Makefile.inc Mon Jul 10 02:31:54 2023 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile.inc,v 1.250 2021/11/01 05:53:45 thorpej Exp $
+#      $NetBSD: Makefile.inc,v 1.251 2023/07/10 02:31:54 christos Exp $
 #      @(#)Makefile.inc        8.3 (Berkeley) 10/24/94
 
 # sys sources
@@ -134,7 +134,8 @@ ASM=\
                _lwp_unpark_all.S _lwp_suspend.S _lwp_continue.S \
                _lwp_wakeup.S _lwp_detach.S _lwp_setprivate.S \
                _lwp_setname.S _lwp_getname.S _lwp_ctl.S \
-       madvise.S mincore.S minherit.S mkdir.S mkdirat.S mkfifo.S mkfifoat.S \
+       madvise.S memfd_create.S mincore.S minherit.S mkdir.S mkdirat.S \
+               mkfifo.S mkfifoat.S \
                __mknod50.S mlock.S mlockall.S modctl.S __mount50.S \
                mprotect.S __msgctl50.S msgget.S munlock.S munlockall.S \
                munmap.S \
@@ -275,7 +276,7 @@ MAN+=       accept.2 access.2 acct.2 adjtime.2
        _lwp_suspend.2 _lwp_wakeup.2 _lwp_wait.2 _lwp_kill.2 \
        _lwp_getname.2 _lwp_getprivate.2 \
        _lwp_park.2 _lwp_unpark.2 _lwp_unpark_all.2 \
-       mkdir.2 mkfifo.2 mknod.2 \
+       memfd_create.2 mkdir.2 mkfifo.2 mknod.2 \
        madvise.2 mincore.2 minherit.2 mlock.2 mlockall.2 mmap.2 modctl.2 \
        mount.2 \
        mprotect.2 mremap.2 msgctl.2 msgget.2 msgrcv.2 msgsnd.2 msync.2 \
diff -r cc254a4eccb8 -r 7a5610292ad9 lib/libc/sys/fcntl.2
--- a/lib/libc/sys/fcntl.2      Sun Jul 09 20:24:06 2023 +0000
+++ b/lib/libc/sys/fcntl.2      Mon Jul 10 02:31:54 2023 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: fcntl.2,v 1.49 2022/12/04 19:01:19 uwe Exp $
+.\"    $NetBSD: fcntl.2,v 1.50 2023/07/10 02:31:54 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 September 26, 2019
+.Dd July 5, 2023
 .Dt FCNTL 2
 .Os
 .Sh NAME
@@ -162,6 +162,24 @@ in the buffer pointed to by
 .Fa arg
 should be pointing to a buffer of at least
 .Dv MAXPATHLEN .
+.It Dv F_ADD_SEALS
+Add seals specified in
+.Fa arg
+to
+.Fa fd
+to restrict possible operations on
+.Fa fd
+as described below.
+Like flags, multiple seals can be specified at once.
+Additionally, specifying seals that are already associated with
+.Fa fd
+is a no-op.
+.It Dv F_GET_SEALS
+Get the seals currently associated with
+.Fa fd
+as described below
+.Fa ( arg
+is ignored).
 .El
 .Pp
 The set of valid flags for the
@@ -324,13 +342,44 @@ 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.
+.Pp
+Possible seals are:
+.Bl -tag -width F_SEAL_FUTURE_WRITE
+.It Dv F_SEAL_SEAL
+Prevent any further seals from being added to
+.Fa fd .
+.It Dv F_SEAL_SHRINK
+Prevent the size of
+.Fa fd
+from decreasing.
+.It Dv F_SEAL_GROW
+Prevent the size of
+.Fa fd
+from increasing.
+.It Dv F_SEAL_WRITE
+Prevent any write operations to
+.Fa fd .
+.Dv F_SEAL_WRITE
+cannot be applied if
+.Fa fd
+has any memory mappings.
+.It Dv F_SEAL_FUTURE_WRITE
+Like
+.Dv F_SEAL_WRITE
+but allow any current memory mappings of
+.Fa fd
+to remain open, including those with
+.Dv PROT_WRITE .
+.El
 .Sh NOTES
-The
-.Dv F_GETPATH
-functionality is implemented using the reverse
+For
+.Dv F_GETPATH :
+.Bl -bullet -compact
+.It
+For vnodes, functionality is implemented using the reverse
 .Xr namei 9
 cache.
-The implications of this are:
+The implications of this are
 .Bl -bullet -compact
 .It
 For hard links where the file descriptor can resolve to multiple pathnames,
@@ -341,16 +390,25 @@ may fail if the corresponding entry has 
 .Xr namei 9
 cache and return
 .Er ENOENT .
+.El
 .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.
+For a file descriptor created by
+.Xr memfd_create 2 ,
+the name provided at
+.Fa fd
+creation, with the prefix
+.Dq memfd:
+is used.
+.It
+Other types of file descriptors 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
 as follows:
-.Bl -tag -width F_GETOWNX -offset indent
+.Bl -tag -width F_GET_SEALS -offset indent
 .It Dv F_DUPFD
 A new file descriptor.
 .It Dv F_GETFD
@@ -361,6 +419,9 @@ Value of flags.
 Value of file descriptor owner.
 .It Dv F_MAXFD
 Value of the highest file descriptor open by the process.
+.It Dv F_GET_SEALS
+Value of the seals currently associated with
+.Fa fd .
 .It other
 Value other than \-1.
 .El
@@ -473,6 +534,18 @@ is an exclusive lock
 and
 .Fa fildes
 is not a valid file descriptor open for writing.
+.It Bq Er EBUSY
+The argument
+.Fa cmd
+is
+.Dv F_ADD_SEALS ,
+.Fa arg
+contains
+.Dv F_SEAL_WRITE
+and
+.Fa fd
+is currently mapped by
+.Xr mmap 2 .
 .It Bq Er EDEADLK
 The argument
 .Fa cmd
@@ -512,6 +585,24 @@ and the data to which
 points is not valid, or
 .Fa fildes
 refers to a file that does not support locking.
+.Pp
+The argument
+.Fa cmd
+is
+.Dv F_ADD_SEALS
+or
+.Dv F_GET_SEALS
+and
+.Fa fd
+does not support seals.
+.Pp
+The argument
+.Fa cmd
+is
+.Dv F_ADD_SEALS
+and
+.Fa arg
+contains set bits for unsupported seals.
 .It Bq Er EMFILE
 The argument
 .Fa cmd
@@ -562,6 +653,15 @@ has been reached.
 It can be modified using the
 .Li kern.maxfiles
 .Xr sysctl 7 .
+.It Bq Er EPERM
+The argument
+.Fa cmd
+is
+.Dv F_ADD_SEALS
+and
+.Fa fd
+already has
+.Dv F_SEAL_SEAL .
 .It Bq Er ERANGE
 The argument
 .Fa cmd
diff -r cc254a4eccb8 -r 7a5610292ad9 lib/libc/sys/memfd_create.2
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libc/sys/memfd_create.2       Mon Jul 10 02:31:54 2023 +0000
@@ -0,0 +1,125 @@
+.\"    $NetBSD: memfd_create.2,v 1.1 2023/07/10 02:31:54 christos Exp $
+.\"
+.\" Copyright (c) 2023 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" This code is derived from software contributed to The NetBSD Foundation
+.\" by Theodore Preduta.
+.\"
+.\" 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.
+.\"
+.Dd July 5, 2023
+.Dt MEMFD_CREATE 2
+.Os
+.Sh NAME
+.Nm memfd_create
+.Nd create anonymous files
+.Sh LIBRARY
+.Lb libc
+.Sh SYNOPSIS
+.In sys/mman.h
+.Ft int
+.Fn memfd_create "const char *name" "unsigned int flags"
+.Sh DESCRIPTION
+The



Home | Main Index | Thread Index | Old Index