terminal-trx 0.1.0

Provides a handle to the terminal of the current process
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use crate::StdioLocks;
use libc::{c_int, fcntl, termios, F_GETFL, O_RDWR};
use std::ffi::{CStr, CString, OsStr};
use std::fmt;
use std::fs::{File, OpenOptions};
use std::io::{self, stderr, stdin, stdout, IsTerminal};
use std::mem::{self, ManuallyDrop};
use std::ops::{Deref, DerefMut};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd as _};
use std::os::unix::ffi::OsStrExt;

mod attr;

pub(crate) fn terminal() -> io::Result<Terminal> {
    None.or_else(|| reuse_tty_from_stdio(stderr).transpose())
        .or_else(|| reuse_tty_from_stdio(stdout).transpose())
        .or_else(|| reuse_tty_from_stdio(stdin).transpose())
        .map(|r| r.and_then(Terminal::from_stdio))
        .unwrap_or_else(|| Ok(Terminal::from_controlling(open_controlling_tty()?)))
}

fn reuse_tty_from_stdio<S: IsTerminal + AsFd>(
    stream: impl FnOnce() -> S,
) -> io::Result<Option<TerminalFile>> {
    let stream = stream();

    if stream.is_terminal() {
        // This branch here is a bit questionable to me:
        // I've seen a lot of code that re-uses the standard I/O fd if possible.
        // But I don't quite understand what the benefit of that is. Is it to have as little fds open as possible?
        // Is it a lot faster than opening the tty ourselves?
        if is_read_write(stream.as_fd())? {
            // SAFETY: We know that the file descriptor is valid.
            // However we break the assumption that the file descriptor is owned.
            // That's why the file is immediately wrapped in a ManuallyDrop to prevent
            // the standard I/O descriptor from being closed.
            let file = unsafe { File::from_raw_fd(stream.as_fd().as_raw_fd()) };
            Ok(Some(TerminalFile::Borrowed(ManuallyDrop::new(file))))
        } else {
            reopen_tty(stream.as_fd())
                .map(TerminalFile::Owned)
                .map(Some)
        }
    } else {
        Ok(None)
    }
}

fn open_controlling_tty() -> io::Result<TerminalFile> {
    OpenOptions::new()
        .read(true)
        .write(true)
        .open("/dev/tty")
        .map(TerminalFile::Owned)
}

fn is_read_write(fd: BorrowedFd) -> io::Result<bool> {
    // SAFETY: We know that the file descriptor is valid.
    let mode = to_io_result(unsafe { fcntl(fd.as_raw_fd(), F_GETFL) })?;
    Ok(mode & O_RDWR == O_RDWR)
}

fn reopen_tty(fd: BorrowedFd) -> io::Result<File> {
    let name = ttyname_r(fd)?;
    OpenOptions::new()
        .read(true)
        .write(true)
        .open(OsStr::from_bytes(name.as_bytes()))
}

fn is_same_file(a: BorrowedFd, b: BorrowedFd) -> io::Result<bool> {
    Ok(a.as_raw_fd() == b.as_raw_fd() || {
        let stat_a = fstat(a)?;
        let stat_b = fstat(b)?;
        stat_a.st_dev == stat_b.st_dev && stat_a.st_ino == stat_b.st_ino
    })
}

fn fstat(fd: BorrowedFd) -> io::Result<libc::stat> {
    // SAFETY: If fstat is successful, then we get a valid stat structure.
    let mut stat = unsafe { mem::zeroed() };
    // SAFETY: We know that the file descriptor is valid.
    to_io_result(unsafe { libc::fstat(fd.as_raw_fd(), &mut stat) })?;
    Ok(stat)
}

#[derive(Debug)]
pub(crate) struct Terminal {
    file: TerminalFile,
    same_as_stdin: bool,
    same_as_stdout: bool,
    same_as_stderr: bool,
}

impl Terminal {
    pub(crate) fn lock_stdio(&self) -> StdioLocks {
        StdioLocks {
            stdin_lock: self.same_as_stdin.then(|| stdin().lock()),
            stdout_lock: self.same_as_stdout.then(|| stdout().lock()),
            stderr_lock: self.same_as_stderr.then(|| stderr().lock()),
        }
    }

    pub(crate) fn enable_raw_mode(&mut self) -> io::Result<RawModeGuard<'_>> {
        let fd = self.file.as_fd();
        let old_termios = attr::get_terminal_attr(fd)?;

        if !attr::is_raw_mode_enabled(&old_termios) {
            let mut termios = old_termios;
            attr::enable_raw_mode(&mut termios);
            attr::set_terminal_attr(fd, &termios)?;
            Ok(RawModeGuard {
                inner: self,
                old_termios: Some(old_termios),
            })
        } else {
            Ok(RawModeGuard {
                inner: self,
                old_termios: None,
            })
        }
    }
}

impl Terminal {
    fn from_stdio(file: TerminalFile) -> io::Result<Self> {
        Ok(Terminal {
            same_as_stdin: is_same_file(file.as_fd(), stdin().as_fd())?,
            same_as_stdout: is_same_file(file.as_fd(), stdout().as_fd())?,
            same_as_stderr: is_same_file(file.as_fd(), stderr().as_fd())?,
            file,
        })
    }

