* [PATCH v2 2/4] selftests/nolibc: split out the structure member compatibility checks
2026-12-02 7:29 [PATCH v2 0/4] tools/nolibc: add support for fcntl() Thomas Weißschuh
2026-12-02 7:29 ` [PATCH v2 1/4] tools/nolibc: derive clang target flags from ARCH if CROSS_COMPILE is not set Thomas Weißschuh
@ 2026-12-02 7:29 ` Thomas Weißschuh
2026-12-02 11:29 ` David Laight
2026-12-02 7:29 ` [PATCH v2 3/4] tools/nolibc: add support for fcntl() Thomas Weißschuh
2026-12-02 7:29 ` [PATCH v2 4/4] tools/nolibc: remove the open-coded fcntl() in dup2() Thomas Weißschuh
3 siblings, 1 reply; 6+ messages in thread
From: Thomas Weißschuh @ 2026-12-02 7:29 UTC (permalink / raw)
To: Willy Tarreau; +Cc: linux-kernel, Thomas Weißschuh, Danish Khateeb
A future test will add more similar tests.
Factor out a helper to validate that two structures have compatible
members.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
tools/testing/selftests/nolibc/nolibc-test.c | 30 +++++++++++++++++-----------
1 file changed, 18 insertions(+), 12 deletions(-)
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index a924d53260fd..fce755ffed14 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -58,6 +58,18 @@
#define SINT_MAX_OF_TYPE(type) (((type)1 << (sizeof(type) * 8 - 2)) - (type)1 + ((type)1 << (sizeof(type) * 8 - 2)))
#define SINT_MIN_OF_TYPE(type) (-SINT_MAX_OF_TYPE(type) - 1)
+#define struct_members_identical(_member, _left, _right) ({ \
+ bool different = false; \
+ \
+ different |= sizeof((_left)._member) != sizeof((_right)._member); \
+ different |= offsetof(__typeof__(_left), _member) != \
+ offsetof(__typeof__(_right), _member); \
+ different |= !__builtin_types_compatible_p(__typeof__((_left)._member), \
+ __typeof__((_right)._member)); \
+ \
+ !different; \
+})
+
/* will be used to test initialization of environ */
static char **test_envp;
@@ -1903,22 +1915,16 @@ int test_time_types(void)
struct timespec ts;
if (!__builtin_types_compatible_p(time_t, __kernel_time64_t))
- return 1;
+ return __LINE__;
if (sizeof(ts) != sizeof(kts))
- return 1;
-
- if (!__builtin_types_compatible_p(__typeof__(ts.tv_sec), __typeof__(kts.tv_sec)))
- return 1;
-
- if (!__builtin_types_compatible_p(__typeof__(ts.tv_nsec), __typeof__(kts.tv_nsec)))
- return 1;
+ return __LINE__;
- if (offsetof(__typeof__(ts), tv_sec) != offsetof(__typeof__(kts), tv_sec))
- return 1;
+ if (!struct_members_identical(tv_sec, ts, kts))
+ return __LINE__;
- if (offsetof(__typeof__(ts), tv_nsec) != offsetof(__typeof__(kts), tv_nsec))
- return 1;
+ if (!struct_members_identical(tv_nsec, ts, kts))
+ return __LINE__;
#endif /* NOLIBC */
return 0;
--
2.56.1
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH v2 3/4] tools/nolibc: add support for fcntl()
2026-12-02 7:29 [PATCH v2 0/4] tools/nolibc: add support for fcntl() Thomas Weißschuh
2026-12-02 7:29 ` [PATCH v2 1/4] tools/nolibc: derive clang target flags from ARCH if CROSS_COMPILE is not set Thomas Weißschuh
2026-12-02 7:29 ` [PATCH v2 2/4] selftests/nolibc: split out the structure member compatibility checks Thomas Weißschuh
@ 2026-12-02 7:29 ` Thomas Weißschuh
2026-12-02 7:29 ` [PATCH v2 4/4] tools/nolibc: remove the open-coded fcntl() in dup2() Thomas Weißschuh
3 siblings, 0 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-12-02 7:29 UTC (permalink / raw)
To: Willy Tarreau; +Cc: linux-kernel, Thomas Weißschuh, Danish Khateeb
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@weissschuh.net>
---
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
^ permalink raw reply related [flat|nested] 6+ messages in thread