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
}