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