3 * Copyright (C) 2006 by Steven J. Hill <sjhill@realitydiluted.com>
4 * Copyright (C) 2003-2005 by Erik Andersen <andersen@uclibc.org>
6 * This program 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; see the file COPYING.LIB. If
18 * not, see <http://www.gnu.org/licenses/>.
23 #include <sys/syscall.h>
27 #if defined __NR_rt_sigtimedwait && defined __UCLIBC_HAS_REALTIME__
31 /* Return any pending signal or wait for one for the given time. */
32 static int __NC(sigwait
)(const sigset_t
*set
, int *sig
)
37 /* we might as well use sigtimedwait and do not care about cancellation */
38 ret
= __NC(sigtimedwait
)(set
, NULL
, NULL
);
39 while (ret
== -1 && errno
== EINTR
);
49 #else /* __NR_rt_sigtimedwait */
51 /* variant without REALTIME extensions */
52 #include <unistd.h> /* smallint */
54 static smallint was_sig
; /* obviously not thread-safe */
56 static void ignore_signal(int sig
)
61 static int __NC(sigwait
)(const sigset_t
*set
, int *sig
)
64 struct sigaction saved
[NSIG
];
65 struct sigaction action
;
70 __sigfillset (&tmp_mask
);
72 /* Unblock all signals in the SET and register our nice handler. */
73 action
.sa_handler
= ignore_signal
;
75 __sigfillset (&action
.sa_mask
); /* Block all signals for handler. */
77 /* Make sure we recognize error conditions by setting WAS_SIG to a
78 value which does not describe a legal signal number. */
81 for (this = 1; this < NSIG
; ++this)
82 if (__sigismember (set
, this))
84 /* Unblock this signal. */
85 __sigdelset (&tmp_mask
, this);
87 /* Register temporary action handler. */
88 /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
89 /* (so, will it work correctly if set has, say, SIGSTOP?) */
90 if (sigaction (this, &action
, &saved
[this]) != 0)
94 /* Now we can wait for signals. */
95 __NC(sigsuspend
)(&tmp_mask
);
101 if (__sigismember (set
, this))
102 /* We ignore errors here since we must restore all handlers. */
103 sigaction (this, &saved
[this], NULL
);
105 __set_errno (save_errno
);
107 /* Store the result and return. */
109 return was_sig
== -1 ? -1 : 0;
111 #endif /* __NR_rt_sigtimedwait */
113 CANCELLABLE_SYSCALL(int, sigwait
, (const sigset_t
*set
, int *sig
), (set
, sig
))