blob: f790695e562e9745dad14fbbba1d564614fac9bf [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);
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 Evans3388a032020-03-24 11:25:55 +090030} // namespace patchpanel
Garrick Evanscf036f32018-12-21 12:56:59 +090031
Garrick Evans3388a032020-03-24 11:25:55 +090032#endif // PATCHPANEL_SCOPED_NS_H_