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>
18 #include <unistd.h>
19
20 #include <android/permission_manager.h>
21 #include <binder/ActivityManager.h>
22 #include <binder/Binder.h>
23 #include <binder/IServiceManager.h>
24 #include <binder/ProcessState.h>
25
26 namespace android {
27
28 using namespace std::chrono_literals;
29
ActivityManager()30 ActivityManager::ActivityManager()
31 {
32 }
33
getService()34 sp<IActivityManager> ActivityManager::getService()
35 {
36 std::lock_guard<Mutex> scoped_lock(mLock);
37 sp<IActivityManager> service = mService;
38 if (ProcessState::self()->isThreadPoolStarted()) {
39 if (service == nullptr || !IInterface::asBinder(service)->isBinderAlive()) {
40 sp<IBinder> binder = defaultServiceManager()->waitForService(String16("activity"));
41 service = interface_cast<IActivityManager>(binder);
42 mService = service;
43 }
44 } else {
45 ALOGI("Thread pool not started. Polling for activity service.");
46 auto startTime = std::chrono::steady_clock::now().min();
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...
51 if (startTime == startTime.min()) {
52 startTime = std::chrono::steady_clock::now();
53 ALOGI("Waiting for activity service");
54 } else if (std::chrono::steady_clock::now() - startTime > 1000s) {
55 // TODO(b/342453147): timeout of 1000s = 16min and 40s doesn't seem intended
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 }
66 }
67 return mService;
68 }
69
openContentUri(const String16 & stringUri)70 int ActivityManager::openContentUri(const String16& stringUri)
71 {
72 sp<IActivityManager> service = getService();
73 return service != nullptr ? service->openContentUri(stringUri) : -1;
74 }
75
registerUidObserver(const sp<IUidObserver> & observer,const int32_t event,const int32_t cutpoint,const String16 & callingPackage)76 status_t ActivityManager::registerUidObserver(const sp<IUidObserver>& observer,
77 const int32_t event,
78 const int32_t cutpoint,
79 const String16& callingPackage)
80 {
81 sp<IActivityManager> service = getService();
82 if (service != nullptr) {
83 return service->registerUidObserver(observer, event, cutpoint, callingPackage);
84 }
85 // ActivityManagerService appears dead. Return usual error code for dead service.
86 return DEAD_OBJECT;
87 }
88
registerUidObserverForUids(const sp<IUidObserver> & observer,const int32_t event,const int32_t cutpoint,const String16 & callingPackage,const int32_t uids[],size_t nUids,sp<IBinder> & observerToken)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
unregisterUidObserver(const sp<IUidObserver> & observer)103 status_t ActivityManager::unregisterUidObserver(const sp<IUidObserver>& observer)
104 {
105 sp<IActivityManager> service = getService();
106 if (service != nullptr) {
107 return service->unregisterUidObserver(observer);
108 }
109 // ActivityManagerService appears dead. Return usual error code for dead service.
110 return DEAD_OBJECT;
111 }
112
addUidToObserver(const sp<IBinder> & observerToken,const String16 & callingPackage,int32_t uid)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
removeUidFromObserver(const sp<IBinder> & observerToken,const String16 & callingPackage,int32_t uid)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
isUidActive(const uid_t uid,const String16 & callingPackage)133 bool ActivityManager::isUidActive(const uid_t uid, const String16& callingPackage)
134 {
135 sp<IActivityManager> service = getService();
136 if (service != nullptr) {
137 return service->isUidActive(uid, callingPackage);
138 }
139 return false;
140 }
141
getUidProcessState(const uid_t uid,const String16 & callingPackage)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
checkPermission(const String16 & permission,const pid_t pid,const uid_t uid,int32_t * outResult)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
linkToDeath(const sp<IBinder::DeathRecipient> & recipient)163 status_t ActivityManager::linkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
164 sp<IActivityManager> service = getService();
165 if (service != nullptr) {
166 return IInterface::asBinder(service)->linkToDeath(recipient);
167 }
168 return INVALID_OPERATION;
169 }
170
unlinkToDeath(const sp<IBinder::DeathRecipient> & recipient)171 status_t ActivityManager::unlinkToDeath(const sp<IBinder::DeathRecipient>& recipient) {
172 sp<IActivityManager> service = getService();
173 if (service != nullptr) {
174 return IInterface::asBinder(service)->unlinkToDeath(recipient);
175 }
176 return INVALID_OPERATION;
177 }
178
179 } // namespace android
180