[RFC 1/2] tools/nolibc: add sigaction()
Thomas Weißschuh
thomas.weissschuh at linutronix.de
Thu Jun 26 21:48:53 PDT 2025
On Thu, Jun 26, 2025 at 09:57:13PM +0200, Benjamin Berg wrote:
> From: Benjamin Berg <benjamin.berg at intel.com>
>
> In preparation to add tests that use it.
I tried to implement signal support in nolibc before but ran into some issues.
Unfortunately I don't remember the details.
But I guess you know more about signals than me, so let's try it again.
In any case this should add some tests to
tools/testing/selftests/nolibc/nolibc-test.c.
If you test it with qemu-user please be aware that there were issues around
SA_RESTORER up until recently [0] [1].
[1] https://lore.kernel.org/qemu-devel/20250202-riscv-sa-restorer-v1-1-6f4bf814a1dd@t-8ch.de/
[0] https://lore.kernel.org/qemu-devel/mvmed060xc9.fsf@suse.de/
>
> Signed-off-by: Benjamin Berg <benjamin.berg at intel.com>
> ---
> tools/include/nolibc/nolibc.h | 3 +++
> tools/include/nolibc/signal.h | 34 ++++++++++++++++++++++++++++++++++
> 2 files changed, 37 insertions(+)
>
> diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h
> index c199ade200c2..29e37a755aba 100644
> --- a/tools/include/nolibc/nolibc.h
> +++ b/tools/include/nolibc/nolibc.h
> @@ -92,6 +92,9 @@
> #ifndef _NOLIBC_H
> #define _NOLIBC_H
>
> +/* So that we do not get compatibility types/defines */
> +#define __KERNEL__
Could you explain this more?
The user may have included kernel UAPI headers already before including nolibc,
so it doesn't really work.
> +
> #include "std.h"
> #include "arch.h"
> #include "types.h"
> diff --git a/tools/include/nolibc/signal.h b/tools/include/nolibc/signal.h
> index ac13e53ac31d..fa52119e577f 100644
> --- a/tools/include/nolibc/signal.h
> +++ b/tools/include/nolibc/signal.h
> @@ -14,6 +14,7 @@
> #include "arch.h"
> #include "types.h"
> #include "sys.h"
> +#include <linux/signal.h>
>
> /* This one is not marked static as it's needed by libgcc for divide by zero */
> int raise(int signal);
> @@ -23,4 +24,37 @@ int raise(int signal)
> return sys_kill(sys_getpid(), signal);
> }
>
> +/*
> + * sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
> + */
> +
> +#ifdef SA_RESTORER
> +__attribute__((naked))
__attribute__((naked)) is not supported everywhere.
> +static void my_sa_restorer(void)
This should use a name with less potential to conflict with user code.
Like __nolibc_sa_restorer().
> +{
> + my_syscall0(__NR_rt_sigreturn);
> +}
> +#endif
> +
> +static __attribute__((unused))
> +int sys_sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
> +{
> + struct sigaction real_act = *act;
> +#ifdef SA_RESTORER
> + if (!(real_act.sa_flags & SA_RESTORER)) {
> + real_act.sa_flags |= SA_RESTORER;
> + real_act.sa_restorer = my_sa_restorer;
> + }
> +#endif
> +
> + return my_syscall4(__NR_rt_sigaction, signum, &real_act, oldact,
> + sizeof(act->sa_mask));
No need for the linebreak.
> +}
> +
> +static __attribute__((unused))
> +int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
> +{
> + return __sysret(sys_sigaction(signum, act, oldact));
> +}
> +
> #endif /* _NOLIBC_SIGNAL_H */
> --
> 2.50.0
>
More information about the linux-um
mailing list