Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #include "patchpanel/scoped_ns.h" |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 6 | |
| 7 | #include <fcntl.h> |
| 8 | #include <sched.h> |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 11 | |
| 12 | #include <string> |
| 13 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 14 | namespace patchpanel { |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 15 | |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 16 | ScopedNS::ScopedNS(pid_t pid, Type type) : valid_(false) { |
| 17 | std::string current_ns_path; |
| 18 | std::string target_ns_path; |
| 19 | switch (type) { |
| 20 | case Mount: |
| 21 | nstype_ = CLONE_NEWNS; |
| 22 | current_ns_path = "/proc/self/ns/mnt"; |
| 23 | target_ns_path = "/proc/" + std::to_string(pid) + "/ns/mnt"; |
| 24 | break; |
| 25 | case Network: |
| 26 | nstype_ = CLONE_NEWNET; |
| 27 | current_ns_path = "/proc/self/ns/net"; |
| 28 | target_ns_path = "/proc/" + std::to_string(pid) + "/ns/net"; |
| 29 | break; |
| 30 | default: |
| 31 | LOG(ERROR) << "Unsupported namespace type " << type; |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | ns_fd_.reset(open(target_ns_path.c_str(), O_RDONLY | O_CLOEXEC)); |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 36 | if (!ns_fd_.is_valid()) { |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 37 | PLOG(ERROR) << "Could not open namespace " << target_ns_path; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 38 | return; |
| 39 | } |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 40 | self_fd_.reset(open(current_ns_path.c_str(), O_RDONLY | O_CLOEXEC)); |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 41 | if (!self_fd_.is_valid()) { |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 42 | PLOG(ERROR) << "Could not open host namespace " << current_ns_path; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 43 | return; |
| 44 | } |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 45 | if (setns(ns_fd_.get(), nstype_) != 0) { |
| 46 | PLOG(ERROR) << "Could not enter namespace " << target_ns_path; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 47 | return; |
| 48 | } |
| 49 | valid_ = true; |
| 50 | } |
| 51 | |
| 52 | ScopedNS::~ScopedNS() { |
| 53 | if (valid_) { |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 54 | if (setns(self_fd_.get(), nstype_) != 0) |
| 55 | PLOG(FATAL) << "Could not re-enter host namespace type " << nstype_; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 59 | } // namespace patchpanel |