|
| 1 | +//! Create master and slave virtual pseudo-terminals (PTTYs) |
| 2 | +
|
| 3 | +use std::ffi::CStr; |
| 4 | +use std::os::unix::io::RawFd; |
| 5 | + |
| 6 | +use libc; |
| 7 | + |
| 8 | +use {Error, fcntl, Result}; |
| 9 | + |
| 10 | +/// Grant access to a slave pseudoterminal (see |
| 11 | +/// [grantpt(3)](http://man7.org/linux/man-pages/man3/grantpt.3.html)) |
| 12 | +/// |
| 13 | +/// `grantpt()` changezs the mode and owner of the slave pseudoterminal device corresponding to the |
| 14 | +/// master pseudoterminal referred to by `fd`. This is a necessary step towards opening the slave. |
| 15 | +#[inline] |
| 16 | +pub fn grantpt(fd: RawFd) -> Result<()> { |
| 17 | + if unsafe { libc::grantpt(fd) } < 0 { |
| 18 | + return Err(Error::last().into()); |
| 19 | + } |
| 20 | + |
| 21 | + Ok(()) |
| 22 | +} |
| 23 | + |
| 24 | +/// Open a pseudoterminal device (see |
| 25 | +/// [posix_openpt(3)](http://man7.org/linux/man-pages/man3/posix_openpt.3.html)) |
| 26 | +/// |
| 27 | +/// `posix_openpt()` returns a file descriptor to an existing unused pseuterminal master device. |
| 28 | +/// |
| 29 | +/// # Examples |
| 30 | +/// |
| 31 | +/// A common use case with this function is to open both a master and slave PTTY pair. This can be |
| 32 | +/// done as follows: |
| 33 | +/// |
| 34 | +/// ``` |
| 35 | +/// use std::path::Path; |
| 36 | +/// use nix::fcntl::{O_RDWR, open}; |
| 37 | +/// use nix::pty::*; |
| 38 | +/// use nix::sys::stat; |
| 39 | +/// |
| 40 | +/// # #[allow(dead_code)] |
| 41 | +/// # fn run() -> nix::Result<()> { |
| 42 | +/// // Open a new PTTY master |
| 43 | +/// let master_fd = posix_openpt(O_RDWR)?; |
| 44 | +/// |
| 45 | +/// // Allow a slave to be generated for it |
| 46 | +/// grantpt(master_fd)?; |
| 47 | +/// unlockpt(master_fd)?; |
| 48 | +/// |
| 49 | +/// // Get the name of the slave |
| 50 | +/// let slave_name = ptsname(master_fd)?; |
| 51 | +/// |
| 52 | +/// // Try to open the slave |
| 53 | +/// # #[allow(unused_variables)] |
| 54 | +/// let slave_fd = open(Path::new(&slave_name), O_RDWR, stat::Mode::empty())?; |
| 55 | +/// # Ok(()) |
| 56 | +/// # } |
| 57 | +/// ``` |
| 58 | +#[inline] |
| 59 | +pub fn posix_openpt(flags: fcntl::OFlag) -> Result<RawFd> { |
| 60 | + let fd = unsafe { |
| 61 | + libc::posix_openpt(flags.bits()) |
| 62 | + }; |
| 63 | + |
| 64 | + if fd < 0 { |
| 65 | + return Err(Error::last().into()); |
| 66 | + } |
| 67 | + |
| 68 | + Ok(fd) |
| 69 | +} |
| 70 | + |
| 71 | +/// Get the name of the slave pseudoterminal (see |
| 72 | +/// [ptsname(3)](http://man7.org/linux/man-pages/man3/ptsname.3.html)) |
| 73 | +/// |
| 74 | +/// `ptsname()` returns the name of the slave pseudoterminal device corresponding to the master |
| 75 | +/// referred to by `fd`. Note that this function is *not* threadsafe. For that see `ptsname_r()`. |
| 76 | +/// |
| 77 | +/// This value is useful for opening the slave ptty once the master has already been opened with |
| 78 | +/// `posix_openpt()`. |
| 79 | +#[inline] |
| 80 | +pub fn ptsname(fd: RawFd) -> Result<String> { |
| 81 | + let name_ptr = unsafe { libc::ptsname(fd) }; |
| 82 | + if name_ptr.is_null() { |
| 83 | + return Err(Error::last().into()); |
| 84 | + } |
| 85 | + |
| 86 | + let name = unsafe { |
| 87 | + CStr::from_ptr(name_ptr) |
| 88 | + }; |
| 89 | + Ok(name.to_string_lossy().into_owned()) |
| 90 | +} |
| 91 | + |
| 92 | +/// Get the name of the slave pseudoterminal (see |
| 93 | +/// [ptsname(3)](http://man7.org/linux/man-pages/man3/ptsname.3.html)) |
| 94 | +/// |
| 95 | +/// `ptsname_r()` returns the name of the slave pseudoterminal device corresponding to the master |
| 96 | +/// referred to by `fd`. This is the threadsafe version of `ptsname()`, but it is not part of the |
| 97 | +/// POSIX standard and is instead a Linux-specific extension. |
| 98 | +/// |
| 99 | +/// This value is useful for opening the slave ptty once the master has already been opened with |
| 100 | +/// `posix_openpt()`. |
| 101 | +#[cfg(any(target_os = "android", target_os = "linux"))] |
| 102 | +#[inline] |
| 103 | +pub fn ptsname_r(fd: RawFd) -> Result<String> { |
| 104 | + let mut name_buf = [0 as libc::c_char; 64]; |
| 105 | + if unsafe { libc::ptsname_r(fd, name_buf.as_mut_ptr(), name_buf.len()) } != 0 { |
| 106 | + return Err(Error::last().into()); |
| 107 | + } |
| 108 | + |
| 109 | + let name = unsafe { |
| 110 | + CStr::from_ptr(name_buf.as_ptr()) |
| 111 | + }; |
| 112 | + Ok(name.to_string_lossy().into_owned()) |
| 113 | +} |
| 114 | + |
| 115 | +/// Unlock a pseudoterminal master/slave pseudoterminal pair (see |
| 116 | +/// [unlockpt(3)](http://man7.org/linux/man-pages/man3/unlockpt.3.html)) |
| 117 | +/// |
| 118 | +/// `unlockpt()` unlocks the slave pseudoterminal device corresponding to the master pseudoterminal |
| 119 | +/// referred to by `fd`. This must be called before trying to open the slave side of a |
| 120 | +/// pseuoterminal. |
| 121 | +#[inline] |
| 122 | +pub fn unlockpt(fd: RawFd) -> Result<()> { |
| 123 | + if unsafe { libc::unlockpt(fd) } < 0 { |
| 124 | + return Err(Error::last().into()); |
| 125 | + } |
| 126 | + |
| 127 | + Ok(()) |
| 128 | +} |
0 commit comments