| 1 | /* Copyright (C) 2009-2023 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 4 | The GNU C Library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | The GNU C Library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, see |
| 16 | <https://www.gnu.org/licenses/>. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <signal.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | #include <pthreadP.h> |
| 23 | #include <tls.h> |
| 24 | #include <sysdep.h> |
| 25 | #include <shlib-compat.h> |
| 26 | |
| 27 | int |
| 28 | __pthread_sigqueue (pthread_t threadid, int signo, const union sigval value) |
| 29 | { |
| 30 | #ifdef __NR_rt_tgsigqueueinfo |
| 31 | struct pthread *pd = (struct pthread *) threadid; |
| 32 | |
| 33 | /* Force load of pd->tid into local variable or register. Otherwise |
| 34 | if a thread exits between ESRCH test and tgkill, we might return |
| 35 | EINVAL, because pd->tid would be cleared by the kernel. */ |
| 36 | pid_t tid = atomic_forced_read (pd->tid); |
| 37 | if (__glibc_unlikely (tid <= 0)) |
| 38 | /* Not a valid thread handle. */ |
| 39 | return ESRCH; |
| 40 | |
| 41 | /* Disallow sending the signal we use for cancellation, timers, |
| 42 | for the setxid implementation. */ |
| 43 | if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID) |
| 44 | return EINVAL; |
| 45 | |
| 46 | pid_t pid = getpid (); |
| 47 | |
| 48 | /* Set up the siginfo_t structure. */ |
| 49 | siginfo_t info; |
| 50 | memset (&info, '\0', sizeof (siginfo_t)); |
| 51 | info.si_signo = signo; |
| 52 | info.si_code = SI_QUEUE; |
| 53 | info.si_pid = pid; |
| 54 | info.si_uid = __getuid (); |
| 55 | info.si_value = value; |
| 56 | |
| 57 | /* We have a special syscall to do the work. */ |
| 58 | int val = INTERNAL_SYSCALL_CALL (rt_tgsigqueueinfo, pid, tid, signo, |
| 59 | &info); |
| 60 | return (INTERNAL_SYSCALL_ERROR_P (val) |
| 61 | ? INTERNAL_SYSCALL_ERRNO (val) : 0); |
| 62 | #else |
| 63 | return ENOSYS; |
| 64 | #endif |
| 65 | } |
| 66 | versioned_symbol (libc, __pthread_sigqueue, pthread_sigqueue, GLIBC_2_34); |
| 67 | |
| 68 | #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_11, GLIBC_2_34) |
| 69 | compat_symbol (libpthread, __pthread_sigqueue, pthread_sigqueue, GLIBC_2_11); |
| 70 | #endif |
| 71 | |