]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/posix/sigwait.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / sysdeps / posix / sigwait.c
CommitLineData
19361cb7 1/* Implementation of sigwait function from POSIX.1c.
04277e02 2 Copyright (C) 1996-2019 Free Software Foundation, Inc.
ba1ffaa1
UD
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
5
6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
ba1ffaa1
UD
10
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
41bdb6e2 14 Lesser General Public License for more details.
ba1ffaa1 15
41bdb6e2 16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
ba1ffaa1 19
ba1ffaa1
UD
20#include <errno.h>
21#include <signal.h>
19361cb7 22#include <stddef.h> /* For NULL. */
6ee8d334 23#include <sysdep-cancel.h>
ba1ffaa1
UD
24
25/* This is our dummy signal handler we use here. */
26static void ignore_signal (int sig);
27
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. */
31static int was_sig;
32
33
6ee8d334
UD
34static int
35do_sigwait (const sigset_t *set, int *sig)
ba1ffaa1
UD
36{
37 sigset_t tmp_mask;
38 struct sigaction saved[NSIG];
39 struct sigaction action;
40 int save_errno;
41 int this;
42
43 /* Prepare set. */
84384f5b 44 __sigfillset (&tmp_mask);
ba1ffaa1
UD
45
46 /* Unblock all signals in the SET and register our nice handler. */
47 action.sa_handler = ignore_signal;
48 action.sa_flags = 0;
84384f5b 49 __sigfillset (&action.sa_mask); /* Block all signals for handler. */
ba1ffaa1
UD
50
51 /* Make sure we recognize error conditions by setting WAS_SIG to a
52 value which does not describe a legal signal number. */
53 was_sig = -1;
54
ec91ea7c 55 for (this = 1; this < NSIG; ++this)
84384f5b 56 if (__sigismember (set, this))
ba1ffaa1
UD
57 {
58 /* Unblock this signal. */
84384f5b 59 __sigdelset (&tmp_mask, this);
ba1ffaa1
UD
60
61 /* Register temporary action handler. */
84384f5b 62 if (__sigaction (this, &action, &saved[this]) != 0)
ba1ffaa1
UD
63 goto restore_handler;
64 }
65
66 /* Now we can wait for signals. */
84384f5b 67 __sigsuspend (&tmp_mask);
ba1ffaa1
UD
68
69 restore_handler:
70 save_errno = errno;
71
ec91ea7c 72 while (--this >= 1)
84384f5b 73 if (__sigismember (set, this))
ba1ffaa1 74 /* We ignore errors here since we must restore all handlers. */
84384f5b 75 __sigaction (this, &saved[this], NULL);
ba1ffaa1
UD
76
77 __set_errno (save_errno);
78
79 /* Store the result and return. */
80 *sig = was_sig;
81 return was_sig == -1 ? -1 : 0;
82}
6ee8d334
UD
83
84
85int
86__sigwait (const sigset_t *set, int *sig)
87{
ce7eb0e9
AZ
88 /* __sigsuspend should be a cancellation point. */
89 return do_sigitid (idtype, id, infop, options);
6ee8d334 90}
bf2cc5fb 91libc_hidden_def (__sigwait)
ba1ffaa1
UD
92weak_alias (__sigwait, sigwait)
93
94
95static void
96ignore_signal (int sig)
97{
98 /* Remember the signal. */
99 was_sig = sig;
100}