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
This commit is contained in:
Jooyung Han 2024-05-14 14:02:44 +09:00
parent 9fbd7e1026
commit ec54595aff
4 changed files with 52 additions and 3 deletions

View file

@ -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.

View file

@ -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

View file

@ -23,8 +23,10 @@
#include "log/log.h"
#include "stats_event.h"
#include "stats_socket.h"
#include <stats_event.h>
#include <stats_socket.h>
#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.

View file

@ -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());
}