Skip to content

clone: Allow specifying termination signal #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions src/sched.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::mem;
use std::{mem, ops};
use std::os::unix::io::RawFd;
use libc::{self, c_int, c_void, c_ulong, pid_t};
use {Errno, Result};
Expand Down Expand Up @@ -35,6 +35,39 @@ bitflags!{
}
}

#[derive(Clone, Copy, Debug)]
pub struct CloneFlagsArg {
flags: CloneFlags,
signal: Option<c_int>,
}

impl CloneFlagsArg {
fn bits(self) -> c_int {
self.flags.bits() | self.signal.unwrap_or(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To ensure we do not overwrite the other bits, it may make sense to mask self.signal before or'ing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. It may not be necessary if we use an enum for signals though.... will toy with it over the next few days.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does. Because only the low 8 signals are allowed if I understand the manpage correctly.

}
}

impl ops::BitOr<c_int> for CloneFlags {
type Output = CloneFlagsArg;
fn bitor(self, rhs: c_int) -> CloneFlagsArg {
CloneFlagsArg {
flags: self,
signal: Some(rhs),
}
}
}

impl From<CloneFlags> for CloneFlagsArg {
fn from(flags: CloneFlags) -> CloneFlagsArg {
CloneFlagsArg {
flags: flags,
signal: None,
}
}
}



// Support a maximum CPU set of 1024 nodes
#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
mod cpuset_attribs {
Expand Down Expand Up @@ -197,12 +230,32 @@ pub fn sched_setaffinity(pid: isize, cpuset: &CpuSet) -> Result<()> {
Errno::result(res).map(drop)
}

pub fn clone(mut cb: CloneCb, stack: &mut [u8], flags: CloneFlags) -> Result<pid_t> {
/// The clone(2) system call.
///
/// # Examples
///
/// ```no_run
/// use std::thread;
/// use std::time::Duration;
///
/// use nix::sched::clone;
/// use nix::sched::CLONE_NEWUTS;
/// use nix::sys::signal::SIGCHLD;
///
/// let mut stack = Box::new([0u8; 1024 * 1024]);
/// let pid = clone(Box::new(|| {
/// thread::sleep(Duration::from_secs(5));
/// 0
/// }), &mut stack[..], CLONE_NEWUTS | SIGCHLD).unwrap();
/// ```
pub fn clone<F: Into<CloneFlagsArg>>(mut cb: CloneCb, stack: &mut [u8], flags: F) -> Result<pid_t> {
extern "C" fn callback(data: *mut CloneCb) -> c_int {
let cb: &mut CloneCb = unsafe { &mut *data };
(*cb)() as c_int
}

let flags: CloneFlagsArg = flags.into();

let res = unsafe {
let ptr = stack.as_mut_ptr().offset(stack.len() as isize);
ffi::clone(mem::transmute(callback), ptr as *mut c_void, flags.bits(), &mut cb)
Expand Down