Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "mount_namespace.h" |
| 18 | |
| 19 | #include <sys/mount.h> |
| 20 | |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Jiyong Park | 8502ed3 | 2019-02-25 22:18:37 +0900 | [diff] [blame] | 24 | #include <ApexProperties.sysprop.h> |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 25 | #include <android-base/file.h> |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/properties.h> |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 28 | #include <android-base/result.h> |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 29 | #include <android-base/unique_fd.h> |
| 30 | |
| 31 | #include "util.h" |
| 32 | |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 33 | #ifndef RECOVERY |
| 34 | #define ACTIVATE_FLATTENED_APEX 1 |
| 35 | #endif |
| 36 | |
| 37 | #ifdef ACTIVATE_FLATTENED_APEX |
| 38 | #include <apex_manifest.pb.h> |
| 39 | #include <com_android_apex.h> |
| 40 | #include <selinux/android.h> |
| 41 | #endif // ACTIVATE_FLATTENED_APEX |
| 42 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 43 | namespace android { |
| 44 | namespace init { |
| 45 | namespace { |
| 46 | |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 47 | static bool BindMount(const std::string& source, const std::string& mount_point) { |
| 48 | if (mount(source.c_str(), mount_point.c_str(), nullptr, MS_BIND | MS_REC, nullptr) == -1) { |
Martijn Coenen | c70c066 | 2020-01-10 15:42:15 +0100 | [diff] [blame] | 49 | PLOG(ERROR) << "Failed to bind mount " << source; |
| 50 | return false; |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 55 | static bool ChangeMount(const std::string& mount_point, unsigned long mountflags) { |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 56 | if (mount(nullptr, mount_point.c_str(), nullptr, mountflags, nullptr) == -1) { |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 57 | PLOG(ERROR) << "Failed to remount " << mount_point << " as " << std::hex << mountflags; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | static int OpenMountNamespace() { |
| 64 | int fd = open("/proc/self/ns/mnt", O_RDONLY | O_CLOEXEC); |
| 65 | if (fd < 0) { |
| 66 | PLOG(ERROR) << "Cannot open fd for current mount namespace"; |
| 67 | } |
| 68 | return fd; |
| 69 | } |
| 70 | |
| 71 | static std::string GetMountNamespaceId() { |
| 72 | std::string ret; |
| 73 | if (!android::base::Readlink("/proc/self/ns/mnt", &ret)) { |
| 74 | PLOG(ERROR) << "Failed to read namespace ID"; |
| 75 | return ""; |
| 76 | } |
| 77 | return ret; |
| 78 | } |
| 79 | |
Jiyong Park | 8502ed3 | 2019-02-25 22:18:37 +0900 | [diff] [blame] | 80 | static bool IsApexUpdatable() { |
| 81 | static bool updatable = android::sysprop::ApexProperties::updatable().value_or(false); |
| 82 | return updatable; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Jooyung Han | 653b063 | 2021-07-29 17:11:23 +0900 | [diff] [blame^] | 85 | static bool IsMicrodroid() { |
| 86 | static bool is_microdroid = android::base::GetProperty("ro.hardware", "") == "microdroid"; |
| 87 | return is_microdroid; |
| 88 | } |
| 89 | |
| 90 | // In case we have two sets of APEXes (non-updatable, updatable), we need two separate mount |
| 91 | // namespaces. |
| 92 | static bool NeedsTwoMountNamespaces() { |
| 93 | if (!IsApexUpdatable()) return false; |
| 94 | if (IsRecoveryMode()) return false; |
| 95 | // In microdroid, there's only one set of APEXes in built-in directories include block devices. |
| 96 | if (IsMicrodroid()) return false; |
| 97 | return true; |
| 98 | } |
| 99 | |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 100 | #ifdef ACTIVATE_FLATTENED_APEX |
| 101 | |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 102 | static Result<void> MountDir(const std::string& path, const std::string& mount_path) { |
Satoshi Niwa | 1eb300d | 2020-01-20 18:00:49 +0900 | [diff] [blame] | 103 | if (int ret = mkdir(mount_path.c_str(), 0755); ret != 0 && errno != EEXIST) { |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 104 | return ErrnoError() << "Could not create mount point " << mount_path; |
| 105 | } |
| 106 | if (mount(path.c_str(), mount_path.c_str(), nullptr, MS_BIND, nullptr) != 0) { |
| 107 | return ErrnoError() << "Could not bind mount " << path << " to " << mount_path; |
| 108 | } |
| 109 | return {}; |
| 110 | } |
| 111 | |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 112 | static Result<apex::proto::ApexManifest> GetApexManifest(const std::string& apex_dir) { |
Jiyong Park | 648ae3a | 2019-12-08 00:25:15 +0900 | [diff] [blame] | 113 | const std::string manifest_path = apex_dir + "/apex_manifest.pb"; |
| 114 | std::string content; |
| 115 | if (!android::base::ReadFileToString(manifest_path, &content)) { |
| 116 | return Error() << "Failed to read manifest file: " << manifest_path; |
| 117 | } |
| 118 | apex::proto::ApexManifest manifest; |
| 119 | if (!manifest.ParseFromString(content)) { |
| 120 | return Error() << "Can't parse manifest file: " << manifest_path; |
| 121 | } |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 122 | return manifest; |
Jiyong Park | 648ae3a | 2019-12-08 00:25:15 +0900 | [diff] [blame] | 123 | } |
| 124 | |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 125 | template <typename Fn> |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 126 | static Result<void> ActivateFlattenedApexesFrom(const std::string& from_dir, |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 127 | const std::string& to_dir, Fn on_activate) { |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 128 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(from_dir.c_str()), closedir); |
| 129 | if (!dir) { |
| 130 | return {}; |
| 131 | } |
| 132 | dirent* entry; |
Adrian DC | 9449583 | 2020-12-30 20:11:24 +0100 | [diff] [blame] | 133 | std::vector<std::string> entries; |
| 134 | |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 135 | while ((entry = readdir(dir.get())) != nullptr) { |
| 136 | if (entry->d_name[0] == '.') continue; |
| 137 | if (entry->d_type == DT_DIR) { |
Adrian DC | 9449583 | 2020-12-30 20:11:24 +0100 | [diff] [blame] | 138 | entries.push_back(entry->d_name); |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 139 | } |
| 140 | } |
Adrian DC | 9449583 | 2020-12-30 20:11:24 +0100 | [diff] [blame] | 141 | |
| 142 | std::sort(entries.begin(), entries.end()); |
| 143 | for (const auto& name : entries) { |
| 144 | const std::string apex_path = from_dir + "/" + name; |
| 145 | const auto apex_manifest = GetApexManifest(apex_path); |
| 146 | if (!apex_manifest.ok()) { |
| 147 | LOG(ERROR) << apex_path << " is not an APEX directory: " << apex_manifest.error(); |
| 148 | continue; |
| 149 | } |
| 150 | const std::string mount_path = to_dir + "/" + apex_manifest->name(); |
| 151 | if (auto result = MountDir(apex_path, mount_path); !result.ok()) { |
| 152 | return result; |
| 153 | } |
| 154 | on_activate(apex_path, *apex_manifest); |
| 155 | } |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 156 | return {}; |
| 157 | } |
| 158 | |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 159 | static bool ActivateFlattenedApexesIfPossible() { |
| 160 | if (IsRecoveryMode() || IsApexUpdatable()) { |
| 161 | return true; |
| 162 | } |
| 163 | |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 164 | const std::string kApexTop = "/apex"; |
| 165 | const std::vector<std::string> kBuiltinDirsForApexes = { |
| 166 | "/system/apex", |
| 167 | "/system_ext/apex", |
| 168 | "/product/apex", |
| 169 | "/vendor/apex", |
| 170 | }; |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 171 | |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 172 | std::vector<com::android::apex::ApexInfo> apex_infos; |
| 173 | auto on_activate = [&](const std::string& apex_path, |
| 174 | const apex::proto::ApexManifest& apex_manifest) { |
| 175 | apex_infos.emplace_back(apex_manifest.name(), apex_path, apex_path, apex_manifest.version(), |
Nikita Ioffe | 1f66299 | 2021-07-05 14:29:17 +0100 | [diff] [blame] | 176 | apex_manifest.versionname(), /*isFactory=*/true, /*isActive=*/true, |
| 177 | /* lastUpdateMillis= */ 0); |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 178 | }; |
| 179 | |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 180 | for (const auto& dir : kBuiltinDirsForApexes) { |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 181 | if (auto result = ActivateFlattenedApexesFrom(dir, kApexTop, on_activate); !result.ok()) { |
Jooyung Han | 5bb9d21 | 2019-11-25 13:50:44 +0900 | [diff] [blame] | 182 | LOG(ERROR) << result.error(); |
| 183 | return false; |
| 184 | } |
| 185 | } |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 186 | |
| 187 | std::ostringstream oss; |
| 188 | com::android::apex::ApexInfoList apex_info_list(apex_infos); |
| 189 | com::android::apex::write(oss, apex_info_list); |
| 190 | const std::string kApexInfoList = kApexTop + "/apex-info-list.xml"; |
| 191 | if (!android::base::WriteStringToFile(oss.str(), kApexInfoList)) { |
| 192 | PLOG(ERROR) << "Failed to write " << kApexInfoList; |
| 193 | return false; |
| 194 | } |
| 195 | if (selinux_android_restorecon(kApexInfoList.c_str(), 0) != 0) { |
| 196 | PLOG(ERROR) << "selinux_android_restorecon(" << kApexInfoList << ") failed"; |
| 197 | } |
| 198 | |
Jiyong Park | 648ae3a | 2019-12-08 00:25:15 +0900 | [diff] [blame] | 199 | return true; |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 200 | } |
| 201 | |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 202 | #endif // ACTIVATE_FLATTENED_APEX |
| 203 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 204 | static android::base::unique_fd bootstrap_ns_fd; |
| 205 | static android::base::unique_fd default_ns_fd; |
| 206 | |
| 207 | static std::string bootstrap_ns_id; |
| 208 | static std::string default_ns_id; |
| 209 | |
| 210 | } // namespace |
| 211 | |
| 212 | bool SetupMountNamespaces() { |
| 213 | // Set the propagation type of / as shared so that any mounting event (e.g. |
| 214 | // /data) is by default visible to all processes. When private mounting is |
| 215 | // needed for /foo/bar, then we will make /foo/bar as a mount point (by |
| 216 | // bind-mounting by to itself) and set the propagation type of the mount |
| 217 | // point to private. |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 218 | if (!ChangeMount("/", MS_SHARED | MS_REC)) return false; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 219 | |
Jiyong Park | 7b4801a | 2019-02-25 16:41:38 +0900 | [diff] [blame] | 220 | // /apex is a private mountpoint to give different sets of APEXes for |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame] | 221 | // the bootstrap and default mount namespaces. The processes running with |
| 222 | // the bootstrap namespace get APEXes from the read-only partition. |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 223 | if (!(ChangeMount("/apex", MS_PRIVATE))) return false; |
Jiyong Park | dcbaf9f | 2019-02-22 22:15:25 +0900 | [diff] [blame] | 224 | |
Kiyoung Kim | 99df54b | 2019-11-22 16:14:10 +0900 | [diff] [blame] | 225 | // /linkerconfig is a private mountpoint to give a different linker configuration |
| 226 | // based on the mount namespace. Subdirectory will be bind-mounted based on current mount |
| 227 | // namespace |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 228 | if (!(ChangeMount("/linkerconfig", MS_PRIVATE))) return false; |
Kiyoung Kim | 99df54b | 2019-11-22 16:14:10 +0900 | [diff] [blame] | 229 | |
Martijn Coenen | c70c066 | 2020-01-10 15:42:15 +0100 | [diff] [blame] | 230 | // The two mount namespaces present challenges for scoped storage, because |
| 231 | // vold, which is responsible for most of the mounting, lives in the |
| 232 | // bootstrap mount namespace, whereas most other daemons and all apps live |
| 233 | // in the default namespace. Scoped storage has a need for a |
| 234 | // /mnt/installer view that is a slave bind mount of /mnt/user - in other |
| 235 | // words, all mounts under /mnt/user should automatically show up under |
| 236 | // /mnt/installer. However, additional mounts done under /mnt/installer |
| 237 | // should not propagate back to /mnt/user. In a single mount namespace |
| 238 | // this is easy to achieve, by simply marking the /mnt/installer a slave |
| 239 | // bind mount. Unfortunately, if /mnt/installer is only created and |
| 240 | // bind mounted after the two namespaces are created below, we end up |
| 241 | // with the following situation: |
| 242 | // /mnt/user and /mnt/installer share the same peer group in both the |
| 243 | // bootstrap and default namespaces. Marking /mnt/installer slave in either |
| 244 | // namespace means that it won't propagate events to the /mnt/installer in |
| 245 | // the other namespace, which is still something we require - vold is the |
| 246 | // one doing the mounting under /mnt/installer, and those mounts should |
| 247 | // show up in the default namespace as well. |
| 248 | // |
| 249 | // The simplest solution is to do the bind mount before the two namespaces |
| 250 | // are created: the effect is that in both namespaces, /mnt/installer is a |
| 251 | // slave to the /mnt/user mount, and at the same time /mnt/installer in the |
| 252 | // bootstrap namespace shares a peer group with /mnt/installer in the |
| 253 | // default namespace. |
Ricky Wai | a4c163d | 2020-04-21 12:16:43 +0100 | [diff] [blame] | 254 | // /mnt/androidwritable is similar to /mnt/installer but serves for |
| 255 | // MOUNT_EXTERNAL_ANDROID_WRITABLE apps. |
Martijn Coenen | c70c066 | 2020-01-10 15:42:15 +0100 | [diff] [blame] | 256 | if (!mkdir_recursive("/mnt/user", 0755)) return false; |
| 257 | if (!mkdir_recursive("/mnt/installer", 0755)) return false; |
Ricky Wai | a4c163d | 2020-04-21 12:16:43 +0100 | [diff] [blame] | 258 | if (!mkdir_recursive("/mnt/androidwritable", 0755)) return false; |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 259 | if (!(BindMount("/mnt/user", "/mnt/installer"))) return false; |
| 260 | if (!(BindMount("/mnt/user", "/mnt/androidwritable"))) return false; |
Ricky Wai | a4c163d | 2020-04-21 12:16:43 +0100 | [diff] [blame] | 261 | // First, make /mnt/installer and /mnt/androidwritable a slave bind mount |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 262 | if (!(ChangeMount("/mnt/installer", MS_SLAVE))) return false; |
| 263 | if (!(ChangeMount("/mnt/androidwritable", MS_SLAVE))) return false; |
Martijn Coenen | c70c066 | 2020-01-10 15:42:15 +0100 | [diff] [blame] | 264 | // Then, make it shared again - effectively creating a new peer group, that |
| 265 | // will be inherited by new mount namespaces. |
Elliott Hughes | e79b8c2 | 2020-07-28 11:09:03 -0700 | [diff] [blame] | 266 | if (!(ChangeMount("/mnt/installer", MS_SHARED))) return false; |
| 267 | if (!(ChangeMount("/mnt/androidwritable", MS_SHARED))) return false; |
Martijn Coenen | c70c066 | 2020-01-10 15:42:15 +0100 | [diff] [blame] | 268 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 269 | bootstrap_ns_fd.reset(OpenMountNamespace()); |
| 270 | bootstrap_ns_id = GetMountNamespaceId(); |
| 271 | |
Jiyong Park | 7b4801a | 2019-02-25 16:41:38 +0900 | [diff] [blame] | 272 | // When APEXes are updatable (e.g. not-flattened), we create separate mount |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 273 | // namespaces for processes that are started before and after the APEX is |
Jiyong Park | 7b4801a | 2019-02-25 16:41:38 +0900 | [diff] [blame] | 274 | // activated by apexd. In the namespace for pre-apexd processes, small |
| 275 | // number of essential APEXes (e.g. com.android.runtime) are activated. |
| 276 | // In the namespace for post-apexd processes, all APEXes are activated. |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 277 | bool success = true; |
Jooyung Han | 653b063 | 2021-07-29 17:11:23 +0900 | [diff] [blame^] | 278 | if (NeedsTwoMountNamespaces()) { |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 279 | // Creating a new namespace by cloning, saving, and switching back to |
| 280 | // the original namespace. |
| 281 | if (unshare(CLONE_NEWNS) == -1) { |
| 282 | PLOG(ERROR) << "Cannot create mount namespace"; |
| 283 | return false; |
| 284 | } |
| 285 | default_ns_fd.reset(OpenMountNamespace()); |
| 286 | default_ns_id = GetMountNamespaceId(); |
| 287 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 288 | if (setns(bootstrap_ns_fd.get(), CLONE_NEWNS) == -1) { |
| 289 | PLOG(ERROR) << "Cannot switch back to bootstrap mount namespace"; |
| 290 | return false; |
| 291 | } |
| 292 | } else { |
| 293 | // Otherwise, default == bootstrap |
| 294 | default_ns_fd.reset(OpenMountNamespace()); |
| 295 | default_ns_id = GetMountNamespaceId(); |
| 296 | } |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 297 | #ifdef ACTIVATE_FLATTENED_APEX |
Jiyong Park | d7f7c20 | 2019-05-10 21:12:15 +0900 | [diff] [blame] | 298 | success &= ActivateFlattenedApexesIfPossible(); |
Jooyung Han | 1d951b7 | 2020-07-05 03:33:36 +0900 | [diff] [blame] | 299 | #endif |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 300 | LOG(INFO) << "SetupMountNamespaces done"; |
| 301 | return success; |
| 302 | } |
| 303 | |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 304 | Result<void> SwitchToMountNamespaceIfNeeded(MountNamespace target_mount_namespace) { |
| 305 | if (IsRecoveryMode() || !IsApexUpdatable()) { |
| 306 | // we don't have multiple namespaces in recovery mode or if apex is not updatable |
| 307 | return {}; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 308 | } |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 309 | const auto& ns_id = target_mount_namespace == NS_BOOTSTRAP ? bootstrap_ns_id : default_ns_id; |
| 310 | const auto& ns_fd = target_mount_namespace == NS_BOOTSTRAP ? bootstrap_ns_fd : default_ns_fd; |
| 311 | const auto& ns_name = target_mount_namespace == NS_BOOTSTRAP ? "bootstrap" : "default"; |
| 312 | if (ns_id != GetMountNamespaceId() && ns_fd.get() != -1) { |
| 313 | if (setns(ns_fd.get(), CLONE_NEWNS) == -1) { |
| 314 | return ErrnoError() << "Failed to switch to " << ns_name << " mount namespace."; |
Kiyoung Kim | e4d3f21 | 2019-12-16 14:31:04 +0900 | [diff] [blame] | 315 | } |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 316 | } |
Jooyung Han | 4f23d5a | 2020-06-09 13:44:17 +0900 | [diff] [blame] | 317 | return {}; |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 318 | } |
| 319 | |
Kiyoung Kim | 0cbee0d | 2021-03-02 16:45:27 +0900 | [diff] [blame] | 320 | base::Result<MountNamespace> GetCurrentMountNamespace() { |
| 321 | std::string current_namespace_id = GetMountNamespaceId(); |
| 322 | if (current_namespace_id == "") { |
| 323 | return Error() << "Failed to get current mount namespace ID"; |
| 324 | } |
| 325 | |
| 326 | if (current_namespace_id == bootstrap_ns_id) { |
| 327 | return NS_BOOTSTRAP; |
| 328 | } else if (current_namespace_id == default_ns_id) { |
| 329 | return NS_DEFAULT; |
| 330 | } |
| 331 | |
| 332 | return Error() << "Failed to find current mount namespace"; |
| 333 | } |
| 334 | |
Jiyong Park | 6866041 | 2019-01-16 23:00:59 +0900 | [diff] [blame] | 335 | } // namespace init |
| 336 | } // namespace android |