Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #pragma once |
| 30 | |
| 31 | #include <string.h> |
Elliott Hughes | 620eec1 | 2024-08-07 17:59:53 +0000 | [diff] [blame] | 32 | #include <sys/system_properties.h> |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | a98aa56 | 2017-04-11 17:23:37 -0700 | [diff] [blame] | 34 | // Cached system property lookup. For code that needs to read the same property multiple times, |
| 35 | // this class helps optimize those lookups. |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 36 | class CachedProperty { |
| 37 | public: |
Elliott Hughes | a98aa56 | 2017-04-11 17:23:37 -0700 | [diff] [blame] | 38 | // The lifetime of `property_name` must be greater than that of this CachedProperty. |
Elliott Hughes | 5cec377 | 2018-01-19 15:45:23 -0800 | [diff] [blame] | 39 | explicit CachedProperty(const char* property_name) |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 40 | : property_name_(property_name), |
| 41 | prop_info_(nullptr), |
| 42 | cached_area_serial_(0), |
Tom Cherry | 24a95d2 | 2017-10-18 10:25:39 -0700 | [diff] [blame] | 43 | cached_property_serial_(0), |
| 44 | is_read_only_(strncmp(property_name, "ro.", 3) == 0), |
| 45 | read_only_property_(nullptr) { |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 46 | cached_value_[0] = '\0'; |
| 47 | } |
| 48 | |
Elliott Hughes | e4ddb3c | 2017-04-17 14:12:25 -0700 | [diff] [blame] | 49 | // Returns true if the property has been updated (based on the serial rather than the value) |
| 50 | // since the last call to Get. |
| 51 | bool DidChange() { |
| 52 | uint32_t initial_property_serial_ = cached_property_serial_; |
| 53 | Get(); |
| 54 | return (cached_property_serial_ != initial_property_serial_); |
| 55 | } |
| 56 | |
Elliott Hughes | a98aa56 | 2017-04-11 17:23:37 -0700 | [diff] [blame] | 57 | // Returns the current value of the underlying system property as cheaply as possible. |
| 58 | // The returned pointer is valid until the next call to Get. It is the caller's responsibility |
| 59 | // to provide a lock for thread-safety. |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 60 | const char* Get() { |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 61 | // Do we have a `struct prop_info` yet? |
| 62 | if (prop_info_ == nullptr) { |
| 63 | // `__system_property_find` is expensive, so only retry if a property |
| 64 | // has been created since last time we checked. |
| 65 | uint32_t property_area_serial = __system_property_area_serial(); |
| 66 | if (property_area_serial != cached_area_serial_) { |
| 67 | prop_info_ = __system_property_find(property_name_); |
| 68 | cached_area_serial_ = property_area_serial; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if (prop_info_ != nullptr) { |
| 73 | // Only bother re-reading the property if it's actually changed since last time. |
| 74 | uint32_t property_serial = __system_property_serial(prop_info_); |
| 75 | if (property_serial != cached_property_serial_) { |
| 76 | __system_property_read_callback(prop_info_, &CachedProperty::Callback, this); |
| 77 | } |
| 78 | } |
Tom Cherry | 24a95d2 | 2017-10-18 10:25:39 -0700 | [diff] [blame] | 79 | if (is_read_only_ && read_only_property_ != nullptr) { |
| 80 | return read_only_property_; |
| 81 | } |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 82 | return cached_value_; |
| 83 | } |
| 84 | |
| 85 | private: |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 86 | const char* property_name_; |
| 87 | const prop_info* prop_info_; |
| 88 | uint32_t cached_area_serial_; |
| 89 | uint32_t cached_property_serial_; |
| 90 | char cached_value_[PROP_VALUE_MAX]; |
Tom Cherry | 24a95d2 | 2017-10-18 10:25:39 -0700 | [diff] [blame] | 91 | bool is_read_only_; |
| 92 | const char* read_only_property_; |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 93 | |
| 94 | static void Callback(void* data, const char*, const char* value, uint32_t serial) { |
| 95 | CachedProperty* instance = reinterpret_cast<CachedProperty*>(data); |
| 96 | instance->cached_property_serial_ = serial; |
Tom Cherry | 24a95d2 | 2017-10-18 10:25:39 -0700 | [diff] [blame] | 97 | // Read only properties can be larger than PROP_VALUE_MAX, but also never change value or |
| 98 | // location, thus we return the pointer from the shared memory directly. |
| 99 | if (instance->is_read_only_) { |
| 100 | instance->read_only_property_ = value; |
| 101 | } else { |
| 102 | strlcpy(instance->cached_value_, value, PROP_VALUE_MAX); |
| 103 | } |
Elliott Hughes | 9e27e58 | 2017-03-23 17:42:49 -0700 | [diff] [blame] | 104 | } |
| 105 | }; |