| 1 | /* Copyright (C) 1991-2024 Free Software Foundation, Inc. |
| 2 | |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 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 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <signal.h> |
| 21 | #include <hurd.h> |
| 22 | #include <hurd/signal.h> |
| 23 | |
| 24 | /* If ACT is not NULL, change the action for SIG to *ACT. |
| 25 | If OACT is not NULL, put the old action for SIG in *OACT. */ |
| 26 | int |
| 27 | __libc_sigaction (int sig, const struct sigaction *act, |
| 28 | struct sigaction *oact) |
| 29 | { |
| 30 | struct hurd_sigstate *ss; |
| 31 | struct sigaction a, old; |
| 32 | sigset_t pending; |
| 33 | |
| 34 | if (act != NULL && act->sa_handler != SIG_DFL |
| 35 | && ((__sigmask (sig) & _SIG_CANT_MASK) || act->sa_handler == SIG_ERR)) |
| 36 | return __hurd_fail (EINVAL); |
| 37 | |
| 38 | /* Copy so we fault before taking locks. */ |
| 39 | if (act != NULL) |
| 40 | a = *act; |
| 41 | |
| 42 | ss = _hurd_self_sigstate (); |
| 43 | |
| 44 | __spin_lock (&ss->critical_section_lock); |
| 45 | _hurd_sigstate_lock (ss); |
| 46 | old = _hurd_sigstate_actions (ss) [sig]; |
| 47 | if (act != NULL) |
| 48 | _hurd_sigstate_actions (ss) [sig] = a; |
| 49 | |
| 50 | if (act != NULL && sig == SIGCHLD |
| 51 | && (a.sa_flags & SA_NOCLDSTOP) != (old.sa_flags & SA_NOCLDSTOP)) |
| 52 | { |
| 53 | _hurd_sigstate_unlock (ss); |
| 54 | |
| 55 | /* Inform the proc server whether or not it should send us SIGCHLD for |
| 56 | stopped children. We do this in a critical section so that no |
| 57 | SIGCHLD can arrive in the middle and be of indeterminate status. */ |
| 58 | __USEPORT (PROC, |
| 59 | __proc_mod_stopchild (port, !(a.sa_flags & SA_NOCLDSTOP))); |
| 60 | |
| 61 | _hurd_sigstate_lock (ss); |
| 62 | pending = _hurd_sigstate_pending (ss) & ~ss->blocked; |
| 63 | } |
| 64 | else if (act != NULL && (a.sa_handler == SIG_IGN || a.sa_handler == SIG_DFL)) |
| 65 | /* We are changing to an action that might be to ignore SIG signals. |
| 66 | If SIG is blocked and pending and the new action is to ignore it, we |
| 67 | must remove it from the pending set now; if the action is changed |
| 68 | back and then SIG is unblocked, the signal pending now should not |
| 69 | arrive. So wake up the signal thread to check the new state and do |
| 70 | the right thing. */ |
| 71 | pending = _hurd_sigstate_pending (ss) & __sigmask (sig); |
| 72 | else |
| 73 | pending = 0; |
| 74 | |
| 75 | _hurd_sigstate_unlock (ss); |
| 76 | __spin_unlock (&ss->critical_section_lock); |
| 77 | |
| 78 | if (pending) |
| 79 | __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ()); |
| 80 | |
| 81 | if (oact != NULL) |
| 82 | *oact = old; |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | libc_hidden_def (__libc_sigaction) |
| 87 | |