am b75343ae: am 210228a0: Merge "Start tracking added users with serial numbers." into mnc-dev

* commit 'b75343ae9968326a7d93b3e8981fb3734a11b81d':
  Start tracking added users with serial numbers.
diff --git a/CommandListener.cpp b/CommandListener.cpp
index 1f89b6a..89a7a09 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -185,13 +185,21 @@
         // mkdirs [path]
         return sendGenericOkFail(cli, vm->mkdirs(argv[2]));
 
-    } else if (cmd == "start_user" && argc > 2) {
-        // start_user [user]
-        return sendGenericOkFail(cli, vm->startUser(atoi(argv[2])));
+    } else if (cmd == "user_added" && argc > 3) {
+        // user_added [user] [serial]
+        return sendGenericOkFail(cli, vm->onUserAdded(atoi(argv[2]), atoi(argv[3])));
 
-    } else if (cmd == "cleanup_user" && argc > 2) {
-        // cleanup_user [user]
-        return sendGenericOkFail(cli, vm->cleanupUser(atoi(argv[2])));
+    } else if (cmd == "user_removed" && argc > 2) {
+        // user_removed [user]
+        return sendGenericOkFail(cli, vm->onUserRemoved(atoi(argv[2])));
+
+    } else if (cmd == "user_started" && argc > 2) {
+        // user_started [user]
+        return sendGenericOkFail(cli, vm->onUserStarted(atoi(argv[2])));
+
+    } else if (cmd == "user_stopped" && argc > 2) {
+        // user_stopped [user]
+        return sendGenericOkFail(cli, vm->onUserStopped(atoi(argv[2])));
 
     } else if (cmd == "mount" && argc > 2) {
         // mount [volId] [flags] [user]
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 9306363..6f783a2 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -426,28 +426,38 @@
     return 0;
 }
 
-int VolumeManager::startUser(userid_t userId) {
+int VolumeManager::onUserAdded(userid_t userId, int userSerialNumber) {
+    mAddedUsers[userId] = userSerialNumber;
+    return 0;
+}
+
+int VolumeManager::onUserRemoved(userid_t userId) {
+    mAddedUsers.erase(userId);
+    return 0;
+}
+
+int VolumeManager::onUserStarted(userid_t userId) {
     // Note that sometimes the system will spin up processes from Zygote
     // before actually starting the user, so we're okay if Zygote
     // already created this directory.
     std::string path(StringPrintf("%s/%d", kUserMountPath, userId));
     fs_prepare_dir(path.c_str(), 0755, AID_ROOT, AID_ROOT);
 
-    mUsers.push_back(userId);
+    mStartedUsers.insert(userId);
     if (mPrimary) {
         linkPrimary(userId);
     }
     return 0;
 }
 
-int VolumeManager::cleanupUser(userid_t userId) {
-    mUsers.remove(userId);
+int VolumeManager::onUserStopped(userid_t userId) {
+    mStartedUsers.erase(userId);
     return 0;
 }
 
 int VolumeManager::setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol) {
     mPrimary = vol;
-    for (userid_t userId : mUsers) {
+    for (userid_t userId : mStartedUsers) {
         linkPrimary(userId);
     }
     return 0;
@@ -462,7 +472,8 @@
         disk->destroy();
         disk->create();
     }
-    mUsers.clear();
+    mAddedUsers.clear();
+    mStartedUsers.clear();
     return 0;
 }
 
diff --git a/VolumeManager.h b/VolumeManager.h
index f29a7fd..3207de8 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -26,6 +26,8 @@
 #include <list>
 #include <mutex>
 #include <string>
+#include <unordered_map>
+#include <unordered_set>
 
 #include <cutils/multiuser.h>
 #include <utils/List.h>
@@ -118,8 +120,10 @@
 
     nsecs_t benchmarkVolume(const std::string& id);
 
-    int startUser(userid_t userId);
-    int cleanupUser(userid_t userId);
+    int onUserAdded(userid_t userId, int userSerialNumber);
+    int onUserRemoved(userid_t userId);
+    int onUserStarted(userid_t userId);
+    int onUserStopped(userid_t userId);
 
     int setPrimary(const std::shared_ptr<android::vold::VolumeBase>& vol);
 
@@ -198,7 +202,8 @@
     std::list<std::shared_ptr<DiskSource>> mDiskSources;
     std::list<std::shared_ptr<android::vold::Disk>> mDisks;
 
-    std::list<userid_t> mUsers;
+    std::unordered_map<userid_t, int> mAddedUsers;
+    std::unordered_set<userid_t> mStartedUsers;
 
     std::shared_ptr<android::vold::VolumeBase> mInternalEmulated;
     std::shared_ptr<android::vold::VolumeBase> mPrimary;