1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#![feature(process_session_leader)]
extern crate fd;
extern crate libc;
extern crate termios;
use fd::{Pipe, splice_loop};
use ffi::{get_winsize, openpty};
use std::fs::File;
use std::io;
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::thread;
use termios::{Termios, tcsetattr};
pub use fd::FileDesc;
pub mod ffi;
pub struct TtyServer {
master: File,
slave: Option<File>,
path: PathBuf,
}
pub struct TtyClient {
#[allow(dead_code)]
master: FileDesc,
peer: FileDesc,
termios_orig: Termios,
do_flush: Arc<AtomicBool>,
flush_event: Receiver<()>,
}
impl TtyServer {
pub fn new<T>(template: Option<&T>) -> io::Result<TtyServer> where T: AsRawFd {
let pty = match template {
Some(t) => try!(openpty(Some(&try!(Termios::from_fd(t.as_raw_fd()))), Some(&try!(get_winsize(t))))),
None => try!(openpty(None, None)),
};
Ok(TtyServer {
master: pty.master,
slave: Some(pty.slave),
path: pty.path,
})
}
pub fn new_client<T>(&self, peer: T) -> io::Result<TtyClient> where T: AsRawFd + IntoRawFd {
let master = FileDesc::new(self.master.as_raw_fd(), false);
TtyClient::new(master, peer)
}
pub fn get_master(&self) -> &File {
&self.master
}
pub fn take_slave(&mut self) -> Option<File> {
self.slave.take()
}
pub fn spawn(&mut self, mut cmd: Command) -> io::Result<Child> {
match self.slave.take() {
Some(slave) => {
cmd.stdin(unsafe { Stdio::from_raw_fd(slave.as_raw_fd()) }).
stdout(unsafe { Stdio::from_raw_fd(slave.as_raw_fd()) }).
stderr(unsafe { Stdio::from_raw_fd(slave.into_raw_fd()) }).
session_leader(true).
spawn()
},
None => Err(io::Error::new(io::ErrorKind::BrokenPipe, "No TTY slave")),
}
}
}
impl AsRef<Path> for TtyServer {
fn as_ref(&self) -> &Path {
self.path.as_ref()
}
}
impl TtyClient {
pub fn new<T, U>(master: T, peer: U) -> io::Result<TtyClient>
where T: AsRawFd + IntoRawFd, U: AsRawFd + IntoRawFd {
let termios_orig = try!(Termios::from_fd(peer.as_raw_fd()));
let mut termios_peer = try!(Termios::from_fd(peer.as_raw_fd()));
termios_peer.c_lflag &= !(termios::ECHO | termios::ICANON | termios::ISIG);
termios_peer.c_iflag &= !(termios::IGNBRK | termios::ICRNL);
termios_peer.c_iflag |= termios::BRKINT;
termios_peer.c_cc[termios::VMIN] = 1;
termios_peer.c_cc[termios::VTIME] = 0;
try!(tcsetattr(peer.as_raw_fd(), termios::TCSAFLUSH, &termios_peer));
let do_flush_main = Arc::new(AtomicBool::new(false));
let (event_tx, event_rx): (Sender<()>, Receiver<()>) = channel();
let (m2p_tx, m2p_rx) = match Pipe::new() {
Ok(p) => (p.writer, p.reader),
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
};
let do_flush = do_flush_main.clone();
let master_fd = master.as_raw_fd();
thread::spawn(move || splice_loop(do_flush, None, master_fd, m2p_tx.as_raw_fd()));
let do_flush = do_flush_main.clone();
let peer_fd = peer.as_raw_fd();
thread::spawn(move || splice_loop(do_flush, None, m2p_rx.as_raw_fd(), peer_fd));
let (p2m_tx, p2m_rx) = match Pipe::new() {
Ok(p) => (p.writer, p.reader),
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
};
let do_flush = do_flush_main.clone();
let peer_fd = peer.as_raw_fd();
thread::spawn(move || splice_loop(do_flush, None, peer_fd, p2m_tx.as_raw_fd()));
let do_flush = do_flush_main.clone();
let master_fd = master.as_raw_fd();
thread::spawn(move || splice_loop(do_flush, Some(event_tx), p2m_rx.as_raw_fd(), master_fd));
Ok(TtyClient {
master: FileDesc::new(master.into_raw_fd(), true),
peer: FileDesc::new(peer.into_raw_fd(), true),
termios_orig: termios_orig,
do_flush: do_flush_main,
flush_event: event_rx,
})
}
pub fn wait(&self) {
while !self.do_flush.load(Relaxed) {
let _ = self.flush_event.recv();
}
}
}
impl Drop for TtyClient {
fn drop(&mut self) {
self.do_flush.store(true, Relaxed);
let _ = tcsetattr(self.peer.as_raw_fd(), termios::TCSAFLUSH, &self.termios_orig);
}
}