1 /* vi: set sw=4 ts=4: */
4 * Copyright (C) 2006 by Steven J. Hill <sjhill@realitydiluted.com>
5 * Copyright (C) 2003-2005 by Erik Andersen <andersen@uclibc.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * The GNU C Library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with the GNU C Library; see the file COPYING.LIB. If
19 * not, see <http://www.gnu.org/licenses/>.
24 #include <sys/syscall.h>
28 #if defined __NR_rt_sigtimedwait && defined __UCLIBC_HAS_REALTIME__
32 /* Return any pending signal or wait for one for the given time. */
33 static int __NC(sigwait
)(const sigset_t
*set
, int *sig
)
38 /* we might as well use sigtimedwait and do not care about cancellation */
39 ret
= __NC(sigtimedwait
)(set
, NULL
, NULL
);
40 while (ret
== -1 && errno
== EINTR
);
50 #else /* __NR_rt_sigtimedwait */
52 /* variant without REALTIME extensions */
53 #include <unistd.h> /* smallint */
55 static smallint was_sig
; /* obviously not thread-safe */
57 static void ignore_signal(int sig
)
62 static int __NC(sigwait
)(const sigset_t
*set
, int *sig
)
65 struct sigaction saved
[NSIG
];
66 struct sigaction action
;
71 __sigfillset (&tmp_mask
);
73 /* Unblock all signals in the SET and register our nice handler. */
74 action
.sa_handler
= ignore_signal
;
76 __sigfillset (&action
.sa_mask
); /* Block all signals for handler. */
78 /* Make sure we recognize error conditions by setting WAS_SIG to a
79 value which does not describe a legal signal number. */
82 for (this = 1; this < NSIG
; ++this)
83 if (__sigismember (set
, this))
85 /* Unblock this signal. */
86 __sigdelset (&tmp_mask
, this);
88 /* Register temporary action handler. */
89 /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
90 /* (so, will it work correctly if set has, say, SIGSTOP?) */
91 if (sigaction (this, &action
, &saved
[this]) != 0)
95 /* Now we can wait for signals. */
96 __NC(sigsuspend
)(&tmp_mask
);
102 if (__sigismember (set
, this))
103 /* We ignore errors here since we must restore all handlers. */
104 sigaction (this, &saved
[this], NULL
);
106 __set_errno (save_errno
);
108 /* Store the result and return. */
110 return was_sig
== -1 ? -1 : 0;
112 #endif /* __NR_rt_sigtimedwait */
114 CANCELLABLE_SYSCALL(int, sigwait
, (const sigset_t
*set
, int *sig
), (set
, sig
))