-
Notifications
You must be signed in to change notification settings - Fork 3
/
signal.rs
422 lines (372 loc) · 18.7 KB
/
signal.rs
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2023 WATANABE Yuki
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Functions about signals
use super::super::Signal;
pub(super) use crate::signal::*;
use std::num::NonZeroI32;
/// Signal number for `SIGABRT` in the virtual system
///
/// POSIX effectively requires that the signal number for `SIGABRT` is 6.
pub const SIGABRT: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(6) });
/// Signal number for `SIGALRM` in the virtual system
///
/// POSIX effectively requires that the signal number for `SIGALRM` is 14.
pub const SIGALRM: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(14) });
/// Signal number for `SIGBUS` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGBUS: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(101) });
/// Signal number for `SIGCHLD` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGCHLD: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(102) });
/// Signal number for `SIGCLD` in the virtual system
///
/// Currently, this is the same as `SIGCHLD`.
pub const SIGCLD: Number = SIGCHLD;
/// Signal number for `SIGCONT` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGCONT: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(103) });
/// Signal number for `SIGEMT` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGEMT: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(104) });
/// Signal number for `SIGFPE` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGFPE: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(105) });
/// Signal number for `SIGHUP` in the virtual system
///
/// POSIX effectively requires that the signal number for `SIGHUP` is 1.
pub const SIGHUP: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(1) });
/// Signal number for `SIGILL` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGILL: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(106) });
/// Signal number for `SIGINFO` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGINFO: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(107) });
/// Signal number for `SIGINT` in the virtual system
///
/// POSIX effectively requires that the signal number for `SIGINT` is 2.
pub const SIGINT: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(2) });
/// Signal number for `SIGIO` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGIO: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(108) });
/// Signal number for `SIGIOT` in the virtual system
///
/// Currently, this is the same as `SIGABRT`.
pub const SIGIOT: Number = SIGABRT;
/// Signal number for `SIGKILL` in the virtual system
///
/// POSIX effectively requires that the signal number for `SIGKILL` is 9.
pub const SIGKILL: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(9) });
/// Signal number for `SIGLOST` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGLOST: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(109) });
/// Signal number for `SIGPIPE` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGPIPE: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(110) });
/// Signal number for `SIGPOLL` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGPOLL: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(111) });
/// Signal number for `SIGPROF` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGPROF: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(112) });
/// Signal number for `SIGPWR` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGPWR: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(113) });
/// Signal number for `SIGQUIT` in the virtual system
///
/// POSIX effectively requires that the signal number for `SIGQUIT` is 3.
pub const SIGQUIT: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(3) });
/// Signal number for `SIGSEGV` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGSEGV: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(114) });
/// Signal number for `SIGSTKFLT` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGSTKFLT: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(115) });
/// Signal number for `SIGSTOP` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGSTOP: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(116) });
/// Signal number for `SIGSYS` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGSYS: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(117) });
/// Signal number for `SIGTERM` in the virtual system
///
/// POSIX effectively requires that the signal number for `SIGTERM` is 15.
pub const SIGTERM: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(15) });
/// Signal number for `SIGTHR` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGTHR: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(118) });
/// Signal number for `SIGTRAP` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGTRAP: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(119) });
/// Signal number for `SIGTSTP` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGTSTP: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(120) });
/// Signal number for `SIGTTIN` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGTTIN: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(121) });
/// Signal number for `SIGTTOU` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGTTOU: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(122) });
/// Signal number for `SIGURG` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGURG: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(123) });
/// Signal number for `SIGUSR1` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGUSR1: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(124) });
/// Signal number for `SIGUSR2` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGUSR2: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(125) });
/// Signal number for `SIGVTALRM` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGVTALRM: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(126) });
/// Signal number for `SIGWINCH` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGWINCH: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(127) });
/// Signal number for `SIGXCPU` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGXCPU: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(128) });
/// Signal number for `SIGXFSZ` in the virtual system
///
/// Note that this is not the same as the signal number in the real system.
pub const SIGXFSZ: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(129) });
/// Signal number for `SIGRTMIN` in the virtual system
///
/// The current implementation supports nine real-time signals (201 through 209).
pub const SIGRTMIN: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(201) });
/// Signal number for `SIGRTMAX` in the virtual system
///
/// The current implementation supports nine real-time signals (201 through 209).
pub const SIGRTMAX: Number = Number::from_raw_unchecked(unsafe { NonZeroI32::new_unchecked(209) });
/// Range of the real-time signals supported by the virtual system.
const RT_RANGE: std::ops::RangeInclusive<RawNumber> = SIGRTMIN.as_raw()..=SIGRTMAX.as_raw();
impl Name {
pub(super) fn to_raw_virtual(self) -> Option<Number> {
fn rt(base: Number, n: RawNumber) -> Option<Number> {
let number = base.as_raw().checked_add(n)?;
let non_zero = NonZeroI32::new(number)?;
RT_RANGE
.contains(&number)
.then(|| Number::from_raw_unchecked(non_zero))
}
match self {
Self::Abrt => Some(SIGABRT),
Self::Alrm => Some(SIGALRM),
Self::Bus => Some(SIGBUS),
Self::Chld => Some(SIGCHLD),
Self::Cld => Some(SIGCLD),
Self::Cont => Some(SIGCONT),
Self::Emt => Some(SIGEMT),
Self::Fpe => Some(SIGFPE),
Self::Hup => Some(SIGHUP),
Self::Ill => Some(SIGILL),
Self::Info => Some(SIGINFO),
Self::Int => Some(SIGINT),
Self::Io => Some(SIGIO),
Self::Iot => Some(SIGIOT),
Self::Kill => Some(SIGKILL),
Self::Lost => Some(SIGLOST),
Self::Pipe => Some(SIGPIPE),
Self::Poll => Some(SIGPOLL),
Self::Prof => Some(SIGPROF),
Self::Pwr => Some(SIGPWR),
Self::Quit => Some(SIGQUIT),
Self::Segv => Some(SIGSEGV),
Self::Stkflt => Some(SIGSTKFLT),
Self::Stop => Some(SIGSTOP),
Self::Sys => Some(SIGSYS),
Self::Term => Some(SIGTERM),
Self::Thr => Some(SIGTHR),
Self::Trap => Some(SIGTRAP),
Self::Tstp => Some(SIGTSTP),
Self::Ttin => Some(SIGTTIN),
Self::Ttou => Some(SIGTTOU),
Self::Urg => Some(SIGURG),
Self::Usr1 => Some(SIGUSR1),
Self::Usr2 => Some(SIGUSR2),
Self::Vtalrm => Some(SIGVTALRM),
Self::Winch => Some(SIGWINCH),
Self::Xcpu => Some(SIGXCPU),
Self::Xfsz => Some(SIGXFSZ),
Self::Rtmin(n) => rt(SIGRTMIN, n),
Self::Rtmax(n) => rt(SIGRTMAX, n),
}
}
/// Returns the name for the raw signal number for the virtual system.
///
/// This function returns `None` if the given number is not a valid signal.
pub(super) fn try_from_raw_virtual(number: RawNumber) -> Option<Self> {
match () {
() if number == SIGABRT.as_raw() => Some(Self::Abrt),
() if number == SIGALRM.as_raw() => Some(Self::Alrm),
() if number == SIGBUS.as_raw() => Some(Self::Bus),
() if number == SIGCHLD.as_raw() => Some(Self::Chld),
() if number == SIGCLD.as_raw() => Some(Self::Cld),
() if number == SIGCONT.as_raw() => Some(Self::Cont),
() if number == SIGEMT.as_raw() => Some(Self::Emt),
() if number == SIGFPE.as_raw() => Some(Self::Fpe),
() if number == SIGHUP.as_raw() => Some(Self::Hup),
() if number == SIGILL.as_raw() => Some(Self::Ill),
() if number == SIGINFO.as_raw() => Some(Self::Info),
() if number == SIGINT.as_raw() => Some(Self::Int),
() if number == SIGIO.as_raw() => Some(Self::Io),
() if number == SIGIOT.as_raw() => Some(Self::Iot),
() if number == SIGKILL.as_raw() => Some(Self::Kill),
() if number == SIGLOST.as_raw() => Some(Self::Lost),
() if number == SIGPIPE.as_raw() => Some(Self::Pipe),
() if number == SIGPOLL.as_raw() => Some(Self::Poll),
() if number == SIGPROF.as_raw() => Some(Self::Prof),
() if number == SIGPWR.as_raw() => Some(Self::Pwr),
() if number == SIGQUIT.as_raw() => Some(Self::Quit),
() if number == SIGSEGV.as_raw() => Some(Self::Segv),
() if number == SIGSTKFLT.as_raw() => Some(Self::Stkflt),
() if number == SIGSTOP.as_raw() => Some(Self::Stop),
() if number == SIGSYS.as_raw() => Some(Self::Sys),
() if number == SIGTERM.as_raw() => Some(Self::Term),
() if number == SIGTHR.as_raw() => Some(Self::Thr),
() if number == SIGTRAP.as_raw() => Some(Self::Trap),
() if number == SIGTSTP.as_raw() => Some(Self::Tstp),
() if number == SIGTTIN.as_raw() => Some(Self::Ttin),
() if number == SIGTTOU.as_raw() => Some(Self::Ttou),
() if number == SIGURG.as_raw() => Some(Self::Urg),
() if number == SIGUSR1.as_raw() => Some(Self::Usr1),
() if number == SIGUSR2.as_raw() => Some(Self::Usr2),
() if number == SIGVTALRM.as_raw() => Some(Self::Vtalrm),
() if number == SIGWINCH.as_raw() => Some(Self::Winch),
() if number == SIGXCPU.as_raw() => Some(Self::Xcpu),
() if number == SIGXFSZ.as_raw() => Some(Self::Xfsz),
() if RT_RANGE.contains(&number) => {
// Return a name relative to `Rtmin` or `Rtmax`,
// whichever is closer to the given number.
let incr = number - SIGRTMIN.as_raw();
debug_assert!(incr >= 0);
let decr = number - SIGRTMAX.as_raw();
debug_assert!(decr <= 0);
debug_assert!(decr > RawNumber::MIN);
if incr <= -decr {
Some(Self::Rtmin(incr))
} else {
Some(Self::Rtmax(decr))
}
}
_ => None,
}
}
}
// TODO Remove this
impl Number {
/// Converts a signal number in the real system to a signal number in the virtual system.
pub(super) fn from_signal_virtual(signal: Signal) -> Self {
use crate::system::System as _;
unsafe { crate::RealSystem::new() }
.validate_signal(signal as RawNumber)
.and_then(|(name, _real_number)| name.to_raw_virtual())
.unwrap()
}
/// Converts a signal number in the virtual system to a signal number in the real system.
pub(super) fn to_signal_virtual(self) -> Option<Signal> {
use crate::system::System as _;
unsafe { crate::RealSystem::new() }
.signal_number_from_name(Name::try_from_raw_virtual(self.as_raw())?)?
.as_raw()
.try_into()
.ok()
}
}
/// Default effect of a signal delivered to a process.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum SignalEffect {
/// Does nothing.
None,
/// Terminates the process.
Terminate { core_dump: bool },
/// Suspends the process.
Suspend,
/// Resumes the process.
Resume,
}
impl SignalEffect {
/// Returns the default effect for the specified signal.
///
/// This function returns `Terminate { core_dump: true }` for `Rtmin(n)` and
/// `Rtmax(n)` whatever `n` is.
#[must_use]
pub const fn of(signal: Name) -> Self {
match signal {
Name::Abrt => Self::Terminate { core_dump: true },
Name::Alrm => Self::Terminate { core_dump: false },
Name::Bus => Self::Terminate { core_dump: true },
Name::Chld | Name::Cld => Self::None,
Name::Cont => Self::Resume,
Name::Emt => Self::Terminate { core_dump: false },
Name::Fpe => Self::Terminate { core_dump: true },
Name::Hup => Self::Terminate { core_dump: false },
Name::Ill => Self::Terminate { core_dump: true },
Name::Info => Self::Terminate { core_dump: false },
Name::Int => Self::Terminate { core_dump: false },
Name::Io => Self::Terminate { core_dump: false },
Name::Iot => Self::Terminate { core_dump: true },
Name::Kill => Self::Terminate { core_dump: false },
Name::Lost => Self::Terminate { core_dump: false },
Name::Pipe => Self::Terminate { core_dump: false },
Name::Poll => Self::Terminate { core_dump: false },
Name::Prof => Self::Terminate { core_dump: false },
Name::Pwr => Self::Terminate { core_dump: false },
Name::Quit => Self::Terminate { core_dump: true },
Name::Segv => Self::Terminate { core_dump: true },
Name::Stkflt => Self::Terminate { core_dump: false },
Name::Stop => Self::Suspend,
Name::Sys => Self::Terminate { core_dump: true },
Name::Term => Self::Terminate { core_dump: false },
Name::Thr => Self::Terminate { core_dump: false },
Name::Trap => Self::Terminate { core_dump: true },
Name::Tstp | Name::Ttin | Name::Ttou => Self::Suspend,
Name::Urg => Self::None,
Name::Usr1 | Name::Usr2 => Self::Terminate { core_dump: false },
Name::Vtalrm => Self::Terminate { core_dump: false },
Name::Winch => Self::None,
Name::Xcpu => Self::Terminate { core_dump: true },
Name::Xfsz => Self::Terminate { core_dump: true },
Name::Rtmin(_) | Name::Rtmax(_) => Self::Terminate { core_dump: false },
}
}
}