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
// 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/>.

//! File descriptor set
//!
//! Items defined in this module are mainly used for arguments to [`select`].
//!
//! [`select`]: crate::system::System::select

use super::Errno;
use nix::libc;
use std::os::fd::RawFd;
use thiserror::Error;
use yash_syntax::syntax::Fd;

/// Error indicating that the file descriptor is invalid
///
/// This error occurs when a specified file descriptor is negative or too large
/// (`>= libc::FD_SETSIZE`).
#[derive(Clone, Debug, Eq, Error, Hash, PartialEq)]
#[error("invalid file descriptor")]
pub struct InvalidFd;

/// Converts `InvalidFd` to `Errno::EBADF`.
impl From<InvalidFd> for Errno {
    fn from(InvalidFd: InvalidFd) -> Errno {
        Errno::EBADF
    }
}

fn validate(fd: Fd) -> Result<RawFd, InvalidFd> {
    if (0..(libc::FD_SETSIZE as _)).contains(&fd.0) {
        Ok(fd.0)
    } else {
        Err(InvalidFd)
    }
}

/// Set of file descriptors
#[derive(Clone, Copy, Debug, Eq)]
pub struct FdSet {
    /// Inner set of file descriptors
    pub(crate) inner: libc::fd_set,

    /// Some file descriptor that is larger than any file descriptor in the set
    ///
    /// This value is used to optimize the iteration over the file descriptors
    /// in the set. Only file descriptors below this value are considered.
    upper_bound: Fd,
}

impl FdSet {
    /// Creates a new empty set.
    #[must_use]
    pub fn new() -> Self {
        let inner = unsafe {
            let mut inner = std::mem::MaybeUninit::uninit();
            libc::FD_ZERO(inner.as_mut_ptr());
            inner.assume_init()
        };
        let upper_bound = Fd(0);
        Self { inner, upper_bound }
    }

    /// Inserts a file descriptor into the set.
    pub fn insert(&mut self, fd: Fd) -> Result<(), InvalidFd> {
        let fd = validate(fd)?;
        unsafe { libc::FD_SET(fd, &mut self.inner) };
        self.upper_bound = self.upper_bound.max(Fd(fd + 1));
        Ok(())
    }

    /// Removes a file descriptor from the set.
    pub fn remove(&mut self, fd: Fd) {
        if let Ok(fd) = validate(fd) {
            unsafe { libc::FD_CLR(fd, &mut self.inner) }
        }
    }

    /// Removes all file descriptors from the set.
    pub fn clear(&mut self) {
        unsafe { libc::FD_ZERO(&mut self.inner) }
    }

    /// Returns `true` if the set contains the file descriptor.
    #[must_use]
    pub fn contains(&self, fd: Fd) -> bool {
        match validate(fd) {
            Ok(fd) => unsafe { libc::FD_ISSET(fd, &self.inner) },
            Err(_) => false,
        }
    }

    /// Returns some file descriptor that is larger than any file descriptor in the set.
    #[must_use]
    pub fn upper_bound(&self) -> Fd {
        self.upper_bound
    }

    /// Returns an iterator over the file descriptors in the set.
    pub fn iter(&self) -> Iter {
        Iter {
            fd_set: self,
            range: 0..self.upper_bound.0,
        }
    }
}

impl Default for FdSet {
    fn default() -> Self {
        Self::new()
    }
}

impl PartialEq for FdSet {
    fn eq(&self, other: &Self) -> bool {
        // The upper bound does not affect the equality
        self.inner == other.inner
    }
}

impl std::hash::Hash for FdSet {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        // The upper bound does not affect the hash
        self.inner.hash(state)
    }
}

/// Iterator over the file descriptors in a set
#[derive(Debug)]
pub struct Iter<'a> {
    fd_set: &'a FdSet,
    range: std::ops::Range<RawFd>,
}

impl Iterator for Iter<'_> {
    type Item = Fd;

    fn next(&mut self) -> Option<Fd> {
        loop {
            let fd = Fd(self.range.next()?);
            if self.fd_set.contains(fd) {
                return Some(fd);
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.range.size_hint()
    }
}

impl DoubleEndedIterator for Iter<'_> {
    fn next_back(&mut self) -> Option<Fd> {
        loop {
            let fd = Fd(self.range.next_back()?);
            if self.fd_set.contains(fd) {
                return Some(fd);
            }
        }
    }
}

