1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#![allow(unsafe_code)]
use bitflags::bitflags;
use linux_raw_sys::general::{
CLONE_FILES, CLONE_FS, CLONE_NEWCGROUP, CLONE_NEWIPC, CLONE_NEWNET, CLONE_NEWNS, CLONE_NEWPID,
CLONE_NEWTIME, CLONE_NEWUSER, CLONE_NEWUTS, CLONE_SYSVSEM,
};
use crate::backend::c::c_int;
use crate::backend::thread::syscalls;
use crate::fd::BorrowedFd;
use crate::io;
bitflags! {
pub struct ThreadNameSpaceType: u32 {
const TIME = CLONE_NEWTIME;
const MOUNT = CLONE_NEWNS;
const CONTROL_GROUP = CLONE_NEWCGROUP;
const HOST_NAME_AND_NIS_DOMAIN_NAME = CLONE_NEWUTS;
const INTER_PROCESS_COMMUNICATION = CLONE_NEWIPC;
const USER = CLONE_NEWUSER;
const PROCESS_ID = CLONE_NEWPID;
const NETWORK = CLONE_NEWNET;
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u32)]
pub enum LinkNameSpaceType {
Time = CLONE_NEWTIME,
Mount = CLONE_NEWNS,
ControlGroup = CLONE_NEWCGROUP,
HostNameAndNISDomainName = CLONE_NEWUTS,
InterProcessCommunication = CLONE_NEWIPC,
User = CLONE_NEWUSER,
ProcessID = CLONE_NEWPID,
Network = CLONE_NEWNET,
}
bitflags! {
pub struct UnshareFlags: u32 {
const FILES = CLONE_FILES;
const FS = CLONE_FS;
const NWCGROUP = CLONE_NEWCGROUP;
const NEWIPC = CLONE_NEWIPC;
const NEWNET = CLONE_NEWNET;
const NEWNS = CLONE_NEWNS;
const NEWPID = CLONE_NEWPID;
const NEWTIME = CLONE_NEWTIME;
const NEWUSER = CLONE_NEWUSER;
const SYSVSEM = CLONE_SYSVSEM;
}
}
pub fn move_into_link_name_space(
fd: BorrowedFd,
allowed_type: Option<LinkNameSpaceType>,
) -> io::Result<()> {
let allowed_type = allowed_type.map_or(0, |t| t as c_int);
syscalls::setns(fd, allowed_type).map(|_r| ())
}
pub fn move_into_thread_name_spaces(
fd: BorrowedFd,
allowed_types: ThreadNameSpaceType,
) -> io::Result<()> {
syscalls::setns(fd, allowed_types.bits() as c_int).map(|_r| ())
}
pub fn unshare(flags: UnshareFlags) -> io::Result<()> {
syscalls::unshare(flags)
}