2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/cdefs.h>
23 #if __has_include(<sys/system_properties.h>)
24 #include <sys/system_properties.h>
26 #define PROP_VALUE_MAX 92
36 // See <android-base/properties.h> for better API.
39 #define PROPERTY_KEY_MAX PROP_NAME_MAX
40 #define PROPERTY_VALUE_MAX PROP_VALUE_MAX
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).
46 ** If the property read fails or returns an empty value, the default
47 ** value is used (if nonnull).
49 int property_get(const char* key
, char* value
, const char* default_value
);
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.
54 * The following is considered to be true (1):
55 ** "1", "true", "y", "yes", "on"
57 ** The following is considered to be false (0):
58 ** "0", "false", "n", "no", "off"
60 ** The conversion is whitespace-sensitive (e.g. " off" will not be false).
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.
65 int8_t property_get_bool(const char *key
, int8_t default_value
);
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.
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
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
80 ** Leading/trailing whitespace is ignored. Overflow/underflow will cause
81 ** numeric conversion to fail.
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.
86 int64_t property_get_int64(const char *key
, int64_t default_value
);
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.
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
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
101 ** Leading/trailing whitespace is ignored. Overflow/underflow will cause
102 ** numeric conversion to fail.
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.
107 int32_t property_get_int32(const char *key
, int32_t default_value
);
109 /* property_set: returns 0 on success, < 0 on failure
111 int property_set(const char *key
, const char *value
);
113 int property_list(void (*propfn
)(const char *key
, const char *value
, void *cookie
), void *cookie
);
115 #if defined(__BIONIC_FORTIFY)
116 #define __property_get_err_str "property_get() called with too small of a buffer"
118 #if defined(__clang__)
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
129 #else /* defined(__clang__) */
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
);
135 __BIONIC_FORTIFY_INLINE
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();
141 return __property_get_real(key
, value
, default_value
);
144 #endif /* defined(__clang__) */
146 #undef __property_get_err_str
147 #endif /* defined(__BIONIC_FORTIFY) */