xref: /aosp_15_r20/bionic/libc/include/sys/system_properties.h (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2008 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  * All rights reserved.
4*8d67ca89SAndroid Build Coastguard Worker  *
5*8d67ca89SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8d67ca89SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*8d67ca89SAndroid Build Coastguard Worker  * are met:
8*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*8d67ca89SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*8d67ca89SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*8d67ca89SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*8d67ca89SAndroid Build Coastguard Worker  *    distribution.
14*8d67ca89SAndroid Build Coastguard Worker  *
15*8d67ca89SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*8d67ca89SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*8d67ca89SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*8d67ca89SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*8d67ca89SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*8d67ca89SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*8d67ca89SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*8d67ca89SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*8d67ca89SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*8d67ca89SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*8d67ca89SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8d67ca89SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*8d67ca89SAndroid Build Coastguard Worker  */
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker #pragma once
30*8d67ca89SAndroid Build Coastguard Worker 
31*8d67ca89SAndroid Build Coastguard Worker /**
32*8d67ca89SAndroid Build Coastguard Worker  * @file system_properties.h
33*8d67ca89SAndroid Build Coastguard Worker  * @brief System properties.
34*8d67ca89SAndroid Build Coastguard Worker  */
35*8d67ca89SAndroid Build Coastguard Worker 
36*8d67ca89SAndroid Build Coastguard Worker #include <sys/cdefs.h>
37*8d67ca89SAndroid Build Coastguard Worker #include <stdbool.h>
38*8d67ca89SAndroid Build Coastguard Worker #include <stddef.h>
39*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
40*8d67ca89SAndroid Build Coastguard Worker 
41*8d67ca89SAndroid Build Coastguard Worker __BEGIN_DECLS
42*8d67ca89SAndroid Build Coastguard Worker 
43*8d67ca89SAndroid Build Coastguard Worker /** An opaque structure representing a system property. */
44*8d67ca89SAndroid Build Coastguard Worker typedef struct prop_info prop_info;
45*8d67ca89SAndroid Build Coastguard Worker 
46*8d67ca89SAndroid Build Coastguard Worker /**
47*8d67ca89SAndroid Build Coastguard Worker  * The limit on the length of a property value.
48*8d67ca89SAndroid Build Coastguard Worker  * (See PROP_NAME_MAX for property names.)
49*8d67ca89SAndroid Build Coastguard Worker  */
50*8d67ca89SAndroid Build Coastguard Worker #define PROP_VALUE_MAX  92
51*8d67ca89SAndroid Build Coastguard Worker 
52*8d67ca89SAndroid Build Coastguard Worker /**
53*8d67ca89SAndroid Build Coastguard Worker  * Sets system property `name` to `value`, creating the system property if it doesn't already exist.
54*8d67ca89SAndroid Build Coastguard Worker  *
55*8d67ca89SAndroid Build Coastguard Worker  * Returns 0 on success, or -1 on failure.
56*8d67ca89SAndroid Build Coastguard Worker  */
57*8d67ca89SAndroid Build Coastguard Worker int __system_property_set(const char* _Nonnull __name, const char* _Nonnull __value);
58*8d67ca89SAndroid Build Coastguard Worker 
59*8d67ca89SAndroid Build Coastguard Worker /**
60*8d67ca89SAndroid Build Coastguard Worker  * Returns a `prop_info` corresponding system property `name`, or nullptr if it doesn't exist.
61*8d67ca89SAndroid Build Coastguard Worker  * Use __system_property_read_callback() to query the current value.
62*8d67ca89SAndroid Build Coastguard Worker  *
63*8d67ca89SAndroid Build Coastguard Worker  * Property lookup is expensive, so it can be useful to cache the result of this
64*8d67ca89SAndroid Build Coastguard Worker  * function rather than using __system_property_get().
65*8d67ca89SAndroid Build Coastguard Worker  */
66*8d67ca89SAndroid Build Coastguard Worker const prop_info* _Nullable __system_property_find(const char* _Nonnull __name);
67*8d67ca89SAndroid Build Coastguard Worker 
68*8d67ca89SAndroid Build Coastguard Worker /**
69*8d67ca89SAndroid Build Coastguard Worker  * Calls `callback` with a consistent trio of name, value, and serial number
70*8d67ca89SAndroid Build Coastguard Worker  * for property `pi`.
71*8d67ca89SAndroid Build Coastguard Worker  *
72*8d67ca89SAndroid Build Coastguard Worker  * Available since API level 26.
73*8d67ca89SAndroid Build Coastguard Worker  */
74*8d67ca89SAndroid Build Coastguard Worker 
75*8d67ca89SAndroid Build Coastguard Worker #if __BIONIC_AVAILABILITY_GUARD(26)
76*8d67ca89SAndroid Build Coastguard Worker void __system_property_read_callback(const prop_info* _Nonnull __pi,
77*8d67ca89SAndroid Build Coastguard Worker     void (* _Nonnull __callback)(void* _Nullable __cookie, const char* _Nonnull __name, const char* _Nonnull __value, uint32_t __serial),
78*8d67ca89SAndroid Build Coastguard Worker     void* _Nullable __cookie) __INTRODUCED_IN(26);
79*8d67ca89SAndroid Build Coastguard Worker #endif /* __BIONIC_AVAILABILITY_GUARD(26) */
80*8d67ca89SAndroid Build Coastguard Worker 
81*8d67ca89SAndroid Build Coastguard Worker 
82*8d67ca89SAndroid Build Coastguard Worker /**
83*8d67ca89SAndroid Build Coastguard Worker  * Passes a `prop_info` for each system property to the provided
84*8d67ca89SAndroid Build Coastguard Worker  * callback. Use __system_property_read_callback() to read the value of
85*8d67ca89SAndroid Build Coastguard Worker  * any of the properties.
86*8d67ca89SAndroid Build Coastguard Worker  *
87*8d67ca89SAndroid Build Coastguard Worker  * This method is for inspecting and debugging the property system, and not generally useful.
88*8d67ca89SAndroid Build Coastguard Worker  *
89*8d67ca89SAndroid Build Coastguard Worker  * Returns 0 on success, or -1 on failure.
90*8d67ca89SAndroid Build Coastguard Worker  */
91*8d67ca89SAndroid Build Coastguard Worker int __system_property_foreach(void (* _Nonnull __callback)(const prop_info* _Nonnull __pi, void* _Nullable __cookie), void* _Nullable __cookie);
92*8d67ca89SAndroid Build Coastguard Worker 
93*8d67ca89SAndroid Build Coastguard Worker /**
94*8d67ca89SAndroid Build Coastguard Worker  * Waits for the specific system property identified by `pi` to be updated
95*8d67ca89SAndroid Build Coastguard Worker  * past `old_serial`. Waits no longer than `relative_timeout`, or forever
96*8d67ca89SAndroid Build Coastguard Worker  * if `relative_timeout` is null.
97*8d67ca89SAndroid Build Coastguard Worker  *
98*8d67ca89SAndroid Build Coastguard Worker  * If `pi` is null, waits for the global serial number instead.
99*8d67ca89SAndroid Build Coastguard Worker  *
100*8d67ca89SAndroid Build Coastguard Worker  * If you don't know the current serial, use 0.
101*8d67ca89SAndroid Build Coastguard Worker  *
102*8d67ca89SAndroid Build Coastguard Worker  * Returns true and updates `*new_serial_ptr` on success, or false if the call
103*8d67ca89SAndroid Build Coastguard Worker  * timed out.
104*8d67ca89SAndroid Build Coastguard Worker  *
105*8d67ca89SAndroid Build Coastguard Worker  * Available since API level 26.
106*8d67ca89SAndroid Build Coastguard Worker  */
107*8d67ca89SAndroid Build Coastguard Worker struct timespec;
108*8d67ca89SAndroid Build Coastguard Worker 
109*8d67ca89SAndroid Build Coastguard Worker #if __BIONIC_AVAILABILITY_GUARD(26)
110*8d67ca89SAndroid Build Coastguard Worker 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)
111*8d67ca89SAndroid Build Coastguard Worker     __INTRODUCED_IN(26);
112*8d67ca89SAndroid Build Coastguard Worker #endif /* __BIONIC_AVAILABILITY_GUARD(26) */
113*8d67ca89SAndroid Build Coastguard Worker 
114*8d67ca89SAndroid Build Coastguard Worker 
115*8d67ca89SAndroid Build Coastguard Worker /**
116*8d67ca89SAndroid Build Coastguard Worker  * Deprecated: there's no limit on the length of a property name since
117*8d67ca89SAndroid Build Coastguard Worker  * API level 26, though the limit on property values (PROP_VALUE_MAX) remains.
118*8d67ca89SAndroid Build Coastguard Worker  */
119*8d67ca89SAndroid Build Coastguard Worker #define PROP_NAME_MAX   32
120*8d67ca89SAndroid Build Coastguard Worker 
121*8d67ca89SAndroid Build Coastguard Worker /** Deprecated. Use __system_property_foreach() instead. */
122*8d67ca89SAndroid Build Coastguard Worker const prop_info* _Nullable __system_property_find_nth(unsigned __n);
123*8d67ca89SAndroid Build Coastguard Worker /** Deprecated. Use __system_property_read_callback() instead. */
124*8d67ca89SAndroid Build Coastguard Worker int __system_property_read(const prop_info* _Nonnull __pi, char* _Nullable __name, char* _Nonnull __value);
125*8d67ca89SAndroid Build Coastguard Worker /** Deprecated. Use __system_property_read_callback() instead. */
126*8d67ca89SAndroid Build Coastguard Worker int __system_property_get(const char* _Nonnull __name, char* _Nonnull __value);
127*8d67ca89SAndroid Build Coastguard Worker /** Deprecated: use __system_property_wait() instead. */
128*8d67ca89SAndroid Build Coastguard Worker uint32_t __system_property_wait_any(uint32_t __old_serial);
129*8d67ca89SAndroid Build Coastguard Worker 
130*8d67ca89SAndroid Build Coastguard Worker /**
131*8d67ca89SAndroid Build Coastguard Worker  * Reads the global serial number of the system properties _area_.
132*8d67ca89SAndroid Build Coastguard Worker  *
133*8d67ca89SAndroid Build Coastguard Worker  * Called to predict if a series of cached __system_property_find()
134*8d67ca89SAndroid Build Coastguard Worker  * objects will have seen __system_property_serial() values change.
135*8d67ca89SAndroid Build Coastguard Worker  * Also aids the converse, as changes in the global serial can
136*8d67ca89SAndroid Build Coastguard Worker  * also be used to predict if a failed __system_property_find()
137*8d67ca89SAndroid Build Coastguard Worker  * could in turn now find a new object; thus preventing the
138*8d67ca89SAndroid Build Coastguard Worker  * cycles of effort to poll __system_property_find().
139*8d67ca89SAndroid Build Coastguard Worker  *
140*8d67ca89SAndroid Build Coastguard Worker  * Typically called at beginning of a cache cycle to signal if _any_ possible
141*8d67ca89SAndroid Build Coastguard Worker  * changes have occurred since last. If there is, one may check each individual
142*8d67ca89SAndroid Build Coastguard Worker  * __system_property_serial() to confirm dirty, or __system_property_find()
143*8d67ca89SAndroid Build Coastguard Worker  * to check if the property now exists. If a call to __system_property_add()
144*8d67ca89SAndroid Build Coastguard Worker  * or __system_property_update() has completed between two calls to
145*8d67ca89SAndroid Build Coastguard Worker  * __system_property_area_serial() then the second call will return a larger
146*8d67ca89SAndroid Build Coastguard Worker  * value than the first call. Beware of race conditions as changes to the
147*8d67ca89SAndroid Build Coastguard Worker  * properties are not atomic, the main value of this call is to determine
148*8d67ca89SAndroid Build Coastguard Worker  * whether the expensive __system_property_find() is worth retrying to see if
149*8d67ca89SAndroid Build Coastguard Worker  * a property now exists.
150*8d67ca89SAndroid Build Coastguard Worker  *
151*8d67ca89SAndroid Build Coastguard Worker  * Returns the serial number on success, -1 on error.
152*8d67ca89SAndroid Build Coastguard Worker  */
153*8d67ca89SAndroid Build Coastguard Worker uint32_t __system_property_area_serial(void);
154*8d67ca89SAndroid Build Coastguard Worker 
155*8d67ca89SAndroid Build Coastguard Worker /**
156*8d67ca89SAndroid Build Coastguard Worker  * Reads the serial number of a specific system property previously returned by
157*8d67ca89SAndroid Build Coastguard Worker  * __system_property_find(). This is a cheap way to check whether a system
158*8d67ca89SAndroid Build Coastguard Worker  * property has changed or not.
159*8d67ca89SAndroid Build Coastguard Worker  *
160*8d67ca89SAndroid Build Coastguard Worker  * Returns the serial number on success, -1 on error.
161*8d67ca89SAndroid Build Coastguard Worker  */
162*8d67ca89SAndroid Build Coastguard Worker uint32_t __system_property_serial(const prop_info* _Nonnull __pi);
163*8d67ca89SAndroid Build Coastguard Worker 
164*8d67ca89SAndroid Build Coastguard Worker //
165*8d67ca89SAndroid Build Coastguard Worker // libc implementation detail.
166*8d67ca89SAndroid Build Coastguard Worker //
167*8d67ca89SAndroid Build Coastguard Worker 
168*8d67ca89SAndroid Build Coastguard Worker /**
169*8d67ca89SAndroid Build Coastguard Worker  * Initializes the system properties area in read-only mode.
170*8d67ca89SAndroid Build Coastguard Worker  *
171*8d67ca89SAndroid Build Coastguard Worker  * This is called automatically during libc initialization,
172*8d67ca89SAndroid Build Coastguard Worker  * so user code should never need to call this.
173*8d67ca89SAndroid Build Coastguard Worker  *
174*8d67ca89SAndroid Build Coastguard Worker  * Returns 0 on success, -1 otherwise.
175*8d67ca89SAndroid Build Coastguard Worker  */
176*8d67ca89SAndroid Build Coastguard Worker int __system_properties_init(void);
177*8d67ca89SAndroid Build Coastguard Worker 
178*8d67ca89SAndroid Build Coastguard Worker //
179*8d67ca89SAndroid Build Coastguard Worker // init implementation details.
180*8d67ca89SAndroid Build Coastguard Worker //
181*8d67ca89SAndroid Build Coastguard Worker 
182*8d67ca89SAndroid Build Coastguard Worker #define PROP_SERVICE_NAME "property_service"
183*8d67ca89SAndroid Build Coastguard Worker #define PROP_SERVICE_FOR_SYSTEM_NAME "property_service_for_system"
184*8d67ca89SAndroid Build Coastguard Worker #define PROP_DIRNAME "/dev/__properties__"
185*8d67ca89SAndroid Build Coastguard Worker 
186*8d67ca89SAndroid Build Coastguard Worker // Messages sent to init.
187*8d67ca89SAndroid Build Coastguard Worker #define PROP_MSG_SETPROP 1
188*8d67ca89SAndroid Build Coastguard Worker #define PROP_MSG_SETPROP2 0x00020001
189*8d67ca89SAndroid Build Coastguard Worker 
190*8d67ca89SAndroid Build Coastguard Worker // Status codes returned by init (but not passed from libc to the caller).
191*8d67ca89SAndroid Build Coastguard Worker #define PROP_SUCCESS 0
192*8d67ca89SAndroid Build Coastguard Worker #define PROP_ERROR_READ_CMD 0x0004
193*8d67ca89SAndroid Build Coastguard Worker #define PROP_ERROR_READ_DATA 0x0008
194*8d67ca89SAndroid Build Coastguard Worker #define PROP_ERROR_READ_ONLY_PROPERTY 0x000B
195*8d67ca89SAndroid Build Coastguard Worker #define PROP_ERROR_INVALID_NAME 0x0010
196*8d67ca89SAndroid Build Coastguard Worker #define PROP_ERROR_INVALID_VALUE 0x0014
197*8d67ca89SAndroid Build Coastguard Worker #define PROP_ERROR_PERMISSION_DENIED 0x0018
198*8d67ca89SAndroid Build Coastguard Worker #define PROP_ERROR_INVALID_CMD 0x001B
199*8d67ca89SAndroid Build Coastguard Worker #define PROP_ERROR_HANDLE_CONTROL_MESSAGE 0x0020
200*8d67ca89SAndroid Build Coastguard Worker #define PROP_ERROR_SET_FAILED 0x0024
201*8d67ca89SAndroid Build Coastguard Worker 
202*8d67ca89SAndroid Build Coastguard Worker /**
203*8d67ca89SAndroid Build Coastguard Worker  * Initializes the area to be used to store properties.
204*8d67ca89SAndroid Build Coastguard Worker  *
205*8d67ca89SAndroid Build Coastguard Worker  * Can only be done by the process that has write access to the property area,
206*8d67ca89SAndroid Build Coastguard Worker  * typically init.
207*8d67ca89SAndroid Build Coastguard Worker  *
208*8d67ca89SAndroid Build Coastguard Worker  * See __system_properties_init() for the equivalent for all other processes.
209*8d67ca89SAndroid Build Coastguard Worker  */
210*8d67ca89SAndroid Build Coastguard Worker int __system_property_area_init(void);
211*8d67ca89SAndroid Build Coastguard Worker 
212*8d67ca89SAndroid Build Coastguard Worker /**
213*8d67ca89SAndroid Build Coastguard Worker  * Adds a new system property.
214*8d67ca89SAndroid Build Coastguard Worker  * Can only be done by the process that has write access to the property area --
215*8d67ca89SAndroid Build Coastguard Worker  * typically init -- which must handle sequencing to ensure that only one property is
216*8d67ca89SAndroid Build Coastguard Worker  * updated at a time.
217*8d67ca89SAndroid Build Coastguard Worker  *
218*8d67ca89SAndroid Build Coastguard Worker  * Returns 0 on success, -1 if the property area is full.
219*8d67ca89SAndroid Build Coastguard Worker  */
220*8d67ca89SAndroid Build Coastguard Worker int __system_property_add(const char* _Nonnull __name, unsigned int __name_length, const char* _Nonnull __value, unsigned int __value_length);
221*8d67ca89SAndroid Build Coastguard Worker 
222*8d67ca89SAndroid Build Coastguard Worker /**
223*8d67ca89SAndroid Build Coastguard Worker  * Updates the value of a system property returned by __system_property_find().
224*8d67ca89SAndroid Build Coastguard Worker  * Can only be done by the process that has write access to the property area --
225*8d67ca89SAndroid Build Coastguard Worker  * typically init -- which must handle sequencing to ensure that only one property is
226*8d67ca89SAndroid Build Coastguard Worker  * updated at a time.
227*8d67ca89SAndroid Build Coastguard Worker  *
228*8d67ca89SAndroid Build Coastguard Worker  * Returns 0 on success, -1 if the parameters are incorrect.
229*8d67ca89SAndroid Build Coastguard Worker  */
230*8d67ca89SAndroid Build Coastguard Worker int __system_property_update(prop_info* _Nonnull __pi, const char* _Nonnull __value, unsigned int __value_length);
231*8d67ca89SAndroid Build Coastguard Worker 
232*8d67ca89SAndroid Build Coastguard Worker /**
233*8d67ca89SAndroid Build Coastguard Worker  * Reloads the system properties from disk.
234*8d67ca89SAndroid Build Coastguard Worker  * Not intended for use by any apps except the Zygote.
235*8d67ca89SAndroid Build Coastguard Worker  * Should only be called from the main thread.
236*8d67ca89SAndroid Build Coastguard Worker  *
237*8d67ca89SAndroid Build Coastguard Worker  * Pointers received from functions such as __system_property_find()
238*8d67ca89SAndroid Build Coastguard Worker  * may be invalidated by calls to this function.
239*8d67ca89SAndroid Build Coastguard Worker  *
240*8d67ca89SAndroid Build Coastguard Worker  * Returns 0 on success, -1 otherwise.
241*8d67ca89SAndroid Build Coastguard Worker  *
242*8d67ca89SAndroid Build Coastguard Worker  * Available since API level 35.
243*8d67ca89SAndroid Build Coastguard Worker  */
244*8d67ca89SAndroid Build Coastguard Worker 
245*8d67ca89SAndroid Build Coastguard Worker #if __BIONIC_AVAILABILITY_GUARD(35)
246*8d67ca89SAndroid Build Coastguard Worker int __system_properties_zygote_reload(void) __INTRODUCED_IN(35);
247*8d67ca89SAndroid Build Coastguard Worker #endif /* __BIONIC_AVAILABILITY_GUARD(35) */
248*8d67ca89SAndroid Build Coastguard Worker 
249*8d67ca89SAndroid Build Coastguard Worker 
250*8d67ca89SAndroid Build Coastguard Worker /**
251*8d67ca89SAndroid Build Coastguard Worker  * Deprecated: previously for testing, but now that SystemProperties is its own
252*8d67ca89SAndroid Build Coastguard Worker  * testable class, there is never a reason to call this function and its
253*8d67ca89SAndroid Build Coastguard Worker  * implementation simply returns -1.
254*8d67ca89SAndroid Build Coastguard Worker  */
255*8d67ca89SAndroid Build Coastguard Worker int __system_property_set_filename(const char* _Nullable __unused __filename);
256*8d67ca89SAndroid Build Coastguard Worker 
257*8d67ca89SAndroid Build Coastguard Worker __END_DECLS
258