Skip to contentSkip to conversation

signal handling support for nolibc

PATCH Superseded Benjamin Berg 3 participants 10 messages Jul 9, 2025 – Jul 11, 2025

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover signal handling support for nolibc

From: Benjamin Berg <[email protected]>

Hi,

This patchset adds signal handling to nolibc. Initially, I would like to use this for tests. But in the long run, the goal is to use nolibc for the UML kernel itself. In both cases, signal handling will be needed.

Benjamin

Benjamin Berg (3):
tools/nolibc: show failed run if test process crashes tools/nolibc: add more generic BITSET_* macros for FD_* tools/nolibc: add signal support

 tools/include/nolibc/arch-arm.h               |   7 ++
 tools/include/nolibc/arch-arm64.h             |   3 +
 tools/include/nolibc/arch-loongarch.h         |   3 +
 tools/include/nolibc/arch-m68k.h              |  10 ++
 tools/include/nolibc/arch-mips.h              |   3 +
 tools/include/nolibc/arch-powerpc.h           |   8 ++
 tools/include/nolibc/arch-riscv.h             |   3 +
 tools/include/nolibc/arch-s390.h              |   8 +-
 tools/include/nolibc/arch-sh.h                |   5 +
 tools/include/nolibc/arch-sparc.h             |  47 ++++++++
 tools/include/nolibc/arch-x86.h               |  13 +++
 tools/include/nolibc/signal.h                 | 103 ++++++++++++++++++
 tools/include/nolibc/sys.h                    |   2 +-
 tools/include/nolibc/time.h                   |   3 +-
 tools/include/nolibc/types.h                  |  67 ++++++------
 .../testing/selftests/nolibc/Makefile.nolibc  |   3 +-
 tools/testing/selftests/nolibc/nolibc-test.c  |  67 ++++++++++++
 17 files changed, 319 insertions(+), 36 deletions(-)
Signature · 1 line
2.50.0
#1 lore raw

PATCH 1/3 tools/nolibc: show failed run if test process crashes

From: Benjamin Berg <[email protected]>

The logic would not catch if the test process crashes and would incorrectly report a "success" state. Fix this by looking for the final "Total number of errors:" message and printing "failure" if it was not seen.

---
 tools/testing/selftests/nolibc/Makefile.nolibc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/nolibc/Makefile.nolibc +2 -1
index 8cb65392fa10..51ba853dd97e 100644
@@ -245,8 +245,9 @@ endif
 include $(srctree)/tools/scripts/Makefile.include
 
 REPORT  ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \
+		/^Total number of errors:/{done++} \
 		END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \
-		if (f || !p) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \
+		if (f || !p || !done) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \
 		printf("\nSee all results in %s\n", ARGV[1]); }'
 
 help:
Signature · 1 line
2.50.0
#2 lore raw

Re: [PATCH 1/3] tools/nolibc: show failed run if test process crashes

Quoted 6 lines · Benjamin Berg

From: Benjamin Berg <[email protected]>

The logic would not catch if the test process crashes and would incorrectly report a "success" state. Fix this by looking for the final "Total number of errors:" message and printing "failure" if it was not seen.

Nice, I'll pick this up directly.

Quoted 23 lines
---
 tools/testing/selftests/nolibc/Makefile.nolibc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/nolibc/Makefile.nolibc +2 -1
index 8cb65392fa10..51ba853dd97e 100644
@@ -245,8 +245,9 @@ endif
 include $(srctree)/tools/scripts/Makefile.include
 
 REPORT  ?= awk '/\[OK\][\r]*$$/{p++} /\[FAIL\][\r]*$$/{if (!f) printf("\n"); f++; print;} /\[SKIPPED\][\r]*$$/{s++} \
+		/^Total number of errors:/{done++} \
 		END{ printf("\n%3d test(s): %3d passed, %3d skipped, %3d failed => status: ", p+s+f, p, s, f); \
-		if (f || !p) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \
+		if (f || !p || !done) printf("failure\n"); else if (s) printf("warning\n"); else printf("success\n");; \
 		printf("\nSee all results in %s\n", ARGV[1]); }'
 
 help:
