blob: ab251802096ccdcdf885f4d2a308addbe39899db [file] [log] [blame]
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include <base/files/scoped_file.h>
#include <base/macros.h>
namespace patchpanel {
// Utility class for running code blocks within a network namespace or a mount
// namespace.
class ScopedNS {
// Records the current mount (network) namespace and enters another namespace
// identified by the input argument. Will go back to the current namespace if
// the returned object goes out of scope. Returns nullptr on failure.
static std::unique_ptr<ScopedNS> EnterMountNS(pid_t pid);
static std::unique_ptr<ScopedNS> EnterNetworkNS(pid_t pid);
static std::unique_ptr<ScopedNS> EnterNetworkNS(
const std::string& netns_name);
ScopedNS(const ScopedNS&) = delete;
ScopedNS& operator=(const ScopedNS&) = delete;
ScopedNS(int nstype,
const std::string& current_ns_path,
const std::string& target_ns_path);
int nstype_;
bool valid_;
base::ScopedFD ns_fd_;
base::ScopedFD self_fd_;
} // namespace patchpanel