Skip to content

Commit

Permalink
Implement waitid.
Browse files Browse the repository at this point in the history
Fixes #75.
  • Loading branch information
sunfishcode committed Nov 28, 2023
1 parent 9362ee5 commit 2e29be5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
48 changes: 46 additions & 2 deletions c-scape/src/process/wait.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::mem::zeroed;
use errno::{set_errno, Errno};
use libc::{c_int, pid_t};
use rustix::process::{Pid, WaitOptions};
use libc::{c_int, id_t, idtype_t, pid_t, siginfo_t};
use rustix::fd::BorrowedFd;
use rustix::process::{Pid, WaitId, WaitOptions, WaitidOptions};

use crate::convert_res;

Expand Down Expand Up @@ -62,3 +64,45 @@ unsafe extern "C" fn wait(status: *mut c_int) -> pid_t {
libc!(libc::wait(status));
waitpid(-1, status, 0)
}

#[no_mangle]
unsafe extern "C" fn waitid(
idtype: idtype_t,
id: id_t,
infop: *mut siginfo_t,
options: c_int,
) -> c_int {
libc!(libc::waitid(idtype, id, infop, options));

let id = match idtype {
libc::P_PID => {
if let Some(pid) = Pid::from_raw(id as _) {
WaitId::Pid(pid)
} else {
set_errno(Errno(libc::EINVAL));
return -1;
}
}
libc::P_PIDFD => WaitId::PidFd(BorrowedFd::borrow_raw(id as _)),
libc::P_PGID => WaitId::Pgid(Pid::from_raw(id as _)),
libc::P_ALL => WaitId::All,
_ => {
set_errno(Errno(libc::EINVAL));
return -1;
}
};

let options = WaitidOptions::from_bits(options as _).unwrap();

match convert_res(rustix::process::waitid(id, options)) {
Some(Some(new_info)) => {
*infop = zeroed();
(*infop).si_signo = new_info.as_raw().__bindgen_anon_1.__bindgen_anon_1.si_signo;
(*infop).si_errno = new_info.as_raw().__bindgen_anon_1.__bindgen_anon_1.si_errno;
(*infop).si_code = new_info.as_raw().__bindgen_anon_1.__bindgen_anon_1.si_code;
0
}
Some(None) => 0,
None => -1,
}
}
4 changes: 0 additions & 4 deletions c-scape/src/todo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ unsafe extern "C" fn process_vm_readv() {
todo!("process_vm_readv")
}
#[no_mangle]
unsafe extern "C" fn waitid() {
todo!("waitid")
}
#[no_mangle]
unsafe extern "C" fn signalfd() {
todo!("signalfd")
}
Expand Down

0 comments on commit 2e29be5

Please sign in to comment.