signal.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* BSD-like signal function.
  2. Copyright (C) 1991,1992,1996,1997,2000,2002,2005
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <errno.h>
  18. #include <signal.h>
  19. #include <string.h> /* For the real memset prototype. */
  20. libc_hidden_proto(sigaction)
  21. sigset_t _sigintr attribute_hidden; /* Set by siginterrupt. */
  22. /* Set the handler for the signal SIG to HANDLER,
  23. returning the old handler, or SIG_ERR on error. */
  24. extern __typeof(bsd_signal) __bsd_signal;
  25. attribute_hidden __sighandler_t
  26. __bsd_signal (int sig, __sighandler_t handler)
  27. {
  28. struct sigaction act, oact;
  29. /* Check signal extents to protect __sigismember. */
  30. if (handler == SIG_ERR || sig < 1 || sig >= NSIG)
  31. {
  32. __set_errno (EINVAL);
  33. return SIG_ERR;
  34. }
  35. act.sa_handler = handler;
  36. if (__sigemptyset (&act.sa_mask) < 0
  37. || __sigaddset (&act.sa_mask, sig) < 0)
  38. return SIG_ERR;
  39. act.sa_flags = __sigismember (&_sigintr, sig) ? 0 : SA_RESTART;
  40. if (sigaction (sig, &act, &oact) < 0)
  41. return SIG_ERR;
  42. return oact.sa_handler;
  43. }
  44. strong_alias(__bsd_signal,bsd_signal)
  45. libc_hidden_proto(signal)
  46. strong_alias(__bsd_signal,signal)
  47. libc_hidden_def(signal)