Martijn Coenen | e6f6b8f | 2018-08-17 09:48:33 +0200 | [diff] [blame] | 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 | |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 17 | #include "apexservice.h" |
| 18 | |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 19 | #include <dirent.h> |
Dario Freni | 6ed6332 | 2018-10-03 11:37:08 +0100 | [diff] [blame] | 20 | #include <stdio.h> |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 21 | #include <stdlib.h> |
Dario Freni | 6ed6332 | 2018-10-03 11:37:08 +0100 | [diff] [blame] | 22 | |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 23 | #include <android-base/file.h> |
| 24 | #include <android-base/logging.h> |
Andreas Gampe | 8eb187a | 2018-10-19 21:18:03 -0700 | [diff] [blame] | 25 | #include <android-base/properties.h> |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 26 | #include <android-base/stringprintf.h> |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 27 | #include <android-base/strings.h> |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 28 | #include <binder/IPCThreadState.h> |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 29 | #include <binder/IResultReceiver.h> |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 30 | #include <binder/IServiceManager.h> |
| 31 | #include <binder/ProcessState.h> |
Andreas Gampe | 1773914 | 2019-01-09 16:00:26 -0800 | [diff] [blame] | 32 | #include <binder/Status.h> |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 33 | #include <utils/String16.h> |
Dario Freni | 6ed6332 | 2018-10-03 11:37:08 +0100 | [diff] [blame] | 34 | |
Andreas Gampe | b99abdd | 2018-10-19 19:59:17 -0700 | [diff] [blame] | 35 | #include "apexd.h" |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 36 | #include "apexd_session.h" |
Andreas Gampe | c9a01e4 | 2018-10-22 12:50:19 -0700 | [diff] [blame] | 37 | #include "status.h" |
Jiyong Park | 9b80048 | 2018-12-03 22:16:40 +0900 | [diff] [blame] | 38 | #include "string_log.h" |
Dario Freni | 6ed6332 | 2018-10-03 11:37:08 +0100 | [diff] [blame] | 39 | |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 40 | #include <android/apex/BnApexService.h> |
| 41 | |
Martijn Coenen | e6f6b8f | 2018-08-17 09:48:33 +0200 | [diff] [blame] | 42 | namespace android { |
| 43 | namespace apex { |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 44 | namespace binder { |
| 45 | namespace { |
Martijn Coenen | e6f6b8f | 2018-08-17 09:48:33 +0200 | [diff] [blame] | 46 | |
Andreas Gampe | c9a01e4 | 2018-10-22 12:50:19 -0700 | [diff] [blame] | 47 | using BinderStatus = ::android::binder::Status; |
| 48 | |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 49 | class ApexService : public BnApexService { |
| 50 | public: |
| 51 | using BinderStatus = ::android::binder::Status; |
Martijn Coenen | 610909b | 2019-01-18 13:49:38 +0100 | [diff] [blame] | 52 | using SessionState = ::apex::proto::SessionState; |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 53 | |
| 54 | ApexService(){}; |
| 55 | |
| 56 | BinderStatus stagePackage(const std::string& packageTmpPath, |
| 57 | bool* aidl_return) override; |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 58 | BinderStatus stagePackages(const std::vector<std::string>& paths, |
| 59 | bool* aidl_return) override; |
Nikita Ioffe | 496a4a4 | 2019-03-05 16:32:51 +0000 | [diff] [blame] | 60 | BinderStatus unstagePackages(const std::vector<std::string>& paths) override; |
Dario Freni | ab5b6c4 | 2019-01-15 11:53:57 +0000 | [diff] [blame] | 61 | BinderStatus submitStagedSession(int session_id, |
| 62 | const std::vector<int>& child_session_ids, |
| 63 | ApexInfoList* apex_info_list, |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 64 | bool* aidl_return) override; |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 65 | BinderStatus markStagedSessionReady(int session_id, |
| 66 | bool* aidl_return) override; |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 67 | BinderStatus markStagedSessionSuccessful(int session_id) override; |
Martijn Coenen | 4d20642 | 2019-01-31 15:58:55 +0100 | [diff] [blame] | 68 | BinderStatus getSessions(std::vector<ApexSessionInfo>* aidl_return) override; |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 69 | BinderStatus getStagedSessionInfo( |
| 70 | int session_id, ApexSessionInfo* apex_session_info) override; |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 71 | BinderStatus activatePackage(const std::string& packagePath) override; |
| 72 | BinderStatus deactivatePackage(const std::string& packagePath) override; |
Dario Freni | dded6c1 | 2018-11-19 16:02:26 +0000 | [diff] [blame] | 73 | BinderStatus getActivePackages(std::vector<ApexInfo>* aidl_return) override; |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 74 | BinderStatus getActivePackage(const std::string& packageName, |
| 75 | ApexInfo* aidl_return) override; |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 76 | BinderStatus getAllPackages(std::vector<ApexInfo>* aidl_return) override; |
Andreas Gampe | 1773914 | 2019-01-09 16:00:26 -0800 | [diff] [blame] | 77 | BinderStatus preinstallPackages( |
| 78 | const std::vector<std::string>& paths) override; |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 79 | BinderStatus postinstallPackages( |
| 80 | const std::vector<std::string>& paths) override; |
Nikita Ioffe | 1c079c1 | 2019-02-07 17:41:27 +0000 | [diff] [blame] | 81 | BinderStatus abortActiveSession() override; |
Andreas Gampe | 6e13ff8 | 2019-04-01 15:23:50 -0700 | [diff] [blame] | 82 | BinderStatus rollbackActiveSession() override; |
| 83 | BinderStatus resumeRollbackIfNeeded() override; |
Andreas Gampe | 1773914 | 2019-01-09 16:00:26 -0800 | [diff] [blame] | 84 | |
Abhijeet Kaur | 54695d3 | 2019-01-15 13:07:20 +0000 | [diff] [blame] | 85 | status_t dump(int fd, const Vector<String16>& args) override; |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 86 | |
| 87 | // Override onTransact so we can handle shellCommand. |
| 88 | status_t onTransact(uint32_t _aidl_code, const Parcel& _aidl_data, |
Nikita Ioffe | 6bea4e5 | 2019-02-10 22:46:05 +0000 | [diff] [blame] | 89 | Parcel* _aidl_reply, uint32_t _aidl_flags) override; |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 90 | |
| 91 | status_t shellCommand(int in, int out, int err, const Vector<String16>& args); |
| 92 | }; |
Andreas Gampe | 8eb187a | 2018-10-19 21:18:03 -0700 | [diff] [blame] | 93 | |
Andreas Gampe | d6c3df8 | 2018-11-08 15:34:06 -0800 | [diff] [blame] | 94 | BinderStatus CheckDebuggable(const std::string& name) { |
Andreas Gampe | 8eb187a | 2018-10-19 21:18:03 -0700 | [diff] [blame] | 95 | if (!::android::base::GetBoolProperty("ro.debuggable", false)) { |
Andreas Gampe | d6c3df8 | 2018-11-08 15:34:06 -0800 | [diff] [blame] | 96 | std::string tmp = name + " unavailable"; |
| 97 | return BinderStatus::fromExceptionCode(BinderStatus::EX_SECURITY, |
| 98 | String8(tmp.c_str())); |
Andreas Gampe | 8eb187a | 2018-10-19 21:18:03 -0700 | [diff] [blame] | 99 | } |
| 100 | return BinderStatus::ok(); |
| 101 | } |
| 102 | |
Dario Freni | 16cbb56 | 2018-11-07 12:09:15 +0000 | [diff] [blame] | 103 | BinderStatus ApexService::stagePackage(const std::string& packageTmpPath, |
| 104 | bool* aidl_return) { |
Nikita Ioffe | 6930988 | 2019-02-13 01:31:15 +0000 | [diff] [blame] | 105 | BinderStatus debugCheck = CheckDebuggable("stagePackage"); |
| 106 | if (!debugCheck.isOk()) { |
| 107 | return debugCheck; |
| 108 | } |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 109 | std::vector<std::string> tmp; |
| 110 | tmp.push_back(packageTmpPath); |
| 111 | return stagePackages(tmp, aidl_return); |
| 112 | } |
| 113 | |
| 114 | BinderStatus ApexService::stagePackages(const std::vector<std::string>& paths, |
| 115 | bool* aidl_return) { |
Nikita Ioffe | 6930988 | 2019-02-13 01:31:15 +0000 | [diff] [blame] | 116 | BinderStatus debugCheck = CheckDebuggable("stagePackages"); |
| 117 | if (!debugCheck.isOk()) { |
| 118 | return debugCheck; |
| 119 | } |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 120 | LOG(DEBUG) << "stagePackages() received by ApexService, paths " |
| 121 | << android::base::Join(paths, ','); |
Dario Freni | 6ed6332 | 2018-10-03 11:37:08 +0100 | [diff] [blame] | 122 | |
Andreas Gampe | b99abdd | 2018-10-19 19:59:17 -0700 | [diff] [blame] | 123 | *aidl_return = false; |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 124 | Status res = ::android::apex::stagePackages(paths); |
Dario Freni | 6ed6332 | 2018-10-03 11:37:08 +0100 | [diff] [blame] | 125 | |
Andreas Gampe | b99abdd | 2018-10-19 19:59:17 -0700 | [diff] [blame] | 126 | if (res.Ok()) { |
Andreas Gampe | c9a01e4 | 2018-10-22 12:50:19 -0700 | [diff] [blame] | 127 | *aidl_return = true; |
| 128 | return BinderStatus::ok(); |
Dario Freni | 6ed6332 | 2018-10-03 11:37:08 +0100 | [diff] [blame] | 129 | } |
Dario Freni | 6ed6332 | 2018-10-03 11:37:08 +0100 | [diff] [blame] | 130 | |
Andreas Gampe | b99abdd | 2018-10-19 19:59:17 -0700 | [diff] [blame] | 131 | // TODO: Get correct binder error status. |
Andreas Gampe | a00c545 | 2018-12-10 13:38:33 -0800 | [diff] [blame] | 132 | LOG(ERROR) << "Failed to stage " << android::base::Join(paths, ',') << ": " |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 133 | << res.ErrorMessage(); |
Andreas Gampe | c9a01e4 | 2018-10-22 12:50:19 -0700 | [diff] [blame] | 134 | return BinderStatus::fromExceptionCode(BinderStatus::EX_ILLEGAL_ARGUMENT, |
| 135 | String8(res.ErrorMessage().c_str())); |
Martijn Coenen | e6f6b8f | 2018-08-17 09:48:33 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Nikita Ioffe | 496a4a4 | 2019-03-05 16:32:51 +0000 | [diff] [blame] | 138 | BinderStatus ApexService::unstagePackages( |
| 139 | const std::vector<std::string>& paths) { |
| 140 | Status res = ::android::apex::unstagePackages(paths); |
| 141 | if (res.Ok()) { |
| 142 | return BinderStatus::ok(); |
| 143 | } |
| 144 | |
| 145 | // TODO: Get correct binder error status. |
| 146 | LOG(ERROR) << "Failed to unstage " << android::base::Join(paths, ',') << ": " |
| 147 | << res.ErrorMessage(); |
| 148 | return BinderStatus::fromExceptionCode(BinderStatus::EX_ILLEGAL_ARGUMENT, |
| 149 | String8(res.ErrorMessage().c_str())); |
| 150 | } |
| 151 | |
Dario Freni | ab5b6c4 | 2019-01-15 11:53:57 +0000 | [diff] [blame] | 152 | BinderStatus ApexService::submitStagedSession( |
| 153 | int session_id, const std::vector<int>& child_session_ids, |
| 154 | ApexInfoList* apex_info_list, bool* aidl_return) { |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 155 | LOG(DEBUG) << "submitStagedSession() received by ApexService, session id " |
| 156 | << session_id; |
| 157 | |
| 158 | StatusOr<std::vector<ApexFile>> packages = |
Dario Freni | ab5b6c4 | 2019-01-15 11:53:57 +0000 | [diff] [blame] | 159 | ::android::apex::submitStagedSession(session_id, child_session_ids); |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 160 | if (!packages.Ok()) { |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 161 | *aidl_return = false; |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 162 | LOG(ERROR) << "Failed to submit session id " << session_id << ": " |
| 163 | << packages.ErrorMessage(); |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 164 | return BinderStatus::ok(); |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | for (const auto& package : *packages) { |
| 168 | ApexInfo out; |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 169 | out.packageName = package.GetManifest().name(); |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 170 | out.packagePath = package.GetPath(); |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 171 | out.versionCode = package.GetManifest().version(); |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 172 | apex_info_list->apexInfos.push_back(out); |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 173 | } |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 174 | *aidl_return = true; |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 175 | return BinderStatus::ok(); |
| 176 | } |
| 177 | |
Dario Freni | f36c962 | 2019-01-25 11:30:00 +0000 | [diff] [blame] | 178 | BinderStatus ApexService::markStagedSessionReady(int session_id, |
| 179 | bool* aidl_return) { |
| 180 | LOG(DEBUG) << "markStagedSessionReady() received by ApexService, session id " |
| 181 | << session_id; |
| 182 | Status success = ::android::apex::markStagedSessionReady(session_id); |
| 183 | if (!success.Ok()) { |
| 184 | *aidl_return = false; |
| 185 | LOG(ERROR) << "Failed to mark session id " << session_id |
| 186 | << " as ready: " << success.ErrorMessage(); |
| 187 | return BinderStatus::ok(); |
| 188 | } |
| 189 | *aidl_return = true; |
| 190 | return BinderStatus::ok(); |
| 191 | } |
| 192 | |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 193 | BinderStatus ApexService::markStagedSessionSuccessful(int session_id) { |
| 194 | LOG(DEBUG) |
| 195 | << "markStagedSessionSuccessful() received by ApexService, session id " |
| 196 | << session_id; |
| 197 | Status ret = ::android::apex::markStagedSessionSuccessful(session_id); |
| 198 | if (!ret.Ok()) { |
| 199 | LOG(ERROR) << "Failed to mark session " << session_id |
| 200 | << " as SUCCESS: " << ret.ErrorMessage(); |
| 201 | return BinderStatus::fromExceptionCode(BinderStatus::EX_ILLEGAL_ARGUMENT, |
| 202 | String8(ret.ErrorMessage().c_str())); |
| 203 | } |
| 204 | return BinderStatus::ok(); |
| 205 | } |
| 206 | |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 207 | static void ClearSessionInfo(ApexSessionInfo* session_info) { |
| 208 | session_info->sessionId = -1; |
Martijn Coenen | 4d20642 | 2019-01-31 15:58:55 +0100 | [diff] [blame] | 209 | session_info->isUnknown = false; |
| 210 | session_info->isVerified = false; |
| 211 | session_info->isStaged = false; |
| 212 | session_info->isActivated = false; |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 213 | session_info->isRollbackInProgress = false; |
Martijn Coenen | 4d20642 | 2019-01-31 15:58:55 +0100 | [diff] [blame] | 214 | session_info->isActivationFailed = false; |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 215 | session_info->isSuccess = false; |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 216 | session_info->isRolledBack = false; |
Nikita Ioffe | 6f87d54 | 2019-04-09 13:09:26 +0100 | [diff] [blame] | 217 | session_info->isRollbackFailed = false; |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void convertToApexSessionInfo(const ApexSession& session, |
| 221 | ApexSessionInfo* session_info) { |
| 222 | using SessionState = ::apex::proto::SessionState; |
| 223 | |
| 224 | ClearSessionInfo(session_info); |
| 225 | session_info->sessionId = session.GetId(); |
Martijn Coenen | 4d20642 | 2019-01-31 15:58:55 +0100 | [diff] [blame] | 226 | |
| 227 | switch (session.GetState()) { |
| 228 | case SessionState::VERIFIED: |
| 229 | session_info->isVerified = true; |
| 230 | break; |
| 231 | case SessionState::STAGED: |
| 232 | session_info->isStaged = true; |
| 233 | break; |
| 234 | case SessionState::ACTIVATED: |
| 235 | session_info->isActivated = true; |
| 236 | break; |
Martijn Coenen | 4d20642 | 2019-01-31 15:58:55 +0100 | [diff] [blame] | 237 | case SessionState::ACTIVATION_FAILED: |
| 238 | session_info->isActivationFailed = true; |
| 239 | break; |
Nikita Ioffe | a0c0ccb | 2019-02-12 22:00:41 +0000 | [diff] [blame] | 240 | case SessionState::SUCCESS: |
| 241 | session_info->isSuccess = true; |
| 242 | break; |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 243 | case SessionState::ROLLBACK_IN_PROGRESS: |
| 244 | session_info->isRollbackInProgress = true; |
| 245 | break; |
| 246 | case SessionState::ROLLED_BACK: |
| 247 | session_info->isRolledBack = true; |
| 248 | break; |
Nikita Ioffe | 6f87d54 | 2019-04-09 13:09:26 +0100 | [diff] [blame] | 249 | case SessionState::ROLLBACK_FAILED: |
| 250 | session_info->isRollbackFailed = true; |
| 251 | break; |
Martijn Coenen | 4d20642 | 2019-01-31 15:58:55 +0100 | [diff] [blame] | 252 | case SessionState::UNKNOWN: |
| 253 | default: |
| 254 | session_info->isUnknown = true; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 259 | static ApexInfo getApexInfo(const ApexFile& package) { |
| 260 | ApexInfo out; |
| 261 | out.packageName = package.GetManifest().name(); |
| 262 | out.packagePath = package.GetPath(); |
| 263 | out.versionCode = package.GetManifest().version(); |
Gavin Corkery | 43bd4b7 | 2019-04-30 10:40:12 +0100 | [diff] [blame] | 264 | out.versionName = package.GetManifest().versionname(); |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 265 | return out; |
| 266 | } |
| 267 | |
| 268 | static std::string toString(const ApexInfo& package) { |
| 269 | std::string msg = StringLog() |
| 270 | << "Package: " << package.packageName |
| 271 | << " Version: " << package.versionCode |
Gavin Corkery | 43bd4b7 | 2019-04-30 10:40:12 +0100 | [diff] [blame] | 272 | << " VersionName: " << package.versionName |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 273 | << " Path: " << package.packagePath |
| 274 | << " IsActive: " << std::boolalpha << package.isActive |
| 275 | << " IsFactory: " << std::boolalpha << package.isFactory |
| 276 | << std::endl; |
| 277 | return msg; |
| 278 | } |
| 279 | |
| 280 | static bool contains(const std::vector<ApexFile>& list, |
| 281 | const ApexFile& apexFile) { |
| 282 | return std::find_if(list.begin(), list.end(), |
| 283 | [&apexFile](const ApexFile& listFile) { |
Nikita Ioffe | ff34b18 | 2019-09-06 12:51:58 +0100 | [diff] [blame] | 284 | return apexFile.GetPath() == listFile.GetPath(); |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 285 | }) != list.end(); |
| 286 | } |
| 287 | |
Martijn Coenen | 4d20642 | 2019-01-31 15:58:55 +0100 | [diff] [blame] | 288 | BinderStatus ApexService::getSessions( |
| 289 | std::vector<ApexSessionInfo>* aidl_return) { |
| 290 | auto sessions = ApexSession::GetSessions(); |
| 291 | for (const auto& session : sessions) { |
| 292 | ApexSessionInfo sessionInfo; |
| 293 | convertToApexSessionInfo(session, &sessionInfo); |
| 294 | aidl_return->push_back(sessionInfo); |
| 295 | } |
| 296 | |
| 297 | return BinderStatus::ok(); |
| 298 | } |
| 299 | |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 300 | BinderStatus ApexService::getStagedSessionInfo( |
| 301 | int session_id, ApexSessionInfo* apex_session_info) { |
| 302 | LOG(DEBUG) << "getStagedSessionInfo() received by ApexService, session id " |
| 303 | << session_id; |
Martijn Coenen | 610909b | 2019-01-18 13:49:38 +0100 | [diff] [blame] | 304 | auto session = ApexSession::GetSession(session_id); |
| 305 | if (!session.Ok()) { |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 306 | // Unknown session. |
Nikita Ioffe | 9ae986a | 2019-02-18 22:39:27 +0000 | [diff] [blame] | 307 | ClearSessionInfo(apex_session_info); |
Martijn Coenen | 4d20642 | 2019-01-31 15:58:55 +0100 | [diff] [blame] | 308 | apex_session_info->isUnknown = true; |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 309 | return BinderStatus::ok(); |
| 310 | } |
Martijn Coenen | 4d20642 | 2019-01-31 15:58:55 +0100 | [diff] [blame] | 311 | |
| 312 | convertToApexSessionInfo(*session, apex_session_info); |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 313 | |
| 314 | return BinderStatus::ok(); |
| 315 | } |
| 316 | |
Dario Freni | af27504 | 2018-11-06 16:50:04 +0000 | [diff] [blame] | 317 | BinderStatus ApexService::activatePackage(const std::string& packagePath) { |
Andreas Gampe | d6c3df8 | 2018-11-08 15:34:06 -0800 | [diff] [blame] | 318 | BinderStatus debugCheck = CheckDebuggable("activatePackage"); |
Andreas Gampe | 8eb187a | 2018-10-19 21:18:03 -0700 | [diff] [blame] | 319 | if (!debugCheck.isOk()) { |
| 320 | return debugCheck; |
| 321 | } |
| 322 | |
Dario Freni | af27504 | 2018-11-06 16:50:04 +0000 | [diff] [blame] | 323 | LOG(DEBUG) << "activatePackage() received by ApexService, path " |
| 324 | << packagePath; |
Andreas Gampe | 8eb187a | 2018-10-19 21:18:03 -0700 | [diff] [blame] | 325 | |
Dario Freni | af27504 | 2018-11-06 16:50:04 +0000 | [diff] [blame] | 326 | Status res = ::android::apex::activatePackage(packagePath); |
Andreas Gampe | 8eb187a | 2018-10-19 21:18:03 -0700 | [diff] [blame] | 327 | |
| 328 | if (res.Ok()) { |
| 329 | return BinderStatus::ok(); |
| 330 | } |
| 331 | |
| 332 | // TODO: Get correct binder error status. |
Dario Freni | af27504 | 2018-11-06 16:50:04 +0000 | [diff] [blame] | 333 | LOG(ERROR) << "Failed to activate " << packagePath << ": " |
| 334 | << res.ErrorMessage(); |
Andreas Gampe | 8eb187a | 2018-10-19 21:18:03 -0700 | [diff] [blame] | 335 | return BinderStatus::fromExceptionCode(BinderStatus::EX_ILLEGAL_ARGUMENT, |
| 336 | String8(res.ErrorMessage().c_str())); |
| 337 | } |
| 338 | |
Andreas Gampe | d6c3df8 | 2018-11-08 15:34:06 -0800 | [diff] [blame] | 339 | BinderStatus ApexService::deactivatePackage(const std::string& packagePath) { |
| 340 | BinderStatus debugCheck = CheckDebuggable("deactivatePackage"); |
| 341 | if (!debugCheck.isOk()) { |
| 342 | return debugCheck; |
| 343 | } |
| 344 | |
| 345 | LOG(DEBUG) << "deactivatePackage() received by ApexService, path " |
| 346 | << packagePath; |
| 347 | |
| 348 | Status res = ::android::apex::deactivatePackage(packagePath); |
| 349 | |
| 350 | if (res.Ok()) { |
| 351 | return BinderStatus::ok(); |
| 352 | } |
| 353 | |
| 354 | // TODO: Get correct binder error status. |
| 355 | LOG(ERROR) << "Failed to deactivate " << packagePath << ": " |
| 356 | << res.ErrorMessage(); |
| 357 | return BinderStatus::fromExceptionCode(BinderStatus::EX_ILLEGAL_ARGUMENT, |
| 358 | String8(res.ErrorMessage().c_str())); |
| 359 | } |
| 360 | |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 361 | BinderStatus ApexService::getActivePackages( |
Dario Freni | dded6c1 | 2018-11-19 16:02:26 +0000 | [diff] [blame] | 362 | std::vector<ApexInfo>* aidl_return) { |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 363 | auto packages = ::android::apex::getActivePackages(); |
| 364 | for (const auto& package : packages) { |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 365 | ApexInfo apexInfo = getApexInfo(package); |
| 366 | apexInfo.isActive = true; |
| 367 | apexInfo.isFactory = |
| 368 | ::android::apex::isPathForBuiltinApexes(package.GetPath()); |
| 369 | aidl_return->push_back(std::move(apexInfo)); |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | return BinderStatus::ok(); |
| 373 | } |
| 374 | |
| 375 | BinderStatus ApexService::getActivePackage(const std::string& packageName, |
| 376 | ApexInfo* aidl_return) { |
| 377 | StatusOr<ApexFile> apex = ::android::apex::getActivePackage(packageName); |
| 378 | if (apex.Ok()) { |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 379 | aidl_return->packageName = apex->GetManifest().name(); |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 380 | aidl_return->packagePath = apex->GetPath(); |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 381 | aidl_return->versionCode = apex->GetManifest().version(); |
Gavin Corkery | 43bd4b7 | 2019-04-30 10:40:12 +0100 | [diff] [blame] | 382 | aidl_return->versionName = apex->GetManifest().versionname(); |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 383 | aidl_return->isActive = true; |
| 384 | aidl_return->isFactory = |
| 385 | ::android::apex::isPathForBuiltinApexes(apex->GetPath()); |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 386 | } |
Andreas Gampe | 0fa59af | 2018-11-16 11:12:11 -0800 | [diff] [blame] | 387 | |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 388 | return BinderStatus::ok(); |
| 389 | } |
| 390 | |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 391 | BinderStatus ApexService::getAllPackages(std::vector<ApexInfo>* aidl_return) { |
| 392 | auto activePackages = ::android::apex::getActivePackages(); |
| 393 | auto factoryPackages = ::android::apex::getFactoryPackages(); |
| 394 | for (const ApexFile& factoryFile : factoryPackages) { |
| 395 | ApexInfo apexInfo = getApexInfo(factoryFile); |
| 396 | apexInfo.isFactory = true; |
| 397 | if (contains(activePackages, factoryFile)) { |
| 398 | apexInfo.isActive = true; |
| 399 | } else { |
| 400 | apexInfo.isActive = false; |
| 401 | } |
| 402 | aidl_return->push_back(std::move(apexInfo)); |
| 403 | } |
| 404 | |
| 405 | for (const ApexFile& activeFile : activePackages) { |
| 406 | if (!contains(factoryPackages, activeFile)) { |
| 407 | ApexInfo apexInfo = getApexInfo(activeFile); |
| 408 | apexInfo.isFactory = false; |
| 409 | apexInfo.isActive = true; |
| 410 | aidl_return->push_back(std::move(apexInfo)); |
| 411 | } |
| 412 | } |
| 413 | return BinderStatus::ok(); |
| 414 | } |
| 415 | |
Andreas Gampe | 1773914 | 2019-01-09 16:00:26 -0800 | [diff] [blame] | 416 | BinderStatus ApexService::preinstallPackages( |
| 417 | const std::vector<std::string>& paths) { |
| 418 | BinderStatus debugCheck = CheckDebuggable("preinstallPackages"); |
| 419 | if (!debugCheck.isOk()) { |
| 420 | return debugCheck; |
| 421 | } |
| 422 | |
| 423 | Status res = ::android::apex::preinstallPackages(paths); |
| 424 | if (res.Ok()) { |
| 425 | return BinderStatus::ok(); |
| 426 | } |
| 427 | |
| 428 | // TODO: Get correct binder error status. |
| 429 | LOG(ERROR) << "Failed to preinstall packages " |
| 430 | << android::base::Join(paths, ',') << ": " << res.ErrorMessage(); |
| 431 | return BinderStatus::fromExceptionCode(BinderStatus::EX_ILLEGAL_ARGUMENT, |
| 432 | String8(res.ErrorMessage().c_str())); |
| 433 | } |
| 434 | |
Andreas Gampe | f4c7e7c | 2019-01-14 12:33:34 -0800 | [diff] [blame] | 435 | BinderStatus ApexService::postinstallPackages( |
| 436 | const std::vector<std::string>& paths) { |
| 437 | BinderStatus debugCheck = CheckDebuggable("postinstallPackages"); |
| 438 | if (!debugCheck.isOk()) { |
| 439 | return debugCheck; |
| 440 | } |
| 441 | |
| 442 | Status res = ::android::apex::postinstallPackages(paths); |
| 443 | if (res.Ok()) { |
| 444 | return BinderStatus::ok(); |
| 445 | } |
| 446 | |
| 447 | // TODO: Get correct binder error status. |
| 448 | LOG(ERROR) << "Failed to postinstall packages " |
| 449 | << android::base::Join(paths, ',') << ": " << res.ErrorMessage(); |
| 450 | return BinderStatus::fromExceptionCode(BinderStatus::EX_ILLEGAL_ARGUMENT, |
| 451 | String8(res.ErrorMessage().c_str())); |
| 452 | } |
| 453 | |
Nikita Ioffe | 1c079c1 | 2019-02-07 17:41:27 +0000 | [diff] [blame] | 454 | BinderStatus ApexService::abortActiveSession() { |
Nikita Ioffe | 463d4e8 | 2019-02-10 18:46:20 +0000 | [diff] [blame] | 455 | LOG(DEBUG) << "abortActiveSession() received by ApexService."; |
| 456 | Status res = ::android::apex::abortActiveSession(); |
| 457 | if (!res.Ok()) { |
| 458 | return BinderStatus::fromExceptionCode(BinderStatus::EX_ILLEGAL_ARGUMENT, |
| 459 | String8(res.ErrorMessage().c_str())); |
| 460 | } |
Nikita Ioffe | 1c079c1 | 2019-02-07 17:41:27 +0000 | [diff] [blame] | 461 | return BinderStatus::ok(); |
| 462 | } |
| 463 | |
Andreas Gampe | 6e13ff8 | 2019-04-01 15:23:50 -0700 | [diff] [blame] | 464 | BinderStatus ApexService::rollbackActiveSession() { |
| 465 | BinderStatus debugCheck = CheckDebuggable("rollbackActiveSession"); |
| 466 | if (!debugCheck.isOk()) { |
| 467 | return debugCheck; |
| 468 | } |
| 469 | |
| 470 | LOG(DEBUG) << "rollbackActiveSession() received by ApexService."; |
| 471 | Status res = ::android::apex::rollbackActiveSession(); |
| 472 | if (!res.Ok()) { |
| 473 | return BinderStatus::fromExceptionCode(BinderStatus::EX_ILLEGAL_ARGUMENT, |
| 474 | String8(res.ErrorMessage().c_str())); |
| 475 | } |
| 476 | return BinderStatus::ok(); |
| 477 | } |
| 478 | |
| 479 | BinderStatus ApexService::resumeRollbackIfNeeded() { |
| 480 | BinderStatus debugCheck = CheckDebuggable("resumeRollbackIfNeeded"); |
| 481 | if (!debugCheck.isOk()) { |
| 482 | return debugCheck; |
| 483 | } |
| 484 | |
| 485 | LOG(DEBUG) << "resumeRollbackIfNeeded() received by ApexService."; |
| 486 | Status res = ::android::apex::resumeRollbackIfNeeded(); |
| 487 | if (!res.Ok()) { |
| 488 | return BinderStatus::fromExceptionCode(BinderStatus::EX_ILLEGAL_ARGUMENT, |
| 489 | String8(res.ErrorMessage().c_str())); |
| 490 | } |
| 491 | return BinderStatus::ok(); |
| 492 | } |
| 493 | |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 494 | status_t ApexService::onTransact(uint32_t _aidl_code, const Parcel& _aidl_data, |
| 495 | Parcel* _aidl_reply, uint32_t _aidl_flags) { |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 496 | switch (_aidl_code) { |
| 497 | case IBinder::SHELL_COMMAND_TRANSACTION: { |
| 498 | int in = _aidl_data.readFileDescriptor(); |
| 499 | int out = _aidl_data.readFileDescriptor(); |
| 500 | int err = _aidl_data.readFileDescriptor(); |
| 501 | int argc = _aidl_data.readInt32(); |
| 502 | Vector<String16> args; |
| 503 | for (int i = 0; i < argc && _aidl_data.dataAvail() > 0; i++) { |
| 504 | args.add(_aidl_data.readString16()); |
| 505 | } |
| 506 | sp<IBinder> unusedCallback; |
| 507 | sp<IResultReceiver> resultReceiver; |
| 508 | status_t status; |
| 509 | if ((status = _aidl_data.readNullableStrongBinder(&unusedCallback)) != OK) |
| 510 | return status; |
| 511 | if ((status = _aidl_data.readNullableStrongBinder(&resultReceiver)) != OK) |
| 512 | return status; |
| 513 | status = shellCommand(in, out, err, args); |
| 514 | if (resultReceiver != nullptr) { |
| 515 | resultReceiver->send(status); |
| 516 | } |
| 517 | return OK; |
| 518 | } |
| 519 | } |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 520 | return BnApexService::onTransact(_aidl_code, _aidl_data, _aidl_reply, |
| 521 | _aidl_flags); |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 522 | } |
Abhijeet Kaur | 54695d3 | 2019-01-15 13:07:20 +0000 | [diff] [blame] | 523 | status_t ApexService::dump(int fd, const Vector<String16>& args) { |
Abhijeet Kaur | 54695d3 | 2019-01-15 13:07:20 +0000 | [diff] [blame] | 524 | std::vector<ApexInfo> list; |
| 525 | BinderStatus status = getActivePackages(&list); |
Martijn Coenen | a418d31 | 2019-01-30 12:03:38 +0100 | [diff] [blame] | 526 | dprintf(fd, "ACTIVE PACKAGES:\n"); |
| 527 | if (!status.isOk()) { |
| 528 | std::string msg = StringLog() << "Failed to retrieve packages: " |
| 529 | << status.toString8().string() << std::endl; |
| 530 | dprintf(fd, "%s", msg.c_str()); |
| 531 | return BAD_VALUE; |
| 532 | } else { |
Abhijeet Kaur | 54695d3 | 2019-01-15 13:07:20 +0000 | [diff] [blame] | 533 | for (const auto& item : list) { |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 534 | std::string msg = toString(item); |
Abhijeet Kaur | 54695d3 | 2019-01-15 13:07:20 +0000 | [diff] [blame] | 535 | dprintf(fd, "%s", msg.c_str()); |
| 536 | } |
Abhijeet Kaur | 54695d3 | 2019-01-15 13:07:20 +0000 | [diff] [blame] | 537 | } |
Martijn Coenen | a418d31 | 2019-01-30 12:03:38 +0100 | [diff] [blame] | 538 | |
| 539 | dprintf(fd, "SESSIONS:\n"); |
| 540 | std::vector<ApexSession> sessions = ApexSession::GetSessions(); |
| 541 | |
| 542 | for (const auto& session : sessions) { |
| 543 | std::string child_ids_str = ""; |
| 544 | auto child_ids = session.GetChildSessionIds(); |
| 545 | if (child_ids.size() > 0) { |
| 546 | child_ids_str = "Child IDs:"; |
| 547 | for (auto childSessionId : session.GetChildSessionIds()) { |
| 548 | child_ids_str += " " + std::to_string(childSessionId); |
| 549 | } |
| 550 | } |
| 551 | std::string msg = |
| 552 | StringLog() << "Session ID: " << session.GetId() << child_ids_str |
| 553 | << " State: " << SessionState_State_Name(session.GetState()) |
| 554 | << std::endl; |
| 555 | dprintf(fd, "%s", msg.c_str()); |
| 556 | } |
| 557 | |
| 558 | return OK; |
Abhijeet Kaur | 54695d3 | 2019-01-15 13:07:20 +0000 | [diff] [blame] | 559 | } |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 560 | |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 561 | status_t ApexService::shellCommand(int in, int out, int err, |
| 562 | const Vector<String16>& args) { |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 563 | if (in == BAD_TYPE || out == BAD_TYPE || err == BAD_TYPE) { |
| 564 | return BAD_VALUE; |
| 565 | } |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 566 | auto print_help = [](int fd, const char* prefix = nullptr) { |
| 567 | StringLog log; |
| 568 | if (prefix != nullptr) { |
| 569 | log << prefix << std::endl; |
| 570 | } |
| 571 | log << "ApexService:" << std::endl |
| 572 | << " help - display this help" << std::endl |
| 573 | << " stagePackage [packagePath] - stage package from the given path" |
| 574 | << std::endl |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 575 | << " stagePackages [packagePath1] ([packagePath2]...) - stage " |
| 576 | "multiple packages from the given path" |
| 577 | << std::endl |
| 578 | << " getActivePackage [packageName] - return info for active package " |
| 579 | "with given name, if present" |
| 580 | << std::endl |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 581 | << " getAllPackages - return the list of all packages" << std::endl |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 582 | << " getActivePackages - return the list of active packages" |
| 583 | << std::endl |
| 584 | << " activatePackage [packagePath] - activate package from the " |
| 585 | "given path" |
| 586 | << std::endl |
| 587 | << " deactivatePackage [packagePath] - deactivate package from the " |
| 588 | "given path" |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 589 | << std::endl |
Andreas Gampe | 1a48b1b | 2019-02-05 16:12:25 -0800 | [diff] [blame] | 590 | << " preinstallPackages [packagePath1] ([packagePath2]...) - run " |
| 591 | "pre-install hooks of the given packages" |
| 592 | << std::endl |
| 593 | << " postinstallPackages [packagePath1] ([packagePath2]...) - run " |
| 594 | "post-install hooks of the given packages" |
| 595 | << std::endl |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 596 | << " getStagedSessionInfo [sessionId] - displays information about a " |
| 597 | "given session previously submitted" |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 598 | << " submitStagedSession [sessionId] - attempts to submit the " |
| 599 | "installer session with given id" |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 600 | << std::endl; |
| 601 | dprintf(fd, "%s", log.operator std::string().c_str()); |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 602 | }; |
| 603 | |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 604 | if (args.size() == 0) { |
| 605 | print_help(err, "No command given"); |
| 606 | return BAD_VALUE; |
| 607 | } |
| 608 | |
| 609 | const String16& cmd = args[0]; |
| 610 | |
Andreas Gampe | 59ec1d9 | 2018-12-19 14:43:24 -0800 | [diff] [blame] | 611 | if (cmd == String16("stagePackage") || cmd == String16("stagePackages")) { |
| 612 | if (args.size() < 2) { |
| 613 | print_help(err, "stagePackage(s) requires at least one packagePath"); |
| 614 | return BAD_VALUE; |
| 615 | } |
| 616 | if (args.size() != 2 && cmd == String16("stagePackage")) { |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 617 | print_help(err, "stagePackage requires one packagePath"); |
| 618 | return BAD_VALUE; |
| 619 | } |
Andreas Gampe | 59ec1d9 | 2018-12-19 14:43:24 -0800 | [diff] [blame] | 620 | std::vector<std::string> pkgs; |
| 621 | pkgs.reserve(args.size() - 1); |
| 622 | for (size_t i = 1; i != args.size(); ++i) { |
| 623 | pkgs.emplace_back(String8(args[i]).string()); |
| 624 | } |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 625 | bool ret_value; |
Andreas Gampe | 59ec1d9 | 2018-12-19 14:43:24 -0800 | [diff] [blame] | 626 | BinderStatus status = stagePackages(pkgs, &ret_value); |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 627 | if (status.isOk()) { |
| 628 | return OK; |
| 629 | } |
Andreas Gampe | 59ec1d9 | 2018-12-19 14:43:24 -0800 | [diff] [blame] | 630 | std::string msg = StringLog() << "Failed to stage package(s): " |
| 631 | << status.toString8().string() << std::endl; |
Jiyong Park | 9b80048 | 2018-12-03 22:16:40 +0900 | [diff] [blame] | 632 | dprintf(err, "%s", msg.c_str()); |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 633 | return BAD_VALUE; |
| 634 | } |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 635 | if (cmd == String16("getAllPackages")) { |
| 636 | if (args.size() != 1) { |
| 637 | print_help(err, "Unrecognized options"); |
| 638 | return BAD_VALUE; |
| 639 | } |
| 640 | std::vector<ApexInfo> list; |
| 641 | BinderStatus status = getAllPackages(&list); |
| 642 | if (status.isOk()) { |
| 643 | for (const auto& item : list) { |
| 644 | std::string msg = toString(item); |
| 645 | dprintf(out, "%s", msg.c_str()); |
| 646 | } |
| 647 | return OK; |
| 648 | } |
| 649 | std::string msg = StringLog() << "Failed to retrieve packages: " |
| 650 | << status.toString8().string() << std::endl; |
| 651 | dprintf(err, "%s", msg.c_str()); |
| 652 | return BAD_VALUE; |
| 653 | } |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 654 | |
| 655 | if (cmd == String16("getActivePackages")) { |
| 656 | if (args.size() != 1) { |
| 657 | print_help(err, "Unrecognized options"); |
| 658 | return BAD_VALUE; |
| 659 | } |
Dario Freni | dded6c1 | 2018-11-19 16:02:26 +0000 | [diff] [blame] | 660 | std::vector<ApexInfo> list; |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 661 | BinderStatus status = getActivePackages(&list); |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 662 | if (status.isOk()) { |
Chih-Hung Hsieh | 43bb023 | 2018-12-12 14:30:01 -0800 | [diff] [blame] | 663 | for (const auto& item : list) { |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 664 | std::string msg = toString(item); |
Jiyong Park | 9b80048 | 2018-12-03 22:16:40 +0900 | [diff] [blame] | 665 | dprintf(out, "%s", msg.c_str()); |
Dario Freni | 8e88b68 | 2018-10-25 13:06:21 +0100 | [diff] [blame] | 666 | } |
| 667 | return OK; |
| 668 | } |
Andreas Gampe | 1773914 | 2019-01-09 16:00:26 -0800 | [diff] [blame] | 669 | std::string msg = StringLog() << "Failed to retrieve packages: " |
| 670 | << status.toString8().string() << std::endl; |
Jiyong Park | 9b80048 | 2018-12-03 22:16:40 +0900 | [diff] [blame] | 671 | dprintf(err, "%s", msg.c_str()); |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 672 | return BAD_VALUE; |
| 673 | } |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 674 | |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 675 | if (cmd == String16("getActivePackage")) { |
| 676 | if (args.size() != 2) { |
| 677 | print_help(err, "Unrecognized options"); |
| 678 | return BAD_VALUE; |
| 679 | } |
| 680 | |
| 681 | ApexInfo package; |
| 682 | BinderStatus status = getActivePackage(String8(args[1]).string(), &package); |
| 683 | if (status.isOk()) { |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 684 | std::string msg = toString(package); |
| 685 | dprintf(out, "%s", msg.c_str()); |
Narayan Kamath | 5ea5778 | 2019-01-03 18:17:05 +0000 | [diff] [blame] | 686 | return OK; |
| 687 | } |
| 688 | |
| 689 | std::string msg = StringLog() << "Failed to fetch active package: " |
| 690 | << String8(args[1]).string() |
| 691 | << ", error: " << status.toString8().string() |
| 692 | << std::endl; |
| 693 | dprintf(err, "%s", msg.c_str()); |
| 694 | return BAD_VALUE; |
| 695 | } |
| 696 | |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 697 | if (cmd == String16("activatePackage")) { |
| 698 | if (args.size() != 2) { |
| 699 | print_help(err, "activatePackage requires one packagePath"); |
| 700 | return BAD_VALUE; |
| 701 | } |
| 702 | BinderStatus status = activatePackage(String8(args[1]).string()); |
Andreas Gampe | 8eb187a | 2018-10-19 21:18:03 -0700 | [diff] [blame] | 703 | if (status.isOk()) { |
| 704 | return OK; |
| 705 | } |
Andreas Gampe | 1773914 | 2019-01-09 16:00:26 -0800 | [diff] [blame] | 706 | std::string msg = StringLog() << "Failed to activate package: " |
| 707 | << status.toString8().string() << std::endl; |
Jiyong Park | 9b80048 | 2018-12-03 22:16:40 +0900 | [diff] [blame] | 708 | dprintf(err, "%s", msg.c_str()); |
Andreas Gampe | 8eb187a | 2018-10-19 21:18:03 -0700 | [diff] [blame] | 709 | return BAD_VALUE; |
| 710 | } |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 711 | |
| 712 | if (cmd == String16("deactivatePackage")) { |
| 713 | if (args.size() != 2) { |
| 714 | print_help(err, "deactivatePackage requires one packagePath"); |
| 715 | return BAD_VALUE; |
| 716 | } |
| 717 | BinderStatus status = deactivatePackage(String8(args[1]).string()); |
Andreas Gampe | a4fe113 | 2018-11-16 11:14:31 -0800 | [diff] [blame] | 718 | if (status.isOk()) { |
| 719 | return OK; |
| 720 | } |
Andreas Gampe | 1773914 | 2019-01-09 16:00:26 -0800 | [diff] [blame] | 721 | std::string msg = StringLog() << "Failed to deactivate package: " |
| 722 | << status.toString8().string() << std::endl; |
Jiyong Park | 9b80048 | 2018-12-03 22:16:40 +0900 | [diff] [blame] | 723 | dprintf(err, "%s", msg.c_str()); |
Andreas Gampe | a4fe113 | 2018-11-16 11:14:31 -0800 | [diff] [blame] | 724 | return BAD_VALUE; |
| 725 | } |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 726 | |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 727 | if (cmd == String16("getStagedSessionInfo")) { |
| 728 | if (args.size() != 2) { |
| 729 | print_help(err, "getStagedSessionInfo requires one session id"); |
| 730 | return BAD_VALUE; |
| 731 | } |
| 732 | int session_id = strtol(String8(args[1]).c_str(), nullptr, 10); |
| 733 | if (session_id < 0) { |
| 734 | std::string msg = StringLog() |
| 735 | << "Failed to parse session id. Must be an integer."; |
| 736 | dprintf(err, "%s", msg.c_str()); |
| 737 | return BAD_VALUE; |
| 738 | } |
| 739 | |
| 740 | ApexSessionInfo session_info; |
| 741 | BinderStatus status = getStagedSessionInfo(session_id, &session_info); |
| 742 | if (status.isOk()) { |
| 743 | std::string msg = StringLog() |
| 744 | << "session_info: " |
| 745 | << " isUnknown: " << session_info.isUnknown |
| 746 | << " isVerified: " << session_info.isVerified |
| 747 | << " isStaged: " << session_info.isStaged |
| 748 | << " isActivated: " << session_info.isActivated |
Dario Freni | d243764 | 2019-01-11 14:35:23 +0000 | [diff] [blame] | 749 | << " isActivationFailed: " |
| 750 | << session_info.isActivationFailed << std::endl; |
| 751 | dprintf(out, "%s", msg.c_str()); |
| 752 | return OK; |
| 753 | } |
| 754 | std::string msg = StringLog() << "Failed to query session: " |
| 755 | << status.toString8().string() << std::endl; |
| 756 | dprintf(err, "%s", msg.c_str()); |
| 757 | return BAD_VALUE; |
| 758 | } |
| 759 | |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 760 | if (cmd == String16("submitStagedSession")) { |
| 761 | if (args.size() != 2) { |
| 762 | print_help(err, "submitStagedSession requires one session id"); |
| 763 | return BAD_VALUE; |
| 764 | } |
| 765 | int session_id = strtol(String8(args[1]).c_str(), nullptr, 10); |
| 766 | if (session_id < 0) { |
| 767 | std::string msg = StringLog() |
| 768 | << "Failed to parse session id. Must be an integer."; |
| 769 | dprintf(err, "%s", msg.c_str()); |
| 770 | return BAD_VALUE; |
| 771 | } |
| 772 | |
Dario Freni | ab5b6c4 | 2019-01-15 11:53:57 +0000 | [diff] [blame] | 773 | ApexInfoList list; |
| 774 | std::vector<int> empty_child_session_ids; |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 775 | bool ret_value; |
Dario Freni | ab5b6c4 | 2019-01-15 11:53:57 +0000 | [diff] [blame] | 776 | |
| 777 | BinderStatus status = submitStagedSession( |
| 778 | session_id, empty_child_session_ids, &list, &ret_value); |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 779 | if (status.isOk()) { |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 780 | if (ret_value) { |
Dario Freni | ab5b6c4 | 2019-01-15 11:53:57 +0000 | [diff] [blame] | 781 | for (const auto& item : list.apexInfos) { |
Gavin Corkery | 729905c | 2019-03-21 11:52:06 +0000 | [diff] [blame] | 782 | std::string msg = toString(item); |
Dario Freni | a6ad33e | 2019-01-09 14:35:43 +0000 | [diff] [blame] | 783 | dprintf(out, "%s", msg.c_str()); |
| 784 | } |
| 785 | } else { |
| 786 | std::string msg = StringLog() << "Verification failed." << std::endl; |
Dario Freni | 56231b4 | 2019-01-04 11:58:17 +0000 | [diff] [blame] | 787 | dprintf(out, "%s", msg.c_str()); |
| 788 | } |
| 789 | return OK; |
| 790 | } |
| 791 | std::string msg = StringLog() << "Failed to submit session: " |
| 792 | << status.toString8().string() << std::endl; |
| 793 | dprintf(err, "%s", msg.c_str()); |
| 794 | return BAD_VALUE; |
| 795 | } |
| 796 | |
Andreas Gampe | 1a48b1b | 2019-02-05 16:12:25 -0800 | [diff] [blame] | 797 | if (cmd == String16("preinstallPackages") || |
| 798 | cmd == String16("postinstallPackages")) { |
| 799 | if (args.size() < 2) { |
| 800 | print_help(err, |
| 801 | "preinstallPackages/postinstallPackages requires at least" |
| 802 | " one packagePath"); |
| 803 | return BAD_VALUE; |
| 804 | } |
| 805 | std::vector<std::string> pkgs; |
| 806 | pkgs.reserve(args.size() - 1); |
| 807 | for (size_t i = 1; i != args.size(); ++i) { |
| 808 | pkgs.emplace_back(String8(args[i]).string()); |
| 809 | } |
| 810 | BinderStatus status = cmd == String16("preinstallPackages") |
| 811 | ? preinstallPackages(pkgs) |
| 812 | : postinstallPackages(pkgs); |
| 813 | if (status.isOk()) { |
| 814 | return OK; |
| 815 | } |
| 816 | std::string msg = StringLog() << "Failed to pre/postinstall package(s): " |
| 817 | << status.toString8().string() << std::endl; |
| 818 | dprintf(err, "%s", msg.c_str()); |
| 819 | return BAD_VALUE; |
| 820 | } |
| 821 | |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 822 | if (cmd == String16("help")) { |
| 823 | if (args.size() != 1) { |
| 824 | print_help(err, "Help has no options"); |
| 825 | return BAD_VALUE; |
| 826 | } |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 827 | print_help(out); |
| 828 | return OK; |
| 829 | } |
Andreas Gampe | e386480 | 2018-12-19 14:12:59 -0800 | [diff] [blame] | 830 | |
Andreas Gampe | 66b5dcd | 2018-10-17 19:20:00 -0700 | [diff] [blame] | 831 | print_help(err); |
| 832 | return BAD_VALUE; |
| 833 | } |
| 834 | |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 835 | } // namespace |
| 836 | |
| 837 | static constexpr const char* kApexServiceName = "apexservice"; |
| 838 | |
| 839 | using android::defaultServiceManager; |
| 840 | using android::IPCThreadState; |
| 841 | using android::ProcessState; |
| 842 | using android::sp; |
| 843 | using android::String16; |
| 844 | |
| 845 | void CreateAndRegisterService() { |
| 846 | sp<ProcessState> ps(ProcessState::self()); |
| 847 | |
| 848 | // Create binder service and register with servicemanager |
| 849 | sp<ApexService> apexService = new ApexService(); |
| 850 | defaultServiceManager()->addService(String16(kApexServiceName), apexService); |
| 851 | } |
| 852 | |
Zimuzo | 9cc0be4 | 2019-01-09 11:37:34 +0000 | [diff] [blame] | 853 | void StartThreadPool() { |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 854 | sp<ProcessState> ps(ProcessState::self()); |
| 855 | |
| 856 | // Start threadpool, wait for IPC |
| 857 | ps->startThreadPool(); |
Zimuzo | 9cc0be4 | 2019-01-09 11:37:34 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | void JoinThreadPool() { |
Andreas Gampe | 602ef78 | 2018-11-12 16:51:31 -0800 | [diff] [blame] | 861 | IPCThreadState::self()->joinThreadPool(); // should not return |
| 862 | } |
| 863 | |
| 864 | } // namespace binder |
Andreas Gampe | b99abdd | 2018-10-19 19:59:17 -0700 | [diff] [blame] | 865 | } // namespace apex |
| 866 | } // namespace android |