[PATCH 2/3] tools/nolibc: add support for fcntl()
From: Thomas Weißschuh
Date: Wed Nov 25 2026 - 04:44:31 EST
Add the fcntl() function.
Special handling is necessary for 'struct flock'.
The definition from the UAPI headers is not large-file capable.
Provide a custom definition which is compatible with 'struct flock64'
on 32-bit systems and 'struct flock' on 64-bit ones.
Signed-off-by: Thomas Weißschuh <linux@xxxxxxxxxxxxxx>
---
tools/include/nolibc/fcntl.h | 35 +++++++++++++++++++++++++
tools/include/nolibc/types.h | 15 +++++++++++
tools/testing/selftests/nolibc/nolibc-test.c | 39 ++++++++++++++++++++++++++++
3 files changed, 89 insertions(+)
diff --git a/tools/include/nolibc/fcntl.h b/tools/include/nolibc/fcntl.h
index d4b6af60d4cc..98d22f614565 100644
--- a/tools/include/nolibc/fcntl.h
+++ b/tools/include/nolibc/fcntl.h
@@ -71,4 +71,39 @@ int creat(const char *path, mode_t mode)
return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
}
+/*
+ * int fcntl(int fd, int cmd, ...);
+ */
+
+static __attribute__((unused))
+int _sys_fcntl(int fd, int cmd, unsigned long arg)
+{
+#if defined(__NR_fcntl64)
+ return __nolibc_syscall3(__NR_fcntl64, fd, cmd, arg);
+#else
+ __nolibc_static_assert(sizeof(struct flock) == sizeof(struct __nolibc_kernel_flock));
+ __nolibc_static_assert(offsetof(struct flock, l_start) ==
+ offsetof(struct __nolibc_kernel_flock, l_start));
+ __nolibc_static_assert(offsetof(struct flock, l_len) ==
+ offsetof(struct __nolibc_kernel_flock, l_len));
+ __nolibc_static_assert(offsetof(struct flock, l_pid) ==
+ offsetof(struct __nolibc_kernel_flock, l_pid));
+
+ return __nolibc_syscall3(__NR_fcntl, fd, cmd, arg);
+#endif
+}
+
+static __attribute__((unused))
+int fcntl(int fd, int cmd, ...)
+{
+ unsigned long arg;
+ va_list args;
+
+ va_start(args, cmd);
+ arg = va_arg(args, unsigned long);
+ va_end(args);
+
+ return __sysret(_sys_fcntl(fd, cmd, arg));
+}
+
#endif /* _NOLIBC_FCNTL_H */
diff --git a/tools/include/nolibc/types.h b/tools/include/nolibc/types.h
index 4373bd8a5b5f..fba1e6d704e2 100644
--- a/tools/include/nolibc/types.h
+++ b/tools/include/nolibc/types.h
@@ -32,6 +32,21 @@ struct timeval {
#include <linux/time.h>
#undef timeval
+struct flock {
+ short l_type;
+ short l_whence;
+ __kernel_loff_t l_start;
+ __kernel_loff_t l_len;
+ __kernel_pid_t l_pid;
+#if defined(__sparc__)
+ short __pad;
+#endif
+};
+
+#define flock __nolibc_kernel_flock
+#include <linux/fcntl.h>
+#undef flock
+
/* Only the generic macros and types may be defined here. The arch-specific
* ones such as the O_RDONLY and related macros used by fcntl() and open()
* must not be defined here.
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 0eebd534bdb7..63e9ba54b265 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1908,6 +1908,44 @@ int test_difftime(void)
return 0;
}
+int test_flock_type(void)
+{
+ /* Kernel type */
+ struct flock64 fl64;
+
+ /* Nolibc type */
+ struct flock fl;
+
+ if (sizeof(fl) != sizeof(fl64))
+ return __LINE__;
+
+ if (!struct_members_identical(l_type, fl, fl64))
+ return __LINE__;
+
+ if (!struct_members_identical(l_whence, fl, fl64))
+ return __LINE__;
+
+ if (!struct_members_identical(l_start, fl, fl64))
+ return __LINE__;
+
+ if (!struct_members_identical(l_len, fl, fl64))
+ return __LINE__;
+
+ if (!struct_members_identical(l_pid, fl, fl64))
+ return __LINE__;
+
+ if (sizeof(fl.l_start) != sizeof(off_t))
+ return __LINE__;
+
+ if (sizeof(fl.l_len) != sizeof(off_t))
+ return __LINE__;
+
+ if (sizeof(fl.l_pid) != sizeof(pid_t))
+ return __LINE__;
+
+ return 0;
+}
+
int test_time_types(void)
{
#ifdef NOLIBC
@@ -2128,6 +2166,7 @@ int run_stdlib(int min, int max)
CASE_TEST(abs_noop); EXPECT_EQ(1, abs(10), 10); break;
CASE_TEST(alloca); EXPECT_ZR(1, test_alloca()); break;
CASE_TEST(difftime); EXPECT_ZR(1, test_difftime()); break;
+ CASE_TEST(flock_type); EXPECT_ZR(is_nolibc, test_flock_type()); break;
CASE_TEST(memchr_foobar6_o); EXPECT_STREQ(1, memchr("foobar", 'o', 6), "oobar"); break;
CASE_TEST(memchr_foobar3_b); EXPECT_STRZR(1, memchr("foobar", 'b', 3)); break;
CASE_TEST(time_types); EXPECT_ZR(is_nolibc, test_time_types()); break;
--
2.56.1