blob: 01a81d19b202b803bb2f2a759c30394bcf2ba34f [file] [log] [blame]
San Mehatf1b736b2009-10-10 17:22:08 -07001/*
2 * Copyright (C) 2008 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
Jeff Sharkey36801cc2015-03-13 16:09:20 -070017#ifndef ANDROID_VOLD_VOLUME_MANAGER_H
18#define ANDROID_VOLD_VOLUME_MANAGER_H
San Mehatf1b736b2009-10-10 17:22:08 -070019
Jeff Sharkey36801cc2015-03-13 16:09:20 -070020#include <fnmatch.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070021#include <pthread.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070022#include <stdlib.h>
San Mehatf1b736b2009-10-10 17:22:08 -070023
Jeff Sharkey36801cc2015-03-13 16:09:20 -070024#include <list>
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070025#include <mutex>
26#include <string>
Jeff Sharkeybd3038d2015-06-10 09:42:01 -070027#include <unordered_map>
28#include <unordered_set>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070029
Jeff Sharkey11c2d382017-09-11 10:32:01 -060030#include <android-base/unique_fd.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070031#include <cutils/multiuser.h>
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070032#include <sysutils/NetlinkEvent.h>
San Mehatf1b736b2009-10-10 17:22:08 -070033#include <utils/List.h>
Jeff Sharkey5a6bfca2015-05-14 20:33:55 -070034#include <utils/Timers.h>
San Mehatf1b736b2009-10-10 17:22:08 -070035
Jeff Sharkey814e9d32017-09-13 11:49:44 -060036#include "android/os/IVoldListener.h"
37
Jeff Sharkey11c2d382017-09-11 10:32:01 -060038#include "model/Disk.h"
39#include "model/VolumeBase.h"
San Mehatf1b736b2009-10-10 17:22:08 -070040
41class VolumeManager {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070042 private:
43 static VolumeManager* sInstance;
San Mehatf1b736b2009-10-10 17:22:08 -070044
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070045 bool mDebug;
San Mehatf1b736b2009-10-10 17:22:08 -070046
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070047 public:
San Mehatf1b736b2009-10-10 17:22:08 -070048 virtual ~VolumeManager();
49
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070050 // TODO: pipe all requests through VM to avoid exposing this lock
51 std::mutex& getLock() { return mLock; }
Jeff Sharkey83b559c2017-09-12 16:30:52 -060052 std::mutex& getCryptLock() { return mCryptLock; }
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -070053
Jeff Sharkey814e9d32017-09-13 11:49:44 -060054 void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
55 android::sp<android::os::IVoldListener> getListener() { return mListener; }
56
San Mehatf1b736b2009-10-10 17:22:08 -070057 int start();
58 int stop();
59
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070060 void handleBlockEvent(NetlinkEvent* evt);
San Mehatf1b736b2009-10-10 17:22:08 -070061
Jeff Sharkey36801cc2015-03-13 16:09:20 -070062 class DiskSource {
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070063 public:
64 DiskSource(const std::string& sysPattern, const std::string& nickname, int flags)
65 : mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {}
Jeff Sharkey36801cc2015-03-13 16:09:20 -070066
67 bool matches(const std::string& sysPath) {
68 return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
69 }
70
71 const std::string& getNickname() { return mNickname; }
72 int getFlags() { return mFlags; }
73
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070074 private:
Jeff Sharkey36801cc2015-03-13 16:09:20 -070075 std::string mSysPattern;
76 std::string mNickname;
77 int mFlags;
78 };
79
80 void addDiskSource(const std::shared_ptr<DiskSource>& diskSource);
81
82 std::shared_ptr<android::vold::Disk> findDisk(const std::string& id);
83 std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id);
84
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070085 void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list);
86
Jeff Sharkey3ce18252017-10-24 11:08:45 -060087 int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070088
Jeff Sharkeybd3038d2015-06-10 09:42:01 -070089 int onUserAdded(userid_t userId, int userSerialNumber);
90 int onUserRemoved(userid_t userId);
Sudheer Shanka69bc40f2018-10-25 11:06:55 -070091 int onUserStarted(userid_t userId, const std::vector<std::string>& packageNames,
92 const std::vector<int>& appIds, const std::vector<std::string>& sandboxIds);
Jeff Sharkeybd3038d2015-06-10 09:42:01 -070093 int onUserStopped(userid_t userId);
Jeff Sharkey36801cc2015-03-13 16:09:20 -070094
Sudheer Shankad484aa92018-07-31 10:07:34 -070095 int addAppIds(const std::vector<std::string>& packageNames, const std::vector<int32_t>& appIds);
96 int addSandboxIds(const std::vector<int32_t>& appIds,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -070097 const std::vector<std::string>& sandboxIds);
Sudheer Shankafa6a1742018-10-04 16:26:22 -070098 int prepareSandboxForApp(const std::string& packageName, appid_t appId,
99 const std::string& sandboxId, userid_t userId);
Sudheer Shanka69bc40f2018-10-25 11:06:55 -0700100 int destroySandboxForApp(const std::string& packageName, const std::string& sandboxId,
101 userid_t userId);
Sudheer Shankad484aa92018-07-31 10:07:34 -0700102
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700103 int onVolumeMounted(android::vold::VolumeBase* vol);
104 int onVolumeUnmounted(android::vold::VolumeBase* vol);
105
Jeff Sharkey401b2602017-12-14 22:15:20 -0700106 int onSecureKeyguardStateChanged(bool isShowing);
107
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700108 int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol);
109
Sudheer Shanka817b9112018-12-13 17:40:28 -0800110 int remountUid(uid_t uid, int32_t remountMode);
111 int remountUidLegacy(uid_t uid, int32_t remountMode);
Jeff Sharkey66270a22015-06-24 11:49:24 -0700112
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700113 /* Reset all internal state, typically during framework boot */
114 int reset();
115 /* Prepare for device shutdown, safely unmounting all devices */
116 int shutdown();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700117 /* Unmount all volumes, usually for encryption */
118 int unmountAll();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700119
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600120 int updateVirtualDisk();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700121 int setDebug(bool enable);
San Mehatd9a4e352010-03-12 13:32:47 -0800122
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700123 static VolumeManager* Instance();
San Mehatf1b736b2009-10-10 17:22:08 -0700124
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700125 /*
126 * Ensure that all directories along given path exist, creating parent
127 * directories as needed. Validates that given path is absolute and that
128 * it contains no relative "." or ".." paths or symlinks. Last path segment
129 * is treated as filename and ignored, unless the path ends with "/". Also
130 * ensures that path belongs to a volume managed by vold.
131 */
Jeff Sharkey3472e522017-10-06 18:02:53 -0600132 int mkdirs(const std::string& path);
Jeff Sharkey71ebe152013-09-17 17:24:38 -0700133
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600134 int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700135 std::string* outVolId);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600136 int destroyObb(const std::string& volId);
137
Risan8c9f3322018-10-29 08:52:56 +0900138 int createStubVolume(const std::string& sourcePath, const std::string& mountPath,
139 const std::string& fsType, const std::string& fsUuid,
140 const std::string& fsLabel, std::string* outVolId);
141 int destroyStubVolume(const std::string& volId);
142
Risan8f6198d2018-10-26 20:56:45 -0600143 int mountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd);
144 int unmountAppFuse(uid_t uid, int mountId);
145 int openAppFuseFile(uid_t uid, int mountId, int fileId, int flags);
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600146
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700147 private:
San Mehatf1b736b2009-10-10 17:22:08 -0700148 VolumeManager();
Mike Lockwood99635f62010-06-25 23:04:04 -0400149 void readInitialState();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700150
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700151 int linkPrimary(userid_t userId);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700152
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700153 int prepareSandboxes(userid_t userId, const std::vector<std::string>& packageNames,
154 const std::vector<std::string>& visibleVolLabels);
155 int mountPkgSpecificDirsForRunningProcs(userid_t userId,
156 const std::vector<std::string>& packageNames,
Sudheer Shanka817b9112018-12-13 17:40:28 -0800157 const std::vector<std::string>& visibleVolLabels,
158 int remountMode);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700159 int destroySandboxesForVol(android::vold::VolumeBase* vol, userid_t userId);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700160 std::string prepareSandboxSource(uid_t uid, const std::string& sandboxId,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700161 const std::string& sandboxRootDir);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700162 std::string prepareSandboxTarget(const std::string& packageName, uid_t uid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700163 const std::string& volumeLabel,
164 const std::string& mntTargetRootDir, bool isUserDependent);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700165 std::string preparePkgDataSource(const std::string& packageName, uid_t uid,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700166 const std::string& dataRootDir);
Sudheer Shanka53947a32018-08-01 10:24:13 -0700167 std::string prepareSubDirs(const std::string& pathPrefix, const std::string& subDirs,
Paul Crowleyedf7a4e2018-09-18 15:14:18 -0700168 mode_t mode, uid_t uid, gid_t gid);
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700169 bool createPkgSpecificDirRoots(const std::string& volumeRoot);
170 bool createPkgSpecificDirs(const std::string& packageName, uid_t uid,
171 const std::string& volumeRoot, const std::string& sandboxDirRoot);
172 int mountPkgSpecificDir(const std::string& mntSourceRoot, const std::string& mntTargetRoot,
173 const std::string& packageName, const char* dirName);
Sudheer Shankafa6a1742018-10-04 16:26:22 -0700174 int destroySandboxForAppOnVol(const std::string& packageName, const std::string& sandboxId,
175 userid_t userId, const std::string& volLabel);
Sudheer Shanka03992e32018-12-12 12:43:38 -0800176 int getMountModeForRunningProc(const std::vector<std::string>& packagesForUid, userid_t userId,
177 struct stat& mntWriteStat);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700178
Jeff Sharkey401b2602017-12-14 22:15:20 -0700179 void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
180 void handleDiskChanged(dev_t device);
181 void handleDiskRemoved(dev_t device);
182
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700183 std::mutex mLock;
Jeff Sharkey83b559c2017-09-12 16:30:52 -0600184 std::mutex mCryptLock;
Jeff Sharkeyc8e04c52015-04-21 12:14:17 -0700185
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600186 android::sp<android::os::IVoldListener> mListener;
187
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700188 std::list<std::shared_ptr<DiskSource>> mDiskSources;
189 std::list<std::shared_ptr<android::vold::Disk>> mDisks;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700190 std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600191 std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
Risan8c9f3322018-10-29 08:52:56 +0900192 std::list<std::shared_ptr<android::vold::VolumeBase>> mStubVolumes;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700193
Jeff Sharkeybd3038d2015-06-10 09:42:01 -0700194 std::unordered_map<userid_t, int> mAddedUsers;
195 std::unordered_set<userid_t> mStartedUsers;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700196
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600197 std::string mVirtualDiskPath;
198 std::shared_ptr<android::vold::Disk> mVirtualDisk;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700199 std::shared_ptr<android::vold::VolumeBase> mInternalEmulated;
200 std::shared_ptr<android::vold::VolumeBase> mPrimary;
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600201
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700202 std::unordered_map<std::string, appid_t> mAppIds;
203 std::unordered_map<appid_t, std::string> mSandboxIds;
204 std::unordered_map<userid_t, std::vector<std::string>> mUserPackages;
Sudheer Shanka40ab6742018-09-18 13:07:45 -0700205 std::unordered_set<std::string> mVisibleVolumeIds;
Sudheer Shanka62bbb2b2018-08-01 01:09:10 -0700206
Jeff Sharkey11c2d382017-09-11 10:32:01 -0600207 int mNextObbId;
Risan8c9f3322018-10-29 08:52:56 +0900208 int mNextStubVolumeId;
Jeff Sharkey401b2602017-12-14 22:15:20 -0700209 bool mSecureKeyguardShowing;
San Mehatf1b736b2009-10-10 17:22:08 -0700210};
Ken Sumrall29d8da82011-05-18 17:20:07 -0700211
San Mehatf1b736b2009-10-10 17:22:08 -0700212#endif