blob: a8f90ebc6d39b58d1f5f5ded3989651db282ce6f [file] [log] [blame]
Garrick Evanscf036f32018-12-21 12:56:59 +09001// 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 Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_SCOPED_NS_H_
6#define PATCHPANEL_SCOPED_NS_H_
Garrick Evanscf036f32018-12-21 12:56:59 +09007
8#include <base/files/scoped_file.h>
9#include <base/macros.h>
10
Garrick Evans3388a032020-03-24 11:25:55 +090011namespace patchpanel {
Garrick Evanscf036f32018-12-21 12:56:59 +090012
13// Utility class for running code blocks within the network namespace.
14class ScopedNS {
15 public:
16 explicit ScopedNS(pid_t pid);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090017 ScopedNS(const ScopedNS&) = delete;
18 ScopedNS& operator=(const ScopedNS&) = delete;
19
Garrick Evanscf036f32018-12-21 12:56:59 +090020 ~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 Evanscf036f32018-12-21 12:56:59 +090029};
30
Garrick Evans3388a032020-03-24 11:25:55 +090031} // namespace patchpanel
Garrick Evanscf036f32018-12-21 12:56:59 +090032
Garrick Evans3388a032020-03-24 11:25:55 +090033#endif // PATCHPANEL_SCOPED_NS_H_