signal: Add __libc_sigaction
[platform/upstream/glibc.git] / sysdeps / mach / hurd / libc_sigaction.c
1 /* Copyright (C) 1991-2021 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     {
37       errno = EINVAL;
38       return -1;
39     }
40
41   /* Copy so we fault before taking locks.  */
42   if (act != NULL)
43     a = *act;
44
45   ss = _hurd_self_sigstate ();
46
47   __spin_lock (&ss->critical_section_lock);
48   _hurd_sigstate_lock (ss);
49   old = _hurd_sigstate_actions (ss) [sig];
50   if (act != NULL)
51     _hurd_sigstate_actions (ss) [sig] = a;
52
53   if (act != NULL && sig == SIGCHLD
54       && (a.sa_flags & SA_NOCLDSTOP) != (old.sa_flags & SA_NOCLDSTOP))
55     {
56       _hurd_sigstate_unlock (ss);
57
58       /* Inform the proc server whether or not it should send us SIGCHLD for
59          stopped children.  We do this in a critical section so that no
60          SIGCHLD can arrive in the middle and be of indeterminate status.  */
61       __USEPORT (PROC,
62                  __proc_mod_stopchild (port, !(a.sa_flags & SA_NOCLDSTOP)));
63
64       _hurd_sigstate_lock (ss);
65       pending = _hurd_sigstate_pending (ss) & ~ss->blocked;
66     }
67   else if (act != NULL && (a.sa_handler == SIG_IGN || a.sa_handler == SIG_DFL))
68     /* We are changing to an action that might be to ignore SIG signals.
69        If SIG is blocked and pending and the new action is to ignore it, we
70        must remove it from the pending set now; if the action is changed
71        back and then SIG is unblocked, the signal pending now should not
72        arrive.  So wake up the signal thread to check the new state and do
73        the right thing.  */
74     pending = _hurd_sigstate_pending (ss) & __sigmask (sig);
75   else
76     pending = 0;
77
78   _hurd_sigstate_unlock (ss);
79   __spin_unlock (&ss->critical_section_lock);
80
81   if (pending)
82     __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
83
84   if (oact != NULL)
85     *oact = old;
86
87   return 0;
88 }
89 libc_hidden_def (__libc_sigaction)