[libglnx] missing: Sync from latest systemd, add memfd_create()



commit dd5fd9c1e558ae5a3916d4c571e1e8c84a9fb098
Author: Colin Walters <walters verbum org>
Date:   Sun Oct 1 05:20:23 2017 -0700

    missing: Sync from latest systemd, add memfd_create()
    
    Planning to use memfd_create() in flatpak and rpm-ostree, which both use
    bubblewrap, and want to pass read-only data via file descriptor to the
    container. Passing via `O_TMPFILE` requires `O_RDWR` (read and write),
    and passing via a pipe would require buffering.
    
    The systemd `missing.h` has grown enormously; I only cherry-picked the bits for
    memfd.

 glnx-missing-syscall.h |   60 +++++++++++++++++++++++++++++++++++++++++++++++-
 glnx-missing.h         |   54 +++++++++++++++++++++++++++++++++++++-----
 libglnx.h              |    1 +
 libglnx.m4             |    4 +--
 4 files changed, 108 insertions(+), 11 deletions(-)
---
diff --git a/glnx-missing-syscall.h b/glnx-missing-syscall.h
index c4957e0..fef6e60 100644
--- a/glnx-missing-syscall.h
+++ b/glnx-missing-syscall.h
@@ -18,7 +18,18 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-/* Missing glibc definitions to access certain kernel APIs */
+/* Missing glibc definitions to access certain kernel APIs.
+   This file is last updated from systemd git:
+
+   commit 71e5200f94b22589922704aa4abdf95d4fe2e528
+   Author:     Daniel Mack <daniel zonque org>
+   AuthorDate: Tue Oct 18 17:57:10 2016 +0200
+   Commit:     Lennart Poettering <lennart poettering net>
+   CommitDate: Fri Sep 22 15:24:54 2017 +0200
+
+   Add abstraction model for BPF programs
+*/
+
 
 #if !HAVE_DECL_RENAMEAT2
 #  ifndef __NR_renameat2
@@ -26,6 +37,8 @@
 #      define __NR_renameat2 316
 #    elif defined __arm__
 #      define __NR_renameat2 382
+#    elif defined __aarch64__
+#      define __NR_renameat2 276
 #    elif defined _MIPS_SIM
 #      if _MIPS_SIM == _MIPS_SIM_ABI32
 #        define __NR_renameat2 4351
@@ -38,6 +51,12 @@
 #      endif
 #    elif defined __i386__
 #      define __NR_renameat2 353
+#    elif defined __powerpc64__
+#      define __NR_renameat2 357
+#    elif defined __s390__ || defined __s390x__
+#      define __NR_renameat2 347
+#    elif defined __arc__
+#      define __NR_renameat2 276
 #    else
 #      warning "__NR_renameat2 unknown for your architecture"
 #    endif
@@ -53,6 +72,45 @@ static inline int renameat2(int oldfd, const char *oldname, int newfd, const cha
 }
 #endif
 
+#if !HAVE_DECL_MEMFD_CREATE
+#  ifndef __NR_memfd_create
+#    if defined __x86_64__
+#      define __NR_memfd_create 319
+#    elif defined __arm__
+#      define __NR_memfd_create 385
+#    elif defined __aarch64__
+#      define __NR_memfd_create 279
+#    elif defined __s390__
+#      define __NR_memfd_create 350
+#    elif defined _MIPS_SIM
+#      if _MIPS_SIM == _MIPS_SIM_ABI32
+#        define __NR_memfd_create 4354
+#      endif
+#      if _MIPS_SIM == _MIPS_SIM_NABI32
+#        define __NR_memfd_create 6318
+#      endif
+#      if _MIPS_SIM == _MIPS_SIM_ABI64
+#        define __NR_memfd_create 5314
+#      endif
+#    elif defined __i386__
+#      define __NR_memfd_create 356
+#    elif defined __arc__
+#      define __NR_memfd_create 279
+#    else
+#      warning "__NR_memfd_create unknown for your architecture"
+#    endif
+#  endif
+
+static inline int memfd_create(const char *name, unsigned int flags) {
+#  ifdef __NR_memfd_create
+        return syscall(__NR_memfd_create, name, flags);
+#  else
+        errno = ENOSYS;
+        return -1;
+#  endif
+}
+#endif
+
 /* Copied from systemd git:
    commit 6bda23dd6aaba50cf8e3e6024248cf736cc443ca
    Author:     Yu Watanabe <watanabe yu+github gmail com>
diff --git a/glnx-missing.h b/glnx-missing.h
index a60705a..0eba07b 100644
--- a/glnx-missing.h
+++ b/glnx-missing.h
@@ -19,7 +19,17 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-/* Missing glibc definitions to access certain kernel APIs */
+/* Missing glibc definitions to access certain kernel APIs.
+   This file is last updated from systemd git:
+
+   commit 71e5200f94b22589922704aa4abdf95d4fe2e528
+   Author:     Daniel Mack <daniel zonque org>
+   AuthorDate: Tue Oct 18 17:57:10 2016 +0200
+   Commit:     Lennart Poettering <lennart poettering net>
+   CommitDate: Fri Sep 22 15:24:54 2017 +0200
+
+   Add abstraction model for BPF programs
+*/
 
 #include <errno.h>
 #include <fcntl.h>
