ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
| 21 | #include <binder/IBinder.h> |
| 22 | #include <binder/IInterface.h> |
| 23 | |
| 24 | #include <binder/IPCThreadState.h> |
| 25 | #include <binder/IServiceManager.h> |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 26 | #include <binder/PermissionCache.h> |
| 27 | #include <private/android_filesystem_config.h> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 28 | |
| 29 | #include <storaged.h> |
| 30 | #include <storaged_service.h> |
| 31 | |
| 32 | extern storaged_t storaged; |
| 33 | |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 34 | std::vector<struct uid_info> BpStoraged::dump_uids(const char* /*option*/) { |
| 35 | Parcel data, reply; |
| 36 | data.writeInterfaceToken(IStoraged::getInterfaceDescriptor()); |
| 37 | |
| 38 | remote()->transact(DUMPUIDS, data, &reply); |
| 39 | |
| 40 | uint32_t res_size = reply.readInt32(); |
| 41 | std::vector<struct uid_info> res(res_size); |
| 42 | for (auto&& uid : res) { |
| 43 | uid.uid = reply.readInt32(); |
| 44 | uid.name = reply.readCString(); |
| 45 | reply.read(&uid.io, sizeof(uid.io)); |
| 46 | } |
| 47 | return res; |
| 48 | } |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 49 | IMPLEMENT_META_INTERFACE(Storaged, "Storaged"); |
| 50 | |
| 51 | status_t BnStoraged::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 52 | switch(code) { |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 53 | case DUMPUIDS: { |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 54 | if (!data.checkInterface(this)) |
| 55 | return BAD_TYPE; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 56 | std::vector<struct uid_info> res = dump_uids(NULL); |
| 57 | reply->writeInt32(res.size()); |
| 58 | for (auto uid : res) { |
| 59 | reply->writeInt32(uid.uid); |
| 60 | reply->writeCString(uid.name.c_str()); |
| 61 | reply->write(&uid.io, sizeof(uid.io)); |
| 62 | } |
| 63 | return NO_ERROR; |
| 64 | } |
| 65 | break; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 66 | default: |
| 67 | return BBinder::onTransact(code, data, reply, flags); |
| 68 | } |
| 69 | } |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 70 | |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 71 | std::vector<struct uid_info> Storaged::dump_uids(const char* /* option */) { |
| 72 | std::vector<struct uid_info> uids_v; |
| 73 | std::unordered_map<uint32_t, struct uid_info> uids_m = storaged.get_uids(); |
| 74 | |
| 75 | for (const auto& it : uids_m) { |
| 76 | uids_v.push_back(it.second); |
| 77 | } |
| 78 | return uids_v; |
| 79 | } |
| 80 | |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 81 | status_t Storaged::dump(int fd, const Vector<String16>& args) { |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 82 | IPCThreadState* self = IPCThreadState::self(); |
| 83 | const int pid = self->getCallingPid(); |
| 84 | const int uid = self->getCallingUid(); |
| 85 | if ((uid != AID_SHELL) && |
| 86 | !PermissionCache::checkPermission( |
| 87 | String16("android.permission.DUMP"), pid, uid)) { |
| 88 | return PERMISSION_DENIED; |
| 89 | } |
| 90 | |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 91 | int hours = 0; |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 92 | int time_window = 0; |
| 93 | uint64_t threshold = 0; |
Jin Qian | 1275b1b | 2017-02-06 14:02:50 -0800 | [diff] [blame^] | 94 | bool force_report = false; |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 95 | for (size_t i = 0; i < args.size(); i++) { |
| 96 | const auto& arg = args[i]; |
| 97 | if (arg == String16("--hours")) { |
| 98 | if (++i >= args.size()) |
| 99 | break; |
| 100 | hours = stoi(String16::std_string(args[i])); |
| 101 | continue; |
| 102 | } |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 103 | if (arg == String16("--time_window")) { |
| 104 | if (++i >= args.size()) |
| 105 | break; |
| 106 | time_window = stoi(String16::std_string(args[i])); |
| 107 | continue; |
| 108 | } |
| 109 | if (arg == String16("--threshold")) { |
| 110 | if (++i >= args.size()) |
| 111 | break; |
| 112 | threshold = stoll(String16::std_string(args[i])); |
| 113 | continue; |
| 114 | } |
Jin Qian | 1275b1b | 2017-02-06 14:02:50 -0800 | [diff] [blame^] | 115 | if (arg == String16("--force")) { |
| 116 | force_report = true; |
| 117 | continue; |
| 118 | } |
Jin Qian | 9cdfdd3 | 2017-01-31 17:33:20 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 121 | const std::map<uint64_t, std::vector<struct uid_record>>& records = |
Jin Qian | 1275b1b | 2017-02-06 14:02:50 -0800 | [diff] [blame^] | 122 | storaged.get_uid_records(hours, threshold, force_report); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 123 | for (const auto& it : records) { |
| 124 | dprintf(fd, "%llu\n", (unsigned long long)it.first); |
| 125 | for (const auto& record : it.second) { |
| 126 | dprintf(fd, "%s %llu %llu %llu %llu %llu %llu %llu %llu\n", |
| 127 | record.name.c_str(), |
| 128 | (unsigned long long)record.ios.bytes[READ][FOREGROUND][CHARGER_OFF], |
| 129 | (unsigned long long)record.ios.bytes[WRITE][FOREGROUND][CHARGER_OFF], |
| 130 | (unsigned long long)record.ios.bytes[READ][BACKGROUND][CHARGER_OFF], |
| 131 | (unsigned long long)record.ios.bytes[WRITE][BACKGROUND][CHARGER_OFF], |
| 132 | (unsigned long long)record.ios.bytes[READ][FOREGROUND][CHARGER_ON], |
| 133 | (unsigned long long)record.ios.bytes[WRITE][FOREGROUND][CHARGER_ON], |
| 134 | (unsigned long long)record.ios.bytes[READ][BACKGROUND][CHARGER_ON], |
| 135 | (unsigned long long)record.ios.bytes[WRITE][BACKGROUND][CHARGER_ON]); |
| 136 | } |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 137 | } |
Jin Qian | e5ea17c | 2017-02-10 10:50:03 -0800 | [diff] [blame] | 138 | |
| 139 | if (time_window) { |
| 140 | storaged.update_uid_io_interval(time_window); |
| 141 | } |
| 142 | |
Jin Qian | a2e5bd1 | 2017-01-24 16:23:13 -0800 | [diff] [blame] | 143 | return NO_ERROR; |
| 144 | } |
| 145 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 146 | sp<IStoraged> get_storaged_service() { |
| 147 | sp<IServiceManager> sm = defaultServiceManager(); |
| 148 | if (sm == NULL) return NULL; |
| 149 | |
| 150 | sp<IBinder> binder = sm->getService(String16("storaged")); |
| 151 | if (binder == NULL) return NULL; |
| 152 | |
| 153 | sp<IStoraged> storaged = interface_cast<IStoraged>(binder); |
| 154 | |
| 155 | return storaged; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 156 | } |