1
1
use std:: mem;
2
2
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} ;
4
4
use { Errno , Result } ;
5
5
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
+ }
30
37
31
38
// Support a maximum CPU set of 1024 nodes
32
39
#[ cfg( all( target_arch = "x86_64" , target_os = "linux" ) ) ]
@@ -147,17 +154,17 @@ mod ffi {
147
154
pub fn clone (
148
155
cb : * const CloneCb ,
149
156
child_stack : * mut c_void ,
150
- flags : super :: CloneFlags ,
157
+ flags : c_int ,
151
158
arg : * mut super :: CloneCb ,
152
159
...) -> c_int ;
153
160
154
161
// disassociate parts of the process execution context
155
162
// 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 ;
157
164
158
165
// reassociate thread with a namespace
159
166
// 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 ;
161
168
162
169
// Set the current CPU set that a task is allowed to run on
163
170
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
182
189
183
190
let res = unsafe {
184
191
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)
186
193
} ;
187
194
188
195
Errno :: result ( res)
189
196
}
190
197
191
198
pub fn unshare ( flags : CloneFlags ) -> Result < ( ) > {
192
- let res = unsafe { ffi:: unshare ( flags) } ;
199
+ let res = unsafe { ffi:: unshare ( flags. bits ( ) ) } ;
193
200
194
201
Errno :: result ( res) . map ( drop)
195
202
}
196
203
197
204
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 ( ) ) } ;
199
206
200
207
Errno :: result ( res) . map ( drop)
201
208
}
0 commit comments