Add posix_spawn and posix_spawnp for RedoxOS

This commit is contained in:
R Aadarsh
2026-05-14 18:47:42 +05:30
parent df78820f01
commit 1bc472e7e1
6 changed files with 190 additions and 35 deletions
+13 -16
View File
@@ -20,7 +20,7 @@ const FCHDIR: c_char = 4;
const DUP2: c_char = 5;
#[repr(C)]
enum Operation {
pub enum Operation {
Open {
fd: c_int,
path: *const c_char,
@@ -76,14 +76,13 @@ fn copy_op(file_actions: *mut posix_spawn_file_actions_t, op: Operation) -> Resu
pub unsafe extern "C" fn posix_spawn_file_actions_init(
file_actions: *mut posix_spawn_file_actions_t,
) -> c_int {
if file_actions.is_null() {
return EINVAL;
}
let file_actions = match unsafe { file_actions.as_mut().ok_or(Errno(EINVAL)) } {
Ok(v) => v,
Err(_) => return EINVAL,
};
unsafe {
(*file_actions).operation = null();
(*file_actions).size = 0;
}
(*file_actions).operation = null();
(*file_actions).size = 0;
0
}
@@ -92,15 +91,13 @@ pub unsafe extern "C" fn posix_spawn_file_actions_init(
pub unsafe extern "C" fn posix_spawn_file_actions_destroy(
file_actions: *mut posix_spawn_file_actions_t,
) -> c_int {
if file_actions.is_null() {
return EINVAL;
}
let file_actions = match unsafe { file_actions.as_mut().ok_or(Errno(EINVAL)) } {
Ok(v) => v,
Err(_) => return EINVAL,
};
unsafe {
free((*file_actions).operation as *mut c_void);
(*file_actions).operation = null();
(*file_actions).size = 0;
}
(*file_actions).operation = null();
(*file_actions).size = 0;
0
}
+92 -8
View File
@@ -5,21 +5,105 @@
mod file_actions;
mod spawn_attr;
use core::ffi::c_char;
pub use file_actions::{Operation, posix_spawn_file_actions_t};
pub use spawn_attr::{Flags, posix_spawnattr_t};
use crate::{
header::spawn::{file_actions::posix_spawn_file_actions_t, spawn_attr::posix_spawnattr_t},
platform::types::{c_int, pid_t},
c_str::CStr,
error::{Errno, Result},
header::{errno::ENOENT, stdlib::getenv},
platform::{
self, Pal,
types::{c_char, c_int, pid_t},
},
};
fn spawn(
mut program: &str,
file_actions: Option<&posix_spawn_file_actions_t>,
spawn_attr: Option<&posix_spawnattr_t>,
argv: *const *mut c_char,
envp: *const *mut c_char,
use_path: bool,
) -> Result<pid_t> {
if use_path {
let path_env = unsafe {
CStr::from_ptr(getenv("PATH".as_ptr() as *const c_char))
.to_str()
.unwrap()
};
let path_elements = path_env.split(':');
let program_name = program.split("/").last().unwrap();
let mut flag = false;
for element in path_elements {
if element.split("/").last().unwrap() == program_name {
flag = true;
program = element;
break;
}
if !flag {
return Err(Errno(ENOENT));
}
}
}
unsafe {
platform::Sys::spawn(
CStr::from_bytes_with_nul_unchecked(program.as_bytes()),
file_actions,
spawn_attr,
argv,
envp,
use_path,
)
}
}
#[unsafe(no_mangle)]
pub fn posix_spawn(
pub extern "C" fn posix_spawn(
pid: *const pid_t,
path: *const c_char,
program: *const c_char,
file_actions: *const posix_spawn_file_actions_t,
spawn_attr: *const posix_spawnattr_t,
argv: *const *const c_char,
envp: *const *const c_char,
argv: *const *mut c_char,
envp: *const *mut c_char,
) -> c_int {
todo!()
let program = unsafe { CStr::from_ptr(program).to_str().unwrap() };
match spawn(
program,
unsafe { file_actions.as_ref() },
unsafe { spawn_attr.as_ref() },
argv,
envp,
false,
) {
Ok(v) => v,
Err(e) => e.0,
}
}
#[unsafe(no_mangle)]
pub extern "C" fn posix_spawnp(
pid: *const pid_t,
program: *const c_char,
file_actions: *const posix_spawn_file_actions_t,
spawn_attr: *const posix_spawnattr_t,
argv: *const *mut c_char,
envp: *const *mut c_char,
) -> c_int {
let program = unsafe { CStr::from_ptr(program).to_str().unwrap() };
match spawn(
program,
unsafe { file_actions.as_ref() },
unsafe { spawn_attr.as_ref() },
argv,
envp,
true,
) {
Ok(v) => v,
Err(e) => e.0,
}
}
+5 -7
View File
@@ -13,7 +13,7 @@ use crate::header::{
};
bitflags::bitflags! {
struct Flags: c_short
pub struct Flags: c_short
{
const POSIX_SPAWN_RESETIDS = 1;
const POSIX_SPAWN_SETPGROUP = 2;
@@ -34,11 +34,11 @@ pub const POSIX_SPAWN_SETSCHEDULER: c_short = 6;
#[repr(C)]
pub struct posix_spawnattr_t {
pub(crate) param: sched_param,
flags: c_short,
pgroup: c_int,
pub(crate) flags: c_short,
pub(crate) pgroup: c_int,
policy: c_int,
sigdefault: sigset_t,
sigmask: sigset_t,
pub(crate) sigmask: sigset_t,
}
#[unsafe(no_mangle)]
@@ -217,9 +217,7 @@ pub unsafe extern "C" fn posix_spawnattr_getflags(
return EINVAL;
}
unsafe {
*flags = (*attr).flags;
}
unsafe { *flags = (*attr).flags }
0
}