Signature · 1 line
2.50.0
#3 lore raw

PATCH 2/3 tools/nolibc: add more generic BITSET_* macros for FD_*

In reply to Benjamin Berg

From: Benjamin Berg <[email protected]>

The FD_* macros are assuming a specific type for the bitset. Add new macros that introspect the type of the passed variable in order to know the size of the bitset. This way the same macros can be used for other purposes.

---
 tools/include/nolibc/types.h | 67 +++++++++++++++++++-----------------
 1 file changed, 36 insertions(+), 31 deletions(-)
tools/include/nolibc/types.h +36 -31
index 16c6e9ec9451..3100771e21ad 100644
@@ -115,48 +115,53 @@
 #define EXIT_SUCCESS 0
 #define EXIT_FAILURE 1
 
-#define FD_SETIDXMASK (8 * sizeof(unsigned long))
-#define FD_SETBITMASK (8 * sizeof(unsigned long)-1)
-
-/* for select() */
-typedef struct {
-	unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK];
-} fd_set;
-
-#define FD_CLR(fd, set) do {						\
-		fd_set *__set = (set);					\
-		int __fd = (fd);					\
-		if (__fd >= 0)						\
-			__set->fds[__fd / FD_SETIDXMASK] &=		\
-				~(1U << (__fd & FD_SETBITMASK));	\
+#define BITSET_CLR(num, set) do {					  \
+		__typeof__(set) *__set = &(set);			  \
+		int __num = (num);					  \
+		if (__num >= 0 && __num < 8 * (ssize_t)sizeof(*__set))	  \
+			(*__set)[__num / (8 * sizeof(set[0]))] &=	  \
+				~(1U << (__num % (8 * sizeof(**__set)))); \
 	} while (0)
 
-#define FD_SET(fd, set) do {						\
-		fd_set *__set = (set);					\
-		int __fd = (fd);					\
-		if (__fd >= 0)						\
-			__set->fds[__fd / FD_SETIDXMASK] |=		\
-				1 << (__fd & FD_SETBITMASK);		\
+#define BITSET_SET(num, set) do {					\
+		__typeof__(set) *__set = &(set);			\
+		int __num = (num);					\
+		if (__num >= 0 && __num < 8 * (ssize_t)sizeof(*__set))	\
+			(*__set)[__num / (8 * sizeof(set[0]))] |=	\
+				1U << (__num % (8 * sizeof(**__set)));	\
 	} while (0)
 
-#define FD_ISSET(fd, set) ({						\
-			fd_set *__set = (set);				\
-			int __fd = (fd);				\
+#define BITSET_ISSET(num, set) ({					\
+		__typeof__(set) *__set = &(set);			\
+		int __num = (num);					\
 		int __r = 0;						\
-		if (__fd >= 0)						\
-			__r = !!(__set->fds[__fd / FD_SETIDXMASK] &	\
-1U << (__fd & FD_SETBITMASK));						\
-		__r;							\
+		if (__num >= 0 && __num < 8 * (ssize_t)sizeof(*__set))	\
+			__r = (*__set)[__num / (8 * sizeof(set[0]))] &	\
+			      (1U << (__num % (8 * sizeof(**__set))));	\
+		!!__r;							\
 	})
 
-#define FD_ZERO(set) do {						\
-		fd_set *__set = (set);					\
+#define BITSET_ZERO(set) do {						\
+		__typeof__(set) *__set = &(set);			\
 		int __idx;						\
-		int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\
+		int __size = sizeof(*__set) / sizeof(**__set);		\
 		for (__idx = 0; __idx < __size; __idx++)		\
-			__set->fds[__idx] = 0;				\
+			(*__set)[__idx] = 0;				\
 	} while (0)
 
