1
0
Fork 0
mirror of https://gitlab.redox-os.org/CoffeeCode/redox-ssh.git synced 2025-12-28 17:02:19 +01:00

Create a read thread for the PTY

This commit is contained in:
Thomas Gatzweiler 2017-07-21 21:18:48 +02:00
parent 9789c53294
commit 29f16ed240
3 changed files with 90 additions and 53 deletions

View file

@ -1,10 +1,11 @@
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::{self, Read, Write}; use std::io::{self, Read, Write};
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd}; use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
use std::os::unix::process::CommandExt; use std::os::unix::process::CommandExt;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{self, Stdio}; use std::process::{self, Stdio};
use sys::{before_exec, getpty}; use std::thread::{self, JoinHandle};
use sys;
pub type ChannelId = u32; pub type ChannelId = u32;
@ -15,20 +16,20 @@ pub struct Channel {
process: Option<process::Child>, process: Option<process::Child>,
pty: Option<(RawFd, PathBuf)>, pty: Option<(RawFd, PathBuf)>,
master: Option<File>, master: Option<File>,
stdio: Option<(File, File, File)>,
window_size: u32, window_size: u32,
peer_window_size: u32, peer_window_size: u32,
max_packet_size: u32, max_packet_size: u32,
read_thread: Option<JoinHandle<()>>,
} }
#[derive(Debug)] #[derive(Debug)]
pub enum ChannelRequest { pub enum ChannelRequest {
Pty { Pty {
term: String, term: String,
char_width: u32, chars: u16,
row_height: u32, rows: u16,
pixel_width: u32, pixel_width: u16,
pixel_height: u32, pixel_height: u16,
modes: Vec<u8>, modes: Vec<u8>,
}, },
Shell, Shell,
@ -45,19 +46,21 @@ impl Channel {
process: None, process: None,
master: None, master: None,
pty: None, pty: None,
stdio: None,
window_size: peer_window_size, window_size: peer_window_size,
peer_window_size: peer_window_size, peer_window_size: peer_window_size,
max_packet_size: max_packet_size, max_packet_size: max_packet_size,
read_thread: None,
} }
} }
pub fn id(&self) -> ChannelId { pub fn id(&self) -> ChannelId {
self.id self.id
} }
pub fn window_size(&self) -> u32 { pub fn window_size(&self) -> u32 {
self.window_size self.window_size
} }
pub fn max_packet_size(&self) -> u32 { pub fn max_packet_size(&self) -> u32 {
self.max_packet_size self.max_packet_size
} }
@ -65,41 +68,75 @@ impl Channel {
pub fn request(&mut self, request: ChannelRequest) { pub fn request(&mut self, request: ChannelRequest) {
match request match request
{ {
ChannelRequest::Pty { .. } => { ChannelRequest::Pty {
let (master_fd, tty_path) = getpty(); chars,
rows,
pixel_width,
pixel_height,
..
} => {
let (master_fd, tty_path) = sys::getpty();
sys::set_winsize(
master_fd,
chars,
rows,
pixel_width,
pixel_height,
);
self.read_thread = Some(thread::spawn(move || {
use libc::dup;
let master2 = unsafe { dup(master_fd) };
println!("dup result: {}", dup as u32);
let mut master = unsafe { File::from_raw_fd(master2) };
loop {
use std::str::from_utf8_unchecked;
let mut buf = [0; 4096];
let count = master.read(&mut buf).unwrap();
if count == 0 {
break;
}
println!("Read: {}", unsafe {
from_utf8_unchecked(&buf[0..count])
});
}
println!("Quitting read thread.");
}));
self.pty = Some((master_fd, tty_path));
self.master = Some(unsafe { File::from_raw_fd(master_fd) });
}
ChannelRequest::Shell => {
if let Some(&(_, ref tty_path)) = self.pty.as_ref() {
let stdin = OpenOptions::new() let stdin = OpenOptions::new()
.read(true) .read(true)
.write(true) .write(true)
.open(&tty_path) .open(&tty_path)
.unwrap(); .unwrap()
.into_raw_fd();
let stdout = OpenOptions::new() let stdout = OpenOptions::new()
.read(true) .read(true)
.write(true) .write(true)
.open(&tty_path) .open(&tty_path)
.unwrap(); .unwrap()
.into_raw_fd();
let stderr = OpenOptions::new() let stderr = OpenOptions::new()
.read(true) .read(true)
.write(true) .write(true)
.open(&tty_path) .open(&tty_path)
.unwrap(); .unwrap()
.into_raw_fd();
self.stdio = Some((stdin, stdout, stderr));
self.master = Some(unsafe { File::from_raw_fd(master_fd) });
}
ChannelRequest::Shell => {
if let Some((ref stdin, ref stdout, ref stderr)) = self.stdio {
process::Command::new("login") process::Command::new("login")
.stdin(unsafe { Stdio::from_raw_fd(stdin.as_raw_fd()) }) .stdin(unsafe { Stdio::from_raw_fd(stdin) })
.stdout( .stdout(unsafe { Stdio::from_raw_fd(stdout) })
unsafe { Stdio::from_raw_fd(stdout.as_raw_fd()) }, .stderr(unsafe { Stdio::from_raw_fd(stderr) })
) .before_exec(|| sys::before_exec())
.stderr(
unsafe { Stdio::from_raw_fd(stderr.as_raw_fd()) },
)
.before_exec(|| before_exec())
.spawn() .spawn()
.unwrap(); .unwrap();
} }
@ -117,15 +154,4 @@ impl Channel {
Ok(()) Ok(())
} }
} }
pub fn read(&mut self) -> io::Result<Vec<u8>> {
if let Some(ref mut master) = self.master {
let mut buf = [0; 4096];
let count = master.read(&mut buf)?;
Ok(buf[0..count].to_vec())
}
else {
Ok(b"".to_vec())
}
}
} }

View file

@ -11,13 +11,13 @@ pub fn fork() -> usize {
unsafe { syscall::clone(0).unwrap() } unsafe { syscall::clone(0).unwrap() }
} }
pub fn set_winsize(fd: RawFd, row: u16, col: u16, xpixel: u16, ypixel: u16) {}
pub fn getpty() -> (RawFd, PathBuf) { pub fn getpty() -> (RawFd, PathBuf) {
use syscall; use syscall;
let master = syscall::open( let master = syscall::open("pty:", syscall::O_RDWR | syscall::O_CREAT)
"pty:", .unwrap();
syscall::O_RDWR | syscall::O_CREAT | syscall::O_NONBLOCK,
).unwrap();
let mut buf: [u8; 4096] = [0; 4096]; let mut buf: [u8; 4096] = [0; 4096];

View file

@ -12,16 +12,28 @@ pub fn before_exec() -> Result<()> {
} }
pub fn fork() -> usize { pub fn fork() -> usize {
extern crate libc; use libc;
unsafe { libc::fork() as usize } unsafe { libc::fork() as usize }
} }
pub fn set_winsize(fd: RawFd, row: u16, col: u16, xpixel: u16, ypixel: u16) {
use libc;
unsafe {
let size = libc::winsize {
ws_row: row,
ws_col: col,
ws_xpixel: xpixel,
ws_ypixel: ypixel,
};
libc::ioctl(fd, libc::TIOCSWINSZ, &size as *const libc::winsize);
}
}
pub fn getpty() -> (RawFd, PathBuf) { pub fn getpty() -> (RawFd, PathBuf) {
use libc; use libc;
use std::ffi::CStr; use std::ffi::CStr;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Error; use std::io::Error;
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::io::IntoRawFd; use std::os::unix::io::IntoRawFd;
const TIOCPKT: libc::c_ulong = 0x5420; const TIOCPKT: libc::c_ulong = 0x5420;
@ -35,7 +47,6 @@ pub fn getpty() -> (RawFd, PathBuf) {
let master_fd = OpenOptions::new() let master_fd = OpenOptions::new()
.read(true) .read(true)
.write(true) .write(true)
.custom_flags(libc::O_NONBLOCK)
.open("/dev/ptmx") .open("/dev/ptmx")
.unwrap() .unwrap()
.into_raw_fd(); .into_raw_fd();