1cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project/*
2cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * Copyright (C) 2005 The Android Open Source Project
3cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project *
4cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * you may not use this file except in compliance with the License.
6cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * You may obtain a copy of the License at
7cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project *
8cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project *
10cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * See the License for the specific language governing permissions and
14cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project * limitations under the License.
15cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project */
16cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project
17c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn#define LOG_TAG "misc"
18c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn
19cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project#include <utils/misc.h>
20cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project
218da9613b3f866b298bee1236b80bbbc63c554fdfSteven Moreland#include <pthread.h>
22c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn
238da9613b3f866b298bee1236b80bbbc63c554fdfSteven Moreland#include <utils/Log.h>
24c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn#include <utils/Vector.h>
25c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn
260b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park#if defined(__ANDROID__)
270b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park#include <dlfcn.h>
280b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park#include <vndksupport/linker.h>
290b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park#endif
300b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park
310b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Parkextern "C" void do_report_sysprop_change();
320b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park
33cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Projectusing namespace android;
34cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project
35cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Projectnamespace android {
36cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project
37c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackbornstruct sysprop_change_callback_info {
38c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    sysprop_change_callback callback;
39c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    int priority;
40c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn};
41c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn
424a6e5a3b641dd99b658c4c336490371a3a5ae180Yabin Cui#if !defined(_WIN32)
43c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackbornstatic pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER;
44c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackbornstatic Vector<sysprop_change_callback_info>* gSyspropList = NULL;
45c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn#endif
46c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn
47c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackbornvoid add_sysprop_change_callback(sysprop_change_callback cb, int priority) {
484a6e5a3b641dd99b658c4c336490371a3a5ae180Yabin Cui#if !defined(_WIN32)
49c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    pthread_mutex_lock(&gSyspropMutex);
50c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    if (gSyspropList == NULL) {
51c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn        gSyspropList = new Vector<sysprop_change_callback_info>();
52c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    }
53c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    sysprop_change_callback_info info;
54c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    info.callback = cb;
55c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    info.priority = priority;
56c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    bool added = false;
57c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    for (size_t i=0; i<gSyspropList->size(); i++) {
58c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn        if (priority >= gSyspropList->itemAt(i).priority) {
59c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn            gSyspropList->insertAt(info, i);
60c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn            added = true;
61c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn            break;
62c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn        }
63c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    }
64c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    if (!added) {
65c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn        gSyspropList->add(info);
66c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    }
67c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    pthread_mutex_unlock(&gSyspropMutex);
68c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn#endif
69c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn}
70c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn
710b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park#if defined(__ANDROID__)
720b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Parkvoid (*get_report_sysprop_change_func())() {
730b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    void (*func)() = nullptr;
740b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    void* handle = android_load_sphal_library("libutils.so", RTLD_NOW);
750b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    if (handle != nullptr) {
760b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park        func = reinterpret_cast<decltype(func)>(dlsym(handle, "do_report_sysprop_change"));
770b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    }
780b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park
790b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    return func;
800b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park}
810b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park#endif
820b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park
83c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackbornvoid report_sysprop_change() {
840b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    do_report_sysprop_change();
850b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park
860b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park#if defined(__ANDROID__)
870b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    // libutils.so is double loaded; from the default namespace and from the
880b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    // 'sphal' namespace. Redirect the sysprop change event to the other instance
890b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    // of libutils.so loaded in the 'sphal' namespace so that listeners attached
900b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    // to that instance is also notified with this event.
910b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    static auto func = get_report_sysprop_change_func();
920b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    if (func != nullptr) {
930b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park        (*func)();
940b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park    }
950b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park#endif
960b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park}
970b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park
980b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park};  // namespace android
990b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Park
1000b3c24bd4fb29ff48150c03104658af902a0e137Jiyong Parkvoid do_report_sysprop_change() {
1014a6e5a3b641dd99b658c4c336490371a3a5ae180Yabin Cui#if !defined(_WIN32)
102c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    pthread_mutex_lock(&gSyspropMutex);
103c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    Vector<sysprop_change_callback_info> listeners;
104c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    if (gSyspropList != NULL) {
105c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn        listeners = *gSyspropList;
106c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    }
107c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    pthread_mutex_unlock(&gSyspropMutex);
108cbb1011c95e0c25c29e40e203a6a31bccd029da3The Android Open Source Project
109c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    //ALOGI("Reporting sysprop change to %d listeners", listeners.size());
110c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    for (size_t i=0; i<listeners.size(); i++) {
111c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn        listeners[i].callback();
112c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn    }
113c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn#endif
114c1309d74e8929f73e1b9cdb5dbf70aa8a2b09af3Dianne Hackborn}
115