Skip to content

Commit

Permalink
Merge pull request #43 from OSInside/sci_terminal_settings
Browse files Browse the repository at this point in the history
Add terminal settings for pty stdout in sci
  • Loading branch information
schaefi authored Apr 10, 2024
2 parents bc4ff29 + 6b279f7 commit 58d0b6c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions firecracker-pilot/guestvm-tools/sci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ shell-words = { version = "1.1" }
vsock = { version = "0.3" }
libc = { version = "0.2" }
pty = { version = "0.2" }
termios = { version = "0.3" }
16 changes: 15 additions & 1 deletion firecracker-pilot/guestvm-tools/sci/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use std::net::Shutdown;
use std::os::fd::AsRawFd;
use std::io::Write;
use pty::prelude::Fork;
use termios::*;

use crate::defaults::debug;

Expand All @@ -65,6 +66,8 @@ fn main() {
let mut do_exec = false;
let mut ok = true;

env::set_var("PS1", "\\[\\]\\u@\\h: >\n");

// print user space env
for (key, value) in env::vars() {
debug(&format!("{}: {}", key, value));
Expand Down Expand Up @@ -455,6 +458,12 @@ fn redirect_command_to_raw_channels(
let stream_fd = stream.as_raw_fd();
let stdout_fd = stdout.as_raw_fd();
let stderr_fd = stderr.as_raw_fd();

// Disable echo and canonical mode on stdout
let mut termios = Termios::from_fd(stdout_fd).unwrap();
termios.c_lflag &= !(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tcsetattr(stdout_fd, TCSANOW, &termios).unwrap();

// main send/recv loop
let mut buffer = [0_u8; 100];
loop {
Expand Down Expand Up @@ -552,6 +561,11 @@ fn redirect_command_to_pty(
let stdout_fd = master.as_raw_fd();
let stream_fd = stream.as_raw_fd();

// Disable echo and canonical mode on stdout
let mut termios = Termios::from_fd(stdout_fd).unwrap();
termios.c_lflag &= !(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tcsetattr(stdout_fd, TCSANOW, &termios).unwrap();

// main send/recv loop
let mut buffer = [0_u8; 100];
loop {
Expand Down Expand Up @@ -597,7 +611,7 @@ fn redirect_command_to_pty(
}
if unsafe { libc::FD_ISSET(stream_fd, &fdset) } {
// something new happened on the stream
// try to receive some bytes an send them on stdin
// try to receive some bytes and send them to stdout
if let Ok(sz_r) = stream.read(&mut buffer) {
if sz_r == 0 {
debug("EOF detected on stream");
Expand Down
21 changes: 21 additions & 0 deletions firecracker-pilot/src/firecracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@ pub fn check_connected(program_name: &String) -> Result<(), FlakeError> {
}
let mut buffer = [0; 14];
if let Ok(mut stream) = UnixStream::connect(&vsock_uds_path) {
let _ = stream.set_write_timeout(
Some(time::Duration::from_millis(200))
);
let _ = stream.set_read_timeout(
Some(time::Duration::from_millis(200))
);
stream.write_all(
format!("CONNECT {}\n", defaults::VM_PORT
).as_bytes())?;
Expand All @@ -448,6 +454,12 @@ pub fn check_connected(program_name: &String) -> Result<(), FlakeError> {
let some_time = time::Duration::from_millis(
defaults::VM_WAIT_TIMEOUT_MSEC
);
if Lookup::is_debug() {
debug!(
"Sleeping(check_connected): {}ms",
defaults::VM_WAIT_TIMEOUT_MSEC
);
}
thread::sleep(some_time);
retry_count += 1
}
Expand Down Expand Up @@ -508,6 +520,12 @@ pub fn send_command_to_instance(program_name: &String, exec_port: u32) -> i32 {
let some_time = time::Duration::from_millis(
defaults::VM_WAIT_TIMEOUT_MSEC
);
if Lookup::is_debug() {
debug!(
"Sleeping(send_command_to_instance): {}ms",
defaults::VM_WAIT_TIMEOUT_MSEC
);
}
thread::sleep(some_time);
} else {
break
Expand Down Expand Up @@ -543,6 +561,9 @@ pub fn execute_command_at_instance(
break
}
let some_time = time::Duration::from_millis(100);
if Lookup::is_debug() {
debug!("Sleeping(execute_command_at_instance): 100ms");
}
thread::sleep(some_time);
retry_count += 1
}
Expand Down

0 comments on commit 58d0b6c

Please sign in to comment.