    fn from_controlling(file: TerminalFile) -> Self {
        Terminal {
            file,
            same_as_stdin: false,
            same_as_stdout: false,
            same_as_stderr: false,
        }
    }
}

#[derive(Debug)]
enum TerminalFile {
    Owned(File),
    Borrowed(ManuallyDrop<File>),
}

impl io::Write for Terminal {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.file.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.file.flush()
    }
}

impl io::Read for Terminal {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.file.read(buf)
    }
}

impl Deref for TerminalFile {
    type Target = File;

    fn deref(&self) -> &Self::Target {
        match self {
            TerminalFile::Owned(f) => f,
            TerminalFile::Borrowed(f) => f,
        }
    }
}

impl DerefMut for TerminalFile {
    fn deref_mut(&mut self) -> &mut Self::Target {
        match self {
            TerminalFile::Owned(f) => f,
            TerminalFile::Borrowed(f) => f,
        }
    }
}

impl AsFd for super::Terminal {
    fn as_fd(&self) -> std::os::unix::prelude::BorrowedFd<'_> {
        self.0.file.as_fd()
    }
}

impl AsFd for super::TerminalLock<'_> {
    fn as_fd(&self) -> std::os::unix::prelude::BorrowedFd<'_> {
        self.inner.file.as_fd()
    }
}

impl AsFd for super::RawModeGuard<'_> {
    fn as_fd(&self) -> std::os::unix::prelude::BorrowedFd<'_> {
        self.0.inner.file.as_fd()
    }
}

impl AsRawFd for super::Terminal {
    fn as_raw_fd(&self) -> std::os::unix::prelude::RawFd {
        self.0.file.as_raw_fd()
    }
}

impl AsRawFd for super::TerminalLock<'_> {
    fn as_raw_fd(&self) -> std::os::unix::prelude::RawFd {
        self.inner.file.as_raw_fd()
    }
}

impl AsRawFd for super::RawModeGuard<'_> {
    fn as_raw_fd(&self) -> std::os::unix::prelude::RawFd {
        self.0.inner.file.as_raw_fd()
    }
}

pub(crate) struct RawModeGuard<'a> {
    inner: &'a mut Terminal,
    old_termios: Option<termios>,
}

impl fmt::Debug for RawModeGuard<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RawModeGuard")
            .field("inner", &self.inner)
            .finish_non_exhaustive()
    }
}

impl io::Write for RawModeGuard<'_> {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.inner.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

impl io::Read for RawModeGuard<'_> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.read(buf)
    }
}

impl Drop for RawModeGuard<'_> {
    fn drop(&mut self) {
        if let Some(old_termios) = self.old_termios {
            _ = attr::set_terminal_attr(self.inner.file.as_fd(), &old_termios);
        }
    }
}

fn to_io_result(value: c_int) -> io::Result<c_int> {
    if value == -1 {
        Err(io::Error::last_os_error())
    } else {
        Ok(value)
    }
}

/// `ttyname_r` returns the path to the terminal device.
#[cfg(not(target_os = "macos"))]
fn ttyname_r(fd: BorrowedFd) -> io::Result<CString> {
    let mut buf = Vec::with_capacity(64);

    loop {
        // SAFETY: We pass the capacity of our vec to ttyname_r.
        let code = unsafe { libc::ttyname_r(fd.as_raw_fd(), buf.as_mut_ptr(), buf.capacity()) };
        match code {
            // SAFETY: We own the pointer and we know that if ttyname_r is successful, it returns a null-terminated string.
            0 => return Ok(unsafe { CStr::from_ptr(buf.as_ptr()) }.to_owned()),
            libc::ERANGE => buf.reserve(64),
            code => return Err(io::Error::from_raw_os_error(code)),
        }
    }
}

