Redirect stdio (pipes and file descriptors)

Hi @Kestrer

My main aim of the program is something similar to this

  • Program executes a command "sh" and creates pipes between the parent process and the child process

  • Output from the child process goes through the pipe (in this case the prompt) to the parent process

  • I type something on my terminal such as "pwd" and this goes into the pipe to the child process

  • Child process executes the command and sends the output back through the pipe to the parent process and displays on my terminal

How should I do this? I am confused by all the redirection and not even sure if I am redirecting correctly.

Eventually, my aim is to have the parent process act as a proxy and forward this I/O through a TLS connection between a remote ncat instance and the child process. For now, I am trying to get the piping part right.

My latest code snippet

		let message_string = String::from("fd_pipe\0");
		let message = message_string.as_ptr() as *const c_void;

		let mut command_output = std::process::Command::new("/bin/sh")
									.stdin(Stdio::piped())
									.stdout(Stdio::piped())
									.stderr(Stdio::piped())
									.spawn()
									.expect("cannot execute command");

		let command_stdin = command_output.stdin.unwrap();
		println!("command_stdin {}", command_stdin.as_raw_fd());
		
		let command_stdout = command_output.stdout.unwrap();
		println!("command_stdout {}", command_stdout.as_raw_fd());
		
		let command_stderr = command_output.stderr.unwrap();
		println!("command_stderr {}", command_stderr.as_raw_fd());

		nix::unistd::dup2(tokio::io::stdin().as_raw_fd(),command_stdin.as_raw_fd());
		nix::unistd::dup2(tokio::io::stdout().as_raw_fd(),command_stdout.as_raw_fd());
		nix::unistd::dup2(tokio::io::stderr().as_raw_fd(),command_stderr.as_raw_fd());