summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Anderson <dvander@google.com>2019-09-24 23:59:22 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2019-09-24 23:59:22 +0000
commit29eb46ca1d6442af2d181a16e67872741bf6f827 (patch)
treecb2d50c8e7a9724e16d05495b4d29c288978d9f2
parentef4513e526b843fafb5316367d8291f327a48722 (diff)
parent83fdeca17d4aeb8a333b61f21a52a793804e0975 (diff)
downloadgsid-29eb46ca1d6442af2d181a16e67872741bf6f827.tar.gz
Merge "Factor GetGsiService into a helper library."
-rw-r--r--Android.bp26
-rw-r--r--gsi_tool.cpp37
-rw-r--r--include/libgsi/libgsid.h27
-rw-r--r--libgsid.cpp71
4 files changed, 127 insertions, 34 deletions
diff --git a/Android.bp b/Android.bp
index 34ec996..421ece2 100644
--- a/Android.bp
+++ b/Android.bp
@@ -18,14 +18,16 @@ cc_binary {
name: "gsi_tool",
shared_libs: [
"gsi_aidl_interface-cpp",
- "libbinder",
"libbase",
+ "libbinder",
"libcutils",
"libgsi",
"liblog",
- "libservices",
"libutils",
],
+ static_libs: [
+ "libgsid",
+ ],
srcs: [
"gsi_tool.cpp",
],
@@ -44,6 +46,26 @@ cc_library {
export_include_dirs: ["include"],
}
+cc_library_static {
+ name: "libgsid",
+ srcs: [
+ "libgsid.cpp",
+ ],
+ shared_libs: [
+ "gsi_aidl_interface-cpp",
+ "libbase",
+ "libbinder",
+ "libcutils",
+ "liblog",
+ "libservices",
+ "libutils",
+ ],
+ static_libs: [
+ "libgsi",
+ ],
+ export_include_dirs: ["include"],
+}
+
cc_library_headers {
name: "libgsi_headers",
host_supported: true,
diff --git a/gsi_tool.cpp b/gsi_tool.cpp
index 4f885ae..12b35b6 100644
--- a/gsi_tool.cpp
+++ b/gsi_tool.cpp
@@ -27,6 +27,7 @@
#include <string>
#include <thread>
+#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/properties.h>
#include <android-base/unique_fd.h>
@@ -35,6 +36,7 @@
#include <binder/IServiceManager.h>
#include <cutils/android_reboot.h>
#include <libgsi/libgsi.h>
+#include <libgsi/libgsid.h>
using namespace android::gsi;
using namespace std::chrono_literals;
@@ -62,29 +64,6 @@ static const std::map<std::string, CommandCallback> kCommandMap = {
// clang-format on
};
-static sp<IGsid> GetGsiService() {
- if (android::base::GetProperty("init.svc.gsid", "") != "running") {
- if (!android::base::SetProperty("ctl.start", "gsid") ||
- !android::base::WaitForProperty("init.svc.gsid", "running", 5s)) {
- std::cerr << "Unable to start gsid\n";
- return nullptr;
- }
- }
-
- static const int kSleepTimeMs = 50;
- static const int kTotalWaitTimeMs = 3000;
- for (int i = 0; i < kTotalWaitTimeMs / kSleepTimeMs; i++) {
- auto sm = android::defaultServiceManager();
- auto name = android::String16(kGsiServiceName);
- android::sp<android::IBinder> res = sm->checkService(name);
- if (res) {
- return android::interface_cast<IGsid>(res);
- }
- usleep(kSleepTimeMs * 1000);
- }
- return nullptr;
-}
-
static std::string ErrorMessage(const android::binder::Status& status,
int error_code = IGsiService::INSTALL_ERROR_GENERIC) {
if (!status.isOk()) {
@@ -529,16 +508,10 @@ static int usage(int /* argc */, char* argv[]) {
}
int main(int argc, char** argv) {
- auto gsid = GetGsiService();
- if (!gsid) {
- std::cerr << "Could not connect to the gsid service." << std::endl;
- return EX_NOPERM;
- }
+ android::base::InitLogging(argv, android::base::StdioLogger, android::base::DefaultAborter);
- android::sp<IGsiService> service;
- auto status = gsid->getClient(&service);
- if (!status.isOk()) {
- std::cerr << "Could not get gsi client: " << ErrorMessage(status) << "\n";
+ android::sp<IGsiService> service = GetGsiService();
+ if (!service) {
return EX_SOFTWARE;
}
diff --git a/include/libgsi/libgsid.h b/include/libgsi/libgsid.h
new file mode 100644
index 0000000..46f8ef5
--- /dev/null
+++ b/include/libgsi/libgsid.h
@@ -0,0 +1,27 @@
+//
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#pragma once
+
+#include <android/gsi/IGsiService.h>
+
+namespace android {
+namespace gsi {
+
+android::sp<IGsiService> GetGsiService();
+
+} // namespace gsi
+} // namespace android
diff --git a/libgsid.cpp b/libgsid.cpp
new file mode 100644
index 0000000..cfddb60
--- /dev/null
+++ b/libgsid.cpp
@@ -0,0 +1,71 @@
+//
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include <android-base/logging.h>
+#include <android-base/properties.h>
+#include <android/gsi/IGsiService.h>
+#include <android/gsi/IGsid.h>
+#include <binder/IServiceManager.h>
+#include <libgsi/libgsi.h>
+
+namespace android {
+namespace gsi {
+
+using namespace std::chrono_literals;
+using android::sp;
+
+static sp<IGsid> GetGsid() {
+ if (android::base::GetProperty("init.svc.gsid", "") != "running") {
+ if (!android::base::SetProperty("ctl.start", "gsid") ||
+ !android::base::WaitForProperty("init.svc.gsid", "running", 5s)) {
+ LOG(ERROR) << "Unable to start gsid";
+ return nullptr;
+ }
+ }
+
+ static const int kSleepTimeMs = 50;
+ static const int kTotalWaitTimeMs = 3000;
+ for (int i = 0; i < kTotalWaitTimeMs / kSleepTimeMs; i++) {
+ auto sm = android::defaultServiceManager();
+ auto name = android::String16(kGsiServiceName);
+ android::sp<android::IBinder> res = sm->checkService(name);
+ if (res) {
+ return android::interface_cast<IGsid>(res);
+ }
+ usleep(kSleepTimeMs * 1000);
+ }
+
+ LOG(ERROR) << "Timed out trying to start gsid";
+ return nullptr;
+}
+
+sp<IGsiService> GetGsiService() {
+ auto gsid = GetGsid();
+ if (!gsid) {
+ return nullptr;
+ }
+
+ sp<IGsiService> service;
+ auto status = gsid->getClient(&service);
+ if (!status.isOk() || !service) {
+ LOG(ERROR) << "Error acquiring IGsid: " << status.exceptionMessage().string();
+ return nullptr;
+ }
+ return service;
+}
+
+} // namespace gsi
+} // namespace android