|
| 1 | +//! Create master and slave virtual pseudo-terminals (PTYs) |
| 2 | +
|
| 3 | +use std::ffi::CStr; |
| 4 | +use std::mem; |
| 5 | +use std::os::unix::prelude::*; |
| 6 | + |
| 7 | +use libc; |
| 8 | + |
| 9 | +use {Error, fcntl, Result}; |
| 10 | + |
| 11 | +/// Representation of the Master device in a master/slave pty pair |
| 12 | +/// |
| 13 | +/// While this datatype is a thin wrapper around `RawFd`, it enforces that the available PTY |
| 14 | +/// functions are given the correct file descriptor. Additionally this type implements `Drop`, |
| 15 | +/// so that when it's consumed or goes out of scope, it's automatically cleaned-up. |
| 16 | +#[derive(Debug)] |
| 17 | +pub struct PtyMaster(RawFd); |
| 18 | + |
| 19 | +impl AsRawFd for PtyMaster { |
| 20 | + fn as_raw_fd(&self) -> RawFd { |
| 21 | + self.0 |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl IntoRawFd for PtyMaster { |
| 26 | + fn into_raw_fd(self) -> RawFd { |
| 27 | + let fd = self.0; |
| 28 | + mem::forget(self); |
| 29 | + fd |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl Drop for PtyMaster { |
| 34 | + fn drop(&mut self) { |
| 35 | + // Errors when closing are ignored because we don't actually know if the file descriptor |
| 36 | + // was closed. If we retried, it's possible that descriptor was reallocated in the mean |
| 37 | + // time and the wrong file descriptor could be closed. |
| 38 | + let _ = ::unistd::close(self.0); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Grant access to a slave pseudoterminal (see |
| 43 | +/// [grantpt(3)](http://man7.org/linux/man-pages/man3/grantpt.3.html)) |
| 44 | +/// |
| 45 | +/// `grantpt()` changes the mode and owner of the slave pseudoterminal device corresponding to the |
| 46 | +/// master pseudoterminal referred to by `fd`. This is a necessary step towards opening the slave. |
| 47 | +#[inline] |
| 48 | +pub fn grantpt(fd: &PtyMaster) -> Result<()> { |
| 49 | + if unsafe { libc::grantpt(fd.as_raw_fd()) } < 0 { |
| 50 | + return Err(Error::last().into()); |
| 51 | + } |
| 52 | + |
| 53 | + Ok(()) |
| 54 | +} |
| 55 | + |
| 56 | +/// Open a pseudoterminal device (see |
| 57 | +/// [posix_openpt(3)](http://man7.org/linux/man-pages/man3/posix_openpt.3.html)) |
| 58 | +/// |
| 59 | +/// `posix_openpt()` returns a file descriptor to an existing unused pseuterminal master device. |
| 60 | +/// |
| 61 | +/// # Examples |
| 62 | +/// |
| 63 | +/// A common use case with this function is to open both a master and slave PTY pair. This can be |
| 64 | +/// done as follows: |
| 65 | +/// |
| 66 | +/// ``` |
| 67 | +/// use std::path::Path; |
| 68 | +/// use nix::fcntl::{O_RDWR, open}; |
| 69 | +/// use nix::pty::*; |
| 70 | +/// use nix::sys::stat; |
| 71 | +/// |
| 72 | +/// # #[allow(dead_code)] |
| 73 | +/// # fn run() -> nix::Result<()> { |
| 74 | +/// // Open a new PTY master |
| 75 | +/// let master_fd = posix_openpt(O_RDWR)?; |
| 76 | +/// |
| 77 | +/// // Allow a slave to be generated for it |
| 78 | +/// grantpt(&master_fd)?; |
| 79 | +/// unlockpt(&master_fd)?; |
| 80 | +/// |
| 81 | +/// // Get the name of the slave |
| 82 | +/// let slave_name = ptsname(&master_fd)?; |
| 83 | +/// |
| 84 | +/// // Try to open the slave |
| 85 | +/// # #[allow(unused_variables)] |
| 86 | +/// let slave_fd = open(Path::new(&slave_name), O_RDWR, stat::Mode::empty())?; |
| 87 | +/// # Ok(()) |
| 88 | +/// # } |
| 89 | +/// ``` |
| 90 | +#[inline] |
| 91 | +pub fn posix_openpt(flags: fcntl::OFlag) -> Result<PtyMaster> { |
| 92 | + let fd = unsafe { |
| 93 | + libc::posix_openpt(flags.bits()) |
| 94 | + }; |
| 95 | + |
| 96 | + if fd < 0 { |
| 97 | + return Err(Error::last().into()); |
| 98 | + } |
| 99 | + |
| 100 | + Ok(PtyMaster(fd)) |
| 101 | +} |
| 102 | + |
| 103 | +/// Get the name of the slave pseudoterminal (see |
| 104 | +/// [ptsname(3)](http://man7.org/linux/man-pages/man3/ptsname.3.html)) |
| 105 | +/// |
| 106 | +/// `ptsname()` returns the name of the slave pseudoterminal device corresponding to the master |
| 107 | +/// referred to by `fd`. Note that this function is *not* threadsafe. For that see `ptsname_r()`. |
| 108 | +/// |
| 109 | +/// This value is useful for opening the slave pty once the master has already been opened with |
| 110 | +/// `posix_openpt()`. |
| 111 | +#[inline] |
| 112 | +pub fn ptsname(fd: &PtyMaster) -> Result<String> { |
| 113 | + let name_ptr = unsafe { libc::ptsname(fd.as_raw_fd()) }; |
| 114 | + if name_ptr.is_null() { |
| 115 | + return Err(Error::last().into()); |
| 116 | + } |
| 117 | + |
| 118 | + let name = unsafe { |
| 119 | + CStr::from_ptr(name_ptr) |
| 120 | + }; |
| 121 | + Ok(name.to_string_lossy().into_owned()) |
| 122 | +} |
| 123 | + |
| 124 | +/// Get the name of the slave pseudoterminal (see |
| 125 | +/// [ptsname(3)](http://man7.org/linux/man-pages/man3/ptsname.3.html)) |
| 126 | +/// |
| 127 | +/// `ptsname_r()` returns the name of the slave pseudoterminal device corresponding to the master |
| 128 | +/// referred to by `fd`. This is the threadsafe version of `ptsname()`, but it is not part of the |
| 129 | +/// POSIX standard and is instead a Linux-specific extension. |
| 130 | +/// |
| 131 | +/// This value is useful for opening the slave ptty once the master has already been opened with |
| 132 | +/// `posix_openpt()`. |
| 133 | +#[cfg(any(target_os = "android", target_os = "linux"))] |
| 134 | +#[inline] |
| 135 | +pub fn ptsname_r(fd: &PtyMaster) -> Result<String> { |
| 136 | + let mut name_buf = vec![0u8; 64]; |
| 137 | + let name_buf_ptr = name_buf.as_mut_ptr() as *mut libc::c_char; |
| 138 | + println!("{}/{}", name_buf.len(), name_buf.capacity()); |
| 139 | + if unsafe { libc::ptsname_r(fd.as_raw_fd(), name_buf_ptr, name_buf.capacity()) } != 0 { |
| 140 | + return Err(Error::last().into()); |
| 141 | + } |
| 142 | + println!("{}/{}", name_buf.len(), name_buf.capacity()); |
| 143 | + |
| 144 | + // Find the first null-character terminating this string. This is guaranteed to succeed if the |
| 145 | + // return value of `libc::ptsname_r` is 0. |
| 146 | + let null_index = name_buf.iter().position(|c| *c == b'\0').unwrap(); |
| 147 | + name_buf.truncate(null_index); |
| 148 | + |
| 149 | + let name = String::from_utf8(name_buf)?; |
| 150 | + Ok(name) |
| 151 | +} |
| 152 | + |
| 153 | +/// Unlock a pseudoterminal master/slave pseudoterminal pair (see |
| 154 | +/// [unlockpt(3)](http://man7.org/linux/man-pages/man3/unlockpt.3.html)) |
| 155 | +/// |
| 156 | +/// `unlockpt()` unlocks the slave pseudoterminal device corresponding to the master pseudoterminal |
| 157 | +/// referred to by `fd`. This must be called before trying to open the slave side of a |
| 158 | +/// pseuoterminal. |
| 159 | +#[inline] |
| 160 | +pub fn unlockpt(fd: &PtyMaster) -> Result<()> { |
| 161 | + if unsafe { libc::unlockpt(fd.as_raw_fd()) } < 0 { |
| 162 | + return Err(Error::last().into()); |
| 163 | + } |
| 164 | + |
| 165 | + Ok(()) |
| 166 | +} |
0 commit comments