sigset.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Copyright (C) 1998, 2000, 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #define __need_NULL
  16. #include <stddef.h>
  17. #include <signal.h>
  18. #include <string.h>
  19. /* Set the disposition for SIG. */
  20. __sighandler_t sigset (int sig, __sighandler_t disp)
  21. {
  22. struct sigaction act, oact;
  23. sigset_t set;
  24. /* Check signal extents to protect __sigismember. */
  25. if (disp == SIG_ERR || sig < 1 || sig >= NSIG)
  26. {
  27. __set_errno (EINVAL);
  28. return SIG_ERR;
  29. }
  30. #ifdef SIG_HOLD
  31. /* Handle SIG_HOLD first. */
  32. if (disp == SIG_HOLD)
  33. {
  34. __sigemptyset (&set);
  35. __sigaddset (&set, sig);
  36. /* Add the signal set to the current signal mask. */
  37. sigprocmask (SIG_BLOCK, &set, NULL); /* can't fail */
  38. return SIG_HOLD;
  39. }
  40. #endif /* SIG_HOLD */
  41. memset(&act, 0, sizeof(act));
  42. act.sa_handler = disp;
  43. /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
  44. if (sigaction (sig, &act, &oact) < 0)
  45. return SIG_ERR;
  46. /* Create an empty signal set. Add the specified signal. */
  47. __sigemptyset (&set);
  48. __sigaddset (&set, sig);
  49. /* Remove the signal set from the current signal mask. */
  50. sigprocmask (SIG_UNBLOCK, &set, NULL); /* can't fail */
  51. return oact.sa_handler;
  52. }