1 // 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 5 #ifndef LIBBRILLO_BRILLO_SCOPED_MOUNT_NAMESPACE_H_ 6 #define LIBBRILLO_BRILLO_SCOPED_MOUNT_NAMESPACE_H_ 7 8 #include <memory> 9 10 #include <base/macros.h> 11 #include <base/files/file_path.h> 12 #include <base/files/scoped_file.h> 13 14 #include <brillo/brillo_export.h> 15 16 namespace brillo { 17 18 // A class that restores a mount namespace when it goes out of scope. This can 19 // be done by entering another process' mount namespace by using 20 // CreateForPid(), or by supplying a mount namespace FD directly. 21 class BRILLO_EXPORT ScopedMountNamespace { 22 public: 23 // Enters the process identified by |pid|'s mount namespace and returns a 24 // unique_ptr that restores the original mount namespace when it goes out of 25 // scope. 26 static std::unique_ptr<ScopedMountNamespace> CreateForPid(pid_t pid); 27 28 // Enters the mount namespace identified by |path| and returns a unique_ptr 29 // that restores the original mount namespace when it goes out of scope. 30 static std::unique_ptr<ScopedMountNamespace> CreateFromPath( 31 const base::FilePath& ns_path); 32 33 explicit ScopedMountNamespace(base::ScopedFD mount_namespace_fd); 34 ~ScopedMountNamespace(); 35 36 private: 37 base::ScopedFD mount_namespace_fd_; 38 39 DISALLOW_COPY_AND_ASSIGN(ScopedMountNamespace); 40 }; 41 42 } // namespace brillo 43 44 #endif // LIBBRILLO_BRILLO_SCOPED_MOUNT_NAMESPACE_H_ 45