+#define FD_SETIDXMASK (8 * sizeof(unsigned long))
+#define FD_SETBITMASK (8 * sizeof(unsigned long)-1)
+
+/* for select() */
+typedef struct {
+	unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK];
+} fd_set;
+
+#define FD_CLR(fd, set) BITSET_CLR(fd, (set)->fds)
+#define FD_SET(fd, set) BITSET_SET(fd, (set)->fds)
+#define FD_ISSET(fd, set) BITSET_ISSET(fd, (set)->fds)
+#define FD_ZERO(set) BITSET_ZERO((set)->fds)
+
 /* for getdents64() */
 struct linux_dirent64 {
 	uint64_t       d_ino;
Signature · 1 line
2.50.0
#4 lore raw

Re: [PATCH 2/3] tools/nolibc: add more generic BITSET_* macros for FD_*

Quoted 25 lines · Benjamin Berg

From: Benjamin Berg <[email protected]>

The FD_* macros are assuming a specific type for the bitset. Add new macros that introspect the type of the passed variable in order to know the size of the bitset. This way the same macros can be used for other purposes.

---
 tools/include/nolibc/types.h | 67 +++++++++++++++++++-----------------
 1 file changed, 36 insertions(+), 31 deletions(-)
tools/include/nolibc/types.h +0 -5
index 16c6e9ec9451..3100771e21ad 100644
@@ -115,48 +115,53 @@
 #define EXIT_SUCCESS 0
 #define EXIT_FAILURE 1
 
-#define FD_SETIDXMASK (8 * sizeof(unsigned long))
-#define FD_SETBITMASK (8 * sizeof(unsigned long)-1)
-
-/* for select() */
-typedef struct {

tools/include/nolibc/types.h line 118

Quoted 10 lines
tools/include/nolibc/types.h +1 -9
-	unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK];
-} fd_set;
-
-#define FD_CLR(fd, set) do {						\
-		fd_set *__set = (set);					\
-		int __fd = (fd);					\
-		if (__fd >= 0)						\
-			__set->fds[__fd / FD_SETIDXMASK] &=		\
-				~(1U << (__fd & FD_SETBITMASK));	\
+#define BITSET_CLR(num, set) do {					  \

__NOLIBC_BITMASK_CLEAR()

To avoid conflicts with user code, being clear about the intent and having some constency with the kernel code.

Quoted 15 lines
tools/include/nolibc/types.h +7 -6
+		__typeof__(set) *__set = &(set);			  \
+		int __num = (num);					  \
+		if (__num >= 0 && __num < 8 * (ssize_t)sizeof(*__set))	  \
+			(*__set)[__num / (8 * sizeof(set[0]))] &=	  \
+				~(1U << (__num % (8 * sizeof(**__set)))); \
 	} while (0)
 
-#define FD_SET(fd, set) do {						\
-		fd_set *__set = (set);					\
-		int __fd = (fd);					\
-		if (__fd >= 0)						\
-			__set->fds[__fd / FD_SETIDXMASK] |=		\
-				1 << (__fd & FD_SETBITMASK);		\
+#define BITSET_SET(num, set) do {					\
+		__typeof__(set) *__set = &(set);			\

tools/include/nolibc/types.h line 134

Quoted 10 lines
tools/include/nolibc/types.h +5 -3
+		int __num = (num);					\
+		if (__num >= 0 && __num < 8 * (ssize_t)sizeof(*__set))	\
+			(*__set)[__num / (8 * sizeof(set[0]))] |=	\
+				1U << (__num % (8 * sizeof(**__set)));	\
 	} while (0)
 
-#define FD_ISSET(fd, set) ({						\
-			fd_set *__set = (set);				\
-			int __fd = (fd);				\
+#define BITSET_ISSET(num, set) ({					\

__NOLIBC_BITMASK_TEST()

Quoted 27 lines
tools/include/nolibc/types.h +12 -8
+		__typeof__(set) *__set = &(set);			\
+		int __num = (num);					\
 		int __r = 0;						\
-		if (__fd >= 0)						\
-			__r = !!(__set->fds[__fd / FD_SETIDXMASK] &	\
-1U << (__fd & FD_SETBITMASK));						\
-		__r;							\
+		if (__num >= 0 && __num < 8 * (ssize_t)sizeof(*__set))	\
+			__r = (*__set)[__num / (8 * sizeof(set[0]))] &	\
+			      (1U << (__num % (8 * sizeof(**__set))));	\
+		!!__r;							\
 	})
 
