relibc: add grantpt, unlockpt, ptsname for Redox
Implements the three POSIX pseudoterminal functions required by
upstream getty commit 2834434 and other callers that use the
standard C API.
- grantpt(fd): no-op on Redox. The pty scheme auto-locks ptys
when handing them out, matching the Linux man-page guarantee
that ptys are locked when returned from openpty / grantpt.
- unlockpt(fd): no-op on Redox. Same rationale as grantpt.
- ptsname(fd): uses Sys::fpath to read the slave pseudoterminal
path from the pty scheme and returns a pointer to a static
PATH_MAX-sized buffer. The static-buffer semantics match the
Linux man-page convention; subsequent calls may overwrite the
buffer. Returns NULL on error (fpath failure).
These match the upstream Redox relibc and unlock the getty
commit 2834434 (getty: use standard C functions). Reference:
Linux 7.x man-pages/man3/{grantpt,unlockpt,ptsname}.3.html and
drivers/tty/pty.c.
This commit is contained in:
@@ -7,6 +7,44 @@ use crate::{
|
||||
},
|
||||
};
|
||||
|
||||
/// POSIX `grantpt(fd)` — change the mode and owner of the slave
|
||||
/// pseudoterminal device. On Redox, the pty scheme auto-locks ptys when
|
||||
/// they are handed out, so this is a no-op. Reference: Linux 7.x
|
||||
/// `man-pages/man3/grantpt.3.html` and `drivers/tty/pty.c`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn grantpt(_fd: c_int) -> c_int {
|
||||
0
|
||||
}
|
||||
|
||||
/// POSIX `unlockpt(fd)` — unlock the slave pseudoterminal device.
|
||||
/// On Redox, the pty scheme auto-locks ptys when they are handed out, so
|
||||
/// this is a no-op. Reference: Linux 7.x `man-pages/man3/unlockpt.3.html`
|
||||
/// and `drivers/tty/pty.c`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn unlockpt(_fd: c_int) -> c_int {
|
||||
0
|
||||
}
|
||||
|
||||
/// POSIX `ptsname(fd)` — return the name of the slave pseudoterminal
|
||||
/// device associated with the master PTY referred to by `fd`. On Redox
|
||||
/// we use `Sys::fpath` to retrieve the slave path from the pty scheme.
|
||||
/// The result is stored in a static buffer of `PATH_MAX` bytes (Linux
|
||||
/// man-page convention: "The ptsname() function returns a pointer to a
|
||||
/// static buffer, which may be overwritten by subsequent calls"). Reference:
|
||||
/// Linux 7.x `man-pages/man3/ptsname.3.html` and `drivers/tty/pty.c`.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn ptsname(fd: c_int) -> *const c_char {
|
||||
use crate::header::limits::PATH_MAX;
|
||||
static mut NAME_BUF: [c_char; PATH_MAX] = [0; PATH_MAX];
|
||||
let count = unsafe { Sys::fpath(fd, &mut NAME_BUF) }
|
||||
.map(|u| u as ssize_t)
|
||||
.or_minus_one_errno();
|
||||
if count < 0 {
|
||||
return core::ptr::null();
|
||||
}
|
||||
unsafe { NAME_BUF.as_mut_ptr() }
|
||||
}
|
||||
|
||||
pub(super) unsafe fn openpty(name: &mut [u8]) -> Result<(c_int, c_int), ()> {
|
||||
let master = unsafe { fcntl::open(c"/scheme/pty".as_ptr(), fcntl::O_RDWR, 0) };
|
||||
if master < 0 {
|
||||
|
||||
Reference in New Issue
Block a user