From d3766fce4d8206bf76fa1d2488a1555bf22fdb9c Mon Sep 17 00:00:00 2001 From: Jorge Lucangeli Obes Date: Wed, 4 Sep 2019 11:41:39 -0400 Subject: libbrillo: ScopedMountNamespace: Add CreateFromPath. It's useful to be able to create ScopedMountNamespace objects from a path. BUG=chromium:985492 TEST=Tested with https://chromium-review.googlesource.com/c/chromiumos/platform2/+/1721070 Change-Id: I51cf338f7ce881e33d082cc57afec32fa46a9e36 Reviewed-on: https://chromium-review.googlesource.com/1784126 Tested-by: Jorge Lucangeli Obes Commit-Ready: Jorge Lucangeli Obes Legacy-Commit-Queue: Commit Bot Reviewed-by: Ben Chan Reviewed-by: Yusuke Sato Cr-Mirrored-From: https://chromium.googlesource.com/chromiumos/platform2 Cr-Mirrored-Commit: b3cba12dde210c1fc3533c3e87426772d8a5c1e2 --- brillo/scoped_mount_namespace.cc | 30 ++++++++++++++++++++++-------- brillo/scoped_mount_namespace.h | 6 ++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/brillo/scoped_mount_namespace.cc b/brillo/scoped_mount_namespace.cc index d136161..0f35e82 100644 --- a/brillo/scoped_mount_namespace.cc +++ b/brillo/scoped_mount_namespace.cc @@ -9,11 +9,16 @@ #include #include +#include #include #include #include +namespace { +constexpr char kCurrentMountNamespacePath[] = "/proc/self/ns/mnt"; +} // anonymous namespace + namespace brillo { ScopedMountNamespace::ScopedMountNamespace(base::ScopedFD mount_namespace_fd) @@ -25,26 +30,35 @@ ScopedMountNamespace::~ScopedMountNamespace() { } // static -std::unique_ptr -ScopedMountNamespace::CreateForPid(pid_t pid) { - constexpr char kCurrentMountNamespacePath[] = "/proc/self/ns/mnt"; +std::unique_ptr ScopedMountNamespace::CreateForPid( + pid_t pid) { + std::string ns_path = base::StringPrintf("/proc/%d/ns/mnt", pid); + return CreateFromPath(base::FilePath(ns_path)); +} + +// static +std::unique_ptr ScopedMountNamespace::CreateFromPath( + base::FilePath ns_path) { base::ScopedFD original_mount_namespace_fd( HANDLE_EINTR(open(kCurrentMountNamespacePath, O_RDONLY))); if (!original_mount_namespace_fd.is_valid()) { - PLOG(ERROR) << "Failed to get the original mount namespace FD"; + PLOG(ERROR) << "Failed to open original mount namespace FD at " + << kCurrentMountNamespacePath; return nullptr; } - base::ScopedFD mount_namespace_fd(HANDLE_EINTR( - open(base::StringPrintf("/proc/%d/ns/mnt", pid).c_str(), O_RDONLY))); + + base::ScopedFD mount_namespace_fd( + HANDLE_EINTR(open(ns_path.value().c_str(), O_RDONLY))); if (!mount_namespace_fd.is_valid()) { - PLOG(ERROR) << "Failed to get PID " << pid << "'s mount namespace FD"; + PLOG(ERROR) << "Failed to open mount namespace FD at " << ns_path.value(); return nullptr; } if (setns(mount_namespace_fd.get(), CLONE_NEWNS) != 0) { - PLOG(ERROR) << "Failed to enter PID " << pid << "'s mount namespace"; + PLOG(ERROR) << "Failed to enter mount namespace at " << ns_path.value(); return nullptr; } + return std::make_unique( std::move(original_mount_namespace_fd)); } diff --git a/brillo/scoped_mount_namespace.h b/brillo/scoped_mount_namespace.h index e8c91bf..f360221 100644 --- a/brillo/scoped_mount_namespace.h +++ b/brillo/scoped_mount_namespace.h @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -24,6 +25,11 @@ class BRILLO_EXPORT ScopedMountNamespace { // scope. static std::unique_ptr CreateForPid(pid_t pid); + // Enters the mount namespace identified by |path| and returns a unique_ptr + // that restores the original mount namespace when it goes out of scope. + static std::unique_ptr CreateFromPath( + base::FilePath ns_path); + explicit ScopedMountNamespace(base::ScopedFD mount_namespace_fd); ~ScopedMountNamespace(); -- cgit v1.2.3