| 1 | /* Copyright (C) 2002-2020 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 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 <errno.h> |
| 20 | #include <signal.h> |
| 21 | #include <pthreadP.h> |
| 22 | #include <tls.h> |
| 23 | #include <sysdep.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | |
| 27 | int |
| 28 | __pthread_kill (pthread_t threadid, int signo) |
| 29 | { |
| 30 | struct pthread *pd = (struct pthread *) threadid; |
| 31 | |
| 32 | /* Make sure the descriptor is valid. */ |
| 33 | if (DEBUGGING_P && INVALID_TD_P (pd)) |
| 34 | /* Not a valid thread handle. */ |
| 35 | return ESRCH; |
| 36 | |
| 37 | /* Force load of pd->tid into local variable or register. Otherwise |
| 38 | if a thread exits between ESRCH test and tgkill, we might return |
| 39 | EINVAL, because pd->tid would be cleared by the kernel. */ |
| 40 | pid_t tid = atomic_forced_read (pd->tid); |
| 41 | if (__glibc_unlikely (tid <= 0)) |
| 42 | /* Not a valid thread handle. */ |
| 43 | return ESRCH; |
| 44 | |
| 45 | /* Disallow sending the signal we use for cancellation, timers, |
| 46 | for the setxid implementation. */ |
| 47 | if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID) |
| 48 | return EINVAL; |
| 49 | |
| 50 | /* We have a special syscall to do the work. */ |
| 51 | pid_t pid = __getpid (); |
| 52 | |
| 53 | int val = INTERNAL_SYSCALL_CALL (tgkill, pid, tid, signo); |
| 54 | return (INTERNAL_SYSCALL_ERROR_P (val) |
| 55 | ? INTERNAL_SYSCALL_ERRNO (val) : 0); |
| 56 | } |
| 57 | strong_alias (__pthread_kill, pthread_kill) |
| 58 | |