blob: cfddb60a22bb4a1dfad55e323e2c624527a12a4a [file] [log] [blame]
David Anderson83fdeca2019-09-09 17:56:22 -07001//
2// Copyright (C) 2019 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 <android-base/logging.h>
18#include <android-base/properties.h>
19#include <android/gsi/IGsiService.h>
20#include <android/gsi/IGsid.h>
21#include <binder/IServiceManager.h>
22#include <libgsi/libgsi.h>
23
24namespace android {
25namespace gsi {
26
27using namespace std::chrono_literals;
28using android::sp;
29
30static sp<IGsid> GetGsid() {
31 if (android::base::GetProperty("init.svc.gsid", "") != "running") {
32 if (!android::base::SetProperty("ctl.start", "gsid") ||
33 !android::base::WaitForProperty("init.svc.gsid", "running", 5s)) {
34 LOG(ERROR) << "Unable to start gsid";
35 return nullptr;
36 }
37 }
38
39 static const int kSleepTimeMs = 50;
40 static const int kTotalWaitTimeMs = 3000;
41 for (int i = 0; i < kTotalWaitTimeMs / kSleepTimeMs; i++) {
42 auto sm = android::defaultServiceManager();
43 auto name = android::String16(kGsiServiceName);
44 android::sp<android::IBinder> res = sm->checkService(name);
45 if (res) {
46 return android::interface_cast<IGsid>(res);
47 }
48 usleep(kSleepTimeMs * 1000);
49 }
50
51 LOG(ERROR) << "Timed out trying to start gsid";
52 return nullptr;
53}
54
55sp<IGsiService> GetGsiService() {
56 auto gsid = GetGsid();
57 if (!gsid) {
58 return nullptr;
59 }
60
61 sp<IGsiService> service;
62 auto status = gsid->getClient(&service);
63 if (!status.isOk() || !service) {
64 LOG(ERROR) << "Error acquiring IGsid: " << status.exceptionMessage().string();
65 return nullptr;
66 }
67 return service;
68}
69
70} // namespace gsi
71} // namespace android