-#define FD_ZERO(set) do {						\
-		fd_set *__set = (set);					\
+#define BITSET_ZERO(set) do {						\
+		__typeof__(set) *__set = &(set);			\
 		int __idx;						\
-		int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\
+		int __size = sizeof(*__set) / sizeof(**__set);		\
 		for (__idx = 0; __idx < __size; __idx++)		\
-			__set->fds[__idx] = 0;				\
+			(*__set)[__idx] = 0;				\
 	} while (0)
 
+#define FD_SETIDXMASK (8 * sizeof(unsigned long))
+#define FD_SETBITMASK (8 * sizeof(unsigned long)-1)

tools/include/nolibc/types.h line 163

Quoted 10 lines
tools/include/nolibc/types.h +10 -0
+
+/* for select() */
+typedef struct {
+	unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK];
+} fd_set;
+
+#define FD_CLR(fd, set) BITSET_CLR(fd, (set)->fds)
+#define FD_SET(fd, set) BITSET_SET(fd, (set)->fds)
+#define FD_ISSET(fd, set) BITSET_ISSET(fd, (set)->fds)
+#define FD_ZERO(set) BITSET_ZERO((set)->fds)

These could be inline function I think.

Quoted 7 lines
tools/include/nolibc/types.h +1 -0
+
 /* for getdents64() */
 struct linux_dirent64 {
 	uint64_t       d_ino;
Signature · 1 line
2.50.0
maintainer
#5 lore raw

Re: [PATCH 2/3] tools/nolibc: add more generic BITSET_* macros for FD_*

Hi Thomas,

Quoted 6 lines · Thomas Weißschuh

tools/include/nolibc/types.h line 163

Quoted 4 lines
tools/include/nolibc/types.h +4 -0
+#define FD_CLR(fd, set) BITSET_CLR(fd, (set)->fds)
+#define FD_SET(fd, set) BITSET_SET(fd, (set)->fds)
+#define FD_ISSET(fd, set) BITSET_ISSET(fd, (set)->fds)
+#define FD_ZERO(set) BITSET_ZERO((set)->fds)

These could be inline function I think.

It's better not do, because man says they are macros, and I've seen programs using ifdef on them (not to mean that it's common but it's supposed to be possible according to the man).

Cheers,
Willy

#6 lore raw

Re: [PATCH 2/3] tools/nolibc: add more generic BITSET_* macros for FD_*

Quoted 10 lines · Willy Tarreau

Hi Thomas,

Quoted 6 lines · Thomas Weißschuh

tools/include/nolibc/types.h line 163

Quoted 4 lines
tools/include/nolibc/types.h +4 -0
+#define FD_CLR(fd, set) BITSET_CLR(fd, (set)->fds)
+#define FD_SET(fd, set) BITSET_SET(fd, (set)->fds)
+#define FD_ISSET(fd, set) BITSET_ISSET(fd, (set)->fds)
+#define FD_ZERO(set) BITSET_ZERO((set)->fds)

These could be inline function I think.

Quoted 3 lines

It's better not do, because man says they are macros, and I've seen programs using ifdef on them (not to mean that it's common but it's supposed to be possible according to the man).

Ack. OTOH, we could define a noop-macro for the functions:

static __inline__ void FD_SET(...)
{
}
#define FD_SET FD_SET

#7 lore raw

PATCH 3/3 tools/nolibc: add signal support

In reply to Benjamin Berg

From: Benjamin Berg <[email protected]>

Add support for sigaction() and implement the normal sa_mask helpers.

On many architectures, linux/signal.h pulls in compatibility definitions for the old sigaction syscall instead of rt_sigaction. However, the kernel can be compiled without support for this compatibility syscall and it also results in sa_mask to be too small for realtime signals.

To work around this, the includes are handled separately for each architecture. This way either linux/signal.h or the asm-generic headers can be used to get the correct definition for the rt_sigaction syscall including sigset_t.

---

v1:
- Update architecture support (adding sh)
- Move sparc sys_rt_sigaction logic into its header
- Add sig_atomic_t
- Use new BITSET_* macros
- Move test into syscall suite
- Various other small changes

---
 tools/include/nolibc/arch-arm.h              |   7 ++
 tools/include/nolibc/arch-arm64.h            |   3 +
 tools/include/nolibc/arch-loongarch.h        |   3 +
 tools/include/nolibc/arch-m68k.h             |  10 ++
 tools/include/nolibc/arch-mips.h             |   3 +
 tools/include/nolibc/arch-powerpc.h          |   8 ++
 tools/include/nolibc/arch-riscv.h            |   3 +
 tools/include/nolibc/arch-s390.h             |   8 +-
 tools/include/nolibc/arch-sh.h               |   5 +
 tools/include/nolibc/arch-sparc.h            |  47 +++++++++
 tools/include/nolibc/arch-x86.h              |  13 +++
 tools/include/nolibc/signal.h                | 103 +++++++++++++++++++
 tools/include/nolibc/sys.h                   |   2 +-
 tools/include/nolibc/time.h                  |   3 +-
 tools/testing/selftests/nolibc/nolibc-test.c |  67 ++++++++++++
 15 files changed, 281 insertions(+), 4 deletions(-)
tools/include/nolibc/arch-arm.h +7 -0
index 1f66e7e5a444..1faf6c2dbeb8 100644
@@ -10,6 +10,13 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 /* Syscalls for ARM in ARM or Thumb modes :
  *   - registers are 32-bit
  *   - stack is 8-byte aligned
tools/include/nolibc/arch-arm64.h +3 -0
index 02a3f74c8ec8..ad14fc0ae5cb 100644
@@ -10,6 +10,9 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Architecture has a usable linux/signal.h */
+#include <linux/signal.h>
+
 /* Syscalls for ARM64 :
  *   - registers are 64-bit
  *   - stack is 16-byte aligned
tools/include/nolibc/arch-loongarch.h +3 -0
index 5511705303ea..68d60d04ef59 100644
@@ -10,6 +10,9 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Architecture has a usable linux/signal.h */
+#include <linux/signal.h>
+
 /* Syscalls for LoongArch :
  *   - stack is 16-byte aligned
  *   - syscall number is passed in a7
tools/include/nolibc/arch-m68k.h +10 -0
index 6dac1845f298..981b4cc55a69 100644
@@ -13,6 +13,16 @@
 #include "compiler.h"
 #include "crt.h"
 
+/*
+ * Needed to get the correct struct sigaction definition. m68k does not use
+ * sa_restorer, but it is included in the structure.
+ */
+#define SA_RESTORER	0x04000000
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 #define _NOLIBC_SYSCALL_CLOBBERLIST "memory"
 
 #define my_syscall0(num)                                                      \
tools/include/nolibc/arch-mips.h +3 -0
index 0cbac63b249a..fb2f503f151f 100644
@@ -14,6 +14,9 @@
 #error Unsupported MIPS ABI
 #endif
 
+/* Architecture has a usable linux/signal.h */
+#include <linux/signal.h>
+
 /* Syscalls for MIPS ABI O32 :
  *   - WARNING! there's always a delayed slot!
  *   - WARNING again, the syntax is different, registers take a '$' and numbers
tools/include/nolibc/arch-powerpc.h +8 -0
index 204564bbcd32..c846a7ddcf3c 100644
@@ -10,6 +10,14 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+#define _NOLIBC_ARCH_NEEDS_SA_RESTORER
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 /* Syscalls for PowerPC :
  *   - stack is 16-byte aligned
  *   - syscall number is passed in r0
tools/include/nolibc/arch-riscv.h +3 -0
index 885383a86c38..709e6a262d9a 100644
@@ -10,6 +10,9 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Architecture has a usable linux/signal.h */
+#include <linux/signal.h>
+
 /* Syscalls for RISCV :
  *   - stack is 16-byte aligned
  *   - syscall number is passed in a7
tools/include/nolibc/arch-s390.h +7 -1
index df4c3cc713ac..0dccb6d1ad64 100644
@@ -5,13 +5,19 @@
 
 #ifndef _NOLIBC_ARCH_S390_H
 #define _NOLIBC_ARCH_S390_H
-#include <linux/signal.h>
 #include <linux/unistd.h>
 
 #include "compiler.h"
 #include "crt.h"
 #include "std.h"
 
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 /* Syscalls for s390:
  *   - registers are 64-bit
  *   - syscall number is passed in r1
tools/include/nolibc/arch-sh.h +5 -0
index a96b8914607e..3378afc78e26 100644
@@ -7,9 +7,14 @@
 #ifndef _NOLIBC_ARCH_SH_H
 #define _NOLIBC_ARCH_SH_H
 
+#include <linux/unistd.h>
+
 #include "compiler.h"
 #include "crt.h"
 
+/* Architecture has a usable linux/signal.h */
+#include <linux/signal.h>
+
 /*
  * Syscalls for SuperH:
  *   - registers are 32bit wide
tools/include/nolibc/arch-sparc.h +47 -0

57 lines not on this page · raw message

tools/include/nolibc/arch-x86.h +13 -0
index d3efc0c3b8ad..1fe75203d834 100644
@@ -10,8 +10,21 @@
 #include "compiler.h"
 #include "crt.h"
 
+/* Needed to get the correct struct sigaction definition */
+#define SA_RESTORER	0x04000000
+
+/* Restorer must be set on x86 for both 32 and 64 bit */
+#define _NOLIBC_ARCH_NEEDS_SA_RESTORER
+
+/* Avoid linux/signal.h, it has an incorrect _NSIG and sigset_t */
+#include <asm-generic/signal.h>
+#include <asm-generic/siginfo.h>
+
 #if !defined(__x86_64__)
 
+/* On i386 we need to set SA_SIGINFO to use rt_sigreturn */
+#define _NOLIBC_ARCH_FORCE_SIG_FLAGS SA_SIGINFO
+
 /* Syscalls for i386 :
  *   - mostly similar to x86_64
  *   - registers are 32-bit
tools/include/nolibc/signal.h +103 -0

113 lines not on this page · raw message

tools/include/nolibc/sys.h +1 -1
index 295e71d34aba..73b935576561 100644
@@ -14,7 +14,6 @@
 
 /* system includes */
 #include <linux/unistd.h>
-#include <linux/signal.h>  /* for SIGCHLD */
 #include <linux/termios.h>
 #include <linux/mman.h>
 #include <linux/fs.h>
@@ -24,6 +23,7 @@
 #include <linux/fcntl.h> /* for O_* and AT_* */
 #include <linux/sched.h> /* for clone_args */
 #include <linux/stat.h>  /* for statx() */
+/* signal definitions are included by arch.h */
 
 #include "errno.h"
 #include "stdarg.h"
tools/include/nolibc/time.h +1 -2
index d02bc44d2643..103574f76515 100644
@@ -14,9 +14,8 @@
 #include "arch.h"
 #include "types.h"
 #include "sys.h"
-
-#include <linux/signal.h>
 #include <linux/time.h>
+/* signal definitions are included by arch.h */
 
 static __inline__
 void __nolibc_timespec_user_to_kernel(const struct timespec *ts, struct __kernel_timespec *kts)
tools/testing/selftests/nolibc/nolibc-test.c +67 -0

79 lines not on this page · raw message

Signature · 1 line
2.50.0
#8 lore raw

Re: [PATCH 3/3] tools/nolibc: add signal support

On 2025-07-09 17:55:12+0200, Benjamin Berg wrote:
(...)

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1273

Quoted 7 lines
tools/testing/selftests/nolibc/nolibc-test.c +1 -0
@@ -1270,6 +1270,72 @@ int test_namespace(void)
 	return ret;
 }
 
