blob: 84ead010bb1b966c87e16813dc32700f1ce763f1 [file] [log] [blame]
Elliott Hughes9e27e582017-03-23 17:42:49 -07001/*
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>
32
33#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
34#include <sys/_system_properties.h>
35
Elliott Hughesa98aa562017-04-11 17:23:37 -070036// Cached system property lookup. For code that needs to read the same property multiple times,
37// this class helps optimize those lookups.
Elliott Hughes9e27e582017-03-23 17:42:49 -070038class CachedProperty {
39 public:
Elliott Hughesa98aa562017-04-11 17:23:37 -070040 // The lifetime of `property_name` must be greater than that of this CachedProperty.
Elliott Hughes9e27e582017-03-23 17:42:49 -070041 CachedProperty(const char* property_name)
42 : property_name_(property_name),
43 prop_info_(nullptr),
44 cached_area_serial_(0),
Tom Cherry24a95d22017-10-18 10:25:39 -070045 cached_property_serial_(0),
46 is_read_only_(strncmp(property_name, "ro.", 3) == 0),
47 read_only_property_(nullptr) {
Elliott Hughes9e27e582017-03-23 17:42:49 -070048 cached_value_[0] = '\0';
49 }
50
Elliott Hughese4ddb3c2017-04-17 14:12:25 -070051 // Returns true if the property has been updated (based on the serial rather than the value)
52 // since the last call to Get.
53 bool DidChange() {
54 uint32_t initial_property_serial_ = cached_property_serial_;
55 Get();
56 return (cached_property_serial_ != initial_property_serial_);
57 }
58
Elliott Hughesa98aa562017-04-11 17:23:37 -070059 // Returns the current value of the underlying system property as cheaply as possible.
60 // The returned pointer is valid until the next call to Get. It is the caller's responsibility
61 // to provide a lock for thread-safety.
Elliott Hughes9e27e582017-03-23 17:42:49 -070062 const char* Get() {
Elliott Hughes9e27e582017-03-23 17:42:49 -070063 // Do we have a `struct prop_info` yet?
64 if (prop_info_ == nullptr) {
65 // `__system_property_find` is expensive, so only retry if a property
66 // has been created since last time we checked.
67 uint32_t property_area_serial = __system_property_area_serial();
68 if (property_area_serial != cached_area_serial_) {
69 prop_info_ = __system_property_find(property_name_);
70 cached_area_serial_ = property_area_serial;
71 }
72 }
73
74 if (prop_info_ != nullptr) {
75 // Only bother re-reading the property if it's actually changed since last time.
76 uint32_t property_serial = __system_property_serial(prop_info_);
77 if (property_serial != cached_property_serial_) {
78 __system_property_read_callback(prop_info_, &CachedProperty::Callback, this);
79 }
80 }
Tom Cherry24a95d22017-10-18 10:25:39 -070081 if (is_read_only_ && read_only_property_ != nullptr) {
82 return read_only_property_;
83 }
Elliott Hughes9e27e582017-03-23 17:42:49 -070084 return cached_value_;
85 }
86
87 private:
Elliott Hughes9e27e582017-03-23 17:42:49 -070088 const char* property_name_;
89 const prop_info* prop_info_;
90 uint32_t cached_area_serial_;
91 uint32_t cached_property_serial_;
92 char cached_value_[PROP_VALUE_MAX];
Tom Cherry24a95d22017-10-18 10:25:39 -070093 bool is_read_only_;
94 const char* read_only_property_;
Elliott Hughes9e27e582017-03-23 17:42:49 -070095
96 static void Callback(void* data, const char*, const char* value, uint32_t serial) {
97 CachedProperty* instance = reinterpret_cast<CachedProperty*>(data);
98 instance->cached_property_serial_ = serial;
Tom Cherry24a95d22017-10-18 10:25:39 -070099 // Read only properties can be larger than PROP_VALUE_MAX, but also never change value or
100 // location, thus we return the pointer from the shared memory directly.
101 if (instance->is_read_only_) {
102 instance->read_only_property_ = value;
103 } else {
104 strlcpy(instance->cached_value_, value, PROP_VALUE_MAX);
105 }
Elliott Hughes9e27e582017-03-23 17:42:49 -0700106 }
107};