pthread_kill.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <signal.h>
  17. #include <unistd.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. pid_t pid = getpid ();
  51. int val;
  52. val = INTERNAL_SYSCALL (tgkill, err, 3, pid, tid, signo);
  53. return (INTERNAL_SYSCALL_ERROR_P (val, err)
  54. ? INTERNAL_SYSCALL_ERRNO (val, err) : 0);
  55. }
  56. strong_alias (__pthread_kill, pthread_kill)