error when launching pty from rust "cannot set terminal process group (14206): Inappropriate ioctl for device"

134 Views Asked by At

I'm trying to open a pty shell in my program, it seems to work somewhat but in the output there seems to be an error that shows up even though the program seems to work despite it there also seems to be echoing happening but I don't know how to diable that

this program uses openpty function then writes the command uname -r to the master, then it waits for any incoming input and outputs it to the screen in a loop

CODE:

use std::process::{Command, Stdio};
use std::os::fd::{FromRawFd};
use nix::pty::{openpty};
use nix::unistd::{ read, write };

fn launch_pty(shell: &str) -> i32 {
    let ends = openpty(None, None).expect("Failed to open pty");
    let master = ends.master;
    let slave = ends.slave;

    let mut command = Command::new(shell);
    command.stdin(unsafe { Stdio::from_raw_fd(slave) });
    command.stdout(unsafe { Stdio::from_raw_fd(slave) });
    command.stderr(unsafe { Stdio::from_raw_fd(slave) });

    match command.spawn()  {
        Ok(_) => {
            master
        },
        Err(e) => {
            panic!("Failed to create pty: {}", e);
        }
    }


}

fn main() {
    let pty = launch_pty("/bin/bash");
    write(pty, b"uname -r\n").unwrap();
    

    loop {
        let mut buffer = [0; 4096];
        match read(pty, &mut buffer) {
            Ok(bytes_read) => {
                println!("{}", String::from_utf8(buffer[0..bytes_read].to_vec()).unwrap());
            },
            Err(_) => {
                break;
            }
        }
    }
    

}

Output:

uname -r
bash: cannot set terminal process group (14080): Inappropriate ioctl for device
bash: no job control in this shell
mahmoud@DESKTOP-MOT06CH:~/Documents/rust_terminal$ uname -r
5.15.90.1-microsoft-standard-WSL2

Somewhat unrelated:

I also tried making the program execute clear instead of uname -r but that doesn't seem to clear the screen, I assumed it would output some ansi escape code that clears the screen but the clear command just produces no output

however this is undrelated so answering the original question is just fine I could always create a new question for this

0

There are 0 best solutions below