]>
Commit | Line | Data |
---|---|---|
2b778ceb | 1 | /* Copyright (C) 1996-2021 Free Software Foundation, Inc. |
10dc2a90 | 2 | This file is part of the GNU C Library. |
0f110f41 | 3 | |
10dc2a90 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
0f110f41 | 8 | |
10dc2a90 UD |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
0f110f41 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
0f110f41 TBB |
17 | |
18 | #include <errno.h> | |
19 | #include <hurd.h> | |
20 | #include <hurd/signal.h> | |
21 | #include <hurd/msg.h> | |
22 | #include <hurd/sigpreempt.h> | |
23 | #include <assert.h> | |
f5123211 | 24 | #include <sysdep-cancel.h> |
0f110f41 TBB |
25 | |
26 | /* Select any of pending signals from SET or wait for any to arrive. */ | |
27 | int | |
28 | __sigwait (const sigset_t *set, int *sig) | |
29 | { | |
30 | struct hurd_sigstate *ss; | |
653d74f1 | 31 | sigset_t mask, ready, blocked; |
0f110f41 | 32 | int signo = 0; |
10dc2a90 | 33 | struct hurd_signal_preemptor preemptor; |
0f110f41 TBB |
34 | jmp_buf buf; |
35 | mach_port_t wait; | |
36 | mach_msg_header_t msg; | |
f5123211 | 37 | int cancel_oldtype; |
10dc2a90 | 38 | |
0f110f41 | 39 | sighandler_t |
10dc2a90 | 40 | preempt_fun (struct hurd_signal_preemptor *pe, |
0f110f41 TBB |
41 | struct hurd_sigstate *ss, |
42 | int *sigp, | |
43 | struct hurd_signal_detail *detail) | |
44 | { | |
45 | if (signo) | |
46 | /* We've already been run; don't interfere. */ | |
47 | return SIG_ERR; | |
10dc2a90 | 48 | |
0f110f41 TBB |
49 | signo = *sigp; |
50 | ||
51 | /* Make sure this is all kosher */ | |
52 | assert (__sigismember (&mask, signo)); | |
53 | ||
653d74f1 JK |
54 | /* Restore the blocking mask. */ |
55 | ss->blocked = blocked; | |
0f110f41 TBB |
56 | |
57 | return pe->handler; | |
58 | } | |
10dc2a90 | 59 | |
0f110f41 TBB |
60 | void |
61 | handler (int sig) | |
62 | { | |
63 | assert (sig == signo); | |
5e17a480 | 64 | longjmp (buf, 1); |
0f110f41 TBB |
65 | } |
66 | ||
67 | wait = __mach_reply_port (); | |
68 | ||
69 | if (set != NULL) | |
70 | /* Crash before locking */ | |
71 | mask = *set; | |
05dea6d1 RM |
72 | else |
73 | __sigemptyset (&mask); | |
10dc2a90 | 74 | |
0f110f41 | 75 | ss = _hurd_self_sigstate (); |
f5123211 | 76 | cancel_oldtype = LIBC_CANCEL_ASYNC(); |
653d74f1 | 77 | _hurd_sigstate_lock (ss); |
10dc2a90 | 78 | |
362f8329 | 79 | /* See if one of these signals is currently pending. */ |
653d74f1 JK |
80 | sigset_t pending = _hurd_sigstate_pending (ss); |
81 | __sigandset (&ready, &pending, &mask); | |
05dea6d1 | 82 | if (! __sigisemptyset (&ready)) |
0f110f41 TBB |
83 | { |
84 | for (signo = 1; signo < NSIG; signo++) | |
362f8329 | 85 | if (__sigismember (&ready, signo)) |
0f110f41 | 86 | { |
362f8329 | 87 | __sigdelset (&ready, signo); |
0f110f41 TBB |
88 | goto all_done; |
89 | } | |
90 | /* Huh? Where'd it go? */ | |
91 | abort (); | |
92 | } | |
93 | ||
362f8329 | 94 | /* Wait for one of them to show up. */ |
10dc2a90 | 95 | |
0f110f41 TBB |
96 | if (!setjmp (buf)) |
97 | { | |
10dc2a90 UD |
98 | /* Make the preemptor */ |
99 | preemptor.signals = mask; | |
100 | preemptor.first = 0; | |
101 | preemptor.last = -1; | |
102 | preemptor.preemptor = preempt_fun; | |
103 | preemptor.handler = handler; | |
104 | ||
105 | /* Install this preemptor */ | |
106 | preemptor.next = ss->preemptors; | |
107 | ss->preemptors = &preemptor; | |
108 | ||
653d74f1 JK |
109 | /* Unblock the expected signals */ |
110 | blocked = ss->blocked; | |
111 | ss->blocked &= ~mask; | |
112 | ||
113 | _hurd_sigstate_unlock (ss); | |
10dc2a90 | 114 | |
0f110f41 TBB |
115 | /* Wait. */ |
116 | __mach_msg (&msg, MACH_RCV_MSG, 0, sizeof (msg), wait, | |
117 | MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); | |
118 | abort (); | |
119 | } | |
120 | else | |
121 | { | |
122 | assert (signo); | |
123 | ||
653d74f1 | 124 | _hurd_sigstate_lock (ss); |
0f110f41 | 125 | |
10dc2a90 UD |
126 | /* Delete our preemptor. */ |
127 | assert (ss->preemptors == &preemptor); | |
128 | ss->preemptors = preemptor.next; | |
0f110f41 | 129 | } |
10dc2a90 | 130 | |
0f110f41 TBB |
131 | |
132 | all_done: | |
653d74f1 | 133 | _hurd_sigstate_unlock (ss); |
f5123211 | 134 | LIBC_CANCEL_RESET (cancel_oldtype); |
0f110f41 TBB |
135 | |
136 | __mach_port_destroy (__mach_task_self (), wait); | |
137 | *sig = signo; | |
138 | return 0; | |
139 | } | |
140 | ||
6e45e6ef | 141 | libc_hidden_def (__sigwait) |
0f110f41 | 142 | weak_alias (__sigwait, sigwait) |