The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 17 | #define LOG_TAG "misc" |
| 18 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <utils/misc.h> |
Steven Moreland | 8da9613 | 2017-04-13 15:17:59 -0700 | [diff] [blame] | 20 | |
| 21 | #include <pthread.h> |
| 22 | |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 24 | #include <utils/Vector.h> |
| 25 | |
Jiyong Park | 011ee12 | 2018-05-29 16:41:30 +0900 | [diff] [blame] | 26 | #if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__) |
Jiyong Park | 0b3c24b | 2017-05-26 17:57:18 +0900 | [diff] [blame] | 27 | #include <dlfcn.h> |
| 28 | #include <vndksupport/linker.h> |
| 29 | #endif |
| 30 | |
| 31 | extern "C" void do_report_sysprop_change(); |
| 32 | |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | using namespace android; |
| 34 | |
| 35 | namespace android { |
| 36 | |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 37 | struct sysprop_change_callback_info { |
| 38 | sysprop_change_callback callback; |
| 39 | int priority; |
| 40 | }; |
The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 42 | #if !defined(_WIN32) |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 43 | static pthread_mutex_t gSyspropMutex = PTHREAD_MUTEX_INITIALIZER; |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 44 | static Vector<sysprop_change_callback_info>* gSyspropList = nullptr; |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 45 | #endif |
| 46 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 47 | #if !defined(_WIN32) |
Dan Willemsen | 528f144 | 2017-11-29 18:06:11 -0800 | [diff] [blame] | 48 | void add_sysprop_change_callback(sysprop_change_callback cb, int priority) { |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 49 | pthread_mutex_lock(&gSyspropMutex); |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 50 | if (gSyspropList == nullptr) { |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 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); |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 68 | } |
Dan Willemsen | 528f144 | 2017-11-29 18:06:11 -0800 | [diff] [blame] | 69 | #else |
| 70 | void add_sysprop_change_callback(sysprop_change_callback, int) {} |
| 71 | #endif |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 72 | |
Jiyong Park | 011ee12 | 2018-05-29 16:41:30 +0900 | [diff] [blame] | 73 | #if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__) |
Jiyong Park | 0b3c24b | 2017-05-26 17:57:18 +0900 | [diff] [blame] | 74 | void (*get_report_sysprop_change_func())() { |
| 75 | void (*func)() = nullptr; |
| 76 | void* handle = android_load_sphal_library("libutils.so", RTLD_NOW); |
| 77 | if (handle != nullptr) { |
| 78 | func = reinterpret_cast<decltype(func)>(dlsym(handle, "do_report_sysprop_change")); |
| 79 | } |
| 80 | |
| 81 | return func; |
| 82 | } |
| 83 | #endif |
| 84 | |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 85 | void report_sysprop_change() { |
Jiyong Park | 0b3c24b | 2017-05-26 17:57:18 +0900 | [diff] [blame] | 86 | do_report_sysprop_change(); |
| 87 | |
Jiyong Park | 011ee12 | 2018-05-29 16:41:30 +0900 | [diff] [blame] | 88 | #if defined(__ANDROID__) && !defined(__ANDROID_RECOVERY__) |
Jiyong Park | 0b3c24b | 2017-05-26 17:57:18 +0900 | [diff] [blame] | 89 | // libutils.so is double loaded; from the default namespace and from the |
| 90 | // 'sphal' namespace. Redirect the sysprop change event to the other instance |
| 91 | // of libutils.so loaded in the 'sphal' namespace so that listeners attached |
| 92 | // to that instance is also notified with this event. |
| 93 | static auto func = get_report_sysprop_change_func(); |
| 94 | if (func != nullptr) { |
| 95 | (*func)(); |
| 96 | } |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | }; // namespace android |
| 101 | |
| 102 | void do_report_sysprop_change() { |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 103 | #if !defined(_WIN32) |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 104 | pthread_mutex_lock(&gSyspropMutex); |
| 105 | Vector<sysprop_change_callback_info> listeners; |
Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 106 | if (gSyspropList != nullptr) { |
Dianne Hackborn | c1309d7 | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 107 | listeners = *gSyspropList; |
| 108 | } |
| 109 | pthread_mutex_unlock(&gSyspropMutex); |
| 110 | |
| 111 | //ALOGI("Reporting sysprop change to %d listeners", listeners.size()); |
| 112 | for (size_t i=0; i<listeners.size(); i++) { |
| 113 | listeners[i].callback(); |
| 114 | } |
| 115 | #endif |
| 116 | } |