alacritty/alacritty_terminal/src/tty/unix.rs

400 lines
11 KiB
Rust
Raw Normal View History

2020-05-05 22:50:23 +00:00
//! TTY related functionality.
2019-03-30 16:48:36 +00:00
use std::ffi::CStr;
use std::fs::File;
use std::io::{Error, ErrorKind, Result};
use std::mem::MaybeUninit;
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};
use std::os::unix::process::CommandExt;
2019-03-30 16:48:36 +00:00
use std::process::{Child, Command, Stdio};
use std::{env, ptr};
use libc::{self, c_int, winsize, TIOCSCTTY};
use log::error;
use mio::unix::EventedFd;
use nix::pty::openpty;
#[cfg(any(target_os = "linux", target_os = "macos"))]
use nix::sys::termios::{self, InputFlags, SetArg};
2021-11-15 00:56:27 +00:00
use signal_hook::consts as sigconsts;
use signal_hook_mio::v0_6::Signals;
use crate::config::PtyConfig;
use crate::event::{OnResize, WindowSize};
use crate::tty::{ChildEvent, EventedPty, EventedReadWrite};
macro_rules! die {
($($arg:tt)*) => {{
error!($($arg)*);
std::process::exit(1);
}}
}
2020-05-05 22:50:23 +00:00
/// Get raw fds for master/slave ends of a new PTY.
fn make_pty(size: winsize) -> Result<(RawFd, RawFd)> {
let mut window_size = size;
window_size.ws_xpixel = 0;
window_size.ws_ypixel = 0;
Add support for macOS Alacritty now runs on macOS using CoreText for font rendering. The font rendering subsystems were moved into a separate crate called `font`. The font crate provides a unified (albeit limited) API which wraps CoreText on macOS and FreeType/FontConfig on other platforms. The unified API differed slightly from what the original Rasterizer for freetype implemented, and it was updated accordingly. The cell separation properties (sep_x and sep_y) are now premultiplied into the cell width and height. They were previously passed through as uniforms to the shaders; removing them prevents a lot of redundant work. `libc` has some differences between Linux and macOS. `__errno_location` is not available on macOS, and the `errno` crate was brought in to provide a cross-platform API for dealing with errno. Differences in `openpty` were handled by implementing a macOS specific version. It would be worth investigating a way to unify the implementations at some point. A type mismatch with TIOCSCTTY was resolved with a cast. Differences in libc::passwd struct fields were resolved by using std::mem::uninitialized instead of zeroing the struct ourselves. This has the benefit of being much cleaner. The thread setup had to be changed to support both macOS and Linux. macOS requires that events from the window be handled on the main thread. Failure to do so will prevent the glutin window from even showing up! For this reason, the renderer and parser were moved to their own thread, and the input is received on the main thread. This is essentially reverse the setup prior to this commit. Renderer initialization (and thus font cache initialization) had to be moved to the rendering thread as well since there's no way to make_context(null) with glx on Linux. Trying to just call make_context a second time on the rendering thread had resulted in a panic!.
2016-06-10 03:39:40 +00:00
let ends = openpty(Some(&window_size), None)?;
Add support for macOS Alacritty now runs on macOS using CoreText for font rendering. The font rendering subsystems were moved into a separate crate called `font`. The font crate provides a unified (albeit limited) API which wraps CoreText on macOS and FreeType/FontConfig on other platforms. The unified API differed slightly from what the original Rasterizer for freetype implemented, and it was updated accordingly. The cell separation properties (sep_x and sep_y) are now premultiplied into the cell width and height. They were previously passed through as uniforms to the shaders; removing them prevents a lot of redundant work. `libc` has some differences between Linux and macOS. `__errno_location` is not available on macOS, and the `errno` crate was brought in to provide a cross-platform API for dealing with errno. Differences in `openpty` were handled by implementing a macOS specific version. It would be worth investigating a way to unify the implementations at some point. A type mismatch with TIOCSCTTY was resolved with a cast. Differences in libc::passwd struct fields were resolved by using std::mem::uninitialized instead of zeroing the struct ourselves. This has the benefit of being much cleaner. The thread setup had to be changed to support both macOS and Linux. macOS requires that events from the window be handled on the main thread. Failure to do so will prevent the glutin window from even showing up! For this reason, the renderer and parser were moved to their own thread, and the input is received on the main thread. This is essentially reverse the setup prior to this commit. Renderer initialization (and thus font cache initialization) had to be moved to the rendering thread as well since there's no way to make_context(null) with glx on Linux. Trying to just call make_context a second time on the rendering thread had resulted in a panic!.
2016-06-10 03:39:40 +00:00
Ok((ends.master, ends.slave))
Add support for macOS Alacritty now runs on macOS using CoreText for font rendering. The font rendering subsystems were moved into a separate crate called `font`. The font crate provides a unified (albeit limited) API which wraps CoreText on macOS and FreeType/FontConfig on other platforms. The unified API differed slightly from what the original Rasterizer for freetype implemented, and it was updated accordingly. The cell separation properties (sep_x and sep_y) are now premultiplied into the cell width and height. They were previously passed through as uniforms to the shaders; removing them prevents a lot of redundant work. `libc` has some differences between Linux and macOS. `__errno_location` is not available on macOS, and the `errno` crate was brought in to provide a cross-platform API for dealing with errno. Differences in `openpty` were handled by implementing a macOS specific version. It would be worth investigating a way to unify the implementations at some point. A type mismatch with TIOCSCTTY was resolved with a cast. Differences in libc::passwd struct fields were resolved by using std::mem::uninitialized instead of zeroing the struct ourselves. This has the benefit of being much cleaner. The thread setup had to be changed to support both macOS and Linux. macOS requires that events from the window be handled on the main thread. Failure to do so will prevent the glutin window from even showing up! For this reason, the renderer and parser were moved to their own thread, and the input is received on the main thread. This is essentially reverse the setup prior to this commit. Renderer initialization (and thus font cache initialization) had to be moved to the rendering thread as well since there's no way to make_context(null) with glx on Linux. Trying to just call make_context a second time on the rendering thread had resulted in a panic!.
2016-06-10 03:39:40 +00:00
}
2020-05-05 22:50:23 +00:00
/// Really only needed on BSD, but should be fine elsewhere.
fn set_controlling_terminal(fd: c_int) {
let res = unsafe {
// TIOSCTTY changes based on platform and the `ioctl` call is different
// based on architecture (32/64). So a generic cast is used to make sure
// there are no issues. To allow such a generic cast the clippy warning
// is disabled.
#[allow(clippy::cast_lossless)]
libc::ioctl(fd, TIOCSCTTY as _, 0)
};
if res < 0 {
die!("ioctl TIOCSCTTY failed: {}", Error::last_os_error());
}
}
#[derive(Debug)]
struct Passwd<'a> {
name: &'a str,
dir: &'a str,
shell: &'a str,
}
2020-05-05 22:50:23 +00:00
/// Return a Passwd struct with pointers into the provided buf.
///
/// # Unsafety
///
/// If `buf` is changed while `Passwd` is alive, bad thing will almost certainly happen.
fn get_pw_entry(buf: &mut [i8; 1024]) -> Result<Passwd<'_>> {
2020-05-05 22:50:23 +00:00
// Create zeroed passwd struct.
2019-09-10 16:08:01 +00:00
let mut entry: MaybeUninit<libc::passwd> = MaybeUninit::uninit();
let mut res: *mut libc::passwd = ptr::null_mut();
// Try and read the pw file.
let uid = unsafe { libc::getuid() };
let status = unsafe {
2019-09-10 16:08:01 +00:00
libc::getpwuid_r(uid, entry.as_mut_ptr(), buf.as_mut_ptr() as *mut _, buf.len(), &mut res)
};
2019-09-10 16:08:01 +00:00
let entry = unsafe { entry.assume_init() };
if status < 0 {
return Err(Error::new(ErrorKind::Other, "getpwuid_r failed"));
}
if res.is_null() {
return Err(Error::new(ErrorKind::Other, "pw not found"));
}
2020-05-05 22:50:23 +00:00
// Sanity check.
assert_eq!(entry.pw_uid, uid);
2020-05-05 22:50:23 +00:00
// Build a borrowed Passwd struct.
Ok(Passwd {
name: unsafe { CStr::from_ptr(entry.pw_name).to_str().unwrap() },
dir: unsafe { CStr::from_ptr(entry.pw_dir).to_str().unwrap() },
shell: unsafe { CStr::from_ptr(entry.pw_shell).to_str().unwrap() },
})
}
pub struct Pty {
child: Child,
file: File,
token: mio::Token,
signals: Signals,
signals_token: mio::Token,
}
impl Pty {
pub fn child(&self) -> &Child {
&self.child
}
pub fn file(&self) -> &File {
&self.file
}
}
/// Look for a shell in the `$SHELL` environment variable, then in `passwd`.
fn default_shell(pw: &Passwd<'_>) -> String {
env::var("SHELL").unwrap_or_else(|_| pw.shell.to_owned())
}
#[cfg(not(target_os = "macos"))]
fn default_shell_command(pw: &Passwd<'_>) -> Command {
Command::new(default_shell(pw))
}
#[cfg(target_os = "macos")]
fn default_shell_command(pw: &Passwd<'_>) -> Command {
let shell = default_shell(pw);
let shell_name = shell.rsplit('/').next().unwrap();
// On macOS, use the `login` command so the shell will appear as a tty session.
let mut login_command = Command::new("/usr/bin/login");
// Exec the shell with argv[0] prepended by '-' so it becomes a login shell.
// `login` normally does this itself, but `-l` disables this.
let exec = format!("exec -a -{} {}", shell_name, shell);
// -f: Bypasses authentication for the already-logged-in user.
// -l: Skips changing directory to $HOME and prepending '-' to argv[0].
// -p: Preserves the environment.
login_command.args(["-flp", pw.name, "/bin/sh", "-c", &exec]);
login_command
}
2020-05-05 22:50:23 +00:00
/// Create a new TTY and return a handle to interact with it.
pub fn new(config: &PtyConfig, window_size: WindowSize, window_id: u64) -> Result<Pty> {
let (master, slave) = make_pty(window_size.to_winsize())?;
#[cfg(any(target_os = "linux", target_os = "macos"))]
if let Ok(mut termios) = termios::tcgetattr(master) {
// Set character encoding to UTF-8.
termios.input_flags.set(InputFlags::IUTF8, true);
let _ = termios::tcsetattr(master, SetArg::TCSANOW, &termios);
}
let mut buf = [0; 1024];
let pw = get_pw_entry(&mut buf)?;
let mut builder = if let Some(shell) = config.shell.as_ref() {
let mut cmd = Command::new(shell.program());
cmd.args(shell.args());
cmd
} else {
default_shell_command(&pw)
};
2020-05-05 22:50:23 +00:00
// Setup child stdin/stdout/stderr as slave fd of PTY.
// Ownership of fd is transferred to the Stdio structs and will be closed by them at the end of
// this scope. (It is not an issue that the fd is closed three times since File::drop ignores
2020-05-05 22:50:23 +00:00
// error on libc::close.).
builder.stdin(unsafe { Stdio::from_raw_fd(slave) });
builder.stderr(unsafe { Stdio::from_raw_fd(slave) });
builder.stdout(unsafe { Stdio::from_raw_fd(slave) });
2020-05-05 22:50:23 +00:00
// Setup shell environment.
let window_id = window_id.to_string();
builder.env("ALACRITTY_WINDOW_ID", &window_id);
2017-01-23 08:00:52 +00:00
builder.env("LOGNAME", pw.name);
builder.env("USER", pw.name);
builder.env("HOME", pw.dir);
// Set Window ID for clients relying on X11 hacks.
builder.env("WINDOWID", window_id);
unsafe {
builder.pre_exec(move || {
2020-05-05 22:50:23 +00:00
// Create a new process group.
2017-01-23 08:00:52 +00:00
let err = libc::setsid();
if err == -1 {
return Err(Error::new(ErrorKind::Other, "Failed to set session id"));
}
set_controlling_terminal(slave);
2020-05-05 22:50:23 +00:00
// No longer need slave/master fds.
2017-01-23 08:00:52 +00:00
libc::close(slave);
libc::close(master);
2017-01-23 08:00:52 +00:00
libc::signal(libc::SIGCHLD, libc::SIG_DFL);
libc::signal(libc::SIGHUP, libc::SIG_DFL);
libc::signal(libc::SIGINT, libc::SIG_DFL);
libc::signal(libc::SIGQUIT, libc::SIG_DFL);
libc::signal(libc::SIGTERM, libc::SIG_DFL);
libc::signal(libc::SIGALRM, libc::SIG_DFL);
Ok(())
});
}
2017-01-23 08:00:52 +00:00
2020-05-05 22:50:23 +00:00
// Handle set working directory option.
if let Some(dir) = &config.working_directory {
builder.current_dir(dir);
}
2020-05-05 22:50:23 +00:00
// Prepare signal handling before spawning child.
let signals = Signals::new([sigconsts::SIGCHLD]).expect("error preparing signal handling");
2017-01-23 08:00:52 +00:00
match builder.spawn() {
Ok(child) => {
unsafe {
// Maybe this should be done outside of this function so nonblocking
// isn't forced upon consumers. Although maybe it should be?
set_nonblocking(master);
}
let mut pty = Pty {
child,
file: unsafe { File::from_raw_fd(master) },
token: mio::Token::from(0),
signals,
signals_token: mio::Token::from(0),
};
pty.on_resize(window_size);
Ok(pty)
2017-01-23 08:00:52 +00:00
},
Err(err) => Err(Error::new(
err.kind(),
format!(
"Failed to spawn command '{}': {}",
builder.get_program().to_string_lossy(),
err
),
)),
}
}
impl Drop for Pty {
fn drop(&mut self) {
// Make sure the PTY is terminated properly.
unsafe {
libc::kill(self.child.id() as i32, libc::SIGHUP);
}
let _ = self.child.wait();
}
}
impl EventedReadWrite for Pty {
type Reader = File;
type Writer = File;
#[inline]
fn register(
&mut self,
poll: &mio::Poll,
token: &mut dyn Iterator<Item = mio::Token>,
interest: mio::Ready,
poll_opts: mio::PollOpt,
) -> Result<()> {
self.token = token.next().unwrap();
poll.register(&EventedFd(&self.file.as_raw_fd()), self.token, interest, poll_opts)?;
self.signals_token = token.next().unwrap();
poll.register(
&self.signals,
self.signals_token,
mio::Ready::readable(),
2019-03-30 16:48:36 +00:00
mio::PollOpt::level(),
)
}
#[inline]
fn reregister(
&mut self,
poll: &mio::Poll,
interest: mio::Ready,
2019-03-30 16:48:36 +00:00
poll_opts: mio::PollOpt,
) -> Result<()> {
poll.reregister(&EventedFd(&self.file.as_raw_fd()), self.token, interest, poll_opts)?;
poll.reregister(
&self.signals,
self.signals_token,
mio::Ready::readable(),
2019-03-30 16:48:36 +00:00
mio::PollOpt::level(),
)
}
#[inline]
fn deregister(&mut self, poll: &mio::Poll) -> Result<()> {
poll.deregister(&EventedFd(&self.file.as_raw_fd()))?;
poll.deregister(&self.signals)
}
#[inline]
fn reader(&mut self) -> &mut File {
&mut self.file
}
#[inline]
fn read_token(&self) -> mio::Token {
self.token
}
#[inline]
fn writer(&mut self) -> &mut File {
&mut self.file
}
#[inline]
fn write_token(&self) -> mio::Token {
self.token
}
}
impl EventedPty for Pty {
#[inline]
fn next_child_event(&mut self) -> Option<ChildEvent> {
2019-03-30 16:48:36 +00:00
self.signals.pending().next().and_then(|signal| {
2021-11-15 00:56:27 +00:00
if signal != sigconsts::SIGCHLD {
2019-03-30 16:48:36 +00:00
return None;
}
match self.child.try_wait() {
Err(e) => {
error!("Error checking child process termination: {}", e);
None
},
Ok(None) => None,
Ok(_) => Some(ChildEvent::Exited),
}
})
}
#[inline]
fn child_event_token(&self) -> mio::Token {
self.signals_token
}
}
impl OnResize for Pty {
/// Resize the PTY.
///
/// Tells the kernel that the window size changed with the new pixel
/// dimensions and line/column counts.
fn on_resize(&mut self, window_size: WindowSize) {
let win = window_size.to_winsize();
let res = unsafe { libc::ioctl(self.file.as_raw_fd(), libc::TIOCSWINSZ, &win as *const _) };
if res < 0 {
die!("ioctl TIOCSWINSZ failed: {}", Error::last_os_error());
}
}
}
2020-05-05 22:50:23 +00:00
/// Types that can produce a `libc::winsize`.
pub trait ToWinsize {
2020-05-05 22:50:23 +00:00
/// Get a `libc::winsize`.
fn to_winsize(self) -> winsize;
}
impl ToWinsize for WindowSize {
fn to_winsize(self) -> winsize {
let ws_row = self.num_lines as libc::c_ushort;
let ws_col = self.num_cols as libc::c_ushort;
let ws_xpixel = ws_col * self.cell_width as libc::c_ushort;
let ws_ypixel = ws_row * self.cell_height as libc::c_ushort;
winsize { ws_row, ws_col, ws_xpixel, ws_ypixel }
}
}
unsafe fn set_nonblocking(fd: c_int) {
use libc::{fcntl, F_GETFL, F_SETFL, O_NONBLOCK};
let res = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
assert_eq!(res, 0);
}
#[test]
fn test_get_pw_entry() {
let mut buf: [i8; 1024] = [0; 1024];
let _pw = get_pw_entry(&mut buf).unwrap();
}