]>
Commit | Line | Data |
---|---|---|
ba1ffaa1 UD |
1 | /* sigwait - implementation of sigwait function from POSIX.1c. |
2 | Copyright (C) 1996 Free Software Foundation, Inc. | |
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 | |
7 | modify it under the terms of the GNU Library General Public License as | |
8 | published by the Free Software Foundation; either version 2 of the | |
9 | License, or (at your option) any later version. | |
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 | |
14 | Library General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Library General Public | |
17 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include <assert.h> | |
22 | #include <errno.h> | |
23 | #include <signal.h> | |
24 | ||
25 | ||
26 | /* This is our dummy signal handler we use here. */ | |
27 | static void ignore_signal (int sig); | |
28 | ||
29 | /* Place where to remember which signal we got. Please note that this | |
30 | implementation cannot be used for the threaded libc. The | |
31 | libpthread must provide an own version. */ | |
32 | static int was_sig; | |
33 | ||
34 | ||
35 | int | |
36 | __sigwait (const sigset_t *set, int *sig) | |
37 | { | |
38 | sigset_t tmp_mask; | |
39 | struct sigaction saved[NSIG]; | |
40 | struct sigaction action; | |
41 | int save_errno; | |
42 | int this; | |
43 | ||
44 | /* Prepare set. */ | |
84384f5b | 45 | __sigfillset (&tmp_mask); |
ba1ffaa1 UD |
46 | |
47 | /* Unblock all signals in the SET and register our nice handler. */ | |
48 | action.sa_handler = ignore_signal; | |
49 | action.sa_flags = 0; | |
84384f5b | 50 | __sigfillset (&action.sa_mask); /* Block all signals for handler. */ |
ba1ffaa1 UD |
51 | |
52 | /* Make sure we recognize error conditions by setting WAS_SIG to a | |
53 | value which does not describe a legal signal number. */ | |
54 | was_sig = -1; | |
55 | ||
56 | for (this = 0; this < NSIG; ++this) | |
84384f5b | 57 | if (__sigismember (set, this)) |
ba1ffaa1 UD |
58 | { |
59 | /* Unblock this signal. */ | |
84384f5b | 60 | __sigdelset (&tmp_mask, this); |
ba1ffaa1 UD |
61 | |
62 | /* Register temporary action handler. */ | |
84384f5b | 63 | if (__sigaction (this, &action, &saved[this]) != 0) |
ba1ffaa1 UD |
64 | goto restore_handler; |
65 | } | |
66 | ||
67 | /* Now we can wait for signals. */ | |
84384f5b | 68 | __sigsuspend (&tmp_mask); |
ba1ffaa1 UD |
69 | |
70 | restore_handler: | |
71 | save_errno = errno; | |
72 | ||
73 | while (--this >= 0) | |
84384f5b | 74 | if (__sigismember (set, this)) |
ba1ffaa1 | 75 | /* We ignore errors here since we must restore all handlers. */ |
84384f5b | 76 | __sigaction (this, &saved[this], NULL); |
ba1ffaa1 UD |
77 | |
78 | __set_errno (save_errno); | |
79 | ||
80 | /* Store the result and return. */ | |
81 | *sig = was_sig; | |
82 | return was_sig == -1 ? -1 : 0; | |
83 | } | |
84 | weak_alias (__sigwait, sigwait) | |
85 | ||
86 | ||
87 | static void | |
88 | ignore_signal (int sig) | |
89 | { | |
90 | /* Remember the signal. */ | |
91 | was_sig = sig; | |
92 | } |