Skip to content

Commit 53cae89

Browse files
committed
Convert CLONE_ flags to bitflags! type.
1 parent 745c791 commit 53cae89

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

src/sched.rs

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
use std::mem;
22
use std::os::unix::io::RawFd;
3-
use libc::{c_int, c_uint, c_void, c_ulong, pid_t};
3+
use libc::{self, c_int, c_void, c_ulong, pid_t};
44
use {Errno, Result};
55

6-
pub type CloneFlags = c_uint;
7-
8-
pub static CLONE_VM: CloneFlags = 0x00000100;
9-
pub static CLONE_FS: CloneFlags = 0x00000200;
10-
pub static CLONE_FILES: CloneFlags = 0x00000400;
11-
pub static CLONE_SIGHAND: CloneFlags = 0x00000800;
12-
pub static CLONE_PTRACE: CloneFlags = 0x00002000;
13-
pub static CLONE_VFORK: CloneFlags = 0x00004000;
14-
pub static CLONE_PARENT: CloneFlags = 0x00008000;
15-
pub static CLONE_THREAD: CloneFlags = 0x00010000;
16-
pub static CLONE_NEWNS: CloneFlags = 0x00020000;
17-
pub static CLONE_SYSVSEM: CloneFlags = 0x00040000;
18-
pub static CLONE_SETTLS: CloneFlags = 0x00080000;
19-
pub static CLONE_PARENT_SETTID: CloneFlags = 0x00100000;
20-
pub static CLONE_CHILD_CLEARTID: CloneFlags = 0x00200000;
21-
pub static CLONE_DETACHED: CloneFlags = 0x00400000;
22-
pub static CLONE_UNTRACED: CloneFlags = 0x00800000;
23-
pub static CLONE_CHILD_SETTID: CloneFlags = 0x01000000;
24-
pub static CLONE_NEWUTS: CloneFlags = 0x04000000;
25-
pub static CLONE_NEWIPC: CloneFlags = 0x08000000;
26-
pub static CLONE_NEWUSER: CloneFlags = 0x10000000;
27-
pub static CLONE_NEWPID: CloneFlags = 0x20000000;
28-
pub static CLONE_NEWNET: CloneFlags = 0x40000000;
29-
pub static CLONE_IO: CloneFlags = 0x80000000;
6+
// For some functions taking with a parameter of type CloneFlags,
7+
// only a subset of these flags have an effect.
8+
bitflags!{
9+
flags CloneFlags: c_int {
10+
const CLONE_VM = libc::CLONE_VM,
11+
const CLONE_FS = libc::CLONE_FS,
12+
const CLONE_FILES = libc::CLONE_FILES,
13+
const CLONE_SIGHAND = libc::CLONE_SIGHAND,
14+
const CLONE_PTRACE = libc::CLONE_PTRACE,
15+
const CLONE_VFORK = libc::CLONE_VFORK,
16+
const CLONE_PARENT = libc::CLONE_PARENT,
17+
const CLONE_THREAD = libc::CLONE_THREAD,
18+
const CLONE_NEWNS = libc::CLONE_NEWNS,
19+
const CLONE_SYSVSEM = libc::CLONE_SYSVSEM,
20+
const CLONE_SETTLS = libc::CLONE_SETTLS,
21+
const CLONE_PARENT_SETTID = libc::CLONE_PARENT_SETTID,
22+
const CLONE_CHILD_CLEARTID = libc::CLONE_CHILD_CLEARTID,
23+
const CLONE_DETACHED = libc::CLONE_DETACHED,
24+
const CLONE_UNTRACED = libc::CLONE_UNTRACED,
25+
const CLONE_CHILD_SETTID = libc::CLONE_CHILD_SETTID,
26+
// TODO: Once, we use a version containing
27+
// https://github.com/rust-lang-nursery/libc/pull/147
28+
// get rid of the casts.
29+
const CLONE_NEWUTS = libc::CLONE_NEWUTS as c_int,
30+
const CLONE_NEWIPC = libc::CLONE_NEWIPC as c_int,
31+
const CLONE_NEWUSER = libc::CLONE_NEWUSER as c_int,
32+
const CLONE_NEWPID = libc::CLONE_NEWPID as c_int,
33+
const CLONE_NEWNET = libc::CLONE_NEWNET as c_int,
34+
const CLONE_IO = libc::CLONE_IO as c_int,
35+
}
36+
}
3037

3138
// Support a maximum CPU set of 1024 nodes
3239
#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
@@ -147,17 +154,17 @@ mod ffi {
147154
pub fn clone(
148155
cb: *const CloneCb,
149156
child_stack: *mut c_void,
150-
flags: super::CloneFlags,
157+
flags: c_int,
151158
arg: *mut super::CloneCb,
152159
...) -> c_int;
153160

154161
// disassociate parts of the process execution context
155162
// doc: http://man7.org/linux/man-pages/man2/unshare.2.html
156-
pub fn unshare(flags: super::CloneFlags) -> c_int;
163+
pub fn unshare(flags: c_int) -> c_int;
157164

158165
// reassociate thread with a namespace
159166
// doc: http://man7.org/linux/man-pages/man2/setns.2.html
160-
pub fn setns(fd: c_int, nstype: super::CloneFlags) -> c_int;
167+
pub fn setns(fd: c_int, nstype: c_int) -> c_int;
161168

162169
// Set the current CPU set that a task is allowed to run on
163170
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<pid
182189

183190
let res = unsafe {
184191
let ptr = stack.as_mut_ptr().offset(stack.len() as isize);
185-
ffi::clone(mem::transmute(callback), ptr as *mut c_void, flags, &mut cb)
192+
ffi::clone(mem::transmute(callback), ptr as *mut c_void, flags.bits(), &mut cb)
186193
};
187194

188195
Errno::result(res)
189196
}
190197

191198
pub fn unshare(flags: CloneFlags) -> Result<()> {
192-
let res = unsafe { ffi::unshare(flags) };
199+
let res = unsafe { ffi::unshare(flags.bits()) };
193200

194201
Errno::result(res).map(drop)
195202
}
196203

197204
pub fn setns(fd: RawFd, nstype: CloneFlags) -> Result<()> {
198-
let res = unsafe { ffi::setns(fd, nstype) };
205+
let res = unsafe { ffi::setns(fd, nstype.bits()) };
199206

200207
Errno::result(res).map(drop)
201208
}

0 commit comments

Comments
 (0)