MCPcopy Create free account
hub / github.com/apache/mesos / setns

Function setns

src/linux/ns.cpp:165–214  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

163
164
165Try<Nothing> setns(
166 const string& path,
167 const string& ns,
168 bool checkMultithreaded)
169{
170 if (checkMultithreaded) {
171 // Return error if there're multiple threads in the calling process.
172 Try<set<pid_t>> threads = proc::threads(::getpid());
173 if (threads.isError()) {
174 return Error(
175 "Failed to get the threads of the current process: " +
176 threads.error());
177 } else if (threads->size() > 1) {
178 return Error("Multiple threads exist in the current process");
179 }
180 }
181
182 if (ns::namespaces().count(ns) == 0) {
183 return Error("Namespace '" + ns + "' is not supported");
184 }
185
186 // Currently, we don't support pid namespace as its semantics is
187 // different from other namespaces (instead of re-associating the
188 // calling thread, it re-associates the *children* of the calling
189 // thread with the specified namespace).
190 if (ns == "pid") {
191 return Error("Pid namespace is not supported");
192 }
193
194 Try<int> fd = os::open(path, O_RDONLY | O_CLOEXEC);
195
196 if (fd.isError()) {
197 return Error("Failed to open '" + path + "': " + fd.error());
198 }
199
200 Try<int> nstype = ns::nstype(ns);
201 if (nstype.isError()) {
202 return Error(nstype.error());
203 }
204
205 if (::setns(fd.get(), nstype.get()) == -1) {
206 // Save the errno as it might be overwritten by 'os::close' below.
207 ErrnoError error;
208 os::close(fd.get());
209 return error;
210 }
211
212 os::close(fd.get());
213 return Nothing();
214}
215
216
217Try<Nothing> setns(pid_t pid, const string& ns, bool checkMultithreaded)

Callers 1

cloneFunction · 0.70

Calls 15

namespacesFunction · 0.85
nstypeFunction · 0.85
NothingClass · 0.85
threadsFunction · 0.70
closeFunction · 0.70
existsFunction · 0.70
stringifyFunction · 0.70
errorMethod · 0.65
ErrorFunction · 0.50
openFunction · 0.50
joinFunction · 0.50
isErrorMethod · 0.45

Tested by

no test coverage detected