Add forkpty, ptsname and posix_openpt

This commit is contained in:
Darley Barreto
2024-01-17 03:05:50 +00:00
committed by Jeremy Soller
parent faadbc3d70
commit 9d8094baee
10 changed files with 291 additions and 7 deletions
+4
View File
@@ -7,3 +7,7 @@ cpp_compat = true
[enum]
prefix_with_name = true
[export.rename]
"winsize" = "struct winsize"
"termios" = "struct termios"
+75 -4
View File
@@ -1,10 +1,10 @@
//! pty.h implementation, not POSIX specified
use core::slice;
use core::{mem, ptr, slice};
use crate::{
header::{limits, sys_ioctl, termios, unistd},
platform::types::*,
header::{fcntl, limits, pthread, signal, sys_ioctl, sys_wait, termios, unistd, utmp},
platform::{self, types::*},
};
#[cfg(target_os = "linux")]
@@ -24,7 +24,7 @@ pub unsafe extern "C" fn openpty(
winp: *const sys_ioctl::winsize,
) -> c_int {
let mut tmp_name = [0; limits::PATH_MAX];
let mut name = if !namep.is_null() {
let name = if !namep.is_null() {
slice::from_raw_parts_mut(namep as *mut u8, limits::PATH_MAX)
} else {
&mut tmp_name
@@ -48,3 +48,74 @@ pub unsafe extern "C" fn openpty(
return 0;
}
#[no_mangle]
pub unsafe extern "C" fn forkpty(
pm: *mut c_int,
name: *mut c_char,
tio: *const termios::termios,
ws: *const sys_ioctl::winsize,
) -> c_int {
let mut m = 0;
let mut s = 0;
let mut ec = 0;
let mut p: [c_int; 2] = [0; 2];
let mut cs = 0;
let mut pid = -1;
let mut set = signal::sigset_t::default();
let mut oldset = signal::sigset_t::default();
if openpty(&mut m, &mut s, name, tio, ws) < 0 {
return -1;
}
signal::sigfillset(&mut set);
signal::pthread_sigmask(signal::SIG_BLOCK, &mut set, &mut oldset);
pthread::pthread_setcancelstate(pthread::PTHREAD_CANCEL_DISABLE, &mut cs);
if unistd::pipe2(p.as_mut_ptr(), fcntl::O_CLOEXEC) != 0 {
unistd::close(s);
} else {
pid = unistd::fork();
if pid == 0 {
unistd::close(m);
unistd::close(p[0]);
if utmp::login_tty(s) != 0 {
unistd::write(
p[1],
&platform::errno as *const _ as *const c_void,
mem::size_of::<c_int>(),
);
unistd::_exit(127);
}
unistd::close(p[1]);
pthread::pthread_setcancelstate(cs, ptr::null_mut());
signal::pthread_sigmask(signal::SIG_SETMASK, &mut oldset, ptr::null_mut());
return 0;
}
unistd::close(s);
unistd::close(p[1]);
if unistd::read(
p[0],
&mut ec as *mut c_int as *mut c_void,
mem::size_of::<c_int>(),
) > 0
{
let mut status = 0;
sys_wait::waitpid(pid, &mut status, 0);
pid = -1;
platform::errno = ec;
}
unistd::close(p[0]);
}
if pid > 0 {
*pm = m;
} else {
unistd::close(m);
}
pthread::pthread_setcancelstate(cs, ptr::null_mut());
signal::pthread_sigmask(signal::SIG_SETMASK, &mut oldset, ptr::null_mut());
pid
}
+82 -3
View File
@@ -726,9 +726,88 @@ pub unsafe extern "C" fn posix_memalign(
}
}
// #[no_mangle]
pub extern "C" fn ptsname(fildes: c_int) -> *mut c_char {
unimplemented!();
#[no_mangle]
pub unsafe extern "C" fn posix_openpt(flags: c_int) -> c_int {
#[cfg(target_os = "redox")]
let r = open((b"pty:\0" as *const u8).cast(), O_CREAT);
#[cfg(target_os = "linux")]
let r = open((b"/dev/ptmx\0" as *const u8).cast(), flags);
if r < 0 && platform::errno == ENOSPC {
platform::errno = EAGAIN;
}
return r;
}
#[no_mangle]
unsafe extern "C" fn ptsname(fd: c_int) -> *mut c_char {
static mut PTS_BUFFER: [c_char; 9 + mem::size_of::<c_int>() * 3 + 1] =
[0; 9 + mem::size_of::<c_int>() * 3 + 1];
if ptsname_r(fd, PTS_BUFFER.as_mut_ptr(), PTS_BUFFER.len()) != 0 {
ptr::null_mut()
} else {
PTS_BUFFER.as_mut_ptr()
}
}
#[no_mangle]
unsafe extern "C" fn ptsname_r(fd: c_int, buf: *mut c_char, buflen: size_t) -> c_int {
if buf.is_null() {
platform::errno = EINVAL;
EINVAL
} else {
__ptsname_r(fd, buf, buflen)
}
}
#[cfg(target_os = "redox")]
#[inline(always)]
unsafe fn __ptsname_r(fd: c_int, buf: *mut c_char, buflen: size_t) -> c_int {
let tty_ptr = unistd::ttyname(fd);
if !tty_ptr.is_null() {
if let Ok(name) = CStr::from_ptr(tty_ptr).to_str() {
let len = name.len();
if len > buflen {
platform::errno = ERANGE;
return ERANGE;
} else {
// we have checked the string will fit in the buffer
// so can use strcpy safely
let s = name.as_ptr().cast();
ptr::copy_nonoverlapping(s, buf, len);
return 0;
}
}
}
platform::errno
}
#[cfg(target_os = "linux")]
#[inline(always)]
unsafe fn __ptsname_r(fd: c_int, buf: *mut c_char, buflen: size_t) -> c_int {
let mut pty = 0;
let err = platform::errno;
if ioctl(fd, TIOCGPTN, &mut pty as *mut _ as *mut c_void) == 0 {
let name = format!("/dev/pts/{}", pty);
let len = name.len();
if len > buflen {
platform::errno = ERANGE;
ERANGE
} else {
// we have checked the string will fit in the buffer
// so can use strcpy safely
let s = name.as_ptr().cast();
ptr::copy_nonoverlapping(s, buf, len);
platform::errno = err;
0
}
} else {
platform::errno
}
}
unsafe fn put_new_env(insert: *mut c_char) {