2016-06-29 23:56:12 -04:00
|
|
|
// Copyright 2016 Joe Wilm, The Alacritty Project Contributors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
2016-05-24 23:55:51 -04:00
|
|
|
//! tty related functionality
|
|
|
|
//!
|
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::os::unix::io::FromRawFd;
|
2017-01-23 03:00:52 -05:00
|
|
|
use std::os::unix::process::CommandExt;
|
2016-05-24 23:55:51 -04:00
|
|
|
use std::ptr;
|
2017-01-23 14:10:26 -05:00
|
|
|
use std::process::{Command, Stdio};
|
2016-05-24 23:55:51 -04:00
|
|
|
|
2017-03-14 03:39:20 -04:00
|
|
|
use libc::{self, winsize, c_int, pid_t, WNOHANG, SIGCHLD, TIOCSCTTY};
|
2016-06-06 17:57:48 -04:00
|
|
|
|
2016-12-12 12:31:48 -05:00
|
|
|
use term::SizeInfo;
|
|
|
|
use display::OnResize;
|
2017-01-23 14:10:26 -05:00
|
|
|
use config::{Config, Shell};
|
2017-01-24 18:19:45 -05:00
|
|
|
use cli::Options;
|
2016-12-12 12:31:48 -05:00
|
|
|
|
2016-06-06 17:57:48 -04:00
|
|
|
/// Process ID of child process
|
|
|
|
///
|
|
|
|
/// Necessary to put this in static storage for `sigchld` to have access
|
|
|
|
static mut PID: pid_t = 0;
|
|
|
|
|
2016-06-09 00:24:31 -04:00
|
|
|
/// Exit flag
|
|
|
|
///
|
|
|
|
/// Calling exit() in the SIGCHLD handler sometimes causes opengl to deadlock,
|
|
|
|
/// and the process hangs. Instead, this flag is set, and its status can be
|
2017-10-30 11:03:58 -04:00
|
|
|
/// checked via `process_should_exit`.
|
2016-06-09 00:24:31 -04:00
|
|
|
static mut SHOULD_EXIT: bool = false;
|
|
|
|
|
2016-06-09 11:30:55 -04:00
|
|
|
extern "C" fn sigchld(_a: c_int) {
|
2016-06-06 17:57:48 -04:00
|
|
|
let mut status: c_int = 0;
|
|
|
|
unsafe {
|
|
|
|
let p = libc::waitpid(PID, &mut status, WNOHANG);
|
|
|
|
if p < 0 {
|
|
|
|
die!("Waiting for pid {} failed: {}\n", PID, errno());
|
|
|
|
}
|
|
|
|
|
2017-03-14 03:39:20 -04:00
|
|
|
if PID == p {
|
|
|
|
SHOULD_EXIT = true;
|
2016-06-06 17:57:48 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-09 00:24:31 -04:00
|
|
|
}
|
2016-06-06 17:57:48 -04:00
|
|
|
|
2016-06-09 00:24:31 -04:00
|
|
|
pub fn process_should_exit() -> bool {
|
|
|
|
unsafe { SHOULD_EXIT }
|
2016-06-06 17:57:48 -04:00
|
|
|
}
|
2016-05-24 23:55:51 -04:00
|
|
|
|
|
|
|
/// Get the current value of errno
|
|
|
|
fn errno() -> c_int {
|
2016-06-09 23:39:40 -04:00
|
|
|
::errno::errno().0
|
2016-05-24 23:55:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get raw fds for master/slave ends of a new pty
|
2016-06-09 23:39:40 -04:00
|
|
|
#[cfg(target_os = "linux")]
|
2016-05-24 23:55:51 -04:00
|
|
|
fn openpty(rows: u8, cols: u8) -> (c_int, c_int) {
|
|
|
|
let mut master: c_int = 0;
|
|
|
|
let mut slave: c_int = 0;
|
|
|
|
|
|
|
|
let win = winsize {
|
2018-01-05 20:42:55 -05:00
|
|
|
ws_row: libc::c_ushort::from(rows),
|
|
|
|
ws_col: libc::c_ushort::from(cols),
|
2016-05-24 23:55:51 -04:00
|
|
|
ws_xpixel: 0,
|
|
|
|
ws_ypixel: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = unsafe {
|
|
|
|
libc::openpty(&mut master, &mut slave, ptr::null_mut(), ptr::null(), &win)
|
|
|
|
};
|
|
|
|
|
|
|
|
if res < 0 {
|
|
|
|
die!("openpty failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
(master, slave)
|
|
|
|
}
|
|
|
|
|
2017-01-12 16:16:09 -05:00
|
|
|
#[cfg(any(target_os = "macos",target_os = "freebsd"))]
|
2016-06-09 23:39:40 -04:00
|
|
|
fn openpty(rows: u8, cols: u8) -> (c_int, c_int) {
|
|
|
|
let mut master: c_int = 0;
|
|
|
|
let mut slave: c_int = 0;
|
|
|
|
|
|
|
|
let mut win = winsize {
|
2018-01-05 20:42:55 -05:00
|
|
|
ws_row: libc::c_ushort::from(rows),
|
|
|
|
ws_col: libc::c_ushort::from(cols),
|
2016-06-09 23:39:40 -04:00
|
|
|
ws_xpixel: 0,
|
|
|
|
ws_ypixel: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = unsafe {
|
|
|
|
libc::openpty(&mut master, &mut slave, ptr::null_mut(), ptr::null_mut(), &mut win)
|
|
|
|
};
|
|
|
|
|
|
|
|
if res < 0 {
|
|
|
|
die!("openpty failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
(master, slave)
|
|
|
|
}
|
|
|
|
|
2016-05-24 23:55:51 -04:00
|
|
|
/// Really only needed on BSD, but should be fine elsewhere
|
|
|
|
fn set_controlling_terminal(fd: c_int) {
|
|
|
|
let res = unsafe {
|
2018-01-08 17:49:01 -05:00
|
|
|
// 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.
|
2018-01-05 20:42:55 -05:00
|
|
|
#[cfg_attr(feature = "clippy", allow(cast_lossless))]
|
2018-01-08 17:49:01 -05:00
|
|
|
libc::ioctl(fd, TIOCSCTTY as _, 0)
|
2016-05-24 23:55:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if res < 0 {
|
|
|
|
die!("ioctl TIOCSCTTY failed: {}", errno());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Passwd<'a> {
|
|
|
|
name: &'a str,
|
|
|
|
passwd: &'a str,
|
|
|
|
uid: libc::uid_t,
|
|
|
|
gid: libc::gid_t,
|
|
|
|
gecos: &'a str,
|
|
|
|
dir: &'a str,
|
|
|
|
shell: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2016-12-17 01:13:51 -05:00
|
|
|
fn get_pw_entry(buf: &mut [i8; 1024]) -> Passwd {
|
2016-05-24 23:55:51 -04:00
|
|
|
// Create zeroed passwd struct
|
2016-06-09 23:39:40 -04:00
|
|
|
let mut entry: libc::passwd = unsafe { ::std::mem::uninitialized() };
|
2016-05-24 23:55:51 -04:00
|
|
|
|
|
|
|
let mut res: *mut libc::passwd = ptr::null_mut();
|
|
|
|
|
|
|
|
// Try and read the pw file.
|
|
|
|
let uid = unsafe { libc::getuid() };
|
|
|
|
let status = unsafe {
|
2017-01-05 09:21:24 -05:00
|
|
|
libc::getpwuid_r(uid, &mut entry, buf.as_mut_ptr() as *mut _, buf.len(), &mut res)
|
2016-05-24 23:55:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if status < 0 {
|
|
|
|
die!("getpwuid_r failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.is_null() {
|
|
|
|
die!("pw not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// sanity check
|
|
|
|
assert_eq!(entry.pw_uid, uid);
|
|
|
|
|
|
|
|
// Build a borrowed Passwd struct
|
|
|
|
Passwd {
|
2017-01-15 19:39:22 -05:00
|
|
|
name: unsafe { CStr::from_ptr(entry.pw_name).to_str().unwrap() },
|
|
|
|
passwd: unsafe { CStr::from_ptr(entry.pw_passwd).to_str().unwrap() },
|
2016-05-24 23:55:51 -04:00
|
|
|
uid: entry.pw_uid,
|
|
|
|
gid: entry.pw_gid,
|
2017-01-15 19:39:22 -05:00
|
|
|
gecos: unsafe { CStr::from_ptr(entry.pw_gecos).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() },
|
2016-05-24 23:55:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-23 03:00:52 -05:00
|
|
|
/// Create a new tty and return a handle to interact with it.
|
2018-01-05 20:42:55 -05:00
|
|
|
pub fn new<T: ToWinsize>(config: &Config, options: &Options, size: &T, window_id: Option<usize>) -> Pty {
|
2017-01-23 03:00:52 -05:00
|
|
|
let win = size.to_winsize();
|
2016-05-24 23:55:51 -04:00
|
|
|
let mut buf = [0; 1024];
|
|
|
|
let pw = get_pw_entry(&mut buf);
|
|
|
|
|
2017-01-23 03:00:52 -05:00
|
|
|
let (master, slave) = openpty(win.ws_row as _, win.ws_col as _);
|
2016-05-24 23:55:51 -04:00
|
|
|
|
2017-01-24 18:19:45 -05:00
|
|
|
let default_shell = &Shell::new(pw.shell);
|
2017-04-01 19:30:29 -04:00
|
|
|
let shell = config.shell()
|
2017-09-27 20:29:44 -04:00
|
|
|
.unwrap_or(default_shell);
|
2016-05-24 23:55:51 -04:00
|
|
|
|
2017-09-27 20:29:44 -04:00
|
|
|
let initial_command = options.command().unwrap_or(shell);
|
2017-04-01 19:30:29 -04:00
|
|
|
|
|
|
|
let mut builder = Command::new(initial_command.program());
|
|
|
|
for arg in initial_command.args() {
|
2017-01-23 14:10:26 -05:00
|
|
|
builder.arg(arg);
|
|
|
|
}
|
2016-12-11 01:44:13 -05:00
|
|
|
|
2017-01-23 03:00:52 -05:00
|
|
|
// Setup child stdin/stdout/stderr as slave fd of pty
|
2017-05-27 22:52:58 -04:00
|
|
|
// 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
|
|
|
|
// error on libc::close.)
|
2017-01-23 14:10:26 -05:00
|
|
|
builder.stdin(unsafe { Stdio::from_raw_fd(slave) });
|
|
|
|
builder.stderr(unsafe { Stdio::from_raw_fd(slave) });
|
|
|
|
builder.stdout(unsafe { Stdio::from_raw_fd(slave) });
|
2016-05-24 23:55:51 -04:00
|
|
|
|
2017-01-23 03:00:52 -05:00
|
|
|
// Setup environment
|
|
|
|
builder.env("LOGNAME", pw.name);
|
|
|
|
builder.env("USER", pw.name);
|
2017-01-23 14:10:26 -05:00
|
|
|
builder.env("SHELL", shell.program());
|
2017-01-23 03:00:52 -05:00
|
|
|
builder.env("HOME", pw.dir);
|
2017-02-13 21:22:59 -05:00
|
|
|
builder.env("TERM", "xterm-256color"); // default term until we can supply our own
|
2017-05-27 23:08:28 -04:00
|
|
|
if let Some(window_id) = window_id {
|
|
|
|
builder.env("WINDOWID", format!("{}", window_id));
|
|
|
|
}
|
2017-02-13 21:22:59 -05:00
|
|
|
for (key, value) in config.env().iter() {
|
|
|
|
builder.env(key, value);
|
|
|
|
}
|
2016-05-24 23:55:51 -04:00
|
|
|
|
2017-01-23 03:00:52 -05:00
|
|
|
builder.before_exec(move || {
|
|
|
|
// Create a new process group
|
|
|
|
unsafe {
|
|
|
|
let err = libc::setsid();
|
|
|
|
if err == -1 {
|
|
|
|
die!("Failed to set session id: {}", errno());
|
2016-05-24 23:55:51 -04:00
|
|
|
}
|
2017-01-23 03:00:52 -05:00
|
|
|
}
|
2016-05-24 23:55:51 -04:00
|
|
|
|
2017-01-23 03:00:52 -05:00
|
|
|
set_controlling_terminal(slave);
|
2016-05-24 23:55:51 -04:00
|
|
|
|
2017-01-23 03:00:52 -05:00
|
|
|
// No longer need slave/master fds
|
|
|
|
unsafe {
|
|
|
|
libc::close(slave);
|
|
|
|
libc::close(master);
|
|
|
|
}
|
2016-05-24 23:55:51 -04:00
|
|
|
|
2017-01-23 03:00:52 -05:00
|
|
|
unsafe {
|
|
|
|
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-03-14 03:39:26 -04:00
|
|
|
// Handle set working directory option
|
|
|
|
if let Some(ref dir) = options.working_dir {
|
|
|
|
builder.current_dir(dir.as_path());
|
|
|
|
}
|
|
|
|
|
2017-01-23 03:00:52 -05:00
|
|
|
match builder.spawn() {
|
|
|
|
Ok(child) => {
|
2016-05-24 23:55:51 -04:00
|
|
|
unsafe {
|
2016-06-06 17:57:48 -04:00
|
|
|
// Set PID for SIGCHLD handler
|
2017-01-23 03:00:52 -05:00
|
|
|
PID = child.id() as _;
|
2016-06-06 17:57:48 -04:00
|
|
|
|
|
|
|
// Handle SIGCHLD
|
|
|
|
libc::signal(SIGCHLD, sigchld as _);
|
2016-05-24 23:55:51 -04:00
|
|
|
}
|
2016-09-24 19:11:50 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-12-11 01:44:13 -05:00
|
|
|
let pty = Pty { fd: master };
|
2018-01-05 20:42:55 -05:00
|
|
|
pty.resize(size);
|
2016-12-11 01:44:13 -05:00
|
|
|
pty
|
2017-01-23 03:00:52 -05:00
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
die!("Command::spawn() failed: {}", err);
|
2016-05-30 23:44:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 19:16:20 -05:00
|
|
|
pub struct Pty {
|
2016-05-30 23:44:37 -04:00
|
|
|
fd: c_int,
|
|
|
|
}
|
|
|
|
|
2016-11-19 19:16:20 -05:00
|
|
|
impl Pty {
|
2016-05-30 23:44:37 -04:00
|
|
|
/// Get reader for the TTY
|
|
|
|
///
|
|
|
|
/// XXX File is a bad abstraction here; it closes the fd on drop
|
|
|
|
pub fn reader(&self) -> File {
|
|
|
|
unsafe {
|
|
|
|
File::from_raw_fd(self.fd)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 01:44:13 -05:00
|
|
|
/// Resize the pty
|
|
|
|
///
|
|
|
|
/// Tells the kernel that the window size changed with the new pixel
|
|
|
|
/// dimensions and line/column counts.
|
2017-11-29 18:38:29 -05:00
|
|
|
pub fn resize<T: ToWinsize>(&self, size: &T) {
|
2016-12-11 01:44:13 -05:00
|
|
|
let win = size.to_winsize();
|
2016-05-30 23:44:37 -04:00
|
|
|
|
|
|
|
let res = unsafe {
|
|
|
|
libc::ioctl(self.fd, libc::TIOCSWINSZ, &win as *const _)
|
|
|
|
};
|
|
|
|
|
|
|
|
if res < 0 {
|
|
|
|
die!("ioctl TIOCSWINSZ failed: {}", errno());
|
2016-05-24 23:55:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 01:44:13 -05:00
|
|
|
/// Types that can produce a `libc::winsize`
|
|
|
|
pub trait ToWinsize {
|
|
|
|
/// Get a `libc::winsize`
|
|
|
|
fn to_winsize(&self) -> winsize;
|
|
|
|
}
|
|
|
|
|
2016-12-12 12:31:48 -05:00
|
|
|
impl<'a> ToWinsize for &'a SizeInfo {
|
|
|
|
fn to_winsize(&self) -> winsize {
|
|
|
|
winsize {
|
|
|
|
ws_row: self.lines().0 as libc::c_ushort,
|
|
|
|
ws_col: self.cols().0 as libc::c_ushort,
|
|
|
|
ws_xpixel: self.width as libc::c_ushort,
|
|
|
|
ws_ypixel: self.height as libc::c_ushort,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OnResize for Pty {
|
|
|
|
fn on_resize(&mut self, size: &SizeInfo) {
|
2017-11-29 18:38:29 -05:00
|
|
|
self.resize(&size);
|
2016-12-12 12:31:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-24 19:11:50 -04:00
|
|
|
unsafe fn set_nonblocking(fd: c_int) {
|
|
|
|
use libc::{fcntl, F_SETFL, F_GETFL, O_NONBLOCK};
|
|
|
|
|
|
|
|
let res = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
assert_eq!(res, 0);
|
|
|
|
}
|
|
|
|
|
2016-05-24 23:55:51 -04:00
|
|
|
#[test]
|
|
|
|
fn test_get_pw_entry() {
|
|
|
|
let mut buf: [i8; 1024] = [0; 1024];
|
2017-02-02 23:50:48 -05:00
|
|
|
let _pw = get_pw_entry(&mut buf);
|
2016-05-24 23:55:51 -04:00
|
|
|
}
|