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 | #ifndef PATCHPANEL_SCOPED_NS_H_ |
| 6 | #define PATCHPANEL_SCOPED_NS_H_ |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 7 | |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 11 | #include <base/files/scoped_file.h> |
| 12 | #include <base/macros.h> |
| 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 | // Utility class for running code blocks within a network namespace or a mount |
| 17 | // namespace. |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 18 | class ScopedNS { |
| 19 | public: |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 20 | // Records the current mount (network) namespace and enters another namespace |
| 21 | // identified by the input argument. Will go back to the current namespace if |
| 22 | // the returned object goes out of scope. Returns nullptr on failure. |
| 23 | static std::unique_ptr<ScopedNS> EnterMountNS(pid_t pid); |
| 24 | static std::unique_ptr<ScopedNS> EnterNetworkNS(pid_t pid); |
| 25 | static std::unique_ptr<ScopedNS> EnterNetworkNS( |
| 26 | const std::string& netns_name); |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 27 | |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 28 | ScopedNS(const ScopedNS&) = delete; |
| 29 | ScopedNS& operator=(const ScopedNS&) = delete; |
| 30 | |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 31 | ~ScopedNS(); |
| 32 | |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 33 | private: |
Jie Jiang | f679931 | 2021-05-14 16:27:03 +0900 | [diff] [blame] | 34 | ScopedNS(int nstype, |
| 35 | const std::string& current_ns_path, |
| 36 | const std::string& target_ns_path); |
| 37 | |
Hugo Benichi | 0781d40 | 2021-02-22 13:43:11 +0900 | [diff] [blame] | 38 | int nstype_; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 39 | bool valid_; |
| 40 | base::ScopedFD ns_fd_; |
| 41 | base::ScopedFD self_fd_; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 42 | }; |
| 43 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 44 | } // namespace patchpanel |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 45 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 46 | #endif // PATCHPANEL_SCOPED_NS_H_ |