pthread_sigqueue.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (C) 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2009.
  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 <string.h>
  18. #include <unistd.h>
  19. #include <pthreadP.h>
  20. #include <tls.h>
  21. #include <sysdep.h>
  22. #include <bits/kernel-features.h>
  23. int
  24. pthread_sigqueue (
  25. pthread_t threadid,
  26. int signo,
  27. const union sigval value)
  28. {
  29. #ifdef __NR_rt_tgsigqueueinfo
  30. struct pthread *pd = (struct pthread *) threadid;
  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. /* Force load of pd->tid into local variable or register. Otherwise
  36. if a thread exits between ESRCH test and tgkill, we might return
  37. EINVAL, because pd->tid would be cleared by the kernel. */
  38. pid_t tid = atomic_forced_read (pd->tid);
  39. if (__builtin_expect (tid <= 0, 0))
  40. /* Not a valid thread handle. */
  41. return ESRCH;
  42. /* Disallow sending the signal we use for cancellation, timers, for
  43. for the setxid implementation. */
  44. if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID)
  45. return EINVAL;
  46. /* Set up the siginfo_t structure. */
  47. siginfo_t info;
  48. memset (&info, '\0', sizeof (siginfo_t));
  49. info.si_signo = signo;
  50. info.si_code = SI_QUEUE;
  51. info.si_pid = THREAD_GETMEM (THREAD_SELF, pid);
  52. info.si_uid = getuid ();
  53. info.si_value = value;
  54. /* We have a special syscall to do the work. */
  55. INTERNAL_SYSCALL_DECL (err);
  56. /* One comment: The PID field in the TCB can temporarily be changed
  57. (in fork). But this must not affect this code here. Since this
  58. function would have to be called while the thread is executing
  59. fork, it would have to happen in a signal handler. But this is
  60. no allowed, pthread_sigqueue is not guaranteed to be async-safe. */
  61. int val = INTERNAL_SYSCALL (rt_tgsigqueueinfo, err, 4,
  62. THREAD_GETMEM (THREAD_SELF, pid),
  63. tid, signo, &info);
  64. return (INTERNAL_SYSCALL_ERROR_P (val, err)
  65. ? INTERNAL_SYSCALL_ERRNO (val, err) : 0);
  66. #else
  67. return ENOSYS;
  68. #endif
  69. }