getty: use standard C functions

grantpt() and unlockpt() need to be used according to the standard, even
though our grantpt is a no-op (since we auto-lock ptys as we give them
out).

This is part of my series for ptyd. This can be safely merged as long as
the relibc/ and base/ changes are merged at the same time.
This commit is contained in:
Connor-GH
2026-06-03 19:15:31 -05:00
committed by vasilito
parent 670693e114
commit ac3cff2c7a
+20 -11
View File
@@ -1,6 +1,7 @@
#[macro_use] #[macro_use]
extern crate clap; extern crate clap;
use core::ptr::slice_from_raw_parts;
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::{self, ErrorKind, Read, Stderr, Write}; use std::io::{self, ErrorKind, Read, Stderr, Write};
@@ -11,6 +12,7 @@ use std::time::{Duration, Instant};
use event::{EventFlags, RawEventQueue}; use event::{EventFlags, RawEventQueue};
use extra::io::fail; use extra::io::fail;
use libc::{grantpt, ptsname, strlen, unlockpt};
use libredox::call as redox; use libredox::call as redox;
use libredox::errno::EAGAIN; use libredox::errno::EAGAIN;
use libredox::flag; use libredox::flag;
@@ -114,7 +116,7 @@ pub fn handle(
pub fn getpty(columns: u16, lines: u16) -> (RawFd, String) { pub fn getpty(columns: u16, lines: u16) -> (RawFd, String) {
let master = redox::open( let master = redox::open(
"/scheme/pty", "/scheme/pty/ptmx",
flag::O_CLOEXEC | flag::O_RDWR | flag::O_CREAT | flag::O_NONBLOCK, flag::O_CLOEXEC | flag::O_RDWR | flag::O_CREAT | flag::O_NONBLOCK,
0, 0,
) )
@@ -130,9 +132,12 @@ pub fn getpty(columns: u16, lines: u16) -> (RawFd, String) {
); );
let _ = redox::close(winsize_fd); let _ = redox::close(winsize_fd);
} }
let _ = unsafe { grantpt(master as RawFd) };
let _ = unsafe { unlockpt(master as RawFd) };
let mut buf: [u8; 4096] = [0; 4096]; let name = unsafe { ptsname(master as RawFd) };
let count = redox::fpath(master, &mut buf).unwrap(); let count = unsafe { strlen(name) };
let buf = unsafe { &*slice_from_raw_parts(name.cast(), count) };
(master as RawFd, unsafe { (master as RawFd, unsafe {
String::from_utf8_unchecked(Vec::from(&buf[..count])) String::from_utf8_unchecked(Vec::from(&buf[..count]))
}) })
@@ -149,15 +154,19 @@ fn tty_cursor_pos(tty: &mut File) -> Result<(u16, u16), Box<dyn Error>> {
while instant.elapsed() < timeout { while instant.elapsed() < timeout {
let mut bytes = [0]; let mut bytes = [0];
match tty.read(&mut bytes) { match tty.read(&mut bytes) {
Ok(count) => if count == 1 { Ok(count) => {
let c = bytes[0] as char; if count == 1 {
if c == 'R' { let c = bytes[0] as char;
break; if c == 'R' {
break;
}
data.push(c);
}
}
Err(err) => {
if err.kind() != ErrorKind::WouldBlock {
return Err(err.into());
} }
data.push(c);
},
Err(err) => if err.kind() != ErrorKind::WouldBlock {
return Err(err.into());
} }
} }
} }