blob: d95fd056bfb0d47c4ea852122101c55c65c5a112 [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 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
Dianne Hackbornc1309d72012-05-08 18:54:22 -070017#define LOG_TAG "misc"
18
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080019#include <utils/misc.h>
Steven Moreland8da96132017-04-13 15:17:59 -070020
21#include <pthread.h>
22
Dianne Hackbornc1309d72012-05-08 18:54:22 -070023#include <utils/Log.h>
Dianne Hackbornc1309d72012-05-08 18:54:22 -070024#include <utils/Vector.h>
25
Jiyong Park0b3c24b2017-05-26 17:57:18 +090026#if defined(__ANDROID__)
27#include <dlfcn.h>
28#include <vndksupport/linker.h>
29#endif
30
31extern "C" void do_report_sysprop_change();
32
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080033using namespace android;
34
35namespace android {
36
Dianne Hackbornc1309d72012-05-08 18:54:22 -070037struct sysprop_change_callback_info {
38 sysprop_change_callback callback;
39 int priority;
40};
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080041
Yabin Cui4a6e5a32015-01-26 19:48:54 -080042#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070043static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER;
44static Vector<sysprop_change_callback_info>* gSyspropList = NULL;
45#endif
46
47void add_sysprop_change_callback(sysprop_change_callback cb, int priority) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -080048#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -070049 pthread_mutex_lock(&gSyspropMutex);
50 if (gSyspropList == NULL) {
51 gSyspropList = new Vector<sysprop_change_callback_info>();
52 }
53 sysprop_change_callback_info info;
54 info.callback = cb;
55 info.priority = priority;
56 bool added = false;
57 for (size_t i=0; i<gSyspropList->size(); i++) {
58 if (priority >= gSyspropList->itemAt(i).priority) {
59 gSyspropList->insertAt(info, i);
60 added = true;
61 break;
62 }
63 }
64 if (!added) {
65 gSyspropList->add(info);
66 }
67 pthread_mutex_unlock(&gSyspropMutex);
68#endif
69}
70
Jiyong Park0b3c24b2017-05-26 17:57:18 +090071#if defined(__ANDROID__)
72void (*get_report_sysprop_change_func())() {
73 void (*func)() = nullptr;
74 void* handle = android_load_sphal_library("libutils.so", RTLD_NOW);
75 if (handle != nullptr) {
76 func = reinterpret_cast<decltype(func)>(dlsym(handle, "do_report_sysprop_change"));
77 }
78
79 return func;
80}
81#endif
82
Dianne Hackbornc1309d72012-05-08 18:54:22 -070083void report_sysprop_change() {
Jiyong Park0b3c24b2017-05-26 17:57:18 +090084 do_report_sysprop_change();
85
86#if defined(__ANDROID__)
87 // libutils.so is double loaded; from the default namespace and from the
88 // 'sphal' namespace. Redirect the sysprop change event to the other instance
89 // of libutils.so loaded in the 'sphal' namespace so that listeners attached
90 // to that instance is also notified with this event.
91 static auto func = get_report_sysprop_change_func();
92 if (func != nullptr) {
93 (*func)();
94 }
95#endif
96}
97
98}; // namespace android
99
100void do_report_sysprop_change() {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800101#if !defined(_WIN32)
Dianne Hackbornc1309d72012-05-08 18:54:22 -0700102 pthread_mutex_lock(&gSyspropMutex);
103 Vector<sysprop_change_callback_info> listeners;
104 if (gSyspropList != NULL) {
105 listeners = *gSyspropList;
106 }
107 pthread_mutex_unlock(&gSyspropMutex);
108
109 //ALOGI("Reporting sysprop change to %d listeners", listeners.size());
110 for (size_t i=0; i<listeners.size(); i++) {
111 listeners[i].callback();
112 }
113#endif
114}