+sig_atomic_t signal_check;

static?

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1282

Quoted 9 lines
tools/testing/selftests/nolibc/nolibc-test.c +9 -0
+
+static void sighandler(int signum)
+{
+	if (signum == SIGUSR1) {
+		kill(getpid(), SIGUSR2);
+		signal_check = 1;
+	} else {
+		signal_check++;
+	}

This could use some operations which make it clearer which steps occurred in which order. See the constructor tests.

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +10 -0
+}
+
+int test_signals(int test_idx)
+{
+	struct sigaction sa = {
+		.sa_flags = 0,
+		.sa_handler = sighandler,
+	};
+	struct sigaction sa_old = {
+		/* Anything other than SIG_DFL */

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1302

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +10 -0
+		.sa_handler = sighandler,
+	};
+	int llen; /* line length */
+	int ret = 0;
+	int res;
+
+	signal_check = 0;
+
+	sigemptyset(&sa.sa_mask);
+	sigaddset(&sa.sa_mask, SIGUSR2);

I'd like to see some tests for the sigset functions.

Quoted 22 lines
tools/testing/selftests/nolibc/nolibc-test.c +22 -0
+
+	res = sigaction(SIGUSR1, &sa, &sa_old);
+	llen = printf("    register SIGUSR1: %d", res);
+	EXPECT_SYSZR(1, res);
+	if (res)
+		goto out;
+
+	llen = printf("    sa_old.sa_handler: SIG_DFL (%p)", SIG_DFL);
+	EXPECT_PTREQ(1, SIG_DFL, sa_old.sa_handler);
+	if (res)
+		goto out;
+
+	res = sigaction(SIGUSR2, &sa, NULL);
+	llen = printf("    register SIGUSR2: %d", res);
+	EXPECT_SYSZR(1, res);
+	if (res)
+		goto out;
+
+	/* Trigger the first signal. */
+	kill(getpid(), SIGUSR1);
+
+	/* If signal_check is 1 or higher, then signal emission worked */

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1334

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +10 -0
+	llen = printf("    signal emission: 1 <= signal_check");
+	EXPECT_GE(1, signal_check, 1);
+
+	/* If it is 2, then signal masking worked */
+	llen = printf("    signal masking: 2 == signal_check");
+	EXPECT_EQ(1, signal_check, 2);
+
+out:
+	llen = printf("%d %s", test_idx, "sigaction");
+	EXPECT_EQ(1, res, 0);

Restore the default handler afterwards?

Quoted 18 lines
tools/testing/selftests/nolibc/nolibc-test.c +5 -0
+
+	return ret;
+}
+
 /* Run syscall tests between IDs <min> and <max>.
  * Return 0 on success, non-zero on failure.
  */
