1 | /* Send a signal to a specific pthread. Stub version. |
2 | Copyright (C) 2014-2023 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <libc-lock.h> |
20 | #include <unistd.h> |
21 | #include <pthreadP.h> |
22 | #include <shlib-compat.h> |
23 | |
24 | /* Sends SIGNO to THREADID. If the thread is about to exit or has |
25 | already exited on the kernel side, return NO_TID. Otherwise return |
26 | 0 or an error code. */ |
27 | static int |
28 | __pthread_kill_implementation (pthread_t threadid, int signo, int no_tid) |
29 | { |
30 | struct pthread *pd = (struct pthread *) threadid; |
31 | if (pd == THREAD_SELF) |
32 | { |
33 | /* Use the actual TID from the kernel, so that it refers to the |
34 | current thread even if called after vfork. There is no |
35 | signal blocking in this case, so that the signal is delivered |
36 | immediately, before __pthread_kill_internal returns: a signal |
37 | sent to the thread itself needs to be delivered |
38 | synchronously. (It is unclear if Linux guarantees the |
39 | delivery of all pending signals after unblocking in the code |
40 | below. POSIX only guarantees delivery of a single signal, |
41 | which may not be the right one.) */ |
42 | pid_t tid = INTERNAL_SYSCALL_CALL (gettid); |
43 | int ret = INTERNAL_SYSCALL_CALL (tgkill, __getpid (), tid, signo); |
44 | return INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0; |
45 | } |
46 | |
47 | /* Block all signals, as required by pd->exit_lock. */ |
48 | internal_sigset_t old_mask; |
49 | internal_signal_block_all (&old_mask); |
50 | __libc_lock_lock (pd->exit_lock); |
51 | |
52 | int ret; |
53 | if (pd->exiting) |
54 | /* The thread is about to exit (or has exited). Sending the |
55 | signal is either not observable (the target thread has already |
56 | blocked signals at this point), or it will fail, or it might be |
57 | delivered to a new, unrelated thread that has reused the TID. |
58 | So do not actually send the signal. */ |
59 | ret = no_tid; |
60 | else |
61 | { |
62 | ret = INTERNAL_SYSCALL_CALL (tgkill, __getpid (), pd->tid, signo); |
63 | ret = INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0; |
64 | } |
65 | |
66 | __libc_lock_unlock (pd->exit_lock); |
67 | internal_signal_restore_set (&old_mask); |
68 | |
69 | return ret; |
70 | } |
71 | |
72 | int |
73 | __pthread_kill_internal (pthread_t threadid, int signo) |
74 | { |
75 | /* Do not report an error in the no-tid case because the threadid |
76 | argument is still valid (the thread ID lifetime has not ended), |
77 | and ESRCH (for example) would be misleading. */ |
78 | return __pthread_kill_implementation (threadid, signo, 0); |
79 | } |
80 | |
81 | int |
82 | __pthread_kill (pthread_t threadid, int signo) |
83 | { |
84 | /* Disallow sending the signal we use for cancellation, timers, |
85 | for the setxid implementation. */ |
86 | if (is_internal_signal (signo)) |
87 | return EINVAL; |
88 | |
89 | return __pthread_kill_internal (threadid, signo); |
90 | } |
91 | |
92 | /* Some architectures (for instance arm) might pull raise through libgcc, so |
93 | avoid the symbol version if it ends up being used on ld.so. */ |
94 | #if !IS_IN(rtld) |
95 | libc_hidden_def (__pthread_kill) |
96 | versioned_symbol (libc, __pthread_kill, pthread_kill, GLIBC_2_34); |
97 | |
98 | # if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_34) |
99 | /* Variant which returns ESRCH in the no-TID case, for backwards |
100 | compatibility. */ |
101 | int |
102 | attribute_compat_text_section |
103 | __pthread_kill_esrch (pthread_t threadid, int signo) |
104 | { |
105 | if (is_internal_signal (signo)) |
106 | return EINVAL; |
107 | |
108 | return __pthread_kill_implementation (threadid, signo, ESRCH); |
109 | } |
110 | compat_symbol (libc, __pthread_kill_esrch, pthread_kill, GLIBC_2_0); |
111 | # endif |
112 | #endif |
113 | |