* [PATCH v2 0/4] tools/nolibc: add support for fcntl()
@ 2026-12-02 7:29 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
` (3 more replies)
0 siblings, 4 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
---
Changes in v2:
- Add functional tests.
- Add translations for F_GETLK -> F_GETLK64, etc.
- Make the clang --target= detection more robust.
- Link to v1: https://patch.msgid.link/20261125-nolibc-fcntl-v1-0-503d81db293f@weissschuh.net
To: Willy Tarreau <w@1wt.eu>
Cc: linux-kernel@vger.kernel.org
Cc: Thomas Weißschuh <linux@weissschuh.net>
Cc: Danish Khateeb <danishkhateeb03@gmail.com>
---
Thomas Weißschuh (4):
tools/nolibc: derive clang target flags from ARCH if CROSS_COMPILE is not set
selftests/nolibc: split out the structure member compatibility checks
tools/nolibc: add support for fcntl()
tools/nolibc: remove the open-coded fcntl() in dup2()
tools/include/nolibc/Makefile | 8 ++
tools/include/nolibc/fcntl.h | 48 +++++++++
tools/include/nolibc/sys.h | 11 +-
tools/include/nolibc/types.h | 15 +++
tools/testing/selftests/nolibc/nolibc-test.c | 150 ++++++++++++++++++++++++---
5 files changed, 212 insertions(+), 20 deletions(-)
---
base-commit: 7f77437e795bbaed6de60fa8b8bf1290eefaa5c9
change-id: 20261023-nolibc-fcntl-0eea08b0f897
Best regards,
--
Thomas Weißschuh <linux@weissschuh.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 1/4] tools/nolibc: derive clang target flags from ARCH if CROSS_COMPILE is not set
2026-12-02 7:29 [PATCH v2 0/4] tools/nolibc: add support for fcntl() Thomas Weißschuh
@ 2026-12-02 7:29 ` Thomas Weißschuh
2026-12-02 7:29 ` [PATCH v2 2/4] selftests/nolibc: split out the structure member compatibility checks Thomas Weißschuh
` (2 subsequent siblings)
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
The clang --target= flags are important to make sure that the UAPI
headers match the clang's architecture. If they don't match the
header test compilation may fail spuriously.
Currently the --target= flags are only derived from the CROSS_COMPILE
variable. This variable may not be set for native builds or for native
LLVM builds. Currently this works by chance.
Fall back to ARCH to determine the --target= flags if CROSS_COMPILE
was not set. ARCH is always set.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
tools/include/nolibc/Makefile | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile
index 0880be0f6ef5..20160f3f9547 100644
--- a/tools/include/nolibc/Makefile
+++ b/tools/include/nolibc/Makefile
@@ -122,6 +122,14 @@ headers_standalone: headers
CFLAGS_s390 := -m64
CFLAGS := $(CFLAGS_$(ARCH))
+ifneq ($(LLVM),)
+ifeq ($(CLANG_CROSS_FLAGS),)
+SRCARCH := $(ARCH)
+include $(srctree)/scripts/Makefile.clang
+CLANG_CROSS_FLAGS := --target=$(CLANG_TARGET_FLAGS)
+endif
+endif
+
headers_check: headers_standalone
$(Q)for header in $(filter-out crt.h std.h,$(all_files)); do \
$(CC) $(CFLAGS) $(CLANG_CROSS_FLAGS) -Wall -Werror -nostdinc -fsyntax-only -x c /dev/null \
--
2.56.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [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 [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 [flat|nested] 6+ messages in thread
* [PATCH v2 4/4] tools/nolibc: remove the open-coded fcntl() in dup2()
2026-12-02 7:29 [PATCH v2 0/4] tools/nolibc: add support for fcntl() Thomas Weißschuh
` (2 preceding siblings ...)
2026-12-02 7:29 ` [PATCH v2 3/4] tools/nolibc: add support for fcntl() Thomas Weißschuh
@ 2026-12-02 7:29 ` 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
Now that nolibc has a proper fctnl() wrapper use that in the dup2()
implementation instead of the open-coded logic.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
tools/include/nolibc/sys.h | 11 +++--------
1 file changed, 3 insertions(+), 8 deletions(-)
diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index b63fa6c618c5..5cd1c3ea9013 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -29,6 +29,7 @@
#include "stdarg.h"
#include "types.h"
+static int _sys_fcntl(int fd, int cmd, unsigned long arg);
/* Syscall return helper: takes the syscall value in argument and checks for an
* error in it. This may only be used with signed returns (int or long), but
@@ -262,16 +263,10 @@ static __attribute__((unused))
int _sys_dup2(int old, int new)
{
#if defined(__NR_dup3)
- int ret, nr_fcntl;
-
-#ifdef __NR_fcntl64
- nr_fcntl = __NR_fcntl64;
-#else
- nr_fcntl = __NR_fcntl;
-#endif
+ int ret;
if (old == new) {
- ret = __nolibc_syscall2(nr_fcntl, old, F_GETFD);
+ ret = _sys_fcntl(old, F_GETFD, 0);
return ret < 0 ? ret : old;
}
--
2.56.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 2/4] selftests/nolibc: split out the structure member compatibility checks
2026-12-02 7:29 ` [PATCH v2 2/4] selftests/nolibc: split out the structure member compatibility checks Thomas Weißschuh
@ 2026-12-02 11:29 ` David Laight
0 siblings, 0 replies; 6+ messages in thread
From: David Laight @ 2026-12-02 11:29 UTC (permalink / raw)
To: Thomas Weißschuh; +Cc: Willy Tarreau, linux-kernel, Danish Khateeb
On Wed, 02 Dec 2026 08:29:47 +0100
Thomas Weißschuh <linux@weissschuh.net> wrote:
> 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) ({ \
Maybe put the member parameter last?
I might have called them (struct_1, struct_2, member).
Those variables don't need protecting with a leading _ ...
> + bool different = false; \
... but, in theory, that one does.
(Clearly the calls are very limited.)
David
> + \
> + 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;
>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-12-02 11:29 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®