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 | |
| 8 | #include <base/files/scoped_file.h> |
| 9 | #include <base/macros.h> |
| 10 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 11 | namespace patchpanel { |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 12 | |
| 13 | // Utility class for running code blocks within the network namespace. |
| 14 | class ScopedNS { |
| 15 | public: |
| 16 | explicit ScopedNS(pid_t pid); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 17 | ScopedNS(const ScopedNS&) = delete; |
| 18 | ScopedNS& operator=(const ScopedNS&) = delete; |
| 19 | |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 20 | ~ScopedNS(); |
| 21 | |
| 22 | // Returns whether or not the object was able to enter the network namespace. |
| 23 | bool IsValid() const { return valid_; } |
| 24 | |
| 25 | private: |
| 26 | bool valid_; |
| 27 | base::ScopedFD ns_fd_; |
| 28 | base::ScopedFD self_fd_; |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 29 | }; |
| 30 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 31 | } // namespace patchpanel |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 32 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 33 | #endif // PATCHPANEL_SCOPED_NS_H_ |