Skip to content

Commit

Permalink
calls: Update clone/clone3
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jul 23, 2024
1 parent 789ffe4 commit bc3e14f
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 56 deletions.
14 changes: 14 additions & 0 deletions examples/clone3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main() {
let mut args = nc::clone_args_t::default();
let mut pid_fd: i32 = -1;
args.exit_signal = nc::SIGCHLD as u64;
args.pidfd = &mut pid_fd as *mut i32 as usize as u64;
args.flags = nc::CLONE_PIDFD as u64 | nc::CLONE_PARENT_SETTID as u64;
let pid = unsafe { nc::clone3(&mut args) };
match pid {
Err(errno) => eprintln!("clone3() failed, err: {:?}", nc::strerror(errno)),
Ok(0) => println!("[child] pid"),
Ok(pid) => println!("[parent] pid: {pid:?}"),
}
assert!(pid.is_ok());
}
2 changes: 1 addition & 1 deletion src/calls/abort2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// Abort process with diagnostics
pub unsafe fn abort2(why: &str, args_len: i32, args: usize) -> ! {
pub unsafe fn abort2(why: &str, args_len: i32, args: uinptr_t) -> ! {
let why = CString::new(why);
let why_ptr = why.as_ptr() as usize;
let args_len = args_len as usize;
Expand Down
2 changes: 1 addition & 1 deletion src/calls/add_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub unsafe fn add_key<P: AsRef<Path>>(
type_: P,
description: P,
payload: usize,
payload: uintptr_t,
plen: size_t,
dest_keyring: key_serial_t,
) -> Result<key_serial_t, Errno> {
Expand Down
3 changes: 1 addition & 2 deletions src/calls/clone.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/// Create a child process.
pub unsafe fn clone(
clone_flags: i32,
clone_flags: usize,
newsp: usize,
parent_tid: &mut i32,
child_tid: &mut i32,
tls: usize,
) -> Result<pid_t, Errno> {
let clone_flags = clone_flags as usize;
let parent_tid_ptr = parent_tid as *mut i32 as usize;
let child_tid_ptr = child_tid as *mut i32 as usize;
syscall5(
Expand Down
5 changes: 3 additions & 2 deletions src/calls/clone3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
/// args.exit_signal = nc::SIGCHLD as u64;
/// args.pidfd = &mut pid_fd as *mut i32 as usize as u64;
/// args.flags = nc::CLONE_PIDFD as u64 | nc::CLONE_PARENT_SETTID as u64;
/// let pid = unsafe { nc::clone3(&mut args, core::mem::size_of::<nc::clone_args_t>()) };
/// let pid = unsafe { nc::clone3(&mut args) };
/// assert!(pid.is_ok());
/// ```
pub unsafe fn clone3(cl_args: &mut clone_args_t, size: size_t) -> Result<pid_t, Errno> {
pub unsafe fn clone3(cl_args: &mut clone_args_t) -> Result<pid_t, Errno> {
let cl_args_ptr = cl_args as *mut clone_args_t as usize;
let size = core::mem::size_of::<clone_args_t>();
syscall2(SYS_CLONE3, cl_args_ptr, size).map(|ret| ret as pid_t)
}
10 changes: 5 additions & 5 deletions src/platform/linux-aarch64/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub unsafe fn acct<P: AsRef<Path>>(filename: P) -> Result<(), Errno> {
pub unsafe fn add_key<P: AsRef<Path>>(
type_: P,
description: P,
payload: usize,
payload: uintptr_t,
plen: size_t,
dest_keyring: key_serial_t,
) -> Result<key_serial_t, Errno> {
Expand Down Expand Up @@ -321,13 +321,12 @@ pub unsafe fn clock_settime(which_clock: clockid_t, tp: &timespec_t) -> Result<(

/// Create a child process.
pub unsafe fn clone(
clone_flags: i32,
clone_flags: usize,
newsp: usize,
parent_tid: &mut i32,
child_tid: &mut i32,
tls: usize,
) -> Result<pid_t, Errno> {
let clone_flags = clone_flags as usize;
let parent_tid_ptr = parent_tid as *mut i32 as usize;
let child_tid_ptr = child_tid as *mut i32 as usize;
syscall5(
Expand All @@ -351,11 +350,12 @@ pub unsafe fn clone(
/// args.exit_signal = nc::SIGCHLD as u64;
/// args.pidfd = &mut pid_fd as *mut i32 as usize as u64;
/// args.flags = nc::CLONE_PIDFD as u64 | nc::CLONE_PARENT_SETTID as u64;
/// let pid = unsafe { nc::clone3(&mut args, core::mem::size_of::<nc::clone_args_t>()) };
/// let pid = unsafe { nc::clone3(&mut args) };
/// assert!(pid.is_ok());
/// ```
pub unsafe fn clone3(cl_args: &mut clone_args_t, size: size_t) -> Result<pid_t, Errno> {
pub unsafe fn clone3(cl_args: &mut clone_args_t) -> Result<pid_t, Errno> {
let cl_args_ptr = cl_args as *mut clone_args_t as usize;
let size = core::mem::size_of::<clone_args_t>();
syscall2(SYS_CLONE3, cl_args_ptr, size).map(|ret| ret as pid_t)
}

Expand Down
10 changes: 5 additions & 5 deletions src/platform/linux-arm/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub unsafe fn acct<P: AsRef<Path>>(filename: P) -> Result<(), Errno> {
pub unsafe fn add_key<P: AsRef<Path>>(
type_: P,
description: P,
payload: usize,
payload: uintptr_t,
plen: size_t,
dest_keyring: key_serial_t,
) -> Result<key_serial_t, Errno> {
Expand Down Expand Up @@ -407,13 +407,12 @@ pub unsafe fn clock_settime(which_clock: clockid_t, tp: &timespec_t) -> Result<(

/// Create a child process.
pub unsafe fn clone(
clone_flags: i32,
clone_flags: usize,
newsp: usize,
parent_tid: &mut i32,
child_tid: &mut i32,
tls: usize,
) -> Result<pid_t, Errno> {
let clone_flags = clone_flags as usize;
let parent_tid_ptr = parent_tid as *mut i32 as usize;
let child_tid_ptr = child_tid as *mut i32 as usize;
syscall5(
Expand All @@ -437,11 +436,12 @@ pub unsafe fn clone(
/// args.exit_signal = nc::SIGCHLD as u64;
/// args.pidfd = &mut pid_fd as *mut i32 as usize as u64;
/// args.flags = nc::CLONE_PIDFD as u64 | nc::CLONE_PARENT_SETTID as u64;
/// let pid = unsafe { nc::clone3(&mut args, core::mem::size_of::<nc::clone_args_t>()) };
/// let pid = unsafe { nc::clone3(&mut args) };
/// assert!(pid.is_ok());
/// ```
pub unsafe fn clone3(cl_args: &mut clone_args_t, size: size_t) -> Result<pid_t, Errno> {
pub unsafe fn clone3(cl_args: &mut clone_args_t) -> Result<pid_t, Errno> {
let cl_args_ptr = cl_args as *mut clone_args_t as usize;
let size = core::mem::size_of::<clone_args_t>();
syscall2(SYS_CLONE3, cl_args_ptr, size).map(|ret| ret as pid_t)
}

Expand Down
10 changes: 5 additions & 5 deletions src/platform/linux-loongarch64/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub unsafe fn acct<P: AsRef<Path>>(filename: P) -> Result<(), Errno> {
pub unsafe fn add_key<P: AsRef<Path>>(
type_: P,
description: P,
payload: usize,
payload: uintptr_t,
plen: size_t,
dest_keyring: key_serial_t,
) -> Result<key_serial_t, Errno> {
Expand Down Expand Up @@ -278,13 +278,12 @@ pub unsafe fn clock_settime(which_clock: clockid_t, tp: &timespec_t) -> Result<(

/// Create a child process.
pub unsafe fn clone(
clone_flags: i32,
clone_flags: usize,
newsp: usize,
parent_tid: &mut i32,
child_tid: &mut i32,
tls: usize,
) -> Result<pid_t, Errno> {
let clone_flags = clone_flags as usize;
let parent_tid_ptr = parent_tid as *mut i32 as usize;
let child_tid_ptr = child_tid as *mut i32 as usize;
syscall5(
Expand All @@ -308,11 +307,12 @@ pub unsafe fn clone(
/// args.exit_signal = nc::SIGCHLD as u64;
/// args.pidfd = &mut pid_fd as *mut i32 as usize as u64;
/// args.flags = nc::CLONE_PIDFD as u64 | nc::CLONE_PARENT_SETTID as u64;
/// let pid = unsafe { nc::clone3(&mut args, core::mem::size_of::<nc::clone_args_t>()) };
/// let pid = unsafe { nc::clone3(&mut args) };
/// assert!(pid.is_ok());
/// ```
pub unsafe fn clone3(cl_args: &mut clone_args_t, size: size_t) -> Result<pid_t, Errno> {
pub unsafe fn clone3(cl_args: &mut clone_args_t) -> Result<pid_t, Errno> {
let cl_args_ptr = cl_args as *mut clone_args_t as usize;
let size = core::mem::size_of::<clone_args_t>();
syscall2(SYS_CLONE3, cl_args_ptr, size).map(|ret| ret as pid_t)
}

Expand Down
10 changes: 5 additions & 5 deletions src/platform/linux-mips/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub unsafe fn acct<P: AsRef<Path>>(filename: P) -> Result<(), Errno> {
pub unsafe fn add_key<P: AsRef<Path>>(
type_: P,
description: P,
payload: usize,
payload: uintptr_t,
plen: size_t,
dest_keyring: key_serial_t,
) -> Result<key_serial_t, Errno> {
Expand Down Expand Up @@ -449,13 +449,12 @@ pub unsafe fn clock_settime(which_clock: clockid_t, tp: &timespec_t) -> Result<(

/// Create a child process.
pub unsafe fn clone(
clone_flags: i32,
clone_flags: usize,
newsp: usize,
parent_tid: &mut i32,
child_tid: &mut i32,
tls: usize,
) -> Result<pid_t, Errno> {
let clone_flags = clone_flags as usize;
let parent_tid_ptr = parent_tid as *mut i32 as usize;
let child_tid_ptr = child_tid as *mut i32 as usize;
syscall5(
Expand All @@ -479,11 +478,12 @@ pub unsafe fn clone(
/// args.exit_signal = nc::SIGCHLD as u64;
/// args.pidfd = &mut pid_fd as *mut i32 as usize as u64;
/// args.flags = nc::CLONE_PIDFD as u64 | nc::CLONE_PARENT_SETTID as u64;
/// let pid = unsafe { nc::clone3(&mut args, core::mem::size_of::<nc::clone_args_t>()) };
/// let pid = unsafe { nc::clone3(&mut args) };
/// assert!(pid.is_ok());
/// ```
pub unsafe fn clone3(cl_args: &mut clone_args_t, size: size_t) -> Result<pid_t, Errno> {
pub unsafe fn clone3(cl_args: &mut clone_args_t) -> Result<pid_t, Errno> {
let cl_args_ptr = cl_args as *mut clone_args_t as usize;
let size = core::mem::size_of::<clone_args_t>();
syscall2(SYS_CLONE3, cl_args_ptr, size).map(|ret| ret as pid_t)
}

Expand Down
10 changes: 5 additions & 5 deletions src/platform/linux-mips64/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub unsafe fn acct<P: AsRef<Path>>(filename: P) -> Result<(), Errno> {
pub unsafe fn add_key<P: AsRef<Path>>(
type_: P,
description: P,
payload: usize,
payload: uintptr_t,
plen: size_t,
dest_keyring: key_serial_t,
) -> Result<key_serial_t, Errno> {
Expand Down Expand Up @@ -433,13 +433,12 @@ pub unsafe fn clock_settime(which_clock: clockid_t, tp: &timespec_t) -> Result<(

/// Create a child process.
pub unsafe fn clone(
clone_flags: i32,
clone_flags: usize,
newsp: usize,
parent_tid: &mut i32,
child_tid: &mut i32,
tls: usize,
) -> Result<pid_t, Errno> {
let clone_flags = clone_flags as usize;
let parent_tid_ptr = parent_tid as *mut i32 as usize;
let child_tid_ptr = child_tid as *mut i32 as usize;
syscall5(
Expand All @@ -463,11 +462,12 @@ pub unsafe fn clone(
/// args.exit_signal = nc::SIGCHLD as u64;
/// args.pidfd = &mut pid_fd as *mut i32 as usize as u64;
/// args.flags = nc::CLONE_PIDFD as u64 | nc::CLONE_PARENT_SETTID as u64;
/// let pid = unsafe { nc::clone3(&mut args, core::mem::size_of::<nc::clone_args_t>()) };
/// let pid = unsafe { nc::clone3(&mut args) };
/// assert!(pid.is_ok());
/// ```
pub unsafe fn clone3(cl_args: &mut clone_args_t, size: size_t) -> Result<pid_t, Errno> {
pub unsafe fn clone3(cl_args: &mut clone_args_t) -> Result<pid_t, Errno> {
let cl_args_ptr = cl_args as *mut clone_args_t as usize;
let size = core::mem::size_of::<clone_args_t>();
syscall2(SYS_CLONE3, cl_args_ptr, size).map(|ret| ret as pid_t)
}

Expand Down
10 changes: 5 additions & 5 deletions src/platform/linux-ppc64/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub unsafe fn acct<P: AsRef<Path>>(filename: P) -> Result<(), Errno> {
pub unsafe fn add_key<P: AsRef<Path>>(
type_: P,
description: P,
payload: usize,
payload: uintptr_t,
plen: size_t,
dest_keyring: key_serial_t,
) -> Result<key_serial_t, Errno> {
Expand Down Expand Up @@ -442,13 +442,12 @@ pub unsafe fn clock_settime(which_clock: clockid_t, tp: &timespec_t) -> Result<(

/// Create a child process.
pub unsafe fn clone(
clone_flags: i32,
clone_flags: usize,
newsp: usize,
parent_tid: &mut i32,
child_tid: &mut i32,
tls: usize,
) -> Result<pid_t, Errno> {
let clone_flags = clone_flags as usize;
let parent_tid_ptr = parent_tid as *mut i32 as usize;
let child_tid_ptr = child_tid as *mut i32 as usize;
syscall5(
Expand All @@ -472,11 +471,12 @@ pub unsafe fn clone(
/// args.exit_signal = nc::SIGCHLD as u64;
/// args.pidfd = &mut pid_fd as *mut i32 as usize as u64;
/// args.flags = nc::CLONE_PIDFD as u64 | nc::CLONE_PARENT_SETTID as u64;
/// let pid = unsafe { nc::clone3(&mut args, core::mem::size_of::<nc::clone_args_t>()) };
/// let pid = unsafe { nc::clone3(&mut args) };
/// assert!(pid.is_ok());
/// ```
pub unsafe fn clone3(cl_args: &mut clone_args_t, size: size_t) -> Result<pid_t, Errno> {
pub unsafe fn clone3(cl_args: &mut clone_args_t) -> Result<pid_t, Errno> {
let cl_args_ptr = cl_args as *mut clone_args_t as usize;
let size = core::mem::size_of::<clone_args_t>();
syscall2(SYS_CLONE3, cl_args_ptr, size).map(|ret| ret as pid_t)
}

Expand Down
10 changes: 5 additions & 5 deletions src/platform/linux-riscv64/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub unsafe fn acct<P: AsRef<Path>>(filename: P) -> Result<(), Errno> {
pub unsafe fn add_key<P: AsRef<Path>>(
type_: P,
description: P,
payload: usize,
payload: uintptr_t,
plen: size_t,
dest_keyring: key_serial_t,
) -> Result<key_serial_t, Errno> {
Expand Down Expand Up @@ -321,13 +321,12 @@ pub unsafe fn clock_settime(which_clock: clockid_t, tp: &timespec_t) -> Result<(

/// Create a child process.
pub unsafe fn clone(
clone_flags: i32,
clone_flags: usize,
newsp: usize,
parent_tid: &mut i32,
child_tid: &mut i32,
tls: usize,
) -> Result<pid_t, Errno> {
let clone_flags = clone_flags as usize;
let parent_tid_ptr = parent_tid as *mut i32 as usize;
let child_tid_ptr = child_tid as *mut i32 as usize;
syscall5(
Expand All @@ -351,11 +350,12 @@ pub unsafe fn clone(
/// args.exit_signal = nc::SIGCHLD as u64;
/// args.pidfd = &mut pid_fd as *mut i32 as usize as u64;
/// args.flags = nc::CLONE_PIDFD as u64 | nc::CLONE_PARENT_SETTID as u64;
/// let pid = unsafe { nc::clone3(&mut args, core::mem::size_of::<nc::clone_args_t>()) };
/// let pid = unsafe { nc::clone3(&mut args) };
/// assert!(pid.is_ok());
/// ```
pub unsafe fn clone3(cl_args: &mut clone_args_t, size: size_t) -> Result<pid_t, Errno> {
pub unsafe fn clone3(cl_args: &mut clone_args_t) -> Result<pid_t, Errno> {
let cl_args_ptr = cl_args as *mut clone_args_t as usize;
let size = core::mem::size_of::<clone_args_t>();
syscall2(SYS_CLONE3, cl_args_ptr, size).map(|ret| ret as pid_t)
}

Expand Down
10 changes: 5 additions & 5 deletions src/platform/linux-s390x/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub unsafe fn acct<P: AsRef<Path>>(filename: P) -> Result<(), Errno> {
pub unsafe fn add_key<P: AsRef<Path>>(
type_: P,
description: P,
payload: usize,
payload: uintptr_t,
plen: size_t,
dest_keyring: key_serial_t,
) -> Result<key_serial_t, Errno> {
Expand Down Expand Up @@ -424,13 +424,12 @@ pub unsafe fn clock_settime(which_clock: clockid_t, tp: &timespec_t) -> Result<(

/// Create a child process.
pub unsafe fn clone(
clone_flags: i32,
clone_flags: usize,
newsp: usize,
parent_tid: &mut i32,
child_tid: &mut i32,
tls: usize,
) -> Result<pid_t, Errno> {
let clone_flags = clone_flags as usize;
let parent_tid_ptr = parent_tid as *mut i32 as usize;
let child_tid_ptr = child_tid as *mut i32 as usize;
syscall5(
Expand All @@ -454,11 +453,12 @@ pub unsafe fn clone(
/// args.exit_signal = nc::SIGCHLD as u64;
/// args.pidfd = &mut pid_fd as *mut i32 as usize as u64;
/// args.flags = nc::CLONE_PIDFD as u64 | nc::CLONE_PARENT_SETTID as u64;
/// let pid = unsafe { nc::clone3(&mut args, core::mem::size_of::<nc::clone_args_t>()) };
/// let pid = unsafe { nc::clone3(&mut args) };
/// assert!(pid.is_ok());
/// ```
pub unsafe fn clone3(cl_args: &mut clone_args_t, size: size_t) -> Result<pid_t, Errno> {
pub unsafe fn clone3(cl_args: &mut clone_args_t) -> Result<pid_t, Errno> {
let cl_args_ptr = cl_args as *mut clone_args_t as usize;
let size = core::mem::size_of::<clone_args_t>();
syscall2(SYS_CLONE3, cl_args_ptr, size).map(|ret| ret as pid_t)
}

Expand Down
Loading

0 comments on commit bc3e14f

Please sign in to comment.