impl std::iter::FusedIterator for Iter<'_> {}

impl<'a> IntoIterator for &'a FdSet {
    type Item = Fd;
    type IntoIter = Iter<'a>;
    fn into_iter(self) -> Iter<'a> {
        self.iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_set_is_empty() {
        let set = FdSet::new();
        assert!(!set.contains(Fd::STDIN));
        assert!(!set.contains(Fd::STDOUT));
        assert!(!set.contains(Fd::STDERR));
        assert_eq!(set.iter().next(), None);
    }

    #[test]
    fn adding_fd_to_set() {
        let mut set = FdSet::new();
        set.insert(Fd::STDIN).unwrap();
        assert!(set.contains(Fd::STDIN));
    }

    #[test]
    fn adding_many_fds_to_set() {
        let mut set = FdSet::new();
        set.insert(Fd::STDOUT).unwrap();
        set.insert(Fd::STDERR).unwrap();
        set.insert(Fd(3)).unwrap();
        assert!(!set.contains(Fd::STDIN));
        assert!(set.contains(Fd::STDOUT));
        assert!(set.contains(Fd::STDERR));
        assert!(set.contains(Fd(3)));
    }

    #[test]
    fn adding_invalid_fd_to_set() {
        let mut set = FdSet::new();
        set.insert(Fd(-1)).unwrap_err();
        set.insert(Fd(libc::FD_SETSIZE as _)).unwrap_err();
    }

    #[test]
    fn removing_fd_from_set() {
        let mut set = FdSet::new();
        set.insert(Fd::STDIN).unwrap();

        set.remove(Fd::STDIN);
        assert!(!set.contains(Fd::STDIN));
    }

    #[test]
    fn clearing_set() {
        let mut set = FdSet::new();
        set.insert(Fd::STDOUT).unwrap();
        set.insert(Fd::STDERR).unwrap();

        set.clear();
        assert!(!set.contains(Fd::STDIN));
        assert!(!set.contains(Fd::STDOUT));
        assert!(!set.contains(Fd::STDERR));
        assert_eq!(set.iter().next(), None);
    }

    #[test]
    fn adding_fd_updates_upper_bound() {
        let mut set = FdSet::new();
        assert_eq!(set.upper_bound(), Fd(0));

        set.insert(Fd(1)).unwrap();
        assert_eq!(set.upper_bound(), Fd(2));

        set.insert(Fd(0)).unwrap();
        assert_eq!(set.upper_bound(), Fd(2));

        set.insert(Fd(2)).unwrap();
        assert_eq!(set.upper_bound(), Fd(3));

        set.remove(Fd(2));
        assert!(set.upper_bound() >= Fd(2), "{:?}", set.upper_bound());
    }

    #[test]
    fn equality_ignores_upper_bound() {
        let mut set = FdSet::new();
        assert_eq!(set, set);

        set.insert(Fd(1)).unwrap();
        set.insert(Fd(4)).unwrap();
        assert_eq!(set, set);

        let mut new_set = set;
        new_set.insert(Fd(5)).unwrap();
        assert_ne!(set, new_set);
        new_set.remove(Fd(5));
        assert_eq!(set, new_set);
    }

    #[test]
    fn iterating_fds() {
        let mut set = FdSet::new();
        set.insert(Fd(1)).unwrap();
        set.insert(Fd(6)).unwrap();
        set.insert(Fd(3)).unwrap();

        let fds = set.iter().collect::<Vec<_>>();
        assert_eq!(fds, [Fd(1), Fd(3), Fd(6)]);
    }

    #[test]
    fn reverse_iterating_fds() {
        let fd_max = Fd((libc::FD_SETSIZE - 1) as _);

        let mut set = FdSet::new();
        set.insert(Fd(0)).unwrap();
        set.insert(fd_max).unwrap();

        let fds = set.iter().rev().collect::<Vec<_>>();
        assert_eq!(fds, [fd_max, Fd(0)]);
    }
}