1 /*
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 
17 #ifndef ANDROID_VOLD_VOLUME_MANAGER_H
18 #define ANDROID_VOLD_VOLUME_MANAGER_H
19 
20 #include <pthread.h>
21 #include <fnmatch.h>
22 #include <stdlib.h>
23 
24 #include <list>
25 #include <mutex>
26 #include <string>
27 #include <unordered_map>
28 #include <unordered_set>
29 
30 #include <android-base/unique_fd.h>
31 #include <cutils/multiuser.h>
32 #include <utils/List.h>
33 #include <utils/Timers.h>
34 #include <sysutils/NetlinkEvent.h>
35 
36 #include "android/os/IVoldListener.h"
37 
38 #include "model/Disk.h"
39 #include "model/VolumeBase.h"
40 
41 #define DEBUG_APPFUSE 0
42 
43 class VolumeManager {
44 private:
45     static VolumeManager *sInstance;
46 
47     bool                   mDebug;
48 
49 public:
50     virtual ~VolumeManager();
51 
52     // TODO: pipe all requests through VM to avoid exposing this lock
getLock()53     std::mutex& getLock() { return mLock; }
getCryptLock()54     std::mutex& getCryptLock() { return mCryptLock; }
55 
setListener(android::sp<android::os::IVoldListener> listener)56     void setListener(android::sp<android::os::IVoldListener> listener) { mListener = listener; }
getListener()57     android::sp<android::os::IVoldListener> getListener() { return mListener; }
58 
59     int start();
60     int stop();
61 
62     void handleBlockEvent(NetlinkEvent *evt);
63 
64     class DiskSource {
65     public:
DiskSource(const std::string & sysPattern,const std::string & nickname,int flags)66         DiskSource(const std::string& sysPattern, const std::string& nickname, int flags) :
67                 mSysPattern(sysPattern), mNickname(nickname), mFlags(flags) {
68         }
69 
matches(const std::string & sysPath)70         bool matches(const std::string& sysPath) {
71             return !fnmatch(mSysPattern.c_str(), sysPath.c_str(), 0);
72         }
73 
getNickname()74         const std::string& getNickname() { return mNickname; }
getFlags()75         int getFlags() { return mFlags; }
76 
77     private:
78         std::string mSysPattern;
79         std::string mNickname;
80         int mFlags;
81     };
82 
83     void addDiskSource(const std::shared_ptr<DiskSource>& diskSource);
84 
85     std::shared_ptr<android::vold::Disk> findDisk(const std::string& id);
86     std::shared_ptr<android::vold::VolumeBase> findVolume(const std::string& id);
87 
88     void listVolumes(android::vold::VolumeBase::Type type, std::list<std::string>& list);
89 
90     int forgetPartition(const std::string& partGuid, const std::string& fsUuid);
91 
92     int onUserAdded(userid_t userId, int userSerialNumber);
93     int onUserRemoved(userid_t userId);
94     int onUserStarted(userid_t userId);
95     int onUserStopped(userid_t userId);
96 
97     int onSecureKeyguardStateChanged(bool isShowing);
98 
99     int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol);
100 
101     int remountUid(uid_t uid, const std::string& mode);
102 
103     /* Reset all internal state, typically during framework boot */
104     int reset();
105     /* Prepare for device shutdown, safely unmounting all devices */
106     int shutdown();
107     /* Unmount all volumes, usually for encryption */
108     int unmountAll();
109 
110     int updateVirtualDisk();
111     int setDebug(bool enable);
112 
113     static VolumeManager *Instance();
114 
115     /*
116      * Ensure that all directories along given path exist, creating parent
117      * directories as needed.  Validates that given path is absolute and that
118      * it contains no relative "." or ".." paths or symlinks.  Last path segment
119      * is treated as filename and ignored, unless the path ends with "/".  Also
120      * ensures that path belongs to a volume managed by vold.
121      */
122     int mkdirs(const std::string& path);
123 
124     int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
125             std::string* outVolId);
126     int destroyObb(const std::string& volId);
127 
128     int mountAppFuse(uid_t uid, pid_t pid, int mountId, android::base::unique_fd* device_fd);
129     int unmountAppFuse(uid_t uid, pid_t pid, int mountId);
130 
131 private:
132     VolumeManager();
133     void readInitialState();
134 
135     int linkPrimary(userid_t userId);
136 
137     void handleDiskAdded(const std::shared_ptr<android::vold::Disk>& disk);
138     void handleDiskChanged(dev_t device);
139     void handleDiskRemoved(dev_t device);
140 
141     std::mutex mLock;
142     std::mutex mCryptLock;
143 
144     android::sp<android::os::IVoldListener> mListener;
145 
146     std::list<std::shared_ptr<DiskSource>> mDiskSources;
147     std::list<std::shared_ptr<android::vold::Disk>> mDisks;
148     std::list<std::shared_ptr<android::vold::Disk>> mPendingDisks;
149     std::list<std::shared_ptr<android::vold::VolumeBase>> mObbVolumes;
150 
151     std::unordered_map<userid_t, int> mAddedUsers;
152     std::unordered_set<userid_t> mStartedUsers;
153 
154     std::string mVirtualDiskPath;
155     std::shared_ptr<android::vold::Disk> mVirtualDisk;
156     std::shared_ptr<android::vold::VolumeBase> mInternalEmulated;
157     std::shared_ptr<android::vold::VolumeBase> mPrimary;
158 
159     int mNextObbId;
160     bool mSecureKeyguardShowing;
161 };
162 
163 #endif
164