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/stat.h> |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 10 | #include <sys/types.h> |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 11 | |
| 12 | #include <string> |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
| 15 | #include <base/memory/ptr_util.h> |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 16 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 17 | namespace patchpanel { |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 18 | |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 19 | std::unique_ptr<ScopedNS> ScopedNS::EnterMountNS(pid_t pid) { |
| 20 | int nstype = CLONE_NEWNS; |
| 21 | const std::string current_path = "/proc/self/ns/mnt"; |
| 22 | const std::string target_path = "/proc/" + std::to_string(pid) + "/ns/mnt"; |
| 23 | auto ns = base::WrapUnique(new ScopedNS(nstype, current_path, target_path)); |
| 24 | return ns->valid_ ? std::move(ns) : nullptr; |
| 25 | } |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 26 | |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 27 | std::unique_ptr<ScopedNS> ScopedNS::EnterNetworkNS(pid_t pid) { |
| 28 | int nstype = CLONE_NEWNET; |
| 29 | const std::string current_path = "/proc/self/ns/net"; |
| 30 | const std::string target_path = "/proc/" + std::to_string(pid) + "/ns/net"; |
| 31 | auto ns = base::WrapUnique(new ScopedNS(nstype, current_path, target_path)); |
| 32 | return ns->valid_ ? std::move(ns) : nullptr; |
| 33 | } |
| 34 | |
| 35 | std::unique_ptr<ScopedNS> ScopedNS::EnterNetworkNS( |
| 36 | const std::string& netns_name) { |
| 37 | int nstype = CLONE_NEWNET; |
| 38 | const std::string current_path = "/proc/self/ns/net"; |
| 39 | const std::string target_path = "/run/netns/" + netns_name; |
| 40 | auto ns = base::WrapUnique(new ScopedNS(nstype, current_path, target_path)); |
| 41 | return ns->valid_ ? std::move(ns) : nullptr; |
| 42 | } |
| 43 | |
| 44 | ScopedNS::ScopedNS(int nstype, |
| 45 | const std::string& current_ns_path, |
| 46 | const std::string& target_ns_path) |
| 47 | : nstype_(nstype), valid_(false) { |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 48 | 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] | 49 | if (!ns_fd_.is_valid()) { |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 50 | PLOG(ERROR) << "Could not open namespace " << target_ns_path; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 51 | return; |
| 52 | } |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 53 | 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] | 54 | if (!self_fd_.is_valid()) { |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 55 | PLOG(ERROR) << "Could not open host namespace " << current_ns_path; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 56 | return; |
| 57 | } |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 58 | if (setns(ns_fd_.get(), nstype_) != 0) { |
| 59 | PLOG(ERROR) << "Could not enter namespace " << target_ns_path; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | valid_ = true; |
| 63 | } |
| 64 | |
| 65 | ScopedNS::~ScopedNS() { |
| 66 | if (valid_) { |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 67 | if (setns(self_fd_.get(), nstype_) != 0) |
| 68 | PLOG(FATAL) << "Could not re-enter host namespace type " << nstype_; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 72 | } // namespace patchpanel |