1 /*
2  * Copyright 2015 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 <binder/ProcessInfoService.h>
18 #include <binder/IServiceManager.h>
19 
20 #include <utils/Log.h>
21 #include <utils/String16.h>
22 
23 namespace android {
24 
ProcessInfoService()25 ProcessInfoService::ProcessInfoService() {
26     updateBinderLocked();
27 }
28 
getProcessStatesImpl(size_t length,int32_t * pids,int32_t * states)29 status_t ProcessInfoService::getProcessStatesImpl(size_t length, /*in*/ int32_t* pids,
30         /*out*/ int32_t* states) {
31     status_t err = NO_ERROR;
32     sp<IProcessInfoService> pis;
33     mProcessInfoLock.lock();
34     pis = mProcessInfoService;
35     mProcessInfoLock.unlock();
36 
37     for (int i = 0; i < BINDER_ATTEMPT_LIMIT; i++) {
38 
39         if (pis != NULL) {
40             err = pis->getProcessStatesFromPids(length, /*in*/ pids, /*out*/ states);
41             if (err == NO_ERROR) return NO_ERROR; // success
42             if (IInterface::asBinder(pis)->isBinderAlive()) return err;
43         }
44         sleep(1);
45 
46         mProcessInfoLock.lock();
47         if (pis == mProcessInfoService) {
48             updateBinderLocked();
49         }
50         pis = mProcessInfoService;
51         mProcessInfoLock.unlock();
52     }
53 
54     ALOGW("%s: Could not retrieve process states from ProcessInfoService after %d retries.",
55             __FUNCTION__, BINDER_ATTEMPT_LIMIT);
56 
57     return TIMED_OUT;
58 }
59 
updateBinderLocked()60 void ProcessInfoService::updateBinderLocked() {
61     const sp<IServiceManager> sm(defaultServiceManager());
62     if (sm != NULL) {
63         const String16 name("processinfo");
64         mProcessInfoService = interface_cast<IProcessInfoService>(sm->checkService(name));
65     }
66 }
67 
68 ANDROID_SINGLETON_STATIC_INSTANCE(ProcessInfoService);
69 
70 }; // namespace android
71