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
//! Ctrl+C, turned into the cancellation flag the engine already reads.
//!
//! Invariant: **the handler does one thing and it is the one thing a handler is
//! allowed to do.** It stores `true` in an already-allocated `AtomicBool` and
//! returns. No allocation, no locking, no I/O, no exit: a console control
//! handler on Windows runs on a thread the operating system created inside this
//! process, and a `SIGINT` handler on Unix runs between two instructions of
//! whatever was executing, so anything that could block or allocate is a
//! deadlock waiting for the wrong moment.
//!
//! ## Why this exists (task-1932, H11)
//!
//! `Connection::cancel` in the driver was correct and unreachable. The flag the
//! executor polls every batch was created by `command::run` and dropped by it,
//! so nothing outside the call could set it, and a long statement in
//! `inillucent-shell` could only be stopped by killing the process - which
//! loses the shell's history and any open transaction's chance to roll back
//! cleanly.
//!
//! A person pressing Ctrl+C means "stop what you are doing", not "stop being a
//! program". The second press still ends the process, because the operating
//! system's default handler is restored once this one has fired: a statement
//! that ignores the flag, or a wait inside the operating system that never
//! reaches a check, has to be escapable.
//!
//! ## Why the `unsafe` is here rather than nowhere
//!
//! There is no way to be told about Ctrl+C in the standard library. Both
//! platforms offer exactly one call - `SetConsoleCtrlHandler` and `signal` -
//! and both are FFI. The whole of this crate's `unsafe` is the two calls below,
//! each installing a handler and reading nothing; `crates/inillucent-cli/src/interrupt.rs`
//! is the one file named in `policy.rs`'s `UNSAFE_ALLOWED` for this crate, and
//! the rest of the crate still refuses the word.
use ;
use ;
/// The flags a Ctrl+C sets, one per session that asked to be interruptible.
///
/// A `Mutex<Vec<_>>` rather than a single flag because a process can hold more
/// than one surface - the shell opens one and a `.read` of a script could open
/// another - and because the handler must not care how many there are.
/// Sets every registered flag.
///
/// Called from the handler, so it takes the lock without blocking: a handler
/// that waited for a lock held by the thread it interrupted would deadlock the
/// process. A contended lock means another thread is registering a flag at this
/// instant, and the press is dropped rather than waited for - the next one is a
/// tenth of a second away, and the second press ends the process anyway.
/// Registers a flag for Ctrl+C to set, installing the handler on first use.
///
/// Safe to call more than once; the handler is installed once.
///
/// @param flag - the session's cancellation flag
/// Installs the console control handler.
/// What Windows calls on Ctrl+C.
///
/// Returns 1 for the two events this handles, which tells Windows the press was
/// dealt with and stops the default handler from ending the process. Every
/// other event - a close, a logoff, a shutdown - returns 0, because those are
/// not "stop what you are doing" and a program that refused them would be a
/// program the operating system has to kill.
///
/// @param event - which control event arrived
extern "system"
/// Installs the `SIGINT` handler.
/// What the operating system calls on `SIGINT`.
///
/// @param _signal - which signal arrived, always `SIGINT` here
extern "C"