diff --git a/native/src/core/lib.rs b/native/src/core/lib.rs index 95e276dbf..1fdbb9cbd 100644 --- a/native/src/core/lib.rs +++ b/native/src/core/lib.rs @@ -16,7 +16,7 @@ use logging::{android_logging, setup_logfile, zygisk_close_logd, zygisk_get_logd use mount::{find_preinit_device, revert_unmount}; use resetprop::{persist_delete_prop, persist_get_prop, persist_get_props, persist_set_prop}; use socket::{recv_fd, recv_fds, send_fd, send_fds}; -use su::{pump_stdin_stdout, get_pty_num, restore_stdin}; +use su::{pump_tty, get_pty_num, restore_stdin}; use std::fs::File; use std::mem::ManuallyDrop; use std::ops::DerefMut; @@ -209,7 +209,7 @@ pub mod ffi { #[namespace = "rust"] fn daemon_entry(); - fn pump_stdin_stdout(infd: i32, outfd: i32); + fn pump_tty(infd: i32, outfd: i32); fn get_pty_num(fd: i32) -> i32; fn restore_stdin() -> bool; } diff --git a/native/src/core/su/mod.rs b/native/src/core/su/mod.rs index b77db4b2e..59fafe849 100644 --- a/native/src/core/su/mod.rs +++ b/native/src/core/su/mod.rs @@ -3,4 +3,4 @@ mod db; mod pts; pub use daemon::SuInfo; -pub use pts::{pump_stdin_stdout, get_pty_num, restore_stdin}; +pub use pts::{pump_tty, get_pty_num, restore_stdin}; diff --git a/native/src/core/su/pts.rs b/native/src/core/su/pts.rs index 68d2ae49f..b6295d0ff 100644 --- a/native/src/core/su/pts.rs +++ b/native/src/core/su/pts.rs @@ -28,10 +28,14 @@ fn set_stdin_raw() -> bool { return false; } + let old_c_oflag = termios.c_oflag; OLD_STDIN = Some(termios); cfmakeraw(&mut termios); + // don't modify output flags, since we are not setting stdout raw + termios.c_oflag = old_c_oflag; + if tcsetattr(STDIN_FILENO, TCSAFLUSH, &termios) < 0 && tcsetattr(STDIN_FILENO, TCSADRAIN, &termios) < 0 { @@ -65,7 +69,7 @@ fn resize_pty(outfd: i32) { } } -pub fn pump_stdin_stdout(infd: i32, outfd: i32) { +pub fn pump_tty(infd: i32, outfd: i32) { set_stdin_raw(); let sfd = unsafe { @@ -82,7 +86,7 @@ pub fn pump_stdin_stdout(infd: i32, outfd: i32) { let mut pfds = [ pollfd { - fd: STDIN_FILENO, + fd: if outfd > 0 { STDIN_FILENO } else { -1 }, events: POLLIN, revents: 0, }, diff --git a/native/src/core/su/su.cpp b/native/src/core/su/su.cpp index 772b248e5..b8a33ee62 100644 --- a/native/src/core/su/su.cpp +++ b/native/src/core/su/su.cpp @@ -239,7 +239,9 @@ int su_client_main(int argc, char *argv[]) { if (atty) { setup_sighandlers(sighandler); - pump_stdin_stdout(ptmx, ptmx); + // if stdin is not a tty, if we pump to ptmx, our process may intercept the input to ptmx and + // output to stdout, which cause the target process lost input. + pump_tty(ptmx, (atty & ATTY_IN) ? ptmx : -1); } // Get the exit code