]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Linux: Add memfd_create system call wrapper
authorFlorian Weimer <fweimer@redhat.com>
Thu, 23 Nov 2017 09:00:40 +0000 (10:00 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Thu, 23 Nov 2017 09:00:40 +0000 (10:00 +0100)
The system call is somewhat obscure because it is closely related
to file descriptor sealing.  However, it is also the recommended
way to create alias mappings, which is why it has more general use.

No emulation is provided.  Except for the name of the
/proc/self/fd links, it would be possible to implement an
approximation using O_TMPFILE and tmpfs, but this does not appear
to be worth the added complexity.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Reviewed-by: Christian Brauner <christian.brauner@ubuntu.com>
36 files changed:
ChangeLog
NEWS
manual/llio.texi
sysdeps/unix/sysv/linux/Makefile
sysdeps/unix/sysv/linux/Versions
sysdeps/unix/sysv/linux/aarch64/libc.abilist
sysdeps/unix/sysv/linux/alpha/libc.abilist
sysdeps/unix/sysv/linux/arm/libc.abilist
sysdeps/unix/sysv/linux/bits/mman-linux.h
sysdeps/unix/sysv/linux/hppa/libc.abilist
sysdeps/unix/sysv/linux/i386/libc.abilist
sysdeps/unix/sysv/linux/ia64/libc.abilist
sysdeps/unix/sysv/linux/m68k/coldfire/libc.abilist
sysdeps/unix/sysv/linux/m68k/m680x0/libc.abilist
sysdeps/unix/sysv/linux/microblaze/libc.abilist
sysdeps/unix/sysv/linux/mips/mips32/fpu/libc.abilist
sysdeps/unix/sysv/linux/mips/mips32/nofpu/libc.abilist
sysdeps/unix/sysv/linux/mips/mips64/n32/libc.abilist
sysdeps/unix/sysv/linux/mips/mips64/n64/libc.abilist
sysdeps/unix/sysv/linux/nios2/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libc.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc64/libc-le.abilist
sysdeps/unix/sysv/linux/powerpc/powerpc64/libc.abilist
sysdeps/unix/sysv/linux/s390/s390-32/libc.abilist
sysdeps/unix/sysv/linux/s390/s390-64/libc.abilist
sysdeps/unix/sysv/linux/sh/libc.abilist
sysdeps/unix/sysv/linux/sparc/sparc32/libc.abilist
sysdeps/unix/sysv/linux/sparc/sparc64/libc.abilist
sysdeps/unix/sysv/linux/syscalls.list
sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libc.abilist
sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libc.abilist
sysdeps/unix/sysv/linux/tile/tilepro/libc.abilist
sysdeps/unix/sysv/linux/tst-memfd_create.c [new file with mode: 0644]
sysdeps/unix/sysv/linux/x86_64/64/libc.abilist
sysdeps/unix/sysv/linux/x86_64/x32/libc.abilist

index a84f970761c0f27a599b0099804fb52f37bbf1ed..04fac09f2d27964c81b84b4e8b9909ed937a6f8d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2017-11-23  Florian Weimer  <fweimer@redhat.com>
+
+       Linux: Add memfd_create system call wrapper
+       * sysdeps/unix/sysv/linux/Makefile [misc] (tests): Add
+       tst-memfd_create.
+       * sysdeps/unix/sysv/linux/bits/mman-linux.h [__USE_GNU]
+       (MFD_CLOEXEC, MFD_ALLOW_SEALING): Define.
+       [__USE_GNU] (memfd_create): Declare.
+       * sysdeps/unix/sysv/linux/Versions (GLIBC_2.27): Add memfd_create.
+       * sysdeps/unix/sysv/linux/syscalls.list (memfd_create): Add.
+       * sysdeps/unix/sysv/linux/tst-memfd_create.c: New file.
+       * sysdeps/unix/sysv/linux/**.abilist: Update.
+       * manual/llio.texi (Memory-mapped I/O): Document memfd_create.
+
 2017-11-22  Joseph Myers  <joseph@codesourcery.com>
 
        * localedata/gen-locale.sh: Fix typo in variable name.
diff --git a/NEWS b/NEWS
index 30ff04ea625568ec48c8d6a3d15b1782755eab1b..ab14d1eb1ba4c8e675893b193f64bd8d01f10064 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -35,6 +35,8 @@ Major new features:
   are the same interfaces added in version 2.26 for some platforms where
   this format is supported but is not the format of long double.
 
+* glibc now implements the memfd_create function on Linux.
+
 Deprecated and removed features, and other changes affecting compatibility:
 
 * On GNU/Linux, the obsolete Linux constant PTRACE_SEIZE_DEVEL is no longer
index 41c3e068d1f46e18d6a04cdb2c8e670662c25719..8b2f599c79d2979bbb69c2c468bbbf6d40e5ce3e 100644 (file)
@@ -1801,6 +1801,69 @@ the given @var{name} previously created by @code{shm_open}.
 On failure @code{errno} is set.
 @end deftypefn
 
+@deftypefun int memfd_create (const char *@var{name}, unsigned int @var{flags})
+@standards{Linux, sys/mman.h}
+@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{@acsfd{}}}
+The @code{memfd_create} function returns a file descriptor which can be
+used to create memory mappings using the @code{mmap} function.  It is
+similar to the @code{shm_open} function in the sense that these mappings
+are not backed by actual files.  However, the descriptor returned by
+@code{memfd_create} does not correspond to a named object; the
+@var{name} argument is used for debugging purposes only (e.g., will
+appear in @file{/proc}), and separate invocations of @code{memfd_create}
+with the same @var{name} will not return descriptors for the same region
+of memory.  The descriptor can also be used to create alias mappings
+within the same process.
+
+The descriptor initially refers to a zero-length file.  Before mappings
+can be created which are backed by memory, the file size needs to be
+increased with the @code{ftruncate} function.  @xref{File Size}.
+
+The @var{flags} argument can be a combination of the following flags:
+
+@vtable @code
+@item MFD_CLOEXEC
+@standards{Linux, sys/mman.h}
+The descriptor is created with the @code{O_CLOEXEC} flag.
+
+@item MFD_ALLOW_SEALING
+@standards{Linux, sys/mman.h}
+The descriptor supports the addition of seals using the @code{fcntl}
+function.
+
+@item MFD_HUGETLB
+@standards{Linux, sys/mman.h}
+This requests that mappings created using the returned file descriptor
+use a larger page size.  See @code{MAP_HUGETLB} above for details.
+
+This flag is incompatible with @code{MFD_ALLOW_SEALING}.
+@end vtable
+
+@code{memfd_create} returns a file descriptor on success, and @math{-1}
+on failure.
+
+The following @code{errno} error conditions are defined for this
+function:
+
+@table @code
+@item EINVAL
+An invalid combination is specified in @var{flags}, or @var{name} is
+too long.
+
+@item EFAULT
+The @var{name} argument does not point to a string.
+
+@item EMFILE
+The operation would exceed the file descriptor limit for this process.
+
+@item ENFILE
+The operation would exceed the system-wide file descriptor limit.
+
+@item ENOMEM
+There is not enough memory for the operation.
+@end table
+@end deftypefun
+
 @node Waiting for I/O
 @section Waiting for Input or Output
 @cindex waiting for input or output
index c6675b3aa5da664f580d11f5fa93955620259ae5..c484d2688a773e9f83a95a61ad39cf6c851d8b06 100644 (file)
@@ -44,7 +44,7 @@ sysdep_headers += sys/mount.h sys/acct.h sys/sysctl.h \
 
 tests += tst-clone tst-clone2 tst-clone3 tst-fanotify tst-personality \
         tst-quota tst-sync_file_range tst-sysconf-iov_max tst-ttyname \
-        test-errno-linux
+        test-errno-linux tst-memfd_create
 
 # Generate the list of SYS_* macros for the system calls (__NR_*
 # macros).  The file syscall-names.list contains all possible system
index d3dbcde8c9e51cf7d95126227298215582a23e54..6f2fe516d7830d5687c6d3f6a9572e8f69ecc0d9 100644 (file)
@@ -166,6 +166,9 @@ libc {
   GLIBC_2.15 {
     process_vm_readv; process_vm_writev;
   }
+  GLIBC_2.27 {
+    memfd_create;
+  }
   GLIBC_PRIVATE {
     # functions used in other libraries
     __syscall_rt_sigqueueinfo;
index ed598aedac0ca9dde3756a69bee5d193fab1ce7e..140ca28abc6e7d61837e6c7786d4c17c3e879a6e 100644 (file)
@@ -2106,6 +2106,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
index 4e57f36bcfd238cb51928d8abda539df09e726ee..f698e1b2f401d883ed6a022cb7547a30930b9ade 100644 (file)
@@ -2017,6 +2017,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
index 5b70e1bfc97a569b1f08bc63635e30717b7b4c97..8a8af3e3e43c4bdb38699bd4662eeb25b626e3d7 100644 (file)
@@ -107,6 +107,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.4 GLIBC_2.4 A
 GLIBC_2.4 _Exit F
 GLIBC_2.4 _IO_2_1_stderr_ D 0xa0
index 78c07c890ab10e1413ec2f82457365d6a7ae408f..1ffa5490afe7e432fafacf08abcf97ef5ff4a66e 100644 (file)
 # define MCL_ONFAULT   4               /* Lock all pages that are
                                           faulted in.  */
 #endif
+
+#ifdef __USE_GNU
+/* Flags for memfd_create.  */
+# define MFD_CLOEXEC 1U
+# define MFD_ALLOW_SEALING 2U
+# define MFD_HUGETLB 4U
+
+__BEGIN_DECLS
+
+/* Create a new memory file descriptor.  NAME is a name for debugging.
+   FLAGS is a combination of the MFD_* constants.  */
+int memfd_create (const char *__name, unsigned int __flags) __THROW;
+
+__END_DECLS
+
+#endif /* __USE_GNU */
index 6a2500a8b3fcd35f76dbcc4956412cdb80705750..5b81a6cd7d8cb5a207dda64a5bdd7e912d0877a3 100644 (file)
@@ -1871,6 +1871,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
index 9ab4e3642a12e253a4cc3e6037d149020e85d19a..51ead9e86714fbb839767df3acc31b11d062fba4 100644 (file)
@@ -2036,6 +2036,7 @@ GLIBC_2.26 wcstof128_l F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
index 81bb623fe8fa2b03174d0b18c83165bc973e9783..78b4ee8d40c0c6ada705a0e703d3aeb9346fc744 100644 (file)
@@ -1900,6 +1900,7 @@ GLIBC_2.26 wcstof128_l F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
index 5a33b57390e7c26e0ce6a0da3d3cf9b671e1d536..d9c97779e48de7315918cded5643c13800d6c1f6 100644 (file)
@@ -108,6 +108,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.4 GLIBC_2.4 A
 GLIBC_2.4 _Exit F
 GLIBC_2.4 _IO_2_1_stderr_ D 0x98
index 50a86e74fa2d59274c8b6bde26755aeb7c46a015..4acbf7eeed95fc518d86c61f8ac34b3c6e82704a 100644 (file)
@@ -1985,6 +1985,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
index 250ef305c3277646b6bfc0f9f3689e5d3472753e..93f02f08ceaa85a67260576b632bd468dbb1b2e7 100644 (file)
@@ -2106,3 +2106,4 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
index 87a1dc4ad7b0a77694dc09215d5faeffcf339908..795e85de7083b48a984a22b0bde4440d51356dca 100644 (file)
@@ -1960,6 +1960,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
index f2b35f250ec83a61c449d21a09c2a9f19d9a58a8..dc714057b75e011b0d571dcc545643c1a6c1bcc3 100644 (file)
@@ -1958,6 +1958,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
index ade654dbeab43e81135af2476e0d8995259e8430..ce7bc9b1751fa9235bbff0671f8cb8e7fc903e5c 100644 (file)
@@ -1956,6 +1956,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
index 56032c3f826e2dcaf637205985538d24a5f4adf4..3fdd85eace96654304bb282cf44f768a412a3e66 100644 (file)
@@ -1951,6 +1951,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
index c599dd921235b62baa101d5b607f0f6ad8d743e7..3e0bcb2a5ccd3f64be13bcf4b85fed1719640b0a 100644 (file)
@@ -2147,3 +2147,4 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
index 385409aa6edd466d5deb7b9eb44469a022f9cdb2..375c69d9d1b201905bb4ece98b6002f4b36e3205 100644 (file)
@@ -1989,6 +1989,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
index e99cb454b5d5cfb01088906e99ead5579b8ac6be..a88172a9069611d1a7db858e16e11e0b49a80504 100644 (file)
@@ -1994,6 +1994,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
index 173672ab5e0f2a24190ab297bd20fb7d5dc2cf71..fa026a332c4f07fc4bc53b1a89c54a98c6007187 100644 (file)
@@ -2201,3 +2201,4 @@ GLIBC_2.26 wcstof128_l F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
index 8a654436ab6281b8f333b2af31c31503ef376a71..838f395d78e91401b194896efb551b210b6b2ed2 100644 (file)
@@ -108,6 +108,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 _Exit F
 GLIBC_2.3 _IO_2_1_stderr_ D 0xe0
index dbd411ceb19ff1ac2bd933a971a15d5be641c0ff..41b79c496a6f55531c5f7855bcd1a8e8fb0cc66f 100644 (file)
@@ -1989,6 +1989,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
index 5617784ca058a234479016ababa9e493773273df..68251a0e6922bd4bd5da92de58ea4581d027bb2e 100644 (file)
@@ -1890,6 +1890,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
index 0f840e6e88ba6807fa3e694b7918f7f86d745b42..bc1aae275eb64c7daba0d46496a18f7ad135925c 100644 (file)
@@ -1875,6 +1875,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
index bb7e1042c782245bdb4f27efb9efde5fd095b40f..93e6d092ac8e000d1e5047a0889b78574cf7dcca 100644 (file)
@@ -1982,6 +1982,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
index 4053b0a51cef95ba486c40c8c27e5b38e768175c..b11d6764d45e47600de56342366ab6482d4dc9f9 100644 (file)
@@ -1919,6 +1919,7 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.27 strfromf128 F
 GLIBC_2.27 strtof128 F
 GLIBC_2.27 strtof128_l F
index 8bfb080d80a57059e3a1a07c9e156a06e1eeb9dd..40c4fbb9ea3ab104ac98c0bb95bf681a0bfdc00d 100644 (file)
@@ -109,3 +109,4 @@ setns               EXTRA   setns           i:ii    setns
 
 process_vm_readv EXTRA process_vm_readv i:ipipii process_vm_readv
 process_vm_writev EXTRA        process_vm_writev i:ipipii process_vm_writev
+memfd_create    EXTRA  memfd_create    i:si    memfd_create
index 38a96d3a02053d107de081113ceb2ea34df82507..e9eb4ff7bdeb42ff6402da8efd7378080d05f1e2 100644 (file)
@@ -2113,3 +2113,4 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
index 572b917d7df8d88c34c835405afdaec4941b7fcb..8f08e909cd033f447557cc1bbd111d04c23a2cc7 100644 (file)
@@ -2113,3 +2113,4 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
index 38a96d3a02053d107de081113ceb2ea34df82507..e9eb4ff7bdeb42ff6402da8efd7378080d05f1e2 100644 (file)
@@ -2113,3 +2113,4 @@ GLIBC_2.26 reallocarray F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
diff --git a/sysdeps/unix/sysv/linux/tst-memfd_create.c b/sysdeps/unix/sysv/linux/tst-memfd_create.c
new file mode 100644 (file)
index 0000000..fd173ff
--- /dev/null
@@ -0,0 +1,121 @@
+/* Test for the memfd_create system call.
+   Copyright (C) 2017 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <support/check.h>
+#include <support/support.h>
+#include <support/test-driver.h>
+#include <support/xunistd.h>
+#include <sys/mman.h>
+
+/* Return true if the descriptor has the FD_CLOEXEC flag set.  */
+static bool
+is_cloexec (int fd)
+{
+  int flags = fcntl (fd, F_GETFD);
+  TEST_VERIFY (flags >= 0);
+  return flags & FD_CLOEXEC;
+}
+
+/* Return the seals set on FD.  */
+static int
+get_seals (int fd)
+{
+  int flags = fcntl (fd, F_GET_SEALS);
+  TEST_VERIFY (flags >= 0);
+  return flags;
+}
+
+/* Return true if the F_SEAL_SEAL flag is set on the descriptor.  */
+static bool
+is_sealed (int fd)
+{
+  return get_seals (fd) & F_SEAL_SEAL;
+}
+
+static int
+do_test (void)
+{
+  /* Initialized by the first call to memfd_create to 0 (memfd_create
+     unsupported) or 1 (memfd_create is implemented in the kernel).
+     Subsequent iterations check that the success/failure state is
+     consistent.  */
+  int supported = -1;
+
+  for (int do_cloexec = 0; do_cloexec < 2; ++do_cloexec)
+    for (int do_sealing = 0; do_sealing < 2; ++do_sealing)
+      {
+        int flags = 0;
+        if (do_cloexec)
+          flags |= MFD_CLOEXEC;
+        if (do_sealing)
+          flags |= MFD_ALLOW_SEALING;
+        if  (test_verbose > 0)
+          printf ("info: memfd_create with flags=0x%x\n", flags);
+        int fd = memfd_create ("tst-memfd_create", flags);
+        if (fd < 0)
+          {
+            if (errno == ENOSYS)
+              {
+                if (supported < 0)
+                  {
+                    printf ("warning: memfd_create is unsupported\n");
+                    supported = 0;
+                    continue;
+                  }
+                TEST_VERIFY (supported == 0);
+                continue;
+              }
+            else
+              FAIL_EXIT1 ("memfd_create: %m");
+          }
+        if (supported < 0)
+          supported = 1;
+        TEST_VERIFY (supported > 0);
+
+        char *fd_path = xasprintf ("/proc/self/fd/%d", fd);
+        char *link = xreadlink (fd_path);
+        if (test_verbose > 0)
+          printf ("info: memfd link: %s\n", link);
+        TEST_VERIFY (strcmp (link, "memfd:tst-memfd_create (deleted)"));
+        TEST_VERIFY (is_cloexec (fd) == do_cloexec);
+        TEST_VERIFY (is_sealed (fd) == !do_sealing);
+        if (do_sealing)
+          {
+            TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_WRITE) == 0);
+            TEST_VERIFY (!is_sealed (fd));
+            TEST_VERIFY (get_seals (fd) & F_SEAL_WRITE);
+            TEST_VERIFY (fcntl (fd, F_ADD_SEALS, F_SEAL_SEAL) == 0);
+            TEST_VERIFY (is_sealed (fd));
+          }
+        xclose (fd);
+        free (fd_path);
+        free (link);
+      }
+
+  if (supported == 0)
+    return EXIT_UNSUPPORTED;
+  return 0;
+}
+
+#include <support/test-driver.c>
index b83d25c2e38d7d52432a9f212c42318be8501f7c..0a4f7797ac5c8db469771cfadb24c24fe0391a13 100644 (file)
@@ -1877,6 +1877,7 @@ GLIBC_2.26 wcstof128_l F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F
 GLIBC_2.3 GLIBC_2.3 A
 GLIBC_2.3 __ctype_b_loc F
 GLIBC_2.3 __ctype_tolower_loc F
index cba1d590577304309fed0e80df860bb038f06306..23f6a9142916194e1c9aad2cbf3d1e808b8e64e2 100644 (file)
@@ -2120,3 +2120,4 @@ GLIBC_2.26 wcstof128_l F
 GLIBC_2.27 GLIBC_2.27 A
 GLIBC_2.27 glob F
 GLIBC_2.27 glob64 F
+GLIBC_2.27 memfd_create F