@@ -29,22 +39,30 @@
 #include <uchar.h>
 #include <unistd.h>
 
-#if defined(__i386__) || defined(__x86_64__)
-
-/* The precise definition of __O_TMPFILE is arch specific, so let's
- * just define this on x86 where we know the value. */
+/* The precise definition of __O_TMPFILE is arch specific; use the
+ * values defined by the kernel (note: some are hexa, some are octal,
+ * duplicated as-is from the kernel definitions):
+ * - alpha, parisc, sparc: each has a specific value;
+ * - others: they use the "generic" value.
+ */
 
 #ifndef __O_TMPFILE
+#if defined(__alpha__)
+#define __O_TMPFILE     0100000000
+#elif defined(__parisc__) || defined(__hppa__)
+#define __O_TMPFILE     0400000000
+#elif defined(__sparc__) || defined(__sparc64__)
+#define __O_TMPFILE     0x2000000
+#else
 #define __O_TMPFILE     020000000
 #endif
+#endif
 
 /* a horrid kludge trying to make sure that this will fail on old kernels */
 #ifndef O_TMPFILE
 #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
 #endif
 
-#endif
-
 #ifndef RENAME_NOREPLACE
 #define RENAME_NOREPLACE (1 << 0)
 #endif
@@ -52,4 +70,26 @@
 #define RENAME_EXCHANGE (1 << 1)
 #endif
 
+#ifndef F_LINUX_SPECIFIC_BASE
+#define F_LINUX_SPECIFIC_BASE 1024
+#endif
+
+#ifndef F_ADD_SEALS
+#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
+#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
+
+#define F_SEAL_SEAL     0x0001  /* prevent further seals from being set */
+#define F_SEAL_SHRINK   0x0002  /* prevent file from shrinking */
+#define F_SEAL_GROW     0x0004  /* prevent file from growing */
+#define F_SEAL_WRITE    0x0008  /* prevent writes */
+#endif
+
+#ifndef MFD_ALLOW_SEALING
+#define MFD_ALLOW_SEALING 0x0002U
+#endif
+
+#ifndef MFD_CLOEXEC
+#define MFD_CLOEXEC 0x0001U
+#endif
+
 #include "glnx-missing-syscall.h"
diff --git a/libglnx.h b/libglnx.h
index 494810d..411d4fa 100644
--- a/libglnx.h
+++ b/libglnx.h
@@ -25,6 +25,7 @@
 G_BEGIN_DECLS
 
 #include <glnx-macros.h>
+#include <glnx-missing.h>
 #include <glnx-local-alloc.h>
 #include <glnx-backport-autocleanups.h>
 #include <glnx-backports.h>
diff --git a/libglnx.m4 b/libglnx.m4
index 9b2e30c..770f117 100644
--- a/libglnx.m4
+++ b/libglnx.m4
@@ -1,8 +1,6 @@
 AC_DEFUN([LIBGLNX_CONFIGURE],
 [
-AC_CHECK_DECLS([
-        renameat2,
-        ],
+AC_CHECK_DECLS([renameat2, memfd_create],
         [], [], [[
 #include <sys/types.h>
 #include <unistd.h>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]