[v8,1/6] nptl: Add __raise_direct

Message ID 20260318193454.2466865-2-adhemerval.zanella@linaro.org (mailing list archive)
State New
Headers
Series elf: Allow RPATH/RUNPATH for static-pie |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

Adhemerval Zanella Netto March 18, 2026, 7:34 p.m. UTC
  The function sends a signal to current thread using raw syscalls.
---
 include/signal.h                       |  3 +++
 nptl/pthread_kill.c                    | 21 +++++++------------
 sysdeps/posix/raise.c                  |  2 +-
 sysdeps/unix/sysv/linux/Makefile       |  4 ++++
 sysdeps/unix/sysv/linux/raise_direct.c | 29 ++++++++++++++++++++++++++
 5 files changed, 44 insertions(+), 15 deletions(-)
 create mode 100644 sysdeps/unix/sysv/linux/raise_direct.c
  

Comments

Florian Weimer March 20, 2026, 10:53 a.m. UTC | #1
* Adhemerval Zanella:

> The function sends a signal to current thread using raw syscalls.
> ---
>  include/signal.h                       |  3 +++
>  nptl/pthread_kill.c                    | 21 +++++++------------
>  sysdeps/posix/raise.c                  |  2 +-
>  sysdeps/unix/sysv/linux/Makefile       |  4 ++++
>  sysdeps/unix/sysv/linux/raise_direct.c | 29 ++++++++++++++++++++++++++
>  5 files changed, 44 insertions(+), 15 deletions(-)
>  create mode 100644 sysdeps/unix/sysv/linux/raise_direct.c

Okay.

Reviewed-by: Florian Weimer <fweimer@redhat.com>

Thanks,
Florian
  

Patch

diff --git a/include/signal.h b/include/signal.h
index 73f18dddd7..099d721514 100644
--- a/include/signal.h
+++ b/include/signal.h
@@ -65,6 +65,9 @@  extern int __xpg_sigpause (int sig);
 /* Allocate real-time signal with highest/lowest available priority.  */
 extern int __libc_allocate_rtsig (int __high);
 
+/* Similar to raise, but does not set errno.  */
+extern int __raise_direct (int signo) attribute_hidden;
+
 #  if IS_IN (rtld)
 extern __typeof (__sigaction) __sigaction attribute_hidden;
 extern __typeof (__libc_sigaction) __libc_sigaction attribute_hidden;
diff --git a/nptl/pthread_kill.c b/nptl/pthread_kill.c
index 221689e36a..bc038e9824 100644
--- a/nptl/pthread_kill.c
+++ b/nptl/pthread_kill.c
@@ -29,20 +29,13 @@  __pthread_kill_implementation (pthread_t threadid, int signo, int no_tid)
 {
   struct pthread *pd = (struct pthread *) threadid;
   if (pd == THREAD_SELF)
-    {
-      /* Use the actual TID from the kernel, so that it refers to the
-         current thread even if called after vfork.  There is no
-         signal blocking in this case, so that the signal is delivered
-         immediately, before __pthread_kill_internal returns: a signal
-         sent to the thread itself needs to be delivered
-         synchronously.  (It is unclear if Linux guarantees the
-         delivery of all pending signals after unblocking in the code
-         below.  POSIX only guarantees delivery of a single signal,
-         which may not be the right one.)  */
-      pid_t tid = INTERNAL_SYSCALL_CALL (gettid);
-      int ret = INTERNAL_SYSCALL_CALL (tgkill, __getpid (), tid, signo);
-      return INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0;
-    }
+    /* There is no signal blocking in this case, so that the signal is
+       delivered immediately, before __pthread_kill_internal returns: a signal
+       sent to the thread itself needs to be delivered synchronously.  (It is
+       unclear whether Linux guarantees the delivery of all pending signals
+       after unblocking in the code  below.  POSIX only guarantees delivery
+       of a single signal, which may not be the right one.)  */
+    return __raise_direct (signo);
 
   /* Block all signals, as required by pd->exit_lock.  */
   internal_sigset_t old_mask;
diff --git a/sysdeps/posix/raise.c b/sysdeps/posix/raise.c
index 8abe8d4cad..cf0770f1c8 100644
--- a/sysdeps/posix/raise.c
+++ b/sysdeps/posix/raise.c
@@ -23,7 +23,7 @@ 
 int
 raise (int sig)
 {
-  int ret = __pthread_kill (__pthread_self (), sig);
+  int ret = __raise_direct (sig);
   if (ret != 0)
     {
       __set_errno (ret);
diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index c6bd97abf1..8797c5c9b3 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -455,6 +455,10 @@  tests += \
 endif
 
 ifeq ($(subdir),signal)
+sysdep_routines += \
+  raise_direct \
+  # sysdep_routines
+
 tests-special += \
   $(objpfx)tst-signal-numbers.out \
   # tests-special
diff --git a/sysdeps/unix/sysv/linux/raise_direct.c b/sysdeps/unix/sysv/linux/raise_direct.c
new file mode 100644
index 0000000000..d38bd10817
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/raise_direct.c
@@ -0,0 +1,29 @@ 
+/* Internal function to send a signal to itself.  Linux version.
+   Copyright (C) 2026 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <sysdep.h>
+#include <pthread.h>
+#include <unistd.h>
+
+int
+__raise_direct (int signo)
+{
+  pid_t tid = INTERNAL_SYSCALL_CALL (gettid);
+  int ret = INTERNAL_SYSCALL_CALL (tkill, tid, signo);
+  return INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0;
+}