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
use std::{
    iter::FromIterator,
    mem::MaybeUninit,
    task::{Context, Poll},
};

use super::SignalKind;

// Required to enable polyfills on non-Unix platforms when documenting.
#[cfg(not(unix))]
use super::libc_polyfill as libc;

/// A stream for receiving a set of signals.
#[derive(Debug)]
pub struct SignalSet {
    _private: (),
}

impl SignalSet {
    /// Returns a builder for constructing an instance.
    #[inline]
    pub fn builder() -> SignalSetBuilder {
        SignalSetBuilder::new()
    }

    /// Receive the next signal notification event.
    #[inline]
    pub async fn recv(&mut self) -> Option<SignalKind> {
        crate::util::poll_fn(|cx| self.poll_recv(cx)).await
    }

    /// Poll to receive the next signal notification event, outside of an
    /// `async` context.
    pub fn poll_recv(
        &mut self,
        _cx: &mut Context<'_>,
    ) -> Poll<Option<SignalKind>> {
        unimplemented!()
    }
}

cfg_stream! {
    impl futures::stream::Stream for SignalSet {
        type Item = SignalKind;

        #[inline]
        fn poll_next(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut Context<'_>
        ) -> Poll<Option<SignalKind>> {
            self.poll_recv(cx)
        }
    }
}

/// Constructs a [`SignalSet`] using the builder pattern.
///
/// Signals that cannot be handled are not listed as methods.
///
/// [`SignalSet`]: struct.SignalSet.html
#[derive(Clone, Copy)]
pub struct SignalSetBuilder {
    signal_set: libc::sigset_t,
}

impl FromIterator<SignalKind> for SignalSetBuilder {
    #[inline]
    fn from_iter<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = SignalKind>,
    {
        iter.into_iter()
            .fold(Self::new(), |builder, signal| builder.with(signal))
    }
}

impl Extend<SignalKind> for SignalSetBuilder {
    #[inline]
    fn extend<I>(&mut self, iter: I)
    where
        I: IntoIterator<Item = SignalKind>,
    {
        iter.into_iter().for_each(|signal| self.insert(signal));
    }
}

impl Default for SignalSetBuilder {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl SignalSetBuilder {
    /// Creates a new, empty signal set builder.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        unsafe {
            let mut set = MaybeUninit::<libc::sigset_t>::uninit();
            libc::sigemptyset(set.as_mut_ptr());
            Self::from_raw(set.assume_init())
        }
    }

    /// Creates a new builder from the raw `signal_set`.
    ///
    /// # Safety
    ///
    /// This library assumes that all signals used are valid. Supplying an
    /// unsupported signal set invalidates this assumption.
    #[inline]
    #[must_use]
    pub const unsafe fn from_raw(signal_set: libc::sigset_t) -> Self {
        Self { signal_set }
    }

    /// Returns the raw value of this signal set builder.
    #[inline]
    #[must_use]
    pub const fn into_raw(self) -> libc::sigset_t {
        self.signal_set
    }

    /// The set of signals that result in process termination.
    #[inline]
    #[must_use]
    pub fn termination_set(self) -> Self {
        self.alarm()
            .hangup()
            .interrupt()
            .pipe()
            .quit()
            .terminate()
            .user_defined_1()
            .user_defined_2()
    }

    // REMINDER: When updating the documentation of the following methods, their
    // corresponding `SignalKind` constants must be updated as well.

    /// The `SIGALRM` signal; sent when a real-time timer expires.
    ///
    /// **Default behavior:** process termination.
    #[inline]
    #[must_use]
    pub fn alarm(self) -> Self {
        self.with(SignalKind::ALARM)
    }

    /// The `SIGCHLD` signal; sent when the status of a child process changes.
    ///
    /// **Default behavior:** ignored.
    #[inline]
    #[must_use]
    pub fn child(self) -> Self {
        self.with(SignalKind::CHILD)
    }

    /// The `SIGHUP` signal; sent when the terminal is disconnected.
    ///
    /// **Default behavior:** process termination.
    #[inline]
    #[must_use]
    pub fn hangup(self) -> Self {
        self.with(SignalKind::HANGUP)
    }

    /// The `SIGINFO` signal; sent to request a status update from the process.
    ///
    /// **Not supported on:** `android`, `emscripten`, `linux`.
    ///
    /// **Keyboard shortcut:** `CTRL` + `T`.
    ///
    /// **Default behavior:** ignored.
    #[cfg(any(
        not(any(
            target_os = "android",
            target_os = "emscripten",
            target_os = "linux",
        )),
        docsrs,
    ))]
    // This doesn't seem to change docs to list the supported target OSes.
    #[cfg_attr(
        docsrs,
        doc(not(any(
            target_os = "android",
            target_os = "emscripten",
            target_os = "linux",
        )))
    )]
    #[inline]
    #[must_use]
    pub fn info(self) -> Self {
        self.with(SignalKind::INFO)
    }

    /// The `SIGINT` signal; sent to interrupt a program.
    ///
    /// **Keyboard shortcut:** `CTRL` + `C`.
    ///
    /// **Default behavior:** process termination.
    #[inline]
    #[must_use]
    pub fn interrupt(self) -> Self {
        self.with(SignalKind::INTERRUPT)
    }

    /// The `SIGIO` signal; sent when I/O operations are possible on some file
    /// descriptor.
    ///
    /// **Default behavior:** ignored.
    #[inline]
    #[must_use]
    pub fn io(self) -> Self {
        self.with(SignalKind::IO)
    }

    /// The `SIGPIPE` signal; sent when the process attempts to write to a pipe
    /// which has no reader.
    ///
    /// **Default behavior:** process termination.
    #[inline]
    #[must_use]
    pub fn pipe(self) -> Self {
        self.with(SignalKind::PIPE)
    }

    /// The `SIGQUIT` signal; sent to issue a shutdown of the process, after
    /// which the OS will dump the process core.
    ///
    /// **Keyboard shortcut:** `CTRL` + `\`.
    ///
    /// **Default behavior:** process termination.
    #[inline]
    #[must_use]
    pub fn quit(self) -> Self {
        self.with(SignalKind::QUIT)
    }

    /// The `SIGTERM` signal; sent to issue a shutdown of the process.
    ///
    /// **Default behavior:** process termination.
    #[inline]
    #[must_use]
    pub fn terminate(self) -> Self {
        self.with(SignalKind::TERMINATE)
    }

    /// The `SIGUSR1` signal; a user defined signal.
    ///
    /// **Default behavior:** process termination.
    #[inline]
    #[must_use]
    pub fn user_defined_1(self) -> Self {
        self.with(SignalKind::USER_DEFINED_1)
    }

    /// The `SIGUSR2` signal; a user defined signal.
    ///
    /// **Default behavior:** process termination.
    #[inline]
    #[must_use]
    pub fn user_defined_2(self) -> Self {
        self.with(SignalKind::USER_DEFINED_2)
    }

    /// The `SIGWINCH` signal; sent when the terminal window is resized.
    ///
    /// **Default behavior:** ignored.
    #[inline]
    #[must_use]
    pub fn window_change(self) -> Self {
        self.with(SignalKind::WINDOW_CHANGE)
    }

    /// Returns `self` with `signal` added to it.
    #[inline]
    #[must_use]
    pub fn with(mut self, signal: SignalKind) -> Self {
        self.insert(signal);
        self
    }

    /// Adds `signal` to `self`.
    #[inline]
    pub fn insert(&mut self, signal: SignalKind) {
        unsafe {
            libc::sigaddset(&mut self.signal_set, signal.into_raw());
        }
    }
}