/// macOS does not have `ttyname_r` (the race free version), so we have to resort to `fcntl`.
#[cfg(target_os = "macos")]
fn ttyname_r(fd: BorrowedFd) -> io::Result<CString> {
    use libc::{F_GETPATH, PATH_MAX};

    // the buffer size must be >= MAXPATHLEN, see `man fcntl`
    let buf: [i8; PATH_MAX as usize] = [0; PATH_MAX as usize];

    unsafe {
        match fcntl(fd.as_raw_fd(), F_GETPATH as c_int, &buf) {
            0 => {
                let res = CStr::from_ptr(buf.as_ptr()).to_owned();
                Ok(res)
            }
            _ => Err(io::Error::last_os_error()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use libc::{grantpt, unlockpt};
    use std::env;
    use std::io::Write;
    use std::os::fd::OwnedFd;

    #[test]
    fn ttyname_r_returns_successfully() {
        let (_controlling_fd, user_fd) = create_pty_pair().unwrap();
        let name = ttyname_r(user_fd.as_fd()).unwrap();
        let name_as_str = name.to_str().unwrap();
        assert!(!name_as_str.is_empty());
        assert!(name_as_str.starts_with("/dev/"));
    }

    #[test]
    fn reopened_tty_can_be_written_to() {
        let (_controlling_fd, user_fd) = create_pty_pair().unwrap();
        let mut tty = reopen_tty(user_fd.as_fd()).unwrap();
        tty.write_all(b"foo").unwrap();
    }

    #[test]
    fn is_read_write_returns_true_for_read_write_fd() {
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .open("/dev/null")
            .unwrap();
        assert!(is_read_write(file.as_fd()).unwrap());
    }

    #[test]
    fn is_read_write_returns_false_for_read_only_fd() {
        let file = OpenOptions::new().read(true).open("/dev/null").unwrap();
        assert!(!is_read_write(file.as_fd()).unwrap());
    }

    #[test]
    fn is_read_write_returns_false_for_write_only_fd() {
        let file = OpenOptions::new().write(true).open("/dev/null").unwrap();
        assert!(!is_read_write(file.as_fd()).unwrap());
    }

    #[test]
    fn is_same_file_with_same_fd() {
        let file = stdin();
        let fd = file.as_fd();
        assert!(is_same_file(fd, fd).unwrap());
    }

    #[test]
    fn is_same_file_with_different_fd_but_same_underlying_file() {
        let file_1 = OpenOptions::new().read(true).open("/dev/null").unwrap();
        let file_2 = OpenOptions::new().read(true).open("/dev/null").unwrap();
        assert!(file_1.as_raw_fd() != file_2.as_raw_fd());
        assert!(is_same_file(file_1.as_fd(), file_2.as_fd()).unwrap());
    }

    #[test]
    fn is_not_same_file_with_different_underlying_file() {
        let file_1 = OpenOptions::new()
            .read(true)
            .open(env::args().next().unwrap())
            .unwrap();
        let file_2 = OpenOptions::new().read(true).open("/dev/null").unwrap();
        assert!(!is_same_file(file_1.as_fd(), file_2.as_fd()).unwrap());
    }

    fn create_pty_pair() -> io::Result<(OwnedFd, OwnedFd)> {
        let controlling_fd = openpty()?;
        let user_fd: OwnedFd = File::open(OsStr::from_bytes(
            ptsname_r(controlling_fd.as_fd())?.as_bytes(),
        ))?
        .into();
        Ok((controlling_fd, user_fd))
    }

    fn openpty() -> io::Result<OwnedFd> {
        // O_RDWR:
        //   Open the device for both reading and writing.
        // O_NOCTTY:
        //   Do not make this device the controlling terminal for the process.
        // SAFETY: We check that the file descriptor is valid (not -1).
        let fd = to_io_result(unsafe { libc::posix_openpt(libc::O_RDWR | libc::O_NOCTTY) })?;
        // SAFETY: We just created the fd, so we know it's valid.
        to_io_result(unsafe { grantpt(fd) })?;
        // SAFETY: We just created the fd, so we know it's valid.
        to_io_result(unsafe { unlockpt(fd) })?;
        // SAFETY: posix_openpt creates a new fd for us.
        Ok(unsafe { OwnedFd::from_raw_fd(fd) })
    }

    #[cfg(not(target_os = "macos"))]
    fn ptsname_r(fd: BorrowedFd) -> io::Result<CString> {
        let mut buf = Vec::with_capacity(64);

        loop {
            // SAFETY: We pass the capacity of our vec to ptsname_r.
            let code = unsafe { libc::ptsname_r(fd.as_raw_fd(), buf.as_mut_ptr(), buf.capacity()) };
            match code {
                // SAFETY: We own the pointer and we know that if ptsname_r is successful, it returns a null-terminated string.
                0 => return Ok(unsafe { CStr::from_ptr(buf.as_ptr()).to_owned() }),
                libc::ERANGE => buf.reserve(64),
                code => return Err(io::Error::from_raw_os_error(code)),
            }
        }
    }

    /// macOS does not have `ptsname_r` (the race free version), so we have to resort to `ioctl`.
    #[cfg(target_os = "macos")]
    fn ptsname_r(fd: BorrowedFd) -> io::Result<CString> {
        // This is based on
        // https://github.com/Mobivity/nix-ptsname_r-shim/blob/master/src/lib.rs
        // which in turn is based on
        // https://blog.tarq.io/ptsname-on-osx-with-rust/
        // and its derivative
        // https://github.com/philippkeller/rexpect/blob/a71dd02/src/process.rs#L67
        use libc::{c_ulong, ioctl, TIOCPTYGNAME};

        // the buffer size on OSX is 128, defined by sys/ttycom.h
        let buf: [i8; 128] = [0; 128];

        // SAFETY: Our buffer is big enough according to the docs.
        // Creating the CStr is also ok, since we know that we get back a null-terminated string.
        unsafe {
            match ioctl(fd.as_raw_fd(), TIOCPTYGNAME as c_ulong, &buf) {
                0 => {
                    let res = CStr::from_ptr(buf.as_ptr()).to_owned();
                    Ok(res)
                }
                _ => Err(io::Error::last_os_error()),
            }
        }
    }
}