Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / mach / hurd / sigaction.c
bloba0620de2bd0fdf5414ef81362862e3cec76e8620
1 /* Copyright (C) 1991-2014 Free Software Foundation, Inc.
3 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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <signal.h>
21 #include <hurd.h>
22 #include <hurd/signal.h>
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 __sigaction (sig, act, oact)
28 int sig;
29 const struct sigaction *act;
30 struct sigaction *oact;
32 struct hurd_sigstate *ss;
33 struct sigaction a, old;
34 sigset_t pending;
36 if (sig <= 0 || sig >= NSIG ||
37 (act != NULL && act->sa_handler != SIG_DFL &&
38 ((__sigmask (sig) & _SIG_CANT_MASK) ||
39 act->sa_handler == SIG_ERR)))
41 errno = EINVAL;
42 return -1;
45 /* Copy so we fault before taking locks. */
46 if (act != NULL)
47 a = *act;
49 ss = _hurd_self_sigstate ();
51 __spin_lock (&ss->critical_section_lock);
52 __spin_lock (&ss->lock);
53 old = ss->actions[sig];
54 if (act != NULL)
55 ss->actions[sig] = a;
57 if (act != NULL && sig == SIGCHLD &&
58 (a.sa_flags & SA_NOCLDSTOP) != (old.sa_flags & SA_NOCLDSTOP))
60 __spin_unlock (&ss->lock);
62 /* Inform the proc server whether or not it should send us SIGCHLD for
63 stopped children. We do this in a critical section so that no
64 SIGCHLD can arrive in the middle and be of indeterminate status. */
65 __USEPORT (PROC,
66 __proc_mod_stopchild (port, !(a.sa_flags & SA_NOCLDSTOP)));
68 __spin_lock (&ss->lock);
69 pending = ss->pending & ~ss->blocked;
71 else if (act != NULL && (a.sa_handler == SIG_IGN || a.sa_handler == SIG_DFL))
72 /* We are changing to an action that might be to ignore SIG signals.
73 If SIG is blocked and pending and the new action is to ignore it, we
74 must remove it from the pending set now; if the action is changed
75 back and then SIG is unblocked, the signal pending now should not
76 arrive. So wake up the signal thread to check the new state and do
77 the right thing. */
78 pending = ss->pending & __sigmask (sig);
79 else
80 pending = 0;
82 __spin_unlock (&ss->lock);
83 __spin_unlock (&ss->critical_section_lock);
85 if (pending)
86 __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
88 if (oact != NULL)
89 *oact = old;
91 return 0;
93 libc_hidden_def (__sigaction)
94 weak_alias (__sigaction, sigaction)