1 /* Implementation of sigwait function from POSIX.1c.
2 Copyright (C) 1996-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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, see
18 <http://www.gnu.org/licenses/>. */
22 #include <stddef.h> /* For NULL. */
23 #include <sysdep-cancel.h>
25 /* This is our dummy signal handler we use here. */
26 static void ignore_signal (int sig
);
28 /* Place where to remember which signal we got. Please note that this
29 implementation cannot be used for the threaded libc. The
30 libpthread must provide an own version. */
35 do_sigwait (const sigset_t
*set
, int *sig
)
38 struct sigaction saved
[NSIG
];
39 struct sigaction action
;
44 __sigfillset (&tmp_mask
);
46 /* Unblock all signals in the SET and register our nice handler. */
47 action
.sa_handler
= ignore_signal
;
49 __sigfillset (&action
.sa_mask
); /* Block all signals for handler. */
51 /* Make sure we recognize error conditions by setting WAS_SIG to a
52 value which does not describe a legal signal number. */
55 for (this = 1; this < NSIG
; ++this)
56 if (__sigismember (set
, this))
58 /* Unblock this signal. */
59 __sigdelset (&tmp_mask
, this);
61 /* Register temporary action handler. */
62 if (__sigaction (this, &action
, &saved
[this]) != 0)
66 /* Now we can wait for signals. */
67 __sigsuspend (&tmp_mask
);
73 if (__sigismember (set
, this))
74 /* We ignore errors here since we must restore all handlers. */
75 __sigaction (this, &saved
[this], NULL
);
77 __set_errno (save_errno
);
79 /* Store the result and return. */
81 return was_sig
== -1 ? -1 : 0;
86 __sigwait (const sigset_t
*set
, int *sig
)
89 return do_sigwait (set
, sig
);
91 int oldtype
= LIBC_CANCEL_ASYNC ();
93 int result
= do_sigwait (set
, sig
);
95 LIBC_CANCEL_RESET (oldtype
);
99 libc_hidden_def (__sigwait
)
100 weak_alias (__sigwait
, sigwait
)
104 ignore_signal (int sig
)
106 /* Remember the signal. */