lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1998, 2000, 2005 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 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 | |
| 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 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with the GNU C Library; if not, write to the Free |
| 16 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 17 | 02111-1307 USA. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #define __need_NULL |
| 21 | #include <stddef.h> |
| 22 | #include <signal.h> |
| 23 | #include <string.h> /* For the real memset prototype. */ |
| 24 | |
| 25 | |
| 26 | /* Set the disposition for SIG. */ |
| 27 | __sighandler_t sigset (int sig, __sighandler_t disp) |
| 28 | { |
| 29 | struct sigaction act, oact; |
| 30 | sigset_t set; |
| 31 | |
| 32 | /* Check signal extents to protect __sigismember. */ |
| 33 | if (disp == SIG_ERR || sig < 1 || sig >= NSIG) |
| 34 | { |
| 35 | __set_errno (EINVAL); |
| 36 | return SIG_ERR; |
| 37 | } |
| 38 | |
| 39 | #ifdef SIG_HOLD |
| 40 | /* Handle SIG_HOLD first. */ |
| 41 | if (disp == SIG_HOLD) |
| 42 | { |
| 43 | __sigemptyset (&set); |
| 44 | __sigaddset (&set, sig); |
| 45 | |
| 46 | /* Add the signal set to the current signal mask. */ |
| 47 | sigprocmask (SIG_BLOCK, &set, NULL); /* can't fail */ |
| 48 | |
| 49 | return SIG_HOLD; |
| 50 | } |
| 51 | #endif /* SIG_HOLD */ |
| 52 | |
| 53 | memset(&act, 0, sizeof(act)); |
| 54 | act.sa_handler = disp; |
| 55 | /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */ |
| 56 | if (sigaction (sig, &act, &oact) < 0) |
| 57 | return SIG_ERR; |
| 58 | |
| 59 | /* Create an empty signal set. Add the specified signal. */ |
| 60 | __sigemptyset (&set); |
| 61 | __sigaddset (&set, sig); |
| 62 | |
| 63 | /* Remove the signal set from the current signal mask. */ |
| 64 | sigprocmask (SIG_UNBLOCK, &set, NULL); /* can't fail */ |
| 65 | |
| 66 | return oact.sa_handler; |
| 67 | } |