@@ -1398,6 +1464,7 @@ int run_syscall(int min, int max)
 		CASE_TEST(syscall_noargs);    EXPECT_SYSEQ(1, syscall(__NR_getpid), getpid()); break;
 		CASE_TEST(syscall_args);      EXPECT_SYSER(1, syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;
 		CASE_TEST(namespace);         EXPECT_SYSZR(euid0 && proc, test_namespace()); break;
+		case __LINE__:                ret += test_signals(test); break;
 		case __LINE__:
 			return ret; /* must be last */
 		/* note: do not set any defaults so as to permit holes above */
Signature · 1 line
2.50.0
#9 lore raw

Re: [PATCH 3/3] tools/nolibc: add signal support

Quoted 12 lines · Thomas Weißschuh

On 2025-07-09 17:55:12+0200, Benjamin Berg wrote:
(...)

Quoted 7 lines
tools/testing/selftests/nolibc/nolibc-test.c +0 -0
@@ -1270,6 +1270,72 @@ int test_namespace(void)
  	return ret;
  }


+sig_atomic_t signal_check;

static?

Oops.

Quoted 13 lines

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1282

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +9 -0
+
+static void sighandler(int signum)
+{
+	if (signum == SIGUSR1) {
+		kill(getpid(), SIGUSR2);
+		signal_check = 1;
+	} else {
+		signal_check++;
+	}

This could use some operations which make it clearer which steps occurred in which order. See the constructor tests.

Could it be that the constructor test logic is buggy? It seems to simply OR the bottom two bits, but doing that will not verify the order.

Maybe something more generic like the below macro? i.e. use the bottom bit to store whether the order has been correct.

/* Set BIT(step + 1), BIT(0) shows whether all steps ran in order */

#define mark_step_done(v, step) do {  \
        v |= v & (1 << ((step) + 1)); \
        if (v == 0 && (step) == 0)    \
            v |= 0x1;                 \
        else if (!(v & (1 << (step))) \
            v &= ~0x1;                \
    } while (0)
Quoted 14 lines
Quoted 14 lines
tools/testing/selftests/nolibc/nolibc-test.c +14 -0
+}
+
+int test_signals(int test_idx)
+{
+	struct sigaction sa = {
+		.sa_flags = 0,
+		.sa_handler = sighandler,
+	};
+	struct sigaction sa_old = {
+		/* Anything other than SIG_DFL */
+		.sa_handler = sighandler,
+	};
+	int llen; /* line length */
+	int ret = 0;
Quoted 8 lines

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1302

Quoted 6 lines
tools/testing/selftests/nolibc/nolibc-test.c +6 -0
+	int res;
+
+	signal_check = 0;
+
+	sigemptyset(&sa.sa_mask);
+	sigaddset(&sa.sa_mask, SIGUSR2);

I'd like to see some tests for the sigset functions.

Heh, yeah. sigaddset is kind of tested indirectly when verifying the emission order. But that just makes it unclear where the test failed.

Benjamin

Quoted 61 lines
Quoted 24 lines
tools/testing/selftests/nolibc/nolibc-test.c +8 -0
+
+	res = sigaction(SIGUSR1, &sa, &sa_old);
+	llen = printf("    register SIGUSR1: %d", res);
+	EXPECT_SYSZR(1, res);
+	if (res)
+		goto out;
+
+	llen = printf("    sa_old.sa_handler: SIG_DFL (%p)",

SIG_DFL);

tools/testing/selftests/nolibc/nolibc-test.c +14 -0
+	EXPECT_PTREQ(1, SIG_DFL, sa_old.sa_handler);
+	if (res)
+		goto out;
+
+	res = sigaction(SIGUSR2, &sa, NULL);
+	llen = printf("    register SIGUSR2: %d", res);
+	EXPECT_SYSZR(1, res);
+	if (res)
+		goto out;
+
+	/* Trigger the first signal. */
+	kill(getpid(), SIGUSR1);
+
+	/* If signal_check is 1 or higher, then signal emission

worked */

tools/testing/selftests/nolibc/nolibc-test.c int test_namespace(void) · line 1334

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +10 -0
+	llen = printf("    signal emission: 1 <= signal_check");
+	EXPECT_GE(1, signal_check, 1);
+
+	/* If it is 2, then signal masking worked */
+	llen = printf("    signal masking: 2 == signal_check");
+	EXPECT_EQ(1, signal_check, 2);
+
+out:
+	llen = printf("%d %s", test_idx, "sigaction");
+	EXPECT_EQ(1, res, 0);

Restore the default handler afterwards?

Quoted 23 lines
tools/testing/selftests/nolibc/nolibc-test.c +4 -0
+
+	return ret;
+}
+

 /* Run syscall tests between IDs <min> and <max>.
  * Return 0 on success, non-zero on failure. */

tools/testing/selftests/nolibc/nolibc-test.c +0 -0
@@ -1398,6 +1464,7 @@ int run_syscall(int min, int max)
  		CASE_TEST(syscall_noargs);    EXPECT_SYSEQ(1,
 syscall(__NR_getpid), getpid()); break;
  		CASE_TEST(syscall_args);      EXPECT_SYSER(1,
 syscall(__NR_statx, 0, NULL, 0, 0, NULL), -1, EFAULT); break;

  CASE_TEST(namespace);         EXPECT_SYSZR(euid0 && proc, test_namespace()); break;

tools/testing/selftests/nolibc/nolibc-test.c +1 -0
+		case __LINE__:                ret +=

test_signals(test); break;
  case __LINE__:
  return ret; /* must be last */ /* note: do not set any defaults so as to permit holes above */

Signature · 1 line
2.50.0