1 | /* Copyright (C) 1996-2022 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | |
4 | The GNU C Library is free software; you can redistribute it and/or |
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. |
8 | |
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 |
12 | Lesser General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU Lesser General Public |
15 | License along with the GNU C Library; if not, see |
16 | <https://www.gnu.org/licenses/>. */ |
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> |
24 | #include <sysdep-cancel.h> |
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; |
31 | sigset_t mask, ready, blocked; |
32 | int signo = 0; |
33 | struct hurd_signal_preemptor preemptor; |
34 | jmp_buf buf; |
35 | mach_port_t wait; |
36 | mach_msg_header_t msg; |
37 | int cancel_oldtype; |
38 | |
39 | sighandler_t |
40 | preempt_fun (struct hurd_signal_preemptor *pe, |
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; |
48 | |
49 | signo = *sigp; |
50 | |
51 | /* Make sure this is all kosher */ |
52 | assert (__sigismember (&mask, signo)); |
53 | |
54 | /* Restore the blocking mask. */ |
55 | ss->blocked = blocked; |
56 | |
57 | return pe->handler; |
58 | } |
59 | |
60 | void |
61 | handler (int sig) |
62 | { |
63 | assert (sig == signo); |
64 | longjmp (buf, 1); |
65 | } |
66 | |
67 | wait = __mach_reply_port (); |
68 | |
69 | if (set != NULL) |
70 | /* Crash before locking */ |
71 | mask = *set; |
72 | else |
73 | __sigemptyset (set: &mask); |
74 | |
75 | ss = _hurd_self_sigstate (); |
76 | cancel_oldtype = LIBC_CANCEL_ASYNC(); |
77 | _hurd_sigstate_lock (ss); |
78 | |
79 | /* See if one of these signals is currently pending. */ |
80 | sigset_t pending = _hurd_sigstate_pending (ss); |
81 | __sigandset (dest: &ready, left: &pending, right: &mask); |
82 | if (! __sigisemptyset (set: &ready)) |
83 | { |
84 | for (signo = 1; signo < NSIG; signo++) |
85 | if (__sigismember (set: &ready, sig: signo)) |
86 | { |
87 | __sigdelset (set: &ready, sig: signo); |
88 | goto all_done; |
89 | } |
90 | /* Huh? Where'd it go? */ |
91 | abort (); |
92 | } |
93 | |
94 | /* Wait for one of them to show up. */ |
95 | |
96 | if (!setjmp (buf)) |
97 | { |
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 | |
109 | /* Unblock the expected signals */ |
110 | blocked = ss->blocked; |
111 | ss->blocked &= ~mask; |
112 | |
113 | _hurd_sigstate_unlock (ss); |
114 | |
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 | |
124 | _hurd_sigstate_lock (ss); |
125 | |
126 | /* Delete our preemptor. */ |
127 | assert (ss->preemptors == &preemptor); |
128 | ss->preemptors = preemptor.next; |
129 | } |
130 | |
131 | |
132 | all_done: |
133 | _hurd_sigstate_unlock (ss); |
134 | LIBC_CANCEL_RESET (cancel_oldtype); |
135 | |
136 | __mach_port_destroy (__mach_task_self (), wait); |
137 | *sig = signo; |
138 | return 0; |
139 | } |
140 | |
141 | libc_hidden_def (__sigwait) |
142 | weak_alias (__sigwait, sigwait) |
143 | |