]> git.hungrycats.org Git - bees/commitdiff
openat2: supply the missing definitions for building with old headers and new kernel
authorZygo Blaxell <bees@furryterror.org>
Mon, 20 Jan 2025 02:09:16 +0000 (21:09 -0500)
committerZygo Blaxell <bees@furryterror.org>
Mon, 20 Jan 2025 03:20:06 +0000 (22:20 -0500)
Apparently Ubuntu 20 has upgraded to kernel 5.15, but still builds things
with 5.4 headers.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
include/crucible/openat2.h
lib/openat2.cc

index 45f2c7f3a19a86c19129a4a6bae1e805866f7c0b..8d353508c1c44c41fb5e78295b9a934945acd5fd 100644 (file)
@@ -1,11 +1,44 @@
 #ifndef CRUCIBLE_OPENAT2_H
 #define CRUCIBLE_OPENAT2_H
 
+#include <cstdlib>
+
+// Compatibility for building on old libc for new kernel
+#include <linux/version.h>
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 6, 0)
+
 #include <linux/openat2.h>
 
-#include <fcntl.h>
-#include <sys/syscall.h>
-#include <unistd.h>
+#else
+
+#ifndef RESOLVE_NO_XDEV
+#define RESOLVE_NO_XDEV 1
+
+// RESOLVE_NO_XDEV was there from the beginning of openat2,
+// so if that's missing, so is open_how
+
+struct open_how {
+       __u64 flags;
+       __u64 mode;
+       __u64 resolve;
+};
+#endif
+
+#ifndef RESOLVE_NO_MAGICLINKS
+#define RESOLVE_NO_MAGICLINKS 2
+#endif
+#ifndef RESOLVE_NO_SYMLINKS
+#define RESOLVE_NO_SYMLINKS 4
+#endif
+#ifndef RESOLVE_BENEATH
+#define RESOLVE_BENEATH 8
+#endif
+#ifndef RESOLVE_IN_ROOT
+#define RESOLVE_IN_ROOT 16
+#endif
+
+#endif // Linux version >= v5.6
 
 extern "C" {
 
index 95c9eb3bbdc99cb91e4dc6325ad1e16ca3cac1a9..d868234602e36f9adfbfc3b0c3b55653a9246178 100644 (file)
@@ -1,5 +1,27 @@
 #include "crucible/openat2.h"
 
+#include <sys/syscall.h>
+
+// Compatibility for building on old libc for new kernel
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 6, 0)
+
+// Every arch that defines this uses 437, except Alpha, where 437 is
+// mq_getsetattr.
+
+#ifndef SYS_openat2
+#ifdef __alpha__
+#define SYS_openat2 547
+#else
+#define SYS_openat2 437
+#endif
+#endif
+
+#endif // Linux version >= v5.6
+
+#include <fcntl.h>
+#include <unistd.h>
+
 extern "C" {
 
 int
@@ -7,7 +29,12 @@ __attribute__((weak))
 openat2(int const dirfd, const char *const pathname, struct open_how *const how, size_t const size)
 throw()
 {
+#ifdef SYS_openat2
        return syscall(SYS_openat2, dirfd, pathname, how, size);
+#else
+       errno = ENOSYS;
+       return -1;
+#endif
 }
 
 };