Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 <mutex> |
Svetoslav Ganov | 86fc4f3 | 2018-02-22 00:29:30 -0800 | [diff] [blame] | 18 | #include <unistd.h> |
| 19 | |
Chong Zhang | b632bd5 | 2020-11-02 11:01:48 -0800 | [diff] [blame] | 20 | #include <android/permission_manager.h> |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 21 | #include <binder/ActivityManager.h> |
| 22 | #include <binder/Binder.h> |
| 23 | #include <binder/IServiceManager.h> |
Yifei Zhang | 7086bcb | 2023-06-02 15:18:11 -0700 | [diff] [blame] | 24 | #include <binder/ProcessState.h> |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 25 | |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 26 | namespace android { |
| 27 | |
Tomasz Wasilczyk | 1d46f58 | 2024-05-21 15:06:29 -0700 | [diff] [blame] | 28 | using namespace std::chrono_literals; |
| 29 | |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 30 | ActivityManager::ActivityManager() |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | sp<IActivityManager> ActivityManager::getService() |
| 35 | { |
| 36 | std::lock_guard<Mutex> scoped_lock(mLock); |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 37 | sp<IActivityManager> service = mService; |
Yifei Zhang | 7086bcb | 2023-06-02 15:18:11 -0700 | [diff] [blame] | 38 | if (ProcessState::self()->isThreadPoolStarted()) { |
| 39 | if (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) { |
| 40 | sp<IBinder> binder = defaultServiceManager()->waitForService(String16("activity")); |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 41 | service = interface_cast<IActivityManager>(binder); |
| 42 | mService = service; |
| 43 | } |
Yifei Zhang | 7086bcb | 2023-06-02 15:18:11 -0700 | [diff] [blame] | 44 | } else { |
| 45 | ALOGI("Thread pool not started. Polling for activity service."); |
Tomasz Wasilczyk | 1d46f58 | 2024-05-21 15:06:29 -0700 | [diff] [blame] | 46 | auto startTime = std::chrono::steady_clock::now().min(); |
Yifei Zhang | 7086bcb | 2023-06-02 15:18:11 -0700 | [diff] [blame] | 47 | while (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) { |
| 48 | sp<IBinder> binder = defaultServiceManager()->checkService(String16("activity")); |
| 49 | if (binder == nullptr) { |
| 50 | // Wait for the activity service to come back... |
Tomasz Wasilczyk | 1d46f58 | 2024-05-21 15:06:29 -0700 | [diff] [blame] | 51 | if (startTime == startTime.min()) { |
| 52 | startTime = std::chrono::steady_clock::now(); |
Yifei Zhang | 7086bcb | 2023-06-02 15:18:11 -0700 | [diff] [blame] | 53 | ALOGI("Waiting for activity service"); |
Tomasz Wasilczyk | 1d46f58 | 2024-05-21 15:06:29 -0700 | [diff] [blame] | 54 | } else if (std::chrono::steady_clock::now() - startTime > 1000s) { |
| 55 | // TODO(b/342453147): timeout of 1000s = 16min and 40s doesn't seem intended |
Yifei Zhang | 7086bcb | 2023-06-02 15:18:11 -0700 | [diff] [blame] | 56 | ALOGW("Waiting too long for activity service, giving up"); |
| 57 | service = nullptr; |
| 58 | break; |
| 59 | } |
| 60 | usleep(25000); |
| 61 | } else { |
| 62 | service = interface_cast<IActivityManager>(binder); |
| 63 | mService = service; |
| 64 | } |
| 65 | } |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 66 | } |
Yifei Zhang | 7086bcb | 2023-06-02 15:18:11 -0700 | [diff] [blame] | 67 | return mService; |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | int ActivityManager::openContentUri(const String16& stringUri) |
| 71 | { |
| 72 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 73 | return service != nullptr ? service->openContentUri(stringUri) : -1; |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 76 | status_t ActivityManager::registerUidObserver(const sp<IUidObserver>& observer, |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 77 | const int32_t event, |
| 78 | const int32_t cutpoint, |
| 79 | const String16& callingPackage) |
| 80 | { |
| 81 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 82 | if (service != nullptr) { |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 83 | return service->registerUidObserver(observer, event, cutpoint, callingPackage); |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 84 | } |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 85 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 86 | return DEAD_OBJECT; |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Austin Borger | 805d804 | 2023-04-21 20:07:49 -0700 | [diff] [blame] | 89 | status_t ActivityManager::registerUidObserverForUids(const sp<IUidObserver>& observer, |
| 90 | const int32_t event, const int32_t cutpoint, |
| 91 | const String16& callingPackage, |
| 92 | const int32_t uids[], size_t nUids, |
| 93 | /*out*/ sp<IBinder>& observerToken) { |
| 94 | sp<IActivityManager> service = getService(); |
| 95 | if (service != nullptr) { |
| 96 | return service->registerUidObserverForUids(observer, event, cutpoint, callingPackage, uids, |
| 97 | nUids, observerToken); |
| 98 | } |
| 99 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 100 | return DEAD_OBJECT; |
| 101 | } |
| 102 | |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 103 | status_t ActivityManager::unregisterUidObserver(const sp<IUidObserver>& observer) |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 104 | { |
| 105 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 106 | if (service != nullptr) { |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 107 | return service->unregisterUidObserver(observer); |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 108 | } |
Chong Zhang | 1576534 | 2020-11-17 14:44:20 -0800 | [diff] [blame] | 109 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 110 | return DEAD_OBJECT; |
Ganesh Mahendran | 4d85b8c | 2017-11-02 14:43:38 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Austin Borger | 805d804 | 2023-04-21 20:07:49 -0700 | [diff] [blame] | 113 | status_t ActivityManager::addUidToObserver(const sp<IBinder>& observerToken, |
| 114 | const String16& callingPackage, int32_t uid) { |
| 115 | sp<IActivityManager> service = getService(); |
| 116 | if (service != nullptr) { |
| 117 | return service->addUidToObserver(observerToken, callingPackage, uid); |
| 118 | } |
| 119 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 120 | return DEAD_OBJECT; |
| 121 | } |
| 122 | |
| 123 | status_t ActivityManager::removeUidFromObserver(const sp<IBinder>& observerToken, |
| 124 | const String16& callingPackage, int32_t uid) { |
| 125 | sp<IActivityManager> service = getService(); |
| 126 | if (service != nullptr) { |
| 127 | return service->removeUidFromObserver(observerToken, callingPackage, uid); |
| 128 | } |
| 129 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 130 | return DEAD_OBJECT; |
| 131 | } |
| 132 | |
Svet Ganov | fa85180 | 2018-03-27 17:17:46 -0700 | [diff] [blame] | 133 | bool ActivityManager::isUidActive(const uid_t uid, const String16& callingPackage) |
| 134 | { |
| 135 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 136 | if (service != nullptr) { |
Svet Ganov | fa85180 | 2018-03-27 17:17:46 -0700 | [diff] [blame] | 137 | return service->isUidActive(uid, callingPackage); |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
Eric Laurent | 0559589 | 2018-10-18 14:56:24 -0700 | [diff] [blame] | 142 | int32_t ActivityManager::getUidProcessState(const uid_t uid, const String16& callingPackage) |
| 143 | { |
| 144 | sp<IActivityManager> service = getService(); |
| 145 | if (service != nullptr) { |
| 146 | return service->getUidProcessState(uid, callingPackage); |
| 147 | } |
| 148 | return PROCESS_STATE_UNKNOWN; |
| 149 | } |
| 150 | |
Chong Zhang | b632bd5 | 2020-11-02 11:01:48 -0800 | [diff] [blame] | 151 | status_t ActivityManager::checkPermission(const String16& permission, |
| 152 | const pid_t pid, |
| 153 | const uid_t uid, |
| 154 | int32_t* outResult) { |
| 155 | sp<IActivityManager> service = getService(); |
| 156 | if (service != nullptr) { |
| 157 | return service->checkPermission(permission, pid, uid, outResult); |
| 158 | } |
| 159 | // ActivityManagerService appears dead. Return usual error code for dead service. |
| 160 | return DEAD_OBJECT; |
| 161 | } |
| 162 | |
Eino-Ville Talvala | ae8b20d | 2018-03-20 11:05:23 -0700 | [diff] [blame] | 163 | status_t ActivityManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) { |
| 164 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 165 | if (service != nullptr) { |
Eino-Ville Talvala | ae8b20d | 2018-03-20 11:05:23 -0700 | [diff] [blame] | 166 | return IInterface::asBinder(service)->linkToDeath(recipient); |
| 167 | } |
| 168 | return INVALID_OPERATION; |
| 169 | } |
| 170 | |
| 171 | status_t ActivityManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) { |
| 172 | sp<IActivityManager> service = getService(); |
Yi Kong | 9163556 | 2018-06-07 14:38:36 -0700 | [diff] [blame] | 173 | if (service != nullptr) { |
Eino-Ville Talvala | ae8b20d | 2018-03-20 11:05:23 -0700 | [diff] [blame] | 174 | return IInterface::asBinder(service)->unlinkToDeath(recipient); |
| 175 | } |
| 176 | return INVALID_OPERATION; |
| 177 | } |
| 178 | |
Steven Moreland | 6511af5 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 179 | } // namespace android |