From ec54595aff70654988a84e2e4b6f9fc2385c0a3d Mon Sep 17 00:00:00 2001 From: Jooyung Han Date: Tue, 14 May 2024 14:02:44 +0900 Subject: [PATCH] libstatssocket_lazy: add isAvailable() Early processes might not have access to libstatssocket. They should be able to check before using libstatssocket APIs. Adding isAvailable(). Bug: 281162849 Test: atest libstatssocket_lazy_test Test: atest StagedRollbackTest Change-Id: Ie2a851ef5bcf4fe081898153d688a843d8886788 --- libstats/socket_lazy/Android.bp | 11 +++++++- .../socket_lazy/include/statssocket_lazy.h | 25 +++++++++++++++++++ libstats/socket_lazy/libstatssocket_lazy.cpp | 13 ++++++++-- .../tests/libstatssocket_lazy_test.cpp | 6 +++++ 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 libstats/socket_lazy/include/statssocket_lazy.h diff --git a/libstats/socket_lazy/Android.bp b/libstats/socket_lazy/Android.bp index b2cd7b26b..241e87af3 100644 --- a/libstats/socket_lazy/Android.bp +++ b/libstats/socket_lazy/Android.bp @@ -7,6 +7,12 @@ package { cc_library_static { name: "libstatssocket_lazy", + local_include_dirs: [ + "include", + ], + export_include_dirs: [ + "include", + ], header_libs: [ "libstatssocket_headers", ], @@ -28,7 +34,10 @@ cc_test { "-Wall", "-Werror", ], - test_suites: ["device-tests", "mts-statsd"], + test_suites: [ + "device-tests", + "mts-statsd", + ], test_config: "libstatssocket_lazy_test.xml", // TODO(b/153588990): Remove when the build system properly separates. // 32bit and 64bit architectures. diff --git a/libstats/socket_lazy/include/statssocket_lazy.h b/libstats/socket_lazy/include/statssocket_lazy.h new file mode 100644 index 000000000..7dda0bac4 --- /dev/null +++ b/libstats/socket_lazy/include/statssocket_lazy.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2024 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 + +namespace android::statssocket::lazy { + +// See if libstatssocket.so is available. Early processes relying on _lazy might not have access +// to libstatssocket.so when they start before the StatsD APEX is available. +bool IsAvailable(); + +} // namespace android::statssocket::lazy diff --git a/libstats/socket_lazy/libstatssocket_lazy.cpp b/libstats/socket_lazy/libstatssocket_lazy.cpp index fe94ef2d8..d907c7e27 100644 --- a/libstats/socket_lazy/libstatssocket_lazy.cpp +++ b/libstats/socket_lazy/libstatssocket_lazy.cpp @@ -23,8 +23,10 @@ #include "log/log.h" -#include "stats_event.h" -#include "stats_socket.h" +#include +#include + +#include "statssocket_lazy.h" // This file provides a lazy interface to libstatssocket.so to address early boot dependencies. // Specifically bootanimation, surfaceflinger, and lmkd run before the statsd APEX is loaded and @@ -77,6 +79,13 @@ static void* LoadLibstatssocket(int dlopen_flags) { return dlopen("libstatssocket.so", dlopen_flags); } +namespace android::statssocket::lazy { +bool IsAvailable() { + static const void* handle = LoadLibstatssocket(RTLD_NOW); + return handle != nullptr; +} +} // namespace android::statssocket::lazy + // // Initialization and symbol binding. diff --git a/libstats/socket_lazy/tests/libstatssocket_lazy_test.cpp b/libstats/socket_lazy/tests/libstatssocket_lazy_test.cpp index 3de6cd762..733f1e41c 100644 --- a/libstats/socket_lazy/tests/libstatssocket_lazy_test.cpp +++ b/libstats/socket_lazy/tests/libstatssocket_lazy_test.cpp @@ -21,6 +21,8 @@ #include "stats_event.h" #include "stats_socket.h" +#include "statssocket_lazy.h" + // The tests here are just for the case when libstatssocket.so cannot be loaded by // libstatssocket_lazy. class LibstatssocketLazyTest : public ::testing::Test { @@ -57,3 +59,7 @@ TEST_F(LibstatssocketLazyTest, NoLibstatssocketForStatsEvent) { TEST_F(LibstatssocketLazyTest, NoLibstatssocketForStatsSocket) { EXPECT_DEATH(AStatsSocket_close(), kLoadFailed); } + +TEST_F(LibstatssocketLazyTest, IsAvailableFalse) { + EXPECT_FALSE(android::statssocket::lazy::IsAvailable()); +}