[PATCH v2 3/4] tools/nolibc: add support for fcntl()

From: Thomas Weißschuh

Date: Wed Dec 02 2026 - 02:30:07 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 | 48 +++++++++++
tools/include/nolibc/types.h | 15 ++++
tools/testing/selftests/nolibc/nolibc-test.c | 120 +++++++++++++++++++++++++++
3 files changed, 183 insertions(+)

diff --git a/tools/include/nolibc/fcntl.h b/tools/include/nolibc/fcntl.h
index d4b6af60d4cc..86341430825a 100644
--- a/tools/include/nolibc/fcntl.h
+++ b/tools/include/nolibc/fcntl.h
@@ -71,4 +71,52 @@ 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) && __BITS_PER_LONG != 64
+ switch (cmd) {
+ case F_GETLK:
+ cmd = F_GETLK64;
+ break;
+ case F_SETLK:
+ cmd = F_SETLK64;
+ break;
+ case F_SETLKW:
+ cmd = F_SETLKW64;
+ break;
+ }
+
+ 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 fce755ffed14..84c957c74371 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -907,6 +907,86 @@ int test_dirfd(void)
return ret;
}

+int test_fcntl(void)
+{
+ int fd, ret, status;
+ struct flock flock;
+ ssize_t written;
+ pid_t pid;
+
+ fd = memfd_create(__func__, 0);
+ if (fd == -1)
+ return __LINE__;
+
+ written = write(fd, "0123456789", 10);
+ if (written != 10) {
+ ret = __LINE__;
+ goto out;
+ }
+
+ memset(&flock, 0, sizeof(flock));
+ flock.l_type = F_WRLCK;
+ /* Lock bytes 3456 */
+ flock.l_whence = SEEK_END;
+ flock.l_start = -3;
+ flock.l_len = -4;
+
+ ret = fcntl(fd, F_SETLK, &flock);
+ if (ret) {
+ ret = __LINE__;
+ goto out;
+ }
+
+ pid = fork();
+
+ switch (pid) {
+ case -1:
+ ret = __LINE__;
+ goto out;
+
+ case 0:
+ close(STDOUT_FILENO);
+ close(STDERR_FILENO);
+
+ memset(&flock, 0, sizeof(flock));
+ flock.l_type = F_WRLCK;
+ flock.l_whence = SEEK_SET;
+ flock.l_start = 5;
+ flock.l_len = 1;
+
+ ret = fcntl(fd, F_GETLK, &flock);
+ if (ret)
+ exit(1);
+
+ if (flock.l_type != F_WRLCK)
+ exit(2);
+
+ if (flock.l_start != 3)
+ exit(3);
+
+ if (flock.l_len != 4)
+ exit(4);
+
+ if (flock.l_pid != getppid())
+ exit(5);
+
+ exit(0);
+ }
+
+ pid = waitpid(pid, &status, 0);
+ if (pid == -1 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+ ret = __LINE__;
+ goto out;
+ }
+
+ ret = 0;
+
+out:
+ close(fd);
+
+ return ret;
+}
+
int test_getcwd(void)
{
char cwd_syscall[PATH_MAX];
@@ -1813,6 +1893,7 @@ int run_syscall(int min, int max)
CASE_TEST(getdents64_null); EXPECT_SYSER(1, test_getdents64("/dev/null"), -1, ENOTDIR); break;
CASE_TEST(directories); EXPECT_SYSZR(proc, test_dirent()); break;
CASE_TEST(dirfd); EXPECT_SYSZR(has_working_64bit_syscall_arg, test_dirfd()); break;
+ CASE_TEST(fcntl); EXPECT_SYSZR(1, test_fcntl()); break;
CASE_TEST(getrandom); EXPECT_SYSZR(1, test_getrandom()); break;
CASE_TEST(gettimeofday_tv); EXPECT_SYSZR(1, gettimeofday(&tv, NULL)); break;
CASE_TEST(gettimeofday_tv_tz);EXPECT_SYSZR(1, gettimeofday(&tv, &tz)); break;
@@ -1908,6 +1989,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
@@ -2137,6 +2256,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