1 | /* Copyright (C) 2002-2016 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 | <http://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 | |
25 | |
26 | int |
27 | __pthread_kill (pthread_t threadid, int signo) |
28 | { |
29 | struct pthread *pd = (struct pthread *) threadid; |
30 | |
31 | /* Make sure the descriptor is valid. */ |
32 | if (DEBUGGING_P && INVALID_TD_P (pd)) |
33 | /* Not a valid thread handle. */ |
34 | return ESRCH; |
35 | |
36 | /* Force load of pd->tid into local variable or register. Otherwise |
37 | if a thread exits between ESRCH test and tgkill, we might return |
38 | EINVAL, because pd->tid would be cleared by the kernel. */ |
39 | pid_t tid = atomic_forced_read (pd->tid); |
40 | if (__glibc_unlikely (tid <= 0)) |
41 | /* Not a valid thread handle. */ |
42 | return ESRCH; |
43 | |
44 | /* Disallow sending the signal we use for cancellation, timers, |
45 | for the setxid implementation. */ |
46 | if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID) |
47 | return EINVAL; |
48 | |
49 | /* We have a special syscall to do the work. */ |
50 | INTERNAL_SYSCALL_DECL (err); |
51 | |
52 | /* One comment: The PID field in the TCB can temporarily be changed |
53 | (in fork). But this must not affect this code here. Since this |
54 | function would have to be called while the thread is executing |
55 | fork, it would have to happen in a signal handler. But this is |
56 | no allowed, pthread_kill is not guaranteed to be async-safe. */ |
57 | int val; |
58 | val = INTERNAL_SYSCALL (tgkill, err, 3, THREAD_GETMEM (THREAD_SELF, pid), |
59 | tid, signo); |
60 | |
61 | return (INTERNAL_SYSCALL_ERROR_P (val, err) |
62 | ? INTERNAL_SYSCALL_ERRNO (val, err) : 0); |
63 | } |
64 | strong_alias (__pthread_kill, pthread_kill) |
65 | |