pthread_sigqueue.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, 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 <string.h>
  19. #include <unistd.h>
  20. #include <pthreadP.h>
  21. #include <tls.h>
  22. #include <sysdep.h>
  23. #include <bits/kernel-features.h>
  24. int
  25. pthread_sigqueue (
  26. pthread_t threadid,
  27. int signo,
  28. const union sigval value)
  29. {
  30. #ifdef __NR_rt_tgsigqueueinfo
  31. struct pthread *pd = (struct pthread *) threadid;
  32. /* Make sure the descriptor is valid. */
  33. if (DEBUGGING_P && INVALID_TD_P (pd))
  34. /* Not a valid thread handle. */
  35. return ESRCH;
  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 (__builtin_expect (tid <= 0, 0))
  41. /* Not a valid thread handle. */
  42. return ESRCH;
  43. /* Disallow sending the signal we use for cancellation, timers, for
  44. for the setxid implementation. */
  45. if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID)
  46. return EINVAL;
  47. /* Set up the siginfo_t structure. */
  48. siginfo_t info;
  49. memset (&info, '\0', sizeof (siginfo_t));
  50. info.si_signo = signo;
  51. info.si_code = SI_QUEUE;
  52. info.si_pid = THREAD_GETMEM (THREAD_SELF, pid);
  53. info.si_uid = getuid ();
  54. info.si_value = value;
  55. /* We have a special syscall to do the work. */
  56. INTERNAL_SYSCALL_DECL (err);
  57. /* One comment: The PID field in the TCB can temporarily be changed
  58. (in fork). But this must not affect this code here. Since this
  59. function would have to be called while the thread is executing
  60. fork, it would have to happen in a signal handler. But this is
  61. no allowed, pthread_sigqueue is not guaranteed to be async-safe. */
  62. int val = INTERNAL_SYSCALL (rt_tgsigqueueinfo, err, 4,
  63. THREAD_GETMEM (THREAD_SELF, pid),
  64. tid, signo, &info);
  65. return (INTERNAL_SYSCALL_ERROR_P (val, err)
  66. ? INTERNAL_SYSCALL_ERRNO (val, err) : 0);
  67. #else
  68. return ENOSYS;
  69. #endif
  70. }