raise.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Copyright (C) 2002, 2003, 2004 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 <limits.h>
  18. #include <signal.h>
  19. #include <sysdep.h>
  20. #include <pthreadP.h>
  21. #include <bits/kernel-features.h>
  22. int
  23. raise (
  24. int sig)
  25. {
  26. struct pthread *pd = THREAD_SELF;
  27. #if (defined(__ASSUME_TGKILL) && __ASSUME_TGKILL) || defined __NR_tgkill
  28. pid_t pid = THREAD_GETMEM (pd, pid);
  29. #endif
  30. pid_t selftid = THREAD_GETMEM (pd, tid);
  31. if (selftid == 0)
  32. {
  33. /* This system call is not supposed to fail. */
  34. #ifdef INTERNAL_SYSCALL
  35. INTERNAL_SYSCALL_DECL (err);
  36. selftid = INTERNAL_SYSCALL (gettid, err, 0);
  37. #else
  38. selftid = INLINE_SYSCALL (gettid, 0);
  39. #endif
  40. THREAD_SETMEM (pd, tid, selftid);
  41. #if (defined(__ASSUME_TGKILL) && __ASSUME_TGKILL) || defined __NR_tgkill
  42. /* We do not set the PID field in the TID here since we might be
  43. called from a signal handler while the thread executes fork. */
  44. pid = selftid;
  45. #endif
  46. }
  47. #if (defined(__ASSUME_TGKILL) && __ASSUME_TGKILL) || defined __NR_tgkill
  48. else
  49. /* raise is an async-safe function. It could be called while the
  50. fork/vfork function temporarily invalidated the PID field. Adjust for
  51. that. */
  52. if (__builtin_expect (pid <= 0, 0))
  53. pid = (pid & INT_MAX) == 0 ? selftid : -pid;
  54. #endif
  55. #if defined(__ASSUME_TGKILL) && __ASSUME_TGKILL
  56. return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
  57. #else
  58. # ifdef __NR_tgkill
  59. int res = INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
  60. if (res != -1 || errno != ENOSYS)
  61. return res;
  62. # endif
  63. return INLINE_SYSCALL (tkill, 2, selftid, sig);
  64. #endif
  65. }
  66. libc_hidden_def (raise)