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); |
| 17 | ~ScopedNS(); |
| 18 | |
| 19 | // Returns whether or not the object was able to enter the network namespace. |
| 20 | bool IsValid() const { return valid_; } |
| 21 | |
| 22 | private: |
| 23 | bool valid_; |
| 24 | base::ScopedFD ns_fd_; |
| 25 | base::ScopedFD self_fd_; |
| 26 | |
| 27 | DISALLOW_COPY_AND_ASSIGN(ScopedNS); |
| 28 | }; |
| 29 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 30 | } // namespace patchpanel |
Garrick Evans | cf036f3 | 2018-12-21 12:56:59 +0900 | [diff] [blame] | 31 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 32 | #endif // PATCHPANEL_SCOPED_NS_H_ |