signal.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. sigset_t _sigintr; /* Set by siginterrupt. */
  21. /* Set the handler for the signal SIG to HANDLER,
  22. returning the old handler, or SIG_ERR on error. */
  23. __sighandler_t
  24. signal (int sig, __sighandler_t handler)
  25. {
  26. struct sigaction act, oact;
  27. /* Check signal extents to protect __sigismember. */
  28. if (handler == SIG_ERR || sig < 1 || sig >= NSIG)
  29. {
  30. __set_errno (EINVAL);
  31. return SIG_ERR;
  32. }
  33. act.sa_handler = handler;
  34. __sigemptyset (&act.sa_mask);
  35. __sigaddset (&act.sa_mask, sig);
  36. act.sa_flags = __sigismember (&_sigintr, sig) ? 0 : SA_RESTART;
  37. /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
  38. if (sigaction (sig, &act, &oact) < 0)
  39. return SIG_ERR;
  40. return oact.sa_handler;
  41. }
  42. libc_hidden_def(signal)
  43. #ifdef __UCLIBC_SUSV3_LEGACY__
  44. strong_alias(signal,bsd_signal)
  45. #endif