pthread_kill.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Copyright (C) 2002-2019 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 <pthreadP.h>
  18. #include <tls.h>
  19. #include <sysdep.h>
  20. #include <unistd.h>
  21. int
  22. __pthread_kill (pthread_t threadid, int signo)
  23. {
  24. struct pthread *pd = (struct pthread *) threadid;
  25. /* Make sure the descriptor is valid. */
  26. if (DEBUGGING_P && INVALID_TD_P (pd))
  27. /* Not a valid thread handle. */
  28. return ESRCH;
  29. /* Force load of pd->tid into local variable or register. Otherwise
  30. if a thread exits between ESRCH test and tgkill, we might return
  31. EINVAL, because pd->tid would be cleared by the kernel. */
  32. pid_t tid = atomic_forced_read (pd->tid);
  33. if (__glibc_unlikely (tid <= 0))
  34. /* Not a valid thread handle. */
  35. return ESRCH;
  36. /* Disallow sending the signal we use for cancellation, timers,
  37. for the setxid implementation. */
  38. if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID)
  39. return EINVAL;
  40. /* We have a special syscall to do the work. */
  41. INTERNAL_SYSCALL_DECL (err);
  42. pid_t pid = __getpid ();
  43. int val = INTERNAL_SYSCALL_CALL (tgkill, err, pid, tid, signo);
  44. return (INTERNAL_SYSCALL_ERROR_P (val, err)
  45. ? INTERNAL_SYSCALL_ERRNO (val, err) : 0);
  46. }
  47. strong_alias (__pthread_kill, pthread_kill)