1 /* 2 * Copyright (C) 2018 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 "QuotaUtils.h" 18 19 #include <fstream> 20 #include <unordered_map> 21 22 #include <sys/quota.h> 23 24 #include <android-base/logging.h> 25 26 #include "utils.h" 27 28 namespace android { 29 namespace installd { 30 31 namespace { 32 33 std::recursive_mutex mMountsLock; 34 35 /* Map of all quota mounts from target to source */ 36 std::unordered_map<std::string, std::string> mQuotaReverseMounts; 37 38 std::string& FindQuotaDeviceForUuid(const std::string& uuid) { 39 std::lock_guard<std::recursive_mutex> lock(mMountsLock); 40 auto path = create_data_path(uuid.empty() ? nullptr : uuid.c_str()); 41 return mQuotaReverseMounts[path]; 42 } 43 44 } // namespace 45 46 bool InvalidateQuotaMounts() { 47 std::lock_guard<std::recursive_mutex> lock(mMountsLock); 48 49 mQuotaReverseMounts.clear(); 50 51 std::ifstream in("/proc/mounts"); 52 if (!in.is_open()) { 53 return false; 54 } 55 56 std::string source; 57 std::string target; 58 std::string ignored; 59 while (!in.eof()) { 60 std::getline(in, source, ' '); 61 std::getline(in, target, ' '); 62 std::getline(in, ignored); 63 64 if (source.compare(0, 11, "/dev/block/") == 0) { 65 struct dqblk dq; 66 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0, 67 reinterpret_cast<char*>(&dq)) == 0) { 68 LOG(DEBUG) << "Found quota mount " << source << " at " << target; 69 mQuotaReverseMounts[target] = source; 70 } 71 } 72 } 73 return true; 74 } 75 76 bool IsQuotaSupported(const std::string& uuid) { 77 return !FindQuotaDeviceForUuid(uuid).empty(); 78 } 79 80 int64_t GetOccupiedSpaceForUid(const std::string& uuid, uid_t uid) { 81 const std::string device = FindQuotaDeviceForUuid(uuid); 82 if (device == "") { 83 return -1; 84 } 85 struct dqblk dq; 86 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, 87 reinterpret_cast<char*>(&dq)) != 0) { 88 if (errno != ESRCH) { 89 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; 90 } 91 return -1; 92 } else { 93 #if MEASURE_DEBUG 94 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; 95 #endif 96 return dq.dqb_curspace; 97 } 98 } 99 100 int64_t GetOccupiedSpaceForGid(const std::string& uuid, gid_t gid) { 101 const std::string device = FindQuotaDeviceForUuid(uuid); 102 if (device == "") { 103 return -1; 104 } 105 struct dqblk dq; 106 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), gid, 107 reinterpret_cast<char*>(&dq)) != 0) { 108 if (errno != ESRCH) { 109 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << gid; 110 } 111 return -1; 112 } else { 113 #if MEASURE_DEBUG 114 LOG(DEBUG) << "quotactl() for GID " << gid << " " << dq.dqb_curspace; 115 #endif 116 return dq.dqb_curspace; 117 } 118 119 } 120 121 } // namespace installd 122 } // namespace android 123