[PATCH v2 2/5] nptl: Add __raise_nocancel

Adhemerval Zanella adhemerval.zanella@linaro.org
Tue Sep 2 19:26:54 GMT 2025


The function sends a signal to current thread using raw syscalls.
---
 include/signal.h                         |  2 ++
 nptl/pthread_kill.c                      | 21 ++++++----------
 signal/Makefile                          |  2 +-
 signal/raise_nostatus.c                  | 27 +++++++++++++++++++++
 sysdeps/posix/raise.c                    |  2 +-
 sysdeps/unix/sysv/linux/raise_nostatus.c | 31 ++++++++++++++++++++++++
 6 files changed, 69 insertions(+), 16 deletions(-)
 create mode 100644 signal/raise_nostatus.c
 create mode 100644 sysdeps/unix/sysv/linux/raise_nostatus.c

diff --git a/include/signal.h b/include/signal.h
index 73f18dddd7..9aa4c0e055 100644
--- a/include/signal.h
+++ b/include/signal.h
@@ -65,6 +65,8 @@ extern int __xpg_sigpause (int sig);
 /* Allocate real-time signal with highest/lowest available priority.  */
 extern int __libc_allocate_rtsig (int __high);
 
+extern int __raise_nostatus (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 3938e3f8cd..6529887ff5 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 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.)  */
+    return __raise_nostatus (signo);
 
   /* Block all signals, as required by pd->exit_lock.  */
   internal_sigset_t old_mask;
diff --git a/signal/Makefile b/signal/Makefile
index 8c7961c5f0..468a8b93f0 100644
--- a/signal/Makefile
+++ b/signal/Makefile
@@ -34,7 +34,7 @@ headers := signal.h sys/signal.h \
 	   bits/types/__sigval_t.h bits/signal_ext.h \
 	   bits/sigstksz.h
 
-routines	:= signal raise killpg \
+routines	:= signal raise raise_nostatus killpg \
 		   sigaction libc_sigaction sigprocmask kill \
 		   sigpending sigsuspend sigwait \
 		   sigblock sigsetmask sigpause sigvec \
diff --git a/signal/raise_nostatus.c b/signal/raise_nostatus.c
new file mode 100644
index 0000000000..ed572ddee2
--- /dev/null
+++ b/signal/raise_nostatus.c
@@ -0,0 +1,27 @@
+/* Internal function to send a signal to itself.  Generic version.
+   Copyright (C) 2025 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_nostatus (int signo)
+{
+  return __kill (__getpid (), signo);
+}
diff --git a/sysdeps/posix/raise.c b/sysdeps/posix/raise.c
index da3f11ccf2..3c5f032c5c 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_nostatus (sig);
   if (ret != 0)
     {
       __set_errno (ret);
diff --git a/sysdeps/unix/sysv/linux/raise_nostatus.c b/sysdeps/unix/sysv/linux/raise_nostatus.c
new file mode 100644
index 0000000000..6f9ebc1ac9
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/raise_nostatus.c
@@ -0,0 +1,31 @@
+/* Internal function to send a signal to itself.  Linux version.
+   Copyright (C) 2025 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_nostatus (int signo)
+{
+  /* Use direct syscall to avoid external symbols.  */
+  pid_t tid = INTERNAL_SYSCALL_CALL (gettid);
+  pid_t pid = INTERNAL_SYSCALL_CALL (getpid);
+  int ret = INTERNAL_SYSCALL_CALL (tgkill, pid, tid, signo);
+  return INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0;
+}
-- 
2.43.0



More information about the Libc-alpha mailing list