1 /*
2  * Copyright (C) 2006 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 
17 #pragma once
18 
19 #include <sys/cdefs.h>
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #if __has_include(<sys/system_properties.h>)
24 #include <sys/system_properties.h>
25 #else
26 #define PROP_VALUE_MAX 92
27 #endif
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 //
34 // Deprecated.
35 //
36 // See <android-base/properties.h> for better API.
37 //
38 
39 #define PROPERTY_KEY_MAX PROP_NAME_MAX
40 #define PROPERTY_VALUE_MAX PROP_VALUE_MAX
41 
42 /* property_get: returns the length of the value which will never be
43 ** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated.
44 ** (the length does not include the terminating zero).
45 **
46 ** If the property read fails or returns an empty value, the default
47 ** value is used (if nonnull).
48 */
49 int property_get(const char* key, char* value, const char* default_value);
50 
51 /* property_get_bool: returns the value of key coerced into a
52 ** boolean. If the property is not set, then the default value is returned.
53 **
54 * The following is considered to be true (1):
55 **   "1", "true", "y", "yes", "on"
56 **
57 ** The following is considered to be false (0):
58 **   "0", "false", "n", "no", "off"
59 **
60 ** The conversion is whitespace-sensitive (e.g. " off" will not be false).
61 **
62 ** If no property with this key is set (or the key is NULL) or the boolean
63 ** conversion fails, the default value is returned.
64 **/
65 int8_t property_get_bool(const char *key, int8_t default_value);
66 
67 /* property_get_int64: returns the value of key truncated and coerced into a
68 ** int64_t. If the property is not set, then the default value is used.
69 **
70 ** The numeric conversion is identical to strtoimax with the base inferred:
71 ** - All digits up to the first non-digit characters are read
72 ** - The longest consecutive prefix of digits is converted to a long
73 **
74 ** Valid strings of digits are:
75 ** - An optional sign character + or -
76 ** - An optional prefix indicating the base (otherwise base 10 is assumed)
77 **   -- 0 prefix is octal
78 **   -- 0x / 0X prefix is hex
79 **
80 ** Leading/trailing whitespace is ignored. Overflow/underflow will cause
81 ** numeric conversion to fail.
82 **
83 ** If no property with this key is set (or the key is NULL) or the numeric
84 ** conversion fails, the default value is returned.
85 **/
86 int64_t property_get_int64(const char *key, int64_t default_value);
87 
88 /* property_get_int32: returns the value of key truncated and coerced into an
89 ** int32_t. If the property is not set, then the default value is used.
90 **
91 ** The numeric conversion is identical to strtoimax with the base inferred:
92 ** - All digits up to the first non-digit characters are read
93 ** - The longest consecutive prefix of digits is converted to a long
94 **
95 ** Valid strings of digits are:
96 ** - An optional sign character + or -
97 ** - An optional prefix indicating the base (otherwise base 10 is assumed)
98 **   -- 0 prefix is octal
99 **   -- 0x / 0X prefix is hex
100 **
101 ** Leading/trailing whitespace is ignored. Overflow/underflow will cause
102 ** numeric conversion to fail.
103 **
104 ** If no property with this key is set (or the key is NULL) or the numeric
105 ** conversion fails, the default value is returned.
106 **/
107 int32_t property_get_int32(const char *key, int32_t default_value);
108 
109 /* property_set: returns 0 on success, < 0 on failure
110 */
111 int property_set(const char *key, const char *value);
112 
113 int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie);
114 
115 #if defined(__BIONIC_FORTIFY)
116 #define __property_get_err_str "property_get() called with too small of a buffer"
117 
118 #if defined(__clang__)
119 
120 /* Some projects use -Weverything; diagnose_if is clang-specific. */
121 #pragma clang diagnostic push
122 #pragma clang diagnostic ignored "-Wgcc-compat"
123 int property_get(const char* key, char* value, const char* default_value)
124     __clang_error_if(__bos(value) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
125                          __bos(value) < PROPERTY_VALUE_MAX,
126                      __property_get_err_str);
127 #pragma clang diagnostic pop
128 
129 #else /* defined(__clang__) */
130 
131 extern int __property_get_real(const char *, char *, const char *)
132     __asm__(__USER_LABEL_PREFIX__ "property_get");
133 __errordecl(__property_get_too_small_error, __property_get_err_str);
134 
135 __BIONIC_FORTIFY_INLINE
property_get(const char * key,char * value,const char * default_value)136 int property_get(const char *key, char *value, const char *default_value) {
137     size_t bos = __bos(value);
138     if (bos < PROPERTY_VALUE_MAX) {
139         __property_get_too_small_error();
140     }
141     return __property_get_real(key, value, default_value);
142 }
143 
144 #endif /* defined(__clang__) */
145 
146 #undef __property_get_err_str
147 #endif /* defined(__BIONIC_FORTIFY) */
148 
149 #ifdef __cplusplus
150 }
151 #endif
152