Skip to content

Commit c0a1fb7

Browse files
committed
Code cleanup
1 parent 4864c11 commit c0a1fb7

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

native/src/core/su/pts.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
use base::{
2-
error,
2+
ResultExt, error,
33
libc::{
4-
POLLIN, SFD_CLOEXEC, SIG_BLOCK, SIGWINCH, STDOUT_FILENO, TCSADRAIN, TCSAFLUSH, TIOCGWINSZ,
5-
TIOCSWINSZ, cfmakeraw, close, poll, pollfd, raise, read, sigaddset, sigemptyset, signalfd,
6-
sigprocmask, sigset_t, tcsetattr, winsize, write,
4+
POLLIN, SFD_CLOEXEC, SIG_BLOCK, SIGWINCH, TCSADRAIN, TCSAFLUSH, TIOCGWINSZ, TIOCSWINSZ,
5+
cfmakeraw, close, poll, pollfd, raise, sigaddset, sigemptyset, signalfd, sigprocmask,
6+
sigset_t, tcsetattr, winsize,
77
},
8-
libc::{STDIN_FILENO, ioctl, tcgetattr, termios},
8+
libc::{STDIN_FILENO, STDOUT_FILENO, tcgetattr, termios},
99
warn,
1010
};
11+
use std::fs::File;
12+
use std::io::{Read, Write};
13+
use std::mem::ManuallyDrop;
14+
use std::os::fd::{FromRawFd, RawFd};
1115
use std::ptr::null_mut;
1216

1317
static mut OLD_STDIN: Option<termios> = None;
18+
const TIOCGPTN: u32 = 0x80045430;
19+
20+
unsafe extern "C" {
21+
// Don't use the declaration from the libc crate as request should be u32 not i32
22+
fn ioctl(fd: RawFd, request: u32, ...) -> i32;
23+
}
1424

1525
pub fn get_pty_num(fd: i32) -> i32 {
1626
let mut pty_num = -1i32;
17-
if unsafe { ioctl(fd, 0x80045430u32 as _, &mut pty_num) } != 0 {
27+
if unsafe { ioctl(fd, TIOCGPTN, &mut pty_num) } != 0 {
1828
warn!("Failed to get pty number");
1929
}
2030
pty_num
@@ -64,8 +74,8 @@ pub fn restore_stdin() -> bool {
6474

6575
fn resize_pty(outfd: i32) {
6676
let mut ws: winsize = unsafe { std::mem::zeroed() };
67-
if unsafe { ioctl(STDIN_FILENO, TIOCGWINSZ, &mut ws) } >= 0 {
68-
unsafe { ioctl(outfd, TIOCSWINSZ, &ws) };
77+
if unsafe { ioctl(STDIN_FILENO, TIOCGWINSZ as u32, &mut ws) } >= 0 {
78+
unsafe { ioctl(outfd, TIOCSWINSZ as u32, &ws) };
6979
}
7080
}
7181

@@ -113,15 +123,19 @@ pub fn pump_tty(infd: i32, outfd: i32) {
113123

114124
for pfd in &pfds {
115125
if pfd.revents & POLLIN != 0 {
116-
let n = unsafe { read(pfd.fd, buf.as_mut_ptr() as _, buf.len()) };
117-
if n <= 0 {
126+
let mut in_file = ManuallyDrop::new(unsafe { File::from_raw_fd(pfd.fd) });
127+
128+
let Ok(n) = in_file.read(&mut buf) else {
118129
error!("read error");
119130
break 'poll;
120-
}
131+
};
132+
121133
if pfd.fd == STDIN_FILENO {
122-
unsafe { write(outfd, buf.as_ptr() as _, n as _) };
134+
let mut out = ManuallyDrop::new(unsafe { File::from_raw_fd(outfd) });
135+
out.write_all(&buf[..n]).log_ok();
123136
} else if pfd.fd == infd {
124-
unsafe { write(STDOUT_FILENO, buf.as_ptr() as _, n as _) };
137+
let mut out = ManuallyDrop::new(unsafe { File::from_raw_fd(STDOUT_FILENO) });
138+
out.write_all(&buf[..n]).log_ok();
125139
} else if pfd.fd == sfd {
126140
resize_pty(outfd);
127141
}

0 commit comments

Comments
 (0)