2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / sigaction.c
blobfe452e828311b840f589cd3b31b26f372d5e82c8
1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2002, 2007
2 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <signal.h>
23 #include <hurd.h>
24 #include <hurd/signal.h>
26 /* If ACT is not NULL, change the action for SIG to *ACT.
27 If OACT is not NULL, put the old action for SIG in *OACT. */
28 int
29 __sigaction (sig, act, oact)
30 int sig;
31 const struct sigaction *act;
32 struct sigaction *oact;
34 struct hurd_sigstate *ss;
35 struct sigaction a, old;
36 sigset_t pending;
38 if (sig <= 0 || sig >= NSIG ||
39 (act != NULL && act->sa_handler != SIG_DFL &&
40 ((__sigmask (sig) & _SIG_CANT_MASK) ||
41 act->sa_handler == SIG_ERR)))
43 errno = EINVAL;
44 return -1;
47 /* Copy so we fault before taking locks. */
48 if (act != NULL)
49 a = *act;
51 ss = _hurd_self_sigstate ();
53 __spin_lock (&ss->critical_section_lock);
54 __spin_lock (&ss->lock);
55 old = ss->actions[sig];
56 if (act != NULL)
57 ss->actions[sig] = a;
59 if (act != NULL && sig == SIGCHLD &&
60 (a.sa_flags & SA_NOCLDSTOP) != (old.sa_flags & SA_NOCLDSTOP))
62 __spin_unlock (&ss->lock);
64 /* Inform the proc server whether or not it should send us SIGCHLD for
65 stopped children. We do this in a critical section so that no
66 SIGCHLD can arrive in the middle and be of indeterminate status. */
67 __USEPORT (PROC,
68 __proc_mod_stopchild (port, !(a.sa_flags & SA_NOCLDSTOP)));
70 __spin_lock (&ss->lock);
71 pending = ss->pending & ~ss->blocked;
73 else if (act != NULL && (a.sa_handler == SIG_IGN || a.sa_handler == SIG_DFL))
74 /* We are changing to an action that might be to ignore SIG signals.
75 If SIG is blocked and pending and the new action is to ignore it, we
76 must remove it from the pending set now; if the action is changed
77 back and then SIG is unblocked, the signal pending now should not
78 arrive. So wake up the signal thread to check the new state and do
79 the right thing. */
80 pending = ss->pending & __sigmask (sig);
81 else
82 pending = 0;
84 __spin_unlock (&ss->lock);
85 __spin_unlock (&ss->critical_section_lock);
87 if (pending)
88 __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
90 if (oact != NULL)
91 *oact = old;
93 return 0;
95 libc_hidden_def (__sigaction)
96 weak_alias (__sigaction, sigaction)