pthread_kill.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (C) 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <signal.h>
  18. #include <pthreadP.h>
  19. #include <tls.h>
  20. #include <sysdep.h>
  21. #include <bits/kernel-features.h>
  22. int
  23. __pthread_kill (
  24. pthread_t threadid,
  25. int signo)
  26. {
  27. struct pthread *pd = (struct pthread *) threadid;
  28. /* Make sure the descriptor is valid. */
  29. if (DEBUGGING_P && INVALID_TD_P (pd))
  30. /* Not a valid thread handle. */
  31. return ESRCH;
  32. /* Force load of pd->tid into local variable or register. Otherwise
  33. if a thread exits between ESRCH test and tgkill, we might return
  34. EINVAL, because pd->tid would be cleared by the kernel. */
  35. pid_t tid = atomic_forced_read (pd->tid);
  36. if (__builtin_expect (tid <= 0, 0))
  37. /* Not a valid thread handle. */
  38. return ESRCH;
  39. /* Disallow sending the signal we use for cancellation, timers, for
  40. for the setxid implementation. */
  41. if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID)
  42. return EINVAL;
  43. /* We have a special syscall to do the work. */
  44. INTERNAL_SYSCALL_DECL (err);
  45. /* One comment: The PID field in the TCB can temporarily be changed
  46. (in fork). But this must not affect this code here. Since this
  47. function would have to be called while the thread is executing
  48. fork, it would have to happen in a signal handler. But this is
  49. no allowed, pthread_kill is not guaranteed to be async-safe. */
  50. int val;
  51. #if defined(__ASSUME_TGKILL) && __ASSUME_TGKILL
  52. val = INTERNAL_SYSCALL (tgkill, err, 3, THREAD_GETMEM (THREAD_SELF, pid),
  53. tid, signo);
  54. #else
  55. # ifdef __NR_tgkill
  56. val = INTERNAL_SYSCALL (tgkill, err, 3, THREAD_GETMEM (THREAD_SELF, pid),
  57. tid, signo);
  58. if (INTERNAL_SYSCALL_ERROR_P (val, err)
  59. && INTERNAL_SYSCALL_ERRNO (val, err) == ENOSYS)
  60. # endif
  61. val = INTERNAL_SYSCALL (tkill, err, 2, tid, signo);
  62. #endif
  63. return (INTERNAL_SYSCALL_ERROR_P (val, err)
  64. ? INTERNAL_SYSCALL_ERRNO (val, err) : 0);
  65. }
  66. strong_alias (__pthread_kill, pthread_kill)