aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJorge Lucangeli Obes <jorgelo@chromium.org>2019-09-04 11:41:39 -0400
committerchrome-bot <chrome-bot@chromium.org>2019-09-12 20:31:37 -0700
commitd3766fce4d8206bf76fa1d2488a1555bf22fdb9c (patch)
tree2a70cfca0700d2fc2a3eb1d10319d4dba1730295
parent4c3417219353917c6f8350cefd88d6c9b0763391 (diff)
downloadplatform_external_libbrillo-d3766fce4d8206bf76fa1d2488a1555bf22fdb9c.tar.gz
platform_external_libbrillo-d3766fce4d8206bf76fa1d2488a1555bf22fdb9c.tar.bz2
platform_external_libbrillo-d3766fce4d8206bf76fa1d2488a1555bf22fdb9c.zip
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 <jorgelo@chromium.org> Commit-Ready: Jorge Lucangeli Obes <jorgelo@chromium.org> Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org> Reviewed-by: Ben Chan <benchan@chromium.org> Reviewed-by: Yusuke Sato <yusukes@chromium.org> Cr-Mirrored-From: https://chromium.googlesource.com/chromiumos/platform2 Cr-Mirrored-Commit: b3cba12dde210c1fc3533c3e87426772d8a5c1e2
-rw-r--r--brillo/scoped_mount_namespace.cc30
-rw-r--r--brillo/scoped_mount_namespace.h6
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 <sys/stat.h>
#include <sys/types.h>
+#include <string>
#include <utility>
#include <base/posix/eintr_wrapper.h>
#include <base/strings/stringprintf.h>
+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>
-ScopedMountNamespace::CreateForPid(pid_t pid) {
- constexpr char kCurrentMountNamespacePath[] = "/proc/self/ns/mnt";
+std::unique_ptr<ScopedMountNamespace> 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> 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<ScopedMountNamespace>(
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 <memory>
#include <base/macros.h>
+#include <base/files/file_path.h>
#include <base/files/scoped_file.h>
#include <brillo/brillo_export.h>
@@ -24,6 +25,11 @@ class BRILLO_EXPORT ScopedMountNamespace {
// scope.
static std::unique_ptr<ScopedMountNamespace> 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<ScopedMountNamespace> CreateFromPath(
+ base::FilePath ns_path);
+
explicit ScopedMountNamespace(base::ScopedFD mount_namespace_fd);
~ScopedMountNamespace();