The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 29 | #pragma once |
| 30 | |
| 31 | /** |
| 32 | * @file system_properties.h |
| 33 | * @brief System properties. |
| 34 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
David 'Digit' Turner | 49f0a8f | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 36 | #include <sys/cdefs.h> |
Dimitry Ivanov | 41a3a6f | 2017-02-16 15:34:21 -0800 | [diff] [blame] | 37 | #include <stdbool.h> |
Dimitry Ivanov | 16b2a4d | 2017-01-24 20:43:29 +0000 | [diff] [blame] | 38 | #include <stddef.h> |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 39 | #include <stdint.h> |
David 'Digit' Turner | 49f0a8f | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 40 | |
| 41 | __BEGIN_DECLS |
| 42 | |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 43 | /** An opaque structure representing a system property. */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 44 | typedef struct prop_info prop_info; |
| 45 | |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 46 | /** |
| 47 | * The limit on the length of a property value. |
| 48 | * (See PROP_NAME_MAX for property names.) |
| 49 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 50 | #define PROP_VALUE_MAX 92 |
| 51 | |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 52 | /** |
Elliott Hughes | ff26a16 | 2017-08-17 22:34:21 +0000 | [diff] [blame] | 53 | * Sets system property `name` to `value`, creating the system property if it doesn't already exist. |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 54 | * |
| 55 | * Returns 0 on success, or -1 on failure. |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 56 | */ |
zijunzhao | a505b2d | 2023-05-17 00:20:12 +0000 | [diff] [blame] | 57 | int __system_property_set(const char* _Nonnull __name, const char* _Nonnull __value); |
Brad Fitzpatrick | 4399df8 | 2011-03-10 15:52:49 -0800 | [diff] [blame] | 58 | |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 59 | /** |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 60 | * Returns a `prop_info` corresponding system property `name`, or nullptr if it doesn't exist. |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 61 | * Use __system_property_read_callback() to query the current value. |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 62 | * |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 63 | * Property lookup is expensive, so it can be useful to cache the result of this |
| 64 | * function rather than using __system_property_get(). |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 65 | */ |
zijunzhao | a505b2d | 2023-05-17 00:20:12 +0000 | [diff] [blame] | 66 | const prop_info* _Nullable __system_property_find(const char* _Nonnull __name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 67 | |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 68 | /** |
| 69 | * Calls `callback` with a consistent trio of name, value, and serial number |
| 70 | * for property `pi`. |
| 71 | * |
| 72 | * Available since API level 26. |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 73 | */ |
zijunzhao | a505b2d | 2023-05-17 00:20:12 +0000 | [diff] [blame] | 74 | void __system_property_read_callback(const prop_info* _Nonnull __pi, |
| 75 | void (* _Nonnull __callback)(void* _Nullable __cookie, const char* _Nonnull __name, const char* _Nonnull __value, uint32_t __serial), |
| 76 | void* _Nullable __cookie) __INTRODUCED_IN(26); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 77 | |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 78 | /** |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 79 | * Passes a `prop_info` for each system property to the provided |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 80 | * callback. Use __system_property_read_callback() to read the value of |
| 81 | * any of the properties. |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 82 | * |
| 83 | * This method is for inspecting and debugging the property system, and not generally useful. |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 84 | * |
| 85 | * Returns 0 on success, or -1 on failure. |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 86 | */ |
Elliott Hughes | 655e430 | 2023-06-16 12:39:33 -0700 | [diff] [blame] | 87 | int __system_property_foreach(void (* _Nonnull __callback)(const prop_info* _Nonnull __pi, void* _Nullable __cookie), void* _Nullable __cookie); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 88 | |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 89 | /** |
Dimitry Ivanov | 41a3a6f | 2017-02-16 15:34:21 -0800 | [diff] [blame] | 90 | * Waits for the specific system property identified by `pi` to be updated |
| 91 | * past `old_serial`. Waits no longer than `relative_timeout`, or forever |
zijunzhao | a505b2d | 2023-05-17 00:20:12 +0000 | [diff] [blame] | 92 | * if `relative_timeout` is null. |
Dimitry Ivanov | 41a3a6f | 2017-02-16 15:34:21 -0800 | [diff] [blame] | 93 | * |
| 94 | * If `pi` is null, waits for the global serial number instead. |
| 95 | * |
| 96 | * If you don't know the current serial, use 0. |
| 97 | * |
| 98 | * Returns true and updates `*new_serial_ptr` on success, or false if the call |
| 99 | * timed out. |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 100 | * |
| 101 | * Available since API level 26. |
Dimitry Ivanov | 41a3a6f | 2017-02-16 15:34:21 -0800 | [diff] [blame] | 102 | */ |
| 103 | struct timespec; |
zijunzhao | a505b2d | 2023-05-17 00:20:12 +0000 | [diff] [blame] | 104 | bool __system_property_wait(const prop_info* _Nullable __pi, uint32_t __old_serial, uint32_t* _Nonnull __new_serial_ptr, const struct timespec* _Nullable __relative_timeout) |
Josh Gao | 2e8e5e6 | 2017-04-20 12:58:31 -0700 | [diff] [blame] | 105 | __INTRODUCED_IN(26); |
Dimitry Ivanov | 41a3a6f | 2017-02-16 15:34:21 -0800 | [diff] [blame] | 106 | |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Deprecated: there's no limit on the length of a property name since |
| 109 | * API level 26, though the limit on property values (PROP_VALUE_MAX) remains. |
| 110 | */ |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 111 | #define PROP_NAME_MAX 32 |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 112 | |
Elliott Hughes | 620eec1 | 2024-08-07 17:59:53 +0000 | [diff] [blame] | 113 | /** Deprecated. Use __system_property_foreach() instead. */ |
| 114 | const prop_info* _Nullable __system_property_find_nth(unsigned __n); |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 115 | /** Deprecated. Use __system_property_read_callback() instead. */ |
zijunzhao | a505b2d | 2023-05-17 00:20:12 +0000 | [diff] [blame] | 116 | int __system_property_read(const prop_info* _Nonnull __pi, char* _Nullable __name, char* _Nonnull __value); |
Elliott Hughes | 3772ae4 | 2024-05-16 18:45:03 +0000 | [diff] [blame] | 117 | /** Deprecated. Use __system_property_read_callback() instead. */ |
zijunzhao | a505b2d | 2023-05-17 00:20:12 +0000 | [diff] [blame] | 118 | int __system_property_get(const char* _Nonnull __name, char* _Nonnull __value); |
Elliott Hughes | 620eec1 | 2024-08-07 17:59:53 +0000 | [diff] [blame] | 119 | /** Deprecated: use __system_property_wait() instead. */ |
| 120 | uint32_t __system_property_wait_any(uint32_t __old_serial); |
| 121 | |
| 122 | /** |
| 123 | * Reads the global serial number of the system properties _area_. |
| 124 | * |
| 125 | * Called to predict if a series of cached __system_property_find() |
| 126 | * objects will have seen __system_property_serial() values change. |
| 127 | * Also aids the converse, as changes in the global serial can |
| 128 | * also be used to predict if a failed __system_property_find() |
| 129 | * could in turn now find a new object; thus preventing the |
| 130 | * cycles of effort to poll __system_property_find(). |
| 131 | * |
| 132 | * Typically called at beginning of a cache cycle to signal if _any_ possible |
| 133 | * changes have occurred since last. If there is, one may check each individual |
| 134 | * __system_property_serial() to confirm dirty, or __system_property_find() |
| 135 | * to check if the property now exists. If a call to __system_property_add() |
| 136 | * or __system_property_update() has completed between two calls to |
| 137 | * __system_property_area_serial() then the second call will return a larger |
| 138 | * value than the first call. Beware of race conditions as changes to the |
| 139 | * properties are not atomic, the main value of this call is to determine |
| 140 | * whether the expensive __system_property_find() is worth retrying to see if |
| 141 | * a property now exists. |
| 142 | * |
| 143 | * Returns the serial number on success, -1 on error. |
| 144 | */ |
| 145 | uint32_t __system_property_area_serial(void); |
| 146 | |
| 147 | /** |
| 148 | * Reads the serial number of a specific system property previously returned by |
| 149 | * __system_property_find(). This is a cheap way to check whether a system |
| 150 | * property has changed or not. |
| 151 | * |
| 152 | * Returns the serial number on success, -1 on error. |
| 153 | */ |
| 154 | uint32_t __system_property_serial(const prop_info* _Nonnull __pi); |
| 155 | |
| 156 | // |
| 157 | // libc implementation detail. |
| 158 | // |
| 159 | |
| 160 | /** |
| 161 | * Initializes the system properties area in read-only mode. |
| 162 | * |
| 163 | * This is called automatically during libc initialization, |
| 164 | * so user code should never need to call this. |
| 165 | * |
| 166 | * Returns 0 on success, -1 otherwise. |
| 167 | */ |
| 168 | int __system_properties_init(void); |
| 169 | |
| 170 | // |
| 171 | // init implementation details. |
| 172 | // |
| 173 | |
| 174 | #define PROP_SERVICE_NAME "property_service" |
| 175 | #define PROP_SERVICE_FOR_SYSTEM_NAME "property_service_for_system" |
| 176 | #define PROP_DIRNAME "/dev/__properties__" |
| 177 | |
| 178 | // Messages sent to init. |
| 179 | #define PROP_MSG_SETPROP 1 |
| 180 | #define PROP_MSG_SETPROP2 0x00020001 |
| 181 | |
| 182 | // Status codes returned by init (but not passed from libc to the caller). |
| 183 | #define PROP_SUCCESS 0 |
| 184 | #define PROP_ERROR_READ_CMD 0x0004 |
| 185 | #define PROP_ERROR_READ_DATA 0x0008 |
| 186 | #define PROP_ERROR_READ_ONLY_PROPERTY 0x000B |
| 187 | #define PROP_ERROR_INVALID_NAME 0x0010 |
| 188 | #define PROP_ERROR_INVALID_VALUE 0x0014 |
| 189 | #define PROP_ERROR_PERMISSION_DENIED 0x0018 |
| 190 | #define PROP_ERROR_INVALID_CMD 0x001B |
| 191 | #define PROP_ERROR_HANDLE_CONTROL_MESSAGE 0x0020 |
| 192 | #define PROP_ERROR_SET_FAILED 0x0024 |
| 193 | |
| 194 | /** |
| 195 | * Initializes the area to be used to store properties. |
| 196 | * |
| 197 | * Can only be done by the process that has write access to the property area, |
| 198 | * typically init. |
| 199 | * |
| 200 | * See __system_properties_init() for the equivalent for all other processes. |
| 201 | */ |
| 202 | int __system_property_area_init(void); |
| 203 | |
| 204 | /** |
| 205 | * Adds a new system property. |
| 206 | * Can only be done by the process that has write access to the property area -- |
| 207 | * typically init -- which must handle sequencing to ensure that only one property is |
| 208 | * updated at a time. |
| 209 | * |
| 210 | * Returns 0 on success, -1 if the property area is full. |
| 211 | */ |
| 212 | int __system_property_add(const char* _Nonnull __name, unsigned int __name_length, const char* _Nonnull __value, unsigned int __value_length); |
| 213 | |
| 214 | /** |
| 215 | * Updates the value of a system property returned by __system_property_find(). |
| 216 | * Can only be done by the process that has write access to the property area -- |
| 217 | * typically init -- which must handle sequencing to ensure that only one property is |
| 218 | * updated at a time. |
| 219 | * |
| 220 | * Returns 0 on success, -1 if the parameters are incorrect. |
| 221 | */ |
| 222 | int __system_property_update(prop_info* _Nonnull __pi, const char* _Nonnull __value, unsigned int __value_length); |
| 223 | |
| 224 | /** |
| 225 | * Reloads the system properties from disk. |
| 226 | * Not intended for use by any apps except the Zygote. |
| 227 | * Should only be called from the main thread. |
| 228 | * |
| 229 | * Pointers received from functions such as __system_property_find() |
| 230 | * may be invalidated by calls to this function. |
| 231 | * |
| 232 | * Returns 0 on success, -1 otherwise. |
| 233 | * |
| 234 | * Available since API level 35. |
| 235 | */ |
| 236 | int __system_properties_zygote_reload(void) __INTRODUCED_IN(35); |
| 237 | |
| 238 | /** |
| 239 | * Deprecated: previously for testing, but now that SystemProperties is its own |
| 240 | * testable class, there is never a reason to call this function and its |
| 241 | * implementation simply returns -1. |
| 242 | */ |
| 243 | int __system_property_set_filename(const char* _Nullable __unused __filename); |
Elliott Hughes | a0d374d | 2017-02-10 18:13:46 -0800 | [diff] [blame] | 244 | |
David 'Digit' Turner | 49f0a8f | 2010-02-09 14:05:43 -0800 | [diff] [blame] | 245 | __END_DECLS |