ServiceManagement.cpp revision 12f04d913dae2fb287089f6db24f144731c79d9c
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
17#define LOG_TAG "ServiceManagement"
18
19#include <hidl/HidlBinderSupport.h>
20#include <hidl/ServiceManagement.h>
21#include <hidl/Static.h>
22#include <hidl/Status.h>
23
24#include <hwbinder/IPCThreadState.h>
25#include <hwbinder/Parcel.h>
26#include <utils/Log.h>
27#include <utils/SystemClock.h>
28
29#include <unistd.h>
30
31#include <android/hidl/manager/1.0/IServiceManager.h>
32#include <android/hidl/manager/1.0/BpServiceManager.h>
33#include <android/hidl/manager/1.0/BnServiceManager.h>
34
35using android::hidl::manager::V1_0::IServiceManager;
36using android::hidl::manager::V1_0::BpServiceManager;
37using android::hidl::manager::V1_0::BnServiceManager;
38
39namespace android {
40namespace hardware {
41
42sp<IServiceManager> defaultServiceManager() {
43
44    if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
45    if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {
46        // HwBinder not available on this device or not accessible to
47        // this process.
48        return nullptr;
49    }
50    {
51        AutoMutex _l(gDefaultServiceManagerLock);
52        while (gDefaultServiceManager == NULL) {
53            gDefaultServiceManager = fromBinder<IServiceManager, BpServiceManager, BnServiceManager>(
54                ProcessState::self()->getContextObject(NULL));
55            if (gDefaultServiceManager == NULL)
56                sleep(1);
57        }
58    }
59
60    return gDefaultServiceManager;
61}
62
63}; // namespace hardware
64}; // namespace android
65