From 53cae89c6892b1f125ef69a769ed8147fcba4d92 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Sat, 23 Jan 2016 14:14:51 +0100 Subject: Convert CLONE_ flags to bitflags! type. --- src/sched.rs | 69 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 31 deletions(-) (limited to 'src/sched.rs') diff --git a/src/sched.rs b/src/sched.rs index ce34005d..e59b8b05 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -1,32 +1,39 @@ use std::mem; use std::os::unix::io::RawFd; -use libc::{c_int, c_uint, c_void, c_ulong, pid_t}; +use libc::{self, c_int, c_void, c_ulong, pid_t}; use {Errno, Result}; -pub type CloneFlags = c_uint; - -pub static CLONE_VM: CloneFlags = 0x00000100; -pub static CLONE_FS: CloneFlags = 0x00000200; -pub static CLONE_FILES: CloneFlags = 0x00000400; -pub static CLONE_SIGHAND: CloneFlags = 0x00000800; -pub static CLONE_PTRACE: CloneFlags = 0x00002000; -pub static CLONE_VFORK: CloneFlags = 0x00004000; -pub static CLONE_PARENT: CloneFlags = 0x00008000; -pub static CLONE_THREAD: CloneFlags = 0x00010000; -pub static CLONE_NEWNS: CloneFlags = 0x00020000; -pub static CLONE_SYSVSEM: CloneFlags = 0x00040000; -pub static CLONE_SETTLS: CloneFlags = 0x00080000; -pub static CLONE_PARENT_SETTID: CloneFlags = 0x00100000; -pub static CLONE_CHILD_CLEARTID: CloneFlags = 0x00200000; -pub static CLONE_DETACHED: CloneFlags = 0x00400000; -pub static CLONE_UNTRACED: CloneFlags = 0x00800000; -pub static CLONE_CHILD_SETTID: CloneFlags = 0x01000000; -pub static CLONE_NEWUTS: CloneFlags = 0x04000000; -pub static CLONE_NEWIPC: CloneFlags = 0x08000000; -pub static CLONE_NEWUSER: CloneFlags = 0x10000000; -pub static CLONE_NEWPID: CloneFlags = 0x20000000; -pub static CLONE_NEWNET: CloneFlags = 0x40000000; -pub static CLONE_IO: CloneFlags = 0x80000000; +// For some functions taking with a parameter of type CloneFlags, +// only a subset of these flags have an effect. +bitflags!{ + flags CloneFlags: c_int { + const CLONE_VM = libc::CLONE_VM, + const CLONE_FS = libc::CLONE_FS, + const CLONE_FILES = libc::CLONE_FILES, + const CLONE_SIGHAND = libc::CLONE_SIGHAND, + const CLONE_PTRACE = libc::CLONE_PTRACE, + const CLONE_VFORK = libc::CLONE_VFORK, + const CLONE_PARENT = libc::CLONE_PARENT, + const CLONE_THREAD = libc::CLONE_THREAD, + const CLONE_NEWNS = libc::CLONE_NEWNS, + const CLONE_SYSVSEM = libc::CLONE_SYSVSEM, + const CLONE_SETTLS = libc::CLONE_SETTLS, + const CLONE_PARENT_SETTID = libc::CLONE_PARENT_SETTID, + const CLONE_CHILD_CLEARTID = libc::CLONE_CHILD_CLEARTID, + const CLONE_DETACHED = libc::CLONE_DETACHED, + const CLONE_UNTRACED = libc::CLONE_UNTRACED, + const CLONE_CHILD_SETTID = libc::CLONE_CHILD_SETTID, + // TODO: Once, we use a version containing + // https://github.com/rust-lang-nursery/libc/pull/147 + // get rid of the casts. + const CLONE_NEWUTS = libc::CLONE_NEWUTS as c_int, + const CLONE_NEWIPC = libc::CLONE_NEWIPC as c_int, + const CLONE_NEWUSER = libc::CLONE_NEWUSER as c_int, + const CLONE_NEWPID = libc::CLONE_NEWPID as c_int, + const CLONE_NEWNET = libc::CLONE_NEWNET as c_int, + const CLONE_IO = libc::CLONE_IO as c_int, + } +} // Support a maximum CPU set of 1024 nodes #[cfg(all(target_arch = "x86_64", target_os = "linux"))] @@ -147,17 +154,17 @@ mod ffi { pub fn clone( cb: *const CloneCb, child_stack: *mut c_void, - flags: super::CloneFlags, + flags: c_int, arg: *mut super::CloneCb, ...) -> c_int; // disassociate parts of the process execution context // doc: http://man7.org/linux/man-pages/man2/unshare.2.html - pub fn unshare(flags: super::CloneFlags) -> c_int; + pub fn unshare(flags: c_int) -> c_int; // reassociate thread with a namespace // doc: http://man7.org/linux/man-pages/man2/setns.2.html - pub fn setns(fd: c_int, nstype: super::CloneFlags) -> c_int; + pub fn setns(fd: c_int, nstype: c_int) -> c_int; // Set the current CPU set that a task is allowed to run on pub fn sched_setaffinity(__pid: pid_t, __cpusetsize: size_t, __cpuset: *const CpuSet) -> c_int; @@ -182,20 +189,20 @@ pub fn clone(mut cb: CloneCb, stack: &mut [u8], flags: CloneFlags) -> Result Result<()> { - let res = unsafe { ffi::unshare(flags) }; + let res = unsafe { ffi::unshare(flags.bits()) }; Errno::result(res).map(drop) } pub fn setns(fd: RawFd, nstype: CloneFlags) -> Result<()> { - let res = unsafe { ffi::setns(fd, nstype) }; + let res = unsafe { ffi::setns(fd, nstype.bits()) }; Errno::result(res).map(drop) } -- cgit v1.2.3