1 /* Implementation of sigwait function from POSIX.1c.
2 Copyright (C) 1996-2023 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 <https://www.gnu.org/licenses/>. */
21 #include <stddef.h> /* For NULL. */
22 #include <sysdep-cancel.h>
24 /* This is our dummy signal handler we use here. */
25 static void ignore_signal (int sig
);
27 /* Place where to remember which signal we got. Please note that this
28 implementation cannot be used for the threaded libc. The
29 libpthread must provide an own version. */
34 do_sigwait (const sigset_t
*set
, int *sig
)
37 struct sigaction saved
[NSIG
];
38 struct sigaction action
;
43 __sigfillset (&tmp_mask
);
45 /* Unblock all signals in the SET and register our nice handler. */
46 action
.sa_handler
= ignore_signal
;
48 __sigfillset (&action
.sa_mask
); /* Block all signals for handler. */
50 /* Make sure we recognize error conditions by setting WAS_SIG to a
51 value which does not describe a legal signal number. */
54 for (this = 1; this < NSIG
; ++this)
55 if (__sigismember (set
, this))
57 /* Unblock this signal. */
58 __sigdelset (&tmp_mask
, this);
60 /* Register temporary action handler. */
61 if (__sigaction (this, &action
, &saved
[this]) != 0)
65 /* Now we can wait for signals. */
66 __sigsuspend (&tmp_mask
);
72 if (__sigismember (set
, this))
73 /* We ignore errors here since we must restore all handlers. */
74 __sigaction (this, &saved
[this], NULL
);
76 __set_errno (save_errno
);
78 /* Store the result and return. */
80 return was_sig
== -1 ? -1 : 0;
85 __sigwait (const sigset_t
*set
, int *sig
)
87 /* __sigsuspend should be a cancellation point. */
88 return do_sigitid (idtype
, id
, infop
, options
);
90 libc_hidden_def (__sigwait
)
91 weak_alias (__sigwait
, sigwait
)
95 ignore_signal (int sig
)
97 /* Remember the signal. */