1*1a96fba6SXin Li // Copyright 2020 The Chromium OS Authors. All rights reserved. 2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be 3*1a96fba6SXin Li // found in the LICENSE file. 4*1a96fba6SXin Li 5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_NAMESPACES_MOUNT_NAMESPACE_H_ 6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_NAMESPACES_MOUNT_NAMESPACE_H_ 7*1a96fba6SXin Li 8*1a96fba6SXin Li #include "brillo/namespaces/platform.h" 9*1a96fba6SXin Li 10*1a96fba6SXin Li #include <base/files/file_path.h> 11*1a96fba6SXin Li #include <base/macros.h> 12*1a96fba6SXin Li #include <brillo/brillo_export.h> 13*1a96fba6SXin Li 14*1a96fba6SXin Li namespace brillo { 15*1a96fba6SXin Li 16*1a96fba6SXin Li class BRILLO_EXPORT MountNamespaceInterface { 17*1a96fba6SXin Li // An interface declaring the basic functionality of a mount namespace bound 18*1a96fba6SXin Li // to a specific path. This basic functionality consists of reporting the 19*1a96fba6SXin Li // namespace path. 20*1a96fba6SXin Li public: 21*1a96fba6SXin Li virtual ~MountNamespaceInterface() = default; 22*1a96fba6SXin Li 23*1a96fba6SXin Li virtual const base::FilePath& path() const = 0; 24*1a96fba6SXin Li }; 25*1a96fba6SXin Li 26*1a96fba6SXin Li class BRILLO_EXPORT UnownedMountNamespace : public MountNamespaceInterface { 27*1a96fba6SXin Li // A class to store and retrieve the path of a persistent namespace. This 28*1a96fba6SXin Li // class doesn't create nor destroy the namespace. 29*1a96fba6SXin Li public: UnownedMountNamespace(const base::FilePath & ns_path)30*1a96fba6SXin Li explicit UnownedMountNamespace(const base::FilePath& ns_path) 31*1a96fba6SXin Li : ns_path_(ns_path) {} 32*1a96fba6SXin Li 33*1a96fba6SXin Li ~UnownedMountNamespace() override; 34*1a96fba6SXin Li path()35*1a96fba6SXin Li const base::FilePath& path() const override { return ns_path_; } 36*1a96fba6SXin Li 37*1a96fba6SXin Li private: 38*1a96fba6SXin Li base::FilePath ns_path_; 39*1a96fba6SXin Li 40*1a96fba6SXin Li DISALLOW_COPY_AND_ASSIGN(UnownedMountNamespace); 41*1a96fba6SXin Li }; 42*1a96fba6SXin Li 43*1a96fba6SXin Li class BRILLO_EXPORT MountNamespace : public MountNamespaceInterface { 44*1a96fba6SXin Li // A class to create a persistent mount namespace bound to a specific path. 45*1a96fba6SXin Li // A new mount namespace is unshared from the mount namespace of the calling 46*1a96fba6SXin Li // process when Create() is called; the namespace of the calling process 47*1a96fba6SXin Li // remains unchanged. Recurring creation on a path is not allowed. 48*1a96fba6SXin Li // 49*1a96fba6SXin Li // Given that we cannot ensure that creation always succeeds this class is not 50*1a96fba6SXin Li // fully RAII, but once the namespace is created (with Create()), it will be 51*1a96fba6SXin Li // destroyed when the object goes out of scope. 52*1a96fba6SXin Li public: 53*1a96fba6SXin Li MountNamespace(const base::FilePath& ns_path, Platform* platform); 54*1a96fba6SXin Li ~MountNamespace() override; 55*1a96fba6SXin Li 56*1a96fba6SXin Li bool Create(); 57*1a96fba6SXin Li bool Destroy(); path()58*1a96fba6SXin Li const base::FilePath& path() const override { return ns_path_; } 59*1a96fba6SXin Li 60*1a96fba6SXin Li private: 61*1a96fba6SXin Li base::FilePath ns_path_; 62*1a96fba6SXin Li Platform* platform_; 63*1a96fba6SXin Li bool exists_; 64*1a96fba6SXin Li 65*1a96fba6SXin Li DISALLOW_COPY_AND_ASSIGN(MountNamespace); 66*1a96fba6SXin Li }; 67*1a96fba6SXin Li 68*1a96fba6SXin Li } // namespace brillo 69*1a96fba6SXin Li 70*1a96fba6SXin Li #endif // LIBBRILLO_BRILLO_NAMESPACES_MOUNT_NAMESPACE_H_ 71