2009-10-11 02:22:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
#ifndef ANDROID_VOLD_VOLUME_MANAGER_H
|
|
|
|
#define ANDROID_VOLD_VOLUME_MANAGER_H
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
#include <fnmatch.h>
|
2018-09-19 00:14:18 +02:00
|
|
|
#include <pthread.h>
|
2015-03-14 00:09:20 +01:00
|
|
|
#include <stdlib.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
#include <list>
|
2015-04-21 21:14:17 +02:00
|
|
|
#include <mutex>
|
2019-12-03 16:11:39 +01:00
|
|
|
#include <set>
|
2015-04-21 21:14:17 +02:00
|
|
|
#include <string>
|
2015-06-10 18:42:01 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
#include <android-base/unique_fd.h>
|
2015-03-14 00:09:20 +01:00
|
|
|
#include <cutils/multiuser.h>
|
2018-09-19 00:14:18 +02:00
|
|
|
#include <sysutils/NetlinkEvent.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
#include <utils/List.h>
|
2015-05-15 05:33:55 +02:00
|
|
|
#include <utils/Timers.h>
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2017-09-13 19:49:44 +02:00
|
|
|
#include "android/os/IVoldListener.h"
|
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
#include "model/Disk.h"
|
|
|
|
#include "model/VolumeBase.h"
|
2009-10-11 02:22:08 +02:00
|
|
|
|
|
|
|
class VolumeManager {
|
2018-09-19 00:14:18 +02:00
|
|
|
private:
|
|
|
|
static VolumeManager* sInstance;
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2018-09-19 00:14:18 +02:00
|
|
|
bool mDebug;
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2018-09-19 00:14:18 +02:00
|
|
|
public:
|
2009-10-11 02:22:08 +02:00
|
|
|
virtual ~VolumeManager();
|
|
|
|
|
2015-04-21 21:14:17 +02:00
|
|
|
// TODO: pipe all requests through VM to avoid exposing this lock
|
|
|
|
std::mutex& getLock() { return mLock; }
|
2017-09-13 00:30:52 +02:00
|
|
|
std::mutex& getCryptLock() { return mCryptLock; }
|
2015-04-21 21:14:17 +02:00
|
|
|
|
2017-09-13 19:49:44 +02:00
|
|
|
void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
|
2018-12-18 17:42:08 +01:00
|
|
|
android::sp<android::os::IVoldListener> getListener() const { return mListener; }
|
2017-09-13 19:49:44 +02:00
|
|
|
|
2009-10-11 02:22:08 +02:00
|
|
|
int start();
|
|
|
|
int stop();
|
|
|
|
|
2018-09-19 00:14:18 +02:00
|
|
|
void handleBlockEvent(NetlinkEvent* evt);
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
class DiskSource {
|
2018-09-19 00:14:18 +02:00
|
|
|
public:
|
|
|
|
DiskSource(const std::string& sysPattern, const std::string& nickname, int flags)
|
|
|
|
: mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {}
|
2015-03-14 00:09:20 +01:00
|
|
|
|
|
|
|
bool matches(const std::string& sysPath) {
|
|
|
|
return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
|
|
|
|
}
|
|
|
|
|
2018-12-18 17:42:08 +01:00
|
|
|
const std::string& getNickname() const { return mNickname; }
|
|
|
|
int getFlags() const { return mFlags; }
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2018-09-19 00:14:18 +02:00
|
|
|
private:
|
2015-03-14 00:09:20 +01:00
|
|
|
std::string mSysPattern;
|
|
|
|
std::string mNickname;
|
|
|
|
int mFlags;
|
|
|
|
};
|
|
|
|
|
|
|
|
void addDiskSource(const std::shared_ptr<DiskSource>& diskSource);
|
|
|
|
|
|
|
|
std::shared_ptr<android::vold::Disk> findDisk(const std::string& id);
|
|
|
|
std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id);
|
|
|
|
|
2020-01-24 16:17:32 +01:00
|
|
|
template <typename Fn>
|
|
|
|
std::shared_ptr<android::vold::VolumeBase> findVolumeWithFilter(Fn fn) {
|
|
|
|
for (const auto& vol : mInternalEmulatedVolumes) {
|
|
|
|
if (fn(*vol)) {
|
|
|
|
return vol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const auto& disk : mDisks) {
|
|
|
|
for (const auto& vol : disk->getVolumes()) {
|
|
|
|
if (fn(*vol)) {
|
|
|
|
return vol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-18 17:42:08 +01:00
|
|
|
void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list) const;
|
2015-06-26 23:02:09 +02:00
|
|
|
|
2019-12-03 16:11:39 +01:00
|
|
|
const std::set<userid_t>& getStartedUsers() const { return mStartedUsers; }
|
2019-09-25 15:37:38 +02:00
|
|
|
|
2017-10-24 19:08:45 +02:00
|
|
|
int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
|
2015-06-18 23:25:08 +02:00
|
|
|
|
2015-06-10 18:42:01 +02:00
|
|
|
int onUserAdded(userid_t userId, int userSerialNumber);
|
|
|
|
int onUserRemoved(userid_t userId);
|
2019-04-29 19:46:35 +02:00
|
|
|
int onUserStarted(userid_t userId);
|
2015-06-10 18:42:01 +02:00
|
|
|
int onUserStopped(userid_t userId);
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2020-01-03 14:36:45 +01:00
|
|
|
void createPendingDisksIfNeeded();
|
2017-12-15 06:15:20 +01:00
|
|
|
int onSecureKeyguardStateChanged(bool isShowing);
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol);
|
|
|
|
|
2018-12-14 02:40:28 +01:00
|
|
|
int remountUid(uid_t uid, int32_t remountMode);
|
2020-02-28 17:30:47 +01:00
|
|
|
int remountAppStorageDirs(int uid, int pid, const std::vector<std::string>& packageNames);
|
2015-06-24 20:49:24 +02:00
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
/* Reset all internal state, typically during framework boot */
|
|
|
|
int reset();
|
|
|
|
/* Prepare for device shutdown, safely unmounting all devices */
|
|
|
|
int shutdown();
|
2015-03-31 19:35:33 +02:00
|
|
|
/* Unmount all volumes, usually for encryption */
|
|
|
|
int unmountAll();
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2017-03-26 06:49:13 +02:00
|
|
|
int updateVirtualDisk();
|
2015-04-18 02:35:20 +02:00
|
|
|
int setDebug(bool enable);
|
2010-03-12 22:32:47 +01:00
|
|
|
|
2018-09-19 00:14:18 +02:00
|
|
|
static VolumeManager* Instance();
|
2009-10-11 02:22:08 +02:00
|
|
|
|
2013-09-18 02:24:38 +02:00
|
|
|
/*
|
2019-12-24 12:57:16 +01:00
|
|
|
* Creates a directory 'path' for an application, automatically creating
|
2020-02-12 15:29:02 +01:00
|
|
|
* directories along the given path if they don't exist yet.
|
2019-12-24 12:57:16 +01:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* path = /storage/emulated/0/Android/data/com.foo/files/
|
|
|
|
*
|
2020-02-12 15:29:02 +01:00
|
|
|
* This function will first match the first part of the path with the volume
|
|
|
|
* root of any known volumes; in this case, "/storage/emulated/0" matches
|
|
|
|
* with the volume root of the emulated volume for user 0.
|
|
|
|
*
|
|
|
|
* The subseqent part of the path must start with one of the well-known
|
|
|
|
* Android/ data directories, /Android/data, /Android/obb or
|
|
|
|
* /Android/media.
|
|
|
|
*
|
|
|
|
* The final part of the path is application specific. This function will
|
|
|
|
* create all directories, including the application-specific ones, and
|
|
|
|
* set the UID of all app-specific directories below the well-known data
|
|
|
|
* directories to the 'appUid' argument. In the given example, the UID
|
2019-12-24 12:57:16 +01:00
|
|
|
* of /storage/emulated/0/Android/data/com.foo and
|
|
|
|
* /storage/emulated/0/Android/data/com.foo/files would be set to 'appUid'.
|
|
|
|
*
|
2020-02-12 15:29:02 +01:00
|
|
|
* The UID/GID of the parent directories will be set according to the
|
2019-12-24 12:57:16 +01:00
|
|
|
* requirements of the underlying filesystem and are of no concern to the
|
|
|
|
* caller.
|
|
|
|
*
|
2020-02-18 15:06:37 +01:00
|
|
|
* If fixupExistingOnly is set, we make sure to fixup any existing dirs and
|
|
|
|
* files in the passed in path, but only if that path exists; if it doesn't
|
|
|
|
* exist, this function doesn't create them.
|
|
|
|
*
|
2019-12-24 12:57:16 +01:00
|
|
|
* Validates that given paths are absolute and that they contain no relative
|
|
|
|
* "." or ".." paths or symlinks. Last path segment is treated as filename
|
|
|
|
* and ignored, unless the path ends with "/". Also ensures that path
|
|
|
|
* belongs to a volume managed by vold.
|
2013-09-18 02:24:38 +02:00
|
|
|
*/
|
2020-02-18 15:06:37 +01:00
|
|
|
int setupAppDir(const std::string& path, int32_t appUid, bool fixupExistingOnly = false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fixes up an existing application directory, as if it was created with
|
|
|
|
* setupAppDir() above. This includes fixing up the UID/GID, permissions and
|
|
|
|
* project IDs of the contained files and directories.
|
|
|
|
*/
|
|
|
|
int fixupAppDir(const std::string& path, int32_t appUid);
|
2013-09-18 02:24:38 +02:00
|
|
|
|
2017-09-11 18:32:01 +02:00
|
|
|
int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
|
2018-09-19 00:14:18 +02:00
|
|
|
std::string* outVolId);
|
2017-09-11 18:32:01 +02:00
|
|
|
int destroyObb(const std::string& volId);
|
|
|
|
|
2018-10-29 00:52:56 +01:00
|
|
|
int createStubVolume(const std::string& sourcePath, const std::string& mountPath,
|
|
|
|
const std::string& fsType, const std::string& fsUuid,
|
2020-02-04 08:07:21 +01:00
|
|
|
const std::string& fsLabel, int32_t flags, std::string* outVolId);
|
2018-10-29 00:52:56 +01:00
|
|
|
int destroyStubVolume(const std::string& volId);
|
|
|
|
|
2018-10-27 04:56:45 +02:00
|
|
|
int mountAppFuse(uid_t uid, int mountId, android::base::unique_fd* device_fd);
|
|
|
|
int unmountAppFuse(uid_t uid, int mountId);
|
|
|
|
int openAppFuseFile(uid_t uid, int mountId, int fileId, int flags);
|
2017-09-11 18:32:01 +02:00
|
|
|
|
2018-09-19 00:14:18 +02:00
|
|
|
private:
|
2009-10-11 02:22:08 +02:00
|
|
|
VolumeManager();
|
2010-06-26 05:04:04 +02:00
|
|
|
void readInitialState();
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2018-09-18 22:07:45 +02:00
|
|
|
int linkPrimary(userid_t userId);
|
2018-08-01 19:24:13 +02:00
|
|
|
|
2019-09-25 15:37:38 +02:00
|
|
|
void createEmulatedVolumesForUser(userid_t userId);
|
|
|
|
void destroyEmulatedVolumesForUser(userid_t userId);
|
|
|
|
|
2017-12-15 06:15:20 +01:00
|
|
|
void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
|
|
|
|
void handleDiskChanged(dev_t device);
|
|
|
|
void handleDiskRemoved(dev_t device);
|
|
|
|
|
2020-02-11 15:31:24 +01:00
|
|
|
bool updateFuseMountedProperty();
|
|
|
|
|
2015-04-21 21:14:17 +02:00
|
|
|
std::mutex mLock;
|
2017-09-13 00:30:52 +02:00
|
|
|
std::mutex mCryptLock;
|
2015-04-21 21:14:17 +02:00
|
|
|
|
2017-09-13 19:49:44 +02:00
|
|
|
android::sp<android::os::IVoldListener> mListener;
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
std::list<std::shared_ptr<DiskSource>> mDiskSources;
|
|
|
|
std::list<std::shared_ptr<android::vold::Disk>> mDisks;
|
2017-12-15 06:15:20 +01:00
|
|
|
std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
|
2017-09-11 18:32:01 +02:00
|
|
|
std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
|
2019-09-25 15:37:38 +02:00
|
|
|
std::list<std::shared_ptr<android::vold::VolumeBase>> mInternalEmulatedVolumes;
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2015-06-10 18:42:01 +02:00
|
|
|
std::unordered_map<userid_t, int> mAddedUsers;
|
2019-12-03 16:11:39 +01:00
|
|
|
// This needs to be a regular set because we care about the ordering here;
|
|
|
|
// user 0 should always go first, because it is responsible for sdcardfs.
|
|
|
|
std::set<userid_t> mStartedUsers;
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2017-03-26 06:49:13 +02:00
|
|
|
std::string mVirtualDiskPath;
|
|
|
|
std::shared_ptr<android::vold::Disk> mVirtualDisk;
|
2015-03-14 00:09:20 +01:00
|
|
|
std::shared_ptr<android::vold::VolumeBase> mPrimary;
|
2017-09-11 18:32:01 +02:00
|
|
|
|
|
|
|
int mNextObbId;
|
2020-02-04 08:07:21 +01:00
|
|
|
int mNextStubId;
|
2017-12-15 06:15:20 +01:00
|
|
|
bool mSecureKeyguardShowing;
|
2009-10-11 02:22:08 +02:00
|
|
|
};
|
2011-05-19 02:20:07 +02:00
|
|
|
|
2009-10-11 02:22:08 +02:00
|
|
|
#endif
|