| 163 | |
| 164 | |
| 165 | Try<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 | |
| 217 | Try<Nothing> setns(pid_t pid, const string& ns, bool checkMultithreaded) |
no test coverage detected