1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//! Requests to stop, and other asynchronous notifications: Unix signals
//! and Windows console control events, received without running any of the
//! caller's code in a signal handler.
use fmt;
use Duration;
use crateClock;
use crate::;
/// An asynchronous notification from the system to the process: a signal
/// on Linux and macOS, a console control event on Windows.
///
/// Three exist everywhere:
///
/// | | Linux, macOS | Windows |
/// |---|---|---|
/// | [`INTERRUPT`](Self::INTERRUPT) | `SIGINT` | `CTRL_C_EVENT` |
/// | [`TERMINATE`](Self::TERMINATE) | `SIGTERM` | `CTRL_SHUTDOWN_EVENT` |
/// | [`HANGUP`](Self::HANGUP) | `SIGHUP` | `CTRL_CLOSE_EVENT` |
///
/// The others are the system's own: any signal number through
/// `unix::SignalExt`, and `windows::CTRL_BREAK` and `windows::CTRL_LOGOFF`.
///
/// What a process may still do once it has been notified differs. A signal
/// is only a message: the process goes on for as long as it likes. So does
/// it after Ctrl-C or Ctrl-Break on Windows. But when Windows closes the
/// console, logs the user off or shuts down, the notification is a notice
/// period: the process is terminated a few seconds later whatever it does
/// (about 5 seconds; the delay is the system's to choose), and at once
/// if it lets the system know it is done, which is what returning from
/// `main` or calling [`exit`](super::exit) does. To give the program all
/// of that time, oscall's handler does not return for these three events.
i32);
/// How a backend's wait for a notification ended.
pub
/// A subscription to a set of [`Signal`]s: while it exists they are
/// recorded instead of doing what they do by default (most terminate the
/// process), and the program asks for them when it suits it.
///
/// No code of the caller ever runs in a signal handler. oscall installs
/// its own, which does the only things that are safe there (it sets a flag
/// and wakes a descriptor), and the program learns of the signal from an
/// ordinary thread, with [`try_next`](Self::try_next) or
/// [`wait`](Self::wait).
///
/// ```no_run
/// use oscall::process::{Signal, Signals};
///
/// let signals = Signals::new(&[Signal::INTERRUPT, Signal::TERMINATE])?;
/// // Work, until asked to stop.
/// while signals.try_next().is_none() {
/// // ...
/// }
/// // Or, in a thread of its own: sleep until then.
/// let signal = signals.wait(None)?;
/// # Ok::<(), oscall::Error>(())
/// ```
///
/// # What is reported
///
/// That a signal was delivered since the last time it was reported, not
/// how many times: deliveries that the program has not yet been told of
/// merge into one, as they do in the system itself. Each signal of the set
/// has its own flag, so different signals never hide one another. A signal
/// that came before the subscription is not kept for it; one that the
/// system was still delivering at that very moment may be reported.
///
/// # One subscription per signal
///
/// Dispositions belong to the whole process. Two `Signals` cannot have a
/// signal in common: the second [`new`](Self::new) fails with
/// [`ErrorKind::ResourceBusy`]. Several with different signals are fine,
/// and one can be shared between threads.
///
/// Dropping a `Signals` puts back what it replaced, and what happened in
/// between and was not asked for is forgotten. From then on a signal of
/// the set does what it did before, which for most is to terminate the
/// process: that includes one that was sent while the subscription
/// existed and is delivered a moment after its end. A program that drops
/// its `Signals` while the signal can still come is terminated by it
/// exactly as if it had never listened; keep the subscription for as long
/// as that is not wanted.
///
/// - **Linux, macOS**: the previous action of each signal is restored
/// (`sigaction`). A handler that was there before does not run while
/// the subscription exists: oscall does not chain. A signal that was
/// ignored (as `nohup` does for `SIGHUP`) is listened to all the same.
/// Signals are not blocked: a system call that one of them interrupts
/// in another thread is restarted where the system can do it
/// (`SA_RESTART`), and fails with [`ErrorKind::Interrupted`] where it
/// cannot.
///
/// If other code has installed an action of its own in the meantime,
/// that one is left alone. Should that code later put back what it had
/// found, which is oscall's handler (C's `system` does this around the
/// command it runs), the handler finds that nobody listens when the
/// signal next comes: it then restores the previous action itself and
/// sends the signal again, to the same thread, for that action to take
/// it. A new `Signals` never takes oscall's own handler for the action
/// to go back to.
///
/// **macOS** cannot safely turn a signal that has a handler into one
/// that is ignored. Its kernel decides to deliver a signal, then reads
/// the action again: if "ignore" has replaced the handler in between,
/// the process jumps to address 1 and dies; if "default" has, the
/// process is terminated, even by a signal whose default is to do
/// nothing (`SIGCHLD`, `SIGWINCH`, `SIGURG`, `SIGIO`, `SIGINFO`,
/// `SIGCONT`). So where the action to go back to amounts to ignoring
/// the signal, oscall's handler stays in place on macOS and discards
/// the signal. To the process that is the same, with two differences:
/// a program it starts otherwise than with `Command` finds the default
/// action instead of "ignore", and `SIGCHLD` discarded by a handler does
/// not make the system collect the children, as ignoring it does.
/// - **Windows**: the handler is added to the process's list
/// (`SetConsoleCtrlHandler`) with the first subscription and removed
/// with the last. It answers for the events subscribed to, and passes
/// the others on to the handlers installed before it.
///
/// # Waiting with other things
///
/// On every system that has signals, a
/// [`Poller`](crate::event::Poller) waits for a `Signals` along with
/// sockets and timers:
/// [`Poller::register_signals`](crate::event::Poller::register_signals).
/// Call `try_next` until it returns `None` each time the poller reports
/// it.
///
/// On Linux and macOS a `Signals` also implements
/// [`AsHandle`](crate::AsHandle): the handle (an `eventfd`, the read end
/// of a pipe) is readable when [`try_next`](Self::try_next) may have
/// something to return, and stops being so once it has returned `None`.
/// It is there to be waited for by [`event::poll`](crate::event::poll)
/// (`event::unix::poll_entry`) or by a poller in another mode
/// (`event::unix::register`, level- or edge-triggered), and must not be
/// read from. Call `try_next` until it returns `None` each time the
/// handle is reported: that call leaves the handle not readable, until
/// the next signal of the set is delivered, so that a level-triggered
/// poller does not report it again for nothing. (To that end `try_next`
/// waits for a handler that has recorded a signal and not yet woken the
/// handle, the matter of an instant. It does not wait for ever: if the
/// thread such a handler runs in is kept from running for long, the
/// handle is reported once more with nothing to take.)
///
/// # WebAssembly
///
/// There are no signals: [`new`](Self::new) fails with
/// [`ErrorKind::Unsupported`].
;
/// On Linux and macOS, the handle that is readable when a signal may be
/// pending: see [`Signals`].